def test_json_file(self): """ 测试JSON文件处理 """ FileTools.create_dir(TEST_PATH + 'json_file/') json_obj = dict() json_obj['key_1'] = 'value1' json_obj['key_2'] = 'value2' json_obj['key_3'] = dict() json_obj['key_3']['key_3_1'] = 'value3_1' json_obj['key_3']['key_3_2'] = 'value3_2' json_obj['key_4'] = ['value4_1', 'value4_2'] FCMMTools.save_to_json_file(TEST_PATH + 'json_file/.fcmm4git', json_obj) get_json_obj = FCMMTools.get_fcmm_config(TEST_PATH + 'json_file/') self.assertDictEqual(json_obj, get_json_obj, 'JSON文件处理失败') return
def cmd_add_pkg(dict_cmd_para=None): """ 新增FCMM的pkg分支,如果原分支存在,可以重置分支 @decorators staticmethod @param {dict} dict_cmd_para=None - 参数字典 @returns {list} - 执行结果[returncode, msgstring] returncode - 0代表成功,其他代表失败 msgstring - 要返回显示的内容 """ # 进行初始化 is_exit, res, config, fcmm_config, repo_info, current_branch = FCMMGitCmd.cmd_common_init( 'add-pkg', dict_cmd_para) if is_exit: return res # 进一步检查 ver = FCMMTools.get_cmd_para_value(dict_cmd_para, '-v', '-version') has_pkg = FCMMGitTools.check_branch_exists(repo_info, 'lb-pkg') if not ('-f' in dict_cmd_para.keys() or '-force' in dict_cmd_para.keys()): if has_pkg: return [ 1, FCMMTools.get_i18n_tips(config, 'branch_has_exists', 'lb-pkg') ] # 检查版本 if ver is not None and not FCMMGitTools.check_tag_exists( repo_info, ver): return [ 1, FCMMTools.get_i18n_tips(config, 'tag_not_exists', ver) ] # 开始进行处理 if has_pkg: # 分支已存在,覆盖分支 if config['backup_before'] == 'true': res = FCMMGitTools.backup_branch( repo_info, 'lb-pkg', FCMMGitTools.get_git_config_user_name( repo_info, config['consle_encode'])) if res[0] == 0: res = FCMMGitTools.overwrite_branch(repo_info, 'lb-pkg', 'master', ver) else: # 分支不存在,创建分支并推送到远端 res = FCMMTools.run_sys_cmd('git checkout master') if res[0] == 0: fcmm_config['has_pkg'] = "true" FCMMTools.save_to_json_file(repo_info['work_dir'], fcmm_config) res = FCMMTools.run_sys_cmd_list([ 'git add *', 'git commit -m "change .fcmm4gig by tools"', 'git push origin master' ]) if res[0] == 0: res = FCMMGitTools.add_branch(repo_info, 'lb-pkg', 'master', ver) # 返回值 FCMMTools.run_sys_cmd('git checkout %s' % (current_branch)) if res[0] != 0: res[1] = FCMMTools.get_i18n_tips(config, 'execute_fail') return res
def cmd_init(dict_cmd_para=None): """ 初始化FCMM版本库:根据指定的参数建立及初始化FCMM版本库 @decorators staticmethod @param {dict} dict_cmd_para=None - 参数字典 @returns {list} - 执行结果[returncode, msgstring] returncode - 0代表成功,其他代表失败 msgstring - 要返回显示的内容 """ # 判断是否有帮助 if '-h' in dict_cmd_para.keys() or '-help' in dict_cmd_para.keys(): return FCMMGitCmd.cmd_help({'init': ''}) # 需要获取的参数 config = RunTools.get_global_var('config') # 最基础的参数校验 res = FCMMTools.vailidate_cmd_para(dict_cmd_para, 'init') if res[0] != 0: return res # 要处理的参数 url = FCMMTools.get_cmd_para_value(dict_cmd_para, '-u', '-url') base = FCMMTools.get_cmd_para_value(dict_cmd_para, '-b', '-base') ver = FCMMTools.get_cmd_para_value(dict_cmd_para, '-v', '-version') # 常用信息获取 repo_info = FCMMGitTools.get_repo_info(os.getcwd()) # 获取git库原生信息有建库 repo_name = FileTools.get_dir_name(repo_info['work_dir']) # 获取fcmm4git配置信息 fcmm_config = FCMMTools.get_fcmm_config(repo_info['work_dir']) remote_name = FCMMGitTools.get_remote_repo_name(url) # 远程仓库命名 remote_repo_info = None remote_has_pkg = False remote_has_tag = False # 进行是否可处理的校验 if base == 'local': if fcmm_config is not None: # 已经除初始化过fcmm4git,不允许重复处理exit_fcmm_file return [2, config['i18n_tips']['exit_fcmm_file']] else: # 检查本地目录是否为空 if not ('-f' in dict_cmd_para.keys() or '-force' in dict_cmd_para.keys()): if os.listdir(repo_info['work_dir']): return [3, config['i18n_tips']['local_not_bare']] # 需要将远程版本库下载下来比较处理 fun_res = FCMMGitTools.clone_remote_repo(url, config['temp_path'], None, True) if fun_res[0] != 0: return [fun_res[0], config['i18n_tips']['execute_fail']] remote_repo_info = FCMMGitTools.get_repo_info( config['temp_path'].rstrip('\\/') + '/' + remote_name) # 尝试找远程的版本分支 os.chdir(remote_repo_info['work_dir']) FCMMTools.run_sys_cmd('git checkout --track origin/lb-pkg') remote_has_pkg = FCMMGitTools.check_branch_exists( remote_repo_info, 'lb-pkg') if ver is not None and FCMMGitTools.check_tag_exists( remote_repo_info['repo'], ver): remote_has_tag = True if not ('-f' in dict_cmd_para.keys() or '-force' in dict_cmd_para.keys()): # 没有强制标志,需要进行验证 if base == 'local': if not FCMMGitTools.is_bare(remote_repo_info): return [3, config['i18n_tips']['remote_not_bare']] if remote_has_tag: # 检查版本号是否已存在 return [3, config['i18n_tips']['remote_tag_exists']] # 检查通过或强制执行 is_force_reset = False # 标记是否强制更新服务器端版本 if base == 'local': # 本地为准,打包备份到指备份目录中 if not FCMMGitTools.is_bare(remote_repo_info): FCMMTools.backup_to_tar( src_path=remote_repo_info['work_dir'], save_path=config['backup_path'], save_name='%s.bak.%s.tar' % (remote_name, datetime.datetime.now().strftime("%Y%m%d%H%M%S"))) if '-r' in dict_cmd_para.keys() or '-reset' in dict_cmd_para.keys( ): # 强制替换服务器端的版本,以本地的版本库为准,直接强制替换 # 如果不是git仓库,先初始化 os.chdir(repo_info['work_dir']) if repo_info['repo'] is None: fun_res = FCMMTools.run_sys_cmd('git init') if fun_res[0] != 0: return [ fun_res[0], config['i18n_tips']['execute_fail'] ] repo_info = FCMMGitTools.get_repo_info( repo_info['work_dir']) else: # 如果原来有远程连接,解除连接 for remote_obj in repo_info['repo'].remotes: fun_res = FCMMTools.run_sys_cmd('git remote rm %s' % (remote_obj.name)) if fun_res[0] != 0: return [ fun_res[0], config['i18n_tips']['execute_fail'] ] # 绑定远程仓库 fun_res = FCMMTools.run_sys_cmd('git remote add origin %s' % (url)) if fun_res[0] != 0: return [fun_res[0], config['i18n_tips']['execute_fail']] # 指定强制更新服务器端 is_force_reset = True remote_has_tag = False else: # 保留服务器端版本信息,用文件清除方式实现文件替换 # 1:删除临时目录中远程分支的所有文件 os.chdir(remote_repo_info['work_dir']) FCMMTools.run_sys_cmd('git checkout master') FCMMTools.run_sys_cmd('git rm * -r') # 2: 将本地目录中的文件复制到远程目录,删除本地目录,复制远程目录到本地目录 FileTools.copy_all_with_path(repo_info['work_dir'], remote_repo_info['work_dir'], '^(?!\\.git$)') FileTools.remove_all_with_path(repo_info['work_dir']) FileTools.copy_all_with_path(remote_repo_info['work_dir'], repo_info['work_dir']) os.chdir(repo_info['work_dir']) else: # 远程为准,打包备份到指备份目录中 if os.listdir(repo_info['work_dir']): FCMMTools.backup_to_tar( src_path=repo_info['work_dir'], save_path=config['backup_path'], save_name='%s.%s.tar' % (repo_name, datetime.datetime.now().strftime("%Y%m%d%H%M%S"))) # 复制远程目录到本地目录 os.chdir(remote_repo_info['work_dir']) FileTools.remove_all_with_path(repo_info['work_dir']) FileTools.copy_all_with_path(remote_repo_info['work_dir'], repo_info['work_dir']) os.chdir(repo_info['work_dir']) # 完成本地版本库的建立和更新,统一进行配置参数处理和服务器的推送 FCMMTools.run_sys_cmd('git checkout master') fcmm_config_file = repo_info['work_dir'].rstrip('\\/') + '/.fcmm4git' if not os.path.exists(fcmm_config_file): fcmm_config = dict() fcmm_config['remote_url'] = url if '-n' in dict_cmd_para.keys() or '-nopkg' in dict_cmd_para.keys( ): fcmm_config['has_pkg'] = "false" else: fcmm_config['has_pkg'] = "true" FCMMTools.save_to_json_file(fcmm_config_file, fcmm_config) # 提交修改 FCMMTools.run_sys_cmd('git add *') fun_res = FCMMTools.run_sys_cmd( 'git commit -am "add .fcmm4git by fcmm4git"') if fun_res[0] != 0: return [fun_res[0], config['i18n_tips']['execute_fail']] # 设置版本信息 ver = FCMMTools.get_cmd_para_value(dict_cmd_para, '-v', '-version') if ver is not None: if remote_has_tag: # 远程服务器已经有标签,应先删除标签,再新建 fun_res = FCMMTools.run_sys_cmd('git tag -d %s' % (ver)) if fun_res[0] != 0: return [ fun_res[0], config['i18n_tips']['execute_fail'] ] fun_res = FCMMTools.run_sys_cmd( 'git tag -a %s -m "add version by fcmm4git"' % (ver)) if fun_res[0] != 0: return [fun_res[0], config['i18n_tips']['execute_fail']] else: # 如果已经有.fcmm4git配置文件说明该目录已经初始化过,同步下来即可,不用再重新推送服务器 return [0, config['i18n_tips']['just_clone_remote']] # 推送到服务器端 push_force_tag = '' if is_force_reset: push_force_tag = '-f ' fun_res = FCMMTools.run_sys_cmd( 'git push %s--follow-tags origin master' % (push_force_tag)) if fun_res[0] != 0: return [fun_res[0], config['i18n_tips']['execute_fail']] # 添加版本分支 if not ('-n' in dict_cmd_para.keys() or '-nopkg' in dict_cmd_para.keys()): if remote_has_pkg: # 远程仓库已有版本分支,要删除并强制推送 fun_res = FCMMTools.run_sys_cmd('git branch -d lb-pkg') if fun_res[0] != 0: return [fun_res[0], config['i18n_tips']['execute_fail']] push_force_tag = '-f ' # 指定强制推送 fun_res = FCMMTools.run_sys_cmd('git checkout -b lb-pkg') if fun_res[0] != 0: return [fun_res[0], config['i18n_tips']['execute_fail']] fun_res = FCMMTools.run_sys_cmd( 'git push %s--follow-tags origin lb-pkg' % (push_force_tag)) if fun_res[0] != 0: return [fun_res[0], config['i18n_tips']['execute_fail']] FCMMTools.run_sys_cmd('git checkout master') # 返回执行成功 return [0, config['i18n_tips']['execute_success']]