Merge lp:~jml/txpkgme/no-more-optparse into lp:txpkgme

Proposed by Jonathan Lange
Status: Merged
Approved by: James Westby
Approved revision: 48
Merged at revision: 48
Proposed branch: lp:~jml/txpkgme/no-more-optparse
Merge into: lp:txpkgme
Diff against target: 56 lines (+14/-28)
1 file modified
txpkgme/check_submit.py (+14/-28)
To merge this branch: bzr merge lp:~jml/txpkgme/no-more-optparse
Reviewer Review Type Date Requested Status
James Westby (community) Approve
Review via email: mp+132058@code.launchpad.net

Commit message

Use argparse rather than optparse

Description of the change

Simple branch that uses argparse rather than optparse, sparing us a little
bit of logic.

To post a comment you must log in.
Revision history for this message
James Westby (james-w) :
review: Approve

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file 'txpkgme/check_submit.py'
2--- txpkgme/check_submit.py 2012-08-23 17:11:02 +0000
3+++ txpkgme/check_submit.py 2012-10-30 11:05:29 +0000
4@@ -1,5 +1,4 @@
5-import optparse
6-import os
7+import argparse
8 import sys
9
10 from txpkgme.submitfromdisk import check_saved_output
11@@ -10,32 +9,19 @@
12 # Write to stdout
13 # One line only
14 # returncodes: 0 == OK, 1 == WARN, 2 == CRIT
15- parser = optparse.OptionParser()
16- parser.add_option('--last-run-grace', dest="last_run_grace",
17- help="Maximum number of seconds since the last run")
18- parser.add_option('--warn-duration', dest="warn_duration",
19- help="Generate a warning if the last run took longer than this.")
20- opts, args = parser.parse_args()
21- if len(args) != 1:
22- sys.stdout.write("You must specify filename that the check outputs too.\n")
23- return 2
24- if not opts.last_run_grace:
25- sys.stdout.write("You must specify --last-run-grace.\n")
26- return 2
27- if not opts.warn_duration:
28- sys.stdout.write("You must specify --warn-duration.\n")
29- return 2
30- last_run_grace = int(opts.last_run_grace)
31- warn_duration = int(opts.warn_duration)
32- filename = args[0]
33- if not os.path.exists(filename):
34- sys.stdout.write(
35- "No such file or directory: %s, has submit-for-packaging ever run?"
36- % (filename,))
37- return 2
38- with open(filename) as f:
39- output = f.read()
40+ parser = argparse.ArgumentParser()
41+ parser.add_argument(
42+ '--last-run-grace', dest="last_run_grace", type=int, required=True,
43+ help="Maximum number of seconds since the last run")
44+ parser.add_argument(
45+ '--warn-duration', dest="warn_duration", type=int, required=True,
46+ help="Generate a warning if the last run took longer than this.")
47+ parser.add_argument(
48+ 'output_file', type=argparse.FileType('r'),
49+ help="The filename that the check outputs to.")
50+ args = parser.parse_args()
51+ output = args.output_file.read()
52 result, message = check_saved_output(
53- output, last_run_grace, warn_duration, filename)
54+ output, args.last_run_grace, args.warn_duration, args.output_file.name)
55 sys.stdout.write(message + '\n')
56 return result

Subscribers

People subscribed via source and target branches