通过脚本检测python的项目依赖包及版本

脚本来自于网络,加上注解,具体见以下shell脚本

#!/bin/env/bash
# 当前目录按类型查找文件 | 过滤py文件 | py文件中 正则过滤 from or import打头的行 | 指定"py:"作为分隔字符,输出第二项 | 升序排序 | 去重 > 重定向输出到res0.log
find . -type f |grep py|xargs grep -E '^from|^import'| awk -F "py:" {'print $2'}|sort|uniq>res0.log

# 将文件中的from替换为import    s/pattern-to-find/replacement-pattern/g(global)
sed -i 's/from/import/g' res0.log

# 打开文件 | 指定"import "作为分隔符输出第二项 | 指定"."分隔符输出第一项 > 重定向输出到res1.log
cat res0.log |awk -F "import " {'print $2'}|awk -F '.' {'print $1'}> res1.log

# 删除空格
sed -i "s/,[ ]*/
/g" res1.log

# 删除空行
sed -i “s/s/
/g" res1.log

# 打开文件 | 排序 | 去重 |  while循环 pip list 过滤当前行的包,有的重定向到res2.log中
cat res1.log|sort|uniq |while read line
do
    echo `pip list|grep $line` >> res2.log
done

# 记录字段重定向到res3.log
awk NF res2.log > res3.log

# 替换(为==
sed -i 's/ (/==/g' res3.log

# 直接替换)
sed -i 's/)//g' res3.log

# 删除#
sed -i "s# #
#g" res3.log

# 对文件排序 | 去重 重定向到requires.txt
sort res3.log|uniq >requests.txt

# 删除准备文件
rm /tmp/res* 

参考:https://www.cnblogs.com/nixiaocang/p/7502680.html

原文地址:https://www.cnblogs.com/davis12/p/14930687.html