Esempio n. 1
0
    def cmd_add_temp(dict_cmd_para=None):
        """
        新增FCMM的开发者临时分支,如果原分支存在,可以重置分支

        @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-temp', dict_cmd_para)
        if is_exit:
            return res

        # 进一步检查
        branch_name = 'tb-dev-%s' % (FCMMTools.get_cmd_para_value(
            dict_cmd_para, '-n', '-name'))
        clone_branch_name = FCMMGitTools.get_active_branch(repo_info)
        res = FCMMGitTools.get_remote_branch(repo_info, branch_name)
        has_branch = False
        if res[0] == 0:
            has_branch = True
        elif not res[2]:
            # 远程节点存在但处理失败
            res[1] = FCMMTools.get_i18n_tips(config, 'execute_fail')
            return res
        if not ('-f' in dict_cmd_para.keys()
                or '-force' in dict_cmd_para.keys()):
            if has_branch:
                return [
                    1,
                    FCMMTools.get_i18n_tips(config, 'branch_has_exists',
                                            branch_name)
                ]

        # 进行处理
        para_bare = FCMMTools.get_cmd_para_value(dict_cmd_para, '-b', '-bare')
        if has_branch:
            if para_bare:
                res = FCMMGitTools.overwrite_branch(repo_info,
                                                    branch_name,
                                                    is_bare=True)
            else:
                res = FCMMGitTools.overwrite_branch(
                    repo_info, branch_name, src_branch=clone_branch_name)
        else:
            if para_bare:
                res = FCMMGitTools.add_branch(repo_info,
                                              branch_name,
                                              is_bare=True)
            else:
                res = FCMMGitTools.add_branch(repo_info,
                                              branch_name,
                                              src_branch=clone_branch_name)

        return res
Esempio n. 2
0
    def cmd_merge(dict_cmd_para=None):
        """
        将指定分支版本合并到当前分支中

        @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(
            'merge', dict_cmd_para)
        if is_exit:
            return res

        # 进一步检查
        branch_name = FCMMTools.get_cmd_para_value(dict_cmd_para, '-d',
                                                   '-dest')
        if branch_name is None:
            # 获取当前工作分支
            branch_name = FCMMGitTools.get_active_branch(repo_info)
        else:
            # 检查分支是否存在
            res = FCMMGitTools.get_remote_branch(repo_info, branch_name)
            if res[0] != 0:
                return [
                    1,
                    FCMMTools.get_i18n_tips(config, 'branch_not_exists',
                                            branch_name)
                ]
        source_branch = FCMMTools.get_cmd_para_value(dict_cmd_para, '-s',
                                                     '-source')
        if source_branch is None:
            if fcmm_config['has_pkg'] == 'true':
                source_branch = 'lb-pkg'
            else:
                source_branch = 'master'
        else:
            # 检查分支是否存在
            res = FCMMGitTools.get_remote_branch(repo_info, source_branch)
            if res[0] != 0:
                return [
                    1,
                    FCMMTools.get_i18n_tips(config, 'branch_not_exists',
                                            source_branch)
                ]
        # 检查分支是否同一个
        if branch_name == source_branch:
            return [
                1,
                FCMMTools.get_i18n_tips(config, 'check_branch_is_same',
                                        source_branch)
            ]
        ver = FCMMTools.get_cmd_para_value(dict_cmd_para, '-v', '-version')
        # 检查版本号是否一致
        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)]
        # 获取版本号
        tag = FCMMTools.get_cmd_para_value(dict_cmd_para, '-t', '-tag')
Esempio n. 3
0
    def cmd_rollback(dict_cmd_para=None):
        """
        将指定分支回退到指定版本

        @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(
            'rollback', dict_cmd_para)
        if is_exit:
            return res

        # 进一步检查
        branch_name = FCMMTools.get_cmd_para_value(dict_cmd_para, '-n',
                                                   '-name')
        if branch_name is None:
            # 获取当前工作分支
            branch_name = FCMMGitTools.get_active_branch(repo_info)
        else:
            # 检查分支是否存在
            res = FCMMGitTools.get_remote_branch(repo_info, branch_name)
            if res[0] != 0:
                return [
                    1,
                    FCMMTools.get_i18n_tips(config, 'branch_not_exists',
                                            branch_name)
                ]

        # master和lb-pkg分支不允许回退
        if not ('-f' in dict_cmd_para.keys()
                or '-force' in dict_cmd_para.keys()):
            if branch_name == 'master' or branch_name == 'lb-pkg':
                return [
                    1,
                    FCMMTools.get_i18n_tips(config, 'master_pkg_no_rollback')
                ]

        ver = FCMMTools.get_cmd_para_value(dict_cmd_para, '-v', '-version')
        tag = FCMMTools.get_cmd_para_value(dict_cmd_para, '-t', '-tag')
        if ver is None and tag is None:
            return [
                1,
                FCMMTools.get_i18n_tips(config, 'must_has_para',
                                        'version / tag')
            ]

        # 进行处理
        # 先进行备份
        if config['backup_before'] == 'true':
            res = FCMMGitTools.backup_branch(
                repo_info, branch_name,
                FCMMGitTools.get_git_config_user_name(repo_info,
                                                      config['consle_encode']))
        if res[0] == 0:
            # 处理回滚
            if branch_name == 'master' or branch_name == 'lb-pkg':
                # 采取回滚方式处理
                if ver is not None:
                    res = FCMMGitTools.rollback_to_tag(repo_info, branch_name,
                                                       ver)
                else:
                    res = FCMMGitTools.rollback_to_commit(
                        repo_info, branch_name, tag)
            else:
                # 采取覆盖方式处理
                src_branch = 'master'
                if fcmm_config['has_pkg'] == 'true':
                    src_branch = 'lb-pkg'
                res = FCMMGitTools.overwrite_branch(repo_info,
                                                    branch_name,
                                                    src_branch,
                                                    tag=ver,
                                                    commit=tag)

        # 返回结果
        return res
Esempio n. 4
0
    def cmd_common_init(cmd_str='', dict_cmd_para=None):
        """
        通用命令前期通用处理
        具体处理的内容包括:
            1、判断是否帮助
            2、基础参数校验
            3、检查当前环境是否未提交,如果未提交不允许继续处理
            4、检查本地配置文件及仓库信息是否准确
            5、更新master分支和lb-pkg分支的最新本地版本

        @decorators staticmethod

        @param {dict} dict_cmd_para=None - 参数字典

        @returns {tuple} - 返回多个值的数组,按顺序如下
            is_exit {bool} - 标识是否应直接,不进行后续的处理
            return_obj {list} - 直接退出的执行结果[returncode, msgstring]
                returncode - 0代表成功,其他代表失败
                msgstring - 要返回显示的内容
            config {dict} - 获取到的全局参数config
            fcmm_config {dict} - 获取到的.fcmm4git配置信息
            repo_info {dict} - 获取到的本地仓库信息
            current_branch {string} - 当前本地工作分支

        """
        # 判断是否有帮助
        if '-h' in dict_cmd_para.keys() or '-help' in dict_cmd_para.keys():
            return (True, FCMMGitCmd.cmd_help({cmd_str:
                                               ''}), None, None, None, None)

        # 最基础的参数校验
        res = FCMMTools.vailidate_cmd_para(dict_cmd_para, cmd_str)
        if res[0] != 0:
            return (True, res, None, None, None, None)

        # 本地仓库信息检查
        config = RunTools.get_global_var('config')
        repo_info = FCMMGitTools.get_repo_info(os.getcwd())
        fcmm_config = FCMMTools.get_fcmm_config(repo_info['work_dir'])
        if repo_info['repo'] is None or fcmm_config is None:
            return (True,
                    [2, FCMMTools.get_i18n_tips(config, 'local_git_error')
                     ], None, None, None, None)

        # 检查当前分支是否存在未提交信息
        if FCMMGitTools.is_dirty(repo_info):
            return (True, [
                2,
                FCMMTools.get_i18n_tips(config, 'current_branch_is_dirty')
            ], None, None, None, None)

        # 下载最新的master分支
        current_branch = FCMMGitTools.get_active_branch(repo_info)
        try:
            res = FCMMTools.run_sys_cmd_list(
                ['git checkout master', 'git pull origin master'])
            if res[0] != 0:
                return (True, [
                    res[0],
                    FCMMTools.get_i18n_tips(config, 'execute_fail')
                ], None, None, None, None)
            fcmm_config = FCMMTools.get_fcmm_config(repo_info['work_dir'])
            if fcmm_config is None:
                return (True, [
                    2, FCMMTools.get_i18n_tips(config, 'local_git_error')
                ], None, None, None, None)

            # 判断是否有版本分支
            if fcmm_config['has_pkg'] == "true":
                # 要下载版本分支,绑定与本地版本分支的关系
                res = FCMMGitTools.get_remote_branch(repo_info, 'lb-pkg')

                # 处理结果
                if res[0] != 0:
                    return (True, [
                        res[0],
                        FCMMTools.get_i18n_tips(config, 'execute_fail')
                    ], None, None, None, None)

        finally:
            # 最后尝试跳转回工作分支
            FCMMTools.run_sys_cmd('git checkout %s' % (current_branch))

        # 最后返回
        return (False, [0, ''], config, fcmm_config, repo_info, current_branch)
Esempio n. 5
0
    def cmd_add_dev(dict_cmd_para=None):
        """
        新增FCMM的dev分支,如果原分支存在,可以重置分支

        @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-dev', dict_cmd_para)
        if is_exit:
            return res

        # 进一步检查
        branch_name = 'tb-%s-%s' % (
            FCMMTools.get_cmd_para_value(dict_cmd_para, '-t', '-type '),
            FCMMTools.get_cmd_para_value(dict_cmd_para, '-n', '-name'))
        res = FCMMGitTools.get_remote_branch(repo_info, branch_name)
        has_branch = False
        if res[0] == 0:
            has_branch = True
        elif not res[2]:
            # 远程节点存在但处理失败
            res[1] = FCMMTools.get_i18n_tips(config, 'execute_fail')
            return res
        if not ('-f' in dict_cmd_para.keys()
                or '-force' in dict_cmd_para.keys()):
            if has_branch:
                return [
                    1,
                    FCMMTools.get_i18n_tips(config, 'branch_has_exists',
                                            has_branch)
                ]

        clone_branch_name = ''
        para_clone = FCMMTools.get_cmd_para_value(dict_cmd_para, '-c',
                                                  '-clone')
        ver = FCMMTools.get_cmd_para_value(dict_cmd_para, '-v', '-version')
        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 para_clone is None or ver is not None:
            if fcmm_config['has_pkg'] == "true":
                clone_branch_name = 'lb-pkg'
            else:
                clone_branch_name = 'master'
        else:
            clone_branch_name = 'tb-' + para_clone
            res = FCMMGitTools.get_remote_branch(repo_info, clone_branch_name)
            if res[0] != 0:
                return [
                    1,
                    FCMMTools.get_i18n_tips(config, 'branch_not_exists',
                                            clone_branch_name)
                ]
        tag = FCMMTools.get_cmd_para_value(dict_cmd_para, '-tag', '-tag')

        # 进行实际的处理
        if has_branch:
            # 先备份
            if config['backup_before'] == 'true':
                res = FCMMGitTools.backup_branch(
                    repo_info, branch_name,
                    FCMMGitTools.get_git_config_user_name(
                        repo_info, config['consle_encode']))
            if res[0] == 0:
                res = FCMMGitTools.overwrite_branch(
                    repo_info,
                    branch_name,
                    src_branch=clone_branch_name,
                    tag=ver,
                    commit=tag)
        else:
            res = FCMMGitTools.add_branch(repo_info,
                                          branch_name,
                                          src_branch=clone_branch_name,
                                          tag=ver,
                                          commit=tag)

        return res
Esempio n. 6
0
    def cmd_add_cfg(dict_cmd_para=None):
        """
        新增FCMM的cfg分支,如果原分支存在,可以重置分支

        @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-cfg', dict_cmd_para)
        if is_exit:
            return res

        # 进一步检查
        cfg_branch_name = 'lb-cfg-' + FCMMTools.get_cmd_para_value(
            dict_cmd_para, '-n', '-name')
        res = FCMMGitTools.get_remote_branch(repo_info, cfg_branch_name)
        has_cfg_branch = False
        if res[0] == 0:
            has_cfg_branch = True
        elif not res[2]:
            # 远程节点存在但处理失败
            res[1] = FCMMTools.get_i18n_tips(config, 'execute_fail')
            return res

        if not ('-f' in dict_cmd_para.keys()
                or '-force' in dict_cmd_para.keys()):
            if has_cfg_branch:
                return [
                    1,
                    FCMMTools.get_i18n_tips(config, 'branch_has_exists',
                                            cfg_branch_name)
                ]

        para_clone = FCMMTools.get_cmd_para_value(dict_cmd_para, '-c',
                                                  '-clone')
        para_bare = FCMMTools.get_cmd_para_value(dict_cmd_para, '-b', '-bare')
        if para_bare is None and para_clone is None:
            return [
                1,
                FCMMTools.get_i18n_tips(config, 'must_has_para',
                                        '-bare / -b 或 -clone / -c')
            ]

        clone_branch_name = ''
        if para_bare is None:
            clone_branch_name = 'lb-cfg-' + para_clone
            res = FCMMGitTools.get_remote_branch(repo_info, clone_branch_name)
            if res[0] != 0:
                return [
                    1,
                    FCMMTools.get_i18n_tips(config, 'branch_not_exists',
                                            clone_branch_name)
                ]

        # 进行实际的处理
        if has_cfg_branch:
            # 先备份
            if config['backup_before'] == 'true':
                res = FCMMGitTools.backup_branch(
                    repo_info, cfg_branch_name,
                    FCMMGitTools.get_git_config_user_name(
                        repo_info, config['consle_encode']))
            if res[0] == 0:
                if clone_branch_name != '':
                    res = FCMMGitTools.overwrite_branch(
                        repo_info,
                        cfg_branch_name,
                        src_branch=clone_branch_name)
                else:
                    res = FCMMGitTools.overwrite_branch(repo_info,
                                                        cfg_branch_name,
                                                        is_bare=True)
        else:
            if clone_branch_name != '':
                res = FCMMGitTools.add_branch(repo_info,
                                              cfg_branch_name,
                                              src_branch=clone_branch_name)
            else:
                res = FCMMGitTools.add_branch(repo_info,
                                              cfg_branch_name,
                                              is_bare=True)

        return res
Esempio n. 7
0
    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