Marcel Telka
2024-04-06 7a1e91471d4b540f48a912dfcca9f65211aa599c
tools/python-integrate-project
@@ -69,13 +69,6 @@
[[ -d "$BASE_DIR" ]] || usage "Directory $BASE_DIR not found"
# Get data from pypi
PYPI_PROJECT=$($CURL "$APIURL/$PROJECT/json")
if (($? != 0)) || [[ -z "$PYPI_PROJECT" ]] ; then
   printf "FATAL: Failed to get data from pypi\n" >&2
   exit 1
fi
# Distribution match project
DISTRIBUTION="$PROJECT"
@@ -83,9 +76,9 @@
{
   typeset ENTRY="$1"
   [[ -f "$SOURCE_DIR/PKG-INFO" ]] || return
   [[ -f "$SOURCE_DIR$COMPONENT_SUBDIR/PKG-INFO" ]] || return
   cat "$SOURCE_DIR/PKG-INFO" \
   cat "$SOURCE_DIR$COMPONENT_SUBDIR/PKG-INFO" \
      | sed -e '/^$/,$d' \
      | awk 'END{printf("\n")}/^[^:]+: /{$0="\n"$0}1' ORS=' ' \
      | grep "^$ENTRY: " \
@@ -100,21 +93,35 @@
cd "$DIR"
git restore --staged . > /dev/null 2>&1
git checkout . > /dev/null 2>&1
touch "$CONF"
# Following variables could be set by the hook-begin snippet
VERSION=
HOMEPAGE=
DOWNLOAD_URL=
LICENSE_FILE=
SUMMARY=
# Execute hook-begin snippet
gsed -e '0,/^%hook-begin%/d' -e '/^%/,$d' < "$CONF" > "$SNIPPET"
. "./$SNIPPET"
rm -f "$SNIPPET"
if [[ -f "$CONF" ]] ; then
   gsed -e '0,/^%hook-begin%/d' -e '/^%/,$d' < "$CONF" > "$SNIPPET"
   . "./$SNIPPET"
   rm -f "$SNIPPET"
fi
# Version specified as option takes precedence
[[ -n "$OPT_VERSION" ]] && VERSION="$OPT_VERSION"
# Get data from PyPI if needed
if [[ -z "$VERSION" || -z "$HOMEPAGE" || -z "$SUMMARY" ]] ; then
   PYPI_PROJECT=$($CURL "$APIURL/$PROJECT/json")
   if (($? != 0)) || [[ -z "$PYPI_PROJECT" ]] ; then
      printf 'WARNING: Failed to get data for project %s from PyPI\n' "$PROJECT" >&2
      PYPI_PROJECT=
   fi
fi
# Find the latest version if not already provided
if [[ -z "$VERSION" ]] ; then
@@ -128,13 +135,11 @@
# Is this new project, or just a rebuild?
NEW=1
REBUILD=0
PREV_VER=
PREV_HVER=
PREV_REV=0
if git ls-files --error-unmatch Makefile > /dev/null 2>&1 ; then
   NEW=0
   REBUILD=1
   PREV_VER=$($GMAKE print-value-COMPONENT_VERSION 2>/dev/null)
   (($? != 0)) && printf "FATAL: 'gmake print-value-COMPONENT_VERSION' failed!\n" >&2 && exit 1
   PREV_REV=$($GMAKE print-value-COMPONENT_REVISION 2>/dev/null)
@@ -186,34 +191,44 @@
fi
# Remove everything from git (except known patches, history, and $CONF)
grep "^%patch%" "$CONF" | while read TAG PATCH ; do rm -f "patches/$PATCH" ; done
# Remove everything that is not in git
rm -rf *
git checkout . > /dev/null 2>&1
# Remove everything from git (except known patches, files, history, and $CONF)
[[ -f "$CONF" ]] && grep "^%patch%" "$CONF" | while read TAG PATCH ; do rm -f "patches/$PATCH" ; done
[[ -f "$CONF" ]] && grep "^%file%" "$CONF" | while read TAG FILE ; do rm -f "files/$FILE" ; done
rm -f history "$CONF"
find . -type f | while read f ; do git rm "$f" > /dev/null 2>&1 ; done
rm -rf "$DIR" 2>/dev/null
git checkout history > /dev/null 2>&1
git checkout "$CONF" > /dev/null 2>&1
touch "$CONF"
grep "^%patch%" "$CONF" | while read TAG PATCH ; do
[[ -f "$CONF" ]] && grep "^%patch%" "$CONF" | while read TAG PATCH ; do
   git checkout "patches/$PATCH" > /dev/null 2>&1
   [[ -f "patches/$PATCH" ]] || printf "WARNING: Patch %s not found\n" "$PATCH" >&2
done
[[ -f "$CONF" ]] && grep "^%file%" "$CONF" | while read TAG FILE ; do
   git checkout "files/$FILE" > /dev/null 2>&1
   [[ -f "files/$FILE" ]] || printf "WARNING: File %s not found\n" "$FILE" >&2
done
# Makefile template
GENERATE_CMD="\$WS_TOOLS/$THIS"
[[ "$DIRECTORY" != "python/$DISTRIBUTION" ]] && GENERATE_CMD="$GENERATE_CMD -d $DIRECTORY"
GENERATE_CMD="$GENERATE_CMD $PROJECT"
(
cat $WS_TOP/transforms/copyright-template | sed -e '/^$/,$d'
cat <<EOF
#
# This file was automatically generated using the following command:
#   \$WS_TOOLS/$THIS $PROJECT
#   $GENERATE_CMD
#
BUILD_STYLE = pyproject
USE_COMMON_TEST_MASTER = no
EOF
gsed -e '0,/^%include-1%/d' -e '/^%/,$d' < "$CONF"
[[ -f "$CONF" ]] && gsed -e '0,/^%include-1%/d' -e '/^%/,$d' < "$CONF"
cat <<EOF
include ../../../make-rules/shared-macros.mk
@@ -224,7 +239,7 @@
COMPONENT_SUMMARY =      $PROJECT - TODO
EOF
[[ -n "$HOMEPAGE" ]] && printf "COMPONENT_PROJECT_URL =\t\t%s\n" "$HOMEPAGE"
[[ -n "$DOWNLOAD_URL" ]] && printf 'COMPONENT_ARCHIVE_URL =\t\t\\\n\t%s\n' "$DOWNLOAD_URL"
[[ -n "$DOWNLOAD_URL" ]] && printf 'DOWNLOAD_URL =\t\t\\\n\t%s\n' "$DOWNLOAD_URL"
cat <<EOF
COMPONENT_ARCHIVE_HASH =   \\
   sha256:TODO
@@ -233,15 +248,34 @@
_TEST_STYLE = TODO
EOF
cat "$CONF" | gsed -e '0,/^%include-2%/d' -e '/^%/,$d' | gsed -e '1s/^./\n&/'
[[ -f "$CONF" ]] && cat "$CONF" | gsed -e '0,/^%include-2%/d' -e '/^%/,$d' | gsed -e '1s/^./\n&/'
printf "\ninclude \$(WS_MAKE_RULES)/common.mk\n"
cat "$CONF" | gsed -e '0,/^%include-3%/d' -e '/^%/,$d' | gsed -e '1s/^./\n&/'
[[ -f "$CONF" ]] && cat "$CONF" | gsed -e '0,/^%include-3%/d' -e '/^%/,$d' | gsed -e '1s/^./\n&/'
printf "\n"
) > Makefile
# If the automatically constructed COMPONENT_ARCHIVE_URL points to the same
# location as DOWNLOAD_URL then we should use it
if [[ -n "$DOWNLOAD_URL" ]] ; then
   COMPONENT_ARCHIVE_URL=$($GMAKE print-value-COMPONENT_ARCHIVE_URL)
   [[ "$COMPONENT_ARCHIVE_URL" == "$DOWNLOAD_URL" ]] && DOWNLOAD_URL=
fi
# The default COMPONENT_ARCHIVE_URL usually redirects to DOWNLOAD_URL so check
# that too
if [[ -n "$DOWNLOAD_URL" ]] ; then
   [[ $($CURL --head --write-out "%{redirect_url}\n" --output /dev/null \
       "$COMPONENT_ARCHIVE_URL") == "$DOWNLOAD_URL" ]] && DOWNLOAD_URL=
fi
# Use either DOWNLOAD_URL or default COMPONENT_ARCHIVE_URL
if [[ -n "$DOWNLOAD_URL" ]] ; then
   sed -i -e $'s/^DOWNLOAD_URL =/COMPONENT_ARCHIVE_URL =/' Makefile
else
   sed -i -e $'/^DOWNLOAD_URL =/,+1d' Makefile
fi
# Remove COMPONENT_REVISION if not needed
COMPONENT_VERSION=$($GMAKE print-value-COMPONENT_VERSION)
[[ "$PREV_VER" != "$COMPONENT_VERSION" ]] && REBUILD=0 && sed -i -e '/^COMPONENT_REVISION/d' Makefile
[[ "$PREV_VER" != "$COMPONENT_VERSION" ]] && sed -i -e '/^COMPONENT_REVISION/d' Makefile
git add Makefile
# Calculate sham256 sum for source package
@@ -253,21 +287,39 @@
sed -i -e 's/sha256:TODO/sha256:'"$SHA256"'/g' Makefile
git add Makefile
# Unpack sources
# Unpack sources and apply patches
! $GMAKE patch > /dev/null 2>&1 && printf "FATAL: 'gmake patch' failed!\n" >&2 && exit 1
# Refresh patches
if $GMAKE refresh-patches > /dev/null 2>&1 ; then
   git add patches 2>/dev/null
else
   printf "ERROR: 'gmake refresh-patches' failed!\n" >&2
   git checkout patches 2>/dev/null
fi
# Cleanup after patch refresh
$GMAKE clobber > /dev/null 2>&1
# Prepare sources
! $GMAKE prep > /dev/null 2>&1 && printf "FATAL: 'gmake prep' failed!\n" >&2 && exit 1
SOURCE_DIR=$($GMAKE print-value-SOURCE_DIR)
COMPONENT_SUBDIR=$($GMAKE print-value-COMPONENT_SUBDIR)
[[ -n "$COMPONENT_SUBDIR" ]] && COMPONENT_SUBDIR="/$COMPONENT_SUBDIR"
if [[ ! -f "$SOURCE_DIR/pyproject.toml" ]] ; then
   [[ ! -f "$SOURCE_DIR/setup.py" ]] && printf "FATAL: Neither pyproject.toml nor setup.py found!\n" >&2 && exit 1
if [[ ! -f "$SOURCE_DIR$COMPONENT_SUBDIR/pyproject.toml" ]] ; then
   [[ ! -f "$SOURCE_DIR$COMPONENT_SUBDIR/setup.py" ]] && printf "FATAL: Neither pyproject.toml nor setup.py found!\n" >&2 && exit 1
   sed -i -e 's/^\(BUILD_STYLE = \).*$/\1setup.py/' Makefile
fi
# Get summary
SUMMARY=$(printf "%s" "$PYPI_PROJECT" | /usr/bin/jq -r '.info.summary')
if (($? != 0)) || [[ -z "$SUMMARY" || "$SUMMARY" == "null" ]] ; then
   printf "WARNING: Failed to get summary for project %s from pypi\n" "$PROJECT" >&2
   SUMMARY=$(get_PKGINFO_entry "Summary")
   [[ -z "$SUMMARY" ]] && SUMMARY="TODO"
# Get summary if not already provided
if [[ -z "$SUMMARY" ]] ; then
   SUMMARY=$(printf "%s" "$PYPI_PROJECT" | /usr/bin/jq -r '.info.summary')
   if (($? != 0)) || [[ -z "$SUMMARY" || "$SUMMARY" == "null" ]] ; then
      printf "WARNING: Failed to get summary for project %s from pypi\n" "$PROJECT" >&2
      SUMMARY=$(get_PKGINFO_entry "Summary")
      [[ -z "$SUMMARY" ]] && SUMMARY="TODO"
   fi
fi
# Summary needs to be sanitized
SUMMARY="${SUMMARY//\`/\\\\\`}"
@@ -291,11 +343,11 @@
LICENSE=
LICFILE=
for f in $(get_PKGINFO_entry "License-File") LICENSE LICENSE.rst LICENSE.txt ; do
   [[ -f "$SOURCE_DIR/$f" ]] || continue
for f in $LICENSE_FILE $(get_PKGINFO_entry "License-File") LICENSE LICENSE.rst LICENSE.txt ; do
   [[ -f "$SOURCE_DIR$COMPONENT_SUBDIR/$f" ]] || continue
   LICFILE="$f"
   detect_license LICENSE "$SOURCE_DIR/$LICFILE"
   detect_license LICENSE "$SOURCE_DIR$COMPONENT_SUBDIR/$LICFILE"
   [[ -n "$LICENSE" ]] && break
   printf "WARNING: Failed to detect license type in %s file\n" "$f" >&2
@@ -308,9 +360,11 @@
if [[ -z "$LICENSE" ]] ; then
   # Execute hook-no-license snippet
   gsed -e '0,/^%hook-no-license%/d' -e '/^%/,$d' < "$CONF" > "$SNIPPET"
   . "./$SNIPPET"
   rm -f "$SNIPPET"
   if [[ -f "$CONF" ]] ; then
      gsed -e '0,/^%hook-no-license%/d' -e '/^%/,$d' < "$CONF" > "$SNIPPET"
      . "./$SNIPPET"
      rm -f "$SNIPPET"
   fi
   if [[ -f "$DISTRIBUTION.license" ]] ; then
      sed -i -e '/^COMPONENT_LICENSE_FILE/d' Makefile
@@ -326,13 +380,19 @@
# detect TEST_STYLE
TEST_STYLE=
cd "$SOURCE_DIR"
cd "$SOURCE_DIR$COMPONENT_SUBDIR"
while true ; do
   TOX_OUT=$(tox -l)
   TOX_RET=$?
   ((TOX_RET == 0)) && ! printf "%s" "$TOX_OUT" | grep -q 'assuming empty tox\.ini' && TEST_STYLE="tox" && break
   pytest -p no:checkdocs --setup-plan
   # Disable some pytest plugins that almost always collects tests to run
   # even there are no pytest tests available otherwise.
   #
   # The system-statistics plugin is disabled because it often causes the
   # pytest to fail.
   # See also https://github.com/saltstack/pytest-system-statistics/issues/4
   pytest -p no:black -p no:checkdocs -p no:cov -p no:mypy -p no:relaxed -p no:system-statistics --setup-plan
   (($? != 5)) && TEST_STYLE="pytest" && break
   [[ -f setup.py ]] && python setup.py test --help && TEST_STYLE="setup.py" && break
@@ -359,10 +419,10 @@
fi
# Warn if a testing tool is called directly by tox
if [[ "$($GMAKE print-value-TEST_STYLE)" == "tox" && -f "$SOURCE_DIR/tox.ini" ]] ; then
if [[ "$($GMAKE print-value-TEST_STYLE)" == "tox" && -f "$SOURCE_DIR$COMPONENT_SUBDIR/tox.ini" ]] ; then
   TOX_CALL_INDIRECTLY=$($GMAKE print-value-TOX_CALL_INDIRECTLY)
   for p in $TOX_CALL_INDIRECTLY ; do
      sed -n -e '/^commands *=/,/^$/p' "$SOURCE_DIR/tox.ini" \
      sed -n -e '/^commands *=/,/^$/p' "$SOURCE_DIR$COMPONENT_SUBDIR/tox.ini" \
         | tr '\t' ' ' \
         | grep -q '^\(commands *=\)\{0,1\} *'"$p"'\( \{1,\}.*\)\{0,1\}$' \
      && printf "WARNING: %s is called directly in tox.ini\n" "$p" >&2
@@ -381,17 +441,14 @@
      > "$MANIFEST"
   # Execute hook-manifest snippet
   gsed -e '0,/^%hook-manifest%/d' -e '/^%/,$d' < "$CONF" > "$SNIPPET"
   . "./$SNIPPET"
   rm -f "$SNIPPET"
   if [[ -f "$CONF" ]] ; then
      gsed -e '0,/^%hook-manifest%/d' -e '/^%/,$d' < "$CONF" > "$SNIPPET"
      . "./$SNIPPET"
      rm -f "$SNIPPET"
   fi
   git add manifests/sample-manifest.p5m $MANIFEST
fi
# $CONF is no longer needed
rm -f "$CONF"
git checkout "$CONF" > /dev/null 2>&1
# Generate REQUIRED_PACKAGES
@@ -406,6 +463,36 @@
# Make sure the build environment is setup properly and we do have all
# requirements installed.  Otherwise we cannot continue.
! $GMAKE env-check > /dev/null 2>&1 && printf "FATAL: 'gmake env-check' failed!\n" >&2 && exit 1
# Handle history
COMPONENT_FMRI=$($GMAKE print-value-COMPONENT_FMRI)
PYTHON_VERSIONS_OBSOLETING=$($GMAKE print-value-PYTHON_VERSIONS_OBSOLETING)
OV=
OV_PLURAL=
for o in $(echo $OBSOLETE $PYTHON_VERSIONS_OBSOLETING | LC_ALL=C sort -u) ; do
   PYV=${o//.}
   FMRI=$(pkg list -nvH "$COMPONENT_FMRI-$PYV" 2>/dev/null | egrep -v '(o|r)$' | sed -e 's|^.*\('"$COMPONENT_FMRI"'\)|\1|g' -e 's/:[^:]*$//g' -e 's/\(-[^-]*\)$/,5.11\1/g')
   [[ -n "$FMRI" ]] || continue
   FMRI_H=${FMRI%.*}
   FMRI_T=${FMRI##*.}
   if [[ "$FMRI_H" == "$FMRI" ]] ; then
      printf "WARNING: Wrong fmri format: %s\n" "$FMRI" >&2
      continue
   fi
   FMRI_T=$((FMRI_T + 1))
   printf "%s.%s noincorporate\n" "$FMRI_H" "$FMRI_T" >> history
   [[ -n "$OV" ]] && OV="${OV/ and /, } and " && OV_PLURAL="s"
   OV="$OV$o"
done
if [[ -f history ]] ; then
   LC_ALL=C sort -u history > history.new
   mv history.new history
   git add history
   awk '$NF == "noincorporate" {printf("WARNING: Unincorporated package: %s\n", $1)}' < history >&2
fi
# Cleanup before we try to publish to make sure there are no leftovers from
@@ -484,42 +571,13 @@
fi
# Handle history
COMPONENT_FMRI=$($GMAKE print-value-COMPONENT_FMRI)
PYTHON_VERSIONS_OBSOLETING=$($GMAKE print-value-PYTHON_VERSIONS_OBSOLETING)
OV=
OV_PLURAL=
for o in $(echo $OBSOLETE $PYTHON_VERSIONS_OBSOLETING | LC_ALL=C sort -u) ; do
   PYV=${o//.}
   FMRI=$(pkg list -nvH "$COMPONENT_FMRI-$PYV" 2>/dev/null | egrep -v '(o|r)$' | sed -e 's|^.*\('"$COMPONENT_FMRI"'\)|\1|g' -e 's/:[^:]*$//g' -e 's/\(-[^-]*\)$/,5.11\1/g')
   [[ -n "$FMRI" ]] || continue
   FMRI_H=${FMRI%.*}
   FMRI_T=${FMRI##*.}
   if [[ "$FMRI_H" == "$FMRI" ]] ; then
      printf "WARNING: Wrong fmri format: %s\n" "$FMRI" >&2
      continue
   fi
   FMRI_T=$((FMRI_T + 1))
   printf "%s.%s noincorporate\n" "$FMRI_H" "$FMRI_T" >> history
   [[ -n "$OV" ]] && OV="$OV and " && OV_PLURAL="s"
   OV="$OV$o"
done
if [[ -f history ]] ; then
   LC_ALL=C sort -u history > history.new
   mv history.new history
   git add history
fi
# Construct the commit message
MSG=
if ((NEW)) ; then
   MSG="Add $PROJECT python project"
   MSG="Add $PROJECT Python project"
else
   if ((REBUILD == 0)) ; then
      [[ "$PREV_HVER" != "$VERSION" ]] && MSG="update to $VERSION" || MSG="change version format"
   fi
   [[ "$PREV_VER" != "$COMPONENT_VERSION" ]] && MSG="change version format"
   [[ "$PREV_HVER" != "$VERSION" ]] && MSG="update to $VERSION"
   REBUILDMSG=
@@ -531,12 +589,12 @@
         [[ -n "$NV" ]] && NV="$NV and "
         NV="$NV$v"
      done
      [[ -n "$NV" ]] && REBUILDMSG="rebuild for python $NV"
      [[ -n "$NV" ]] && REBUILDMSG="rebuild for Python $NV"
   fi
   if [[ -n "$OV" ]] ; then
      [[ -n "$REBUILDMSG" ]] && REBUILDMSG="$REBUILDMSG and" || REBUILDMSG="rebuild"
      REBUILDMSG="$REBUILDMSG to get package$OV_PLURAL for python $OV obsoleted"
      [[ -n "$REBUILDMSG" ]] && REBUILDMSG="$REBUILDMSG and "
      REBUILDMSG="${REBUILDMSG}obsolete package$OV_PLURAL for Python $OV"
   fi
   if [[ -n "$REBUILDMSG" ]] ; then