-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mucro.py
79 lines (67 loc) · 1.88 KB
/
mucro.py
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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
"""
Mucro
=====
"""
import argparse
import os
import textwrap
import shutil
__version__ = '2017.10.1'
def make_executable(path):
# http://stackoverflow.com/a/30463972/170656
mode = os.stat(path).st_mode
mode |= (mode & 0o444) >> 2 # copy R bits to X
os.chmod(path, mode)
def main(args):
contents = textwrap.dedent('''\
#!/usr/bin/env bash
"{}" "{}" "$@"
''')
contents = contents.format(
shutil.which('python'),
os.path.abspath(args.pyfile)
)
if not args.wrapper:
args.wrapper = os.path.splitext(
os.path.basename(
args.pyfile
)
)[0]
sym = os.path.join(os.path.abspath(args.bindir), args.wrapper)
if os.path.exists(sym):
raise EnvironmentError(
'Desired symlink already exists in "{}". Aborting'.format(
args.bindir
)
)
with open(args.wrapper, 'w+') as f:
f.write(contents)
wrapper = os.path.abspath(args.wrapper)
make_executable(wrapper)
os.symlink(wrapper, sym)
uninstaller = os.path.abspath(args.wrapper + '-uninstall')
with open(uninstaller, 'w+') as f:
for item in (sym, wrapper, uninstaller):
f.write('rm "%s"\n' % item)
make_executable(uninstaller)
if __name__ == "__main__":
parser = argparse.ArgumentParser()
parser.add_argument(
'--pyfile',
help='Python script to wrap',
required=True
)
parser.add_argument(
'--wrapper',
help=('Name of the new shell script to be created. If '
'not provided, the filename component of the --pyfile '
'option will be used, without the .py extension.'),
default=None
)
parser.add_argument(
'--bindir',
help='Destination of the symlink',
required=True
)
args = parser.parse_args()
main(args)