Skip to content
Merged
Changes from 1 commit
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
a2e4494
Ignore the tests outdir.
ericsnowcurrently Oct 26, 2021
9675b69
Add test helpers for the "freeze" tool.
ericsnowcurrently Oct 26, 2021
a34dca4
Add a test for the "freeze" tool.
ericsnowcurrently Oct 26, 2021
77f334d
Create the outdir.
ericsnowcurrently Oct 26, 2021
561f8e1
Build in the source tree for tests.
ericsnowcurrently Oct 26, 2021
bd54ddd
Copy the repo during tests.
ericsnowcurrently Oct 26, 2021
5d0ec9e
Force the git pull.
ericsnowcurrently Oct 26, 2021
b879a3c
Show the commands.
ericsnowcurrently Oct 26, 2021
5639939
Fix the relfile.
ericsnowcurrently Oct 26, 2021
172a259
Only print some of the run commands.
ericsnowcurrently Oct 26, 2021
875cbe3
Actually print the config var.
ericsnowcurrently Oct 26, 2021
8e4e52e
Fix a kwarg.
ericsnowcurrently Oct 26, 2021
bf95b31
Fall back to Makefile if sysconfig fails.
ericsnowcurrently Oct 26, 2021
a8e2cf0
Do not clean up first if there is not Makefile.
ericsnowcurrently Oct 26, 2021
fab3dad
Clean up get_config_var().
ericsnowcurrently Oct 26, 2021
ef579ac
Do not duplicate configure args.
ericsnowcurrently Oct 26, 2021
7338d60
Clean up the copied repo first.
ericsnowcurrently Oct 26, 2021
f6a8755
Hide error text in get_config_var().
ericsnowcurrently Oct 26, 2021
b12477e
Skip the freeze tests if the tool is missing.
ericsnowcurrently Oct 26, 2021
2dd674d
Be explicit about the -j option.
ericsnowcurrently Oct 26, 2021
cc68b86
Do not use the -C option with git.
ericsnowcurrently Oct 26, 2021
9b6aee8
Simplify.
ericsnowcurrently Oct 26, 2021
4b315b0
Skip the test if running with "-u -cpu".
ericsnowcurrently Oct 27, 2021
aa56cfc
Do not run the test on buildbots.
ericsnowcurrently Oct 27, 2021
4c18103
Add test.support.skip_if_buildbot().
ericsnowcurrently Oct 27, 2021
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Add test helpers for the "freeze" tool.
  • Loading branch information
ericsnowcurrently committed Oct 26, 2021
commit 9675b6924a0854cd78c18a7937649a470109699b
154 changes: 154 additions & 0 deletions Tools/freeze/test/freeze.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,154 @@
import os
import os.path
import shutil
import subprocess


TESTS_DIR = os.path.dirname(__file__)
TOOL_ROOT = os.path.dirname(TESTS_DIR)
SRCDIR = os.path.dirname(os.path.dirname(TOOL_ROOT))

CONFIGURE = os.path.join(SRCDIR, 'configure')
MAKE = shutil.which('make')
FREEZE = os.path.join(TOOL_ROOT, 'freeze.py')
OUTDIR = os.path.join(TESTS_DIR, 'outdir')


class UnsupportedError(Exception):
"""The operation isn't supported."""


def _run_cmd(cmd, cwd, verbose=True):
proc = subprocess.run(
cmd,
cwd=cwd,
capture_output=not verbose,
text=True,
)
if proc.returncode != 0:
print(proc.stderr, file=sys.stderr)
proc.check_returncode()
return proc.stdout


##################################
# building Python

def configure_python(outdir, prefix=None, cachefile=None, *, verbose=True):
if not prefix:
prefix = os.path.join(outdir, 'python-installation')
builddir = os.path.join(outdir, 'python-build')
print(f'configuring python in {builddir}...')
os.makedirs(builddir, exist_ok=True)
cmd = [CONFIGURE, f'--prefix={prefix}']
if cachefile:
cmd.extend(['--cache-file', cachefile])
_run_cmd(cmd, builddir, verbose)
return builddir


def get_prefix(build=None):
if build:
if os.path.isfile(build):
return _run_cmd(
[build, '-c' 'import sys; print(sys.prefix)'],
cwd=os.path.dirname(build),
)

builddir = build
else:
builddir = os.path.abspath('.')
# We have to read it out of Makefile.
makefile = os.path.join(builddir, 'Makefile')
try:
infile = open(makefile)
except FileNotFoundError:
return None
with infile:
for line in infile:
if line.startswith('prefix='):
return line.partition('=')[-1].strip()
return None


def build_python(builddir=None, *, verbose=True):
if not MAKE:
raise UnsupportedError('make')

if builddir:
builddir = os.path.abspath(builddir)
else:
builddir = SRCDIR
if builddir != SRCDIR:
_run_cmd([MAKE, 'clean'], SRCDIR, verbose=False)

print(f'building python in {builddir}...')
_run_cmd([MAKE, '-j'], builddir, verbose)

return os.path.join(builddir, 'python')


def install_python(builddir=None, *, verbose=True):
if not MAKE:
raise UnsupportedError('make')

if not builddir:
builddir = '.'
prefix = get_prefix(builddir)

print(f'installing python into {prefix}...')
_run_cmd([MAKE, '-j', 'install'], builddir, verbose)

if not prefix:
return None
return os.path.join(prefix, 'bin', 'python3')


##################################
# freezing

def pre_freeze(outdir=None, *, verbose=True):
if not outdir:
outdir = OUTDIR
cachefile = os.path.join(outdir, 'python-config.cache')
builddir = configure_python(outdir, cachefile=cachefile, verbose=verbose)
build_python(builddir, verbose=verbose)
return install_python(builddir, verbose=verbose)


def freeze(python, scriptfile, outdir=None, *, verbose=True):
if not MAKE:
raise UnsupportedError('make')

if not outdir:
outdir = OUTDIR

print(f'freezing {scriptfile}...')
os.makedirs(outdir, exist_ok=True)
subprocess.run(
[python, FREEZE, '-o', outdir, scriptfile],
cwd=outdir,
capture_output=not verbose,
check=True,
)

subprocess.run(
[MAKE],
cwd=os.path.dirname(scriptfile),
capture_output=not verbose,
check=True,
)

name = os.path.basename(scriptfile).rpartition('.')[0]
executable = os.path.join(outdir, name)
return executable


def run(executable):
proc = subprocess.run(
[executable],
capture_output=True,
text=True,
check=True,
)
return proc.stdout.strip()