From 7e23c7442ecc67417b3bccbcfe5f6c60a96cae9d Mon Sep 17 00:00:00 2001
From: Hongqing Yang <hoyang@redhat.com>
Date: Fri, 9 Sep 2011 15:02:47 +0800
Subject: [PATCH] iso watcher

---
 watchers/iso/README     |    6 +++
 watchers/iso/watcher.py |   82 +++++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 88 insertions(+), 0 deletions(-)
 create mode 100644 watchers/iso/README
 create mode 100755 watchers/iso/watcher.py

diff --git a/watchers/iso/README b/watchers/iso/README
new file mode 100644
index 0000000..4cacd35
--- /dev/null
+++ b/watchers/iso/README
@@ -0,0 +1,6 @@
+This watcher mirrors the isos of DVD, netinst and Live and kicks off tests if
+new isos are mirrored. The watcher emits following events:
+
+post-iso-build
+
+Usage info: watcher.py --help
diff --git a/watchers/iso/watcher.py b/watchers/iso/watcher.py
new file mode 100755
index 0000000..b1b558f
--- /dev/null
+++ b/watchers/iso/watcher.py
@@ -0,0 +1,82 @@
+#!/usr/bin/python
+#
+# iso watcher.py - A utility to mirror the DVD.iso netinst.iso and Live.iso
+#       and to kick off tests if new isos are mirrorred.
+#
+# Copyright 2011, Red Hat, Inc.
+#
+# This program is free software; you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation; either version 2 of the License, or
+# (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License along
+# with this program; if not, write to the Free Software Foundation, Inc.,
+# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+#
+# Authors: Hongqing YANG<hoyang@redhat.com>
+
+import os
+import subprocess
+import optparse
+
+def parse_args():
+    #Set up the option parser
+    parser = optparse.OptionParser(description='A utility to watch a website \
+    for any file changes and to kick off tests if any .iso files change.')
+    parser.add_option('-u', '--url', action='store',
+        default = "http://dl.fedoraproject.org/pub/alt/stage/",
+        help='URL for the website to monitor')
+    parser.add_option('--cachedir', action='store',
+        default = "/var/cache/autoqa/install-isos",
+        help='Cache directory')
+    parser.add_option('--dryrun', '--dry-run', action='store_true',
+        help='Do not actually execute commands, just show what would be done')
+    parser.add_option('--verbose', action='store_true',
+        help='Print extra information')
+
+    (opts, args) = parser.parse_args()
+    if not opts.url:
+        parser.error("Missing required argument -u,--url")
+
+    return (opts, args)
+
+if __name__ == "__main__":
+
+    (opts, args) = parse_args()
+
+    #Create the cachedir
+    if not os.path.exists(opts.cachedir):
+        os.makedirs(opts.cachedir)
+
+    #mirror opts.url, download all images except deltaiso and tree compose
+    if opts.dryrun:
+        args="lftp -c 'mirror --dry-run --verbose=3 --use-pget-n=20  -x deltaiso -x /os/ -n -e %s %s'" % (opts.url, opts.cachedir)
+    else:
+        args="lftp -c 'mirror --verbose=3 --use-pget-n=20  -x deltaiso -x /os/ -n -e %s %s'" % (opts.url, opts.cachedir)
+ 
+    print "start mirroring, please wait..."
+    out = subprocess.check_output(args, shell=True)
+    print out
+
+    #get all latest iso files downloaed
+    iso_files = []
+    for line in out.split('\n'):
+        line.strip()
+        if line.startswith('Transferring file') and line.endswith(".iso\'"):
+            iso_files.append(line[19:(len(line)-1)])
+
+    #create autotest
+    if iso_files:
+        for iso_file in iso_files:
+            harnesscall = ['autoqa','post-iso-build',os.path.join(opts.cachedir,iso_file),'--test','mediakit_sanity']
+            if opts.dryrun:
+                print ' '.join(harnesscall)
+                continue
+            else:
+                subprocess.call(harnesscall)
-- 
1.7.6

