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
=== modified file 'examples/run_utah_tests.py'
--- examples/run_utah_tests.py 2012-07-16 14:45:15 +0000
+++ examples/run_utah_tests.py 2012-07-17 14:31:55 +0000
@@ -4,22 +4,22 @@
4import sys4import sys
55
6from utah.exceptions import URLNotFound, URLNotReadable6from utah.exceptions import URLNotFound, URLNotReadable
7from utah.url import URLChecker7from utah.url import url_argument
8from utah.group import check_user_group, print_group_error_message8from utah.group import check_user_group, print_group_error_message
9from run_test_vm import run_test_vm9from run_test_vm import run_test_vm
1010
1111
12def get_parser():12def get_parser():
13 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)13 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)
14 parser.add_argument('runlists', metavar='runlist', nargs='+', help='URLs of runlist files to run')14 parser.add_argument('runlists', metavar='runlist', nargs='+', type=url_argument, help='URLs of runlist files to run')
15 parser.add_argument('-m', '--machinetype', choices=('physical', 'virtual'), default='virtual', help='Type of machine to provision')15 parser.add_argument('-m', '--machinetype', choices=('physical', 'virtual'), default='virtual', help='Type of machine to provision')
16 parser.add_argument('-e', '--emulator', help='Emulator to use (kvm and qemu are supported, kvm will be favored if available)')16 parser.add_argument('-e', '--emulator', help='Emulator to use (kvm and qemu are supported, kvm will be favored if available)')
17 parser.add_argument('-i', '--image', help='Image/ISO file to use for installation')17 parser.add_argument('-i', '--image', type=url_argument, help='Image/ISO file to use for installation')
18 parser.add_argument('-k', '--kernel', help='Kernel file to use for installation')18 parser.add_argument('-k', '--kernel', type=url_argument, help='Kernel file to use for installation')
19 parser.add_argument('-r', '--initrd', help='InitRD file to use for installation')19 parser.add_argument('-r', '--initrd', type=url_argument, help='InitRD file to use for installation')
20 parser.add_argument('-p', '--preseed', help='Preseed file to use for installation')20 parser.add_argument('-p', '--preseed', type=url_argument, help='Preseed file to use for installation')
21 parser.add_argument('-b', '--boot', help='Boot arguments for initial installation')21 parser.add_argument('-b', '--boot', help='Boot arguments for initial installation')
22 parser.add_argument('-x', '--xml', help='XML VM definition file')22 parser.add_argument('-x', '--xml', type=url_argument, help='XML VM definition file')
23 parser.add_argument('-g', '--gigabytes', action='append', help='Size in gigabytes of virtual disk, specify more than once for multiple disks')23 parser.add_argument('-g', '--gigabytes', action='append', help='Size in gigabytes of virtual disk, specify more than once for multiple disks')
24 parser.add_argument('-s', '--series', choices=('hardy', 'lucid', 'natty', 'oneiric', 'precise', 'quantal'), help='Series to use for installation')24 parser.add_argument('-s', '--series', choices=('hardy', 'lucid', 'natty', 'oneiric', 'precise', 'quantal'), help='Series to use for installation')
25 parser.add_argument('-t', '--type', choices=('desktop', 'server', 'mini', 'alternate'), help='Install type to use for installation')25 parser.add_argument('-t', '--type', choices=('desktop', 'server', 'mini', 'alternate'), help='Install type to use for installation')
@@ -49,27 +49,6 @@
49 sys.stderr.write("Please check the roadmap, as it will be included in a future version.\n")49 sys.stderr.write("Please check the roadmap, as it will be included in a future version.\n")
50 sys.exit(4)50 sys.exit(4)
5151
52 url_checker = URLChecker()
53 if args.image is not None:
54 try:
55 url_checker.open(args.image)
56 except URLNotFound:
57 sys.stderr.write("ISO image not found: " + args.image + "\n")
58 sys.exit(5)
59 except URLNotReadable:
60 sys.stderr.write("ISO image not readable: " + args.image + "\n")
61 sys.exit(5)
62
63 for runlist in args.runlists:
64 try:
65 url_checker.open(runlist)
66 except URLNotFound:
67 sys.stderr.write("Run list not found: " + runlist + "\n")
68 sys.exit(5)
69 except URLNotReadable:
70 sys.stderr.write("Run list not readable: " + runlist + "\n")
71 sys.exit(5)
72
73 function = run_test_vm52 function = run_test_vm
7453
75 for option in [args.boot, args.emulator, args.image, args.initrd, args.kernel, args.preseed, args.xml]:54 for option in [args.boot, args.emulator, args.image, args.initrd, args.kernel, args.preseed, args.xml]:
7655
=== modified file 'utah/provisioning/provisioning.py'
--- utah/provisioning/provisioning.py 2012-07-16 21:50:15 +0000
+++ utah/provisioning/provisioning.py 2012-07-17 14:31:55 +0000
@@ -15,6 +15,7 @@
1515
16from utah.provisioning.exceptions import *16from utah.provisioning.exceptions import *
17from utah import config, iso17from utah import config, iso
18from utah import UTAHException, URLNotFound, URLNotReadable
1819
19class Machine(object):20class Machine(object):
20 """21 """
@@ -73,18 +74,24 @@
73 self._loggersetup()74 self._loggersetup()
7475
75 for item in ['preseed', 'xml', 'kernel', 'initrd', 'image']:76 for item in ['preseed', 'xml', 'kernel', 'initrd', 'image']:
76 if locals()[item] is None:77 arg = locals()[item]
77 self.__dict__[item] = None78 if arg is None:
79 setattr(self, item, None)
78 else:80 else:
79 if '~' in locals()[item]:81 if arg.startswith('~'):
80 path = os.path.expanduser(locals()[item])82 path = os.path.expanduser(arg)
81 self.logger.debug('Rewriting ~-based path: ' + locals()[item] + ' to: ' + path)83 self.logger.debug('Rewriting ~-based path: ' + arg
84 + ' to: ' + path)
82 else:85 else:
83 path = locals()[item]86 path = arg
87
84 self.percent = 088 self.percent = 0
85 self.logger.info('Preparing ' + item + ': ' + path)89 self.logger.info('Preparing ' + item + ': ' + path)
86 self.__dict__[item] = urllib.urlretrieve(path, reporthook=self.dldisplay)[0]90 filename = urllib.urlretrieve(path,
87 self.logger.debug(path + ' is locally available as ' + self.__dict__[item])91 reporthook=self.dldisplay)[0]
92 setattr(self, item, filename)
93 self.logger.debug(path + ' is locally available as '
94 + getattr(self, item))
8895
89 self.logger.debug('Machine init finished')96 self.logger.debug('Machine init finished')
9097
9198
=== modified file 'utah/url.py'
--- utah/url.py 2012-07-13 10:12:53 +0000
+++ utah/url.py 2012-07-17 14:31:55 +0000
@@ -4,6 +4,7 @@
4import os4import os
5import urllib5import urllib
6import urllib26import urllib2
7from argparse import ArgumentTypeError
78
8from utah.exceptions import URLNotFound, URLNotReadable9from utah.exceptions import URLNotFound, URLNotReadable
910
@@ -57,3 +58,18 @@
57 raise URLNotReadable(path)58 raise URLNotReadable(path)
5859
59 return True60 return True
61
62
63def url_argument(url):
64 """
65 Check URL argument in argparse.ArgumentParser
66 """
67 url_checker = URLChecker()
68 try:
69 url_checker.open(url)
70 except URLNotFound:
71 raise ArgumentTypeError('URL not found: {0}'.format(url))
72 except URLNotReadable:
73 raise ArgumentTypeError('URL not readable: {0}'.format(url))
74
75 return url

Subscribers

People subscribed via source and target branches