diff --git a/bin/autojump b/bin/autojump index 03c0b09..d56b2ba 100755 --- a/bin/autojump +++ b/bin/autojump @@ -21,6 +21,7 @@ from __future__ import print_function +from argparse import ArgumentParser from difflib import SequenceMatcher from itertools import chain from math import sqrt @@ -38,8 +39,6 @@ else: from itertools import ifilter from itertools import imap -from argparse import ArgumentParser - from data import dictify from data import entriefy from data import Entry @@ -59,7 +58,7 @@ from utils import sanitize from utils import surround_quotes from utils import take -VERSION = 'release-v21.8-alpha1' +VERSION = '21.8-alpha' FUZZY_MATCH_THRESHOLD = 0.6 TAB_ENTRIES_COUNT = 9 TAB_SEPARATOR = '__' @@ -102,7 +101,7 @@ def parse_arguments(): help='add path') parser.add_argument( '-i', '--increase', metavar='WEIGHT', nargs='?', type=int, - const=20, default=False, + const=10, default=False, help='increase current directory weight') parser.add_argument( '-d', '--decrease', metavar='WEIGHT', nargs='?', type=int, diff --git a/install.py b/install.py new file mode 100755 index 0000000..c4e7f57 --- /dev/null +++ b/install.py @@ -0,0 +1,121 @@ +#!/usr/bin/env python2 +# -*- coding: utf-8 -*- + +from __future__ import print_function + +from argparse import ArgumentParser +import os +import shutil +import sys + + +SUPPORTED_SHELLS = ('bash', 'zsh', 'fish') + + +def cp(src, dest, dryrun=False): + print("copying file: %s -> %s" % (src, dest)) + if not dryrun: + shutil.copy(src, dest) + + +def get_shell(): + return os.path.basename(os.getenv('SHELL')) + + +def mkdir(path, dryrun=False): + print("creating directory:", path) + if not dryrun and not os.path.exists(path): + os.makedirs(path) + + +def parse_arguments(): + default_destdir = os.path.join(os.path.expanduser("~"), '.autojump') + default_prefix = '' + default_zshshare = 'functions' + + parser = ArgumentParser( + description='Installs autojump globally for root users, otherwise \ + installs in current user\'s home directory.') + parser.add_argument( + '-n', '--dryrun', action="store_true", default=False, + help='simulate installation') + parser.add_argument( + '-d', '--destdir', metavar='DIR', default=default_destdir, + help='set destination to DIR') + parser.add_argument( + '-p', '--prefix', metavar='DIR', default=default_prefix, + help='set prefix to DIR') + parser.add_argument( + '-z', '--zshshare', metavar='DIR', default=default_zshshare, + help='set zsh share destination to DIR') + parser.add_argument( + '-s', '--system', action="store_true", default=False, + help='install system wide for all users') + + args = parser.parse_args() + + if sys.version_info[0] == 2 and sys.version_info[1] < 7: + print("Python v2.7+ or v3.0+ required.", file=sys.stderr) + sys.exit(1) + + if get_shell() not in SUPPORTED_SHELLS: + print("Unsupported shell: %s" % os.getenv('SHELL'), file=sys.stderr) + sys.exit(1) + + if args.system: + if os.geteuid() != 0: + 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' + + return args + + +def print_post_installation_message(etc_dir): + rcfile = '~/.%src' % get_shell() + aj_shell = '%s/autojump.%s' % (etc_dir, get_shell()) + print("\nPlease manually add the following line to %s:\n" % rcfile) + print("\t[[ -s %s ]] && source %s\n" % (aj_shell, aj_shell)) + print("Please restart terminal(s) before running autojump.\n") + + +def main(args): + if args.dryrun: + print("Installing autojump to %s (DRYRUN)..." % args.destdir) + else: + print("Installing autojump to %s ..." % args.destdir) + + 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) + + mkdir(bin_dir, args.dryrun) + mkdir(etc_dir, args.dryrun) + mkdir(doc_dir, args.dryrun) + mkdir(icon_dir, args.dryrun) + 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.sh', etc_dir, args.dryrun) + cp('./bin/autojump.bash', etc_dir, args.dryrun) + cp('./bin/autojump.fish', etc_dir, args.dryrun) + cp('./bin/autojump.zsh', etc_dir, args.dryrun) + cp('./bin/_j', zshshare_dir, args.dryrun) + cp('./bin/icon.png', icon_dir, args.dryrun) + cp('./docs/autojump.1', doc_dir, args.dryrun) + + # TODO(ting|2013-12-30): modify autojump.sh for custom installs + + print_post_installation_message(etc_dir) + +if __name__ == "__main__": + sys.exit(main(parse_arguments())) diff --git a/install.sh b/install.sh deleted file mode 100755 index 07dcd7f..0000000 --- a/install.sh +++ /dev/null @@ -1,249 +0,0 @@ -#!/usr/bin/env bash - -function help_msg { - echo "./install.sh [OPTION..]" - echo - echo " -a, --auto Try to determine destdir, prefix (and zshshare if applicable)" - echo " -g, --global Use default global settings (destdir=/; prefix=usr)" - echo " -l, --local Use default local settings (destdir=~/.autojump)" - echo - echo " -d, --destdir PATH Set install destination to PATH" - echo " -p, --prefix PATH Use PATH as prefix" - echo " -Z, --zshshare PATH Use PATH as zsh share destination" - echo - echo " -f, --force Ignore Python version check" - echo " -n, --dry_run Only show installation paths, don't install anything" - echo - echo "Will install autojump into:" - echo - echo ' Binaries: $destdir$prefix/bin' - echo ' Documentation: $destdir$prefix/share/man/man1' - echo ' Icon: $destdir$prefix/share/autojump' - echo ' Shell scripts: $destdir/etc/profile.d' - echo ' zsh functions: $destdir$zshsharedir' - echo - echo 'Unless specified, $zshshare will be :' - echo ' - $destdir$prefix/functions for local installations' - echo ' - $destdir$prefix/share/zsh/site-functions for all other installations' -} - -dry_run= -local= -global= -force= -shell=`echo ${SHELL} | awk -F/ '{ print $NF }'` -destdir= -prefix="usr/local" -zshsharedir= - -# If no arguments passed, default to --auto. -if [[ ${#} == 0 ]]; then - set -- "--auto" -fi - -# Only dry-run should also default to --auto -if [[ ${#} == 1 ]] && ([[ $1 = "-n" ]] || [[ $1 = "--dry-run" ]]); then - set -- "-n" "--auto" -fi - -# Command line parsing -while true; do - case "$1" in - -a|--auto) - if [[ ${UID} -eq 0 ]]; then - set -- "--global" "${@:2}" - else - set -- "--local" "${@:2}" - fi - ;; - -d|--destdir) - if [ $# -gt 1 ]; then - destdir=$2; shift 2 - else - echo "--destdir or -d requires an argument" 1>&2 - fi - ;; - -f|--force) - force=true - shift - if [[ ${#} == 0 ]]; then - set -- "--auto" - fi - ;; - -g|--global) - global=true - destdir= - prefix=usr - shift - ;; - -h|--help|-\?) - help_msg; - exit 0 - ;; - -l|--local) - local=true - destdir=~/.autojump - prefix= - shift - ;; - -n|--dry_run) - dry_run=true - shift - ;; - -p|--prefix) - if [ $# -gt 1 ]; then - prefix=$2; shift 2 - else - echo "--prefix or -p requires an argument" 1>&2 - exit 1 - fi - ;; - -Z|--zshshare) - if [ $# -gt 1 ]; then - zshsharedir=$2; shift 2 - else - echo "--zshshare or -Z requires an argument" 1>&2 - exit 1 - fi - ;; - --) - shift - break - ;; - -*) - echo "invalid option: $1" 1>&2; - help_msg; - exit 1 - ;; - *) - break - ;; - esac -done - -# destdir must be a full path, and end with a slash -if [[ -n ${destdir} ]]; then - if [[ ${destdir:0:1} != "/" ]]; then - echo "Error: destdir must be a full path" 1>&2 - exit 1 - fi - len=${#destdir} - if [[ ${destdir:len - 1} != "/" ]]; then - destdir="$destdir/" - fi -else - destdir="/" -fi - -# prefix should not start with, and end with, a slash -if [[ -n ${prefix} ]]; then - if [[ ${prefix:0:1} == "/" ]]; then - prefix=${prefix:1} - fi - len=${#prefix} - if [[ ${prefix:len - 1} != "/" ]]; then - prefix="$prefix/" - fi -fi - -# check shell support -if [[ ${shell} != "bash" ]] && [[ ${shell} != "zsh" ]] \ - && [[ ${shell} != "fish" ]]; then - echo "Unsupported shell (${shell}). Only Bash and Zsh shells are supported." - exit 1 -fi - -# zsh functions -if [[ $shell == "zsh" ]]; then - if [[ -z $zshsharedir ]]; then - # if not set, use a default - if [[ $local ]]; then - zshsharedir="${prefix}functions" - else - zshsharedir="${prefix}share/zsh/site-functions" - fi - fi -fi - -# check Python version -if [ ! ${force} ]; then - python_version=`python -c 'import sys; print(sys.version_info[:])'` - - if [[ ${python_version:1:1} -eq 2 && ${python_version:4:1} -lt 6 ]]; then - echo - echo "Incompatible Python version, please upgrade to v2.6+." - if [[ ${python_version:4:1} -ge 4 ]]; then - echo - echo "Alternatively, you can download v12 that supports Python v2.4+ from:" - echo - echo -e "\thttps://github.com/joelthelion/autojump/downloads" - echo - fi - exit 1 - fi -fi - -echo -echo "Installating autojump..." -echo -echo "Destination: $destdir" -if [[ -n $prefix ]]; then - echo "Prefix: /$prefix" -fi -echo -echo "Binary: ${destdir}${prefix}bin/" -echo "Documentation: ${destdir}${prefix}share/man/man1/" -echo "Icon: ${destdir}${prefix}share/autojump/" -echo "Shell scripts: ${destdir}etc/profile.d/" -if [[ -z $shell ]] || [[ $shell == "zsh" ]]; then - echo "zsh functions: ${destdir}${zshsharedir}" -fi -echo - -if [[ $dry_run ]]; then - echo "--dry_run (-n) used, stopping" - exit -fi - -# INSTALL AUTOJUMP -mkdir -p ${destdir}${prefix}share/autojump/ || exit 1 -mkdir -p ${destdir}${prefix}bin/ || exit 1 -mkdir -p ${destdir}${prefix}share/man/man1/ || exit 1 -cp -v ./bin/icon.png ${destdir}${prefix}share/autojump/ || exit 1 -cp -v ./bin/autojump ${destdir}${prefix}bin/ || exit 1 -cp -v ./bin/data.py ${destdir}${prefix}bin/ || exit 1 -cp -v ./bin/utils.py ${destdir}${prefix}bin/ || exit 1 -cp -v ./docs/autojump.1 ${destdir}${prefix}share/man/man1/ || exit 1 -mkdir -p ${destdir}etc/profile.d/ || exit 1 -cp -v ./bin/autojump.sh ${destdir}etc/profile.d/ || exit 1 -cp -v ./bin/autojump.bash ${destdir}etc/profile.d/ || exit 1 -cp -v ./bin/autojump.zsh ${destdir}etc/profile.d/ || exit 1 -cp -v ./bin/autojump.fish ${destdir}etc/profile.d/ || exit 1 -mkdir -p ${destdir}${zshsharedir} || exit 1 -# TODO: remove unused _j function (2013.02.01_1348, ting) -install -v -m 0755 ./bin/_j ${destdir}${zshsharedir} || exit 1 - -# MODIFY AUTOJUMP.SH FOR CUSTOM INSTALLS -if [[ -z ${local} ]] && [[ -z ${global} ]]; then - sed -i "s:#custom#\t::g" ${destdir}etc/profile.d/autojump.sh - sed -i "s:destdir_install\t:${destdir}etc/profile.d:g" ${destdir}etc/profile.d/autojump.sh -fi - -# DISPLAY ADD MESSAGE -rc_file="~/.${shell}rc" -if [[ `uname` == "Darwin" ]] && [[ ${shell} == "bash" ]]; then - rc_file="~/.bash_profile" -fi - -aj_shell_file="${destdir}etc/profile.d/autojump.${shell}" -if [[ ${local} ]]; then - aj_shell_file="~/.autojump/etc/profile.d/autojump.${shell}" -fi - -echo -echo "Please add the line to ${rc_file} :" -echo -echo -e "[[ -s ${aj_shell_file} ]] && . ${aj_shell_file}" -echo -echo "You need to run 'source ${rc_file}' before you can start using autojump. To remove autojump, run './uninstall.sh'" -echo