class DownloadToolController(object): def __init__(self): super(DownloadToolController, self).__init__() self.root_dir = os.getcwd() self.download_tool = None self.log = MyLogger(self.__class__.__name__) self.download_tool_dict = {} self.release_download_tool_name = None self.download_tool_release_zip_dir = "" self.zip_tool = zipTool() self.update() def update(self): json_file = os.path.join(self.root_dir,"json","download_tool.json") json_str = load_json(json_file) self.tmp = os.path.join(self.root_dir, json_str["tmp"]) self.download_tool_dir = os.path.join(self.root_dir, json_str["download_tool_dir"]) self.partition_config = os.path.join(self.root_dir, json_str["partition_config"]) self.template_config = os.path.join(self.root_dir, json_str["template_config"]) self.download_tool_release_dir = json_str["download_tool_release_dir"] self.download_tool_file_name = json_str["download_tool_file_name"] self.win_type = json_str["win_type"] def unzip_download_tool(self,zip_file,extract_dir=None): self.zip_tool.unpack_archive(os.path.join(self.download_tool_dir,zip_file),extract_dir) def update_download_tool(self): tools_list = [_file for _file in os.listdir(self.download_tool_release_dir) if self.win_type in _file and "aboot-tools" in _file and _file.endswith(".exe")] tools_list.sort(key=lambda fn: os.path.getmtime(self.download_tool_release_dir + '\\' + fn)) self.log.debug(tools_list) if not self.download_tool_file_name: self.download_tool_file_name = tools_list[-1] file_name = self.download_tool_file_name self.download_tool_release_zip_dir = os.path.join(self.download_tool_dir, self.download_tool_file_name.replace(".exe","")) # self.log.info(file_name) file_name_l = [file_name,file_name.replace("x64","x86")] self.download_tool = [] for file_name in file_name_l: zip_file = os.path.join(self.download_tool_dir,file_name) # self.log.info(zip_file) download_tool_fname = zip_file.replace(".exe","") if not os.path.exists(zip_file): shutil.copy2(os.path.join(self.download_tool_release_dir,file_name),self.download_tool_dir) time.sleep(3) self.unzip_download_tool(zip_file) elif not os.path.exists(download_tool_fname): time.sleep(3) self.unzip_download_tool(zip_file) self.download_tool.append(download_tool_fname) for _tool in self.download_tool: self.log.info(_tool) def prepare_download_tool(self,images,borad="crane_evb_z2"): """borad : crane_evb_z2, bird_phone, crane_evb_dual_sim""" self.log.debug("\n".join(images)) self.log.debug("\n".join(self.download_tool)) for download_tool_dir in self.download_tool: if not os.path.exists(download_tool_dir): self.log.warning("%s not exists" % download_tool_dir) continue dist_dir = os.path.join(download_tool_dir,"images") dist_bin_l = [os.path.join(dist_dir,os.path.basename(_file)) for _file in images] for _file in dist_bin_l: if os.path.exists(_file): os.remove(_file) for src_bin,dist_bin in zip(images,dist_bin_l): if os.path.exists(src_bin): if os.path.exists(dist_bin): os.remove(dist_bin) shutil.copy2(src_bin,dist_bin) else: self.log.warning("%s not exists" % src_bin) if os.path.isdir(self.partition_config): for _file in os.listdir(self.partition_config): shutil.copy2(os.path.join(self.partition_config,_file), os.path.join(download_tool_dir,"config","partition",_file)) elif os.path.isfile(self.partition_config): shutil.copy2(self.partition_config,os.path.join(download_tool_dir,"config","partition", os.path.basename(self.partition_config))) else: self.log.error("self.partition_config:%s error" % self.partition_config) if os.path.isdir(self.template_config): for _file in os.listdir(self.template_config): shutil.copy2(os.path.join(self.template_config,_file), os.path.join(download_tool_dir,"config","template",_file)) elif os.path.isfile(self.template_config): shutil.copy2(self.template_config, os.path.join(download_tool_dir,"config","template",os.path.basename(self.template_config))) else: self.log.error("self.template_config:%s error" % self.template_config) def release_zip(self, dist_dir, project_type = "CRANE_A0_16MB", zip_name = None): assert os.path.exists( self.download_tool_release_zip_dir)," can not find download tool %s " % self.download_tool_release_zip_dir os.chdir(self.download_tool_release_zip_dir) if "craneg_evb" in dist_dir: project_type = "CRANEG_Z1_32+8MB" if zip_name: zip_file = zip_name else: zip_file = "ASR_CRANE_EVB_%s.zip" % project_type zip_file = os.path.join(dist_dir,zip_file) release_log = os.path.join(self.tmp,"release_log.txt") self.log.info("create zip %s" % zip_file) release_cmd = "arelease.exe -c . -g -p ASR_CRANE_EVB -v %s %s > %s" % ( project_type, zip_file, release_log) os.system(release_cmd) def release_download_tool(self,release_name,borad="crane_evb_z2",dist_dir=None, download_tool_l=[]): import time "borad : crane_evb_z2, bird_phone, crane_evb_dual_sim" date = time.strftime("%Y%m%d_%H%M%S") release_file_name = "%s_%s_DOWNLOAD_TOOL" % (release_name.upper(),borad.upper()) release_dir = os.path.join(self.download_tool_dir,release_file_name) os.mkdir(release_dir) if not os.path.exists(release_dir) else None self.log.info(release_file_name) # self.log.info(release_dir) if download_tool_l: download_tool_src = download_tool_l[:] else: download_tool_src = self.download_tool for _tool in download_tool_src: dist_file = os.path.join(release_dir,os.path.basename(_tool)) # self.log.info(dist_file) if not os.path.exists(dist_file): shutil.copytree(_tool,dist_file) dist = os.path.join(dist_dir,release_file_name) self.zip_tool.make_archive_e(dist,"zip",release_dir) shutil.rmtree(release_dir) self.release_download_tool_name = dist + ".zip" self.download_tool_dict[borad] = self.release_download_tool_name # self.log.info(self.download_tool_dict) return self.release_download_tool_name
class GitPushDMSDK(gitPushR2RCSDK): def __init__(self): super(gitPushR2RCSDK, self).__init__() self.log = MyLogger(self.__class__.__name__) self.branch_name = "master" # self.git.checkout(self.branch_name) def get_config(self): json_file = os.path.join(self.root_dir, "json", "git_push.json") json_str = load_json(json_file) self.config_d = json_str["dm_sdk"] def get_dsp_rf_dir(self, root_dir): self.git_push_dsp_dir = os.path.dirname(self.target_dist_dir) for root, dirs, files in os.walk(root_dir, topdown=False): if "DSP" in dirs: self.dsp_rf_root_dir = os.path.join(root, "DSP") dsp_bin = os.path.join(self.dsp_rf_root_dir, "CRANEM", "CAT1GSM", "dsp.bin") rf_bin = os.path.join(self.dsp_rf_root_dir, "CRANEM", "CAT1GSM", "PM803", "rf.bin") cranem_dm_dir = os.path.join(self.git_push_dsp_dir, "cus", "evb_dm", "images") self.git_push_dsp_rf_list.append((dsp_bin, cranem_dm_dir)) self.git_push_dsp_rf_list.append((rf_bin, cranem_dm_dir)) if "CP" in dirs: cp_root_dir = os.path.join(root, "CP") boot33_bin = os.path.join(cp_root_dir, "CAT1GSM", "boot33.bin") apn_bin = os.path.join(cp_root_dir, "CAT1GSM", "apn.bin") cranem_dm_dir = os.path.join(self.git_push_dsp_dir, "cus", "evb_dm", "images") self.git_push_dsp_rf_list.append((boot33_bin, cranem_dm_dir)) self.git_push_dsp_rf_list.append((apn_bin, cranem_dm_dir)) break cranem_dm_dir = os.path.join(self.git_push_dsp_dir, "cus", "evb_dm", "build") Crane_DS_miniPhone_h = os.path.join( cranem_dm_dir, "Crane_DS_miniPhone_16M_Ram_8M_Flash_XIP_CIPSRAM.h") Crane_DS_miniPhone_sct = os.path.join( cranem_dm_dir, "Crane_DS_miniPhone_16M_Ram_8M_Flash_XIP_CIPSRAM.sct") dist_dir = os.path.join(self.target_dist_dir, "csw", "platform", "dev_plat", "build") self.git_push_dsp_rf_list.append((Crane_DS_miniPhone_h, dist_dir)) self.git_push_dsp_rf_list.append((Crane_DS_miniPhone_sct, dist_dir)) for _item in self.git_push_dsp_rf_list: self.log.info(_item) def find_new_cp_sdk(self): cp_sdk_list = [ _file for _file in os.listdir(self.cp_sdk_release_dir) if _file.startswith(self.release_target) ] cp_sdk_list.sort(key=lambda fn: os.path.getmtime( os.path.join(self.cp_sdk_release_dir, fn))) assert cp_sdk_list, "can not find sdk" self.cp_sdk = cp_sdk_list[-1] self.log.debug("newest adk: %s" % self.cp_sdk) def copy_sdk(self): copy(os.path.join(self.cp_sdk_release_dir, self.cp_sdk), os.path.join(self.cp_sdk_dir, self.cp_sdk)) time.sleep(3) def unzip_sdk(self): fname, _ = os.path.splitext(self.cp_sdk) root_dir = os.path.join(self.cp_sdk_dir, self.cp_sdk, "SDK") sdk_zip = [ os.path.join(root_dir, _file) for _file in os.listdir(root_dir) if _file.startswith(self.release_target) ] assert sdk_zip, "can not find %s" % root_dir sdk_zip = sdk_zip[-1] while True: try: with ziptool_mutex: self.zip_tool.unpack_archive(sdk_zip, root_dir) assert os.path.exists(root_dir), "can not find %s" % root_dir for root, dirs, files in os.walk(root_dir, topdown=False): if "3g_ps" in dirs: self.cp_sdk_root_dir = root break assert os.path.exists( self.cp_sdk_root_dir ), "can not find %s" % self.cp_sdk_root_dir self.get_dsp_rf_dir(os.path.join(self.cp_sdk_dir, self.cp_sdk)) return except Exception, e: time.sleep(10) self.log.error(e)
class GitPushDspBase(GitPushBase): def __init__(self): super(GitPushDspBase, self).__init__() self.log = MyLogger(self.__class__.__name__) self.decompress_tool = zipTool() self.release_dsp_bin = '' self.dsp_version = '' self.git_push_dsp_rf_list = [] self.rf_type = "PM813" self.update() self.create_git() # self.print_info() def get_config(self): self.config_d = {} def update(self): self.get_config() self.release_dir = os.path.normpath(self.config_d["release_dir"]) self.target_dist_dir = os.path.normpath(self.config_d["target_dir"]) self.git_push_root_dir = os.path.normpath( self.config_d["git_push_root_dir"]) self.local_dsp_bin = os.path.normpath(self.config_d["verson_file"]) self.push_cmd = self.config_d["git_push_cmd"] self.dsp_version_pattern = re.compile(self.config_d["version_pattern"]) self.release_target_file = self.config_d["release_target"] def update_git_push_dsp_rf_list(self): root_dir = os.path.dirname(self.release_dsp_bin) self.git_push_dsp_rf_list.append( (self.release_dsp_bin, self.local_dsp_bin)) rf_bin = os.path.join(root_dir, self.rf_type, "rf.bin") self.git_push_dsp_rf_list.append((rf_bin, self.target_dist_dir)) rf_verson_file = os.path.join(root_dir, self.rf_type, "RF_Version.txt") self.git_push_dsp_rf_list.append( (rf_verson_file, self.target_dist_dir)) rf_excel_file = os.path.join(root_dir, self.rf_type, "rf.xlsm") self.git_push_dsp_rf_list.append((rf_excel_file, self.target_dist_dir)) def get_dsp_version(self, dsp_bin=None): """CRANE_CAT1GSM_L1_1.043.000 , Dec 13 2019 03:30:56""" if not dsp_bin: dsp_bin = self.local_dsp_bin dsp_version_file = os.path.join(self.root_dir, "tmp", "dsp_version_tmp.bin") self.decompress_tool.decompress_bin(dsp_bin, dsp_version_file) assert os.path.exists(dsp_version_file), "can not find {}".format( dsp_version_file) with open(dsp_version_file, "rb") as fob: text = fob.read() match = self.dsp_version_pattern.findall(text) if match: self.log.debug(match[0]) version_info = match[0] else: self.log.error("can not find dsp version infomation") version_info = None # os.remove(dsp_version_file) return version_info def get_release_dsp_rf(self): dsp_release_bin_l = [] release_dir_list = [os.path.join(self.release_dir,_dir) for _dir in os.listdir(self.release_dir) \ if os.path.isdir(os.path.join(self.release_dir,_dir))] release_dir_list.sort(key=lambda fn: os.path.getmtime(fn)) self.log.debug("release_dir_list len:", len(release_dir_list)) self.log.debug(release_dir_list[-10:]) release_files = [ os.path.normpath(os.path.join(_dir, self.release_target_file)) for _dir in release_dir_list[-10:] ] for _file in release_files: if os.path.exists(_file): root = os.path.dirname(_file) rf = os.path.join(root, self.rf_type, "rf.bin") if os.path.exists(rf): dsp_release_bin_l.append(_file) dsp_release_bin_l.sort(key=lambda fn: os.path.getmtime(fn)) # self.log.info("\n".join(dsp_release_bin_l)) self.release_dsp_bin = dsp_release_bin_l[-1] def condition(self): self.update() self.git_clean() local_dsp_version = self.get_dsp_version() self.get_release_dsp_rf() release_dsp_version = self.get_dsp_version(self.release_dsp_bin) if local_dsp_version == release_dsp_version: return False else: self.log.info("local_dsp_version :", local_dsp_version) self.log.info("release_dsp_version:", release_dsp_version) self.dsp_version = release_dsp_version return True def close_push(self): to_address = ",".join(["*****@*****.**"]) subject = "%s auto push" % self.dsp_version msg = r"Hi %s, %s auto push done!" % (to_address.split("@")[0], self.dsp_version) send_email_tool(to_address, subject.upper(), msg) def git_push_start(self): if not self.condition(): time.sleep(10) return self.log.info("wait for dsp copy...") time.sleep(30) self.update_git_push_dsp_rf_list() while self.git_push_dsp_rf_list: src_file, dist = self.git_push_dsp_rf_list.pop() self.log.info(src_file) self.log.info(dist) if os.path.exists(dist): if os.path.isfile(src_file): shutil.copy2(src_file, dist) else: self.log.warning("%s" % src_file) self.log.info("=" * 50) self.log.info("git push dsp...") try: self.git_add() match = re.findall(self.dsp_version_pattern, self.dsp_version) if match: dsp_version = match[0] else: dsp_version = str(time.asctime()) commit_info = "update dsp dailybuild %s" % dsp_version self.git_commit(commit_info) self.git_push() self.close_push() return True except Exception, e: self.log.error(e) self.log.error("git push error") self.git_clean() return None
class gitPushSDKBase(GitPushBase): def __init__(self): super(gitPushSDKBase, self).__init__() self.log = MyLogger(self.__class__.__name__) self.zip_tool = zipTool() self.cp_sdk_version = None self.cp_sdk = None self.cp_sdk_root_dir = None self.dsp_rf_root_dir = '' self.git_push_dsp_dir = '' self.git_push_dsp_rf_list = [] self.dsp_bin = None self.rf_bin = None self.update() self.create_git() # self.print_info() def get_config(self): self.config_d = {} def update(self): self.get_config() self.cp_sdk_release_dir = os.path.normpath( self.config_d["release_dir"]) self.git_push_cp_dir = os.path.normpath( self.config_d["git_push_root_dir"]) self.git_push_root_dir = self.git_push_cp_dir self.target_dist_dir = os.path.normpath(self.config_d["target_dir"]) self.cp_sdk_dir = os.path.normpath( os.path.join(self.root_dir, self.config_d["local_dir"])) self.cp_version_file = self.config_d["verson_file"] self.release_target = self.config_d["release_target"] self.push_cmd = self.config_d["git_push_cmd"] def get_dsp_rf_dir(self, root_dir): for root, dirs, files in os.walk(root_dir, topdown=False): if "DSP" in dirs: self.dsp_rf_root_dir = os.path.join(root, "DSP") self.dsp_bin = os.path.join(self.dsp_rf_root_dir, "dsp.bin") self.rf_bin = os.path.join(self.dsp_rf_root_dir, "rf.bin") self.git_push_dsp_dir = os.path.dirname(self.target_dist_dir) self.git_push_dsp_dir = os.path.join(self.git_push_dsp_dir, "cus", "evb", "images") self.git_push_dsp_rf_list.append( (self.dsp_bin, self.git_push_dsp_dir)) self.git_push_dsp_rf_list.append( (self.rf_bin, self.git_push_dsp_dir)) break def find_new_cp_sdk(self): "ASR3603_MINIGUI_20200603_SDK.zip" cp_sdk_list = [ _file for _file in os.listdir(self.cp_sdk_release_dir) if (_file.endswith(".zip") or _file.endswith(".7z")) and _file.startswith(self.release_target) ] cp_sdk_list.sort(key=lambda fn: os.path.getmtime( os.path.join(self.cp_sdk_release_dir, fn))) assert cp_sdk_list, "can not find sdk" self.cp_sdk = cp_sdk_list[-1] self.log.debug("newest adk: %s" % self.cp_sdk) def clean_git_push_cp(self): self.git_clean() for _file in os.listdir(self.target_dist_dir): if _file in [".git", "X.bat"]: continue _file = os.path.join(self.target_dist_dir, _file) if os.path.isfile(_file): os.remove(_file) else: shutil.rmtree(_file) self.log.info("clean cp done") def copy_sdk(self): self.log.info("copy %s..." % self.cp_sdk) shutil.copy2(os.path.join(self.cp_sdk_release_dir, self.cp_sdk), self.cp_sdk_dir) time.sleep(3) self.log.info("copy done.") def copy_sdk_to_git_push_cp(self, cp_sdk): try: root_dir = os.path.join(self.cp_sdk_dir, cp_sdk) for _file in os.listdir(root_dir): fname = os.path.join(root_dir, _file) if os.path.isfile(fname): shutil.copy2(fname, os.path.join(self.target_dist_dir, _file)) elif os.path.isdir(fname): shutil.copytree(fname, os.path.join(self.target_dist_dir, _file)) else: self.log.warning("%s" % fname) self.log.info("%s" % self.dsp_rf_root_dir) if os.path.exists(self.dsp_rf_root_dir): dir_path = os.path.dirname(self.dsp_rf_root_dir) self.log.info("%s" % dir_path) dist_dir = os.path.join(self.target_dist_dir, os.path.basename(dir_path)) self.log.info("%s" % dist_dir) if os.path.exists(dist_dir): shutil.rmtree(dist_dir) shutil.copytree(dir_path, dist_dir) while self.git_push_dsp_rf_list: src_file, dist = self.git_push_dsp_rf_list.pop() self.log.info(src_file) self.log.info(dist) if os.path.exists(dist): if os.path.isfile(src_file): shutil.copy2(src_file, dist) else: self.log.warning("%s" % src_file) self.log.info("copy_sdk_to_git_push_cp done.") except Exception, e: self.log.error(e) self.log.error("copy_sdk_to_git_push_cp error") raise Exception, "copy_sdk_to_git_push_cp error"
class ManagerVersionBase(object): def __init__(self): self.root_dir = os.getcwd() self.version_log = "" self.log = MyLogger(self.__class__.__name__) self.verion_name = '' self.cur_version = '' self.chip_ID = None self.OS_type = None self.ps_mode = None self.decompress_tool = zipTool() self.dsp_version_pattern = re.compile( r"(CRANE_.*?,.*?[0-9][0-9]:[0-9][0-9]:[0-9][0-9]|CRANEG_.*?,.*?[0-9][0-9]:[0-9][0-9]:[0-9][0-9])" ) self.version_pattern = None self.cp_version = None self.dsp_version = None def _get_verion_name(self): assert os.path.exists( self.version_log), "%s no exists" % self.version_log with open(self.version_log) as flog: text = flog.read() text = text.replace('\n', '') text_list = self.version_pattern.findall(text) self.log.debug(text_list) if text_list: # assert text_list,"can not find version info" self.verion_name, _ = text_list[-1] else: self.verion_name = self.branch_name self.log.debug(self.verion_name) def get_nearest_version(self): assert os.path.exists( self.version_log), "%s no exists" % self.version_log with open(self.version_log) as flog: text = flog.read() text = text.replace('\n', '') # text_list = re.findall(r'crane_git_r([0-9]+)',text) text_list = self.version_pattern.findall(text) self.log.debug(text_list) if text_list: # assert text_list,"can not find version info" self.verion_name, cnt = text_list[-1] cnt = int(cnt) version = "%s%04d" % (self.verion_name, cnt + 1) # self.log.info(version) else: version = self.branch_name return version def record_version(self, version='', info=''): flog = open(self.version_log, 'a') if not version: version = self.get_nearest_version() if not info: info = self.get_revion_info() text = "\n".join([version + ":", info]) flog.write(text + "\n\n") flog.flush() flog.close() return version def get_cp_version(self, cp_version_file): """ #define CRANE_CUST_VER_INFO ["##SYSTEM_VERSION##"] ["##DISTRIBUTION_VERSION##"] ["##SYSTEM_TARGET_OS##"] ["##SYSTEM_PS_MODE##"] ["##APPEND_REVERSION##"] """ assert os.path.exists( cp_version_file), "%s not exists" % cp_version_file SYSTEM_CUST_SKU = "MINIGUI" SYSTEM_SKU_REVERSION = "SDK" if self.ps_mode: SYSTEM_PS_MODE = self.ps_mode else: SYSTEM_PS_MODE = "LTEGSM" if self.OS_type: SYSTEM_TARGET_OS = self.OS_type else: SYSTEM_TARGET_OS = "TX" APPEND_REVERSION = "_".join([SYSTEM_CUST_SKU, SYSTEM_SKU_REVERSION]) file_obj = open(cp_version_file) SYSTEM_VERSION = '' DISTRIBUTION_VERSION = '' for _line in file_obj: _format = '#define[ ]+SYSTEM_VERSION[ ]+"(.*?)"' match = re.findall(_format, _line) if match: SYSTEM_VERSION = match[0] _format = '#define[ ]+DISTRIBUTION_VERSION[ ]+"(.*?)"' match = re.findall(_format, _line) if match: DISTRIBUTION_VERSION = match[0] CRANE_CUST_VER_INFO = "[%s][%s][%s][%s][%s]" % (SYSTEM_VERSION, \ DISTRIBUTION_VERSION, SYSTEM_TARGET_OS, SYSTEM_PS_MODE, APPEND_REVERSION) # self.log.debug(CRANE_CUST_VER_INFO) self.cp_version = CRANE_CUST_VER_INFO return CRANE_CUST_VER_INFO def update_cp_version(self, cp_version_file, cp_version_log_file): self.cp_version = self.get_cp_version(cp_version_file) with open(cp_version_log_file, "w") as obj: obj.write(self.cp_version) return self.cp_version @staticmethod def get_old_cp_version(cp_version_log_file): if not os.path.exists(cp_version_log_file): return None else: with open(cp_version_log_file) as obj: return obj.read() def record_cp_version(self, cp_version_log_file): with open(cp_version_log_file, "w") as obj: obj.write(self.cp_version) def get_dsp_version(self, dsp_bin, dsp_version_log_file): """!CRANE_CAT1GSM_L1_1.043.000 , Dec 13 2019 03:30:56""" dsp_version_file = os.path.join(self.root_dir, "tmp", "dsp_version.bin") self.decompress_tool.decompress_bin(dsp_bin, dsp_version_file) assert os.path.exists(dsp_version_file), "can not find {}".format( dsp_version_file) with open(dsp_version_file, "rb") as fob: text = fob.read() match = self.dsp_version_pattern.findall(text) if match: version_info = match[0] # self.log.debug(version_info) self.dsp_version = version_info else: version_info = "can not match dsp version".upper() with open(dsp_version_log_file, "w") as obj: obj.write(version_info) return version_info def print_info(self): print "-" * 80 for k, v in self.__dict__.items(): print("{: <20}: {}".format(k, v))