1
0
mirror of https://github.com/wting/autojump synced 2024-10-27 20:34:07 +00:00
wting_autojump/install.py

217 lines
7.5 KiB
Python
Raw Normal View History

2014-01-02 21:56:43 +00:00
#!/usr/bin/env python
2013-12-30 20:05:24 +00:00
# -*- coding: utf-8 -*-
from __future__ import print_function
import os
import platform
2013-12-30 20:05:24 +00:00
import shutil
import sys
2014-01-11 13:09:19 +00:00
sys.path.append('bin')
2016-04-29 06:33:04 +00:00
from autojump_argparse import ArgumentParser # noqa
2013-12-30 20:05:24 +00:00
SUPPORTED_SHELLS = ('bash', 'zsh', 'fish', 'tcsh')
2013-12-30 20:05:24 +00:00
def cp(src, dest, dryrun=False):
2016-04-29 06:33:04 +00:00
print('copying file: %s -> %s' % (src, dest))
2013-12-30 20:05:24 +00:00
if not dryrun:
shutil.copy(src, dest)
def get_shell():
2013-12-31 00:01:04 +00:00
return os.path.basename(os.getenv('SHELL', ''))
2013-12-30 20:05:24 +00:00
def mkdir(path, dryrun=False):
2016-04-29 06:33:04 +00:00
print('creating directory:', path)
2013-12-30 20:05:24 +00:00
if not dryrun and not os.path.exists(path):
os.makedirs(path)
def modify_autojump_sh(etc_dir, share_dir, dryrun=False):
2013-12-30 21:37:18 +00:00
"""Append custom installation path to autojump.sh"""
2016-04-29 06:33:04 +00:00
custom_install = '\
2013-12-30 21:37:18 +00:00
\n# check custom install \
\nif [ -s %s/autojump.${shell} ]; then \
\n source %s/autojump.${shell} \
2016-04-29 06:33:04 +00:00
\nfi\n' % (share_dir, share_dir)
2013-12-30 21:37:18 +00:00
with open(os.path.join(etc_dir, 'autojump.sh'), 'a') as f:
f.write(custom_install)
def modify_autojump_lua(clink_dir, bin_dir, dryrun=False):
"""Prepend custom AUTOJUMP_BIN_DIR definition to autojump.lua"""
2014-01-18 15:08:36 +00:00
custom_install = "local AUTOJUMP_BIN_DIR = \"%s\"\n" % bin_dir.replace(
2016-04-29 06:33:04 +00:00
'\\',
'\\\\')
clink_file = os.path.join(clink_dir, 'autojump.lua')
with open(clink_file, 'r') as f:
original = f.read()
with open(clink_file, 'w') as f:
f.write(custom_install + original)
2014-01-18 15:08:36 +00:00
def parse_arguments(): # noqa
if platform.system() == 'Windows':
2014-01-18 15:08:36 +00:00
default_user_destdir = os.path.join(
2014-10-05 01:45:36 +00:00
os.getenv('LOCALAPPDATA', ''),
'autojump')
else:
2014-01-18 15:08:36 +00:00
default_user_destdir = os.path.join(
2016-04-29 06:33:04 +00:00
os.path.expanduser('~'),
2014-10-05 01:45:36 +00:00
'.autojump')
2013-12-30 20:49:59 +00:00
default_user_prefix = ''
default_user_zshshare = 'functions'
default_system_destdir = '/'
default_system_prefix = '/usr/local'
default_system_zshshare = '/usr/share/zsh/site-functions'
default_clink_dir = os.path.join(os.getenv('LOCALAPPDATA', ''), 'clink')
2013-12-30 20:05:24 +00:00
parser = ArgumentParser(
2014-10-05 01:45:36 +00:00
description='Installs autojump globally for root users, otherwise \
installs in current user\'s home directory.')
2013-12-30 20:05:24 +00:00
parser.add_argument(
2016-04-29 06:33:04 +00:00
'-n', '--dryrun', action='store_true', default=False,
2014-10-05 01:45:36 +00:00
help='simulate installation')
2013-12-31 16:48:24 +00:00
parser.add_argument(
2016-04-29 06:33:04 +00:00
'-f', '--force', action='store_true', default=False,
2014-10-05 01:45:36 +00:00
help='skip root user, shell type, Python version checks')
2013-12-30 20:05:24 +00:00
parser.add_argument(
2014-10-05 01:45:36 +00:00
'-d', '--destdir', metavar='DIR', default=default_user_destdir,
help='set destination to DIR')
2013-12-30 20:05:24 +00:00
parser.add_argument(
2014-10-05 01:45:36 +00:00
'-p', '--prefix', metavar='DIR', default=default_user_prefix,
help='set prefix to DIR')
2013-12-30 20:05:24 +00:00
parser.add_argument(
2014-10-05 01:45:36 +00:00
'-z', '--zshshare', metavar='DIR', default=default_user_zshshare,
help='set zsh share destination to DIR')
parser.add_argument(
2014-10-05 01:45:36 +00:00
'-c', '--clinkdir', metavar='DIR', default=default_clink_dir,
help='set clink directory location to DIR (Windows only)')
2013-12-30 20:05:24 +00:00
parser.add_argument(
2016-04-29 06:33:04 +00:00
'-s', '--system', action='store_true', default=False,
2014-10-05 01:45:36 +00:00
help='install system wide for all users')
2013-12-30 20:05:24 +00:00
args = parser.parse_args()
2013-12-31 16:48:24 +00:00
if not args.force:
if sys.version_info[0] == 2 and sys.version_info[1] < 6:
2016-04-29 06:33:04 +00:00
print('Python v2.6+ or v3.0+ required.', file=sys.stderr)
2013-12-31 16:48:24 +00:00
sys.exit(1)
if args.system:
if platform.system() == 'Windows':
2016-04-29 06:33:04 +00:00
print('System-wide installation is not supported on Windows.',
2014-01-12 19:34:28 +00:00
file=sys.stderr)
sys.exit(1)
elif os.geteuid() != 0:
2016-04-29 06:33:04 +00:00
print('Please rerun as root for system-wide installation.',
2014-01-12 19:34:28 +00:00
file=sys.stderr)
sys.exit(1)
2014-01-18 15:08:36 +00:00
if platform.system() != 'Windows' \
and get_shell() not in SUPPORTED_SHELLS:
2016-04-29 06:33:04 +00:00
print('Unsupported shell: %s' % os.getenv('SHELL'),
file=sys.stderr)
sys.exit(1)
2013-12-30 20:05:24 +00:00
2013-12-30 21:37:18 +00:00
if args.destdir != default_user_destdir \
or args.prefix != default_user_prefix \
or args.zshshare != default_user_zshshare:
args.custom_install = True
else:
args.custom_install = False
2013-12-30 20:05:24 +00:00
if args.system:
2013-12-30 21:37:18 +00:00
if args.custom_install:
2016-04-29 06:33:04 +00:00
print('Custom paths incompatible with --system option.',
2013-12-30 21:37:18 +00:00
file=sys.stderr)
sys.exit(1)
args.destdir = default_system_destdir
args.prefix = default_system_prefix
args.zshshare = default_system_zshshare
2013-12-30 20:05:24 +00:00
return args
def show_post_installation_message(etc_dir, share_dir, bin_dir):
if platform.system() == 'Windows':
2016-04-29 06:33:04 +00:00
print('\nPlease manually add %s to your user path' % bin_dir)
else:
2014-01-12 19:34:28 +00:00
if get_shell() == 'fish':
aj_shell = '%s/autojump.fish' % share_dir
2016-04-29 06:33:04 +00:00
source_msg = 'if test -f %s; . %s; end' % (aj_shell, aj_shell)
2014-01-12 19:34:28 +00:00
rcfile = '~/.config/fish/config.fish'
2013-12-31 16:59:01 +00:00
else:
2014-01-12 19:34:28 +00:00
aj_shell = '%s/autojump.sh' % etc_dir
2016-04-29 06:33:04 +00:00
source_msg = '[[ -s %s ]] && source %s' % (aj_shell, aj_shell)
2014-01-12 19:34:28 +00:00
if platform.system() == 'Darwin' and get_shell() == 'bash':
rcfile = '~/.profile'
else:
rcfile = '~/.%src' % get_shell()
2016-04-29 06:33:04 +00:00
print('\nPlease manually add the following line(s) to %s:' % rcfile)
2014-01-12 19:34:28 +00:00
print('\n\t' + source_msg)
if get_shell() == 'zsh':
2016-04-29 06:33:04 +00:00
print('\n\tautoload -U compinit && compinit -u')
2016-04-29 06:33:04 +00:00
print('\nPlease restart terminal(s) before running autojump.\n')
2013-12-30 20:05:24 +00:00
def main(args):
if args.dryrun:
2016-04-29 06:33:04 +00:00
print('Installing autojump to %s (DRYRUN)...' % args.destdir)
2013-12-30 20:05:24 +00:00
else:
2016-04-29 06:33:04 +00:00
print('Installing autojump to %s ...' % args.destdir)
2013-12-30 20:05:24 +00:00
bin_dir = os.path.join(args.destdir, args.prefix, 'bin')
2014-01-12 19:34:28 +00:00
etc_dir = os.path.join(args.destdir, 'etc', 'profile.d')
doc_dir = os.path.join(args.destdir, args.prefix, 'share', 'man', 'man1')
share_dir = os.path.join(args.destdir, args.prefix, 'share', 'autojump')
2013-12-30 20:05:24 +00:00
zshshare_dir = os.path.join(args.destdir, args.zshshare)
mkdir(bin_dir, args.dryrun)
mkdir(doc_dir, args.dryrun)
mkdir(etc_dir, args.dryrun)
mkdir(share_dir, args.dryrun)
2013-12-30 20:05:24 +00:00
cp('./bin/autojump', bin_dir, args.dryrun)
cp('./bin/autojump_argparse.py', bin_dir, args.dryrun)
2013-12-30 20:49:59 +00:00
cp('./bin/autojump_data.py', bin_dir, args.dryrun)
cp('./bin/autojump_utils.py', bin_dir, args.dryrun)
cp('./bin/icon.png', share_dir, args.dryrun)
cp('./docs/autojump.1', doc_dir, args.dryrun)
if platform.system() == 'Windows':
cp('./bin/autojump.lua', args.clinkdir, args.dryrun)
cp('./bin/autojump.bat', bin_dir, args.dryrun)
cp('./bin/j.bat', bin_dir, args.dryrun)
cp('./bin/jc.bat', bin_dir, args.dryrun)
cp('./bin/jo.bat', bin_dir, args.dryrun)
cp('./bin/jco.bat', bin_dir, args.dryrun)
if args.custom_install:
modify_autojump_lua(args.clinkdir, bin_dir, args.dryrun)
else:
mkdir(etc_dir, args.dryrun)
mkdir(share_dir, args.dryrun)
mkdir(zshshare_dir, args.dryrun)
2014-01-12 19:34:28 +00:00
cp('./bin/autojump.sh', etc_dir, args.dryrun)
cp('./bin/autojump.bash', share_dir, args.dryrun)
cp('./bin/autojump.fish', share_dir, args.dryrun)
cp('./bin/autojump.zsh', share_dir, args.dryrun)
2014-01-12 19:34:28 +00:00
cp('./bin/_j', zshshare_dir, args.dryrun)
if args.custom_install:
modify_autojump_sh(etc_dir, share_dir, args.dryrun)
2013-12-30 20:05:24 +00:00
show_post_installation_message(etc_dir, share_dir, bin_dir)
2013-12-30 20:05:24 +00:00
2014-01-18 15:08:36 +00:00
2016-04-29 06:33:04 +00:00
if __name__ == '__main__':
2013-12-30 20:05:24 +00:00
sys.exit(main(parse_arguments()))