0

I have a setup.py file, which looks like this:

#!/usr/bin/env python

DIR = Path(__file__).parent
README = (DIR / "README.md").read_text()
install_reqs = parse_requirements(DIR / "requirements.txt")
try:
    dev_reqs = parse_requirements(DIR / "requirements-dev.txt")
except FileNotFoundError:
    dev_reqs = {}
    print("INFO: Could not find dev and/or prepro requirements txt file.")


if __name__ == "__main__":
    setup(
        keywords=[
            "demo"
        ],
        python_requires=">=3.7",
        install_requires=install_reqs,
        extras_require={"dev": dev_reqs},
        entry_points={
            "console_scripts": ["main=smamesdemo.run.main:main"]
        },
    )

I want find this file recursively in a folder and extract the following part:

entry_points={
    "console_scripts": ["main=smamesdemo.run.main:main"]
},

which may also look like this

    entry_points={"console_scripts": ["main=smamesdemo.run.main:main"]}

DESIRED: I want to check if that entrypoints dictionary contains a console_scripts part which a script that starts with the name main and return a True or False (or throw an error).

I get find the file and the needed values like this:

grep -rnw "./" -e "entry_points"

This returns the following:

./setup.py:22:        entry_points={

Does anyone know how to solve this?

wjandrea
  • 28,235
  • 9
  • 60
  • 81
Data Mastery
  • 1,555
  • 4
  • 18
  • 60
  • 1
    Why do you want to use Bash specifically? Python has the [`ast` module](https://docs.python.org/3/library/ast.html), which would actually parse the script and let you find the syntax node you're looking for. – wjandrea Sep 15 '22 at 18:04
  • @wjandrea: I thought this may be the easiest solution, Python would also be fine, probably even better. – Data Mastery Sep 15 '22 at 18:05
  • 1
    Check out [this question](/questions/30037326/how-to-textually-find-an-imported-name-in-a-module) for an idea of how to get started. – wjandrea Sep 15 '22 at 18:14
  • @wjandrea: Thank you, this looks promising – Data Mastery Sep 15 '22 at 18:24

1 Answers1

0

Assuming that there's only one entry_points={...} block in the setup.py file with the same syntax you have stated in your example.

  • The following script will find the setup.py file in the current directory by providing the requested outputs.
#!/bin/bash
directory_to_find='.' # Directory path to find the "${py_script}" file.
py_script="setup.py"  # Python script file name.
dictionary_to_match="console_scripts" # Dictionary to match
dictionary_script_to_match="main"      # Script name to match - If the Dictionary found.
########################################################
py_script=$(find "${directory_to_find}" -name "${py_script}" -type f)

found_entrypoint=$(
  while IFS='' read -r line ;do
    echo -n $line 
  done <<< $(fgrep -A1 'entry_points={' ${py_script})
  echo -n '}' && echo ""
)

found_entrypoint_dictionary=$(echo ${found_entrypoint} | awk -F'{' '{print $2}' | awk -F':' '{print $1}')
found_entrypoint_dictionary=$(echo ${found_entrypoint_dictionary//\"/})

found_dictionary_script=$(echo ${found_entrypoint} | awk -F'[' '{print $2}' | awk -F'=' '{print $1}')
found_dictionary_script=$(echo ${found_dictionary_script//\"/})

if ! [[ "${found_entrypoint}" =~ ^entry_points\=\{.* ]] ;then
  echo "entry_points not found."
  exit 1
fi

if [ "${found_entrypoint_dictionary}" == "${dictionary_to_match}" ] && [ "${found_dictionary_script}" == "${dictionary_script_to_match}" ] ;then
  echo "${found_entrypoint}"
  echo "True"
elif [ "${found_entrypoint_dictionary}" != "${dictionary_to_match}" ] || [ "${found_dictionary_script}" != "${dictionary_script_to_match}" ] ;then
  echo "${found_entrypoint}"
  echo "False"
else
  echo "Somthing went wrong!"
  exit 2
fi

exit 0
Vab
  • 412
  • 1
  • 11