Example #1
0
 def backup_added_and_modified_file(self,
                                    reverse=False,
                                    check_name='',
                                    debug=False):
     """备份或还原增改的文件"""
     for name, key in self.result_path.items():
         path = self.get_validate_result_name(key)
         if check_name == '' or check_name == name:
             print(f'备份 {name} 的文件: {path}')
             dir_list, file_list = self.pares_files(path)
             print(f'共 {len(dir_list)} 个目录,{len(file_list)} 个文件')
             for file in file_list:
                 source_file = os.path.join(self.source_dir, file)
                 target_file = os.path.join(self.root_dir, key, file)
                 if reverse:
                     source_file, target_file = target_file, source_file
                 print('[debug]' if debug else '[do]' +
                       f' {source_file} -> {target_file}')
                 if not debug:
                     filex.check_and_create_dir(target_file)
                     if os.path.isfile(source_file):
                         shutil.copy(source_file, target_file)
                     else:
                         if os.path.exists(target_file):
                             print('删除' + target_file)
                             shutil.rmtree(target_file)
                         shutil.copytree(source_file, target_file)
Example #2
0
    def order_tips_file(tips_names_file, processed_dir, result_dir):
        """
        排序tips的翻译文件
        :param tips_names_file:
        :param processed_dir:
        :param result_dir:
        :return:
        """

        file_dict = Tips.get_file_dict_in_dir(processed_dir)
        if file_dict is None:
            return

        lines = filex.read_lines(tips_names_file, ignore_line_separator=True)
        if lines is None:
            return

        length = len(lines)
        for i in range(length):
            line = lines[i]
            en_name, cn_name = line.split('=')
            if en_name in file_dict.keys():
                old_name = file_dict[en_name]
                dir_name, file_name = os.path.split(old_name)
                new_name = '%s\\%03d-%s' % (result_dir, i + 1, file_name)
                print('复制%s为%s' % (old_name, new_name))
                filex.check_and_create_dir(new_name)
                shutil.copy(old_name, new_name)
            else:
                print('没有文件' + en_name)
Example #3
0
 def copy_file(self, source, target):
     """复制文件"""
     filex.check_and_create_dir(target)
     source_md5 = self.get_file_md5(source)
     target_md5 = self.get_file_md5(target)
     if source_md5 == target_md5:
         print('文件已相同')
     else:
         shutil.copyfile(source, target)
Example #4
0
    def copy_file_to_sdcard(src_dir, dst_dir):
        """复制文件到内存卡

        该需求是因为部分设备读取内存卡不是按照文件名顺序
        可能是按照存储顺序?
        于是格式化内存卡,然后按顺序指定多个目录,复制进内存卡
        """
        files = []
        is_list = False
        if isinstance(src_dir, list):
            is_list = True
            for path in src_dir:
                files.extend(filex.list_file(path))
        else:
            files.extend(filex.list_file(src_dir))
        length = len(files)
        print(f'共 {length} 个文件')
        if length == 0:
            return
        print(f'读取文件大小')
        sum_size = 0
        for i, file in enumerate(files):
            size = os.path.getsize(file)
            sum_size += size
            print(f'{i + 1}/{length},{file} {filex.parse_file_size(size)}')
        print(f'文件总大小 {filex.parse_file_size(sum_size)}')

        print()
        print(f'开始复制文件')
        copy_size = 0
        start_time = time.time()
        for i, file in enumerate(files):
            if is_list:
                # 如果是多个目录,取目录名
                dir_name = os.path.dirname(file)
                dst_file = os.path.join(dst_dir, os.path.basename(dir_name),
                                        os.path.basename(file))
            else:
                # 如果是单个目录,直接取相对路径
                src_file = os.path.relpath(file, src_dir)
                dst_file = os.path.join(dst_dir, src_file)
            print(
                f'复制文件 {i + 1}/{length},{filex.get_file_size_str(file)},{file} -> {dst_file}'
            )
            # 复制文件
            filex.check_and_create_dir(dst_file)
            shutil.copyfile(file, dst_file)
            # 因为太大,需要记录进度
            copy_size += os.path.getsize(file)
            print(
                f'已复制 {filex.parse_file_size(copy_size)}/{filex.parse_file_size(sum_size)}'
            )
            # 记录时间
            FileUtils.count_time(start_time, copy_size / 1024 / 1024,
                                 sum_size / 1024 / 1024, 'M')
Example #5
0
 def export_rooms_to_excel(self, excel_file_path, rooms):
     """写入 excel ,不过滤数据,如果需要,请提前过滤数据"""
     if rooms:
         print('准备写入 excel,共 %d 条数据' % len(rooms))
         # 标题
         data = [self.get_title()]
         for value in rooms.values():
             data.append(self.sort_value(value))
         if os.path.exists(excel_file_path):
             os.remove(excel_file_path)
         filex.check_and_create_dir(excel_file_path)
         excelx.write_list_to_excel(excel_file_path, data)
Example #6
0
 def copy_dir(source_dir, target_dir, file_list=None):
     """备份"""
     if file_list is None:
         file_list = TranslationFile.file_list
     for file in file_list:
         source_file = '%s/%s' % (source_dir, file)
         if not os.path.exists(source_file):
             print('文件不存在%s' % source_file)
             continue
         target_file = '%s/%s' % (target_dir, file)
         if os.path.exists(target_file):
             if filecmp.cmp(source_file, target_file):
                 print('文件已相同%s与%s' % (source_file, target_file))
                 continue
         filex.check_and_create_dir(target_file)
         shutil.copyfile(source_file, target_file)
         print('复制文件%s到%s' % (source_file, target_file))
     pass
Example #7
0
 def copy_required_class_of_uml(uml_file, source_root, target_root):
     """
     读取 uml 文件中所需的 java 类,从 source_root 复制到 target_root
     """
     root = Et.parse(uml_file)
     nodes = root.find('nodes')
     for node in nodes.iter('node'):
         class_name = node.text
         # 根据需要替换
         class_name = class_name.replace('java2', 'java')
         class_path = os.path.sep + class_name.replace('.', os.path.sep) + '.java'
         source_path = source_root + class_path
         target_path = target_root + class_path
         print('%s → %s' % (source_path, target_path))
         if os.path.exists(source_path):
             filex.check_and_create_dir(target_path)
             shutil.copyfile(source_path, target_path)
         else:
             print('%s 不存在' % source_path)
Example #8
0
    def compress_image(self, source_file):
        target_file = source_file.replace(self.source_dir, self.target_dir)
        # 创建目录
        filex.check_and_create_dir(target_file)

        print(f'开始压缩 {source_file}->{target_file}')
        source = tinify.from_file(source_file)
        try:
            source.to_file(target_file)
        except tinify.errors.AccountError:
            print('压缩数量已超限')
            exit()

        source_length = filex.get_file_size_str(source_file)
        target_length = filex.get_file_size_str(target_file)
        print(f'压缩完成 {source_length}->{target_length}')
        print(f'本月已压缩 {tinify.compression_count} 张')
        if tinify.compression_count >= 500:
            print('压缩数量已超限')
            exit()
Example #9
0
    def copy_resources_en_jar(self):
        """复制 jar"""
        if not os.path.exists(self.path):
            print('软件目录不存在 %s' % self.path)
            return

        build_file_path = self.path + os.sep + 'build.txt'
        if not os.path.exists(build_file_path):
            print('build.txt 不存在')
            return
        destination_build_file_path = '%s/jars/%s/英文包/%s/%s' % (self.work_dir, self.name, self.version, 'build.txt')
        print('复制 %s 到 %s' % (build_file_path, destination_build_file_path))
        filex.check_and_create_dir(destination_build_file_path)
        shutil.copyfile(build_file_path, destination_build_file_path)

        jar_file_path = self.path + os.sep + 'lib' + os.sep + 'resources_en.jar'
        if not os.path.exists(jar_file_path):
            print('jar 包不存在')
            return
        print('复制 %s 到 %s' % (jar_file_path, self.en_jar_path))
        shutil.copyfile(jar_file_path, self.en_jar_path)
Example #10
0
    def extra_tips_manifest_file(self):
        """解压出 tips 的清单文件

        Android Studio 和 IntelliJIDEA位于 resources.jar\META-INF\IdeTipsAndTricks.xml
        goland.jar
        phpstorm.jar\META-INF\PhpStormTipsAndTricks.xml
        pycharm.jar
        rubymine.jar
        webstorm.jar
        """

        # jar 包路径
        jar_path = self.path + os.sep + 'lib' + os.sep
        if self.name.lower() in ['AndroidStudio'.lower(), 'IntelliJIDEA'.lower()]:
            jar_path += 'resources'
        else:
            jar_path += self.name
        jar_path += '.jar'

        # 在 jar 包内的路径
        tips_file_in_jar = 'META-INF/'
        if self.name.lower() in [i.lower() for i in ['PhpStorm', 'CLion']]:
            tips_file_in_jar += self.name
        elif self.name.lower() == 'GoLand'.lower():
            tips_file_in_jar += 'Go'
        else:
            tips_file_in_jar += 'Ide'
        tips_file_in_jar += 'TipsAndTricks.xml'
        print(jar_path, tips_file_in_jar)

        project_path = self.work_dir + os.sep + 'source' + os.sep + self.name
        tips_file_path = project_path + os.sep + 'IdeTipsAndTricks' + os.sep + 'IdeTipsAndTricks.xml'

        if not os.path.exists(jar_path):
            print('jar 包不存在', jar_path)
            return

        filex.check_and_create_dir(tips_file_path)
        ZipTools.extra_file(jar_path, tips_file_in_jar, tips_file_path)
Example #11
0
 def zip_jar(source_dir, target_jar, rename_cn=False):
     """
     压缩文件夹
     :param source_dir:
     :param target_jar:
     :param rename_cn: 是否将 _zh_CN 重命名再压缩
     :return:
     """
     print('压缩 %s 到 %s' % (source_dir, target_jar))
     filex.check_and_create_dir(target_jar)
     translation_file_list = []
     for root, dirs, files in os.walk(source_dir):
         for file_name in files:
             path = root + '/' + file_name
             translation_file_list.append(path.replace(source_dir, '').replace('\\', '/').lstrip('/'))
     with zipfile.ZipFile(target_jar, 'a') as zip_file:
         for file in translation_file_list:
             if rename_cn:
                 arcname = file.replace('_zh_CN', '')
             else:
                 arcname = file
             # print('压缩 %s 为 %s' % (file, arcname))
             zip_file.write(source_dir + os.sep + file, arcname)
Example #12
0
 def zip(source_dir, target_jar, source_jar=None, all_file=False):
     """
     将 source_jar 的文件压缩进 target_jar
     如果 source_dir 中存在文件,则取 source_dir 中的,不取 source_jar 中的
     :param source_dir: 要压缩的目录
     :param target_jar: 目标 jar
     :param source_jar: 源 jar
     :param all_file: 如果为 false,只压缩 source_dir 对应的文件
     如果为 true ,压缩所有文件
     :return:
     """
     # 移除 target
     if os.path.exists(target_jar):
         os.remove(target_jar)
     # 创建目录
     filex.check_and_create_dir(target_jar)
     # 写入 source_jar 中的文件
     if source_jar is not None:
         # 记录文件和目录
         translation_dir_list = []
         translation_file_list = []
         for root, dirs, files in os.walk(source_dir):
             for dir_name in dirs:
                 path = root + '/' + dir_name
                 translation_dir_list.append(path.replace(source_dir, '').replace('\\', '/').lstrip('/'))
             for file_name in files:
                 path = root + '/' + file_name
                 translation_file_list.append(path.replace(source_dir, '').replace('\\', '/').lstrip('/'))
         print(translation_dir_list)
         print(translation_file_list)
         # 解压仅存在于 source_jar 中,source_dir 中没有的内容
         print('压缩仅在 source_jar 中的文件')
         with zipfile.ZipFile(target_jar, 'a') as target_zip_file:
             with zipfile.ZipFile(source_jar, 'r') as zip_file:
                 for name in zip_file.namelist():
                     need_extract = all_file
                     # 如果是所有文件,那么 source_dir 中不包含的文件夹也要压缩
                     if not need_extract:
                         # 如果不是所有文件,那么只压缩 source_dir 中包含的文件夹
                         for translation_dir in translation_dir_list:
                             if name.startswith(translation_dir):
                                 # 以源目录开头
                                 need_extract = True
                                 break
                     if need_extract:
                         # 需要压缩文件夹,再判断文件是否存在于 source_dir 中,存在则不需要压缩
                         cn_name = '_zh_CN'.join(os.path.splitext(name))
                         if name in translation_file_list or cn_name in translation_file_list:
                             # 存在于中文或英文
                             need_extract = False
                     if need_extract:
                         if all_file:
                             arcname = name.replace('_zh_CN', '')
                         else:
                             arcname = name
                         # 写入文件
                         # print('压缩 %s 为 %s' % (name, arcname))
                         with zip_file.open(name) as tmp_file:
                             target_zip_file.writestr(arcname, tmp_file.read())
     # 压缩翻译内容,如果是所有文件,则需要重命名
     ZipTools.zip_jar(source_dir, target_jar, all_file)