Merge ~guoqiao/juju-lint:argparse into juju-lint:master

Proposed by Joe Guo
Status: Rejected
Rejected by: James Hebden
Proposed branch: ~guoqiao/juju-lint:argparse
Merge into: juju-lint:master
Diff against target: 77 lines (+22/-23)
1 file modified
jujulint.py (+22/-23)
Reviewer Review Type Date Requested Status
James Hebden (community) Disapprove
Tom Haddon Abstain
Diko Parvanov Approve
Jeremy Lounder Pending
Review via email: mp+377981@code.launchpad.net

Commit message

jujulint.py: change optparse to argparse and improve

- optparse is deprecated by argparse, replace
- original usage info doesn't include positional args, remove and use argparse auto-generated one
- be explicit that input files should be juju status file other than bundle file
- be explicit that input files can be yaml or json(since json is subset of yaml and PyYAML can load json)
- use FileType for input files so argprarse will ensure files exist and open them
- show default value automatically in help

To post a comment you must log in.
Revision history for this message
🤖 Canonical IS Merge Bot (canonical-is-mergebot) wrote :

This merge proposal is being monitored by mergebot. Change the status to Approved to merge.

Revision history for this message
Diko Parvanov (dparv) wrote :

lgtm +1

review: Approve
Revision history for this message
Tom Haddon (mthaddon) wrote :

Leaving for other reviewers more actively involved in this project

review: Abstain
Revision history for this message
James Hebden (ec0) wrote :

Closing this out, as the configuration and argument parsing has been totally refactored.

review: Disapprove

Unmerged commits

4194653... by Joe Guo

jujulint.py: change optparse to argparse and improve

- optparse is deprecated by argparse, replace
- original usage info doesn't include positional args, remove and use argparse auto-generated one
- be explicit that input files should be juju status file other than bundle file
- be explicit that input files can be yaml or json(since json is subset of yaml and PyYAML can load json)
- use FileType for input files so argprarse will ensure files exist and open them
- show default value automatically in help

Signed-off-by: Joe Guo <email address hidden>

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1diff --git a/jujulint.py b/jujulint.py
2index 47806f3..cf22439 100755
3--- a/jujulint.py
4+++ b/jujulint.py
5@@ -20,7 +20,7 @@
6
7 import collections
8 import logging
9-import optparse
10+import argparse
11 import pprint
12 import re
13 import sys
14@@ -393,11 +393,10 @@ def check_azs(applications, model):
15 model.az_unbalanced_apps[app_name] = [num_units, az_counter]
16
17
18-def lint(filename, lint_rules):
19+def lint(juju_status_file, lint_rules):
20 model = ModelInfo()
21
22- with open(filename, 'r') as infile:
23- j = yaml.safe_load(infile.read())
24+ j = yaml.safe_load(juju_status_file.read())
25
26 # Handle Juju 2 vs Juju 1
27 applications = "applications"
28@@ -427,30 +426,30 @@ def lint(filename, lint_rules):
29
30 def init():
31 """Initalization, including parsing of options."""
32-
33- usage = """usage: %prog [OPTIONS]
34- Sanity check a Juju model"""
35- parser = optparse.OptionParser(usage)
36- parser.add_option("-c", "--config", default="lint-rules.yaml",
37- help="File to read lint rules from. Defaults to `lint-rules.yaml`")
38- parser.add_option("-o", "--override-subordinate",
39- dest="override",
40- help="override lint-rules.yaml, e.g. -o canonical-livepatch:all")
41- parser.add_option("--loglevel", "-l", default='INFO',
42- help="Log level. Defaults to INFO")
43- parser.add_option("--logfile", "-L", default=None,
44- help="File to log to in addition to stdout")
45- (options, args) = parser.parse_args()
46-
47- return (options, args)
48+ parser = argparse.ArgumentParser(
49+ description='Sanity check a Juju model',
50+ formatter_class=argparse.ArgumentDefaultsHelpFormatter)
51+ parser.add_argument("-c", "--config", default="lint-rules.yaml",
52+ help="File to read lint rules from")
53+ parser.add_argument("-o", "--override-subordinate", dest="override",
54+ help="override lint-rules.yaml, e.g. -o canonical-livepatch:all")
55+ parser.add_argument("-l", "--loglevel", default='INFO',
56+ help="Log level")
57+ parser.add_argument("-L", "--logfile",
58+ help="File to log to in addition to stdout")
59+ parser.add_argument("juju_status_files",
60+ metavar='JUJU_STATUS_FILE',
61+ type=argparse.FileType(), nargs='+',
62+ help="juju status file, yaml or json")
63+ return parser.parse_args()
64
65
66 def main():
67- (options, args) = init()
68+ options = init()
69 setup_logging(options.loglevel, options.logfile)
70 lint_rules = read_rules(options)
71- for filename in args:
72- lint(filename, lint_rules)
73+ for juju_status_file in options.juju_status_files:
74+ lint(juju_status_file, lint_rules)
75
76
77 if __name__ == "__main__":

Subscribers

People subscribed via source and target branches

to all changes: