-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpathv.py
executable file
·54 lines (52 loc) · 1.81 KB
/
pathv.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
#!/usr/bin/env python3
import sys, os, argparse, shlex
import os.path as osp
def pathv(add=None, index=None, remove=None, contains=None, lines=False,
evalable=False):
value = os.environ['PATH']
values = value.split(os.pathsep)
if contains:
if not isinstance(contains, (list, tuple)):
contains = [contains]
values = {osp.normcase(osp.normpath(osp.expandvars(val)))
for val in values}
for path in contains:
if osp.normcase(osp.abspath(path)) in values:
print('"%s" in path' % path)
else:
print('"%s" not in path' % path)
return
if remove is not None:
if remove not in values:
raise ValueError('"%s" not in path' % remove)
values.remove(remove)
if add is not None:
if add in values:
raise ValueError('"%s" already in path' % add)
if index is None:
index = len(values)
values.insert(index, add)
if add is not None or remove is not None:
value = os.pathsep.join(values)
if lines:
print(value.replace(os.pathsep, '\n'))
elif evalable:
if sys.platform == 'win32':
print('set PATH=' + value)
else:
print('PATH=' + shlex.quote(value))
else:
print(value)
if __name__ == '__main__':
parser = argparse.ArgumentParser()
parser.add_argument('-a', '--add')
parser.add_argument('-i', '--index', type=int)
parser.add_argument('-r', '--remove')
parser.add_argument('-c', '--contains', nargs='+')
parser.add_argument('-l', '--lines', action='store_true')
parser.add_argument('-e', '--evalable', action='store_true')
args = parser.parse_args()
try:
pathv(**vars(args))
except Exception as e:
sys.exit(f'error: {e}')