sbsv2/raptor/util/install-linux/hudson.py
changeset 625 a1925fb7753a
equal deleted inserted replaced
624:f70b728ea30c 625:a1925fb7753a
       
     1 
       
     2 # hudson runs this from the raptor/util/install-linux directory
       
     3 
       
     4 import datetime
       
     5 import os
       
     6 import re
       
     7 import shutil
       
     8 import subprocess
       
     9 import sys
       
    10 
       
    11 # run "hg id" to get the current branch name and tip changeset
       
    12 
       
    13 hgid = subprocess.Popen(["hg", "id"], stdout=subprocess.PIPE)
       
    14 stdout = hgid.communicate()[0]
       
    15 
       
    16 if hgid.returncode == 0 and len(stdout) >= 12:
       
    17 	changeset = stdout[0:12]
       
    18 	print "CHANGESET", changeset
       
    19 
       
    20 	prototype = ("wip" in stdout or "fix" in stdout)
       
    21 	print "PROTOTYPE", prototype
       
    22 else:
       
    23 	sys.stderr.write("error: failed to get tip mercurial changeset.\n")
       
    24 	sys.exit(1)
       
    25 
       
    26 # get today's date in ISO format YYYY-MM-DD
       
    27 
       
    28 today = datetime.date.today().isoformat()
       
    29 print "DATE", today
       
    30 
       
    31 # insert the date and changeset into the raptor_version.py file
       
    32 
       
    33 filename = "../../python/raptor_version.py"
       
    34 lines = []
       
    35 try:
       
    36 	file = open(filename, "r")
       
    37 	for line in file.readlines():
       
    38 		if "ISODATE" in line and "CHANGESET" in line:
       
    39 			line = line.replace("ISODATE", today)
       
    40 			line = line.replace("CHANGESET", changeset)
       
    41 			if prototype:
       
    42 				line = line.replace("system", "system PROTOTYPE")
       
    43 			lines.append(line)
       
    44 		else:
       
    45 			lines.append(line)
       
    46 except IOError, ex:
       
    47 	sys.stderr.write("error: failed to read file '%s'\n%s" % (filename, str(ex)))
       
    48 	sys.exit(1)
       
    49 finally:
       
    50 	file.close()
       
    51 
       
    52 # ... and write the modified raptor_version.py file
       
    53 
       
    54 try:
       
    55 	file = open(filename, "w")
       
    56 	for line in lines:
       
    57 		file.write(line)
       
    58 except IOError, ex:
       
    59 	sys.stderr.write("error: failed to write file '%s'\n%s" % (filename, str(ex)))
       
    60 	sys.exit(1)
       
    61 finally:
       
    62 	file.close()
       
    63 
       
    64 # check that we really did change the raptor version string
       
    65 
       
    66 sbs_v = subprocess.Popen(["../../bin/sbs", "-v"], stdout=subprocess.PIPE)
       
    67 version = sbs_v.communicate()[0]
       
    68 
       
    69 if sbs_v.returncode == 0:
       
    70 	print "VERSION", version
       
    71 	if not today in version or not changeset in version:
       
    72 		sys.stderr.write("error: date or changeset does not match the sbs version.\n")
       
    73 		sys.exit(1)
       
    74         if prototype and not "PROTOTYPE" in version:
       
    75 		sys.stderr.write("error: the sbs version should be marked PROTOTYPE.\n")
       
    76 		sys.exit(1)
       
    77 else:
       
    78 	sys.stderr.write("error: failed to get sbs version.\n")
       
    79 	sys.exit(1)
       
    80 
       
    81 # run the Linux installer maker script
       
    82 
       
    83 package_sbs = subprocess.Popen(["./package_sbs.sh", "-s"], stdout=subprocess.PIPE, stderr=subprocess.PIPE)
       
    84 (stdout, stderr) = package_sbs.communicate()
       
    85 
       
    86 if package_sbs.returncode != 0:
       
    87 	sys.stderr.write("error: failed to create linux package of sbs.\n")
       
    88 	sys.exit(1)
       
    89 
       
    90 # find the name of the archive in /tmp
       
    91 
       
    92 match = re.search('archive "([^"]+)" successfully created', stdout)
       
    93 if match:
       
    94 	tmp_archive = "/tmp/" + match.group(1)
       
    95 	print "TMP ARCHIVE", tmp_archive
       
    96 else:
       
    97 	sys.stderr.write("error: failed to find linux archive file.\n")
       
    98 	sys.exit(1)
       
    99 
       
   100 # move it to the WORKSPACE root
       
   101 
       
   102 if 'WORKSPACE' in os.environ:
       
   103 	name = re.sub(r'/tmp/(sbs-\d+\.\d+\.\d+-).*', r'\1', tmp_archive)
       
   104 	if prototype:
       
   105 		fullname = name + "PROTOTYPE-" + changeset + ".run"
       
   106 	else:
       
   107 		fullname = name + changeset + ".run"
       
   108 	final_archive = os.path.join(os.environ['WORKSPACE'], fullname)
       
   109 	print "WORKSPACE ARCHIVE", final_archive
       
   110 else:
       
   111 	sys.stderr.write("error: no WORKSPACE is set.\n")
       
   112 	sys.exit(1)
       
   113 
       
   114 try:
       
   115 	shutil.move(tmp_archive, final_archive)
       
   116 except Error, err:
       
   117 	sys.stderr.write("error: could not rename '%s' as '%s'.\n" % (tmp_archive, final_archive))
       
   118 	sys.exit(1)
       
   119 
       
   120 # the end