首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >使用bash提取python文件的代码并从中提取值。

使用bash提取python文件的代码并从中提取值。
EN

Stack Overflow用户
提问于 2022-09-15 17:53:35
回答 1查看 54关注 0票数 0

我有一个setup.py文件,如下所示:

代码语言:javascript
运行
复制
#!/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"]
        },
    )

我希望在文件夹中递归地找到这个文件,并解压以下部分:

代码语言:javascript
运行
复制
entry_points={
    "console_scripts": ["main=smamesdemo.run.main:main"]
},

它也可能像这样

代码语言:javascript
运行
复制
    entry_points={"console_scripts": ["main=smamesdemo.run.main:main"]}

所需的:我想检查该入口点字典是否包含一个以main名称开头并返回True或False (或抛出错误)的脚本的console_scripts部分。

我会找到文件和所需的值,如下所示:

代码语言:javascript
运行
复制
grep -rnw "./" -e "entry_points"

这将返回以下内容:

代码语言:javascript
运行
复制
./setup.py:22:        entry_points={

有人知道怎么解决这个问题吗?

EN

回答 1

Stack Overflow用户

发布于 2022-09-17 21:08:29

假设setup.py文件中只有一个entry_points={...}块,其语法与示例中所述的语法相同。

  • 下面的脚本将通过提供请求的输出在当前目录中找到setup.py文件.

代码语言:javascript
运行
复制
#!/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
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/73735514

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档