setup_count=(echo"setup_file" | wc -l)
if [ "setupcount"−ne1];thenecho"❌Expectedexactly1setup.pyfile,butfoundsetup_count:"
echo "setupfile"echo"errors=true">>GITHUB_OUTPUT
exit 0
fi
echo "📦 Found setup.py at: $setup_file"
Install in editable mode - this will automatically install all dependencies
setup_dir=(dirname"setup_file")
cd "$setup_dir"
echo "📥 Installing package and its dependencies..."
pip install -e .
this is not working so just make a shell script that runs pip install on each of the dependencies (inside of installrequires) in setup.py
Assistant
Below is a modified shell script for your GitHub Actions workflow. This script replaces the original logic (which attempted to install the package in editable mode) with a new approach that extracts the dependencies from the install_requires list in setup.py and installs each one individually using pip install. The script uses Python's ast module to parse the setup.py file and extract the dependencies safely.
Here's the updated run section for your GitHub Actions step. I've kept the error-handling logic from your original script (e.g., checking for the presence and uniqueness of setup.py), and added robust dependency extraction and installation.
yaml
name: Install dependencies from setup.py
run: |
# Find setup.py file
setup_file=$(find . -name "setup.py" -type f)
if [ -z "$setup_file" ]; then
echo "❌ No setup.py file found"
echo "errors=true" >> $GITHUB_OUTPUT
exit 0
fi
# Check if there's exactly one setup.py file
setup_count=$(echo "$setup_file" | wc -l)
if [ "$setup_count" -ne 1 ]; then
echo "❌ Expected exactly 1 setup.py file, but found $setup_count:"
echo "$setup_file"
echo "errors=true" >> $GITHUB_OUTPUT
exit 0
fi
echo "📦 Found setup.py at: $setup_file"
# Get the directory and change to it
setup_dir=$(dirname "$setup_file")
cd "$setup_dir"
# Extract dependencies from install_requires using Python
dependencies_output=$(python -c "
import ast
import sys
try:
with open('setup.py', 'r') as f:
tree = ast.parse(f.read(), filename='setup.py')
for node in ast.walk(tree):
if isinstance(node, ast.Call) and isinstance(node.func, ast.Name) and node.func.id == 'setup':
for keyword in node.keywords:
if keyword.arg == 'install_requires':
if isinstance(keyword.value, ast.List):
for elt in keyword.value.elts:
if isinstance(elt, ast.Str):
print(elt.s)
else:
print('Error: Non-string element in install_requires', file=sys.stderr)
sys.exit(1)
else:
print('Error: install_requires is not a list', file=sys.stderr)
sys.exit(1)
except Exception as e:
print(f'Error parsing setup.py: {e}', file=sys.stderr)
sys.exit(1)
" 2>&1)
status=$?
if [ $status -ne 0 ]; then
echo "❌ Error extracting dependencies from setup.py:"
echo "$dependencies_output"
echo "errors=true" >> $GITHUB_OUTPUT
exit 0
fi
# dependencies_output now contains the dependencies, one per line, or is empty
dependencies="$dependencies_output"
# Install each dependency individually
if [ -n "$dependencies" ]; then
echo "📥 Installing dependencies..."
# Use mapfile to handle the list (available in bash 4+)
mapfile -t deps <<< "$dependencies"
for dep in "${deps[@]}"; do
echo "Installing $dep..."
if ! pip install "$dep"; then
echo "❌ Failed to install $dep"
echo "errors=true" >> $GITHUB_OUTPUT
exit 0
fi
done
else
echo "ℹ️ No dependencies found in install_requires."
fi
echo "✅ Dependencies installed successfully."
Key Changes and Explanations:
Dependency Extraction: The script uses a Python one-liner with the ast module to parse the setup.py file. It looks for the setup() call and extracts the install_requires list. If install_requires is not a list or contains non-string elements, it prints an error and exits with a non-zero status. This is captured in the shell script, and if there's an error, it sets errors=true in $GITHUB_OUTPUT.
Installation: Each dependency is installed individually using pip install. The script loops through the dependencies (stored in an array via mapfile) and attempts to install them one by one. If any installation fails, it sets errors=true and exits.
Error Handling:
If no setup.py is found or there are multiple, it sets errors=true and exits, just like your original script.
If dependency extraction fails (e.g., syntax error in setup.py or malformed install_requires), it sets errors=true and exits.
If a pip install command fails, it sets errors=true and exits immediately (on the first failure for simplicity).
No Installation in Editable Mode: As per your request, this script only installs the dependencies listed in install_requires, not the package itself.
Edge Cases:
If install_requires is empty or not present, the script will not attempt any installations and will output a success message.
The script assumes setup.py is a standard setuptools file. If your setup.py uses advanced features (e.g., dynamic install_requires), this might not work perfectly, but it should handle most common cases.
Shell Compatibility: This script is written for bash (which is the default in GitHub Actions runners). The mapfile command is used for handling the dependency list; if you need broader compatibility, you could replace it with a while read loop.
How to Use This in GitHub Actions:
Replace the run section in your workflow file with the above code.
Ensure that GITHUB_OUTPUT is available (it should be set automatically in GitHub Actions).
If you want the step to fail the workflow on error (instead of just setting the output), you can change exit 0 to exit 1 in the error cases, but your original script used exit 0 to allow the workflow to continue, so I kept that.
If you encounter any issues (e.g., with complex setup.py files), let me know for further refinements!