python.py (snapcraft-4.5.1) | : | python.py (snapcraft-4.5.2) | ||
---|---|---|---|---|
skipping to change at line 118 | skipping to change at line 118 | |||
return { | return { | |||
# Add PATH to the python interpreter we always intend to use with | # Add PATH to the python interpreter we always intend to use with | |||
# this plugin. It can be user overridden, but that is an explicit | # this plugin. It can be user overridden, but that is an explicit | |||
# choice made by a user. | # choice made by a user. | |||
"PATH": "${SNAPCRAFT_PART_INSTALL}/bin:${PATH}", | "PATH": "${SNAPCRAFT_PART_INSTALL}/bin:${PATH}", | |||
"SNAPCRAFT_PYTHON_INTERPRETER": "python3", | "SNAPCRAFT_PYTHON_INTERPRETER": "python3", | |||
"SNAPCRAFT_PYTHON_VENV_ARGS": "", | "SNAPCRAFT_PYTHON_VENV_ARGS": "", | |||
} | } | |||
def get_build_commands(self) -> List[str]: | def get_build_commands(self) -> List[str]: | |||
# $SNAPCRAFT_PYTHON_INTERPRETER (as -m venv will create the link to the | ||||
# interpreter invoked). | ||||
build_commands = [ | build_commands = [ | |||
'"${SNAPCRAFT_PYTHON_INTERPRETER}" -m venv ${SNAPCRAFT_PYTHON_VENV_A | # Determine python based on PATH. | |||
RGS} "${SNAPCRAFT_PART_INSTALL}"' | 'SNAPCRAFT_PYTHON_PATH="${SNAPCRAFT_PYTHON_PATH:-$(which "${SNAPCRAF | |||
T_PYTHON_INTERPRETER}")}"', | ||||
# Resolve python, in case it is a link, e.g.: | ||||
# (1) /home/ubuntu/.venv/snapcraft/bin/python3 -> /usr/bin/python3.8 | ||||
# (2) /usr/bin/python3 -> /usr/bin/python3.8 | ||||
# (3) /root/stage/python3 -> /root/stage/python3.8 | ||||
# (4) /root/parts/<part>/install/usr/bin/python3 -> /root/parts/<par | ||||
t>/install/usr/bin/python3.8 | ||||
'SNAPCRAFT_PYTHON_PATH="$(readlink -e "${SNAPCRAFT_PYTHON_PATH}")"', | ||||
'"${SNAPCRAFT_PYTHON_PATH}" -m venv ${SNAPCRAFT_PYTHON_VENV_ARGS} "$ | ||||
{SNAPCRAFT_PART_INSTALL}"', | ||||
'SNAPCRAFT_PYTHON_VENV_INTERP_PATH="${SNAPCRAFT_PART_INSTALL}/bin/${ | ||||
SNAPCRAFT_PYTHON_INTERPRETER}"', | ||||
] | ] | |||
if self.options.constraints: | if self.options.constraints: | |||
constraints = " ".join(f"-c {c!r}" for c in self.options.constraints ) | constraints = " ".join(f"-c {c!r}" for c in self.options.constraints ) | |||
else: | else: | |||
constraints = "" | constraints = "" | |||
if self.options.python_packages: | if self.options.python_packages: | |||
python_packages = " ".join( | python_packages = " ".join( | |||
[shlex.quote(pkg) for pkg in self.options.python_packages] | [shlex.quote(pkg) for pkg in self.options.python_packages] | |||
skipping to change at line 149 | skipping to change at line 160 | |||
build_commands.append(f"[ -f setup.py ] && pip install {constraints} -U .") | build_commands.append(f"[ -f setup.py ] && pip install {constraints} -U .") | |||
# Now fix shebangs. | # Now fix shebangs. | |||
# TODO: replace with snapcraftctl once the two scripts are consolidated | # TODO: replace with snapcraftctl once the two scripts are consolidated | |||
# and use mangling.rewrite_python_shebangs. | # and use mangling.rewrite_python_shebangs. | |||
build_commands.append( | build_commands.append( | |||
dedent( | dedent( | |||
"""\ | """\ | |||
for e in $(find "${SNAPCRAFT_PART_INSTALL}" -type f -executable) | for e in $(find "${SNAPCRAFT_PART_INSTALL}" -type f -executable) | |||
do | do | |||
if head -1 "${e}" | grep -q "python" ; then | sed -i "1 s|^#\\!${SNAPCRAFT_PYTHON_VENV_INTERP_PATH}.*$|#\\!/us | |||
sed \\ | r/bin/env ${SNAPCRAFT_PYTHON_INTERPRETER}|" "${e}" | |||
-r '1 s|#\\!.*python3?$|#\\!/usr/bin/env '${SNAPCRAFT_PY | ||||
THON_INTERPRETER}'|' \\ | ||||
-i "${e}" | ||||
fi | ||||
done | done | |||
""" | """ | |||
) | ) | |||
) | ) | |||
# Lastly, fix the symlink to the "real" python3 interpreter. | # Lastly, fix the symlink to the "real" python3 interpreter. | |||
# TODO: replace with snapcraftctl (create_relative_symlinks). | # TODO: replace with snapcraftctl (create_relative_symlinks). | |||
build_commands.append( | build_commands.append( | |||
dedent( | dedent( | |||
"""\ | """\ | |||
interp_path="${SNAPCRAFT_PART_INSTALL}/bin/${SNAPCRAFT_PYTHON_INTERP | determine_link_target() { | |||
RETER}" | opts_state="$(set +o +x)" | |||
if [ -f "${interp_path}" ]; then | interp_dir="$(dirname "${SNAPCRAFT_PYTHON_VENV_INTERP_PATH}")" | |||
current_link=$(readlink "${interp_path}") | target="${SNAPCRAFT_PYTHON_PATH}" | |||
# Change link if in $SNAPCRAFT_PART_INSTALL | for dir in "${SNAPCRAFT_PART_INSTALL}" "${SNAPCRAFT_STAGE}"; do | |||
if echo "${current_link}" | grep -q "${SNAPCRAFT_PART_INSTALL}" | if echo "${SNAPCRAFT_PYTHON_PATH}" | grep -q "${dir}"; then | |||
; then | target="$(realpath --strip --relative-to="${interp_dir}" | |||
new_link=$(realpath \\ | \\ | |||
--strip \\ | "${SNAPCRAFT_PYTHON_PATH}")" | |||
--relative-to="${SNAPCRAFT_PART_INSTALL}/bin/" \\ | break | |||
"${current_link}") | fi | |||
rm "${interp_path}" | done | |||
ln -s "${new_link}" "${interp_path}" | echo "${target}" | |||
fi | eval "${opts_state}" | |||
fi | } | |||
target="$(determine_link_target)" | ||||
ln -sf "${target}" "${SNAPCRAFT_PYTHON_VENV_INTERP_PATH}" | ||||
""" | """ | |||
) | ) | |||
) | ) | |||
return build_commands | return build_commands | |||
End of changes. 4 change blocks. | ||||
23 lines changed or deleted | 37 lines changed or added |