-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinstall-files
executable file
·47 lines (43 loc) · 1.52 KB
/
install-files
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
#!/usr/bin/env python
from __future__ import print_function
import os
import sys
def main():
import argparse
ap = argparse.ArgumentParser()
ap.add_argument('-n','--noaction', action='store_true',
help='Show commands but do not execute them')
ap.add_argument('util', nargs='+',
help='Utility to install')
args = ap.parse_args()
#
run = CmdRunner(verbose=True, noaction=args.noaction).run
destdir = os.path.expanduser('~/bin')
for utilspec in args.util:
# Allow "alias:realprog"
alias, _, util = utilspec.rpartition(':')
link = os.path.relpath(os.path.join('.', util), destdir)
dest = os.path.join(destdir, alias or util)
if os.path.islink(dest):
if os.readlink(dest) == link:
# Already linked correctly
continue
run('rm %s' % dest,
os.unlink, dest)
elif os.path.isdir(dest):
raise Exception('Cannot link %s: is a directory' % dest)
elif os.path.exists(dest):
run('rm %s' % dest,
os.unlink, dest)
run('ln -s %s %s' % (link, dest),
os.symlink, link, dest)
class CmdRunner:
def __init__(self, verbose=False, noaction=False):
self.verbose = verbose
self.noaction = noaction
def run(self, shcmd, func, *args, **kw):
if self.verbose or self.noaction:
print(shcmd, file=sys.stderr)
if not self.noaction:
func(*args, **kw)
main()