Skip to content

Commit 94a95ae

Browse files
committed
Replace allow_symlinks with follow_symlinks
* Test that resource_iter can follow symlinks Signed-off-by: Jono Yang <jyang@nexb.com>
1 parent 49ae11f commit 94a95ae

6 files changed

Lines changed: 40 additions & 27 deletions

File tree

src/commoncode/filetype.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -45,22 +45,22 @@ def is_link(location):
4545
return location and os.path.islink(location)
4646

4747

48-
def is_file(location, allow_symlinks=False):
48+
def is_file(location, follow_symlinks=False):
4949
"""
5050
Return True if `location` is a file.
5151
"""
5252
_is_file = location and os.path.isfile(location)
53-
if allow_symlinks:
53+
if follow_symlinks:
5454
return _is_file
5555
return _is_file and not is_link(location) and not is_broken_link(location)
5656

5757

58-
def is_dir(location, allow_symlinks=False):
58+
def is_dir(location, follow_symlinks=False):
5959
"""
6060
Return True if `location` is a directory.
6161
"""
6262
_is_dir = location and os.path.isdir(location) and not is_file(location)
63-
if allow_symlinks:
63+
if follow_symlinks:
6464
return _is_dir
6565
return _is_dir and not is_link(location) and not is_broken_link(location)
6666

src/commoncode/fileutils.py

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -365,18 +365,18 @@ def ignore_nothing(_):
365365
return False
366366

367367

368-
def walk(location, ignored=None, allow_symlinks=False):
368+
def walk(location, ignored=None, follow_symlinks=False):
369369
"""
370370
Walk location returning the same tuples as os.walk but with a different
371371
behavior:
372372
- always walk top-down, breadth-first.
373-
- always ignore and never follow symlinks (unless `allow_symlinks` is True),
373+
- always ignore and never follow symlinks (unless `follow_symlinks` is True),
374374
- always ignore special files (FIFOs, etc.)
375375
- optionally ignore files and directories by invoking the `ignored`
376376
callable on files and directories returning True if it should be ignored.
377377
- location is a directory or a file: for a file, the file is returned.
378378
379-
If `allow_symlinks` is True, then symlinks will not be ignored and be
379+
If `follow_symlinks` is True, then symlinks will not be ignored and be
380380
collected like regular files and directories
381381
"""
382382
if on_linux and py2:
@@ -390,17 +390,17 @@ def walk(location, ignored=None, allow_symlinks=False):
390390
logger_debug('walk: ignored:', location, is_ignored)
391391
return
392392

393-
if filetype.is_file(location, allow_symlinks=allow_symlinks) :
393+
if filetype.is_file(location, follow_symlinks=follow_symlinks) :
394394
yield parent_directory(location), [], [file_name(location)]
395395

396-
elif filetype.is_dir(location, allow_symlinks=allow_symlinks):
396+
elif filetype.is_dir(location, follow_symlinks=follow_symlinks):
397397
dirs = []
398398
files = []
399399
# TODO: consider using scandir
400400
for name in os.listdir(location):
401401
loc = os.path.join(location, name)
402402
if filetype.is_special(loc) or (ignored and ignored(loc)):
403-
if (allow_symlinks
403+
if (follow_symlinks
404404
and filetype.is_link(loc)
405405
and not filetype.is_broken_link(location)):
406406
pass
@@ -410,18 +410,18 @@ def walk(location, ignored=None, allow_symlinks=False):
410410
logger_debug('walk: ignored:', loc, ign)
411411
continue
412412
# special files and symlinks are always ignored
413-
if filetype.is_dir(loc, allow_symlinks=allow_symlinks):
413+
if filetype.is_dir(loc, follow_symlinks=follow_symlinks):
414414
dirs.append(name)
415-
elif filetype.is_file(loc, allow_symlinks=allow_symlinks):
415+
elif filetype.is_file(loc, follow_symlinks=follow_symlinks):
416416
files.append(name)
417417
yield location, dirs, files
418418

419419
for dr in dirs:
420-
for tripple in walk(os.path.join(location, dr), ignored, allow_symlinks=allow_symlinks):
420+
for tripple in walk(os.path.join(location, dr), ignored, follow_symlinks=follow_symlinks):
421421
yield tripple
422422

423423

424-
def resource_iter(location, ignored=ignore_nothing, with_dirs=True, allow_symlinks=False):
424+
def resource_iter(location, ignored=ignore_nothing, with_dirs=True, follow_symlinks=False):
425425
"""
426426
Return an iterable of paths at `location` recursively.
427427
@@ -432,7 +432,7 @@ def resource_iter(location, ignored=ignore_nothing, with_dirs=True, allow_symlin
432432
"""
433433
if on_linux and py2:
434434
location = fsencode(location)
435-
for top, dirs, files in walk(location, ignored, allow_symlinks=allow_symlinks):
435+
for top, dirs, files in walk(location, ignored, follow_symlinks=follow_symlinks):
436436
if with_dirs:
437437
for d in dirs:
438438
yield os.path.join(top, d)

tests/commoncode/data/symlink/walk/a

Whitespace-only changes.

tests/commoncode/data/symlink/walk/dir/b

Whitespace-only changes.

tests/commoncode/test_filetype.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -220,14 +220,14 @@ def test_is_file(self):
220220
temp_dir = fileutils.get_temp_dir()
221221
test_link = join(temp_dir, 'test-link')
222222
os.symlink(test_file, test_link)
223-
assert filetype.is_file(test_link, allow_symlinks=True)
224-
assert not filetype.is_file(test_link, allow_symlinks=False)
223+
assert filetype.is_file(test_link, follow_symlinks=True)
224+
assert not filetype.is_file(test_link, follow_symlinks=False)
225225

226226
@skipIf(on_windows, 'os.symlink does not work on Windows')
227227
def test_is_dir(self):
228228
test_dir = self.get_test_loc('symlink', copy=True)
229229
temp_dir = fileutils.get_temp_dir()
230230
test_link = join(temp_dir, 'test-dir-link')
231231
os.symlink(test_dir, test_link)
232-
assert filetype.is_dir(test_link, allow_symlinks=True)
233-
assert not filetype.is_dir(test_link, allow_symlinks=False)
232+
assert filetype.is_dir(test_link, follow_symlinks=True)
233+
assert not filetype.is_dir(test_link, follow_symlinks=False)

tests/commoncode/test_fileutils.py

Lines changed: 21 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -386,17 +386,17 @@ def test_os_walk_can_walk_non_utf8_path_from_unicode_path(self):
386386

387387
@skipIf(on_windows, 'os.symlink does not work on Windows')
388388
def test_walk_on_symlinks(self):
389-
test_dir = self.get_test_loc('symlink', copy=True)
389+
test_dir = self.get_test_loc('symlink/walk', copy=True)
390390
temp_dir = fileutils.get_temp_dir()
391391
test_link = join(temp_dir, 'test-dir-link')
392392
os.symlink(test_dir, test_link)
393-
results = list(fileutils.walk(test_link, allow_symlinks=True))
394-
assert len(results) == 1
395-
top, dirs, files = results[0]
396-
top = os.path.basename(top)
397-
assert 'test-dir-link' == top
398-
assert [] == dirs
399-
assert ['test'] == files
393+
results = list(fileutils.walk(test_link, follow_symlinks=True))
394+
results = [(os.path.basename(top), dirs, files) for top, dirs, files in results]
395+
expected = [
396+
('test-dir-link', ['dir'], ['a']),
397+
('dir', [], ['b'])
398+
]
399+
assert expected == results
400400

401401

402402
class TestFileUtilsIter(FileBasedTesting):
@@ -567,6 +567,19 @@ def test_resource_iter_can_walk_non_utf8_path_from_unicode_path(self):
567567
result = list(fileutils.resource_iter(test_dir, with_dirs=False))
568568
assert 18 == len(result)
569569

570+
def test_resource_iter_follow_symlinks(self):
571+
test_dir = self.get_test_loc('symlink/walk', copy=True)
572+
temp_dir = fileutils.get_temp_dir()
573+
test_link = join(temp_dir, 'test-dir-link')
574+
os.symlink(test_dir, test_link)
575+
result = [os.path.basename(f) for f in fileutils.resource_iter(test_dir, follow_symlinks=True)]
576+
expected = [
577+
'dir',
578+
'a',
579+
'b'
580+
]
581+
assert sorted(expected) == sorted(result)
582+
570583

571584
class TestBaseName(FileBasedTesting):
572585
test_data_dir = os.path.join(os.path.dirname(__file__), 'data')

0 commit comments

Comments
 (0)