Find dependent python scripts of a python script in a project directory.

Overview

检测一个python脚本依赖的python脚本,听起来有点绕,先说一下背景吧。

在做python开发的时候,随着日积月累,工程目录下会有很多py脚本,而在部署的时候只需要用到其中一小部分,无关的脚本很多,感觉很不清爽,人为一个一个去筛吧,又很麻烦,有没有工具可以自动把相关的py脚本抽出来呢?如果有,请告诉我!我没有找到,但是仔细想一下好像可以自己实现。

在部署工程的时候,通常有个主程序(py脚本),但光有主程序还不够,主程序一般会依赖其它模块,有些是自己写的py脚本,工程稍微大一点就会出现层层依赖,人为去查又很麻烦,文章标题的意思就是,怎么自动揪出主程序依赖的脚本呢?

要揪出来的是那些自己开发的脚本(或者是搬运过来的),反正不是那些安装的模块。因为安装的模块不需要找,部署的时候自然会安装。

假设有一个工程的目录结构如下

.
├── demo_main.py
├── demo_package	
    └── demo_module.py

demo_main.py是主程序,demo_package是一个文件夹,里面包含demo_module.py。主程序文件里有一句from demo_package import demo_module,它依赖demo_module.py

python demo_module.py执行主程序之后,工程目录下生成一个子目录__pycache__,里面有一个.pyc文件,从名字可以看出来是与demo_module.py对应的。也就是,执行一个py脚本以后,它所依赖的py脚本会有一个对应的.pyc文件生成。

.
├── demo_main.py
└── demo_package
    ├── __pycache__
    │   └── demo_module.cpython-37.pyc
    └── demo_module.py

而且,.pyc文件的位置和命名是有规律的。__pycache__demo_module.py在同一个目录下,demo_module.cpython-37.pycdemo_module.py前缀相同。

那么,思路就有了:

  1. 执行一下主程序
  2. 找到工程目录下生成的.pyc文件
  3. 根据.pyc文件的位置和名称推出py脚本的位置和名称
  4. 把py脚本copy到新的工程目录
  5. 把主程序也复制过去

这样,需要的py脚本就全复制到新目录了,再把其它相关文件复制过去,干净清新的新工程就诞生了。

You might also like...
A set of Python scripts to surpass human limits in accomplishing simple tasks.

Human benchmark fooler Summary A set of Python scripts with Selenium designed to surpass human limits in accomplishing simple tasks available on https

A repository containing several general purpose Python scripts to automate daily and common tasks.

General Purpose Scripts Introduction This repository holds a curated list of Python scripts which aim to help us automate daily and common tasks. You

 Writing Alfred copy/paste scripts in Python
Writing Alfred copy/paste scripts in Python

Writing Alfred copy/paste scripts in Python This repository shows how to create Alfred scripts in Python. It assumes that you using pyenv for Python v

Just some scripts to export vector tiles to geojson.

Vector tiles to GeoJSON Nowadays modern web maps are usually based on vector tiles. The great thing about vector tiles is, that they are not just imag

A collection of custom scripts for working with Quake assets.

Custom Quake Tools A collection of custom scripts for working with Quake assets. Features Script to list all BSP files in a Quake mod

Collection of code auto-generation utility scripts for the Horizon `Boot` system module

boot-scripts This is a collection of code auto-generation utility scripts for the Horizon Boot system module, intended for use in Atmosphère. Usage Us

A toolkit for writing and executing automation scripts for Final Fantasy XIV

XIV Scripter This is a tool for scripting out series of actions in FFXIV. It allows for custom actions to be defined in config.yaml as well as custom

This repository contains scripts that help you validate QR codes.

Validation tools This repository contains scripts that help you validate QR codes. It's hacky, and a warning for Apple Silicon users: the dependencies

These scripts look for non-printable unicode characters in all text files in a source tree

find-unicode-control These scripts look for non-printable unicode characters in all text files in a source tree. find_unicode_control.py should work w

Owner
null
Astvuln is a simple AST scanner which recursively scans a directory, parses each file as AST and runs specified method.

Astvuln Astvuln is a simple AST scanner which recursively scans a directory, parses each file as AST and runs specified method. Some search methods ar

Bitstamp Security 7 May 29, 2022
Install, run, and update apps without root and only in your home directory

Qube Apps Install, run, and update apps in the private storage of a Qube Building instrutions

Micah Lee 26 Dec 27, 2022
Install, run, and update apps without root and only in your home directory

Qube Apps Install, run, and update apps in the private storage of a Qube. Build and install in Qubes Get the code: git clone https://github.com/micahf

Micah Lee 26 Dec 27, 2022
Find unused resource keys in properties files in a Salesforce Commerce Cloud project and get rid of them.

Find Unused Resource Keys Find unused resource keys in properties files in a Salesforce Commerce Cloud project and get rid of them. It looks through a

Noël 5 Jan 8, 2022
Here, I find the Fibonacci Series using python

Fibonacci-Series-using-python Here, I find the Fibonacci Series using python Requirements No Special Requirements Contribution I have strong belief on

Sachin Vinayak Dabhade 4 Sep 24, 2021
A python package for your Kali Linux distro that find the fastest mirror and configure your apt to use that mirror

Kali Mirror Finder Using Single Python File A python package for your Kali Linux distro that find the fastest mirror and configure your apt to use tha

MrSingh 6 Dec 12, 2022
A python program to find binary, octal and hexadecimal of a decimal.

decimal-converter This little python program can convert a decimal in to, Binary Octal Hexadecimal Needed Python 3 or later or a online python compile

Chandula Janith 0 Nov 27, 2021
This tool analyzes the json files generated by stream-lnd-htlcs to find hidden channel demand.

analyze_lnd_htlc Introduction Rebalancing channels is an important part of running a Lightning Network node. While it would be great if all channels c

Marimox 4 Dec 8, 2022
Find version automatically based on git tags and commit messages.

GIT-CONVENTIONAL-VERSION Find version automatically based on git tags and commit messages. The tool is very specific in its function, so it is very fl

null 0 Nov 7, 2021
Rabbito is a mini tool to find serialized objects in input values

Rabbito-ObjectFinder Rabbito is a mini tool to find serialized objects in input values What does Rabbito do Rabbito has the main object finding Serial

null 7 Dec 13, 2021