Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for running with Python 3.6 and 3.7 #32

Merged
merged 55 commits into from
Mar 16, 2019
Merged
Show file tree
Hide file tree
Changes from 40 commits
Commits
Show all changes
55 commits
Select commit Hold shift + click to select a range
4be519a
Parse targeted Python version via pants.ini value
Eric-Arellano Mar 12, 2019
5f9c5d3
Update docs to mention pinning Python version
Eric-Arellano Mar 12, 2019
2dcdcfc
Remove pants_engine_python_version from pants.ini
Eric-Arellano Mar 12, 2019
3b1c763
Fix interpreter_version failing script when interpreter_version not f…
Eric-Arellano Mar 12, 2019
5531a2b
Add ci.py to CI to allow parameterizing pants_version and python_version
Eric-Arellano Mar 12, 2019
4d2791a
Squashed commit of the following:
Eric-Arellano Mar 12, 2019
65cae1e
Implement naming scheme
Eric-Arellano Mar 12, 2019
481a2d3
Update interpreter selection test to modify pants.ini
Eric-Arellano Mar 12, 2019
28f3c41
Rename venv folders to py27 etc., not py2.7
Eric-Arellano Mar 12, 2019
a9d1f77
Upgrade version to 1.15.0.dev4
Eric-Arellano Mar 12, 2019
37bdbde
Merge branch 'gh-pages' of github.com:pantsbuild/setup into py3
Eric-Arellano Mar 13, 2019
30321a1
Rename to pants_runtime_python_version
Eric-Arellano Mar 14, 2019
a2ef6e2
Refactor ci.py
Eric-Arellano Mar 14, 2019
dc7257c
Merge branch 'gh-pages' of github.com:pantsbuild/setup into py3
Eric-Arellano Mar 14, 2019
3382787
Check interpreter is valid
Eric-Arellano Mar 14, 2019
b1fd519
Fix not using keyword arg
Eric-Arellano Mar 14, 2019
ed937fa
Remove stry pdb import
Eric-Arellano Mar 14, 2019
21ad22f
Nest conditionals in write_config_entry
Eric-Arellano Mar 14, 2019
8721501
Fix one more bad reference to pants_engine_python_version
Eric-Arellano Mar 14, 2019
b4e921c
No longer change documentation
Eric-Arellano Mar 14, 2019
d772494
Skip test that will always fail until 1.15.0
Eric-Arellano Mar 14, 2019
b7ef228
Also remove pants_runtime_python_version from pants.ini
Eric-Arellano Mar 14, 2019
d0dab99
Add back accidently deleted <p>
Eric-Arellano Mar 14, 2019
1b00a22
Improve wording
Eric-Arellano Mar 14, 2019
843803d
Use configparser to dramatically simplify ci.py
Eric-Arellano Mar 14, 2019
f9409ca
Override ConfigParser's default of space around delimiter :
Eric-Arellano Mar 14, 2019
afa23d6
Refactor ./pants
Eric-Arellano Mar 14, 2019
f271934
Use sed line from Danny
Eric-Arellano Mar 14, 2019
124f5b3
Remove bad pants.ini line from debugging
Eric-Arellano Mar 14, 2019
f09a007
Apply suggestions from Danny's code review
cosmicexplorer Mar 14, 2019
575f1b3
Merge branch 'gh-pages' of github.com:pantsbuild/setup into py3
Eric-Arellano Mar 14, 2019
582fabd
Additional bash improvements from Danny
Eric-Arellano Mar 14, 2019
2931c7e
Merge branch 'py3' of github.com:Eric-Arellano/setup into py3
Eric-Arellano Mar 14, 2019
b11c63d
Merge branch 'gh-pages' of github.com:pantsbuild/setup into py3
Eric-Arellano Mar 14, 2019
ec4e5ae
Clarify in high level flow the env var name $PYTHON
Eric-Arellano Mar 14, 2019
f388027
Fix undefined name from bad merge
Eric-Arellano Mar 14, 2019
9a75f9f
Remove bad \ from .travis.yml script entry
Eric-Arellano Mar 15, 2019
03b13d3
Restore pants.ini if command fails and --python-version changed
Eric-Arellano Mar 15, 2019
f231cfd
Remove unnecessary except clause
Eric-Arellano Mar 15, 2019
4731d58
Extract temporarily_rewrite_config() context manager
Eric-Arellano Mar 15, 2019
0c128f8
Use get_exe_path_or_die per Danny's suggestion
Eric-Arellano Mar 15, 2019
fbbde0b
Revert bad setting of '-x'
Eric-Arellano Mar 15, 2019
0b9ee0e
Rename local variables to reflect new function name
Eric-Arellano Mar 15, 2019
1b5a6bc
Remove stray white line
Eric-Arellano Mar 15, 2019
cac7e02
Fix unnamed value in ci.py
Eric-Arellano Mar 15, 2019
be6f731
Upgrade to 1.15.0.dev4!
Eric-Arellano Mar 16, 2019
90e5c7d
Upgrade virtualenv to 16.4.3
Eric-Arellano Mar 16, 2019
fa1f224
See if python -m ensurepip works
Eric-Arellano Mar 16, 2019
741a238
Revert "See if python -m ensurepip works"
Eric-Arellano Mar 16, 2019
e362d8e
Squashed commit of the following:
Eric-Arellano Mar 16, 2019
b9d4b25
Squashed commit of the following:
Eric-Arellano Mar 16, 2019
d79a553
Merge branch 'gh-pages' of github.com:pantsbuild/setup into py3
Eric-Arellano Mar 16, 2019
9a1800e
Merge branch 'gh-pages' of github.com:pantsbuild/setup into py3
Eric-Arellano Mar 16, 2019
215ea00
Fix bad merge leaving off --python-version arg
Eric-Arellano Mar 16, 2019
4322fa2
Remove bad __pycache__ from wip that made it into here
Eric-Arellano Mar 16, 2019
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
14 changes: 12 additions & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,18 @@ script:
# Linux Precise and Trusty cannot install Python 3.7 due to an outdated OpenSSL,
# so we set this variable to allow skipping Python 3.7 on these shards.
- python37_present="$(if which python3.7 >/dev/null; then echo 'true'; else echo 'false'; fi)"
- ./ci.py --pants-version unspecified
- ./ci.py --pants-version config
- ./ci.py --pants-version unspecified --python-version unspecified
# TODO: these entries will not work until 1.15.0 is cut, as ./pants uses 1.14.0 currently
# when pants_version is unspecified. Add this test back once 1.15.0 is released.
# - ./ci.py --pants-version unspecified --python-version 2.7
# - ./ci.py --pants-version unspecified --python-version 3.6
# - if [[ "${python37_present}" = 'true' ]]; then
# ./ci.py --pants-version unspecified --python-version 3.7; fi
- ./ci.py --pants-version config --python-version unspecified
- ./ci.py --pants-version config --python-version 2.7
- ./ci.py --pants-version config --python-version 3.6
- if [[ "${python37_present}" = 'true' ]]; then
./ci.py --pants-version config --python-version 3.7; fi

osx_setup: &osx_setup
os: osx
Expand Down
62 changes: 49 additions & 13 deletions ci.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,19 @@ def __str__(self):
return self.value


class PythonVersion(Enum):

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Love enums!!

unspecified = "unspecified"
py27 = "2.7"
py36 = "3.6"
py37 = "3.7"

def __str__(self):
return self.value


def main() -> None:
args = create_parser().parse_args()
run_tests(test_pants_version=args.pants_version)
run_tests(test_pants_version=args.pants_version, test_python_version=args.python_version)


def create_parser() -> argparse.ArgumentParser:
Expand All @@ -37,41 +47,57 @@ def create_parser() -> argparse.ArgumentParser:
required=True,
help="Pants version to configure ./pants to use."
)
parser.add_argument(
"--python-version",
action="store",
type=PythonVersion,
choices=list(PythonVersion),
required=True,
help="Python version to configure ./pants to use."
)
return parser


def run_tests(*, test_pants_version: PantsVersion) -> None:
def run_tests(*, test_pants_version: PantsVersion, test_python_version: PythonVersion) -> None:
version_command = ["./pants", "--version"]
list_command = ["./pants", "list", "::"]
env_with_pantsd = {**os.environ, "PANTS_ENABLE_PANTSD": "True"}
with setup_pants_version(test_pants_version):
subprocess.run(version_command, check=True)
subprocess.run(list_command, check=True)
subprocess.run(version_command, env=env_with_pantsd, check=True)
subprocess.run(list_command, env=env_with_pantsd, check=True)
with setup_python_version(test_python_version):
subprocess.run(version_command, check=True)
subprocess.run(list_command, check=True)
subprocess.run(version_command, env=env_with_pantsd, check=True)
subprocess.run(list_command, env=env_with_pantsd, check=True)


@contextmanager
def setup_pants_version(test_pants_version: PantsVersion):
"""Modify pants.ini to allow the pants version to be unspecified or keep what was originally there."""
original_config = read_config()
updated_config = read_config()
config_entry = "pants_version"
if test_pants_version == PantsVersion.unspecified:
updated_config.remove_option(GLOBAL_SECTION, config_entry)
# NB: We also remove plugins as they refer to the pants_version.
updated_config.remove_option(GLOBAL_SECTION, "plugins")
write_config(updated_config)
elif test_pants_version == PantsVersion.config:
if config_entry not in original_config[GLOBAL_SECTION]:
raise ValueError("You requested to use the pants_version from pants.ini for this test, but pants.ini "
"does not include a pants_version!")
try:
with temporarily_rewrite_config(updated_config):
yield


@contextmanager
def setup_python_version(test_python_version: PythonVersion):
Eric-Arellano marked this conversation as resolved.
Show resolved Hide resolved
"""Modify pants.ini to allow the Python version to be unspecified or change to what was requested."""
updated_config = read_config()
config_entry = "pants_runtime_python_version"
if test_python_version == PythonVersion.unspecified:
updated_config.remove_option(GLOBAL_SECTION, config_entry)
else:
updated_config[GLOBAL_SECTION][config_entry] = test_python_version.value
with temporarily_rewrite_config(updated_config):
yield
except subprocess.CalledProcessError:
raise
finally:
write_config(original_config)


def read_config() -> configparser.ConfigParser:
Expand All @@ -85,5 +111,15 @@ def write_config(config: configparser.ConfigParser) -> None:
config.write(f)


@contextmanager
def temporarily_rewrite_config(updated_config: configparser.ConfigParser) -> None:
original_config = read_config()
write_config(updated_config)
try:
yield
finally:
write_config(original_config)


if __name__ == "__main__":
main()
36 changes: 30 additions & 6 deletions pants
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@

set -eou pipefail

PYTHON_BIN_NAME="${PYTHON:-python2.7}"
PYTHON_BIN_NAME="${PYTHON:-unspecified}"

PANTS_HOME="${PANTS_HOME:-${XDG_CACHE_HOME:-$HOME/.cache}/pants/setup}"
PANTS_BOOTSTRAP="${PANTS_HOME}/bootstrap-$(uname -s)-$(uname -m)"
Expand Down Expand Up @@ -62,8 +62,25 @@ function get_pants_ini_config_value {
sed -ne "/${prefix}/ s#${prefix}##p" pants.ini
}

function get_python_major_minor_version {
python_exe="$1"
"$python_exe" <<EOF
import sys
major_minor_version = ''.join(str(version_num) for version_num in sys.version_info[0:2])
print(major_minor_version)
EOF
}

function check_interpreter_on_path {
python="$1"
if ! which "${python}" >/dev/null; then
die "Could not find ${python}. Please ensure ${python} is on your PATH."
fi
}

# The high-level flow:
# 1.) Resolve the Python interpreter, defaulting to `python2.7`.
# 1.) Resolve the Python interpreter, first reading from the env var $PYTHON,
# then reading from pants.ini, then defaulting to `python2.7`.
# 2.) Grab pants version from pants.ini or default to latest.
# 3.) Check for a venv via a naming/path convention and execute if found.
# 4.) Otherwise create venv and re-exec self.
Expand All @@ -72,11 +89,17 @@ function get_pants_ini_config_value {
# are installed and up to date.

function determine_pants_runtime_python_version {
if which "${PYTHON_BIN_NAME}" >/dev/null; then
which "${PYTHON_BIN_NAME}"
if [[ "${PYTHON_BIN_NAME}" != 'unspecified' ]]; then
python_bin_name="${PYTHON_BIN_NAME}"
else
die "Could not find ${PYTHON_BIN_NAME}. Please ensure ${PYTHON_BIN_NAME} is on your PATH."
interpreter_version="$(get_pants_ini_config_value 'pants_runtime_python_version')"
if [[ -z "${interpreter_version}" ]]; then
interpreter_version='2.7'
fi
python_bin_name="python${interpreter_version}"
fi
check_interpreter_on_path "${python_bin_name}"
which "${python_bin_name}"
Eric-Arellano marked this conversation as resolved.
Show resolved Hide resolved
}

# TODO(John Sirois): GC race loser tmp dirs leftover from bootstrap_XXX
Expand Down Expand Up @@ -107,7 +130,8 @@ function bootstrap_pants {
pants_version="unspecified"
fi

target_folder_name="${pants_version}"
python_major_minor_version="$(get_python_major_minor_version "${python}")"
target_folder_name="${pants_version}_py${python_major_minor_version}"

if [[ ! -d "${PANTS_BOOTSTRAP}/${target_folder_name}" ]]; then
(
Expand Down