示例#1
0
def test_from_path_except():
    """测试from_path_except方法是否能正常工作。
    """
    fc = FileCollection.from_path_except(dir_path, ignore=[
        "subfolder",
    ])
    basename_list = [winfile.basename for winfile in fc.iterfiles()]
    basename_list.sort()
    expect = ["root_file.txt", "root_image.jpg"]
    expect.sort()
    assert basename_list == expect

    fc = FileCollection.from_path_except(dir_path, ignore_ext=[".jpg"])
    basename_list = [winfile.basename for winfile in fc.iterfiles()]
    basename_list.sort()
    expect = ["root_file.txt", "sub_file.txt"]
    expect.sort()
    assert basename_list == expect

    fc = FileCollection.from_path_except(dir_path, ignore_pattern=["image"])
    basename_list = [winfile.basename for winfile in fc.iterfiles()]
    basename_list.sort()
    expect = ["root_file.txt", "sub_file.txt"]
    expect.sort()
    assert basename_list == expect
示例#2
0
def backup_dir(filename,
               root_dir,
               ignore=None,
               ignore_ext=None,
               ignore_pattern=None):
    """The backup utility method. Basically it just add files that need to be
    backupped to zip archives.

    :param filename: the output file name, DO NOT NEED FILE EXTENSION.
    :param root_dir: the directory you want to backup.
    :param ignore: file or directory defined in this list will be ignored.
    :param ignore_ext: file with extensions defined in this list will be ignored.
    :param ignore_pattern: any file or directory that contains this pattern
      will be ignored.
    """
    tab = "    "
    # Step 1, calculate files to backup
    root_dir = os.path.abspath(root_dir)
    print("Perform backup '%s'..." % root_dir)
    print(tab + "1. Calculate files...")

    total_size_in_bytes = 0

    init_mode = WinFile.init_mode
    WinFile.use_regular_init()
    fc = FileCollection.from_path_except(root_dir, ignore, ignore_ext,
                                         ignore_pattern)
    WinFile.set_initialize_mode(complexity=init_mode)

    for winfile in fc.iterfiles():
        total_size_in_bytes += winfile.size_on_disk

    # Step 2, write files to zip archive
    print(tab * 2 + "Done, got %s files, total size is %s." %
          (len(fc), repr_data_size(total_size_in_bytes)))
    print(tab + "2. Backup files...")

    basename = "%s %s.zip" % (filename,
                              datetime.now().strftime("%Y-%m-%d %Hh-%Mm-%Ss"))

    print(tab * 2 + "Write to '%s'..." % basename)
    current_dir = os.getcwd()
    with ZipFile(basename, "w") as f:
        os.chdir(root_dir)
        for winfile in fc.iterfiles():
            relpath = os.path.relpath(winfile.abspath, root_dir)
            f.write(relpath)
    os.chdir(current_dir)

    print(tab + "Complete!")
示例#3
0
def test_from_path_except():
    """测试from_path_except方法是否能正常工作。
    """
    fc = FileCollection.from_path_except(dir_path, ignore=["subfolder", ])
    basename_list = [winfile.basename for winfile in fc.iterfiles()]
    basename_list.sort()
    expect = ["root_file.txt", "root_image.jpg"]
    expect.sort()
    assert basename_list == expect

    fc = FileCollection.from_path_except(dir_path, ignore_ext=[".jpg"])
    basename_list = [winfile.basename for winfile in fc.iterfiles()]
    basename_list.sort()
    expect = ["root_file.txt", "sub_file.txt"]
    expect.sort()
    assert basename_list == expect

    fc = FileCollection.from_path_except(dir_path, ignore_pattern=["image"])
    basename_list = [winfile.basename for winfile in fc.iterfiles()]
    basename_list.sort()
    expect = ["root_file.txt", "sub_file.txt"]
    expect.sort()
    assert basename_list == expect
示例#4
0
def backup_dir(filename, root_dir, ignore=None, ignore_ext=None, ignore_pattern=None):
    """The backup utility method. Basically it just add files that need to be
    backupped to zip archives.

    :param filename: the output file name, DO NOT NEED FILE EXTENSION.
    :param root_dir: the directory you want to backup.
    :param ignore: file or directory defined in this list will be ignored.
    :param ignore_ext: file with extensions defined in this list will be ignored.
    :param ignore_pattern: any file or directory that contains this pattern
      will be ignored.
    """
    tab = "    "
    # Step 1, calculate files to backup
    root_dir = os.path.abspath(root_dir)
    print("Perform backup '%s'..." % root_dir)
    print(tab + "1. Calculate files...")

    total_size_in_bytes = 0

    init_mode = WinFile.init_mode
    WinFile.use_regular_init()
    fc = FileCollection.from_path_except(
        root_dir, ignore, ignore_ext, ignore_pattern)
    WinFile.set_initialize_mode(complexity=init_mode)

    for winfile in fc.iterfiles():
        total_size_in_bytes += winfile.size_on_disk

    # Step 2, write files to zip archive
    print(tab * 2 + "Done, got %s files, total size is %s." % (
        len(fc), repr_data_size(total_size_in_bytes)))
    print(tab + "2. Backup files...")

    basename = "%s %s.zip" % (
        filename, datetime.now().strftime("%Y-%m-%d %Hh-%Mm-%Ss"))

    print(tab * 2 + "Write to '%s'..." % basename)
    current_dir = os.getcwd()
    with ZipFile(basename, "w") as f:
        os.chdir(root_dir)
        for winfile in fc.iterfiles():
            relpath = os.path.relpath(winfile.abspath, root_dir)
            f.write(relpath)
    os.chdir(current_dir)

    print(tab + "Complete!")
示例#5
0
    def run(self):
        """Run analysis.

        The basic idea is to recursively find all script files in specific 
        programming language, and analyze each file then sum it up.
        """
        n_target_file, n_other_file = 0, 0

        code, comment, docstr, purecode = 0, 0, 0, 0

        fc = FileCollection.from_path_except(self.workspace, self.ignore)

        fc_yes, fc_no = fc.select(self.filter, keepboth=True)
        n_other_file += len(fc_no)

        for abspath in fc_yes:
            try:
                with open(abspath, "rb") as f:
                    code_text = f.read().decode("utf-8")
                    code_, comment_, docstr_, purecode_ = self.analyzer(
                        code_text)
                    code += code_
                    comment += comment_
                    docstr += docstr_
                    purecode += purecode_
                    n_target_file += 1
            except Exception as e:
                n_other_file += 1

        lines = list()
        lines.append("Code statistic result for '%s'" % self.workspace)
        lines.append("  %r %r files, %r other files." %
                     (n_target_file, self.language, n_other_file))
        lines.append("    code line: %s" % code)
        lines.append("    comment line: %s" % comment)
        lines.append("    docstr line: %s" % docstr)
        lines.append("    purecode line: %s" % purecode)
        message = "\n".join(lines)

        print(message)
        return message