2014-01-02 21:56:43 +00:00
|
|
|
#!/usr/bin/env python
|
2013-12-30 20:49:34 +00:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
from __future__ import print_function
|
|
|
|
|
|
|
|
import os
|
2013-12-30 20:49:59 +00:00
|
|
|
import platform
|
2013-12-30 20:49:34 +00:00
|
|
|
import shutil
|
|
|
|
import sys
|
|
|
|
|
2014-01-12 19:34:28 +00:00
|
|
|
sys.path.append('bin')
|
2016-04-29 06:33:04 +00:00
|
|
|
from autojump_argparse import ArgumentParser # noqa
|
2014-01-07 15:27:05 +00:00
|
|
|
|
2013-12-30 20:49:34 +00:00
|
|
|
|
2013-12-30 21:38:19 +00:00
|
|
|
def is_empty_dir(path):
|
|
|
|
"""
|
|
|
|
Checks if any files are present within a directory and all sub-directories.
|
|
|
|
"""
|
|
|
|
for _, _, files in os.walk(path):
|
|
|
|
if files:
|
|
|
|
return False
|
|
|
|
return True
|
|
|
|
|
|
|
|
|
2013-12-30 20:49:34 +00:00
|
|
|
def parse_arguments():
|
2014-01-16 11:39:17 +00:00
|
|
|
default_clink_dir = os.path.join(os.getenv('LOCALAPPDATA', ''), 'clink')
|
|
|
|
|
2013-12-30 20:49:34 +00:00
|
|
|
parser = ArgumentParser(
|
2014-08-10 02:59:41 +00:00
|
|
|
description='Uninstalls autojump.')
|
2013-12-30 20:49:34 +00:00
|
|
|
parser.add_argument(
|
2016-04-29 06:33:04 +00:00
|
|
|
'-n', '--dryrun', action='store_true', default=False,
|
2014-08-10 02:59:41 +00:00
|
|
|
help='simulate installation')
|
2013-12-30 20:49:59 +00:00
|
|
|
parser.add_argument(
|
2016-04-29 06:33:04 +00:00
|
|
|
'-u', '--userdata', action='store_true', default=False,
|
2014-08-10 02:59:41 +00:00
|
|
|
help='delete user data')
|
2013-12-30 20:49:34 +00:00
|
|
|
parser.add_argument(
|
2014-08-10 02:59:41 +00:00
|
|
|
'-d', '--destdir', metavar='DIR',
|
|
|
|
help='custom destdir')
|
2013-12-30 20:49:34 +00:00
|
|
|
parser.add_argument(
|
2014-08-10 02:59:41 +00:00
|
|
|
'-p', '--prefix', metavar='DIR', default='',
|
|
|
|
help='custom prefix')
|
2013-12-30 20:49:34 +00:00
|
|
|
parser.add_argument(
|
2014-08-10 02:59:41 +00:00
|
|
|
'-z', '--zshshare', metavar='DIR', default='functions',
|
|
|
|
help='custom zshshare')
|
2014-01-16 11:39:17 +00:00
|
|
|
parser.add_argument(
|
2014-08-10 02:59:41 +00:00
|
|
|
'-c', '--clinkdir', metavar='DIR', default=default_clink_dir)
|
2013-12-30 20:49:34 +00:00
|
|
|
|
|
|
|
return parser.parse_args()
|
|
|
|
|
|
|
|
|
2013-12-30 20:49:59 +00:00
|
|
|
def remove_custom_installation(args, dryrun=False):
|
|
|
|
if not args.destdir:
|
|
|
|
return
|
|
|
|
|
|
|
|
bin_dir = os.path.join(args.destdir, args.prefix, 'bin')
|
2014-10-05 03:44:21 +00:00
|
|
|
doc_dir = os.path.join(args.destdir, args.prefix, 'share', 'man', 'man1')
|
|
|
|
etc_dir = os.path.join(args.destdir, 'etc', 'profile.d')
|
|
|
|
share_dir = os.path.join(args.destdir, args.prefix, 'share', 'autojump')
|
2013-12-30 20:49:59 +00:00
|
|
|
zshshare_dir = os.path.join(args.destdir, args.zshshare)
|
|
|
|
|
2014-10-05 03:44:21 +00:00
|
|
|
if not os.path.exists(share_dir):
|
2013-12-30 20:49:59 +00:00
|
|
|
return
|
|
|
|
|
2016-04-29 06:33:04 +00:00
|
|
|
print('\nFound custom installation...')
|
2013-12-30 20:49:59 +00:00
|
|
|
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)
|
2014-01-16 11:39:17 +00:00
|
|
|
rm(os.path.join(bin_dir, 'autojump_argparse.py'), dryrun)
|
|
|
|
if platform.system() == 'Windows':
|
|
|
|
if os.path.exists(args.clinkdir):
|
|
|
|
rm(os.path.join(args.clinkdir, 'autojump.lua'), dryrun)
|
|
|
|
rm(os.path.join(bin_dir, 'autojump.bat'), dryrun)
|
|
|
|
rm(os.path.join(bin_dir, 'j.bat'), dryrun)
|
|
|
|
rm(os.path.join(bin_dir, 'jc.bat'), dryrun)
|
|
|
|
rm(os.path.join(bin_dir, 'jco.bat'), dryrun)
|
|
|
|
rm(os.path.join(bin_dir, 'jo.bat'), dryrun)
|
|
|
|
else:
|
|
|
|
rm(os.path.join(etc_dir, 'autojump.sh'), dryrun)
|
2014-10-05 03:44:21 +00:00
|
|
|
rm(os.path.join(share_dir, 'autojump.bash'), dryrun)
|
|
|
|
rm(os.path.join(share_dir, 'autojump.fish'), dryrun)
|
|
|
|
rm(os.path.join(share_dir, 'autojump.tcsh'), dryrun)
|
|
|
|
rm(os.path.join(share_dir, 'autojump.zsh'), dryrun)
|
2014-01-16 11:39:17 +00:00
|
|
|
rm(os.path.join(zshshare_dir, '_j'), dryrun)
|
2014-10-05 03:44:21 +00:00
|
|
|
rmdir(share_dir, dryrun)
|
2013-12-30 20:49:59 +00:00
|
|
|
rm(os.path.join(doc_dir, 'autojump.1'), dryrun)
|
|
|
|
|
2013-12-30 21:38:19 +00:00
|
|
|
if is_empty_dir(args.destdir):
|
|
|
|
rmdir(args.destdir, dryrun)
|
|
|
|
|
2013-12-30 20:49:59 +00:00
|
|
|
|
2013-12-30 20:49:34 +00:00
|
|
|
def remove_system_installation(dryrun=False):
|
|
|
|
default_destdir = '/'
|
|
|
|
default_prefix = '/usr/local'
|
|
|
|
default_zshshare = '/usr/share/zsh/site-functions'
|
|
|
|
|
|
|
|
bin_dir = os.path.join(default_destdir, default_prefix, 'bin')
|
2016-04-29 06:33:04 +00:00
|
|
|
doc_dir = os.path.join(
|
|
|
|
default_destdir,
|
|
|
|
default_prefix,
|
|
|
|
'share',
|
|
|
|
'man',
|
|
|
|
'man1')
|
2014-10-05 03:44:21 +00:00
|
|
|
etc_dir = os.path.join(default_destdir, 'etc', 'profile.d')
|
2016-04-29 06:33:04 +00:00
|
|
|
share_dir = os.path.join(
|
|
|
|
default_destdir,
|
|
|
|
default_prefix,
|
|
|
|
'share',
|
|
|
|
'autojump')
|
2013-12-30 20:49:34 +00:00
|
|
|
zshshare_dir = os.path.join(default_destdir, default_zshshare)
|
|
|
|
|
2014-10-05 03:44:21 +00:00
|
|
|
if not os.path.exists(share_dir):
|
2013-12-30 20:49:59 +00:00
|
|
|
return
|
|
|
|
|
2016-04-29 06:33:04 +00:00
|
|
|
print('\nFound system installation...')
|
2013-12-30 20:49:59 +00:00
|
|
|
|
|
|
|
if os.geteuid() != 0:
|
2016-04-29 06:33:04 +00:00
|
|
|
print('Please rerun as root for system-wide uninstall, skipping...',
|
2013-12-30 20:49:59 +00:00
|
|
|
file=sys.stderr)
|
2013-12-30 20:49:34 +00:00
|
|
|
return
|
|
|
|
|
|
|
|
rm(os.path.join(bin_dir, 'autojump'), dryrun)
|
2013-12-30 20:49:59 +00:00
|
|
|
rm(os.path.join(bin_dir, 'autojump_data.py'), dryrun)
|
|
|
|
rm(os.path.join(bin_dir, 'autojump_utils.py'), dryrun)
|
2013-12-30 20:49:34 +00:00
|
|
|
rm(os.path.join(etc_dir, 'autojump.sh'), dryrun)
|
2014-10-05 03:44:21 +00:00
|
|
|
rm(os.path.join(share_dir, 'autojump.bash'), dryrun)
|
|
|
|
rm(os.path.join(share_dir, 'autojump.fish'), dryrun)
|
|
|
|
rm(os.path.join(share_dir, 'autojump.tcsh'), dryrun)
|
|
|
|
rm(os.path.join(share_dir, 'autojump.zsh'), dryrun)
|
2013-12-30 20:49:34 +00:00
|
|
|
rm(os.path.join(zshshare_dir, '_j'), dryrun)
|
2014-10-05 03:44:21 +00:00
|
|
|
rmdir(share_dir, dryrun)
|
2013-12-30 20:49:34 +00:00
|
|
|
rm(os.path.join(doc_dir, 'autojump.1'), dryrun)
|
|
|
|
|
|
|
|
|
2013-12-30 20:49:59 +00:00
|
|
|
def remove_user_data(dryrun=False):
|
|
|
|
if platform.system() == 'Darwin':
|
|
|
|
data_home = os.path.join(
|
2014-08-10 02:59:41 +00:00
|
|
|
os.path.expanduser('~'),
|
|
|
|
'Library',
|
|
|
|
'autojump')
|
2014-01-12 19:34:28 +00:00
|
|
|
elif platform.system() == 'Windows':
|
2014-01-16 11:39:17 +00:00
|
|
|
data_home = os.path.join(
|
2014-08-10 02:59:41 +00:00
|
|
|
os.getenv('APPDATA'),
|
|
|
|
'autojump')
|
2013-12-30 20:49:59 +00:00
|
|
|
else:
|
|
|
|
data_home = os.getenv(
|
2014-08-10 02:59:41 +00:00
|
|
|
'XDG_DATA_HOME',
|
|
|
|
os.path.join(
|
|
|
|
os.path.expanduser('~'),
|
|
|
|
'.local',
|
|
|
|
'share',
|
|
|
|
'autojump'))
|
2013-12-30 20:49:59 +00:00
|
|
|
|
|
|
|
if os.path.exists(data_home):
|
2016-04-29 06:33:04 +00:00
|
|
|
print('\nFound user data...')
|
2013-12-30 20:49:59 +00:00
|
|
|
rmdir(data_home, dryrun)
|
|
|
|
|
|
|
|
|
2013-12-30 20:49:34 +00:00
|
|
|
def remove_user_installation(dryrun=False):
|
2014-01-16 11:39:17 +00:00
|
|
|
if platform.system() == 'Windows':
|
2014-01-18 15:08:36 +00:00
|
|
|
default_destdir = os.path.join(os.getenv('LOCALAPPDATA', ''),
|
2014-08-10 02:59:41 +00:00
|
|
|
'autojump')
|
2014-01-16 11:39:17 +00:00
|
|
|
clink_dir = os.path.join(os.getenv('LOCALAPPDATA', ''), 'clink')
|
|
|
|
else:
|
2016-04-29 06:33:04 +00:00
|
|
|
default_destdir = os.path.join(os.path.expanduser('~'), '.autojump')
|
2014-01-18 15:08:36 +00:00
|
|
|
|
2013-12-30 20:49:34 +00:00
|
|
|
if os.path.exists(default_destdir):
|
2016-04-29 06:33:04 +00:00
|
|
|
print('\nFound user installation...')
|
2013-12-30 20:49:34 +00:00
|
|
|
rmdir(default_destdir, dryrun)
|
2014-01-16 11:39:17 +00:00
|
|
|
if platform.system() == 'Windows' and os.path.exists(clink_dir):
|
|
|
|
rm(os.path.join(clink_dir, 'autojump.lua'), dryrun)
|
2013-12-30 20:49:34 +00:00
|
|
|
|
|
|
|
|
|
|
|
def rm(path, dryrun):
|
|
|
|
if os.path.exists(path):
|
2016-04-29 06:33:04 +00:00
|
|
|
print('deleting file:', path)
|
2013-12-30 20:49:34 +00:00
|
|
|
if not dryrun:
|
2013-12-30 20:49:59 +00:00
|
|
|
os.remove(path)
|
2013-12-30 20:49:34 +00:00
|
|
|
|
|
|
|
|
|
|
|
def rmdir(path, dryrun):
|
|
|
|
if os.path.exists(path):
|
2016-04-29 06:33:04 +00:00
|
|
|
print('deleting directory:', path)
|
2013-12-30 20:49:34 +00:00
|
|
|
if not dryrun:
|
|
|
|
shutil.rmtree(path)
|
|
|
|
|
|
|
|
|
|
|
|
def main(args):
|
|
|
|
if args.dryrun:
|
2016-04-29 06:33:04 +00:00
|
|
|
print('Uninstalling autojump (DRYRUN)...')
|
2013-12-30 20:49:34 +00:00
|
|
|
else:
|
2016-04-29 06:33:04 +00:00
|
|
|
print('Uninstalling autojump...')
|
2013-12-30 20:49:34 +00:00
|
|
|
|
|
|
|
remove_user_installation(args.dryrun)
|
|
|
|
remove_system_installation(args.dryrun)
|
2013-12-30 20:49:59 +00:00
|
|
|
remove_custom_installation(args, args.dryrun)
|
|
|
|
if args.userdata:
|
|
|
|
remove_user_data(args.dryrun)
|
2013-12-30 20:49:34 +00:00
|
|
|
|
|
|
|
|
2016-04-29 06:33:04 +00:00
|
|
|
if __name__ == '__main__':
|
2013-12-30 20:49:34 +00:00
|
|
|
sys.exit(main(parse_arguments()))
|