def f(filename, raw=False): if not filename.endswith('.json'): return name = filename.rpartition('.')[0] filename = getFilePath(path, filename, '') if not os.path.exists(filename): return mtime = cache[name][0] if name in cache else 0 st_mtime = os.stat(filename).st_mtime if mtime < st_mtime: try: with open(filename, 'r', encoding='utf-8') as fp: text = fp.read() item = json.loads(text, encoding='utf-8') name = item['name'] # should equals name if compareVersion(version, item['version']) < 0: return 'Incompatible version' if raw else None cache[name] = (st_mtime, text, getBrief(item)) except Exception as e: return str(e) if raw else None return cache[name][1] if raw else cache[name][2]
def update(): # make temp dir if not os.path.exists('./update_tmp'): os.mkdir('./update_tmp') v = getVersion() current_v = json.load('./package.json')['version'] print('current version==', current_v) if compareVersion(v, current_v) <= 0: print('已是最新版本') result = '已是最新版本' else: url_new_version = ufile + v + '.zip' # download zip print('downloading from ', url_new_version) url = url_new_version r = requests.get(url) with open("./update_tmp/tmp.zip", "wb") as code: code.write(r.content) # extract zip z = zipfile.ZipFile('./update_tmp/tmp.zip', 'r') z.extractall(path='./update_tmp') z.close() # copy files print('copying files') py_files = os.listdir('./update_tmp') for f in py_files: if f[-3:] == '.py': # copyfile('./update_tmp/'+f,'./python/update_tmp/'+f) compile_pyc(path='./update_tmp/') copyfile('./update_tmp/__pycache__/' + f[:-3] + '.pyc', './' + f[:-3] + '.pyc') # recompile pyc elif f[-4:] == '.txt': copyfile('./update_tmp/' + f, './' + f) print('升级完成,请重启软件') #clean temp files shutil.rmtree('./update_tmp') result = '升级完成,请重启软件' return result