Prettier on autolink

main
Buddy Sandidge 4 years ago
parent 2d436b859f
commit 40cc8216a4

@ -8,12 +8,15 @@ SCRIPT_NAME = path.basename(SCRIPT_ABS_PATH)
DOT_FILES_DIR = path.dirname(SCRIPT_ABS_PATH) DOT_FILES_DIR = path.dirname(SCRIPT_ABS_PATH)
HOME_DIR = path.expanduser('~') HOME_DIR = path.expanduser('~')
class ExistingFileError(OSError): class ExistingFileError(OSError):
pass pass
class ExistingSymlinkError(OSError): class ExistingSymlinkError(OSError):
pass pass
def main(*, dot_files=DOT_FILES_DIR, home_dir=HOME_DIR): def main(*, dot_files=DOT_FILES_DIR, home_dir=HOME_DIR):
for directory in walk_directories(dot_files): for directory in walk_directories(dot_files):
hidden_dir = path.join(home_dir, '.' + directory) hidden_dir = path.join(home_dir, '.' + directory)
@ -40,6 +43,7 @@ def walk_directories(directory):
yield rel_dir yield rel_dir
visited.add(rel_dir) visited.add(rel_dir)
def walk_files(directory): def walk_files(directory):
visited = set() visited = set()
for root, _, files in os.walk(directory): for root, _, files in os.walk(directory):
@ -52,19 +56,25 @@ def walk_files(directory):
yield relative_file yield relative_file
visited.add(relative_file) visited.add(relative_file)
def make_directory(directory): def make_directory(directory):
if path.isdir(directory): if path.isdir(directory):
return return
if path.exists(directory): if path.exists(directory):
raise ExistingFileError('expected no file or directory: {0}'.format(directory)) raise (
ExistingFileError('expected no file or directory: {0}'.format(directory))
)
os.mkdir(directory, mode=0o755) os.mkdir(directory, mode=0o755)
def link_to(src, dest): def link_to(src, dest):
if path.islink(dest): if path.islink(dest):
if path.realpath(src) == path.realpath(dest): if path.realpath(src) == path.realpath(dest):
return return
if path.exists(dest): if path.exists(dest):
raise ExistingSymlinkError('found unexpected symlink: {0}'.format(dest)) raise (
ExistingSymlinkError('found unexpected symlink: {0}'.format(dest))
)
# we have a broken symlink, remove it # we have a broken symlink, remove it
os.remove(dest) os.remove(dest)
if path.isfile(dest): if path.isfile(dest):
@ -73,6 +83,7 @@ def link_to(src, dest):
dir_fd = os.open(path.dirname(dest), os.O_RDONLY) dir_fd = os.open(path.dirname(dest), os.O_RDONLY)
os.symlink(src_rel, dest, dir_fd=dir_fd) os.symlink(src_rel, dest, dir_fd=dir_fd)
def path_contains(directory, *strings): def path_contains(directory, *strings):
directories = directory.split(os.sep) directories = directory.split(os.sep)
for s in strings: for s in strings:
@ -80,5 +91,6 @@ def path_contains(directory, *strings):
return True return True
return False return False
if __name__ == '__main__': if __name__ == '__main__':
main() main()

Loading…
Cancel
Save