Last active
January 3, 2020 15:08
-
-
Save wodeguaiguai/384f9da20465ff00174af65b4dae1b64 to your computer and use it in GitHub Desktop.
update.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# -*- coding: utf-8 -*- | |
# update.py | |
# @author wodeguaiguai | |
# @description | |
# @created 2020-01-02T21:49:39.000Z+08:00 | |
# @last-modified 2020-01-03T23:08:13.251Z+08:00 | |
# | |
import os | |
import sys | |
import hashlib | |
import json | |
import requests | |
BLACK_FILE_LIST = ['.log', '.pdb', 'config.ini', '.bat', '.iobj', '.bsc', '.exp', '.lib', 'update.json'] | |
BLACK_DIR_LIST = ['log', 'blob_storage', 'VideoDecodeStats'] | |
BASE_URL = 'http://127.0.0.1:8887' | |
def get_md5(file): | |
if not os.path.exists(file): | |
return '' | |
md5obj = hashlib.md5() | |
with open(file, 'rb') as f: | |
md5obj.update(f.read()) | |
return md5obj.hexdigest() | |
def is_black_file(file): | |
for item in BLACK_FILE_LIST: | |
if file.lower().endswith(item.lower()): | |
return True | |
return False | |
def in_back_dir(file): | |
for item in BLACK_DIR_LIST: | |
if file.lower().endswith(item.lower()): | |
return True | |
return False | |
def get_tag_info(): | |
tag_info = '' | |
try: | |
with open('tag.txt', 'r') as f: | |
tag_info = f.read().strip() | |
except Exception as identifier: | |
print('exception:', identifier) | |
return tag_info | |
def create_update_info(base_path='.'): | |
file_list = [] | |
base_dir = os.path.abspath(base_path) | |
for (root, dirs, files) in os.walk(base_dir): | |
if in_back_dir(root): | |
continue | |
for file in files: | |
if is_black_file(file): | |
continue | |
file_path = os.path.join(root, file) | |
file_list.append({ | |
'file': file, | |
'path': os.path.dirname(os.path.relpath(file_path, base_dir)), | |
'md5': get_md5(file_path) | |
}) | |
with open(os.path.join(base_dir, 'update.json'), 'w') as f: | |
json.dump({ | |
'tag': get_tag_info(), | |
'file_list': file_list | |
}, f, indent=2) | |
def load_online_info(): | |
try: | |
ret = requests.get('%s/update.json' % (BASE_URL)) | |
update_info = ret.json() | |
with open('newest.json', 'w') as f: | |
json.dump(update_info, f) | |
except Exception as err: | |
print('exception:', err) | |
update_info = {} | |
return update_info | |
def check_update(): | |
online_info = load_online_info() | |
if not online_info: | |
return | |
base_dir = os.path.abspath('.') | |
try: | |
for online_item in online_info['file_list']: | |
if get_md5(os.path.join(base_dir, online_item['path'], online_item['file'])) == online_item['md5']: | |
continue | |
if not update_file(online_item): | |
return | |
json_file = os.path.join(base_dir, 'update.json') | |
if os.path.exists(json_file): | |
os.remove(json_file) | |
os.rename(os.path.join(base_dir, 'newest.json'), json_file) | |
except Exception as identifier: | |
print('exception:', identifier) | |
print('update finish...') | |
def update_file(online_item): | |
print('update file: ', online_item['file']) | |
file_url = '%s/%s' % (BASE_URL, os.path.join(online_item['path'], online_item['file'])) | |
file_path = os.path.join(os.path.abspath('.'), online_item['path']) | |
if not os.path.exists(file_path): | |
os.mkdir(file_path) | |
file_path = os.path.join(file_path, online_item['file']) | |
new_file_path = '%s_new' % (file_path) | |
old_file_path = '%s_old' % (file_path) | |
with requests.get(file_url, stream=True) as r: | |
r.raise_for_status() | |
with open(new_file_path, 'wb') as f: | |
for chunk in r.iter_content(chunk_size=8192): | |
if chunk: | |
f.write(chunk) | |
if get_md5(new_file_path) == online_item['md5']: | |
if os.path.exists(old_file_path): | |
os.remove(old_file_path) | |
if os.path.exists(file_path): | |
os.rename(file_path, old_file_path) | |
os.rename(new_file_path, file_path) | |
return True | |
os.remove(new_file_path) | |
return False | |
if __name__ == "__main__": | |
if len(sys.argv) > 1: | |
base_path = sys.argv[1].strip() | |
if os.path.exists(base_path): | |
create_update_info(base_path) | |
else: | |
create_update_info() | |
else: | |
check_update() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment