def test_sub():
    """测试两个集合相减是否正常工作。
    """
    fc1 = FileCollection.from_path(dir_path)
    fc2 = FileCollection.from_path(dir_path)
    fc = fc1 - fc2
    assert fc.howmany == 0
Exemple #2
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
Exemple #3
0
def test_sub():
    """测试两个集合相减是否正常工作。
    """
    fc1 = FileCollection.from_path(dir_path)
    fc2 = FileCollection.from_path(dir_path)
    fc = fc1 - fc2
    assert fc.howmany == 0
def test_add_and_remove():
    """测试添加WinFile和删除WinFile的方法是否正常工作。
    """
    fc = FileCollection()
    fc.add(__file__)
    assert fc.howmany == 1
    fc.remove(__file__)
    assert fc.howmany == 0
Exemple #5
0
def test_from_path_by_size():
    """测试from_from_path_by_size方法是否能正常工作。
    """
    fc = FileCollection.from_path_by_size(dir_path, min_size=1024)
    basename_list = [winfile.basename for winfile in fc.iterfiles()]
    basename_list.sort()
    expect = ["root_image.jpg", "sub_image.jpg"]
    expect.sort()
    assert basename_list == expect

    fc = FileCollection.from_path_by_size(dir_path, max_size=1024)
    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
Exemple #6
0
def test_from_path_by_ext():
    """测试from_path_by_ext方法是否能正常工作。
    """
    fc = FileCollection.from_path_by_ext(dir_path, ext=".jpg")
    basename_list = [winfile.basename for winfile in fc.iterfiles()]
    basename_list.sort()
    expect = ["root_image.jpg", "sub_image.jpg"]
    expect.sort()
    assert basename_list == expect

    fc = FileCollection.from_path_by_ext(dir_path, ext=[".txt"])
    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
def test_from_path_by_md5():
    WinFile.set_initialize_mode(complexity=3)
    winfile = WinFile(__file__)
    WinFile.set_initialize_mode(complexity=2)

    res = FileCollection.from_path_by_md5(os.getcwd(), winfile.md5)
    assert res[0].basename == "test_files.py"
def test_from_path_by_ext():
    """测试from_path_by_ext方法是否能正常工作。
    """
    fc = FileCollection.from_path_by_ext(dir_path, ext=".jpg")
    basename_list = [winfile.basename for winfile in fc.iterfiles()]
    basename_list.sort()
    expect = ["root_image.jpg", "sub_image.jpg"]
    expect.sort()
    assert basename_list == expect

    fc = FileCollection.from_path_by_ext(dir_path, ext=[".txt"])
    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
def test_from_path_by_size():
    """测试from_from_path_by_size方法是否能正常工作。
    """
    fc = FileCollection.from_path_by_size(dir_path, min_size=1024)
    basename_list = [winfile.basename for winfile in fc.iterfiles()]
    basename_list.sort()
    expect = ["root_image.jpg", "sub_image.jpg"]
    expect.sort()
    assert basename_list == expect

    fc = FileCollection.from_path_by_size(dir_path, max_size=1024)
    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
Exemple #10
0
def test_from_path_by_md5():
    WinFile.set_initialize_mode(complexity=3)
    winfile = WinFile(__file__)
    WinFile.set_initialize_mode(complexity=2)

    res = FileCollection.from_path_by_md5(os.getcwd(), winfile.md5)
    assert res[0].basename == "test_files.py"
Exemple #11
0
def test_yield_file():
    print("{:=^100}".format("yield_all_file_path"))
    for abspath in FileCollection.yield_all_file_path(dir_path):
        print(abspath)

    print("{:=^100}".format("yield_all_winfile"))
    for winfile in FileCollection.yield_all_winfile(dir_path):
        print(repr(winfile))

    print("{:=^100}".format("yield_all_top_file_path"))
    for abspath in FileCollection.yield_all_top_file_path(dir_path):
        print(abspath)

    print("{:=^100}".format("yield_all_top_winfile"))
    for winfile in FileCollection.yield_all_top_winfile(dir_path):
        print(repr(winfile))
def test_yield_file():
    print("{:=^100}".format("yield_all_file_path"))
    for abspath in FileCollection.yield_all_file_path(dir_path):
        print(abspath)

    print("{:=^100}".format("yield_all_winfile"))
    for winfile in FileCollection.yield_all_winfile(dir_path):
        print(repr(winfile))

    print("{:=^100}".format("yield_all_top_file_path"))
    for abspath in FileCollection.yield_all_top_file_path(dir_path):
        print(abspath)

    print("{:=^100}".format("yield_all_top_winfile"))
    for winfile in FileCollection.yield_all_top_winfile(dir_path):
        print(repr(winfile))
def test_from_path():
    fc = FileCollection.from_path(dir_path)
    basename_list = [winfile.basename for winfile in fc.iterfiles()]
    basename_list.sort()
    expect = ["root_file.txt", "root_image.jpg",
              "sub_file.txt", "sub_image.jpg"]
    expect.sort()
    assert basename_list == expect
def test_from_path_by_pattern():
    """测试from_path_by_pattern方法是否能正常工作。
    """
    fc = FileCollection.from_path_by_pattern(dir_path, pattern=["sub"])
    basename_list = [winfile.basename for winfile in fc.iterfiles()]
    basename_list.sort()
    expect = ["sub_file.txt", "sub_image.jpg"]
    expect.sort()
    assert basename_list == expect
Exemple #15
0
def test_from_path():
    fc = FileCollection.from_path(dir_path)
    basename_list = [winfile.basename for winfile in fc.iterfiles()]
    basename_list.sort()
    expect = [
        "root_file.txt", "root_image.jpg", "sub_file.txt", "sub_image.jpg"
    ]
    expect.sort()
    assert basename_list == expect
Exemple #16
0
def test_from_path_by_pattern():
    """测试from_path_by_pattern方法是否能正常工作。
    """
    fc = FileCollection.from_path_by_pattern(dir_path, pattern=["sub"])
    basename_list = [winfile.basename for winfile in fc.iterfiles()]
    basename_list.sort()
    expect = ["sub_file.txt", "sub_image.jpg"]
    expect.sort()
    assert basename_list == expect
def test_sort():
    """测试排序功能是否正常工作。
    """
    fc = FileCollection.from_path(dir_path)
    fc.sort_by_abspath()
    fc.sort_by_dirname()
    fc.sort_by_fname()
    fc.sort_by_ext()
    fc.sort_by_atime()
    fc.sort_by_ctime()
    fc.sort_by_mtime()
    fc.sort_by_size()
Exemple #18
0
def test_sort():
    """测试排序功能是否正常工作。
    """
    fc = FileCollection.from_path(dir_path)
    fc.sort_by_abspath()
    fc.sort_by_dirname()
    fc.sort_by_fname()
    fc.sort_by_ext()
    fc.sort_by_atime()
    fc.sort_by_ctime()
    fc.sort_by_mtime()
    fc.sort_by_size()
Exemple #19
0
def test_add_and_remove():
    """测试添加WinFile和删除WinFile的方法是否正常工作。
    """
    fc = FileCollection()
    fc.add(__file__)
    assert fc.howmany == 1
    fc.remove(__file__)
    assert fc.howmany == 0
Exemple #20
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!")
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
Exemple #22
0
def test_add():
    """测试两个集合相加是否正常工作。
    """
    fc1 = FileCollection.from_path(dir_path)
    fc2 = FileCollection.from_path(dir_path)
    fc3 = FileCollection()
    fc3.add(__file__)

    fc = fc1 + fc2 + fc3
    assert fc.howmany == 5

    fc = FileCollection.sum([fc1, fc2, fc3])
    assert fc.howmany == 5
Exemple #23
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!")
def test_add():
    """测试两个集合相加是否正常工作。
    """
    fc1 = FileCollection.from_path(dir_path)
    fc2 = FileCollection.from_path(dir_path)
    fc3 = FileCollection()
    fc3.add(__file__)

    fc = fc1 + fc2 + fc3
    assert fc.howmany == 5

    fc = FileCollection.sum([fc1, fc2, fc3])
    assert fc.howmany == 5
def test_from_path_by_criterion():
    def image_filter(winfile):
        if winfile.ext in [".jpg", ".png"]:
            return True
        else:
            return False

    fc_yes, fc_no = FileCollection.from_path_by_criterion(
        dir_path, image_filter, keepboth=True)

    basename_list = [winfile.basename for winfile in fc_yes.iterfiles()]
    basename_list.sort()
    expect_yes = ["root_image.jpg", "sub_image.jpg"]
    expect_yes.sort()
    assert basename_list == expect_yes

    basename_list = [winfile.basename for winfile in fc_no.iterfiles()]
    basename_list.sort()
    expect_no = ["root_file.txt", "sub_file.txt"]
    expect_no.sort()
    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
Exemple #27
0
def test_from_path_by_criterion():
    def image_filter(winfile):
        if winfile.ext in [".jpg", ".png"]:
            return True
        else:
            return False

    fc_yes, fc_no = FileCollection.from_path_by_criterion(dir_path,
                                                          image_filter,
                                                          keepboth=True)

    basename_list = [winfile.basename for winfile in fc_yes.iterfiles()]
    basename_list.sort()
    expect_yes = ["root_image.jpg", "sub_image.jpg"]
    expect_yes.sort()
    assert basename_list == expect_yes

    basename_list = [winfile.basename for winfile in fc_no.iterfiles()]
    basename_list.sort()
    expect_no = ["root_file.txt", "sub_file.txt"]
    expect_no.sort()
def test_show_patterned_file():
    FileCollection.show_patterned_file(dir_path, ["image", ])
Exemple #29
0
def test_show_patterned_file():
    FileCollection.show_patterned_file(dir_path, [
        "image",
    ])
Exemple #30
0
def test_show_big_file():
    FileCollection.show_big_file(dir_path, 1000)
Exemple #31
0
def test_create_fake_mirror():
    src = dir_path
    dst = dir_path.replace("testdir", "testdir_mirror")
    FileCollection.create_fake_mirror(src, dst)
def test_create_fake_mirror():
    src = dir_path
    dst = dir_path.replace("testdir", "testdir_mirror")
    FileCollection.create_fake_mirror(src, dst)
def test_show_big_file():
    FileCollection.show_big_file(dir_path, 1000)