-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathpushbutton.py
More file actions
executable file
·137 lines (100 loc) · 3.22 KB
/
pushbutton.py
File metadata and controls
executable file
·137 lines (100 loc) · 3.22 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
#!/usr/bin/env python
"""
Pushbutton is a shiny facade for any polyglot mess
"""
from __future__ import print_function
import os
import sys
import subprocess
_BASENAME_ENV = "PUSHBUTTON_BASENAME"
_BASENAME = os.environ.get(_BASENAME_ENV, "pushbutton")
# magic files
_F_ALL = ".all"
_F_EXEC = ".exec"
_F_HELP = ".help"
_F_LIST = ".list"
# magic args
_A_ALL = "all"
_A_HELP = "help"
_A_LIST = "list"
_MAGIC_ARGS = (_A_ALL, _A_HELP, _A_LIST)
def _exec(fpath, args):
return subprocess.call([fpath] + args)
def _get_options(path):
return [i for i in os.listdir(path) if not i.startswith(".")]
def _list_action(path, args):
# magic .list overrides default help
if os.path.isfile(os.path.join(path, _F_LIST)):
return _exec(os.path.join(path, _F_LIST), args)
_default_list(path)
return 0
def _default_list(path):
print("subcommands: %s" % ", ".join(_get_options(path)))
def _help_action(path, args, stack):
"""
N.B. caller determines exit code!
"""
# magic .help overrides default help
if os.path.isfile(os.path.join(path, _F_HELP)):
_exec(os.path.join(path, _F_HELP), args)
_default_help(path, args, stack)
def _default_help(path, args, stack):
"""
N.B. caller determines exit code!
"""
print("Usage: %s SUBCOMMAND" % " ".join(stack))
_default_list(path)
print("(you could also say: %s)" % ", ".join(_MAGIC_ARGS))
if args:
print("You said: %s" % " ".join(args))
def _all_action(path, args):
# magic .all overrides default help
if os.path.isfile(os.path.join(path, _F_ALL)):
return _exec(os.path.join(path, _F_ALL), args)
return _default_all(path, args)
def _default_all(path, args):
code = 0
for action in _get_options(path):
code += _exec(os.path.join(path, action), args)
return code
def pushbutton_exec_file(path, args):
return _exec(path, args)
def pushbutton_exec_dir(path, args, stack):
if args:
if args[0] == _A_ALL:
return _all_action(path, args)
elif args[0] == _A_HELP:
_help_action(path, args[1:], stack)
return 0
elif args[0] == _A_LIST:
return _list_action(path, args[1:])
# magic .exec overrides default behavior
if os.path.isfile(os.path.join(path, _F_EXEC)):
return _exec(os.path.join(path, _F_EXEC), args)
_help_action(path, args, stack)
return 1
def pushbutton_exec(path, args, stack):
if os.path.isdir(path):
return pushbutton_exec_dir(path, args, stack)
return pushbutton_exec_file(path, args)
def pushbutton_navigate(tree, args, stack=(_BASENAME,)):
if not args:
return pushbutton_exec(tree, [], stack)
walker = os.walk(tree)
nextcmd, nextargs = args[0], args[1:]
root, dirs, files = next(walker)
nextpath = os.path.join(root, nextcmd)
if nextcmd in files:
return pushbutton_exec(nextpath, nextargs, stack)
elif nextcmd in dirs:
return pushbutton_navigate(
nextpath, nextargs, stack=stack + (nextcmd,),
)
else:
return pushbutton_exec(tree, args, stack)
def _main():
tree = sys.argv[1]
args = sys.argv[2:]
sys.exit(pushbutton_navigate(tree, args))
if __name__ == "__main__":
_main()