update.py 3.3 KB
Newer Older
H
hjdhnx 已提交
1 2 3 4 5 6 7 8
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# File  : update.py
# Author: DaShenHan&道长-----先苦后甜,任凭晚风拂柳颜------
# Date  : 2022/9/6

import requests
import os
H
hjdhnx 已提交
9
import zipfile
H
hjdhnx 已提交
10
import shutil # https://blog.csdn.net/weixin_33130113/article/details/112336581
H
hjdhnx 已提交
11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26

def getLocalVer():
    base_path = os.path.dirname(os.path.abspath(os.path.dirname(__file__)))  # 上级目录
    version_path = os.path.join(base_path, f'js/version.txt')
    if not os.path.exists(version_path):
        with open(version_path,mode='w+',encoding='utf-8') as f:
            version = '1.0.0'
            f.write(version)
    else:
        with open(version_path,encoding='utf-8') as f:
            version = f.read()
    return version

def getOnlineVer():
    ver = '1.0.1'
    try:
H
hjdhnx 已提交
27
        r = requests.get('https://gitcode.net/qq_32394351/dr_py/-/raw/master/js/version.txt',timeout=(2,2))
H
hjdhnx 已提交
28 29 30
        ver = r.text
    except Exception as e:
        print(f'{e}')
H
hjdhnx 已提交
31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52
    return ver

def checkUpdate():
    local_ver = getLocalVer()
    online_ver = getOnlineVer()
    if local_ver != online_ver:
        return True
    return False


def del_file(filepath):
    """
    删除execl目录下的所有文件或文件夹
    :param filepath: 路径
    :return:
    """
    del_list = os.listdir(filepath)
    for f in del_list:
        file_path = os.path.join(filepath, f)
        if os.path.isfile(file_path):
            os.remove(file_path)

H
hjdhnx 已提交
53 54 55 56 57 58 59 60 61 62 63 64 65
def copy_to_update():
    base_path = os.path.dirname(os.path.abspath(os.path.dirname(__file__)))  # 上级目录
    tmp_path = os.path.join(base_path, f'tmp')
    dr_path = os.path.join(tmp_path, f'dr_py-master')
    if not os.path.exists(dr_path):
        print(f'升级失败,找不到目录{dr_path}')
        return False
    from_path = os.path.join(dr_path, f'js')
    to_path = os.path.join(base_path, f'js')
    print(f'开始拷贝文件{from_path}=>{to_path}')
    shutil.copytree(from_path, to_path)
    return True

H
hjdhnx 已提交
66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92
def download_new_version():
    base_path = os.path.dirname(os.path.abspath(os.path.dirname(__file__)))  # 上级目录
    tmp_path = os.path.join(base_path, f'tmp')
    os.makedirs(tmp_path,exist_ok=True)
    url = 'https://gitcode.net/qq_32394351/dr_py/-/archive/master/dr_py-master.zip'
    # tmp_files = os.listdir(tmp_path)
    # for tp in tmp_files:
    #     print(f'清除缓存文件:{tp}')
    #     os.remove(os.path.join(tmp_path, tp))
    del_file(tmp_path)
    headers = {
        'Referer': 'https://gitcode.net/',
        'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/95.0.4638.54 Safari/537.36',
    }
    msg = ''
    try:
        print(f'开始下载:{url}')
        r = requests.get(url,headers=headers,timeout=(20,20))
        rb = r.content
        download_path = os.path.join(tmp_path, 'dr_py.zip')
        with open(download_path,mode='wb+') as f:
            f.write(rb)
        print(f'开始解压文件:{download_path}')
        f = zipfile.ZipFile(download_path, 'r')  # 压缩文件位置
        for file in f.namelist():
            f.extract(file, tmp_path)  # 解压位置
        f.close()
H
hjdhnx 已提交
93 94 95 96
        print('解压完毕,开始升级')
        ret = copy_to_update()
        print(f'升级完毕,结果为:{ret}')
        msg = '升级成功'
H
hjdhnx 已提交
97
    except Exception as e:
H
hjdhnx 已提交
98
        msg = f'升级失败:{e}'
H
hjdhnx 已提交
99
    return msg