Example #1
0
 def test_not_a_directory(self):
     self.mkdirs('root')
     self.mkfile('root/f1')
     # does not exist
     with pytest.raises(ValueError):
         included_paths(self.path_to('wrong_root'))
     with pytest.raises(ValueError):
         included_paths(self.path_to('root/f1'))
Example #2
0
    def test_symlinked_file(self):
        self.mkdirs('root')
        self.mkfile('root/f1')
        self.mkfile('linked_file')
        self.symlink('linked_file', 'root/f2')

        filepaths = included_paths(self.path_to('root'), linked_files=True)
        assert filepaths == ['f1', 'f2']

        filepaths = included_paths(self.path_to('root'), linked_files=False)
        assert filepaths == ['f1']

        # default is 'linked_files': True
        filepaths = included_paths(self.path_to('root'), )
        assert filepaths == ['f1', 'f2']
Example #3
0
    def test_basic(self):
        self.mkdirs('root/d1/d11')
        self.mkdirs('root/d2')

        self.mkfile('root/f1')
        self.mkfile('root/d1/f1')
        self.mkfile('root/d1/d11/f1')
        self.mkfile('root/d2/f1')

        expected_filepaths = ['d1/d11/f1', 'd1/f1', 'd2/f1', 'f1']
        filepaths = included_paths(self.path_to('root'))
        assert filepaths == expected_filepaths

        # end with '/' or not should not matter
        filepaths = included_paths(self.path_to('root/'))
        assert filepaths == expected_filepaths
Example #4
0
    def test_ignore_hidden_files(self):
        self.mkdirs('root/d1')
        self.mkdirs('root/.d2')

        self.mkfile('root/f1')
        self.mkfile('root/.f2')
        self.mkfile('root/d1/f1')
        self.mkfile('root/d1/.f2')
        self.mkfile('root/.d2/f1')

        # no ignore
        filepaths = included_paths(self.path_to('root'))
        assert filepaths == ['.d2/f1', '.f2', 'd1/.f2', 'd1/f1', 'f1']

        # with ignore
        filepaths = included_paths(self.path_to('root'), match=['*', '!.*'])
        assert filepaths == ['.d2/f1', 'd1/f1', 'f1']
Example #5
0
    def test_symlinked_dir(self):
        self.mkdirs('root')
        self.mkfile('root/f1')
        self.mkdirs('linked_dir')
        self.mkfile('linked_dir/f1')
        self.mkfile('linked_dir/f2')
        self.symlink('linked_dir', 'root/d1')

        filepaths = included_paths(self.path_to('root'), linked_dirs=False)
        assert filepaths == ['f1']

        filepaths = included_paths(self.path_to('root'), linked_dirs=True)
        assert filepaths == ['d1/f1', 'd1/f2', 'f1']

        # default is 'linked_dirs': True
        filepaths = included_paths(self.path_to('root'))
        assert filepaths == ['d1/f1', 'd1/f2', 'f1']
Example #6
0
    def test_empty_dirs_include_vs_exclude(self):
        self.mkdirs('root/d1')
        self.mkdirs('root/d2')
        self.mkdirs('root/d3/d31')
        self.mkdirs('root/d4/d41')

        self.mkfile('root/d1/f')
        self.mkfile('root/d3/d31/f')

        filepaths = included_paths(self.path_to('root'), empty_dirs=False)
        assert filepaths == ['d1/f', 'd3/d31/f']

        # `include_empty=False` is default
        filepaths = included_paths(self.path_to('root'))
        assert filepaths == ['d1/f', 'd3/d31/f']

        filepaths = included_paths(self.path_to('root'), empty_dirs=True)
        assert filepaths == ['d1/f', 'd2/.', 'd3/d31/f', 'd4/d41/.']
Example #7
0
    def test_cyclic_link(self):
        self.mkdirs('root/d1')
        self.symlink('root', 'root/d1/link_back')
        with pytest.raises(SymlinkRecursionError) as exc_info:
            included_paths(self.path_to('root'), allow_cyclic_links=False)
        assert exc_info.value.real_path == os.path.realpath(
            self.path_to('root'))
        assert exc_info.value.first_path == self.path_to('root/')
        assert exc_info.value.second_path == self.path_to('root/d1/link_back')
        assert str(exc_info.value).startswith('Symlink recursion:')

        filepaths = included_paths(self.path_to('root'),
                                   allow_cyclic_links=True)
        assert filepaths == ['d1/link_back/.']

        # default is 'allow_cyclic_links': False
        with pytest.raises(SymlinkRecursionError):
            filepaths = included_paths(self.path_to('root'))
Example #8
0
    def test_empty_dir_inclusion_not_affected_by_match(self):
        self.mkdirs('root/d1')
        self.mkdirs('root/.d2')

        # NOTE that empty dirs are not excluded by match_patterns:

        filepaths = included_paths(self.path_to('root'),
                                   match=['*', '!.*'],
                                   empty_dirs=True)
        assert filepaths == ['.d2/.', 'd1/.']

        filepaths = included_paths(self.path_to('root'),
                                   match=['*', '!.*/'],
                                   empty_dirs=True)
        assert filepaths == ['.d2/.', 'd1/.']

        filepaths = included_paths(self.path_to('root'),
                                   match=['*', '!d1'],
                                   empty_dirs=True)
        assert filepaths == ['.d2/.', 'd1/.']
Example #9
0
    def test_exclude_hidden_dirs(self):
        self.mkdirs('root/d1')
        self.mkdirs('root/.d2')
        self.mkdirs('root/d1/.d1')

        self.mkfile('root/f1')
        self.mkfile('root/.f2')
        self.mkfile('root/d1/f1')
        self.mkfile('root/d1/.f2')
        self.mkfile('root/.d2/f1')

        # no ignore
        filepaths = included_paths(self.path_to('root'), empty_dirs=True)
        assert filepaths == [
            '.d2/f1', '.f2', 'd1/.d1/.', 'd1/.f2', 'd1/f1', 'f1'
        ]

        # with ignore
        filepaths = included_paths(self.path_to('root'), match=['*', '!.*/'])
        assert filepaths == ['.f2', 'd1/.f2', 'd1/f1', 'f1']
Example #10
0
    def test_empty_dirs_because_of_filter_include_vs_exclude(self):
        self.mkdirs('root/d1')
        self.mkdirs('root/d2')

        self.mkfile('root/d1/f')
        self.mkfile('root/d2/.f')

        filepaths = included_paths(self.path_to('root'),
                                   match=['*', '!.*'],
                                   empty_dirs=False)
        assert filepaths == ['d1/f']

        # `include_empty=False` is default
        filepaths = included_paths(
            self.path_to('root'),
            match=['*', '!.*'],
        )
        assert filepaths == ['d1/f']

        filepaths = included_paths(self.path_to('root'),
                                   match=['*', '!.*'],
                                   empty_dirs=True)
        assert filepaths == ['d1/f', 'd2/.']
Example #11
0
def main():
    try:
        kwargs = get_kwargs(sys.argv[1:])
        if kwargs.pop('list'):
            # kwargs below have no effect when listing
            for k in ['algorithm', 'chunk_size', 'jobs', 'entry_properties']:
                kwargs.pop(k)
            for leafpath in dirhash.included_paths(**kwargs):
                print(leafpath)
        else:
            print(dirhash.dirhash(**kwargs))
    except Exception as e:  # pragma: no cover (not picked up by coverage)
        sys.stderr.write('dirhash: {}\n'.format(e))
        sys.exit(1)
Example #12
0
    def test_exclude_extensions(self):
        self.mkdirs('root/d1')

        self.mkfile('root/f')
        self.mkfile('root/f.txt')
        self.mkfile('root/f.skip1')
        self.mkfile('root/fskip1')
        self.mkfile('root/f.skip2')
        self.mkfile('root/f.skip1.txt')
        self.mkfile('root/f.skip1.skip2')
        self.mkfile('root/f.skip1skip2')
        self.mkfile('root/d1/f.txt')
        self.mkfile('root/d1/f.skip1')

        filepaths = included_paths(self.path_to('root'),
                                   match=['*', '!*.skip1', '!*.skip2'])
        assert filepaths == [
            'd1/f.txt', 'f', 'f.skip1.txt', 'f.skip1skip2', 'f.txt', 'fskip1'
        ]