-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrun
More file actions
executable file
·93 lines (64 loc) · 2.24 KB
/
run
File metadata and controls
executable file
·93 lines (64 loc) · 2.24 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
#!/usr/bin/python
import os
import os.path
import sys
import imp
import inspect
verbose = False
# verbose = True
this_root = os.path.dirname(os.path.abspath(__file__))
def find_run_file(file_path):
global verbose
test_path = os.path.join(file_path, "run_file")
if(verbose): print("test_path: " + test_path)
if os.path.exists(test_path):
if(verbose): print("found run_file: " + test_path)
return(test_path, file_path)
else:
if(file_path == "/"):
test_path = os.path.join(this_root, "run_file")
file_path = this_root
if(os.path.exists(test_path)):
return(test_path, file_path)
else:
return(None, None)
(parent_tree, current_dir) = os.path.split(file_path)
if(parent_tree == ""):
parent_tree = "/"
return find_run_file(parent_tree)
def use(run_file, run_file_path, usage):
args = sys.argv[1:]
if len(args) == 0:
function_name = 'default'
else:
function_name = args[0]
for name, fun in inspect.getmembers(run_file, inspect.isfunction):
if fun.__name__ == function_name:
return fun(args[1:])
# we only get here if we don't find the function
exit_code = 0
if len(args) > 0:
if args[0] != 'help':
print("command '" + args[0] + "' was not found in run_file: " + run_file_path)
exit_code = 1
print(usage)
print("available commands: ")
print(' ' + 'help')
for name, fun in inspect.getmembers(run_file, inspect.isfunction):
if(fun.__name__[0] != "_" and fun.__name__ != "default"):
print(' ' + fun.__name__)
return(exit_code)
def main():
(run_file_path, run_file_root) = find_run_file(os.getcwd())
if(run_file_path == None):
print('"run_file" not found in this directory, or any of it\'s parent directories.')
sys.exit(1)
sys.dont_write_bytecode = True
run_file = imp.load_source("run_file", run_file_path)
sys.dont_write_bytecode = False
saved_cwd = os.getcwd()
os.chdir(run_file_root)
exit_code = use(run_file, run_file_path, "usage: <command> [<arg1> <arg2> ...]")
os.chdir(saved_cwd)
sys.exit(exit_code)
main()