Skip to content

Commit

Permalink
various fixes
Browse files Browse the repository at this point in the history
- make-venv: backport fix for python/cpython#98251
  from 3.11
- make-venv: don’t eat the output from the ensurepip invocation
- validate-environment: try to get the runner to not mask the _name_
  of any environment variable whose _value_ needs to be masked.
- validate-environment: handle windows-style suffixed executables
  • Loading branch information
zackw committed Oct 15, 2024
1 parent 4f95516 commit cc75a03
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 5 deletions.
24 changes: 23 additions & 1 deletion .github/scripts/make-venv
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ Customized for our needs, relative to what the stock `python3 -m venv` does:
import argparse
import os
import stat
import subprocess
import sys
import venv

Expand Down Expand Up @@ -77,13 +78,13 @@ unset VIRTUAL_ENV_PROMPT
hash -r 2> /dev/null
"""


SH_PYTHON2_STUB = """\
#! /bin/sh
echo "$0: error: Python 2 interpreter not available." >&2
exit 1
"""


def require_regular_file(path):
"""Raise an exception if PATH does not exist or is not a regular
file or a symlink to a regular file, being specific about what
Expand Down Expand Up @@ -178,6 +179,7 @@ class CustomEnvBuilder(venv.EnvBuilder):

def ensure_directories(self, env_dir):
sys.stderr.write("creating directories...\n")
env_dir = os.path.normcase(os.path.realpath(env_dir))
context = super().ensure_directories(env_dir)

# we always want <env>/bin, not <env>/scripts
Expand Down Expand Up @@ -241,6 +243,26 @@ class CustomEnvBuilder(venv.EnvBuilder):
if do_chmod:
os.chmod(python2, 0o755)

def _setup_pip(self, context):
# differences from base class:
# - bug fix for gh-98251 backported from 3.11
# - don't swallow the output from the command

# gh-98251: We do not want to just use '-I' because that masks
# legitimate user preferences (such as not writing bytecode). All we
# really need is to ensure that the path variables do not overrule
# normal venv handling.
kwargs['env'] = env = os.environ.copy()
env['VIRTUAL_ENV'] = context.env_dir
env.pop('PYTHONHOME', None)
env.pop('PYTHONPATH', None)
kwargs['cwd'] = context.env_dir
kwargs['executable'] = context.env_exec_cmd
subprocess.check_call([
context.env_exec_cmd,
"-Im", "ensurepip", "--upgrade", "--default-pip"
], **kwargs)

def setup_scripts(self, context):
sys.stderr.write("setting up scripts...\n")
# always install the sh activation script; on windows also
Expand Down
20 changes: 16 additions & 4 deletions .github/scripts/validate-environment
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,9 @@ def quote_str(s):
def log_environment(ofp):
ofp.write("::group::Environment variables\n")
for key, value in sorted(os.environ.items()):
ofp.write("{}={}\n".format(quote_str(key), quote_str(value)))
# Spacing here prevents the runner from redacting the name of
# any environment variable whose value is a masked secret.
ofp.write("{} = {}\n".format(quote_str(key), quote_str(value)))
ofp.write("\n")


Expand All @@ -57,6 +59,13 @@ def log_dir_contents(ofp, path):
ofp.write(f" {name}{tag}\n")


def log_file_contents(ofp, path):
ofp.write(quote_str(path) + ":\n")
with open(path, "rt") as fp:
for line in fp:
ofp.write(f"| {line.rstrip()}\n")


def log_cwd(ofp):
ofp.write("::group::Working directory\n")
log_dir_contents(ofp, os.getcwd())
Expand Down Expand Up @@ -94,6 +103,7 @@ def log_venv(ofp):
ofp.write("\n")
log_dir_contents(ofp, os.path.join(venv_root, 'bin'))
ofp.write("\n")
log_file_contents(ofp, os.path.join(venv_root, 'pyvenv.cfg'))
return 0

except OSError as e:
Expand Down Expand Up @@ -166,9 +176,11 @@ def log_commands(ofp, expected_pyver):
for cmd in commands:
for bin_dir in bin_path:
path = os.path.join(os.path.realpath(bin_dir), cmd)
if os.path.isfile(path):
programs.append((cmd, path))
break
for suffix in ("", ".exe", ".bat"):
xpath = path + suffix
if os.path.isfile(xpath):
programs.append((cmd, xpath))
break
else:
programs.append((cmd, None))

Expand Down

0 comments on commit cc75a03

Please sign in to comment.