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脚本就全复制到新目录了,再把其它相关文件复制过去,干净清新的新工程就诞生了。

Greenery - tools for parsing and manipulating regular expressions

Greenery - tools for parsing and manipulating regular expressions

qntm 242 Dec 15, 2022
A Python script that parses and checks public proxies. Multithreading is supported.

A Python script that parses and checks public proxies. Multithreading is supported.

LevPrav 7 Nov 25, 2022
Give you a better view of your Docker registry disk usage.

registry-du Give you a better view of your Docker registry disk usage. This small tool will analysis your Docker registry(vanilla or Harbor both work)

Nova Kwok 16 Jan 07, 2023
Simple Python tool that generates a pseudo-random password with numbers, letters, and special characters in accordance with password policy best practices.

Simple Python tool that generates a pseudo-random password with numbers, letters, and special characters in accordance with password policy best practices.

Joe Helle 7 Mar 25, 2022
A simple gpsd client and python library.

gpsdclient A small and simple gpsd client and library Installation Needs Python 3 (no other dependencies). If you want to use the library, use pip: pi

Thomas Feldmann 33 Nov 24, 2022
Python 3 script unpacking statically x86 CryptOne packer.

Python 3 script unpacking statically x86 CryptOne packer. CryptOne versions: 2021/08 until now (2021/12)

5 Feb 23, 2022
A (very dirty) experiment to remove layers from a Docker image.

Surgically remove layers from a Docker image (with a chainsaw)

Jérôme Petazzoni 9 Jun 08, 2022
A python module to manipulate XCode projects

This module can read, modify, and write a .pbxproj file from an Xcode 4+ projects. The file is usually called project.pbxproj and can be found inside the .xcodeproj bundle. Because some task cannot b

Ignacio Calderon 1.1k Jan 02, 2023
Python based tool to extract forensic info from EventTranscript.db (Windows Diagnostic Data)

EventTranscriptParser EventTranscriptParser is python based tool to extract forensically useful details from EventTranscript.db (Windows Diagnostic Da

P. Abhiram Kumar 24 Nov 18, 2022
The Black shade analyser and comparison tool.

diff-shades The Black shade analyser and comparison tool. AKA Richard's personal take at a better black-primer (by stealing ideas from mypy-primer) :p

Richard Si 10 Apr 29, 2022
Set of utilities for exporting/controlling your robot in Blender

Blender Robotics Utils This repository contains utilities for exporting/controlling your robot in Blender Maintainers This repository is maintained by

Robotology 33 Nov 30, 2022
Every 2 minutes, check for visa slots at VFS website

vfs-visa-slot-germany Every 2 minutes, check for visa slots at VFS website. If there are any, send a call and a message of the format: Sent from your

12 Dec 15, 2022
WindowsDebloat - Windows Debloat with python

Windows Debloat 🗑️ Quickly and easily configure Windows 10. Disclaimer I am NOT

1 Mar 26, 2022
Script to rename and resize folders of images

script to rename and resize folders of images

Tega Brain 2 Oct 29, 2021
Python code to divide big numbers

divide-big-num Python code to divide big numbers

VuMinhNgoc 1 Oct 15, 2021
A simple dork generator written in python that outputs dorks with the domain extensions you enter

Dork Gen A simple dork generator written in python that outputs dorks with the domain extensions you enter in a ".txt file". Usage The code is pretty

Z3NToX 4 Oct 30, 2022
Build capture utility for Linux

CX-BUILD Compilation Database alternative Build Prerequisite the CXBUILD uses linux system call trace utility called strace which was customized. So I

GLaDOS (G? L? Automatic Debug Operation System) 3 Nov 03, 2022
Obsidian tools - a Python package for analysing an Obsidian.md vault

obsidiantools is a Python package for getting structured metadata about your Obsidian.md notes and analysing your vault.

Mark Farragher 153 Jan 04, 2023
Utility to extract Fantasy Grounds Unity Line-of-sight and lighting files from a Univeral VTT file exported from Dungeondraft

uvtt2fgu Utility to extract Fantasy Grounds Unity Line-of-sight and lighting files from a Univeral VTT file exported from Dungeondraft This program wo

Andre Kostur 29 Dec 05, 2022
Make some improvements in the Pizza class and pizzashop file by refactoring.

Make some improvements in the Pizza class and pizzashop file by refactoring.

James Brucker 1 Oct 18, 2021