• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1#encoding=utf-8
2import os
3import platform
4import sys
5import time
6from git.repo import Repo
7from portalocker import FileLock
8
9def printLog(message):
10    now = time.localtime()
11    time_info = time.strftime("%Y-%m-%d %X", now)
12    print(time_info + " " + message)
13
14def getAllParam(param_file):
15    params_dict = {}
16    with open(param_file, 'Ur', encoding="utf-8") as file:
17        for line in file.readlines():
18            line = line.strip()
19            if len(line) != 0:
20                if line.find("=") != -1:
21                    str_list = line.split('=')
22                    if str_list[0].find("#") != -1:
23                        continue
24                    else:
25                        key = str_list[0].strip()
26                        value = line[len(str_list[0]) + 1:].strip()
27                        if len(key) !=0 and len(value) != 0:
28                            params_dict[key] = value
29
30    return params_dict
31
32def parseProjectName(server_link):
33    printLog("INFO: parse the upgrade script project name from %s" % server_link)
34    items = server_link.split("/")
35    if len(items) <= 1:
36        return None
37    project_name = items[-1].split(".")[0]
38    return project_name
39
40def downloadUpgradeScripts(server_link, upgrade_script_path, is_update_script):
41    lock_name = "EnvToolMicroService.lock"
42    lock_file_abspath = os.path.join(WORKSPACE, lock_name)
43    locked = True
44    if locked:
45        printLog("download or update the upgrade script")
46        file_lock = FileLock()
47        file_lock.lockFile(lock_file_abspath)
48        printLog("Get lock. Start to ")
49
50        if not is_update_script and os.path.exists(upgrade_script_path):
51            printLog("update_script is false,there is no need to upgrade script")
52            return True
53
54        project_branch = "master"
55        if "-b" in server_link:
56            project_branch = server_link.split(" -b ")[-1]
57        if os.path.isdir(upgrade_script_path) and os.path.isdir(os.path.join(upgrade_script_path, ".git")):
58            if is_update_script:
59                repo = Repo(upgrade_script_path)
60                repo.git.remote()
61                if not repo.git.pull():
62                    shutil.rmtree(upgrade_script_path)
63                    Repo.clone_from(server_link.split(" -b ")[0], to_path=upgrade_script_path,branch=project_branch)
64                    printLog("pull fail, download the upgrade script")
65                    return True
66                else:
67                    printLog("update the upgrade script")
68                    return True
69        else:
70            if os.path.isdir(upgrade_script_path):
71                shutil.rmtree(upgrade_script_path)
72            Repo.clone_from(server_link.split(" -b ")[0], to_path=upgrade_script_path,branch=project_branch)
73            printLog("download the upgrade script")
74            return True
75
76
77if __name__ == "__main__":
78    if len(sys.argv) <= 1:
79        printLog("not enough args")
80        sys.exit(-1)
81    configPath = sys.argv[1]
82    os_name = platform.system()
83    python_exe_file = None
84    current_path = os.path.abspath(os.path.dirname(__file__))
85    if os_name == "Linux":
86        WORKSPACE = r"/data/MobileUpgrade"
87        python_exe_file = "python3"
88    elif os_name == "Windows":
89        WORKSPACE = r"D:\MobileUpgrade"
90        #python_exe_file = r"D:\DeviceTestTools\python\python.exe"
91        python_exe_file = "python"
92
93    server_link = "https://gitee.com/wenjun_HW/DeployTool_DevicePloy.git"
94    param_dict = getAllParam(configPath)
95
96    if not os.path.isdir(WORKSPACE):
97        try:
98            os.makedirs(WORKSPACE)
99        except:
100            time.sleep(1)
101            pass
102
103    '''================================update or download the upgrade script================================'''
104    if param_dict.get("tool_version") == "False":
105        is_update_script = False
106    elif param_dict.get("tool_version"):
107        is_update_script = True
108    else:
109        is_update_script = False
110    printLog("is_update_script = %s" % is_update_script)
111    project_name = parseProjectName(server_link)
112    upgrade_script_path = os.path.join(WORKSPACE, project_name)
113    ret = downloadUpgradeScripts(server_link, upgrade_script_path, is_update_script)
114    if not ret:
115        printLog("ERROR: download or update script failure")
116        ret = -1
117    else:
118        '''================================upgrade the device================================'''
119        printLog("INFO: ##############start to UpgradeScripts on your pc##############")
120        excute_file = os.path.join(upgrade_script_path, "src", "controller.py")
121        command = "%s -Bu %s %s" % (python_exe_file, excute_file, configPath)
122        printLog(command)
123        ret = os.system(command)
124        printLog("INFO: ##############end to UpgradeScripts on your pc##############")
125        printLog("result is %s" % ret)
126        if ret != 0:
127            printLog("ERROR: Upgrade failure")
128            ret = -1
129    exit(ret)