Addresses code review comments

This commit is contained in:
John Richards
2021-10-18 21:37:46 -04:00
parent 51f607fb1c
commit 7e2609c3d6

View File

@@ -53,7 +53,6 @@ PYVER_CONTAINERS = {
'3.9', '3.9',
'3.10', '3.10',
} }
USE_CONTAINERS = False
CONTAINER_EXES = ['docker', 'podman'] CONTAINER_EXES = ['docker', 'podman']
@@ -191,7 +190,7 @@ def local_compile(snekdir, ver, infile):
return outfile return outfile
def container_compile(snekdir, ver, infile): def container_compile(ver, infile):
if ver not in PYVER_CONTAINERS: if ver not in PYVER_CONTAINERS:
print('Container compilation not supported for version {}'.format(ver)) print('Container compilation not supported for version {}'.format(ver))
return None return None
@@ -208,19 +207,24 @@ def container_compile(snekdir, ver, infile):
fullver = PYVERS[ver] fullver = PYVERS[ver]
indir = os.path.dirname(os.path.abspath(infile))
infile = os.path.basename(infile)
if infile.endswith('.py'): if infile.endswith('.py'):
outfile = os.path.basename(infile)[:-3] outfile = infile[:-3]
else: else:
outfile = os.path.basename(infile) outfile = infile
outfile += '.{}.pyc'.format(ver) outfile += '.{}.pyc'.format(ver)
if os.path.exists(outfile): if os.path.exists(outfile):
os.unlink(outfile) os.unlink(outfile)
print('*** Compiling for Python {}'.format(fullver)) print('*** Compiling for Python {}'.format(fullver))
# The easy way proc = subprocess.Popen([container_exe, 'run', '--rm', '--name', '{}'.format(outfile),
proc = subprocess.Popen([container_exe, 'run', '--privileged', '--rm', '--name', '{}-{}'.format(infile, ver), '-v', '{}:/indir:Z'.format(indir),
'-v', '{}:/src'.format(snekdir), '-w', '/src', 'python:{}'.format(fullver), '-v', '{}:/outdir:Z'.format(os.getcwd()), '-w', '/outdir',
'python', '-c', "import py_compile; py_compile.compile('{}', '{}')".format(infile, outfile)]) 'python:{}'.format(fullver),
'python', '-c',
"import py_compile; py_compile.compile('/indir/{}', '{}')".format(infile, outfile)])
proc.communicate() proc.communicate()
return outfile return outfile
@@ -230,7 +234,8 @@ if len(sys.argv) < 2:
print('Usage: {} [-c] [versions] input.py'.format(sys.argv[0])) print('Usage: {} [-c] [versions] input.py'.format(sys.argv[0]))
print('Compile input.py for one or more python versions') print('Compile input.py for one or more python versions')
print() print()
print('-c\tuse prebuilt containers for running different versions of Python (not available for all versions)') print('-c\tuse prebuilt containers for running different versions of Python')
print('\t(not available for all versions)')
print() print()
print('Output is written to input.<version>.pyc for each version successfully compiled') print('Output is written to input.<version>.pyc for each version successfully compiled')
print() print()
@@ -241,6 +246,7 @@ RE_PYVER = re.compile(r'\d\.\d')
pythons = [] pythons = []
infile = None infile = None
use_containers = False
for arg in sys.argv[1:]: for arg in sys.argv[1:]:
if RE_PYVER.match(arg): if RE_PYVER.match(arg):
if arg in PYVERS.keys(): if arg in PYVERS.keys():
@@ -249,7 +255,7 @@ for arg in sys.argv[1:]:
print('Unknown Python version: {}'.format(arg)) print('Unknown Python version: {}'.format(arg))
sys.exit(1) sys.exit(1)
elif arg == '-c': elif arg == '-c':
USE_CONTAINERS = True use_containers = True
elif arg.startswith('-'): elif arg.startswith('-'):
print("WARNING: Unrecognized argument '{}'".format(arg)) print("WARNING: Unrecognized argument '{}'".format(arg))
else: else:
@@ -269,14 +275,14 @@ if len(pythons) == 0:
snekdir = os.path.dirname(os.path.realpath(__file__)) snekdir = os.path.dirname(os.path.realpath(__file__))
result = 0 result = 0
for ver in pythons: for ver in pythons:
compile_with_container = USE_CONTAINERS compile_with_container = use_containers
if USE_CONTAINERS and ver not in PYVER_CONTAINERS: if use_containers and ver not in PYVER_CONTAINERS:
print('Warning: No supported container for {} - using local build'.format(ver)) print('Warning: No supported container for {} - using local build'.format(ver))
compile_with_container = False compile_with_container = False
outfile = None outfile = None
if compile_with_container: if compile_with_container:
outfile = container_compile(snekdir, ver, infile) outfile = container_compile(ver, infile)
else: else:
outfile = local_compile(snekdir, ver, infile) outfile = local_compile(snekdir, ver, infile)