[Debian-olpc-devel] Finding dependencies of Python code

Jonas Smedegaard dr at jones.dk
Tue Nov 10 16:02:48 UTC 2009

On Tue, Nov 10, 2009 at 01:29:16PM +0100, Jonas Smedegaard wrote:
>On Fri, Nov 06, 2009 at 06:51:13PM +0100, Jonas Smedegaard wrote:
>>On Fri, Nov 06, 2009 at 06:19:19PM +0100, Sascha Silbe wrote:
>>>On Fri, Nov 06, 2009 at 05:39:41PM +0100, Jonas Smedegaard wrote:
>>>>If anyone knows about a tool to extract all "import" statements 
>>>>in  Python code, I would much appreciate it!
>>>find -name "*.py" |xargs grep -h import |sort -u
>>Yeah - better than nothing.  Some time ago I wrote a loooong perl 
>>one-liner doing above + some kinds of calling shell commands, but 
>>then by seession dies and with it the one-liner which off course 
>>wasn't written down anywhere yet and didn't even gt saved to the 
>>bash history :-(
>Here's an improvement of above which simplifies simple import 
>statements and includes filename for indented ones which are 
>potentially trickier (it might be try-encapsulated statements that 
>should then lead to recommends or suggests rather than hard depends):
>find * -name "*.py" -exec grep -H '\bimport\b' {} \; | perl -pe 's/^[^:]+:(?:from\s+(\S+)|import\s+(\S+)).*/$1/; s/^(?=[^:]+:)/A /' | sort -u

..and some further improvements:

   * check all files (to include Python and Python-in-shell scripts)
   * fix regex to not ignore simple "import foo" strings
   * include and split comma-separated lists of imports

find * -type f -exec grep -H '\bimport\b' {} \; | perl -pe 's/^[^:]+:(?:from\s+(\S+)|import\s+(\S+((?<=,)\s\S+)*+))\b.*/$1$2/ and s/(\S+),\s+/$1\n/g; s/^(?=[^:]+:)/A /' | sort -u

Below points still stand:

>I still need to reinvent the parts scanning for external binaries 
>executed from inside Python code.
>Help appreciated!
>>>The "magic" stuff should usually be safe to ignore as it's for 
>>>loading plugins, not external dependencies (AFAICT at least).
>>What "magic"?  I lost you there.
>Please do elaborate.

  - Jonas

* Jonas Smedegaard - idealist & Internet-arkitekt
* Tlf.: +45 40843136  Website: http://dr.jones.dk/

  [x] quote me freely  [ ] ask before reusing  [ ] keep private
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 836 bytes
Desc: Digital signature
URL: <http://lists.alioth.debian.org/pipermail/debian-olpc-devel/attachments/20091110/c1afdf56/attachment-0001.pgp>

More information about the Debian-olpc-devel mailing list