-
-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathscripts.py
More file actions
105 lines (85 loc) · 2.68 KB
/
scripts.py
File metadata and controls
105 lines (85 loc) · 2.68 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
#! /usr/bin/env python
"""Script to """
import os
import sys
import argparse
from bdb import BdbQuit
from pysyte.bash import shell
import script_path
__version__ = "0.1.0"
class ScriptError(NotImplementedError):
pass
def run_args(args, methods):
"""Run any methods eponymous with args"""
if not args:
return False
valuable_args = {k for k, v in args.__dict__.items() if v}
arg_methods = {methods[a] for a in valuable_args if a in methods}
for method in arg_methods:
method(args)
def version(args):
print("%s %s" % (args, __version__))
raise SystemExit
def parse_args(methods):
"""Parse out command line arguments"""
parser = argparse.ArgumentParser(description=__doc__.splitlines()[0])
parser.add_argument(
"keys", metavar="keys", type=str, nargs="*", help="keys to find"
)
parser.add_argument("-l", "--list", action="store_true", help="$ ls -l scripts")
parser.add_argument(
"-m",
"--mains",
action="store_true",
help="show python scripts with main methods",
)
parser.add_argument(
"-p", "--python", action="store_true", help="show python scripts only"
)
parser.add_argument("-s", "--shortlist", action="store_true", help="$ ls scripts")
parser.add_argument("-v", "--version", action="store_true", help="Show version")
args = parser.parse_args()
run_args(args, methods)
return args
def lines(path_to_item):
if path_to_item.isfile():
return path_to_item.lines()
return [""]
def script(args):
paths_to_items = script_path.arg_paths(args.keys)
result = False
for path_to_item in paths_to_items:
if args.python:
if path_to_item.ext != ".py":
if "python" not in lines(path_to_item)[0]:
continue
elif args.mains:
for line in lines(path_to_item):
if line.startswith("def main("):
break
else:
continue
if args.list or args.shortlist:
list_command = "PATH=/bin:/usr/bin ls" + " -l" if args.list else ""
command = "%s %s" % (list_command, path_to_item)
try:
print(shell.run(command))
result = True
continue
except shell.BashError:
pass
print(path_to_item)
result = True
return result
def main():
"""Run the script"""
try:
args = parse_args(globals())
return os.EX_OK if script(args) else not os.EX_OK
except BdbQuit:
pass
except SystemExit as e:
return e.code
return os.EX_OK
if __name__ == "__main__":
sys.exit(main())