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