Merge lp:~stgraber/upstart/upstart-initctl2dot-python3 into lp:upstart
| Status: | Merged | ||||
|---|---|---|---|---|---|
| Merged at revision: | 1392 | ||||
| Proposed branch: | lp:~stgraber/upstart/upstart-initctl2dot-python3 | ||||
| Merge into: | lp:upstart | ||||
| Diff against target: |
917 lines (+386/-436) 1 file modified
scripts/initctl2dot.py (+386/-436) |
||||
| To merge this branch: | bzr merge lp:~stgraber/upstart/upstart-initctl2dot-python3 | ||||
| Related bugs: |
|
| Reviewer | Review Type | Date Requested | Status |
|---|---|---|---|
| James Hunt | 2012-11-28 | Approve on 2012-11-30 | |
| Barry Warsaw (community) | 2012-11-28 | Approve on 2012-11-29 | |
|
Review via email:
|
|||
Description of the Change
This branch updates initctl2dot to work with both python2 and python3.
The tested versions were python2.7 and python3.3.
The main changes are:
- Fixing a bunch of errors spotted by pyflakes.
- Added universal_
- Stop using string.split, instead split use <str>.split
- Re-indent the code, split lines, ... to please pep8
I also fixed a small bug where if a job name contained a dot, the .dot file
would be invalid. I'm simply replacing dots by underscores which appears to do
the trick.
The code should now be PEP-8 clean and the output appears to work as expected
when fed to graphviz.
| Steve Langasek (vorlon) wrote : | # |
- 1396. By Stéphane Graber on 2012-11-28
-
Switch interpreter to python3 (code still works under python2)
| Stéphane Graber (stgraber) wrote : | # |
> On Wed, Nov 28, 2012 at 05:10:25PM -0000, Stéphane Graber wrote:
> > === modified file 'scripts/
> > --- scripts/
> > +++ scripts/
> > @@ -50,522 +50,530 @@
> > import re
> > import fnmatch
> > import os
> > -from string import split
> > import datetime
> > from subprocess import (Popen, PIPE)
> > from optparse import OptionParser
>
> Shouldn't this also change the interpreter to /usr/bin/python3? I don't see
> any reason why we would need this script to be bilingual, we ought to just
> change it and go. If people want to use newer versions of upstart on OSes
> that don't have python3, then they just won't be able to use this script...
> but this is hardly a requirement anyway.
Oops, even though I named the branch -python3 I completely forgot to change the default interpreter to python3 :)
Done now.
> (Also, looking at this I notice we're shipping initctl2dot in /bin in the
> Ubuntu package... that doesn't make much sense when the interpreter is in
> /usr/bin.)
Good point, we should change the packaging to put the script in /usr/bin.
| Barry Warsaw (barry) wrote : | # |
Just a couple more suggestions, since why not.
* No parens are needed in `from subprocess import Popen, PIPE`
* How about switching to argparse instead of optparse?
* In header(), the global statement isn't necessary because you're not assigning to options
* I'd probably rewrite the concats in header() into a triple-quoted multiline string, with {options.foo} replacements
* Remove the global statement from footer()
* There should be some better way to get rid of the multiple concats in footer() too
* sanitize() seems pretty inefficient. maybe that doesn't matter for this script, but it might be better written with a re.sub() where `repl` is a function that knows the mappings
* Why does mk_node_name() even exist? ;)
* show_events(): remove global and rewrite the concats
* Remove the globals from show_events()
* Remove the global in show_jobs()
* Remove the globals in show_jobs()
* In show_jobs(), the `if not restrictions_list: return` is kind of unnecessary.
* None of the show_*() methods need their globals either (just keep at it for the rest of this file ;) - you only need globals if you're *assigning* to a global variable (not if just referencing it, or calling methods like .append() on it)
* read_data(): `for line in ifh:` is probably good enough
* Might want to use a context manager to handle ifh in read_data(). For Python 3.3, see http://
* line 430: extra parens
Probably other stuff to be cleaned up, but those are the major things that jump out at me. I can take a crack at it if you want.
- 1397. By Stéphane Graber on 2012-11-29
-
Cleanup subprocess import and use argparse instead of optparse
- 1398. By Stéphane Graber on 2012-11-29
-
Drop all unneeded global statements and do some extra code simplification.
- 1399. By Stéphane Graber on 2012-11-29
-
Remove some more globals and unused variable
- 1400. By Stéphane Graber on 2012-11-29
-
Simplify header and footer functions
- 1401. By Stéphane Graber on 2012-11-29
-
Fix indent
- 1402. By Stéphane Graber on 2012-11-29
-
Move the code replacing dots by underscores to the sanitise function
| Stéphane Graber (stgraber) wrote : | # |
> Just a couple more suggestions, since why not.
>
> * No parens are needed in `from subprocess import Popen, PIPE`
Done
> * How about switching to argparse instead of optparse?
Done
> * In header(), the global statement isn't necessary because you're not
> assigning to options
Done
> * I'd probably rewrite the concats in header() into a triple-quoted multiline
> string, with {options.foo} replacements
Done
> * Remove the global statement from footer()
Done
> * There should be some better way to get rid of the multiple concats in
> footer() too
Done. Pre-processed the ifs, then used a giant string with format, similar to header()
> * sanitize() seems pretty inefficient. maybe that doesn't matter for this
> script, but it might be better written with a re.sub() where `repl` is a
> function that knows the mappings
Kept it as-is for now as I'm not a fan of using regexps when not absolutely necessary :)
> * Why does mk_node_name() even exist? ;)
No good reason apparently, dropped.
> * show_events(): remove global and rewrite the concats
Done.
> * Remove the globals from show_events()
Done
> * Remove the global in show_jobs()
Done
> * Remove the globals in show_jobs()
Done
> * In show_jobs(), the `if not restrictions_list: return` is kind of
> unnecessary.
Right, at first read, I assumed that restrictions_list could be None, but rechecking the code, that's not actually the case, so iterating will always work and that if statement is unnecessary. Dropped.
> * None of the show_*() methods need their globals either (just keep at it for
> the rest of this file ;) - you only need globals if you're *assigning* to a
> global variable (not if just referencing it, or calling methods like .append()
> on it)
Done
> * read_data(): `for line in ifh:` is probably good enough
Agreed, done.
> * Might want to use a context manager to handle ifh in read_data(). For Python
> 3.3, see http://
I'd rather not depend on python 3.3. Some people may still want to use the fixed script on something slightly older, but that's good to know nevertheless.
> * line 430: extra parens
Fixed that one and another one a few lines further down.
> Probably other stuff to be cleaned up, but those are the major things that
> jump out at me. I can take a crack at it if you want.
As far as I can tell, the script still works with those changes and the output is similar.
| Barry Warsaw (barry) wrote : | # |
Much improved, thanks! Just a few more to go:
* Remove global jobs, events from read_data()
* Remove global jobs, cmd, default_color_* from main(), but keep global
options and restrictions_list.
I'll go ahead and approve the merge on the above conditions.
- 1403. By Stéphane Graber on 2012-11-29
-
Remove last useless globals, fix .dot file syntax in footer, fix indentation of output file.
| Stéphane Graber (stgraber) wrote : | # |
Thanks for the comments. I removed those last few globals and fixed a mistake I did in the output file so that it's now valid. While I was at it, I also tweaked the code a bit so that the output file's indentation is correct.
| James Hunt (jamesodhunt) wrote : | # |
Thanks Stéphane. I found a new bug in testing (bug 1084985), but that is unrelated to the changes you've made, so merged.


On Wed, Nov 28, 2012 at 05:10:25PM -0000, Stéphane Graber wrote: initctl2dot. py' initctl2dot. py 2011-06-06 12:52:08 +0000 initctl2dot. py 2012-11-28 17:09:22 +0000
> === modified file 'scripts/
> --- scripts/
> +++ scripts/
> @@ -50,522 +50,530 @@
> import re
> import fnmatch
> import os
> -from string import split
> import datetime
> from subprocess import (Popen, PIPE)
> from optparse import OptionParser
Shouldn't this also change the interpreter to /usr/bin/python3? I don't see
any reason why we would need this script to be bilingual, we ought to just
change it and go. If people want to use newer versions of upstart on OSes
that don't have python3, then they just won't be able to use this script...
but this is hardly a requirement anyway.
(Also, looking at this I notice we're shipping initctl2dot in /bin in the
Ubuntu package... that doesn't make much sense when the interpreter is in
/usr/bin.)