def process(self): main_prj_name = self.ori_build_config[BuildConfigLabel.WORKSPACE_FLAG][BuildConfigLabel.MAIN_FLAG] self.project_code_path = os.path.join(self.project_path, self.branch) # 进行代码更新操作 if self.to_update: code_url = self.ori_build_config[BuildConfigLabel.ROOT_FLAG][BuildConfigLabel.CODE_URL_FLAG] git.checkout_or_update(self.project_code_path, code_url, self.code_ver, self.branch) # 设置配置信息并获取当前代码版本号 self.prj_root = git.get_git_root(self.project_code_path) main_prj_path = os.path.join(self.prj_root, main_prj_name) self.prj_code_ver = git.get_revision(self.prj_root) print('current code version is ' + self.prj_code_ver) # 下面这部分代码依赖于前面成员变量的初始化,请不要随意调整执行位置 if self.to_update: self._update_local_build_file() # 进行版本编译操作 if self.to_build: to_check_vals = ['ver_name', 'ver_code', 'ver_env'] for name in to_check_vals: value = getattr(self, name) if not value: info = 'Please specify the {}.'.format(name) print(info) exit(1) # 参数非空判断验证通过开始进行正式业务逻辑 self.pro_build_config = self._get_pro_build_config() # 在进行编译前先进行空间清理操作 self._clear_output_directory(self.curr_env_output_root) self.build_app(self.pro_build_config) if os.path.exists(self.apk_output_path) and os.path.isfile(self.apk_output_path): # 将编译信息写文件 self._write_build_info() # 生成apk描述信息 desc_data = self._generate_desc() target_name = os.path.basename(self.apk_output_path) to_upload_path = os.path.dirname(self.apk_output_path) source_name = os.path.basename(self.apk_output_path) # 有需要则先加固,再签名 if BuilderLabel.PROTECT_FLAG in self.pro_build_config and BuilderLabel.SIGNER_FLAG in self.pro_build_config: env_mode = self.pro_build_config[BuilderLabel.ENV_MODE_FLAG] if self.pro_build_config[BuilderLabel.PROTECT_FLAG][BuilderLabel.IS_NEED_FLAG][env_mode]: source_name = self._protect_file(main_prj_path) str_info = 'Build success, code version is {}.'.format(self.prj_code_ver) print(str_info) # 进行编译好的版本提交操作 if hasattr(self, 'to_upload') and self.to_upload: self._upload_file(source_name, target_name, to_upload_path, desc_data=desc_data) else: str_info = 'Build failed, code version is {}.'.format(self.prj_code_ver) print(str_info)
def commit_with_git(self, source_url, source_path, relative_path, targer_config_path, git_branch_name, zip_password=None): print('\n=====git url {}====='.format(source_url)) source_path = source_path + os.sep + git_branch_name source_path = file_util.normalpath(source_path) git.checkout_or_update(source_path, source_url, branch=git_branch_name) git_root = git.get_git_root(source_path) relative_file_path = relative_path + os.sep + _PACK_TARGET_FILE update_git_file(git_root, relative_file_path, targer_config_path, _SVN_UPDATE_MSG, zip_password) self.success_upload_path_arr.append(source_path)
def change_podspec_version(self, tag_version, source_path): source_path = git.get_git_root(source_path) file_list = file.get_file_list(source_path) for file_name in file_list: if file_name.endswith('.podspec'): spec_file = os.path.join(source_path, file_name) file_data = file.read_file_content(spec_file) spec_version_line = re.search( r'([a-z]*).version\s+=\s+\W([0-9].+)', file_data).group() new_spec_version_line = re.sub(r'([0-9]\.|[0-9])+', tag_version, spec_version_line) src_dst_list = list( zip([spec_version_line], [new_spec_version_line])) file.replace_string_in_file(spec_file, src_dst_list) try: git.push_to_remote([spec_file], '[other]: 提交{}版本'.format(tag_version), repository=None, refspecs=None, _dir=source_path) git.git_push_tag(source_path, tag_version) except Exception as e: raise Exception(e)
def process(self): git_flag = 'use_git' if not hasattr(self, git_flag): self.use_git = False code_url = self.ori_build_config[BuildConfigParser.ROOT_FLAG][ BuildConfigParser.CODE_URL_FLAG] static_config_path = self.work_path + os.sep + self.ori_build_config[ BuildConfigParser.ROOT_FLAG][BuildConfigParser.STATIC_FLAG] static_config_path = file_util.normalpath(static_config_path) main_prj_name = self.ori_build_config[ BuildConfigParser.WORKSPACE_FLAG][BuildConfigParser.MAIN_FLAG] if self.use_git: self.project_code_path = os.path.join(self.project_path, self.branch) else: self.project_code_path = self.project_path # 进行代码更新操作 if self.to_update: if self.use_git: git.checkout_or_update(self.project_code_path, code_url, self.code_ver, self.branch) else: # 根据参数配置svn用户名和密码 username_flag = 'svn_user' password_flag = 'svn_pwd' if hasattr(self, username_flag) and hasattr( self, password_flag): # print('{}: {}'.format(username_flag, self.svn_user)) # print('{}: {}'.format(password_flag, self.svn_pwd)) svn.set_user_info(getattr(self, username_flag), getattr(self, password_flag)) # 先checkout svn库上代码,再更新和本地相关的配置文件 checkout_or_update_single_item(self.project_code_path, code_url, revision=self.code_ver) # 更新gradle编译配置配置,新框架不再需要该步骤(20161110 17:39) # gradle_setting_name = 'settings.gradle' # gradle_setting_path = static_config_path + os.sep + gradle_setting_name # update_gradle_setting(main_prj_path, gradle_setting_path) # 设置配置信息并获取当前代码版本号 if self.use_git: self.prj_root = git.get_git_root(self.project_code_path) main_prj_path = os.path.join(self.prj_root, main_prj_name) self.code_ver = git.get_revision(self.prj_root) else: self.prj_root = self.project_code_path main_prj_path = os.path.join(self.project_code_path, main_prj_name) self.code_ver = svn.get_revision(self.project_code_path) print('current code version is ' + str(self.code_ver)) if self.to_update: # 更新android sdk本地配置 local_file_name = 'local.properties' local_build_file = os.path.join(static_config_path, local_file_name) target_build_file = os.path.join(self.prj_root, local_file_name) file_util.replace_file(local_build_file, target_build_file) # 进行版本编译操作 if self.to_build: to_check_vals = ['ver_name', 'ver_code', 'ver_env'] for name in to_check_vals: value = getattr(self, name) if not value: info = 'Please specify the {}.'.format(name) print(info) exit(1) # 参数非空判断验证通过开始进行正式业务逻辑 self.pro_build_config = self._get_pro_build_config() self.pre_build() if self.is_debug: mode_flag = apk_builder.DEBUG_FLAG else: mode_flag = apk_builder.RELEASE_FLAG mode = apk_builder.FLAG_MODE_MAP[mode_flag] workspace_map = self.ori_build_config[ BuildConfigParser.WORKSPACE_FLAG] if BuildConfigParser.CHANNEL_INFO_FLAG in workspace_map: res_path = self.work_path + os.sep + workspace_map[ BuildConfigParser.CHANNEL_INFO_FLAG] else: res_path = None if hasattr(self, 'channel_file') and hasattr(self, 'channel'): if self.channel_file: if not os.path.exists(self.channel_file): print('not exist {}'.format(self.channel_file)) exit(1) self.build_app(main_prj_path, self.channel_file, res_path, self.pro_build_config) else: if self.channel: chan_id = self.channel else: chan_id = ProjectBuilder.DEFAULT_CHAN self.build_app(main_prj_path, None, res_path, self.pro_build_config, chan_id, mode) else: chan_id = ProjectBuilder.DEFAULT_CHAN self.build_app(main_prj_path, None, res_path, self.pro_build_config, chan_id, mode) if os.path.exists(self.apk_output_path) and os.path.isfile( self.apk_output_path): # 将编译信息写文件 build_info_format = self.ori_build_config[ BuildConfigParser.BUILD_INFO_TEMPLET_FLAG] build_info = build_info_format.format(ver_name=self.ver_name, code_ver=self.code_ver, ver_code=self.ver_code) build_info_path = self.output_directory + os.sep + 'readme-{}-{}.txt'.format( self.ver_name, self.ver_code) file_util.write_to_file(build_info_path, build_info, encoding='utf-8') target_name = os.path.basename(self.apk_output_path) to_upload_path = os.path.dirname(self.apk_output_path) source_name = os.path.basename(self.apk_output_path) # 有需要则先加固,再签名 if ProjectBuilder.PROTECT_FLAG in self.pro_build_config and ProjectBuilder.SIGNER_FLAG in self.pro_build_config: env_mode = self.pro_build_config[ ProjectBuilder.ENV_MODE_FLAG] if self.pro_build_config[ProjectBuilder.PROTECT_FLAG][ ProjectBuilder.IS_NEED_FLAG][env_mode]: ip = self.pro_build_config[ ProjectBuilder.PROTECT_FLAG][ ProjectBuilder.IP_FLAG] user_name = self.pro_build_config[ ProjectBuilder.PROTECT_FLAG][ ProjectBuilder.USER_FLAG] api_key = self.pro_build_config[ ProjectBuilder.PROTECT_FLAG][ ProjectBuilder.API_KEY_FLAG] api_secret = self.pro_build_config[ ProjectBuilder.PROTECT_FLAG][ ProjectBuilder.API_SECRET_FLAG] protected_path = protect_app.protect( ip, user_name, api_key, api_secret, self.apk_output_path) if hasattr(self, 'to_align') and self.to_align: aligned_path = file_util.get_middle_path( protected_path) apk_util.zipalign(protected_path, aligned_path) to_sign_path = aligned_path else: to_sign_path = protected_path keystore = os.path.join( main_prj_path, self.pro_build_config[ProjectBuilder.SIGNER_FLAG][ ProjectBuilder.KEYSTORE_FLAG]) storepass = self.pro_build_config[ ProjectBuilder.SIGNER_FLAG][ ProjectBuilder.STOREPASS_FLAG] storealias = self.pro_build_config[ ProjectBuilder.SIGNER_FLAG][ ProjectBuilder.STOREALIAS_FLAG] signed_path = apk_util.get_default_signed_path( protected_path) rtn = apk_util.sign_apk(keystore, storepass, storealias, to_sign_path, signed_path) if rtn: str_info = 'Protect {} and sign success.'.format( self.apk_output_path) source_name = os.path.basename(signed_path) else: str_info = 'Protect {} and sign failed!'.format( self.apk_output_path) raise Exception(str_info) print(str_info) str_info = 'Build success, code version is {}.'.format( self.code_ver) print(str_info) # 进行编译好的版本提交操作 if hasattr(self, 'to_upload') and self.to_upload: result = re.match(BuildManager.__APK_OUTPUT_PATH_PATTERN, target_name) if result: ver_name_info = result.group(1) else: str_info = 'The output file name {} is invalid!'.format( target_name) raise Exception(str_info) # ftp_config_path = os.path.join(self.work_path, 'config') ftp_config_path = self.work_path print('ver_name_info:', ver_name_info) print('target_name: ', target_name) print('source_name:', source_name) ftp_upload.upload_to_sftp(ftp_config_path, ver_name_info, self.ver_env, self.code_ver, to_upload_path, 'Android', target_name, source_name) else: str_info = 'Build failed, code version is {}.'.format( self.code_ver) print(str_info)
def process(self): temp_branch_dict = {} temp_tag_dict = {} if self.branch_dict: temp_branch_dict = ast.literal_eval(self.branch_dict) if self.tag_dict: temp_tag_dict = ast.literal_eval(self.tag_dict) for pod_name in self.pod_name: branch_name = 'master' if pod_name in temp_branch_dict.keys(): branch_name = temp_branch_dict[pod_name] if pod_name in temp_tag_dict.keys(): self.tag_info[pod_name] = temp_tag_dict[pod_name] break temp_source_path = self.work_path + os.sep + 'podFolder' source_path = temp_source_path + os.sep + pod_name source_path = file.normalpath(source_path) print('代码更新本地路径:') print(source_path) git.checkout_or_update(source_path, self.get_remote_url(pod_name), branch=branch_name) git_root = git.get_git_root(source_path) os.chdir(git_root) subprocess.call(['git', 'fetch', '--tags']) new_push_version = git.get_revision(git_root) new_tags_info = git.get_newest_tag_revision(git_root) new_push_tag = '' # 当前仓库有没有打过tag if new_tags_info: new_push_tag = new_tags_info[1] # 最新提交的版本没有打过tag print(new_push_version, new_tags_info) if new_push_version != new_tags_info[0]: new_tag = new_tags_info[1].split('.') tag_num = int(new_tag[len(new_tag) - 1]) + 1 temp_tag = [] for i, val in enumerate(new_tag): if i != len(new_tag) - 1: temp_tag.append(val) else: temp_tag.append(format(tag_num)) new_tag = '.'.join(temp_tag) new_push_tag = new_tag self.change_podspec_version(new_tag, source_path) # 获取远程pod库版本信息 os.chdir(git_root) rtn_str = subprocess.check_output(['pod', 'search', pod_name], universal_newlines=True) version_str = re.search(r'\s+-\s+Versions:.+[PYPodSpec repo]]', rtn_str).group() is_revision_upload = False for detail_ver in version_str.split(','): if new_push_tag in detail_ver.strip(): is_revision_upload = True if not is_revision_upload: try: ret_code = subprocess.check_call(['sh', 'push.sh']) print(ret_code) except Exception as e: raise Exception(e) self.tag_info[pod_name] = new_push_tag if self.podfile_path: for pod_name_key in self.tag_info.keys(): file_data = file.read_file_content(self.podfile_path) version_line_arr = re.search( r"(pod\s+\'({}|{}.+)\'\,\s+\'([0-9]|\.)+\')".format( pod_name_key, pod_name_key), file_data) if version_line_arr: pod_version_line = version_line_arr.group() new_spec_version_line = re.sub(r'([0-9]\.|[0-9])+', self.tag_info[pod_name_key], pod_version_line) src_dst_list = list( zip([pod_version_line], [new_spec_version_line])) file.replace_string_in_file(self.podfile_path, src_dst_list) dir_name = os.path.dirname(os.path.dirname(self.podfile_path)) git_root = git.get_git_root(dir_name) os.chdir(git_root) if git.has_change(git_root): git.push_to_remote([self.podfile_path], '[other]: Podfile文件更新', repository=None, refspecs=None, _dir=git_root)
def process(self): self.project_code_path = os.path.join(self.project_path, self.branch) # 进行代码更新操作 if self.to_update: code_url = self.ori_build_config[Label.config][Label.code_url] git.checkout_or_update(self.project_code_path, code_url, self.code_ver, self.branch) # 设置配置信息并获取当前代码版本号 self.prj_root = git.get_git_root(self.project_code_path) self.prj_code_ver = git.get_revision(self.prj_root) if self.to_update: if Label.submodule in self.ori_build_config[Label.config]: submodules = self.ori_build_config[Label.config][ Label.submodule] for submodule in submodules: submodule_name = submodule sub_branch = submodules[submodule_name][Label.branch] git.update_submodules(self.prj_root, modules_name=submodule_name, branch=sub_branch, need_remote=self.sub_remote) print('current code version is ' + self.prj_code_ver) # 下面这部分代码依赖于前面成员变量的初始化,请不要随意调整执行位置 if self.to_update: self._update_local_build_file() # 进行版本编译操作 if self.to_build: # 参数非空判断验证 to_check_values = ['ver_name'] for name in to_check_values: value = getattr(self, name) if not value: info = 'Please specify the {}.'.format(name) print(info) exit(1) self.pro_build_config = self._get_pro_build_config() rtn = self.build_app(self.pro_build_config) if rtn: # 将编译信息写文件 self._write_build_info() str_info = 'Build success, code version is {}.'.format( self.prj_code_ver) print(str_info) # 进行编译好的版本提交操作 if hasattr(self, 'to_upload' ) and self.to_upload and not self.pro_build_config[ Label.is_test]: self._upload_file( self.pro_build_config[Label.whole_ver_name], self.ver_env) else: str_info = 'Build failed, code version is {}.'.format( self.prj_code_ver) print(str_info)
def process(self): git_flag = 'use_git' if not hasattr(self, git_flag): self.use_git = False # 进行代码更新操作 if self.to_update: code_url = self.app_build_cofig[BuildConfigParser.CODE_URL_FLAG] if self.use_git: self.sftp_config_path = ['config', 'base', 'sftp_config.xml'] self.sftp_config_path = os.sep.join(self.sftp_config_path) self.sftp_config_path = self.work_path + os.sep + self.sftp_config_path doc = xmltodict.parse( myfile.read_file_content(self.sftp_config_path)) sftp_config_data = doc['config'] self.temp_username = sftp_config_data['username'] self.temp_password = sftp_config_data['password'] git.checkout_or_update(self.project_path, code_url, self.code_ver, self.branch) git.revert_temporary(self.project_path) else: # 根据参数配置svn用户名和密码 username_flag = 'svn_user' password_flag = 'svn_pwd' if hasattr(self, username_flag) and hasattr( self, password_flag): # print('{}: {}'.format(username_flag, self.svn_user)) # print('{}: {}'.format(password_flag, self.svn_pwd)) svn.set_user_info(getattr(self, username_flag), getattr(self, password_flag)) svn.checkout_or_update(self.project_path, code_url, self.code_ver) # 获取当前代码版本号 if self.use_git: git_root = git.get_git_root(self.project_path) self.code_ver = git.get_revision(git_root) else: self.code_ver = svn.get_revision(self.project_path) print('current code version is ' + str(self.code_ver)) # 进行版本编译操作 if self.to_build: to_check_vals = ['ver_name', 'ver_code', 'ver_env', 'ver_type'] for name in to_check_vals: value = getattr(self, name) if not value: info = 'Please specify the {}.'.format(name) print(info) exit(1) # 参数非空判断验证通过开始进行正式业务逻辑 self.pro_build_config = self._get_pro_build_config() self.pre_build() self.build(self.pro_build_config) if os.path.isfile(self.ipa_output_path): # 将编译信息写文件 build_info_format = self.ori_build_config[ BuildConfigParser.BUILD_INFO_TEMPLET_FLAG] build_info = build_info_format.format(ver_name=self.ver_name, code_ver=self.code_ver, ver_code=self.ver_code) build_info_path = self.output_directory + os.sep + 'readme-{}-{}.txt'.format( self.ver_name, self.ver_code) myfile.write_to_file(build_info_path, build_info, encoding='utf-8') str_info = 'Build success, current code version is {}.'.format( self.code_ver) print(str_info) else: str_info = 'Build failed, current code version is {}.'.format( self.code_ver) raise Exception(str_info) # 将*.ipa包上传到sftp服务器 if self.to_upload_sftp: doc = ET.parse(self.sftp_config_path) root = doc.getroot() username = root.find('username') username.text = self.temp_username password = root.find('password') password.text = self.temp_password doc.write(self.sftp_config_path) print('----------change it--------') # 复制podfile.lock 到目标文件夹 pods_lock_file = self.pods_path + os.sep + 'Podfile.lock' shutil.copy(pods_lock_file, self.output_directory) # 为防止上传的东西太多,将部分文件打成zip包 print('Archiving data file') tmp_folder = tempfile.mkdtemp() dst_zip_file = self.output_directory + os.sep + '%s_data.zip' % ( self.ipa_name) myfile.process_dir_src_to_dst(self.output_directory, tmp_folder, self.process_func) myzip.zip_dir(tmp_folder, dst_zip_file) shutil.rmtree(tmp_folder) for file in os.listdir(self.output_directory): if os.path.splitext(file)[1] == '.xcarchive': xcarchive_file_path = os.path.join( self.output_directory, file) shutil.rmtree(xcarchive_file_path) sftp.upload_to_sftp(self.work_path, self.ver_name, self.ver_env, self.code_ver, self.app_code, self.output_directory, 'IOS', '', self.ipa_name, self.ipa_name) # 复制文件到document ipa_file_path = self.output_directory + os.sep + self.ipa_name ipa_dir_path = myfile.normalpath('/Users/caifh/Documents/ipa') if os.path.exists(ipa_dir_path): shutil.copy(ipa_file_path, ipa_dir_path) # 更新工程文件 print('更新工程文件') print(self.project_path) new_project_path = self.project_path allfilelist = os.listdir(self.project_path) for file in allfilelist: filepath = os.path.join(self.project_path, file) if os.path.isdir(filepath): new_project_path = myfile.normalpath(filepath) break dst_dir = os.path.abspath(new_project_path) os.chdir(dst_dir) rtn_str = subprocess.check_output( 'git diff --name-only --diff-filter=ACM', shell=True, universal_newlines=True) if rtn_str: info_arr = rtn_str.split('\n') new_info_arr = [] for item in info_arr: if len(item) > 0: new_info_arr.append(item) try: source_path = git.get_git_root(self.project_path) git.push_to_remote(new_info_arr, '[other]: 提交打包版本信息', repository=None, refspecs=None, _dir=source_path) git.revert_temporary(source_path) except Exception as e: raise Exception(e)
def test_revert(): dst_path = '/Users/caifh/Documents/git_test/test' git_root = git.get_git_root(dst_path) git.revert(git_root)