Created
April 29, 2024 08:16
-
-
Save holmeszyx/de13b636a6c68e33b9a4b1f64ac85989 to your computer and use it in GitHub Desktop.
oh-my-rime one-key upgrade
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
import urllib.request | |
import zipfile | |
import os | |
import urllib | |
import shutil | |
UPG_URL = "https://github.com/Mintimate/oh-my-rime/archive/refs/heads/main.zip" | |
SAVE_TMP = "mint-main.zip" | |
MINT_ZIP_PREFIX = "oh-my-rime-main" | |
__DIRTY_RUN = False | |
def download(url): | |
ret = urllib.request.urlopen(url, timeout=10) | |
print("downloading...") | |
with ret: | |
if ret.status == 200: | |
# data = ret.read() | |
with open(SAVE_TMP, "wb") as mint_file: | |
# mint_file.write(data) | |
shutil.copyfileobj(ret, mint_file) | |
def filter_file(filepath)->bool: | |
if ".git" in filepath: | |
return False | |
return True | |
def Main(): | |
base_dir = "" | |
dst_dir = "" | |
dst = "" | |
is_win = False | |
if os.name == "nt": | |
is_win = True | |
base_dir = os.environ["APPDATA"] | |
dst_dir = "Rime" | |
dst = os.path.join(base_dir, dst_dir) | |
else: | |
print("only support windows now!") | |
return | |
if __DIRTY_RUN: | |
print("dirty run!!!") | |
download(UPG_URL) | |
# dst = "mint_tmp" | |
print("upgrade...") | |
with zipfile.ZipFile(SAVE_TMP, 'r') as zip_ref: | |
entries = zip_ref.infolist() | |
for entry in entries: | |
fix_name = entry.filename | |
if fix_name.startswith(MINT_ZIP_PREFIX): | |
fix_name = fix_name[len(MINT_ZIP_PREFIX)+1:] | |
if filter_file(fix_name): | |
dst_file = os.path.join(dst, fix_name) | |
if is_win: | |
dst_file = dst_file.replace("/", "\\") | |
print(fix_name, "->", dst_file) | |
if entry.is_dir(): | |
# mark dir | |
if not os.path.isdir(dst_file): | |
if not __DIRTY_RUN: | |
os.mkdir(dst_file) | |
continue | |
with zip_ref.open(entry) as zf, open(dst_file, "wb") as df: | |
if not __DIRTY_RUN: | |
shutil.copyfileobj(zf, df) | |
# zip_ref.extract(entry, "mint_tmp") | |
if __name__ == "__main__": | |
Main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment