def get_files(config): """ Walk the `source_dir` and return a Files collection. """ files = [] exclude = ['.*', '/templates'] for source_dir, dirnames, filenames in os.walk(config['source_dir'], followlinks=True): relative_dir = os.path.relpath(source_dir, config['source_dir']) for dirname in list(dirnames): path = os.path.normpath(os.path.join(relative_dir, dirname)) # Skip any excluded directories if _filter_paths(basename=dirname, path=path, is_dir=True, exclude=exclude): dirnames.remove(dirname) dirnames.sort() for filename in _sort_files(filenames): path = os.path.normpath(os.path.join(relative_dir, filename)) # Skip any excluded files if _filter_paths(basename=filename, path=path, is_dir=False, exclude=exclude): continue # Skip README.md is an index file also exists in dir if filename.lower() == 'readme.md' and 'index.md' in filenames: continue files.append( File(path, config['source_dir'], config['site_dir'], config['use_directory_urls'])) return Files(files)
def get_files(base_dir, config, site): files = [] exclude = ['.*', '/templates'] base = os.path.relpath(base_dir, config['docs_dir']) for source_dir, dirnames, filenames in os.walk(base_dir, followlinks=True): relative_dir = os.path.relpath(source_dir, config['docs_dir']) for dirname in list(dirnames): path = os.path.normpath(os.path.join(relative_dir, dirname)) # Skip any excluded directories if _filter_paths(basename=dirname, path=path, is_dir=True, exclude=exclude): dirnames.remove(dirname) dirnames.sort() for filename in _sort_files(filenames): path = os.path.normpath(os.path.join(relative_dir, filename)) # Skip any excluded files if _filter_paths(basename=filename, path=path, is_dir=False, exclude=exclude): continue f = File(path, config['docs_dir'], config['site_dir'], config['use_directory_urls']) f.dest_path = f.dest_path.replace(base + '/', '') f.abs_dest_path = os.path.normpath( os.path.join(config['site_dir'], f.dest_path)) f.url = f.url.replace(base + '/', '') if f.url == '': # Skip docs/index.md, use the one from root repo continue files.append(f) return (files, {file.src_path: file for file in files})
def test_filter_paths(self): # Root level file self.assertFalse(_filter_paths('foo.md', 'foo.md', False, ['bar.md'])) self.assertTrue(_filter_paths('foo.md', 'foo.md', False, ['foo.md'])) # Nested file self.assertFalse( _filter_paths('foo.md', 'baz/foo.md', False, ['bar.md'])) self.assertTrue( _filter_paths('foo.md', 'baz/foo.md', False, ['foo.md'])) # Wildcard self.assertFalse(_filter_paths('foo.md', 'foo.md', False, ['*.txt'])) self.assertTrue(_filter_paths('foo.md', 'foo.md', False, ['*.md'])) # Root level dir self.assertFalse(_filter_paths('bar', 'bar', True, ['/baz'])) self.assertFalse(_filter_paths('bar', 'bar', True, ['/baz/'])) self.assertTrue(_filter_paths('bar', 'bar', True, ['/bar'])) self.assertTrue(_filter_paths('bar', 'bar', True, ['/bar/'])) # Nested dir self.assertFalse(_filter_paths('bar', 'foo/bar', True, ['/bar'])) self.assertFalse(_filter_paths('bar', 'foo/bar', True, ['/bar/'])) self.assertTrue(_filter_paths('bar', 'foo/bar', True, ['bar/'])) # Files that look like dirs (no extension). Note that `is_dir` is `False`. self.assertFalse(_filter_paths('bar', 'bar', False, ['bar/'])) self.assertFalse(_filter_paths('bar', 'foo/bar', False, ['bar/']))
def test_filter_paths(self): # Root level file self.assertFalse(_filter_paths('foo.md', 'foo.md', False, ['bar.md'])) self.assertTrue(_filter_paths('foo.md', 'foo.md', False, ['foo.md'])) # Nested file self.assertFalse(_filter_paths('foo.md', 'baz/foo.md', False, ['bar.md'])) self.assertTrue(_filter_paths('foo.md', 'baz/foo.md', False, ['foo.md'])) # Wildcard self.assertFalse(_filter_paths('foo.md', 'foo.md', False, ['*.txt'])) self.assertTrue(_filter_paths('foo.md', 'foo.md', False, ['*.md'])) # Root level dir self.assertFalse(_filter_paths('bar', 'bar', True, ['/baz'])) self.assertFalse(_filter_paths('bar', 'bar', True, ['/baz/'])) self.assertTrue(_filter_paths('bar', 'bar', True, ['/bar'])) self.assertTrue(_filter_paths('bar', 'bar', True, ['/bar/'])) # Nested dir self.assertFalse(_filter_paths('bar', 'foo/bar', True, ['/bar'])) self.assertFalse(_filter_paths('bar', 'foo/bar', True, ['/bar/'])) self.assertTrue(_filter_paths('bar', 'foo/bar', True, ['bar/'])) # Files that look like dirs (no extension). Note that `is_dir` is `False`. self.assertFalse(_filter_paths('bar', 'bar', False, ['bar/'])) self.assertFalse(_filter_paths('bar', 'foo/bar', False, ['bar/']))