Merge lp:~javier.collado/utah/bug1025220 into lp:utah

Proposed by Javier Collado
Status: Merged
Merged at revision: 456
Proposed branch: lp:~javier.collado/utah/bug1025220
Merge into: lp:utah
Diff against target: 137 lines (+38/-36)
3 files modified
examples/run_utah_tests.py (+7/-28)
utah/provisioning/provisioning.py (+15/-8)
utah/url.py (+16/-0)
To merge this branch: bzr merge lp:~javier.collado/utah/bug1025220
Reviewer Review Type Date Requested Status
Max Brustkern (community) Approve
Review via email: mp+115355@code.launchpad.net

Description of the change

Initially I moved the URL checking code to the loop in utah/provisioning/provisioning.py to make sure that all arguments that expect a URL are properly checked.

However, after that, I thought that it would make more sense to use the `type` argument in argparse.ArgumentParser.add_argument to mark each of those arguments as expecting a URL and perform the validation right away.

The validation code still lies in utah.url, but now it's called by argparse when parsing the arguments.

To post a comment you must log in.
Revision history for this message
Max Brustkern (nuclearbob) wrote :

Good idea. We can easily leverage that in future classes as well.

review: Approve

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file 'examples/run_utah_tests.py'
2--- examples/run_utah_tests.py 2012-07-16 14:45:15 +0000
3+++ examples/run_utah_tests.py 2012-07-17 14:31:55 +0000
4@@ -4,22 +4,22 @@
5 import sys
6
7 from utah.exceptions import URLNotFound, URLNotReadable
8-from utah.url import URLChecker
9+from utah.url import url_argument
10 from utah.group import check_user_group, print_group_error_message
11 from run_test_vm import run_test_vm
12
13
14 def get_parser():
15 parser = argparse.ArgumentParser(description='Provision a machine and run one or more UTAH runlists there.', epilog="For example:\n\t%(prog)s -s oneiric -t server -a i386 /usr/share/utah/client/examples/master.run 'http://people.canonical.com/~max/max_test.run'", formatter_class=argparse.RawDescriptionHelpFormatter)
16- parser.add_argument('runlists', metavar='runlist', nargs='+', help='URLs of runlist files to run')
17+ parser.add_argument('runlists', metavar='runlist', nargs='+', type=url_argument, help='URLs of runlist files to run')
18 parser.add_argument('-m', '--machinetype', choices=('physical', 'virtual'), default='virtual', help='Type of machine to provision')
19 parser.add_argument('-e', '--emulator', help='Emulator to use (kvm and qemu are supported, kvm will be favored if available)')
20- parser.add_argument('-i', '--image', help='Image/ISO file to use for installation')
21- parser.add_argument('-k', '--kernel', help='Kernel file to use for installation')
22- parser.add_argument('-r', '--initrd', help='InitRD file to use for installation')
23- parser.add_argument('-p', '--preseed', help='Preseed file to use for installation')
24+ parser.add_argument('-i', '--image', type=url_argument, help='Image/ISO file to use for installation')
25+ parser.add_argument('-k', '--kernel', type=url_argument, help='Kernel file to use for installation')
26+ parser.add_argument('-r', '--initrd', type=url_argument, help='InitRD file to use for installation')
27+ parser.add_argument('-p', '--preseed', type=url_argument, help='Preseed file to use for installation')
28 parser.add_argument('-b', '--boot', help='Boot arguments for initial installation')
29- parser.add_argument('-x', '--xml', help='XML VM definition file')
30+ parser.add_argument('-x', '--xml', type=url_argument, help='XML VM definition file')
31 parser.add_argument('-g', '--gigabytes', action='append', help='Size in gigabytes of virtual disk, specify more than once for multiple disks')
32 parser.add_argument('-s', '--series', choices=('hardy', 'lucid', 'natty', 'oneiric', 'precise', 'quantal'), help='Series to use for installation')
33 parser.add_argument('-t', '--type', choices=('desktop', 'server', 'mini', 'alternate'), help='Install type to use for installation')
34@@ -49,27 +49,6 @@
35 sys.stderr.write("Please check the roadmap, as it will be included in a future version.\n")
36 sys.exit(4)
37
38- url_checker = URLChecker()
39- if args.image is not None:
40- try:
41- url_checker.open(args.image)
42- except URLNotFound:
43- sys.stderr.write("ISO image not found: " + args.image + "\n")
44- sys.exit(5)
45- except URLNotReadable:
46- sys.stderr.write("ISO image not readable: " + args.image + "\n")
47- sys.exit(5)
48-
49- for runlist in args.runlists:
50- try:
51- url_checker.open(runlist)
52- except URLNotFound:
53- sys.stderr.write("Run list not found: " + runlist + "\n")
54- sys.exit(5)
55- except URLNotReadable:
56- sys.stderr.write("Run list not readable: " + runlist + "\n")
57- sys.exit(5)
58-
59 function = run_test_vm
60
61 for option in [args.boot, args.emulator, args.image, args.initrd, args.kernel, args.preseed, args.xml]:
62
63=== modified file 'utah/provisioning/provisioning.py'
64--- utah/provisioning/provisioning.py 2012-07-16 21:50:15 +0000
65+++ utah/provisioning/provisioning.py 2012-07-17 14:31:55 +0000
66@@ -15,6 +15,7 @@
67
68 from utah.provisioning.exceptions import *
69 from utah import config, iso
70+from utah import UTAHException, URLNotFound, URLNotReadable
71
72 class Machine(object):
73 """
74@@ -73,18 +74,24 @@
75 self._loggersetup()
76
77 for item in ['preseed', 'xml', 'kernel', 'initrd', 'image']:
78- if locals()[item] is None:
79- self.__dict__[item] = None
80+ arg = locals()[item]
81+ if arg is None:
82+ setattr(self, item, None)
83 else:
84- if '~' in locals()[item]:
85- path = os.path.expanduser(locals()[item])
86- self.logger.debug('Rewriting ~-based path: ' + locals()[item] + ' to: ' + path)
87+ if arg.startswith('~'):
88+ path = os.path.expanduser(arg)
89+ self.logger.debug('Rewriting ~-based path: ' + arg
90+ + ' to: ' + path)
91 else:
92- path = locals()[item]
93+ path = arg
94+
95 self.percent = 0
96 self.logger.info('Preparing ' + item + ': ' + path)
97- self.__dict__[item] = urllib.urlretrieve(path, reporthook=self.dldisplay)[0]
98- self.logger.debug(path + ' is locally available as ' + self.__dict__[item])
99+ filename = urllib.urlretrieve(path,
100+ reporthook=self.dldisplay)[0]
101+ setattr(self, item, filename)
102+ self.logger.debug(path + ' is locally available as '
103+ + getattr(self, item))
104
105 self.logger.debug('Machine init finished')
106
107
108=== modified file 'utah/url.py'
109--- utah/url.py 2012-07-13 10:12:53 +0000
110+++ utah/url.py 2012-07-17 14:31:55 +0000
111@@ -4,6 +4,7 @@
112 import os
113 import urllib
114 import urllib2
115+from argparse import ArgumentTypeError
116
117 from utah.exceptions import URLNotFound, URLNotReadable
118
119@@ -57,3 +58,18 @@
120 raise URLNotReadable(path)
121
122 return True
123+
124+
125+def url_argument(url):
126+ """
127+ Check URL argument in argparse.ArgumentParser
128+ """
129+ url_checker = URLChecker()
130+ try:
131+ url_checker.open(url)
132+ except URLNotFound:
133+ raise ArgumentTypeError('URL not found: {0}'.format(url))
134+ except URLNotReadable:
135+ raise ArgumentTypeError('URL not readable: {0}'.format(url))
136+
137+ return url

Subscribers

People subscribed via source and target branches