-
Notifications
You must be signed in to change notification settings - Fork 18
/
runvm.py
157 lines (131 loc) · 5.12 KB
/
runvm.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
import sys
try:
from pydos_ui import input
except:
pass
if sys.implementation.name.upper() == "CIRCUITPYTHON":
from supervisor import reload
startupfile = 'code'
else:
startupfile = 'main'
import os
def runvm(runargv):
def chkPath(tstPath):
validPath = True
simpPath = ""
if tstPath != []:
savDir = os.getcwd()
for path in tstPath:
if path == "":
os.chdir("/")
elif os.getcwd() == "/" and path == "..":
validPath = False
break
elif path == ".":
continue
elif path == ".." and len(os.getcwd().split('/')) == 2:
os.chdir('/')
elif path == "..":
os.chdir("..")
elif path in os.listdir() and (os.stat(path)[0] & (2**15) == 0):
os.chdir(path)
else:
validPath = False
simpPath = ""
break
if validPath:
simpPath = os.getcwd()
os.chdir(savDir)
return((validPath,simpPath))
def absolutePath(argPath,currDir):
if argPath[0] == '/':
fullPath = argPath
elif currDir == '/':
fullPath = '/'+argPath
else:
fullPath = currDir+'/'+argPath
if len(fullPath) > 1 and fullPath[-1] == '/':
fullPath = fullPath[:-1]
return(fullPath)
def pFmt(dPath,trailSlh=True):
if dPath == "":
return '/'
elif dPath == '/':
return dPath
elif trailSlh:
return dPath+('/' if dPath[-1]!='/' else "")
else:
return dPath[:(-1 if dPath[-1] == '/' else None)]
args = runargv.split(" ")
savDir = os.getcwd()
args[0] = absolutePath(args[0],savDir)
aPath = args[0].split("/")
newdir = aPath.pop(-1)
(validPath, tmpDir) = chkPath(aPath)
if tmpDir == "" or tmpDir[-1] != "/":
tmpDir += "/"
if validPath:
if newdir in os.listdir(pFmt(tmpDir,False)):
if startupfile+'.py' in os.listdir('/'):
if startupfile+'._PyD' in os.listdir('/'):
if startupfile+'.py' in os.listdir('/'):
os.remove('/'+startupfile+'.py')
else:
os.rename('/'+startupfile+'.py','/'+startupfile+'._PyD')
t = open('/'+startupfile+'.py','w')
r = open(tmpDir+newdir,'r')
if sys.implementation.name.upper() == "CIRCUITPYTHON":
t.write('from supervisor import reload\n')
elif sys.implementation.name.upper() == "MICROPYTHON":
t.write('from sys import exit\n')
t.write('import os\n')
t.write('try:\n')
t.write(' os.chdir("'+tmpDir[:(-1 if tmpDir != "/" else None)]+'")\n')
t.write(" global passedIn\n")
t.write(" passedIn = '"+" ".join(args[1:])+"'\n")
t.write(" global envVars\n")
t.write(" envVars = {}\n")
for _ in envVars:
if _ != ".neopixel":
t.write(" envVars['"+_+"']='"+str(envVars[_]).replace("'",chr(92)+"'")+"'\n")
t.write(" __name__ = 'PyDOS'\n")
for pgmLine in r:
t.write(" "+pgmLine)
t.write("\nexcept:\n")
t.write(" print('****ERROR**** running '+'"+tmpDir+newdir+"')\n")
t.write("os.chdir('/')\n")
t.write("os.remove('/"+startupfile+".py')\n")
if startupfile+'._PyD' in os.listdir('/'):
t.write("os.rename('/"+startupfile+"._PyD','/"+startupfile+".py')\n")
t.write('print("\\n\\nIf PyDOS doesn'+"'"+'t start, press Ctrl-D at the >>> REPL prompt")\n')
t.write('print("===========================================================\\n\\n")\n')
if sys.implementation.name.upper() == "CIRCUITPYTHON":
t.write('reload()\n')
elif sys.implementation.name.upper() == "MICROPYTHON":
t.write('exit()\n')
t.close()
r.close()
#supervisor.set_next_code_file('/PyD-code.py',reload_on_success=True,reload_on_error=True)
print("\n\nIf "+tmpDir+newdir+" doesn't start, press Ctrl-D at the >>> REPL prompt")
print("======================================================"+len(tmpDir+newdir)*"="+"\n\n")
if sys.implementation.name.upper() == "CIRCUITPYTHON":
reload()
elif sys.implementation.name.upper() == "MICROPYTHON":
sys.exit()
else:
print("Invalid Python file: "+tmpDir+newdir)
else:
print("Invalid target path: "+args[0])
if __name__ != "PyDOS":
passedIn = ""
if passedIn == "":
passedIn = input("Python script to run: ")
runargv = passedIn
passedIn = ""
argline = runargv.split(" ")
runargv = argline[0]
if "." not in runargv:
runargv = runargv + ".py"
if len(argline) > 1:
runargv = runargv + " " + " ".join(argline[1:])
runvm(runargv)