diff mbox

[Branch,~linaro-validation/lava-dispatcher/trunk] Rev 374: add a URL mapping feature

Message ID 20120824142213.15181.55721.launchpad@ackee.canonical.com
State Accepted
Headers show

Commit Message

Andy Doan Aug. 24, 2012, 2:22 p.m. UTC
------------------------------------------------------------
revno: 374
committer: Andy Doan <andy.doan@linaro.org>
branch nick: lava-dispatcher
timestamp: Fri 2012-08-24 09:20:35 -0500
message:
  add a URL mapping feature
  
  This allows the dispatcher's downloading code to be able to override
  a URL given to it with another. For example, you might get a job
  that wants to download a file:
    http://blah/root.img
  
    This could be overriden to be file:///root.img
  
    You can specify one or more mappings in:
  
     <INSTANCE>/etc/lava-dispatcher/urlmappings.txt
  
     An example:
  
     ^https:\/\/.*\/precise\/restricted, scp://linaro-lava@mombin.canonical.com/precise/restricted/
     ^https:\/\/.*\/~linaro-android-restricted\/, scp://linaro-lava@mombin.canonical.com/android/~linaro-android-restricted/
  
     these take URLs that are protected by OpenID (not supported in the downloader)
     and replaces them with an SCP based URL
modified:
  lava_dispatcher/downloader.py


--
lp:lava-dispatcher
https://code.launchpad.net/~linaro-validation/lava-dispatcher/trunk

You are subscribed to branch lp:lava-dispatcher.
To unsubscribe from this branch go to https://code.launchpad.net/~linaro-validation/lava-dispatcher/trunk/+edit-subscription
diff mbox

Patch

=== modified file 'lava_dispatcher/downloader.py'
--- lava_dispatcher/downloader.py	2012-06-25 20:45:16 +0000
+++ lava_dispatcher/downloader.py	2012-08-24 14:20:35 +0000
@@ -23,6 +23,7 @@ 
 import contextlib
 import logging
 import os
+import re
 import shutil
 import subprocess
 import urllib2
@@ -107,6 +108,24 @@ 
     filename = os.path.join(path, '.'.join(parts[:-1]))
     return (filename, suffix)
 
+def _url_mapping(url, context):
+    '''allows the downloader to override a URL so that something like:
+     http://blah/ becomes file://localhost/blah
+    '''
+    mappings = '%s/urlmappings.txt' % context.config.config_dir
+    if os.path.exists(mappings):
+        newurl = url
+        with open(mappings, 'r') as f:
+            for line in f.readlines():
+                pat,rep = line.split(',')
+                pat = pat.strip()
+                rep = rep.strip()
+                newurl = re.sub(pat, rep, newurl)
+        if newurl != url:
+            url = newurl
+            logging.info('url mapped to: %s', url)
+    return url
+
 def download_image(url, context, imgdir=None,
                     delete_on_exit=True, decompress=True):
     '''downloads a image that's been compressed as .bz2 or .gz and
@@ -118,6 +137,8 @@ 
         if delete_on_exit:
             atexit.register(shutil.rmtree, imgdir)
 
+    url = _url_mapping(url, context)
+
     url = urlparse.urlparse(url)
     stream = None
     if url.scheme == 'scp':