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

Proposed by Javier Collado
Status: Merged
Approved by: Max Brustkern
Approved revision: 375
Merged at revision: 397
Proposed branch: lp:~javier.collado/utah/bug1023013
Merge into: lp:utah
Diff against target: 77 lines (+55/-0)
2 files modified
examples/run_utah_tests.py (+7/-0)
utah/url.py (+48/-0)
To merge this branch: bzr merge lp:~javier.collado/utah/bug1023013
Reviewer Review Type Date Requested Status
Max Brustkern (community) Approve
Review via email: mp+114398@code.launchpad.net

Description of the change

Added URL checker to run_utah_tests.py, so that it can fail early when the URL for a runlist doesn't exist.

The code that implements the URL check might be useful in some other part of the code and could be moved to a library. Let me know if that's the case or just let's move it if needed in the future.

To post a comment you must log in.
lp:~javier.collado/utah/bug1023013 updated
375. By Javier Collado

Moved URLChecker code to utah.url module

Revision history for this message
Max Brustkern (nuclearbob) wrote :

Good stuff. At some point utah/provisioning/provisioning.py should use this function 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-05 14:48:39 +0000
3+++ examples/run_utah_tests.py 2012-07-11 13:46:21 +0000
4@@ -10,6 +10,7 @@
5 import urllib
6
7 from utah import UTAHException
8+from utah.url import URLChecker
9 import utah.provisioning
10 from run_test_vm import run_test_vm
11
12@@ -63,6 +64,12 @@
13 sys.stderr.write("Please check the roadmap, as it will be included in a future version.\n")
14 sys.exit(4)
15
16+ url_checker = URLChecker()
17+ for runlist in args.runlists:
18+ if not url_checker.open(runlist):
19+ sys.stderr.write("Run list not found: " + runlist + "\n")
20+ sys.exit(5)
21+
22 function = run_test_vm
23
24 for option in [args.boot, args.emulator, args.image, args.initrd, args.kernel, args.preseed, args.xml]:
25
26=== added file 'utah/url.py'
27--- utah/url.py 1970-01-01 00:00:00 +0000
28+++ utah/url.py 2012-07-11 13:46:21 +0000
29@@ -0,0 +1,48 @@
30+"""
31+URL tools
32+"""
33+import os
34+import urllib
35+import urllib2
36+
37+
38+# Inspired by: http://stackoverflow.com/a/2070916/183066
39+class HeadRequest(urllib2.Request):
40+ """
41+ Request that sends HEAD method instead of GET
42+ """
43+ def get_method(self):
44+ return 'HEAD'
45+
46+
47+class URLChecker(urllib.URLopener):
48+ """
49+ Check that URL exists withou opening it
50+ or downloading its contents
51+ """
52+ def open_http(self, url, data=None):
53+ """
54+ Check if http URL exists
55+ """
56+ # This is redundant becuase urllib2 will call urllib
57+ # under the hood, but makes code easy to read.
58+ # Note that in the case of a redirect,
59+ # a GET request will be sent instead of a HEAD one
60+ # since that's how the urllib2.HTTPRedirectHandler
61+ # implementation works
62+ try:
63+ response = urllib2.urlopen(HeadRequest('http:' + url))
64+ except urllib2.URLError:
65+ return False
66+
67+ return response.getcode() == 200
68+
69+ def open_local_file(self, url):
70+ """
71+ Check if local file exists
72+ """
73+ # Based on urllib.URLopener.open_local_file implementation
74+ host, filename = urllib.splithost(url)
75+ path = urllib.url2pathname(filename)
76+
77+ return os.path.exists(path)

Subscribers

People subscribed via source and target branches