-
Notifications
You must be signed in to change notification settings - Fork 6
/
athdir
executable file
·93 lines (80 loc) · 3.69 KB
/
athdir
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
#!/usr/bin/python
import athdir
import sys
import logging
from optparse import OptionParser
def path_callback(option, opt_str, value, parser):
assert value is None
value = []
for arg in parser.rargs:
if arg[:2] == "--" and len(arg) > 2:
break
if arg[:1] == "-" and len(arg) > 1:
break
value.append(arg)
del parser.rargs[:len(value)]
setattr(parser.values, option.dest, value)
usage = """%prog path [type]
or: athdir [-t type] [-p path ...] [-e] [-c] [-l] [-d | -i]
[-r recsep] [-f format] [-s sysname] [-m machtype]"""
parser = OptionParser(usage=usage)
parser.set_defaults(suppressEditorials=False, suppressSearch=False,
listAll=False, forceIndependent=False,
forceDependent=False, recsep="\n",
lockerpath=['%p'], type='%t',
format=None, sysname=None,
machtype=None, debug=False)
parser.add_option("-t", dest="type", action="store",
help="directory type (e.g. 'bin', 'lib', ...)")
parser.add_option("-p", dest="lockerpath", action="callback",
callback=path_callback, help="One or more paths")
parser.add_option("-e", dest="suppressEditorials", action="store_true",
help="Supress editorializing about legacy paths")
parser.add_option("-c", dest="suppressSearch", action="store_true",
help="Choose the first acceptable path")
parser.add_option("-l", dest="listAll", action="store_true",
help="List all search paths")
parser.add_option("-i", dest="forceIndependent", action="store_true",
help="Force athdir to choose a machine-independent path")
parser.add_option("-d", dest="forceDependent", action="store_true",
help="Force athdir to choose a machine-dependent path")
parser.add_option("-r", dest="recsep", action="store",
help="Override the record separator (newline)")
parser.add_option("-f", dest="format", action="store",
help="Supply a custom path format string")
parser.add_option("-s", dest="sysname", help="Override ATHENA_SYS value")
parser.add_option("-m", dest="machtype", help="Override machtype value")
parser.add_option("--debug", dest="debug", action="store_true",
help="Verbose debugging")
if len(sys.argv) < 2:
sys.exit(parser.get_usage())
# athdir can be invoked one of two ways.
# a) athdir path [type=bin]
# b) athdir [bunch of options with no position arguments]
# This is the first invocation. There's probably a better way.
if len(sys.argv) <= 3 and not any(x.startswith('-') for x in sys.argv):
a = athdir.Athdir(sys.argv[1], sys.argv[2] if len(sys.argv) == 3 else 'bin')
paths = a.get_paths()
if len(paths) > 0:
print ''.join(paths)
sys.exit(0 if len(paths) > 0 else 1)
(options, args) = parser.parse_args()
if options.debug:
logging.basicConfig(level=logging.DEBUG)
if (len(args) > 0):
sys.exit(parser.get_usage())
if options.forceDependent and options.forceIndependent:
parser.error("-d and -i are mutually exclusive")
if not options.suppressSearch and (options.forceDependent or
options.forceIndependent):
parser.error("-d and -i are meaningless without -c")
paths = []
for p in options.lockerpath:
a = athdir.Athdir(p, options.type, options.format,
options.sysname, options.machtype)
paths += a.get_paths(options.suppressEditorials, options.suppressSearch,
options.forceDependent, options.forceIndependent,
options.listAll)
if len(paths) > 0:
print options.recsep.join(paths)
sys.exit(0 if len(paths) > 0 else 1)