-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathrunner.py
More file actions
112 lines (95 loc) · 3.83 KB
/
runner.py
File metadata and controls
112 lines (95 loc) · 3.83 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
import os
import sys
requiredVersion = (3, 12, 0)
def pythonVersionOk(v):
(reqMajor, reqMinor, reqMicro) = requiredVersion
if v.major < reqMajor or v.minor < reqMinor:
return False
if v.major == reqMajor and v.minor == reqMinor and v.micro < reqMicro:
return False
else:
return True
if not pythonVersionOk(sys.version_info):
vStr = sys.version.split()[0]
reqVStr = '.'.join([str(x) for x in requiredVersion])
print(f"""
Python in version {reqVStr} or newer is required. You are still using version {vStr}, please upgrade!
""")
sys.exit(1)
from . import ansi
from . import cmdlineArgs
from .constants import *
from . import exceptionHandler
from . import i18n
from . import interactive
from .myLogging import *
from . import paths
from . import replTester
from . import runCode
from . import version as versionMod
def printWelcomeString(file, version, doTypecheck):
cwd = os.getcwd() + "/"
if file.startswith(cwd):
file = file[len(cwd):]
versionStr = '' if not version else 'Version %s, ' % version
pythonVersion = sys.version.split()[0]
tycheck = ''
if not doTypecheck:
tycheck = ', no typechecking'
msg = i18n.tr('=== WELCOME to ') + '"Write Your Python Program" ' + \
'(%sPython %s, %s%s) ===' % (versionStr, pythonVersion, file, tycheck)
fullMsg = (10 * '\n') + ansi.green(msg) + '\n'
printStderr(fullMsg)
def main(globals, argList=None):
(args, restArgs) = cmdlineArgs.parseCmdlineArgs(argList)
if args.verbose:
enableVerbose()
if args.debug:
enableDebug()
verbose(f'VERBOSE={args.verbose}, DEBUG={args.debug}')
if args.lang:
if args.lang in i18n.allLanguages:
i18n.setLang(args.lang)
else:
printStderr(f'Unsupported language {args.lang}. Supported: ' + ', '.join(i18n.allLanguages))
sys.exit(1)
isInteractive = args.interactive
version = versionMod.readVersion()
fileToRun: str|None = args.file
if fileToRun is None:
if args.repls:
replTester.testRepls(args.repls, globals)
return
if not os.path.exists(fileToRun):
printStderr(f'File {fileToRun} does not exist')
sys.exit(1)
if args.changeDir:
d = os.path.dirname(fileToRun)
os.chdir(d)
fileToRun = os.path.basename(fileToRun)
debug(f'Changed directory to {d}, fileToRun={fileToRun}')
if not args.checkRunnable and (not args.quiet or args.verbose):
printWelcomeString(fileToRun, version, doTypecheck=args.checkTypes)
libDefs = runCode.prepareLib(onlyCheckRunnable=args.checkRunnable, enableTypeChecking=args.checkTypes)
runDir = os.path.dirname(fileToRun)
with (runCode.RunSetup(runDir, [fileToRun] + restArgs),
paths.projectDir(os.path.abspath(os.getcwd()))):
globals['__name__'] = '__wypp__'
sys.modules['__wypp__'] = sys.modules['__main__']
loadingFailed = False
try:
verbose(f'running code in {fileToRun}')
debug(f'sys.path: {sys.path}')
globals['__file__'] = fileToRun
globals = runCode.runStudentCode(fileToRun, globals, args.checkRunnable,
doTypecheck=args.checkTypes, extraDirs=args.extraDirs)
except Exception as e:
verbose(f'Error while running code in {fileToRun}')
exceptionHandler.handleCurrentException(exit=not isInteractive)
loadingFailed = True
runCode.performChecks(args.check, args.testFile, globals, libDefs, doTypecheck=args.checkTypes,
extraDirs=args.extraDirs, loadingFailed=loadingFailed)
if args.repls:
replTester.testRepls(args.repls, globals)
if isInteractive:
interactive.enterInteractive(globals, args.checkTypes, loadingFailed)