You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

216 line
7.5 KiB

10 年之前
10 年之前
10 年之前
10 年之前
10 年之前
10 年之前
  1. #!/usr/bin/env python
  2. # -*- coding: utf-8 -*-
  3. from __future__ import print_function
  4. import os
  5. import platform
  6. import shutil
  7. import sys
  8. sys.path.append('bin')
  9. from autojump_argparse import ArgumentParser
  10. SUPPORTED_SHELLS = ('bash', 'zsh', 'fish', 'tcsh')
  11. def cp(src, dest, dryrun=False):
  12. print("copying file: %s -> %s" % (src, dest))
  13. if not dryrun:
  14. shutil.copy(src, dest)
  15. def get_shell():
  16. return os.path.basename(os.getenv('SHELL', ''))
  17. def mkdir(path, dryrun=False):
  18. print("creating directory:", path)
  19. if not dryrun and not os.path.exists(path):
  20. os.makedirs(path)
  21. def modify_autojump_sh(etc_dir, share_dir, dryrun=False):
  22. """Append custom installation path to autojump.sh"""
  23. custom_install = "\
  24. \n# check custom install \
  25. \nif [ -s %s/autojump.${shell} ]; then \
  26. \n source %s/autojump.${shell} \
  27. \nfi\n" % (share_dir, share_dir)
  28. with open(os.path.join(etc_dir, 'autojump.sh'), 'a') as f:
  29. f.write(custom_install)
  30. def modify_autojump_lua(clink_dir, bin_dir, dryrun=False):
  31. """Prepend custom AUTOJUMP_BIN_DIR definition to autojump.lua"""
  32. custom_install = "local AUTOJUMP_BIN_DIR = \"%s\"\n" % bin_dir.replace(
  33. "\\",
  34. "\\\\")
  35. clink_file = os.path.join(clink_dir, 'autojump.lua')
  36. with open(clink_file, 'r') as f:
  37. original = f.read()
  38. with open(clink_file, 'w') as f:
  39. f.write(custom_install + original)
  40. def parse_arguments(): # noqa
  41. if platform.system() == 'Windows':
  42. default_user_destdir = os.path.join(
  43. os.getenv('LOCALAPPDATA', ''),
  44. 'autojump')
  45. else:
  46. default_user_destdir = os.path.join(
  47. os.path.expanduser("~"),
  48. '.autojump')
  49. default_user_prefix = ''
  50. default_user_zshshare = 'functions'
  51. default_system_destdir = '/'
  52. default_system_prefix = '/usr/local'
  53. default_system_zshshare = '/usr/share/zsh/site-functions'
  54. default_clink_dir = os.path.join(os.getenv('LOCALAPPDATA', ''), 'clink')
  55. parser = ArgumentParser(
  56. description='Installs autojump globally for root users, otherwise \
  57. installs in current user\'s home directory.')
  58. parser.add_argument(
  59. '-n', '--dryrun', action="store_true", default=False,
  60. help='simulate installation')
  61. parser.add_argument(
  62. '-f', '--force', action="store_true", default=False,
  63. help='skip root user, shell type, Python version checks')
  64. parser.add_argument(
  65. '-d', '--destdir', metavar='DIR', default=default_user_destdir,
  66. help='set destination to DIR')
  67. parser.add_argument(
  68. '-p', '--prefix', metavar='DIR', default=default_user_prefix,
  69. help='set prefix to DIR')
  70. parser.add_argument(
  71. '-z', '--zshshare', metavar='DIR', default=default_user_zshshare,
  72. help='set zsh share destination to DIR')
  73. parser.add_argument(
  74. '-c', '--clinkdir', metavar='DIR', default=default_clink_dir,
  75. help='set clink directory location to DIR (Windows only)')
  76. parser.add_argument(
  77. '-s', '--system', action="store_true", default=False,
  78. help='install system wide for all users')
  79. args = parser.parse_args()
  80. if not args.force:
  81. if sys.version_info[0] == 2 and sys.version_info[1] < 6:
  82. print("Python v2.6+ or v3.0+ required.", file=sys.stderr)
  83. sys.exit(1)
  84. if args.system:
  85. if platform.system() == 'Windows':
  86. print("System-wide installation is not supported on Windows.",
  87. file=sys.stderr)
  88. sys.exit(1)
  89. elif os.geteuid() != 0:
  90. print("Please rerun as root for system-wide installation.",
  91. file=sys.stderr)
  92. sys.exit(1)
  93. if platform.system() != 'Windows' \
  94. and get_shell() not in SUPPORTED_SHELLS:
  95. print("Unsupported shell: %s" % os.getenv('SHELL'),
  96. file=sys.stderr)
  97. sys.exit(1)
  98. if args.destdir != default_user_destdir \
  99. or args.prefix != default_user_prefix \
  100. or args.zshshare != default_user_zshshare:
  101. args.custom_install = True
  102. else:
  103. args.custom_install = False
  104. if args.system:
  105. if args.custom_install:
  106. print("Custom paths incompatible with --system option.",
  107. file=sys.stderr)
  108. sys.exit(1)
  109. args.destdir = default_system_destdir
  110. args.prefix = default_system_prefix
  111. args.zshshare = default_system_zshshare
  112. return args
  113. def show_post_installation_message(etc_dir, share_dir, bin_dir):
  114. if platform.system() == 'Windows':
  115. print("\nPlease manually add %s to your user path" % bin_dir)
  116. else:
  117. if get_shell() == 'fish':
  118. aj_shell = '%s/autojump.fish' % share_dir
  119. source_msg = "if test -f %s; . %s; end" % (aj_shell, aj_shell)
  120. rcfile = '~/.config/fish/config.fish'
  121. else:
  122. aj_shell = '%s/autojump.sh' % etc_dir
  123. source_msg = "[[ -s %s ]] && source %s" % (aj_shell, aj_shell)
  124. if platform.system() == 'Darwin' and get_shell() == 'bash':
  125. rcfile = '~/.profile'
  126. else:
  127. rcfile = '~/.%src' % get_shell()
  128. print("\nPlease manually add the following line(s) to %s:" % rcfile)
  129. print('\n\t' + source_msg)
  130. if get_shell() == 'zsh':
  131. print("\n\tautoload -U compinit && compinit -u")
  132. print("\nPlease restart terminal(s) before running autojump.\n")
  133. def main(args):
  134. if args.dryrun:
  135. print("Installing autojump to %s (DRYRUN)..." % args.destdir)
  136. else:
  137. print("Installing autojump to %s ..." % args.destdir)
  138. bin_dir = os.path.join(args.destdir, args.prefix, 'bin')
  139. etc_dir = os.path.join(args.destdir, 'etc', 'profile.d')
  140. doc_dir = os.path.join(args.destdir, args.prefix, 'share', 'man', 'man1')
  141. share_dir = os.path.join(args.destdir, args.prefix, 'share', 'autojump')
  142. zshshare_dir = os.path.join(args.destdir, args.zshshare)
  143. mkdir(bin_dir, args.dryrun)
  144. mkdir(doc_dir, args.dryrun)
  145. mkdir(etc_dir, args.dryrun)
  146. mkdir(share_dir, args.dryrun)
  147. cp('./bin/autojump', bin_dir, args.dryrun)
  148. cp('./bin/autojump_argparse.py', bin_dir, args.dryrun)
  149. cp('./bin/autojump_data.py', bin_dir, args.dryrun)
  150. cp('./bin/autojump_utils.py', bin_dir, args.dryrun)
  151. cp('./bin/icon.png', share_dir, args.dryrun)
  152. cp('./docs/autojump.1', doc_dir, args.dryrun)
  153. if platform.system() == 'Windows':
  154. cp('./bin/autojump.lua', args.clinkdir, args.dryrun)
  155. cp('./bin/autojump.bat', bin_dir, args.dryrun)
  156. cp('./bin/j.bat', bin_dir, args.dryrun)
  157. cp('./bin/jc.bat', bin_dir, args.dryrun)
  158. cp('./bin/jo.bat', bin_dir, args.dryrun)
  159. cp('./bin/jco.bat', bin_dir, args.dryrun)
  160. if args.custom_install:
  161. modify_autojump_lua(args.clinkdir, bin_dir, args.dryrun)
  162. else:
  163. mkdir(etc_dir, args.dryrun)
  164. mkdir(share_dir, args.dryrun)
  165. mkdir(zshshare_dir, args.dryrun)
  166. cp('./bin/autojump.sh', etc_dir, args.dryrun)
  167. cp('./bin/autojump.bash', share_dir, args.dryrun)
  168. cp('./bin/autojump.fish', share_dir, args.dryrun)
  169. cp('./bin/autojump.zsh', share_dir, args.dryrun)
  170. cp('./bin/_j', zshshare_dir, args.dryrun)
  171. if args.custom_install:
  172. modify_autojump_sh(etc_dir, share_dir, args.dryrun)
  173. show_post_installation_message(etc_dir, share_dir, bin_dir)
  174. if __name__ == "__main__":
  175. sys.exit(main(parse_arguments()))