>From 8dcc53497d03ad35024705659a763c7cecd86655 Mon Sep 17 00:00:00 2001
From: Josef Skladanka <jskladan@redhat.com>
Date: Fri, 7 May 2010 12:27:13 +0200
Subject: [PATCH 1/3] 65,130 - autotest job-tag propagation to autoqa

---
 autoqa             |   25 ++++++++++++++++++++++++-
 lib/python/util.py |    8 ++++++++
 2 files changed, 32 insertions(+), 1 deletions(-)

diff --git a/autoqa b/autoqa
index efb94e3..9fa2349 100755
--- a/autoqa
+++ b/autoqa
@@ -27,6 +27,7 @@ import optparse
 import tempfile
 import StringIO
 import urlgrabber
+import socket
 from ConfigParser import *
 from subprocess import call
 
@@ -37,6 +38,7 @@ conf = {
     'testdir': '/usr/share/autotest/client/site_tests',
     'hookdir': '/usr/share/autoqa',
     'notification_email': '',
+    'autotest_server': socket.gethostname(),
 }
 cfg_parser = SafeConfigParser() # used by prep_controlfile
 try:
@@ -58,6 +60,12 @@ def prep_controlfile(controlfile, extradata):
     prepended with key='value' lines for each item in extradata and an
     'autoqa_conf' variable which holds the contents of the autoqa configfile.
     '''
+    # the jobtag must be evaluated on every single testrun (since it changes :-))
+    # so line 'jobtag = %s' is added to the [job] part of the autoqa_conf
+    # string, and then is replaced with job.tag when the code is executed
+    if not cfg_parser.has_section('job'):
+        cfg_parser.add_section('job')
+    cfg_parser.set('job', 'tag', '%(jobtag)s')
     controldata = open(controlfile)
     (fd, name) = tempfile.mkstemp(prefix='autoqa-control.')
     os.write(fd, '# -*- coding: utf-8 -*-\n\n')
@@ -65,7 +73,14 @@ def prep_controlfile(controlfile, extradata):
         cfgdata = StringIO.StringIO()
         cfg_parser.write(cfgdata)
         cfgdata.seek(0)
-        os.write(fd, "autoqa_conf = '''\n%s'''\n\n" % cfgdata.read())
+        autoqa_conf_str = "autoqa_conf = '''\n%s'''" % cfgdata.read()
+        # The key names in the following dict must correspond to the special
+        # config values set above (search for e.g. 'jobtag' to see them).
+        # The values in the dict get interpolated when autotest runs the
+        # control file, thus putting those values into the config (and
+        # passing them along to the tests) even though they don't exist yet.
+        autoqa_conf_str += " % {'jobtag' : job.tag}\n\n"
+        os.write(fd, autoqa_conf_str)
     except IOError:
         pass
     for k,v in extradata.iteritems():
@@ -139,6 +154,9 @@ parser.add_option('--local', action='store_true', dest='local',
     help='Do not schedule jobs - run test(s) directly on the local machine')
 parser.add_option('-l', '--list-tests', action='store_true', dest='listtests',
     help='list the tests for the given hookname - do not run any tests')
+parser.add_option('--autotest-server', action='store', default=None,
+    help='Sets the autotest-server hostname. Used for creating URLs to results.\
+Hostname of the local machine is used by default.')
 # Read and validate the hookname
 # Check for no args, or just -h/--help
 if len(sys.argv) == 1 or sys.argv[1] in ('-h', '--help'):
@@ -197,6 +215,11 @@ for arch in opts.arch:
     # N.B. process_testdata may grow new keyword arguments if we add new autoqa
     # args that add another loop here..
     testdata = hook.process_testdata(opts, args, arch=arch)
+    if not 'autotest_server' in testdata.keys():
+        if opts.autotest_server is not None:
+            testdata['autotest_server'] = opts.autotest_server
+        else:
+            testdata['autotest_server'] = conf['autotest_server']
     # XXX FIXME: tests need to be able to indicate that they do not require
     # any specific arch (e.g. rpmlint can run on any arch)
     for test in testlist:
diff --git a/lib/python/util.py b/lib/python/util.py
index 4a533ec..a36aa4e 100644
--- a/lib/python/util.py
+++ b/lib/python/util.py
@@ -151,3 +151,11 @@ def test(key, result):
         write_keyval(key, "FAIL")
         print "TEST RESULT: %s: FAIL" % key
         raise CheckFailedException
+
+def make_autotest_url(config):
+    autotest_server = config.get('general', 'autotest_server')
+    jobtag = config.get('job', 'tag')
+    autotest_url = "http://%s/results/%s/" % (autotest_server, jobtag)
+    return(autotest_url)
+
+
-- 
1.6.6.1

