Created
January 26, 2016 11:18
-
-
Save mexisme/ea4e639d1b0f2fb50274 to your computer and use it in GitHub Desktop.
Walk directory, print symlinks and non-symlinks
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python2 | |
# Given a list of paths, walk the directory hierarchy of each path, taking | |
# the files within each and return a list of those items that are symlinks, | |
# and those that aren't | |
# | |
# Example: | |
# % ./walk-dir.py . | |
# Symlinks: | |
# './test.test' | |
# Non-symlinks: | |
# './Test/tes' | |
# './walk-dir.py' | |
# './walk-dir.py~' | |
import sys, os, stat | |
class SeparatedPath: | |
def __init__(self, separator=", ", header=""): | |
self.separator = separator | |
self.header = header | |
def string(self, paths): | |
repr_paths = sorted(map(repr, paths)) | |
return "%s%s" % (self.header, self.separator.join(repr_paths)) | |
########## | |
def find_files_modes(paths): | |
full_files = [os.path.join(base_dir, current_file) | |
for path in paths | |
for base_dir, dirs, files in os.walk(path) | |
for current_file in files] | |
return [(current_path, os.lstat(current_path).st_mode) | |
for current_path in full_files] | |
def find_symlinks(files_modes): | |
return [path | |
for path, mode in files_modes | |
if stat.S_ISLNK(mode)] | |
def separated_paths(separator, paths): | |
return separator.join(sorted(map(repr, paths))) | |
########## | |
# When running tests: | |
if __name__ == "__main__": | |
sep_path = SeparatedPath("\n ", " ") | |
files_modes = find_files_modes(sys.argv) | |
paths = [path | |
for path, mode in files_modes] | |
symlinks = find_symlinks(files_modes) | |
non_symlinks = list(set(paths) - set(symlinks)) | |
print "Symlinks:\n", sep_path.string(symlinks) | |
print "Non-symlinks:\n", sep_path.string(non_symlinks) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment