def update_git_file(git_root, relative_file_path, new_file, msg, zip_password=None): # 先判断git目录状态是否正常 if not git.is_repository(git_root): raise Exception( '{} is not a valid git source directory!'.format(git_root)) target_file = file_util.normalpath( os.path.join(git_root, relative_file_path)) if not is_same_config(new_file, target_file, zip_password=zip_password): if platform.system() == 'Darwin' and zip_password: UNZIP_CMD = 'unzip -o {}'.format(new_file) exec_cmd.run_cmd_with_system_in_specified_dir( os.path.dirname(new_file), UNZIP_CMD, print_flag=True) os.remove(new_file) ZIP_CMD = 'zip -m -r -P {} {} {}'.format(zip_password, 'config_file.zip', 'config_file') exec_cmd.run_cmd_with_system_in_specified_dir( os.path.dirname(new_file), ZIP_CMD, print_flag=True) file_util.replace_file(new_file, target_file) paths = [] paths.append(relative_file_path) git.push_to_remote(paths, msg, repository=None, refspecs=None, _dir=git_root) else: print('{} and {} is the same!'.format(new_file, target_file))
def commit_to_repo(self): git_root = self.git_root # 先判断git目录状态是否正常 if not git_util.is_repository(git_root): raise Exception(f'{git_root} is not a valid git source directory!') setup_path = self.setup_path[len(git_root):] # init_path = self.init_path[len(git_root):] relative_setup_path = self.get_relative_path(setup_path) # relative_init_path = self.get_relative_path(init_path) paths = [] paths.append(relative_setup_path) # paths.append(relative_init_path) msg = f'updated {self.module_name} version with {self.ver_name}.' git_util.push_to_remote(paths, msg, repository=None, refspecs=None, _dir=git_root)
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): 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): 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)