diff --git a/install.py b/install.py index c4e7f57..50855ae 100755 --- a/install.py +++ b/install.py @@ -29,9 +29,12 @@ def mkdir(path, dryrun=False): def parse_arguments(): - default_destdir = os.path.join(os.path.expanduser("~"), '.autojump') - default_prefix = '' - default_zshshare = 'functions' + default_user_destdir = os.path.join(os.path.expanduser("~"), '.autojump') + default_user_prefix = '' + default_user_zshshare = 'functions' + default_system_destdir = '/' + default_system_prefix = '/usr/local' + default_system_zshshare = '/usr/share/zsh/site-functions' parser = ArgumentParser( description='Installs autojump globally for root users, otherwise \ @@ -40,13 +43,13 @@ def parse_arguments(): '-n', '--dryrun', action="store_true", default=False, help='simulate installation') parser.add_argument( - '-d', '--destdir', metavar='DIR', default=default_destdir, + '-d', '--destdir', metavar='DIR', default=default_user_destdir, help='set destination to DIR') parser.add_argument( - '-p', '--prefix', metavar='DIR', default=default_prefix, + '-p', '--prefix', metavar='DIR', default=default_user_prefix, help='set prefix to DIR') parser.add_argument( - '-z', '--zshshare', metavar='DIR', default=default_zshshare, + '-z', '--zshshare', metavar='DIR', default=default_user_zshshare, help='set zsh share destination to DIR') parser.add_argument( '-s', '--system', action="store_true", default=False, @@ -64,14 +67,15 @@ def parse_arguments(): if args.system: if os.geteuid() != 0: - print("Please rerun as root for system-wide installation.", file=sys.stderr) + print("Please rerun as root for system-wide installation.", + file=sys.stderr) sys.exit(1) - if args.destdir == default_destdir: - args.destdir = '/' - if args.prefix == default_prefix: - args.prefix = '/usr/local' - if args.zshshare == default_zshshare: - args.zshshare = '/usr/share/zsh/site-functions' + if args.destdir == default_user_destdir: + args.destdir = default_system_destdir + if args.prefix == default_user_prefix: + args.prefix = default_system_prefix + if args.zshshare == default_user_zshshare: + args.zshshare = default_system_zshshare return args @@ -103,8 +107,8 @@ def main(args): mkdir(zshshare_dir, args.dryrun) cp('./bin/autojump', bin_dir, args.dryrun) - cp('./bin/data.py', bin_dir, args.dryrun) - cp('./bin/utils.py', bin_dir, args.dryrun) + cp('./bin/autojump_data.py', bin_dir, args.dryrun) + cp('./bin/autojump_utils.py', bin_dir, args.dryrun) cp('./bin/autojump.sh', etc_dir, args.dryrun) cp('./bin/autojump.bash', etc_dir, args.dryrun) cp('./bin/autojump.fish', etc_dir, args.dryrun) diff --git a/uninstall.py b/uninstall.py index f1d695e..91bef49 100755 --- a/uninstall.py +++ b/uninstall.py @@ -5,30 +5,59 @@ from __future__ import print_function from argparse import ArgumentParser import os +import platform import shutil import sys def parse_arguments(): parser = ArgumentParser( - description='Installs autojump globally for root users, otherwise \ - installs in current user\'s home directory.') + description='Uninstalls autojump.') parser.add_argument( '-n', '--dryrun', action="store_true", default=False, help='simulate installation') + parser.add_argument( + '-u', '--userdata', action="store_true", default=False, + help='`delete user data') parser.add_argument( '-d', '--destdir', metavar='DIR', help='set destination to DIR') parser.add_argument( - '-p', '--prefix', metavar='DIR', + '-p', '--prefix', metavar='DIR', default='', help='set prefix to DIR') parser.add_argument( - '-z', '--zshshare', metavar='DIR', + '-z', '--zshshare', metavar='DIR', default='functions', help='set zsh share destination to DIR') return parser.parse_args() +def remove_custom_installation(args, dryrun=False): + if not args.destdir: + return + + bin_dir = os.path.join(args.destdir, args.prefix, 'bin') + etc_dir = os.path.join(args.destdir, 'etc/profile.d') + doc_dir = os.path.join(args.destdir, args.prefix, 'share/man/man1') + icon_dir = os.path.join(args.destdir, args.prefix, 'share/autojump') + zshshare_dir = os.path.join(args.destdir, args.zshshare) + + if not os.path.exists(icon_dir): + return + + print("\nFound custom installation...") + rm(os.path.join(bin_dir, 'autojump'), dryrun) + rm(os.path.join(bin_dir, 'autojump_data.py'), dryrun) + rm(os.path.join(bin_dir, 'autojump_utils.py'), dryrun) + rm(os.path.join(etc_dir, 'autojump.sh'), dryrun) + rm(os.path.join(etc_dir, 'autojump.bash'), dryrun) + rm(os.path.join(etc_dir, 'autojump.fish'), dryrun) + rm(os.path.join(etc_dir, 'autojump.zsh'), dryrun) + rm(os.path.join(zshshare_dir, '_j'), dryrun) + rmdir(icon_dir, dryrun) + rm(os.path.join(doc_dir, 'autojump.1'), dryrun) + + def remove_system_installation(dryrun=False): default_destdir = '/' default_prefix = '/usr/local' @@ -40,14 +69,19 @@ def remove_system_installation(dryrun=False): icon_dir = os.path.join(default_destdir, default_prefix, 'share/autojump') zshshare_dir = os.path.join(default_destdir, default_zshshare) - if os.path.exists(os.path.join(bin_dir, 'autojump')): - print("Found system installation.") - else: + if not os.path.exists(icon_dir): + return + + print("\nFound system installation...") + + if os.geteuid() != 0: + print("Please rerun as root for system-wide uninstall, skipping...", + file=sys.stderr) return rm(os.path.join(bin_dir, 'autojump'), dryrun) - rm(os.path.join(bin_dir, 'data.py'), dryrun) - rm(os.path.join(bin_dir, 'utils.py'), dryrun) + rm(os.path.join(bin_dir, 'autojump_data.py'), dryrun) + rm(os.path.join(bin_dir, 'autojump_utils.py'), dryrun) rm(os.path.join(etc_dir, 'autojump.sh'), dryrun) rm(os.path.join(etc_dir, 'autojump.bash'), dryrun) rm(os.path.join(etc_dir, 'autojump.fish'), dryrun) @@ -57,10 +91,30 @@ def remove_system_installation(dryrun=False): rm(os.path.join(doc_dir, 'autojump.1'), dryrun) +def remove_user_data(dryrun=False): + if platform.system() == 'Darwin': + data_home = os.path.join( + os.path.expanduser('~'), + 'Library', + 'autojump') + else: + data_home = os.getenv( + 'XDG_DATA_HOME', + os.path.join( + os.path.expanduser('~'), + '.local', + 'share', + 'autojump')) + + if os.path.exists(data_home): + print("\nFound user data...") + rmdir(data_home, dryrun) + + def remove_user_installation(dryrun=False): default_destdir = os.path.join(os.path.expanduser("~"), '.autojump') if os.path.exists(default_destdir): - print("Found user installation.") + print("\nFound user installation...") rmdir(default_destdir, dryrun) @@ -68,7 +122,7 @@ def rm(path, dryrun): if os.path.exists(path): print("deleting file:", path) if not dryrun: - shutil.rmtree(path) + os.remove(path) def rmdir(path, dryrun): @@ -86,6 +140,9 @@ def main(args): remove_user_installation(args.dryrun) remove_system_installation(args.dryrun) + remove_custom_installation(args, args.dryrun) + if args.userdata: + remove_user_data(args.dryrun) if __name__ == "__main__":