Better pypy handling.
parent
de6b3e89bd
commit
0181269a15
72
main.py
72
main.py
|
@ -8,7 +8,29 @@ import sys
|
|||
exs = ['py', 'exe']
|
||||
sol_pattern = re.compile(r'(\d+)_.+\.(?:{})'.format('|'.join(exs)))
|
||||
|
||||
try:
|
||||
subprocess.run(['pypy', '--version'],
|
||||
stdout=subprocess.DEVNULL,
|
||||
stderr=subprocess.DEVNULL)
|
||||
|
||||
pypy_installed = True
|
||||
except FileNotFoundError:
|
||||
print('warning : install pypy for faster solving time')
|
||||
pypy_installed = False
|
||||
|
||||
try:
|
||||
subprocess.run(['pypy3', '--version'],
|
||||
stdout=subprocess.DEVNULL,
|
||||
stderr=subprocess.DEVNULL)
|
||||
|
||||
pypy3_installed = True
|
||||
except FileNotFoundError:
|
||||
print('warning : install pypy3 for faster solving time')
|
||||
pypy3_installed = False
|
||||
|
||||
|
||||
def print_bar(i, count):
|
||||
global print
|
||||
step = 1
|
||||
while count // step > 80:
|
||||
step += 1
|
||||
|
@ -16,31 +38,44 @@ def print_bar(i, count):
|
|||
j = i // step
|
||||
bar = '[{}{}] {}/{}'.format('#'*(j), '.'*(w - j), i, count)
|
||||
print(bar, end='\r')
|
||||
return len(bar)
|
||||
|
||||
old_print = print
|
||||
def new_print(x, *args, **kwargs):
|
||||
global print
|
||||
print = old_print
|
||||
print(x + ' '*(len(bar) - len(x)), *args, **kwargs)
|
||||
print = new_print
|
||||
|
||||
|
||||
def get_cmd(fn):
|
||||
ext = fn.split('.')[-1]
|
||||
if ext == 'py':
|
||||
with open(fn) as f:
|
||||
shebang = f.read().lstrip().split('\n')[0]
|
||||
if shebang[:2] != '#!':
|
||||
cmd = 'python '
|
||||
|
||||
if pypy_installed:
|
||||
return shebang[2:].replace('python', 'pypy') + ' '
|
||||
if pypy3_installed:
|
||||
return shebang[2:].replace('python3', 'pypy3') + ' '
|
||||
return './'
|
||||
|
||||
|
||||
|
||||
def run(fn, cmd=None):
|
||||
if cmd == None:
|
||||
ext = fn.split('.')[-1]
|
||||
if ext == 'py':
|
||||
with open(fn) as f:
|
||||
shebang = f.read().lstrip().split('\n')[0]
|
||||
if shebang[:2] != '#!':
|
||||
cmd = 'pypy '
|
||||
else:
|
||||
cmd = shebang[2:].replace('python', 'pypy') + ' '
|
||||
else:
|
||||
cmd = './'
|
||||
cmd = get_cmd(fn) if cmd==None else cmd
|
||||
|
||||
proc = subprocess.run(cmd + fn, shell=True,
|
||||
stdout=subprocess.PIPE,
|
||||
stderr=subprocess.PIPE)
|
||||
if len(proc.stderr) > 0:
|
||||
if 'pypy' in cmd:
|
||||
print('warning : pypy failed. trying cpython', file=sys.stderr)
|
||||
print(proc.stderr)
|
||||
#print('warning : pypy failed. trying cpython', file=sys.stderr)
|
||||
#print(proc.stderr)
|
||||
return run(fn, cmd = cmd.replace('pypy', 'python'))
|
||||
print('error : error while running solution')
|
||||
print(proc.stderr)
|
||||
print(proc.stderr.decode('utf-8'), end='')
|
||||
|
||||
return proc.stdout.decode('utf-8').rstrip().split('\n')[-1]
|
||||
|
||||
|
@ -53,14 +88,11 @@ def main():
|
|||
print('Num Solution')
|
||||
print('---------------')
|
||||
for i, x in enumerate(solved, start=1):
|
||||
bar_len = print_bar(i, len(fs))
|
||||
print_bar(i, len(fs))
|
||||
|
||||
f = fs[x]
|
||||
r = run(f)
|
||||
fst_str = '{:>3d} {}'.format(x, r)
|
||||
print(fst_str, end='')
|
||||
line = ' ' * (bar_len - len(fst_str))
|
||||
print(line)
|
||||
print('{:>3d} {}'.format(x, r))
|
||||
|
||||
if __name__ == '__main__':
|
||||
main()
|
||||
|
|
Loading…
Reference in New Issue