This file is indexed.

/usr/share/doc/snakefood/snakefood-doc.html is in snakefood 1.4-1.

This file is owned by root:root, with mode 0o644.

The actual contents of the file can be viewed below.

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
<?xml version="1.0" encoding="iso-8859-1" ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<meta name="generator" content="Docutils 0.4.1: http://docutils.sourceforge.net/" />
<title>Snakefood User Manual</title>
<meta name="author" content="Martin Blais &lt;blais&#64;furius.ca&gt;" />
<link rel="stylesheet" href="../style.css" type="text/css" />
</head>
<body>

<div id="project-header">
  <a href="/"><img src="/home/furius-logo-w.png" id="logo"></a>
  <div id="project-home"><a href="..">Project Home</a></div>
</div>

<div class="document" id="snakefood-user-manual">
<h1 class="title">Snakefood User Manual</h1>
<table class="docinfo" frame="void" rules="none">
<col class="docinfo-name" />
<col class="docinfo-content" />
<tbody valign="top">
<tr><th class="docinfo-name">Author:</th>
<td>Martin Blais &lt;<a class="reference" href="mailto:blais&#64;furius.ca">blais&#64;furius.ca</a>&gt;</td></tr>
</tbody>
</table>
<div class="contents topic">
<p class="topic-title first"><a id="contents" name="contents">Contents</a></p>
<ul class="simple">
<li><a class="reference" href="#introduction" id="id3" name="id3">Introduction</a><ul>
<li><a class="reference" href="#what-is-a-dependency" id="id4" name="id4">What is a Dependency?</a></li>
</ul>
</li>
<li><a class="reference" href="#seconds-usage-instructions" id="id5" name="id5">30-seconds Usage Instructions</a></li>
<li><a class="reference" href="#installation" id="id6" name="id6">Installation</a></li>
<li><a class="reference" href="#overview" id="id7" name="id7">Overview</a></li>
<li><a class="reference" href="#generating-dependencies" id="id8" name="id8">Generating Dependencies</a><ul>
<li><a class="reference" href="#how-packages-are-automatically-found" id="id9" name="id9">How Packages Are Automatically Found</a></li>
<li><a class="reference" href="#following-dependencies" id="id10" name="id10">Following Dependencies</a></li>
<li><a class="reference" href="#restricting-dependencies" id="id11" name="id11">Restricting Dependencies</a></li>
<li><a class="reference" href="#ignoring-unused-imports" id="id12" name="id12">Ignoring Unused Imports</a></li>
<li><a class="reference" href="#the-format-of-dependencies-files" id="id13" name="id13">The Format of Dependencies Files</a></li>
<li><a class="reference" href="#caching-raw-dependencies" id="id14" name="id14">Caching Raw Dependencies</a></li>
<li><a class="reference" href="#warnings" id="id15" name="id15">Warnings</a></li>
<li><a class="reference" href="#nodes-without-dependencies" id="id16" name="id16">Nodes Without Dependencies</a></li>
<li><a class="reference" href="#pragmas-marking-dependencies-as-ignored" id="id17" name="id17">Pragmas: Marking Dependencies as Ignored</a></li>
</ul>
</li>
<li><a class="reference" href="#filtering-and-clustering-dependencies" id="id18" name="id18">Filtering and Clustering Dependencies</a><ul>
<li><a class="reference" href="#using-standard-unix-tools" id="id19" name="id19">Using Standard UNIX Tools</a></li>
<li><a class="reference" href="#using-the-clustering-tool" id="id20" name="id20">Using the Clustering Tool</a></li>
</ul>
</li>
<li><a class="reference" href="#using-a-makefile" id="id21" name="id21">Using a Makefile</a></li>
<li><a class="reference" href="#listing-the-imports" id="id22" name="id22">Listing the Imports</a></li>
<li><a class="reference" href="#snakefood-import-checker" id="id23" name="id23">Snakefood Import Checker</a><ul>
<li><a class="reference" href="#using-a-pragma-to-ignore-an-unused-dependency" id="id24" name="id24">Using a Pragma to Ignore an Unused Dependency</a></li>
</ul>
</li>
<li><a class="reference" href="#original-uses" id="id25" name="id25">Original Uses</a><ul>
<li><a class="reference" href="#enforcing-dependency-relationships-on-commit" id="id26" name="id26">Enforcing Dependency Relationships on Commit</a></li>
<li><a class="reference" href="#splitting-a-codebase" id="id27" name="id27">Splitting a Codebase</a></li>
</ul>
</li>
<li><a class="reference" href="#feedback-and-comments" id="id28" name="id28">Feedback and Comments</a></li>
</ul>
</div>
<!-- 1   Introduction
  1.1  What is a Dependency?
2   30-seconds Usage Instructions
3   Installation
4   Overview
5   Generating Dependencies
  5.1  How Packages Are Automatically Found
  5.2  Following Dependencies
  5.3  Restricting Dependencies
  5.4  Ignoring Unused Imports
  5.5  The Format of Dependencies Files
  5.6  Caching Raw Dependencies
  5.7  Warnings
  5.8  Nodes Without Dependencies
  5.9  Pragmas: Marking Dependencies as Ignored
6   Filtering and Clustering Dependencies
  6.1  Using Standard UNIX Tools
  6.2  Using the Clustering Tool
7   Using a Makefile
8   Listing the Imports
9   Snakefood Import Checker
  9.1  Using a Pragma to Ignore an Unused Dependency
10  Original Uses
  10.1  Enforcing Dependency Relationships on Commit
  10.2  Splitting a Codebase
11  Feedback and Comments -->
<div class="section">
<h1><a class="toc-backref" href="#id3" id="introduction" name="introduction">Introduction</a></h1>
<p>This is the documentation for Snakefood, a dependency graph generator
for Python source code (written in Python).</p>
<div class="section">
<h2><a class="toc-backref" href="#id4" id="what-is-a-dependency" name="what-is-a-dependency">What is a Dependency?</a></h2>
<p>In this document, dependencies are import relationships between Python
source files.  If file <tt class="docutils literal"><span class="pre">a.py</span></tt> imports some code from file <tt class="docutils literal"><span class="pre">b.py</span></tt>,
we say that <tt class="docutils literal"><span class="pre">a.py</span></tt> <em>depends on</em> <tt class="docutils literal"><span class="pre">b.py</span></tt>, or that <tt class="docutils literal"><span class="pre">a.py</span></tt> has a
dependency on <tt class="docutils literal"><span class="pre">b.py</span></tt>.</p>
<p>Controlling the dependencies between the various parts of your code
(or between projects that your code uses) is a powerful way to
increase the reusability of your code: the least amount of
dependencies a body of source code has, the greater the likelihood
that this code will be usable in the future.  Dependencies cause codes
to break due to the independent evolution of modules and changes in
the interfaces.  Even within a single project, controlling
dependencies between layers is the essence of modularity.</p>
<p>Snakefood allows you to automatically generate a visual graph of those
dependencies.  When you produce these kinds of graphs, you will often
be surprised at how certain parts of your code inadvertently become
tied together.  Dependency graphs allow you to view the relationships
clearly and will generate questions about the high-level organization
of your code and allow to improve modularity and reusability.</p>
</div>
</div>
<div class="section">
<h1><a class="toc-backref" href="#id5" id="seconds-usage-instructions" name="seconds-usage-instructions">30-seconds Usage Instructions</a></h1>
<p>(For the impatient.)  You will need Graphviz.  Snakefood generates a
dot file (Graphviz input file).  Here is the simplest way to generate
a graph <a class="footnote-reference" href="#id2" id="id1" name="id1">[1]</a>:</p>
<pre class="literal-block">
sfood /myproject | sfood-graph | dot -Tps | pstopdf -i | xargf acroread
</pre>
<p>However <strong>this will probably not do what you want</strong>, unless your
project is pretty small.  The dependency graphs for reasonably sized
projects are generally complex, and you will want to filter out some
of the dependencies, and cluster some of them in logical groups of
files or by directory (see the text for the <tt class="docutils literal"><span class="pre">sfood-cluster</span></tt>
tool).</p>
<p>Read on for more details on how to use these tools.</p>
<table class="docutils footnote" frame="void" id="id2" rules="none">
<colgroup><col class="label" /><col /></colgroup>
<tbody valign="top">
<tr><td class="label"><a class="fn-backref" href="#id1" name="id2">[1]</a></td><td>See <a class="reference" href="http://furius.ca/pubcode/">http://furius.ca/pubcode/</a> for the <tt class="docutils literal"><span class="pre">xargf</span></tt> tool.</td></tr>
</tbody>
</table>
</div>
<div class="section">
<h1><a class="toc-backref" href="#id6" id="installation" name="installation">Installation</a></h1>
<ul>
<li><p class="first">You need to install Python-2.5 or greater.</p>
</li>
<li><p class="first">You need to Graphviz tools to generate graphs.  Snakefood produces
input files for Graphviz.</p>
</li>
<li><p class="first">To install the Snakefood tools themselves, just run the usual
distutils command:</p>
<pre class="literal-block">
cd snakefood-&lt;version&gt;
python setup.py install   # as root
</pre>
</li>
</ul>
</div>
<div class="section">
<h1><a class="toc-backref" href="#id7" id="overview" name="overview">Overview</a></h1>
<p>Snakefood provides a few main tools:</p>
<ol class="arabic simple">
<li><tt class="docutils literal"><span class="pre">sfood</span></tt>: Given a set of input files or root directories,
generate a list of dependencies between the files;</li>
<li><tt class="docutils literal"><span class="pre">sfood-graph</span></tt>: Read a list of dependencies and produce a
Graphviz dot file.  This file can be run through the Graphviz
<tt class="docutils literal"><span class="pre">dot</span></tt> tool to produce a viewable/printable PDF file;</li>
<li><tt class="docutils literal"><span class="pre">sfood-cluster</span></tt> (optional use): Read a list of dependencies,
a list of file clusters, and output a list of simplified
(clustered) dependencies.</li>
<li><tt class="docutils literal"><span class="pre">sfood-imports</span></tt> (complementary): A tool that given a set of
files and directories, finds and lists all the import statements
that are found, regardless of whether they can be imported or not.</li>
<li><tt class="docutils literal"><span class="pre">sfood-checker</span></tt> (complementary): An import checker that always
runs: analyze the source code with the AST and list unused or
redundant imports.</li>
</ol>
<p>Typically, you will use <tt class="docutils literal"><span class="pre">sfood</span></tt> to generate the dependencies and
pass those on to <tt class="docutils literal"><span class="pre">sfood-graph</span></tt>, then pass its output to <tt class="docutils literal"><span class="pre">dot</span></tt>
to generate your output PDF file.</p>
<div class="figure">
<img alt="programs1.png" src="programs1.png" />
<p class="caption">Simple pipeline for generating a full dependency graph.</p>
</div>
<p>For details on using the tools, call each program with the <tt class="docutils literal"><span class="pre">--help</span></tt>
option.</p>
</div>
<div class="section">
<h1><a class="toc-backref" href="#id8" id="generating-dependencies" name="generating-dependencies">Generating Dependencies</a></h1>
<p>To generate the dependencies, you use the <tt class="docutils literal"><span class="pre">sfood</span></tt> tool, giving
it a list of filenames:</p>
<pre class="literal-block">
sfood file1.py file2.py file3.py
</pre>
<p>You can also use directories:</p>
<pre class="literal-block">
sfood /path/to/my/project
</pre>
<p>If you specify directories, <tt class="docutils literal"><span class="pre">sfood</span></tt> will recurse through them
and automatically find all the Python source files and process them.
Each file is processed independently, giving the file to <tt class="docutils literal"><span class="pre">sfood</span></tt>
means &#8220;output the dependencies of this file&#8221;.</p>
<p><tt class="docutils literal"><span class="pre">sfood</span></tt> finds the dependencies in your source files using the
AST parser.  It does not figure out which <tt class="docutils literal"><span class="pre">import</span></tt> statements run
conditionally; it simply finds all the imports in the file and outputs
dependencies for each of them.</p>
<div class="note">
<p class="first admonition-title">Note</p>
<p>Note that none of the module your specify on the command-line are
loaded nor run.  Loading modules is almost always problem, because
a lot of codebases run initialization code in the global namespace,
which often requires additional setup.  Snakefood is guaranteed not
to have this problem: it does not run your code, it just looks at
it; the worst thing that can happen is that it misses some
dependencies.</p>
<p class="last">A problem with dependency trackers that run code is that they are
unreliable too, due to the dynamic nature of Python: the presence
of imports within function calls and <tt class="docutils literal"><span class="pre">__import__</span></tt> hooks makes it
almost impossible to always do the right thing.  This script aims
at being right 95% of the time, and we think that given the
trade-offs, 95% is good enough for 95% of its uses.</p>
</div>
<div class="section">
<h2><a class="toc-backref" href="#id9" id="how-packages-are-automatically-found" name="how-packages-are-automatically-found">How Packages Are Automatically Found</a></h2>
<p>For each file or directory that you specify on the command-line,
<tt class="docutils literal"><span class="pre">sfood</span></tt> automatically figures out which root it lives in.  What
we call a &#8220;root&#8221; in this document is a directory that should be in
<tt class="docutils literal"><span class="pre">PYTHONPATH</span></tt> if you were to import the given files from a script.</p>
<p>Snakefood finds the package roots,</p>
<ol class="arabic simple">
<li>by walking <strong>up</strong> the directory hierarchy of each file until it
finds a directory without a <tt class="docutils literal"><span class="pre">__init__.py</span></tt> file, or if no package
root is found this way,</li>
<li>by walking <strong>down</strong> the directory hierarchy until it finds such
roots.</li>
</ol>
<p>All of the package roots found this way are automatically prepended to
the Python import path, so you do not have to configure your
PYTHONPATH before you invoke <tt class="docutils literal"><span class="pre">sfood</span></tt>.  <tt class="docutils literal"><span class="pre">sys.path</span></tt> is in effect
when processing imports.  It is simply augmented to include the roots
that snakefood found</p>
<p>The roots are added to the module path and used to separate the
filename in two parts: the package root, and the filename relative to
that root (see the section on dependencies format below).</p>
</div>
<div class="section">
<h2><a class="toc-backref" href="#id10" id="following-dependencies" name="following-dependencies">Following Dependencies</a></h2>
<p>By default, <tt class="docutils literal"><span class="pre">sfood</span></tt> does not <em>follow</em> dependencies, that is, if
you ask it to process <tt class="docutils literal"><span class="pre">a.py</span></tt> and it finds that it has a dependency
on <tt class="docutils literal"><span class="pre">b.py</span></tt>, the file <tt class="docutils literal"><span class="pre">b.py</span></tt> does not get processed for its
dependencies.</p>
<p>Use the <tt class="docutils literal"><span class="pre">--follow</span></tt> option to tell <tt class="docutils literal"><span class="pre">sfood</span></tt> to follow its
dependencies.  Files for each target dependency that were found are
automatically examined for dependencies, and the process continues
until all leaves are found (we check for cycles too).</p>
</div>
<div class="section">
<h2><a class="toc-backref" href="#id11" id="restricting-dependencies" name="restricting-dependencies">Restricting Dependencies</a></h2>
<p>Normally you will want to find out the relationships only between
files in the packages that you provide on the command-line.  For
example, you will probably not be interested to find out about
dependencies to the modules in the standard library that comes with
Python.</p>
<p>You can filter out the dependencies by yourself using the usual UNIX
tools, like grep or sed. But since this is a typical case, there is a
convenient <tt class="docutils literal"><span class="pre">--internal</span></tt> option to sfood that automatically limits
dependencies to only those files living in the package roots
corresponding to the set files that you specify on the command-line.</p>
<p>You can restrict the dependencies even further, to only the list of
files that have been processed, that is, the list you specified on the
command-line in the first place. You enable this by using the
<tt class="docutils literal"><span class="pre">--internal</span></tt> option twice. This is a convenient feature when you
want to chart the dependencies of only the files in a subdirectory of
a larger project.</p>
<div class="note">
<p class="first admonition-title">Note</p>
<p class="last">Subdirectories that live under a package root but which do not have
an appropriate <tt class="docutils literal"><span class="pre">__init__.py</span></tt> file are considered external to the
containing package root (because they are distinct). Large
codebases often have such directories containing test scripts,
installation code and what not, which cannot be imported directly.
Add those directories to the command-line if you want to include
them while using the <tt class="docutils literal"><span class="pre">--internal</span></tt> option, or generate raw
dependencies and filter with grep.</p>
</div>
</div>
<div class="section">
<h2><a class="toc-backref" href="#id12" id="ignoring-unused-imports" name="ignoring-unused-imports">Ignoring Unused Imports</a></h2>
<p>With the <tt class="docutils literal"><span class="pre">--ignore-unused</span></tt> option, <tt class="docutils literal"><span class="pre">sfood</span></tt> will automatically
ignore dependencies motiviated by symbols imported but not used. This
determination is performed using the same conservative and safe
heuristic as is used in <tt class="docutils literal"><span class="pre">sfood-checker</span></tt>.</p>
<p>The resulting list of dependencies will always be smaller when running
with this option than without. Note that the resulting dependency tree
is the same that you would obtain should you clean up all the analyzed
code using <tt class="docutils literal"><span class="pre">sfood-checker</span></tt>, to hunt for unused imports.</p>
</div>
<div class="section">
<h2><a class="toc-backref" href="#id13" id="the-format-of-dependencies-files" name="the-format-of-dependencies-files">The Format of Dependencies Files</a></h2>
<p>The format of dependencies is really simple:</p>
<pre class="literal-block">
((&lt;source_package_root&gt;, &lt;source_file.py&gt;), (&lt;dest_package_root&gt;, &lt;dest_file.py&gt;))
</pre>
<p>where the <tt class="docutils literal"><span class="pre">package_root</span></tt> files are the directory names at the root
where the module files are found, and the <tt class="docutils literal"><span class="pre">file.py</span></tt> names are the
Python filenames relative to the corresponding root.</p>
<p>Each line is a valid Python tuple expression, so you can easily write
a Python script to process them using a line like:</p>
<pre class="literal-block">
for dep in map(eval, sys.stdin):
    ...
</pre>
<p>and output them like:</p>
<pre class="literal-block">
dep = (froot, fn), (troot, tn)
print repr(dep)
</pre>
</div>
<div class="section">
<h2><a class="toc-backref" href="#id14" id="caching-raw-dependencies" name="caching-raw-dependencies">Caching Raw Dependencies</a></h2>
<p>The process of building a nicely filtered dependency graph is
iterative, you will typically massage the dependencies to highlight
the relationships that you care about.  Since calculating the
dependencies can be a slow process (and filtering and graph generation
is not), we recommend to save the output of <tt class="docutils literal"><span class="pre">sfood</span></tt> to a file
and work from that.</p>
</div>
<div class="section">
<h2><a class="toc-backref" href="#id15" id="warnings" name="warnings">Warnings</a></h2>
<p>You may see a lot of warnings when you run <tt class="docutils literal"><span class="pre">sfood</span></tt>.  This is
normal.  There are a few reasons for this:</p>
<ul>
<li><p class="first">The code you are analyzing requires some external packages that are
not installed or not in your PYTHONPATH;</p>
</li>
<li><p class="first">The <tt class="docutils literal"><span class="pre">from-import</span></tt> Python syntax is ambiguous; for example, in the
following code, it is not clear whether <tt class="docutils literal"><span class="pre">table</span></tt> is a module or an
object defined in the <tt class="docutils literal"><span class="pre">database</span></tt> module:</p>
<pre class="literal-block">
from database import table
</pre>
<p>Therefore, <tt class="docutils literal"><span class="pre">sfood</span></tt> does not normally print out warnings for
these.  If you want to see a list of those failed imports, run it
with the <tt class="docutils literal"><span class="pre">--verbose</span></tt> option.</p>
</li>
</ul>
<p>In eitehr case <tt class="docutils literal"><span class="pre">sfood</span></tt> keeps running and produces all the other
dependencies that it finds.</p>
</div>
<div class="section">
<h2><a class="toc-backref" href="#id16" id="nodes-without-dependencies" name="nodes-without-dependencies">Nodes Without Dependencies</a></h2>
<p>To insure that all the file nodes show up in the graph, for each
processed file we output a line like this:</p>
<pre class="literal-block">
((&lt;source_package_root&gt;, &lt;source_file.py&gt;), (None, None))
</pre>
<p>The graphing tool interprets that to mean it has to at least create a
node for <tt class="docutils literal"><span class="pre">&lt;source_file&gt;.py</span></tt>, even if it has no dependency.  Scripts
you write to filter the dependencies need to be able to interpret
those lines appropriately.</p>
</div>
<div class="section">
<h2><a class="toc-backref" href="#id17" id="pragmas-marking-dependencies-as-ignored" name="pragmas-marking-dependencies-as-ignored">Pragmas: Marking Dependencies as Ignored</a></h2>
<p>Sometimes when an import statement is wrapped in a conditional, you
may want to avoid generating a dependency for that statement, for
example:</p>
<pre class="literal-block">
try:
    import superhero
except ImportError:
    # superhero not available. We fallback on evilgenius.
    import evilgenius
</pre>
<p>In this example, you may want to avoid having snakefood follow
dependencies to <tt class="docutils literal"><span class="pre">superhero</span></tt>, because your software can run fine
without it (that is, with <tt class="docutils literal"><span class="pre">evilgenius</span></tt>). To that effect, snakefood
looks for a string after the import statement, and uses that string as
a hint to modify its actions, for example, this will tell <tt class="docutils literal"><span class="pre">sfood</span></tt> to
not output the dependency for <tt class="docutils literal"><span class="pre">superhero</span></tt>:</p>
<pre class="literal-block">
try:
    import superhero; 'OPTIONAL'
...
</pre>
<p>This is especially useful with the <tt class="docutils literal"><span class="pre">--follow</span></tt> option, when you want
to avoid dragging a large dependency in the dependency list.</p>
</div>
</div>
<div class="section">
<h1><a class="toc-backref" href="#id18" id="filtering-and-clustering-dependencies" name="filtering-and-clustering-dependencies">Filtering and Clustering Dependencies</a></h1>
<div class="section">
<h2><a class="toc-backref" href="#id19" id="using-standard-unix-tools" name="using-standard-unix-tools">Using Standard UNIX Tools</a></h2>
<p>Since dependencies are simple line-based Python expressions, you can
use <tt class="docutils literal"><span class="pre">grep</span></tt> or <tt class="docutils literal"><span class="pre">sed</span></tt> to filter out or modify unwanted lines:</p>
<pre class="literal-block">
cat raw.deps | grep -v /usr/lib/python | sfood-graph &gt; out.dot
</pre>
<p>There is no formula for filtering or reformatting the dependencies; it
depends on your codebase and what you want the graph to show.</p>
</div>
<div class="section">
<h2><a class="toc-backref" href="#id20" id="using-the-clustering-tool" name="using-the-clustering-tool">Using the Clustering Tool</a></h2>
<p>A useful operation is to transform the relative filenames into logical
groups and to remove redundant lines.  We call this &#8220;clustering&#8221;.  A
common example is to lump together all the filenames that start with a
particular directory prefix.</p>
<p>You could do this with <tt class="docutils literal"><span class="pre">sed</span></tt> but you also need to remove redundant
lines to do it properly, i.e. after simplification of the relative
filenames, multiple lines will be equivalent.  <tt class="docutils literal"><span class="pre">sfood-cluster</span></tt>
does that automatically.  You create a file of cluster names:</p>
<pre class="literal-block">
pack1
pack2
</pre>
<p>and a dependency file will be transformed from this:</p>
<pre class="literal-block">
(('/myproject', 'pack1/person.py'), ('/myproject', 'pack1/employee.py'))
(('/myproject', 'pack1/person.py'), ('/myproject', 'pack1/manager.py'))
(('/myproject', 'pack1/person.py'), ('/myproject', 'pack2/boss.py'))
</pre>
<p>to this:</p>
<pre class="literal-block">
(('/myproject', 'pack1'), ('/myproject', 'pack1'))
(('/myproject', 'pack1'), ('/myproject', 'pack2'))
</pre>
<p>Here is how to use the <tt class="docutils literal"><span class="pre">sfood-cluster</span></tt> tool:</p>
<pre class="literal-block">
sfood /myproject | sfood-cluster -f clusters | sfood-graph &gt; myproject.dot
</pre>
<div class="figure">
<img alt="programs2.png" src="programs2.png" />
<p class="caption">Pipeline for dependency graph with clustering.</p>
</div>
<p>You can either create the <tt class="docutils literal"><span class="pre">clusters</span></tt> file manually, or with a
<tt class="docutils literal"><span class="pre">find</span></tt> or <tt class="docutils literal"><span class="pre">ls</span></tt> command in your source tree.&quot;</p>
</div>
</div>
<div class="section">
<h1><a class="toc-backref" href="#id21" id="using-a-makefile" name="using-a-makefile">Using a Makefile</a></h1>
<p>If you will repeatedly compute the dependencies for a codebase that
you maintain, you could write a simple script to do all the custom
things that you need to, for example:</p>
<pre class="literal-block">
# Generate the raw dependencies.
sfood /myproject &gt; /tmp/raw.deps

# Filter and cluster.
cd /myproject ; ls -1d * &gt; /tmp/clusters
cat /tmp/raw.deps | grep -v test_widget | sfood-cluster -f /tmp/clusters &gt; /tmp/filt.deps

# Generate the graph.
cat /tmp/filt.deps | sfood-graph -p | dot -Tps | pstopdf -i -o /tmp/myproject.pdf
</pre>
<p>While this will work, a better way to write such a script is to use a
makefile.</p>
<p>Here is an example for a simple self-contained <tt class="docutils literal"><span class="pre">Makefile</span></tt> that will
process the relevant dependencies as above:</p>
<pre class="literal-block">
NAME = myproject
ROOT = /path/to/myproject
PDFS = $(NAME).pdf

.SUFFIXES: .deps .dot .pdf .clusters

all: $(PDFS)

raw.deps: $(ROOT)
        sfood -i $(ROOT) $(FOOD_FLAGS) &gt; $&#64;

$(NAME).clusters: $(ROOT)
        cd $(ROOT) ; ls -1d * &gt; $(shell pwd)/$&#64;

$(NAME).deps: $(NAME).clusters raw.deps
        cat raw.deps | sfood-cluster -f $&lt; &gt; $&#64;

.deps.pdf:
        cat $&lt; | sfood-graph | dot -Tps | ps2pdf - $&#64;

clean:
        rm -f *.clusters *.dot *.pdf
        ls -1 *.deps | grep -v ^raw.deps | xargs rm -f

realclean: clean
        rm -f raw.deps
</pre>
<p>For a set of more reusable make rules, take a look at
<tt class="docutils literal"><span class="pre">snakefood/test/Makefile.rules</span></tt> and the Makefiles that we use to run
out tests on existing codebases.  You can probably leverage this for
your project (feel free to copy and modify it as needed).</p>
</div>
<div class="section">
<h1><a class="toc-backref" href="#id22" id="listing-the-imports" name="listing-the-imports">Listing the Imports</a></h1>
<p>If you only want to list the imported symbols and modules, without
having the tool try to find the file where the modules are to be
found, you can use the complementary tool <tt class="docutils literal"><span class="pre">sfood-imports</span></tt>, which
essentially replaces grepping the files for imports.</p>
<p><tt class="docutils literal"><span class="pre">sfood-imports</span></tt> also disambiguates local imports from globals by
looking for files below the level of the file that is imported, in the
same way that <tt class="docutils literal"><span class="pre">sfood</span></tt> does.</p>
<p>For example, to list the imports from <tt class="docutils literal"><span class="pre">sfood</span></tt>, I do this:</p>
<pre class="literal-block">
sivananda:~/p/snakefood/bin$ sfood-imports sfood
sfood:20: sys
sfood:20: os
sfood:20: logging
sfood:20: traceback
sfood:20: re
sfood:21: imp
sfood:21: compiler
sfood:22: os.path
sfood:23: collections.defaultdict
sfood:24: operator.itemgetter
sfood:25: dircache.listdir
sfood:151: imp.ImpImporter
sfood:153: pkgutil.ImpImporter
sfood:324: optparse
</pre>
<p>Also, see the <tt class="docutils literal"><span class="pre">--unified</span></tt> option which will output a single set of
unique imports for a set of files.</p>
</div>
<div class="section">
<h1><a class="toc-backref" href="#id23" id="snakefood-import-checker" name="snakefood-import-checker">Snakefood Import Checker</a></h1>
<p>Another tool that comes with Snakefood is a program that checks for
unused and redundant imports. Just run it on files or directories (it
recurses and finds the Python source files). For example:</p>
<pre class="literal-block">
~/p/.../python/xxdiff$ sfood-checker invoke.py

/home/blais/p/xxdiff/lib/python/xxdiff/invoke.py:290: Redundant import 'optparse'
/home/blais/p/xxdiff/lib/python/xxdiff/invoke.py:11: Unused symbol 'os'
</pre>
<p>There are other lint-like tools to do import checking out there; a
problem with most of these tools is that they attempt to import the
modules they analyze, which often fails because of code with
side-effects that runs at global module level. <tt class="docutils literal"><span class="pre">sfood-checker</span></tt> does
not import the code it analyzes, rather, it uses the AST to perform a
&quot;good-enough&quot; analysis. The reason for building this import checker is
the same as the reason for making snakefood: it <strong>always</strong> works on
your code, no matter what; it provides a &quot;good enough&quot; / 99% accurate
solution that at least, always &quot;just works&quot;.</p>
<div class="section">
<h2><a class="toc-backref" href="#id24" id="using-a-pragma-to-ignore-an-unused-dependency" name="using-a-pragma-to-ignore-an-unused-dependency">Using a Pragma to Ignore an Unused Dependency</a></h2>
<p>If you are importing a file for its side-effects only,
<tt class="docutils literal"><span class="pre">sfood-checker</span></tt> has no way to detect this and will report the import
as unused. To avoid this, use a pragma after the import statement to
disable the warning:</p>
<pre class="literal-block">
# Importing for side-effects only.
import injectrace; 'SIDE-EFFECTS'
</pre>
</div>
</div>
<div class="section">
<h1><a class="toc-backref" href="#id25" id="original-uses" name="original-uses">Original Uses</a></h1>
<p>This section documents original ways in which you can use this
program.</p>
<div class="section">
<h2><a class="toc-backref" href="#id26" id="enforcing-dependency-relationships-on-commit" name="enforcing-dependency-relationships-on-commit">Enforcing Dependency Relationships on Commit</a></h2>
<p>If a codebase has some package dependency relationships that should be
enforced, for example, that anything in package <tt class="docutils literal"><span class="pre">root.core</span></tt> should
not depend on anything in package <tt class="docutils literal"><span class="pre">root.util</span></tt>, you can easily write
a post-commit hook in your favorite source code versioning system that
will run <tt class="docutils literal"><span class="pre">sfood</span></tt> on an up-to-date checkout and grep for the
offending relationships.  Such a script could send an email to the
checkins list, or even refuse to commit if the offending dependency
occurs.</p>
</div>
<div class="section">
<h2><a class="toc-backref" href="#id27" id="splitting-a-codebase" name="splitting-a-codebase">Splitting a Codebase</a></h2>
<p>It is often required to extract a portion of a codebase outside a
source tree, in a way that the extracted software will still be
functional.  In other words, you sometimes have to extract a script
and all the dependencies that it drags along with it to remain usable.</p>
<p>You can easily use snakefood for this purpose: run <tt class="docutils literal"><span class="pre">sfood</span> <span class="pre">--follow</span>
<span class="pre">--internal</span></tt> on the given script, and it should produce the list of
dependencies that it needs to continue functioning. Flatten these
dependencies into a list of filenames with <tt class="docutils literal"><span class="pre">sfood-flatten</span></tt> and copy
the files somewhere else.</p>
</div>
</div>
<div class="section">
<h1><a class="toc-backref" href="#id28" id="feedback-and-comments" name="feedback-and-comments">Feedback and Comments</a></h1>
<p>I wrote this script in may 2007.  I had previously tried to write a
reliable dependency grapher about 4 or 5 times, giving up each time on
the various intricacies of the Python import semantics.  I'm pretty
happy this time that I've found something reasonably reliable that
works everywhere, and I'm fully committed to fixing any bugs you may
find and to bring this project to a stable version 1.0 state, where it
can be used ubiquitously on all Python projects. Comments, feedback
and donations are welcome.  I hope you find this program useful.</p>
</div>
</div>
</body>
</html>