dotfile-manager python3

This commit is contained in:
Meutel 2019-06-04 23:26:04 +02:00
parent c92df33bb0
commit 79e893e7a4

View File

@ -1,4 +1,4 @@
#!/usr/bin/env python2 #!/usr/bin/env python3
"""dotfilemanager.py - a dotfiles manager script. See --help for usage """dotfilemanager.py - a dotfiles manager script. See --help for usage
and command-line arguments. and command-line arguments.
@ -31,9 +31,9 @@ def tidy(d,report=False):
if not os.path.exists(target_path): if not os.path.exists(target_path):
# This is a broken symlink. # This is a broken symlink.
if report: if report:
print 'tidy would delete broken symlink: %s->%s' % (path,target_path) print('tidy would delete broken symlink: %s->%s' % (path,target_path))
else: else:
print 'Deleting broken symlink: %s->%s' % (path,target_path) print('Deleting broken symlink: %s->%s' % (path,target_path))
os.remove(path) os.remove(path)
def get_target_paths(to_dir,report=False): def get_target_paths(to_dir,report=False):
@ -49,15 +49,15 @@ def get_target_paths(to_dir,report=False):
path = os.path.join(to_dir,filename) path = os.path.join(to_dir,filename)
if filename.endswith('~'): if filename.endswith('~'):
if report: if report:
print 'Skipping %s' % filename print('Skipping %s' % filename)
continue continue
elif (not os.path.isfile(path)) and (not os.path.isdir(path)): elif (not os.path.isfile(path)) and (not os.path.isdir(path)):
if report: if report:
print 'Skipping %s (not a file or directory)' % filename print('Skipping %s (not a file or directory)' % filename)
continue continue
elif filename.startswith('.'): elif filename.startswith('.'):
if report: if report:
print 'Skipping %s (filename has a leading dot)' % filename print('Skipping %s (filename has a leading dot)' % filename)
continue continue
else: else:
if HOSTNAME_SEPARATOR in filename: if HOSTNAME_SEPARATOR in filename:
@ -70,14 +70,14 @@ def get_target_paths(to_dir,report=False):
paths.append(path) paths.append(path)
else: else:
if report: if report:
print 'Skipping %s (different hostname)' % filename print('Skipping %s (different hostname)' % filename)
continue continue
else: else:
# This appears to be a filename without a trailing # This appears to be a filename without a trailing
# hostname. # hostname.
if filename + HOSTNAME_SEPARATOR + HOSTNAME in filenames: if filename + HOSTNAME_SEPARATOR + HOSTNAME in filenames:
if report: if report:
print 'Skipping %s (there is a host-specific version of this file for this host)' % filename print('Skipping %s (there is a host-specific version of this file for this host)' % filename)
continue continue
else: else:
paths.append(path) paths.append(path)
@ -121,7 +121,7 @@ def link(from_dir,to_dir,report=False):
symlinks[from_path] = to_path symlinks[from_path] = to_path
# Attempt to create the symlinks that don't already exist. # Attempt to create the symlinks that don't already exist.
for from_path,to_path in symlinks.items(): for from_path,to_path in list(symlinks.items()):
# Check that nothing already exists at from_path. # Check that nothing already exists at from_path.
if os.path.islink(from_path): if os.path.islink(from_path):
# A link already exists. # A link already exists.
@ -133,19 +133,19 @@ def link(from_dir,to_dir,report=False):
continue continue
else: else:
# It's a link to somewhere else. # It's a link to somewhere else.
print from_path+" => is already symlinked to "+existing_to_path print(from_path+" => is already symlinked to "+existing_to_path)
elif os.path.isfile(from_path): elif os.path.isfile(from_path):
print "There's a file in the way at "+from_path print("There's a file in the way at "+from_path)
elif os.path.isdir(from_path): elif os.path.isdir(from_path):
print "There's a directory in the way at "+from_path print("There's a directory in the way at "+from_path)
elif os.path.ismount(from_path): elif os.path.ismount(from_path):
print "There's a mount point in the way at "+from_path print("There's a mount point in the way at "+from_path)
else: else:
# The path is clear, make the symlink. # The path is clear, make the symlink.
if report: if report:
print 'link would make symlink: %s->%s' % (from_path,to_path) print('link would make symlink: %s->%s' % (from_path,to_path))
else: else:
print 'Making symlink %s->%s' % (from_path,to_path) print('Making symlink %s->%s' % (from_path,to_path))
os.symlink(to_path,from_path) os.symlink(to_path,from_path)
def usage(): def usage():
@ -165,7 +165,7 @@ if __name__ == "__main__":
try: try:
ACTION = sys.argv[1] ACTION = sys.argv[1]
except IndexError: except IndexError:
print usage() print(usage())
sys.exit(2) sys.exit(2)
try: try:
@ -175,8 +175,8 @@ if __name__ == "__main__":
FROM_DIR = os.path.abspath(os.path.expanduser(FROM_DIR)) FROM_DIR = os.path.abspath(os.path.expanduser(FROM_DIR))
if not os.path.isdir(FROM_DIR): if not os.path.isdir(FROM_DIR):
print "FROM_DIR %s is not a directory!" % FROM_DIR print("FROM_DIR %s is not a directory!" % FROM_DIR)
print usage() print(usage())
sys.exit(2) sys.exit(2)
if ACTION == 'tidy': if ACTION == 'tidy':
@ -190,8 +190,8 @@ if __name__ == "__main__":
TO_DIR = os.path.abspath(os.path.expanduser(TO_DIR)) TO_DIR = os.path.abspath(os.path.expanduser(TO_DIR))
if not os.path.isdir(TO_DIR): if not os.path.isdir(TO_DIR):
print "TO_DIR %s is not a directory!" % TO_DIR print("TO_DIR %s is not a directory!" % TO_DIR)
print usage() print(usage())
sys.exit(2) sys.exit(2)
if ACTION == 'link': if ACTION == 'link':
@ -200,5 +200,5 @@ if __name__ == "__main__":
link(FROM_DIR,TO_DIR,report=True) link(FROM_DIR,TO_DIR,report=True)
tidy(FROM_DIR,report=True) tidy(FROM_DIR,report=True)
else: else:
print usage() print(usage())
sys.exit(2) sys.exit(2)