-
Notifications
You must be signed in to change notification settings - Fork 1
/
sgrun.py
240 lines (209 loc) · 6.01 KB
/
sgrun.py
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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
#!/usr/bin/env python3
import os, sys, re, subprocess, json
from backports.shutil_which import which
prefix = []
exes = []
cwd = "."
plugins = []
p = os.path.abspath(".")
foldername = ""
args = sys.argv[1:]
if not args:
args = []
def path_equals(a, b):
return os.path.normpath(os.path.expanduser(a)) == os.path.normpath(
os.path.expanduser(b)
)
def run():
global args
global exes
global prefix
r = 1
optional_prefix = False
for arg in args:
if arg.startswith("---p="):
arg = arg[len("---p=") :]
prefix += arg.split(",")
break
if arg.startswith("---op="):
arg = arg[len("---op=") :]
prefix += arg.split(",")
break
if prefix and not optional_prefix and not which(prefix[0]):
print("prefix not found")
return 1
if "---l" in args:
print(os.path.join(cwd, exes[0]))
return 0
elif "---d" in args:
print(os.path.dirname(cwd))
return 0
else:
args = [a for a in args if not a.startswith("---")]
try:
with open(os.path.join(cwd, os.path.dirname(exes[0]), "sg.json"), "r") as f:
j = json.load(f)
args += j["args"]
except:
pass
if os.name == "nt":
r = subprocess.call(prefix + [os.path.join(cwd, exes[0])] + args, cwd=(cwd))
else:
r = subprocess.call(prefix + ["./" + exes[0]] + args, cwd=(cwd))
return r
def advanced():
global exes
global args
global foldername
if len(exes) == 1:
return run()
# plugin: project name hint
try:
if len(exes) > 1 and foldername:
for exe in exes:
if os.path.basename(exe) == foldername:
exes = [foldername]
return run()
except:
pass
# TODO: plugin: package.json
try:
import json
with open("package.json") as f:
j = json.load(f)
exe = j["main"] + ".js"
if os.path.isfile(exe):
exes = [exe]
return run()
except:
pass
# TODO: plugin: python
# try:
while True:
pys = []
for fn in os.listdir("."):
fnl = fn.lower()
# if fnl == "requirements.txt":
# if os.name == "nt":
# exes = ["python"] # TODO: version detection?
if fnl.endswith(".py"):
pys += [fn]
if len(pys) == 1:
pass
elif len(pys) > 1:
for py in pys:
if os.name == "nt":
exes = ["python"]
args = [fn] + args
break
else:
exes = [fn]
break
fn_noext = fn[:-3]
fn_noext_l = fn_noext.lower()
if fn_noext_l == "main" or fnl_noext_l == foldername:
if os.name == "nt":
exes = ["python"]
args = [fn] + args
break
else:
exes = [fn]
break
break
# TODO: plugin: expression match
while True:
use_regex = False
idx = -1
try:
idx = args.index("---e")
use_regex = True
except:
pass
if idx == -1:
try:
idx = args.index("---n")
use_regex = False
except:
pass
if idx == -1:
break
try:
expr = args[idx + 1]
except:
print("---e flag needs expression param")
return 1
del args[idx + 1]
del args[idx]
if not use_regex:
if expr not in exes:
print('"%s" does not exist' % expr)
return 1
exes = [expr]
return run()
else: # regex
exes = [e for e in exes if re.search(expr, e)]
if exes:
return advanced()
else:
print("No executables matching expression")
return 1
break
# plugin: ignore tests
if len(exes) > 1:
exes_old = exes[:]
exes = [e for e in exes_old if "test" not in e.lower()]
if len(exes) == 1:
exit(run())
else:
# restore and continue
exes = exes_old
if len(exes) > 1:
print("Found %s executables: %s" % (len(exes), ", ".join(exes)))
print("Too many executables")
elif len(exes) == 0:
print("No executables")
return 1
class Break(Exception):
pass
def exe(fn):
if os.path.isdir(fn):
return False
if os.name == "nt":
return fn.lower().endswith(".exe")
else:
return os.access(fn, os.X_OK)
p = os.path.abspath(".")
try:
while True:
try:
cwd = os.path.join(p, "bin")
for f in os.listdir(cwd):
fn = os.path.join(p, "bin/%s" % f)
if exe(fn):
exes += [f]
if exes:
raise Break
except OSError:
pass
try:
cwd = p
for f in os.listdir(p):
fn = f
if exe(fn):
exes += [f]
fnp = os.path.basename(os.getcwd())
if fnp == "bin":
fnp = os.path.basename(
os.path.abspath(os.path.join(os.getcwd(), ".."))
)
foldername = os.path.join(os.path.basename(fnp))
if exes:
raise Break
except OSError:
pass
p = os.path.abspath(os.path.join(p, ".."))
if p == "/" or path_equals(p, "~") or path_equals(p, "~/bin"):
break
except Break:
pass
exit(advanced())