Merge lp:~tonyyarusso/update-notifier/bug-1167621 into lp:update-notifier/ubuntu

Proposed by Tony Yarusso
Status: Work in progress
Proposed branch: lp:~tonyyarusso/update-notifier/bug-1167621
Merge into: lp:update-notifier/ubuntu
Diff against target: 146 lines (+65/-41)
1 file modified
data/apt_check.py (+65/-41)
To merge this branch: bzr merge lp:~tonyyarusso/update-notifier/bug-1167621
Reviewer Review Type Date Requested Status
Martin Pitt Needs Fixing
Ubuntu Core Development Team Pending
Review via email: mp+158500@code.launchpad.net

Description of the change

Make apt_check.py more suitable for calling as a module from another script (LP: #1167621)

To post a comment you must log in.
Revision history for this message
Iain Lane (laney) wrote :

Hey pitti, looks like this is up your street - fancy reviewing? :-)

(if not, let me know and I'll try to reason it out myself)

Revision history for this message
Martin Pitt (pitti) wrote :

8 -def write_package_names(outstream, cache, depcache):
9 +def write_package_names(cache, depcache, messages, standalone, delim="\n"):
10 " write out package names that change to outstream "

Can you please adjust the docstrings accordingly? Also, you should rename the methods, as they don't write anything any more. Perhaps get_package_names()? Also, it seems this function doesn't actually use the new "standalone" argument?

For write_human_readable_summary, please also document the two new arguments in the docstring. I think it's an unusual style to pass in "messages" and have the function return that plus some extra text; instead, concatenate the result of the two functions in the caller.

64 + "--delimeter",

It's "delimiter" (same typo again further down).

Thanks!

review: Needs Fixing
Revision history for this message
Sebastien Bacher (seb128) wrote :

(setting to "work in progress" so it drops from the sponsoring queue while being worked, please set it back to "needs review" again once you addressed the review comments)

Unmerged revisions

790. By Tony Yarusso <email address hidden>

Make apt_check.py more suitable for calling as a module from another script (LP: #1167621)

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
=== modified file 'data/apt_check.py'
--- data/apt_check.py 2012-10-16 13:45:38 +0000
+++ data/apt_check.py 2013-04-11 21:50:27 +0000
@@ -48,25 +48,57 @@
48 return True48 return True
49 return False49 return False
5050
51def write_package_names(outstream, cache, depcache):51def write_package_names(cache, depcache, messages, standalone, delim="\n"):
52 " write out package names that change to outstream "52 " write out package names that change to outstream "
53 pkgs = filter(lambda pkg:53 pkgs = filter(lambda pkg:
54 depcache.marked_install(pkg) or depcache.marked_upgrade(pkg),54 depcache.marked_install(pkg) or depcache.marked_upgrade(pkg),
55 cache.packages)55 cache.packages)
56 outstream.write("\n".join(map(lambda p: p.name, pkgs)))56 messages += delim.join(map(lambda p: p.name, pkgs)) + "\n"
57 return messages
5758
58def write_human_readable_summary(outstream, upgrades, security_updates):59def write_human_readable_summary(upgrades, security_updates, messages, standalone):
59 " write out human summary summary to outstream "60 " write out human summary summary to outstream "
60 outstream.write(gettext.dngettext("update-notifier",61 messages += gettext.dngettext("update-notifier",
61 "%i package can be updated.",62 "%i package can be updated.",
62 "%i packages can be updated.",63 "%i packages can be updated.",
63 upgrades) % upgrades)64 upgrades) % upgrades
64 outstream.write("\n")65 if standalone:
65 outstream.write(gettext.dngettext("update-notifier",66 messages += "\n"
66 "%i update is a security update.",67 else:
67 "%i updates are security updates.",68 messages += " "
68 security_updates) % security_updates)69 messages += gettext.dngettext("update-notifier",
69 outstream.write("\n")70 "%i update is a security update.",
71 "%i updates are security updates.",
72 security_updates) % security_updates
73 messages += "\n"
74 return messages
75
76def get_options(args):
77 parser = OptionParser()
78 parser.add_option("-p",
79 "--package-names",
80 action="store_true",
81 dest="show_package_names",
82 help=_("Show the packages that are going to be installed/upgraded"))
83 parser.add_option("",
84 "--human-readable",
85 action="store_true",
86 dest="readable_output",
87 help=_("Show human readable output on stdout"))
88 parser.add_option("",
89 "--security-updates-unattended",
90 action="store_true",
91 help=_("Return the time in days when security updates "
92 "are installed unattended (0 means disabled)"))
93 parser.add_option("-d",
94 "--delimeter",
95 action="store",
96 type="string",
97 dest="pkg_delim",
98 help=_("Set delimeter to use between package names when used "
99 "with the -p option. Default is a newline."))
100 return parser.parse_args(args)
101
70def init():102def init():
71 " init the system, be nice "103 " init the system, be nice "
72 # FIXME: do a ionice here too?104 # FIXME: do a ionice here too?
@@ -78,7 +110,9 @@
78 apt_pkg.config.set("Dir::Cache::pkgcache","")110 apt_pkg.config.set("Dir::Cache::pkgcache","")
79 apt_pkg.config.set("Dir::Cache::srcpkgcache","")111 apt_pkg.config.set("Dir::Cache::srcpkgcache","")
80 112
81def run(options=None):113def run(options=None, standalone=False):
114
115 messages = ""
82116
83 # we are run in "are security updates installed automatically?"117 # we are run in "are security updates installed automatically?"
84 # question mode118 # question mode
@@ -146,18 +180,23 @@
146 break180 break
147181
148 # print the number of upgrades182 # print the number of upgrades
183
184 if options and options.readable_output:
185 messages = write_human_readable_summary(upgrades, security_updates, messages, standalone)
186
149 if options and options.show_package_names:187 if options and options.show_package_names:
150 write_package_names(sys.stderr, cache, depcache)188 if options.pkg_delim:
151 elif options and options.readable_output:189 messages = write_package_names(cache, depcache, messages, standalone, options.pkg_delim)
152 write_human_readable_summary(sys.stdout, upgrades, security_updates)190 else:
153 else:191 messages = write_package_names(cache, depcache, messages, standalone)
192
193 if (not options or (options and not (options.readable_output or options.show_package_names))) and standalone:
154 # print the number of regular upgrades and the number of 194 # print the number of regular upgrades and the number of
155 # security upgrades195 # security upgrades
156 sys.stderr.write("%s;%s" % (upgrades,security_updates))196 messages = str(upgrades) + ";" + str(security_updates)
157197
158 # return the number of upgrades (if its used as a module)198 # return the number of upgrades
159 return(upgrades,security_updates)199 return (upgrades, security_updates, messages)
160
161200
162if __name__ == "__main__": 201if __name__ == "__main__":
163 # setup a exception handler to make sure that uncaught stuff goes202 # setup a exception handler to make sure that uncaught stuff goes
@@ -171,24 +210,9 @@
171 gettext.textdomain(APP)210 gettext.textdomain(APP)
172211
173 # check arguments212 # check arguments
174 parser = OptionParser()213 (options, args) = get_options(sys.argv[1:])
175 parser.add_option("-p",
176 "--package-names",
177 action="store_true",
178 dest="show_package_names",
179 help=_("Show the packages that are going to be installed/upgraded"))
180 parser.add_option("",
181 "--human-readable",
182 action="store_true",
183 dest="readable_output",
184 help=_("Show human readable output on stdout"))
185 parser.add_option("",
186 "--security-updates-unattended",
187 action="store_true",
188 help=_("Return the time in days when security updates "
189 "are installed unattended (0 means disabled)"))
190 (options, args) = parser.parse_args()
191214
192 # run it215 # run it
193 init()216 init()
194 run(options)217 result = run(options, True)[2]
218 sys.stdout.write(result)

Subscribers

People subscribed via source and target branches

to all changes: