Skip to content

Commit

Permalink
Add pipdeptree to In-Dockerfile Dependency Generation (#44)
Browse files Browse the repository at this point in the history
  • Loading branch information
ric-evans authored Oct 13, 2023
1 parent 533e85e commit 8a117ba
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 24 deletions.
14 changes: 10 additions & 4 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,8 @@ runs:
- name: Build dependencies.log (and commit)
if: env.IS_GIT_BEHIND != 'true'
run: |
set -x # turn on debugging
# append permissive line to .gitignore since *.log is commonly present
line='!dependencies*.log'
if [[ ! $(grep -F "$line" .gitignore) ]]; then
Expand All @@ -133,12 +135,16 @@ runs:
# Build
if [ -f ./Dockerfile ]; then
# from docker...
$GITHUB_ACTION_PATH/install-podman.sh
podman --version
# install podman... (grep -o -> 1 if found)
if [[ $(grep -o "USER" ./Dockerfile) ]]; then
$GITHUB_ACTION_PATH/install-podman.sh
podman --version
USE_PODMAN='--podman'
fi
# from dockerfile...
for f in ./Dockerfile*; do
echo $f
${{ github.action_path }}/build-dependencies-dockerfile-logs.sh $f
${{ github.action_path }}/build-dependencies-dockerfile-logs.sh $f $USE_PODMAN
done
else
# from setup.cfg...
Expand Down
79 changes: 59 additions & 20 deletions build-dependencies-dockerfile-logs.sh
Original file line number Diff line number Diff line change
Expand Up @@ -17,37 +17,76 @@ if [ ! -f "$1" ]; then
exit 2
fi

DOCKER_DEPS="dependencies-from-$(basename $1).log"

# use podman to get around user permission issues (with --userns=keep-id:uid=1000,gid=1000)
podman build -t my_image --file $1 .
# build
if [ "$2" == "--podman" ]; then
# use podman to get around user permission issues (with --userns=keep-id:uid=1000,gid=1000)
podman build -t my_image --file $1 .
else
docker build -t my_image --file $1 .
fi


PIP_FREEZE='pip_freeze.txt'
PIP_DEP_TREE='pip_dep_tree.txt'


# make script
TEMPDIR="dep-build-$(basename $1)"
mkdir ./$TEMPDIR
echo "#!/bin/bash" >> ./$TEMPDIR/freezer.sh
echo "pip3 freeze > /local/$TEMPDIR/$DOCKER_DEPS" >> ./$TEMPDIR/freezer.sh
chmod +x ./$TEMPDIR/freezer.sh
echo "#!/bin/bash" >> ./$TEMPDIR/make_dep_files.sh
# PIP_FREEZE
echo "pip3 freeze > /local/$TEMPDIR/$PIP_FREEZE" >> ./$TEMPDIR/make_dep_files.sh
# PIP_DEP_TREE
echo "pip3 install --target=. pipdeptree" >> ./$TEMPDIR/make_dep_files.sh
echo "./bin/pipdeptree > /local/$TEMPDIR/$PIP_DEP_TREE" >> ./$TEMPDIR/make_dep_files.sh
chmod +x ./$TEMPDIR/make_dep_files.sh


# generate
podman run --rm -i \
--mount type=bind,source=$(realpath ./$TEMPDIR/),target=/local/$TEMPDIR \
--userns=keep-id:uid=1000,gid=1000 \
my_image \
/local/$TEMPDIR/freezer.sh
if [ "$2" == "--podman" ]; then
podman run --rm -i \
--mount type=bind,source=$(realpath ./$TEMPDIR/),target=/local/$TEMPDIR \
--userns=keep-id:uid=1000,gid=1000 \
my_image \
/local/$TEMPDIR/make_dep_files.sh
else
docker run --rm -i \
--mount type=bind,source=$(realpath ./$TEMPDIR/),target=/local/$TEMPDIR \
my_image \
/local/$TEMPDIR/make_dep_files.sh
fi


# grab deps file
# grab dep files
# - remove main package since this can cause an infinite loop when a new release is made
if [ ! -z "$PACKAGE_NAME" ]; then
sed -i "/^$PACKAGE_NAME==/d" ./$TEMPDIR/$DOCKER_DEPS
sed -i "/^$PACKAGE_NAME /d" ./$TEMPDIR/$DOCKER_DEPS
sed -i "/#egg=$PACKAGE_NAME$/d" ./$TEMPDIR/$DOCKER_DEPS
# PIP_FREEZE
sed -i "/^$PACKAGE_NAME==/d" ./$TEMPDIR/$PIP_FREEZE
sed -i "/^$PACKAGE_NAME /d" ./$TEMPDIR/$PIP_FREEZE
sed -i "/#egg=$PACKAGE_NAME$/d" ./$TEMPDIR/$PIP_FREEZE
# now if using pip's editable-install (-e), pip converts dashes to underscores
package_name_dashes_to_underscores=$(echo "$PACKAGE_NAME" | sed -r 's/-/_/g')
sed -i "/#egg=$package_name_dashes_to_underscores$/d" ./$TEMPDIR/$DOCKER_DEPS
sed -i "/^#/d" ./$TEMPDIR/$DOCKER_DEPS # remove all commented lines # see comments in https://github.com/pypa/pip/issues/6199
sed -i "/#egg=$package_name_dashes_to_underscores$/d" ./$TEMPDIR/$PIP_FREEZE
sed -i "/^#/d" ./$TEMPDIR/$PIP_FREEZE # remove all commented lines # see comments in https://github.com/pypa/pip/issues/6199

# PIP_DEP_TREE
sed -i "s/^$PACKAGE_NAME==.*/$PACKAGE_NAME/g" ./$TEMPDIR/$PIP_DEP_TREE
fi
cat ./$TEMPDIR/$DOCKER_DEPS
# - rename & remove temp dir
mv ./$TEMPDIR/$DOCKER_DEPS $DOCKER_DEPS


# combine & cleanup
DOCKER_DEPS="dependencies-from-$(basename $1).log"
echo "#" >> $DOCKER_DEPS
echo "# This file was autogenerated within the container built from $1" >> $DOCKER_DEPS
echo "#" >> $DOCKER_DEPS
echo "########################################################################" >> $DOCKER_DEPS
echo "# pip freeze" >> $DOCKER_DEPS
echo "########################################################################" >> $DOCKER_DEPS
cat ./$TEMPDIR/$PIP_FREEZE >> $DOCKER_DEPS
echo "########################################################################" >> $DOCKER_DEPS
echo "# pipdeptree" >> $DOCKER_DEPS
echo "########################################################################" >> $DOCKER_DEPS
cat ./$TEMPDIR/$PIP_DEP_TREE >> $DOCKER_DEPS
cat $DOCKER_DEPS
rm -r ./$TEMPDIR/

0 comments on commit 8a117ba

Please sign in to comment.