KVM test: automate handling of cdkeys in unattended_install scripts

- Automatically replace cdkeys in unattended_install scripts.
  A dummy string (KVM_TEST_CDKEY) is replaced by an actual cdkey (normally
  defined in cdkeys.cfg) before installation begins.
- Make all Windows unattended scripts use the same dummy string.
- Add dummy cdkeys to cdkeys.cfg.sample, for WinVista, Win2008 and Win7.
  These must be replaced manually by the user.

Signed-off-by: Michael Goldish <mgoldish@redhat.com>


git-svn-id: http://test.kernel.org/svn/autotest/trunk@4200 592f7852-d20e-0410-864c-8624ca9c26a4
diff --git a/client/tests/kvm/scripts/unattended.py b/client/tests/kvm/scripts/unattended.py
index 13f431a..87a8973 100755
--- a/client/tests/kvm/scripts/unattended.py
+++ b/client/tests/kvm/scripts/unattended.py
@@ -97,7 +97,22 @@
             dest_fname = "autounattend.xml"
 
         dest = os.path.join(self.floppy_mount, dest_fname)
-        shutil.copyfile(self.unattended_file, dest)
+
+        # Replace KVM_TEST_CDKEY (in the unattended file) with the cdkey
+        # provided for this test
+        unattended_contents = open(self.unattended_file).read()
+        dummy_cdkey_re = r'\bKVM_TEST_CDKEY\b'
+        real_cdkey = os.environ.get('KVM_TEST_cdkey')
+        if re.search(dummy_cdkey_re, unattended_contents):
+            if real_cdkey:
+                unattended_contents = re.sub(dummy_cdkey_re, real_cdkey,
+                                             unattended_contents)
+            else:
+                print ("WARNING: 'cdkey' required but not specified for this "
+                       "unattended installation")
+
+        # Write the unattended file contents to 'dest'
+        open(dest, 'w').write(unattended_contents)
 
         if self.finish_program:
             dest_fname = os.path.basename(self.finish_program)