-
-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathadd_to_a_path.py
More file actions
139 lines (109 loc) · 3.25 KB
/
add_to_a_path.py
File metadata and controls
139 lines (109 loc) · 3.25 KB
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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
"""Script to display a collection of paths after inserting one new path
Usage:
add_to_a_path.py [-U] PATHS PATH
add_to_a_path.py [-U] (-s | -i INDEX ) PATHS PATH
Options:
-h, --help Show this help and exit
-v, --version Show version number and exit
-s, --start Add the path at start of list of paths
-i INDEX, --index=INDEX The index at which the path will be inserted
Examples of use:
$ export PATH=/bin:/usr/bin
$ add_to_a_path.py PATH /usr/local/bin
PATH=/bin:/usr/bin:/usr/local/bin
$ add_to_a_path.py PATH /usr/local/bin --start
PATH=/usr/local/bin:/bin:/usr/bin
"""
import os
import sys
import argparse
from bdb import BdbQuit
__version__ = "0.1.0"
class ScriptError(NotImplementedError):
pass
def version():
print(__version__)
raise SystemExit
def parse_args():
"""Parse out command line arguments"""
parser = argparse.ArgumentParser(description=__doc__.splitlines()[0])
parser.add_argument("symbol", help="The bash symbol to be changed")
parser.add_argument("path", help="The path to be added")
parser.add_argument(
"-s",
"--start",
action="store_true",
help="Add the path at start of list of paths",
)
parser.add_argument(
"-i", "--index", type=int, help="The index at which the path will be inserted"
)
parser.add_argument("-v", "--version", action="store_true", help="Show version")
args = parser.parse_args()
if args.version:
version()
if not args.index:
if args.start:
args.index = 0
else:
args.index = False
return args
def _add_symbol_to_paths(paths, symbol, i):
if i is False:
i = len(paths)
result = paths[:]
if not symbol:
return result
if symbol not in result:
result.insert(i, symbol)
return result
j = result.index(symbol)
if i != j:
del result[j]
result.insert(i, symbol)
return result
def get_arg_path(args):
path = args.path
if not path:
return ""
user_path = os.path.expanduser(path)
real_path = os.path.realpath(user_path)
if not os.path.isdir(real_path):
return ""
return real_path
def split_paths(string):
if not string:
return []
return [p for p in string.split(os.path.pathsep) if p]
def get_paths(args):
symbol = args.symbol
paths_string = ""
if symbol in os.environ:
paths_string = os.environ[symbol]
elif os.path.pathsep in symbol:
paths_string = symbol
return split_paths(paths_string)
def script(args):
arg_path = get_arg_path(args)
paths = get_paths(args)
if not arg_path:
if not paths:
return False
elif os.path.isdir(arg_path):
if arg_path in paths:
paths.remove(arg_path)
paths = _add_symbol_to_paths(paths, arg_path, args.index)
else:
return False
print("=".join((args.symbol, os.path.pathsep.join(paths))))
return True
def main():
"""Run the script"""
try:
args = parse_args()
return os.EX_OK if script(args) else not os.EX_OK
except (SystemExit, BdbQuit):
pass
return os.EX_OK
if __name__ == "__main__":
sys.exit(main())