-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathy
executable file
·149 lines (118 loc) · 3.56 KB
/
y
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
#!/usr/bin/env python
import os
import sys
import subprocess
from threading import Thread, Event
from queue import Queue, Empty
DEFAULT_PAGER = '/usr/bin/less'
try:
import fcntl,termios,struct
data = fcntl.ioctl(sys.stdout.fileno(), termios.TIOCGWINSZ, '1234')
LINES_ON_SCREEN = struct.unpack('hh',data)[0]
except:
LINES_ON_SCREEN = 25
too_spammy = Event()
def enqueue_output(out, queue):
for line in iter(out.readline, b''):
queue.put(line)
if too_spammy.is_set():
queue.put(out.read())
break
queue.put(None)
def run(args, calling_python = True):
if calling_python:
args.insert(0, sys.executable)
print("y: now running:")
print("y: " + " ".join(args))
env = dict(os.environ)
env['PYTHONPATH'] = '.'
env['TERM'] = 'screen'
process = subprocess.Popen(
args=args,
env=env,
stdout=subprocess.PIPE,
)
queue = Queue()
thread = Thread(target=enqueue_output, args=(process.stdout, queue))
thread.daemon = True
thread.start()
seen = b''
lines_count = 0
seen_test_error_line = False
while True:
line = queue.get()
if line is None:
break
seen += line
if b'______' in line:
seen_test_error_line = True
lines_count += 1
if too_spammy.is_set():
if queue.qsize()==0:
break
else:
if seen_test_error_line and lines_count > LINES_ON_SCREEN:
sys.stdout.write('\n\n (Stand by...)\n')
too_spammy.set()
thread.join()
try:
seen += queue.get_nowait()
except Empty:
pass
try:
sys.stdout.write(line.decode('utf-8'))
except ValueError:
sys.stdout.write(repr(line))
sys.stdout.flush()
lines_printed = len(seen.split(b'\n'))
if lines_printed > LINES_ON_SCREEN:
pager = os.environ.get('PAGER', DEFAULT_PAGER)
subprocess.run(
args=[pager,
'-R', # enable colour (on less, anyway)
],
input=seen,
)
return process.returncode
def run_tests(log_level, args):
a = ['-m',
'pytest',
f'--log-level={log_level}',
'--color=yes',
'-s',
]
a.extend(args)
result = run(a)
if result:
print("y: result:", result)
def show_usage_banner():
print("""y - run yex without installing
y - shows this help
y - - runs yex with no arguments
y test - runs the test suite
y test <testname> - runs all tests with a name containing <testname>;
this turns on debug logging and so on
anything else - runs yex with those arguments
""")
def main():
if len(sys.argv)==1:
show_usage_banner()
elif len(sys.argv)==2 and sys.argv[1]=='-':
run(['-m', 'yex'])
elif len(sys.argv)>=2 and sys.argv[1]=='test':
if len(sys.argv)==3 and not sys.argv[2].startswith('-'):
run_tests(
log_level='DEBUG',
args = ['-vv', '-k', sys.argv[2]],
)
else:
run_tests(
log_level='WARN',
args=sys.argv[1:],
)
else:
args = ['-m', 'yex']
args.extend(sys.argv[1:])
run(args)
if __name__=='__main__':
main()