Merge lp:~stefanor/objgraph/filename-514422 into lp:objgraph

Proposed by Stefano Rivera
Status: Merged
Merged at revision: 6
Proposed branch: lp:~stefanor/objgraph/filename-514422
Merge into: lp:objgraph
Diff against target: 166 lines (+40/-29)
4 files modified
README.txt (+2/-2)
examples.txt (+1/-1)
objgraph.py (+25/-11)
setup.py (+12/-15)
To merge this branch: bzr merge lp:~stefanor/objgraph/filename-514422
Reviewer Review Type Date Requested Status
Marius Gedminas Approve
Review via email: mp+18292@code.launchpad.net
To post a comment you must log in.
Revision history for this message
Stefano Rivera (stefanor) wrote :

You asked for a patch :)

Revision history for this message
Marius Gedminas (mgedmin) wrote :

On Sat, Jan 30, 2010 at 12:29:12AM -0000, Stefano Rivera wrote:
> Stefano Rivera has proposed merging lp:~stefanor/objgraph/filename-514422 into lp:objgraph.
>
> Requested reviews:
> Marius Gedminas (mgedmin)
> Related bugs:
> #514422 Creates files in the current directory
> https://bugs.launchpad.net/bugs/514422
>
>
> You asked for a patch :)

You're awesome! And switching to subprocess.Popen too!

I'm currently in the middle of a Game Jam with brains leaking out my
ears. I should have time to merge this by the end of tomorrow.

Marius Gedminas
--
Favorite MS-DOS error message: "Drive C: not ready, close door."

Revision history for this message
Marius Gedminas (mgedmin) wrote :

It was merged but launchpad still shows the merge request as "pending". Not very smart of it, I'd say. Let's see what happens if I do this...

review: Approve

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file 'README.txt'
2--- README.txt 2009-03-24 23:08:10 +0000
3+++ README.txt 2010-01-30 00:29:12 +0000
4@@ -24,7 +24,7 @@
5 >>> x = []
6 >>> y = [x, [x], dict(x=x)]
7 >>> import objgraph
8- >>> objgraph.show_refs([y])
9+ >>> objgraph.show_refs([y], filename='sample-graph')
10
11 You should see a graph like this:
12
13@@ -33,7 +33,7 @@
14
15 Now try
16
17- >>> objgraph.show_backrefs([x])
18+ >>> objgraph.show_backrefs([x], filename='sample-backref-graph')
19
20 and you'll see
21
22
23=== modified file 'examples.txt'
24--- examples.txt 2009-03-25 00:23:51 +0000
25+++ examples.txt 2010-01-30 00:29:12 +0000
26@@ -16,6 +16,6 @@
27 >>> del x, y, z
28
29 >>> import objgraph
30- >>> objgraph.show_backrefs(objgraph.by_type('Nondestructible'))
31+ >>> objgraph.show_backrefs(objgraph.by_type('Nondestructible'), filename='finalizers')
32
33 .. image:: finalizers.png
34
35=== modified file 'objgraph.py'
36--- objgraph.py 2009-11-16 15:13:05 +0000
37+++ objgraph.py 2010-01-30 00:29:12 +0000
38@@ -75,6 +75,8 @@
39 import weakref
40 import operator
41 import os
42+import os.path
43+import subprocess
44
45
46 def count(typename):
47@@ -204,7 +206,7 @@
48
49
50 def show_backrefs(objs, max_depth=3, extra_ignore=(), filter=None, too_many=10,
51- highlight=None):
52+ highlight=None, filename='backrefs'):
53 """Generate an object reference graph ending at ``objs``
54
55 The graph will show you what objects refer to ``objs``, directly and
56@@ -236,11 +238,12 @@
57 """
58 show_graph(objs, max_depth=max_depth, extra_ignore=extra_ignore,
59 filter=filter, too_many=too_many, highlight=highlight,
60- edge_func=gc.get_referrers, swap_source_target=False)
61+ edge_func=gc.get_referrers, swap_source_target=False,
62+ filename=filename)
63
64
65 def show_refs(objs, max_depth=3, extra_ignore=(), filter=None, too_many=10,
66- highlight=None):
67+ highlight=None, filename='refs'):
68 """Generate an object reference graph starting at ``objs``
69
70 The graph will show you what objects are reachable from ``objs``, directly
71@@ -272,7 +275,8 @@
72 """
73 show_graph(objs, max_depth=max_depth, extra_ignore=extra_ignore,
74 filter=filter, too_many=too_many, highlight=highlight,
75- edge_func=gc.get_referents, swap_source_target=True)
76+ edge_func=gc.get_referents, swap_source_target=True,
77+ filename=filename)
78
79 #
80 # Internal helpers
81@@ -280,10 +284,10 @@
82
83 def show_graph(objs, edge_func, swap_source_target,
84 max_depth=3, extra_ignore=(), filter=None, too_many=10,
85- highlight=None):
86+ highlight=None, filename='objects'):
87 if not isinstance(objs, (list, tuple)):
88 objs = [objs]
89- f = file('objects.dot', 'w')
90+ f = file(filename + '.dot', 'w')
91 print >> f, 'digraph ObjectGraph {'
92 print >> f, ' node[shape=box, style=filled, fillcolor=white];'
93 queue = []
94@@ -344,13 +348,17 @@
95 break
96 print >> f, "}"
97 f.close()
98- print "Graph written to objects.dot (%d nodes)" % nodes
99- if os.system('which xdot >/dev/null') == 0:
100+ print "Graph written to %s.dot (%d nodes)" % (filename, nodes)
101+ if program_in_path('xdot'):
102 print "Spawning graph viewer (xdot)"
103- os.system("xdot objects.dot &")
104+ subprocess.Popen(['xdot', filename + '.dot'])
105 else:
106- os.system("dot -Tpng objects.dot > objects.png")
107- print "Image generated as objects.png"
108+ pngfile = file(filename + '.png', 'wb')
109+ dot = subprocess.Popen(['dot', '-Tpng', filename + '.dot'],
110+ stdout=pngfile)
111+ dot.wait()
112+ pngfile.close()
113+ print "Image generated as %s.png" % filename
114
115
116 def obj_node_id(obj):
117@@ -416,3 +424,9 @@
118 return ' [label="%s"]' % quote(safe_repr(k))
119 return ''
120
121+
122+def program_in_path(program):
123+ path = os.environ.get("PATH", os.defpath).split(os.pathsep)
124+ path = [os.path.join(dir, program) for dir in path]
125+ path = [True for file in path if os.path.isfile(file)]
126+ return bool(path)
127
128=== modified file 'setup.py'
129--- setup.py 2009-03-25 00:02:24 +0000
130+++ setup.py 2010-01-30 00:29:12 +0000
131@@ -17,23 +17,20 @@
132 return d['__version__']
133
134
135-def build_script_to_build_images():
136- yield 'import os'
137+def build_images():
138+ env = {}
139+ block = []
140 for line in itertools.chain(open(relative('README.txt')),
141 open(relative('examples.txt'))):
142- if line.startswith(' >>>') or line.startswith(' ...'):
143- yield line[8:].rstrip()
144- if line.startswith('.. image:: '):
145- filename = line.split()[2]
146- yield 'os.system("dot -Tpng objects.dot > %s")' % filename
147-
148-
149-def script_to_build_images():
150- return '\n'.join(build_script_to_build_images())
151-
152-
153-def build_images():
154- os.popen(sys.executable, 'w').write(script_to_build_images())
155+ if line.startswith(' ...'):
156+ block.append(line[8:].rstrip())
157+ if line.startswith(' >>>'):
158+ if block:
159+ exec('\n'.join(block), env)
160+ block = []
161+ block.append(line[8:].rstrip())
162+ if block:
163+ exec('\n'.join(block), env)
164
165
166 if len(sys.argv) > 1 and sys.argv[1] == '--show-image-script':

Subscribers

People subscribed via source and target branches