Merge lp:~jamesodhunt/upstart/initctl2dot-fix-for-subdirs into lp:upstart

Proposed by James Hunt
Status: Merged
Merged at revision: 1462
Proposed branch: lp:~jamesodhunt/upstart/initctl2dot-fix-for-subdirs
Merge into: lp:upstart
Diff against target: 270 lines (+109/-30)
4 files modified
ChangeLog (+12/-0)
NEWS (+1/-1)
scripts/initctl2dot.py (+70/-11)
scripts/man/initctl2dot.8 (+26/-18)
To merge this branch: bzr merge lp:~jamesodhunt/upstart/initctl2dot-fix-for-subdirs
Reviewer Review Type Date Requested Status
Dimitri John Ledkov Approve
Review via email: mp+154935@code.launchpad.net

Description of the change

* scripts/initctl2dot.py:
  - footer(): Add details of session.
  - sanitise(): Handle jobs in sub-directories.
  - main(): Add --user and --system options and determine correct
    session to connect to.
* scripts/man/initctl2dot.8:
  - Added --user and --system options.
  - Escape dashes in options.
  - Update date.

To post a comment you must log in.
Revision history for this message
Dimitri John Ledkov (xnox) wrote :

On 22 March 2013 12:37, James Hunt <email address hidden> wrote:
> # Map dash to underscore since graphviz node names cannot
> -# contain dashes. Also remove dollars and colons
> +# contain dashes. Also remove dollars and colons and replace other
> +# punctuation with graphiviz-safe names.
> def sanitise(s):
> return s.replace('-', '_').replace('$', 'dollar_') \
> .replace('[', 'lbracket').replace(']', 'rbracket') \
> .replace('!', 'bang').replace(':', 'colon').replace('*', 'star') \
> - .replace('?', 'question').replace('.', '_')
> + .replace('?', 'question').replace('.', '_').replace('/', '_')
>

I wonder if we can use translate instead here:
sanitize_table = str.maketrans({
    '-': '_',
    '$': 'dollar_',
    '[': 'lbracker',
    ']': 'rbracker',
    '!': 'bang',
    ':': 'colon',
    '*': 'star',
    '?': 'question',
    '.': '_',
    '/': '_',
})
return s.translate(sanitize_table)

Somehow this is easier to read & edit =) maybe make that translation
table global, such that we don't create that object each time
sanitized is called.

>
> + parser.add_argument("--user",
> + dest="user",
> + action='store_true',
> + help="Connect to Upstart user session (default if running within a user session).")
> +
> + parser.add_argument("--system",
> + dest="system",
> + action='store_true',
> + help="Connect to Upstart system session.")
> +
> parser.set_defaults(color_emits=default_color_emits,
> color_start_on=default_color_start_on,
> color_stop_on=default_color_stop_on,
> @@ -504,6 +535,25 @@
> if options.restrictions:
> restrictions_list = options.restrictions.split(",")
>
> + upstart_session = os.environ.get('UPSTART_SESSION')
> +

I'd change it to be os.environ.get('UPSTART_SESSION', False), such
that we get explicit False, instead of vague None when it's not
defined.

> + use_system = True
> +
> + if options.system == False and options.user == False:
> + if upstart_session:
> + use_system = False
> + else:
> + use_system = True
> + elif options.system == True:
> + use_system = True
> + elif options.user == True:
> + use_system = False
> +

"if foo == True" is not pythonic, simply use "if foo:"

Further more you are using if comparison to derive and assign a bollean value.
Why not just assign it?

use_system = options.system or not options.user or not upstart_session

Or you can make it easier by defining the option --user to be
dest='use_system', action='store_false' & --system to be
dest='use_system', action='store_true' then you will only need to test
for the environment variable.
(It will give less flexibility in the future, but I don't think these
two options will evolve beyond simple binary choice).

Regards,

Dmitrijs.

Revision history for this message
Dimitri John Ledkov (xnox) :
review: Needs Fixing
1459. By James Hunt

* scripts/initctl2dot.py:
  - sanitise(): Use global translation table.
  - main(): Remove need for options.user.

1460. By James Hunt

* scripts/initctl2dot.py: footer(): Make more pythonic.

1461. By James Hunt

* scripts/initctl2dot.py: sanitise(): Rather important typo fix.

1462. By James Hunt

* scripts/initctl2dot.py: Remove cruft.

Revision history for this message
James Hunt (jamesodhunt) wrote :

Hi Dmitrijs,

Thanks - I've added the translation table and default value for UPSTART_SESSION.

Regarding the option handling, I had started out as you suggest, but ArgumentParser is slightly odd wrt args that use a shared variable. I have now improved the code by adding a default value and following your suggestions as best that allows.

Revision history for this message
Dimitri John Ledkov (xnox) wrote :

Fair enough. We can iterate on it later.

review: Approve

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file 'ChangeLog'
2--- ChangeLog 2013-03-22 10:55:18 +0000
3+++ ChangeLog 2013-03-22 13:57:01 +0000
4@@ -1,3 +1,15 @@
5+2013-03-22 James Hunt <james.hunt@ubuntu.com>
6+
7+ * scripts/initctl2dot.py:
8+ - footer(): Add details of session.
9+ - sanitise(): Handle jobs in sub-directories.
10+ - main(): Add --user and --system options and determine
11+ correct session to connect to.
12+ * scripts/man/initctl2dot.8:
13+ - Added --user and --system options.
14+ - Escape dashes in options.
15+ - Update date.
16+
17 2013-03-21 James Hunt <james.hunt@ubuntu.com>
18
19 * po/POTFILES.in:
20
21=== modified file 'NEWS'
22--- NEWS 2013-03-04 11:57:18 +0000
23+++ NEWS 2013-03-22 13:57:01 +0000
24@@ -1,4 +1,4 @@
25-1.8 xxxx-xx-xx
26+1.8 2013-03-22 "Scone + Radish = Gherkin?"
27
28 1.7 2013-03-04 "We've all got one now"
29
30
31=== modified file 'scripts/initctl2dot.py' (properties changed: -x to +x)
32--- scripts/initctl2dot.py 2012-12-07 18:26:43 +0000
33+++ scripts/initctl2dot.py 2013-03-22 13:57:01 +0000
34@@ -57,12 +57,28 @@
35 options = None
36 jobs = {}
37 events = {}
38-cmd = "initctl --system show-config -e"
39 script_name = os.path.basename(sys.argv[0])
40
41+cmd = None
42+use_system = True
43+upstart_session = None
44+
45 # list of jobs to restict output to
46 restrictions_list = []
47
48+sanitise_table = str.maketrans({
49+ '-': '_',
50+ '$': 'dollar_',
51+ '[': 'lbracker',
52+ ']': 'rbracker',
53+ '!': 'bang',
54+ ':': 'colon',
55+ '*': 'star',
56+ '?': 'question',
57+ '.': 'dot',
58+ '/': 'slash',
59+})
60+
61 default_color_emits = 'green'
62 default_color_start_on = 'blue'
63 default_color_stop_on = 'red'
64@@ -86,10 +102,20 @@
65
66
67 def footer(ofh):
68+ global upstart_session
69+ global use_system
70+
71+ details = ''
72+
73+ if use_system:
74+ details += "\\nfor the system\\n"
75+ else:
76+ details += "\\nfor session '%s'\\n" % upstart_session
77+
78 if options.restrictions:
79- details = "(subset, "
80+ details += "(subset, "
81 else:
82- details = "("
83+ details += "("
84
85 if options.infile:
86 details += "from file data)."
87@@ -109,13 +135,12 @@
88 script_name=script_name, details=details))
89
90
91-# Map dash to underscore since graphviz node names cannot
92-# contain dashes. Also remove dollars and colons
93+# Map punctuation to symbols palatable to graphviz
94+# (which amongst other things dislikes dashes in node names)
95 def sanitise(s):
96- return s.replace('-', '_').replace('$', 'dollar_') \
97- .replace('[', 'lbracket').replace(']', 'rbracket') \
98- .replace('!', 'bang').replace(':', 'colon').replace('*', 'star') \
99- .replace('?', 'question').replace('.', '_')
100+ global sanitise_table
101+
102+ return s.translate(sanitise_table)
103
104
105 # Convert a dollar in @name to a unique-ish new name, based on @job and
106@@ -334,6 +359,10 @@
107
108
109 def read_data():
110+ global cmd
111+ global upstart_session
112+ global use_system
113+
114 if options.infile:
115 try:
116 ifh = open(options.infile, 'r')
117@@ -412,6 +441,9 @@
118 def main():
119 global options
120 global restrictions_list
121+ global cmd
122+ global use_system
123+ global upstart_session
124
125 description = "Convert initctl(8) output to GraphViz dot(1) format."
126 epilog = "See http://www.graphviz.org/doc/info/colors.html " \
127@@ -426,8 +458,8 @@
128
129 parser.add_argument("-f", "--infile",
130 dest="infile",
131- help="File to read '%s' output from. If not specified"
132- ", initctl will be run automatically." % cmd)
133+ help="File to read output from. If not specified"
134+ ", initctl will be run automatically.")
135
136 parser.add_argument("-o", "--outfile",
137 dest="outfile",
138@@ -479,6 +511,18 @@
139 help="Specify color for job boxes (default=%s)." %
140 default_color_job)
141
142+ parser.add_argument("--user",
143+ dest="system",
144+ default=None,
145+ action='store_false',
146+ help="Connect to Upstart user session (default if running within a user session).")
147+
148+ parser.add_argument("--system",
149+ dest="system",
150+ default=None,
151+ action='store_true',
152+ help="Connect to Upstart system session.")
153+
154 parser.set_defaults(color_emits=default_color_emits,
155 color_start_on=default_color_start_on,
156 color_stop_on=default_color_stop_on,
157@@ -504,6 +548,21 @@
158 if options.restrictions:
159 restrictions_list = options.restrictions.split(",")
160
161+ upstart_session = os.environ.get('UPSTART_SESSION', False)
162+
163+ if options.system == None:
164+ if upstart_session:
165+ use_system = False
166+ else:
167+ use_system = True
168+ else:
169+ use_system = options.system or not upstart_session
170+
171+ if use_system:
172+ cmd = "initctl --system show-config -e"
173+ else:
174+ cmd = "initctl show-config -e"
175+
176 read_data()
177
178 for job in restrictions_list:
179
180=== modified file 'scripts/man/initctl2dot.8'
181--- scripts/man/initctl2dot.8 2011-06-01 14:28:20 +0000
182+++ scripts/man/initctl2dot.8 2013-03-22 13:57:01 +0000
183@@ -1,4 +1,4 @@
184-.TH initctl2dot 8 2011-03-07 "Upstart"
185+.TH initctl2dot 8 2013-03-22 "Upstart"
186 .\"
187 .SH NAME
188 initctl2dot \- manual page for initctl2dot
189@@ -17,56 +17,64 @@
190 With no options,
191 .BR initctl (8)
192 will be invoked automatically and the output written to file
193-\fIupstart.dot\fP.
194+\fIupstart.dot\fP. If run from within an Upstart user session, unless
195+.B \-\-system
196+is specified, the data generated will be for the user session.
197 .\"
198 .SH OPTIONS
199 .TP
200-.B -h
201-Display usage statement.
202-.TP
203-\fB-f\fP \fIINFILE\fP , \fP--infile\fP=\fIINFILE\fP
204+\fB\-f\fP \fIINFILE\fP , \fP\-\-infile\fP=\fIINFILE\fP
205 File to read
206 .BR initctl (8)
207 output from ("initctl show-config -e"). If not specified,
208 .BR initctl (8)
209 will be run automatically.
210 .TP
211-\fB-w\fP \fIOUTFILE\fP , \fP--outfile\fP=\fIOUTFILE\fP
212-File to write output to.
213+.B \-h
214+Display usage statement.
215 .TP
216-\fB-r\fP \fIRESTRICTIONS\fP , \fP--restrict-to-jobs\fP=\fIRESTRICTIONS\fP
217+\fB\-r\fP \fIRESTRICTIONS\fP , \fP\-\-restrict-to-jobs\fP=\fIRESTRICTIONS\fP
218 Limit display of
219 .B start on
220 and
221 .B stop on
222 conditions to comma-separated list of jobs.
223 .TP
224-\fB--color-emits\fP=\fICOLOR_EMITS\fP
225+\fB\-w\fP \fIOUTFILE\fP , \fP\-\-outfile\fP=\fIOUTFILE\fP
226+File to write output to.
227+.TP
228+\fB\-\-color-emits\fP=\fICOLOR_EMITS\fP
229 Specify color for 'emits' lines.
230 .TP
231-\fB--color-start-on\fP=\fICOLOR_START_ON\fP
232+\fB\-\-color-start-on\fP=\fICOLOR_START_ON\fP
233 Specify color for 'start on' lines.
234 .TP
235-\fB--color-stop-on\fP=\fICOLOR_STOP_ON\fP
236+\fB\-\-color-stop-on\fP=\fICOLOR_STOP_ON\fP
237 Specify color for 'stop on' lines.
238 .TP
239-\fB--color-event\fP=\fICOLOR_EVENT\fP
240+\fB\-\-color-event\fP=\fICOLOR_EVENT\fP
241 Specify color for event boxes.
242 .TP
243-\fB--color-text\fP=\fICOLOR_TEXT\fP
244+\fB\-\-color-text\fP=\fICOLOR_TEXT\fP
245 Specify color for summary text.
246 .TP
247-\fB--color-bg\fP=\fICOLOR_BG\fP
248+\fB\-\-color-bg\fP=\fICOLOR_BG\fP
249 Specify background color for diagram.
250 .TP
251-\fB--color-event-text\fP=\fICOLOR_EVENT_TEXT\fP
252+\fB\-\-color-event-text\fP=\fICOLOR_EVENT_TEXT\fP
253 Specify color for text in event boxes.
254 .TP
255-\fB--color-job-text\fP=\fICOLOR_JOB_TEXT\fP
256+\fB\-\-color-job-text\fP=\fICOLOR_JOB_TEXT\fP
257 Specify color for text in job boxes.
258 .TP
259-\fB--color-job\fP=\fICOLOR_JOB\fP
260+\fB\-\-color-job\fP=\fICOLOR_JOB\fP
261 Specify color for job boxes.
262+.TP
263+\fB\-\-system\fP
264+Connect to the Upstart system session.
265+.TP
266+\fB\-\-user\fP
267+Connect to the Upstart user session.
268 .\"
269 .SH AUTHOR
270 Written by James Hunt

Subscribers

People subscribed via source and target branches