如何为python代码做Makefile依赖

时间:2022-02-03 03:21:13

I have a bunch of C files that are generated by a collection of python programs that have a number of shared python modules and I need to account for this in my make system.

我有一堆C文件是由一组python程序生成的,这些程序有许多共享的python模块,我需要在make系统中考虑到这一点。

It is easy enough to enumerate which python program need to be run to generate each C file. What I can't find a good solution for is determining which other python files those programs depend on. I need this so make will know what needs regenerating if one of the shared python files changes.

枚举需要运行哪个python程序来生成每个C文件是很容易的。我找不到一个好的解决方案是确定那些程序所依赖的其他python文件。我需要这个,所以如果其中一个共享的python文件发生变化,make会知道需要重新生成什么。

Is there a good system for producing make style dependency rules from a collection of python sources?

是否有一个很好的系统来从python源集合中生成make风格依赖规则?

2 个解决方案

#1


3  

modulefinder can be used to get the dependency graph.

modulefinder可用于获取依赖图。

#2


1  

The import statements are pretty much all the dependencies there are. There are are two relevant forms for the import statements:

import语句几乎都是依赖项。导入语句有两种相关形式:

import x, y, z
from x import a, b, c

You'll also need the PYTHONPATH and sites information that is used to build sys.path. This shows the physical locations of the modules and packages.

您还需要PYTHONPATH和用于构建sys.path的站点信息。这显示了模块和包的物理位置。

That's kind of painful to process, since you have to do the transitive closure of all imports in all modules you import.

处理起来很麻烦,因为您必须对导入的所有模块中的所有导入进行传递闭包。

As an alternative approach, you can use the -v option to get the complete list of imports and physical files. This produces a log that you can edit into a flat list of dependencies.

作为替代方法,您可以使用-v选项获取导入和物理文件的完整列表。这会生成一个日志,您可以将其编辑为依赖项的平面列表。

For instance, when I do

例如,当我这样做

>>> import math

I see this in the log

我在日志中看到了这一点

dlopen("/Library/Frameworks/Python.framework/Versions/2.5/lib/python2.5/lib-dynload/math.so", 2);
import math # dynamically loaded from /Library/Frameworks/Python.framework/Versions/2.5/lib/python2.5/lib-dynload/math.so

#1


3  

modulefinder can be used to get the dependency graph.

modulefinder可用于获取依赖图。

#2


1  

The import statements are pretty much all the dependencies there are. There are are two relevant forms for the import statements:

import语句几乎都是依赖项。导入语句有两种相关形式:

import x, y, z
from x import a, b, c

You'll also need the PYTHONPATH and sites information that is used to build sys.path. This shows the physical locations of the modules and packages.

您还需要PYTHONPATH和用于构建sys.path的站点信息。这显示了模块和包的物理位置。

That's kind of painful to process, since you have to do the transitive closure of all imports in all modules you import.

处理起来很麻烦,因为您必须对导入的所有模块中的所有导入进行传递闭包。

As an alternative approach, you can use the -v option to get the complete list of imports and physical files. This produces a log that you can edit into a flat list of dependencies.

作为替代方法,您可以使用-v选项获取导入和物理文件的完整列表。这会生成一个日志,您可以将其编辑为依赖项的平面列表。

For instance, when I do

例如,当我这样做

>>> import math

I see this in the log

我在日志中看到了这一点

dlopen("/Library/Frameworks/Python.framework/Versions/2.5/lib/python2.5/lib-dynload/math.so", 2);
import math # dynamically loaded from /Library/Frameworks/Python.framework/Versions/2.5/lib/python2.5/lib-dynload/math.so