Example #1
0
def test_upload_apk_with_protected():
    ftp_config_path = r'D:\auto_build\pytxxy'
    ver_name_info = '5.1.5beta_t_01'
    channel = 'oppo'  # 调用的接口内部实现默认是空串
    target_name = '5.1.5beta_t_01-702-20200930.apk'
    source_name = '5.1.5beta_t_01-702-20200930_sec.apk'
    ver_env = 'test'
    prj_code_ver = '319e2dd5f6a7043fde8770a08079fe26f0d65d92'
    app_code = 'pyzx'

    to_upload_path = r'D:\auto_build\pytxxy\output\test\20200930_142454'
    mobile_os = 'Android'

    desc_data = {'info': 'just for test'}

    print(f'ftp_config_path: {ftp_config_path}')
    print(f'ver_name_info: {ver_name_info}')
    print(f'target_name: {target_name}')
    print(f'source_name: {source_name}')
    print(f'channel: {channel}')

    ftp_upload.upload_to_sftp(ftp_config_path,
                              ver_name_info,
                              ver_env,
                              prj_code_ver,
                              app_code,
                              to_upload_path,
                              mobile_os=mobile_os,
                              channel=channel,
                              target_file_name=target_name,
                              source_file_name=source_name,
                              desc_data=desc_data)
Example #2
0
    def _upload_file(self, whole_ver_name, ver_env):
        # ftp_config_path = os.path.join(self.work_path, 'config')
        ftp_config_path = self.work_path
        print('ver_name_info:', whole_ver_name)
        print('ver_env: ', ver_env)

        ftp_upload.upload_to_sftp(ftp_config_path,
                                  whole_ver_name,
                                  ver_env,
                                  self.prj_code_ver,
                                  mobile_os='wechat')
    def _upload_file(self, source_name, target_name, to_upload_path):
        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.prj_code_ver, 'gzjd', to_upload_path,
                                  'Android', target_name, source_name)
Example #4
0
    def _upload_file(self, source_name, target_name, to_upload_path, desc_data=None):
        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)
            log_error(str_info)
            raise Exception(str_info)

        channel = ''  # 调用的接口内部实现默认是空串
        if self.channel != BuilderLabel.DEFAULT_CHAN:
            channel = self.channel

        # ftp_config_path = os.path.join(self.work_path, 'config')
        ftp_config_path = self.work_path
        log_info(f'ver_name_info: {ver_name_info}')
        log_info(f'target_name: {target_name}')
        log_info(f'source_name: {source_name}')
        log_info(f'channel: {channel}')

        ftp_upload.upload_to_sftp(ftp_config_path, ver_name_info, self.ver_env, self.prj_code_ver, self.app_code, to_upload_path, mobile_os='Android', channel=channel, target_file_name=target_name, source_file_name=source_name, desc_data=desc_data)
Example #5
0
    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)
Example #6
0
    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)