def test_get_revision(): # dst_dir = r'D:\repository\config_file' # dst_dir = r'D:\repository\build_script' # dst_dir = r'D:\repository\upgrade_demo' dst_dir = r'/Users/caifh/Documents/AutoBuild/projects/pycredit' ver_code = svn.get_revision(dst_dir) print(str(ver_code))
def get_prj_svn_ver(prj_root, code_svn_path): svn_url_info = get_svn_url_info(code_svn_path) max_svn = 0 for item in svn_url_info: path = prj_root + os.sep + item if os.path.exists(path) and os.path.isdir(path): if SourceConfigParser.PRJ_FLAG == svn_url_info[item][SourceConfigParser.TYPE_FLAG]: svn_ver = svn.get_revision(path) if svn_ver > max_svn: max_svn = svn_ver return max_svn
def process(self): code_svn_url = self.ori_build_config[BuildConfigParser.ROOT_FLAG][BuildConfigParser.CODE_SVN_FLAG] static_config_path = self.work_path + os.sep + self.ori_build_config[BuildConfigParser.ROOT_FLAG][BuildConfigParser.STATIC_FLAG] static_config_path = myfile.normalpath(static_config_path) main_prj_name = self.ori_build_config[BuildConfigParser.WORKSPACE_FLAG][BuildConfigParser.MAIN_FLAG] main_prj_path = self.project_path + os.sep + main_prj_name # 进行代码更新操作 if self.to_update: # 先checkout svn库上代码,再更新和本地相关的配置文件 checkout_or_update_single_item(self.project_path, static_config_path, code_svn_url, revision=self.svn_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) # 获取当前svn版本号 self.svn_ver_code = svn.get_revision(self.project_path) print('svn version code is ' + str(self.svn_ver_code)) # 进行版本编译操作 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] res_path = self.work_path + os.sep + self.ori_build_config[BuildConfigParser.WORKSPACE_FLAG][BuildConfigParser.CHANNEL_INFO_FLAG] if self.channel_file: if not os.path.exists(self.channel_file): print('not exist {}'.format(self.channel_file)) exit(1) 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 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, svn_ver=self.svn_ver_code, 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, svn code is {}.'.format(self.svn_ver_code) print(str_info) else: str_info = 'Build failed, svn code is {}.'.format(self.svn_ver_code) print(str_info)
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): 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 process(src_dir, dst_dir, _type=_PACK_TYPE_ALL, filter_folders=[], _upload_type=_UPLOAD_TYPE_NONE, filter_file_dict=None): # 进行提取,过滤,更新操作 temp_root = tempfile.gettempdir() pack_root = temp_root + os.sep + 'pytxxy_config' try: if os.path.isdir(pack_root): shutil.rmtree(pack_root) os.makedirs(pack_root) # 先获取当前svn目录的版本号 ver_base = 0 ver_base_path = src_dir + os.sep + _PACK_VER_BASE_FILE ver_base_str = file_util.read_file_content(ver_base_path) match = re.match('^\s*(\d+)\s*$', ver_base_str, re.S) if match: ver_base = int(match.group(1)) print('version base is {}.'.format(ver_base)) svn_ver_code = int(svn.get_revision(src_dir)) ver_code = ver_base + svn_ver_code print('subversion code is {} and version code is {}.'.format( svn_ver_code, ver_code)) dst_ver_dir = dst_dir + os.sep + str(ver_code) upload_target_path_dict = {} # 进行整体或单项提取打包操作 filter_dir_file = [] if _type == _PACK_TYPE_ALL: for item in _PACK_SUB_DIRS: temp_dir = pack_root + os.sep + _PACK_SUB_DIRS[item] target_dir = dst_ver_dir + os.sep + _PACK_SUB_DIRS[item] if _PACK_SUB_DIRS[item] in filter_file_dict: filter_dir_file = filter_file_dict[ _PACK_SUB_DIRS[item]].split(',') process_item(src_dir, target_dir, temp_dir, item, ver_code, filter_folders, filter_file=filter_dir_file) target_file = _PACK_TARGET_FILE target_path = target_dir + os.sep + target_file upload_target_path_dict[_PACK_SUB_DIRS[item]] = target_path else: temp_dir = pack_root + os.sep + _PACK_SUB_DIRS[_type] target_dir = dst_ver_dir + os.sep + _PACK_SUB_DIRS[_type] if _type == _PACK_TYPE_IOS: filter_dir_file = filter_file_dict[_PACK_TYPE_IOS].split(',') process_item(src_dir, target_dir, temp_dir, _type, ver_code, filter_folders, filter_file=filter_dir_file) target_file = _PACK_TARGET_FILE target_path = target_dir + os.sep + target_file upload_target_path_dict[_PACK_SUB_DIRS[_type]] = target_path commitmanager = CommitManager(args) if _upload_type == _UPLOAD_TYPE_ALL: for item in _UPLOAD_SUB_DIRS: item_key = _UPLOAD_SUB_DIRS[item] config_info = commitmanager.get_item_info(item_key) if config_info: commitmanager.commit(item_key, config_info, upload_target_path_dict[item_key]) elif _upload_type != _UPLOAD_TYPE_NONE: item_key = _UPLOAD_SUB_DIRS[_upload_type] config_info = commitmanager.get_item_info(item_key) if config_info: commitmanager.commit(item_key, config_info, upload_target_path_dict[item_key]) finally: # 清空临时文件夹内容 if os.path.isdir(pack_root): shutil.rmtree(pack_root)