From b840cd4f247094ffe1233e213a4323ff7237f441 Mon Sep 17 00:00:00 2001 From: Raul Cuza Date: Fri, 1 Jul 2011 17:08:50 -0400 Subject: Refactor export.py to use main() clause. Minimal testing has been done to compare export.py and export2.py. --- tools/export2.py | 125 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 125 insertions(+) create mode 100755 tools/export2.py diff --git a/tools/export2.py b/tools/export2.py new file mode 100755 index 000000000..9fa920d18 --- /dev/null +++ b/tools/export2.py @@ -0,0 +1,125 @@ +#!/usr/bin/env python +# encoding: utf-8 + +""" +Second attempt to make our export script more portable than export.sh +""" + +import fileinput +from subprocess import Popen, PIPE +import sys + +# py3k compatibility +try: + from email.Utils import formatdate +except ImportError: + from email.utils import formatdate + +def run(command): + return Popen(command, shell=True, stdout=PIPE).communicate() + +def find_and_replace(f, iftest, rline, startswith=False): + for line in fileinput.input(f, inplace=1): + if startswith: + if line.startswith(iftest): + line = line.replace(line, rline) + sys.stdout.write(line) + else: + if iftest in line and line != "Version: %{version}\n": + line = line.replace(line, rline) + sys.stdout.write(line) + + +if __name__ == '__main__': + pkgname = 'bcfg2' + ftphost = 'terra.mcs.anl.gov' + ftpdir = '/mcs/ftp/pub/bcfg' + + # py3k compatibility + try: + version = raw_input("Please enter the version you are tagging (e.g. 1.0.0): ") + name = raw_input("Your name: ") + email = raw_input("Your email: ") + except NameError: + version = input("Please enter the version you are tagging (e.g. 1.0.0): ") + name = input("Your name: ") + email = input("Your email: ") + + tarname = '/tmp/%s-%s.tar.gz' % (pkgname, version) + + # update the version + majorver = version[:5] + minorver = version[5:] + + newchangelog = \ + """bcfg2 (%s%s-0.0) unstable; urgency=low + + * New upstream release + + -- %s <%s> %s + + """ % (majorver, minorver, name, email, formatdate(localtime=True)) + + + # write out the new debian changelog + with open('debian/changelog', 'r+') as f: + old = f.read() + f.seek(0) + f.write(newchangelog + old) + f.close() + quit() + # Update redhat directory versions + with open('redhat/VERSION', 'w') as f: + f.write("%s\n" % majorver) + f.close() + with open('redhat/RELEASE', 'w') as f: + f.write("0.0%s\n" % minorver) + f.close() + # update solaris version + find_and_replace('solaris/Makefile', 'VERS=', + 'VERS=%s-1\n' % version, startswith=True) + find_and_replace('solaris/pkginfo.bcfg2', 'VERSION=', + 'VERSION="%s"\n' % version, startswith=True) + find_and_replace('solaris/pkginfo.bcfg2-server', 'VERSION=', + 'VERSION="%s"\n' % version, startswith=True) + # set new version in setup.py + find_and_replace('setup.py', 'version=', ' version="%s",\n' % version) + # replace version in misc/bcfg2.spec + find_and_replace('misc/bcfg2.spec', 'Version:', + 'Version: %s\n' % version) + # update the version in reports + find_and_replace('src/lib/Server/Reports/reports/templates/base.html', + 'Bcfg2 Version', ' Bcfg2 Version %s\n' % version) + # update the version in the docs + find_and_replace('doc/conf.py', 'version =', + 'version = \'%s\'\n' % majorver[0:3], startswith=True) + find_and_replace('doc/conf.py', 'release =', + 'release = \'%s\'\n' % (majorver), startswith=True) + + # tag the release + #FIXME: do this using python-dulwich + cmd = "git commit -asm 'Version bump to %s'" % version + output = run(cmd)[0].strip() + # NOTE: This will use the default email address key. If you want to sign the tag + # using a different key, you will need to set 'signingkey' to the proper + # value in the [user] section of your git configuration. + cmd = "git tag -s v%s -m 'tagged %s release'" % (version, version) + output = run(cmd)[0].strip() + cmd = "git archive --format=tar --prefix=%s-%s/ v%s | gzip > %s" % \ + (pkgname, version, version, tarname) + output = run(cmd)[0].strip() + cmd = "gpg --armor --output %s.gpg --detach-sig %s" % (tarname, tarname) + output = run(cmd)[0].strip() + + # upload release to ftp + cmd = "scp %s* terra.mcs.anl.gov:/mcs/ftp/pub/bcfg/" % tarname + output = run(cmd)[0].strip() + + + + + + + + + -- cgit v1.2.3-1-g7c22 From 4ff124d7c9179c5286cf41256883a94ba2e07f7e Mon Sep 17 00:00:00 2001 From: Raul Cuza Date: Fri, 1 Jul 2011 17:13:09 -0400 Subject: Add new version variables to export script --- tools/export2.py | 17 ++++++++++++++--- 1 file changed, 14 insertions(+), 3 deletions(-) diff --git a/tools/export2.py b/tools/export2.py index 9fa920d18..bb7ad23ca 100755 --- a/tools/export2.py +++ b/tools/export2.py @@ -45,6 +45,17 @@ if __name__ == '__main__': name = input("Your name: ") email = input("Your email: ") + # parse version into Major.Minor.Build and validate + try: + [version_major, version_minor, version_build] = version.split(".") + if not version_major.isdigit() or not version_minor.isdigit(): + raise VersionError('isdigit() test failed') + except: + print "Version must be of the form Major.Minor.Build, where Major and Minor are integers and Build is a single digit optionally followed by pre##" + quit() + + version_macbuild = version_build[0:1] + tarname = '/tmp/%s-%s.tar.gz' % (pkgname, version) # update the version @@ -98,14 +109,14 @@ if __name__ == '__main__': # tag the release #FIXME: do this using python-dulwich - cmd = "git commit -asm 'Version bump to %s'" % version + cmd = "ggit commit -asm 'Version bump to %s'" % version output = run(cmd)[0].strip() # NOTE: This will use the default email address key. If you want to sign the tag # using a different key, you will need to set 'signingkey' to the proper # value in the [user] section of your git configuration. - cmd = "git tag -s v%s -m 'tagged %s release'" % (version, version) + cmd = "ggit tag -s v%s -m 'tagged %s release'" % (version, version) output = run(cmd)[0].strip() - cmd = "git archive --format=tar --prefix=%s-%s/ v%s | gzip > %s" % \ + cmd = "ggit archive --format=tar --prefix=%s-%s/ v%s | gzip > %s" % \ (pkgname, version, version, tarname) output = run(cmd)[0].strip() cmd = "gpg --armor --output %s.gpg --detach-sig %s" % (tarname, tarname) -- cgit v1.2.3-1-g7c22 From 09cbf47c9a42a8ce588e9a6bfb5c45b228fcbaca Mon Sep 17 00:00:00 2001 From: Raul Cuza Date: Tue, 5 Jul 2011 11:05:15 -0400 Subject: Add Environment.py file for building classes to gather env data. Includes Pyversion() class which simplifies getting the Python version that Bcfg2 is running under. It is mostly useful for Python 2 and under, since Python 3 has the handy sys.version_info.{major,minor,...} object. --- src/lib/Environment.py | 37 +++++++++++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) create mode 100644 src/lib/Environment.py diff --git a/src/lib/Environment.py b/src/lib/Environment.py new file mode 100644 index 000000000..782407ee2 --- /dev/null +++ b/src/lib/Environment.py @@ -0,0 +1,37 @@ +#!/usr/bin/env python +# encoding: utf-8 +""" +Environment.py + +Classes for easy access to python environment information (e.g. python version). +""" + +import sys +import os + +class Pyversion(): + + def __init__(self): + # This is only helpful for Python 2 and older. Python 3 has sys.version_info.major. + [self.major, self.minor, self.micro, self.releaselevel, self.serial] = sys.version_info + self.version = sys.version + self.hex = sys.hexversion + + +def main(): + # test class Pyversion + pyversion = Pyversion() + print "%s : %s" % ("major", pyversion.major) + print "%s : %s" % ("minor", pyversion.minor) + print "%s : %s" % ("micro", pyversion.micro) + print "%s : %s" % ("releaselevel", pyversion.releaselevel) + print "%s : %s" % ("serial", pyversion.serial) + print "%s : %s" % ("version", pyversion.version) + print "%s : %s" % ("hex", pyversion.hex) + + pass + + +if __name__ == '__main__': + main() + -- cgit v1.2.3-1-g7c22 From 0f16c41f46df8448b127d4e8b2274a9ffdcddb84 Mon Sep 17 00:00:00 2001 From: Raul Cuza Date: Tue, 5 Jul 2011 11:18:22 -0400 Subject: Exclude compiled python files from the git repository. --- .gitignore | 1 + 1 file changed, 1 insertion(+) diff --git a/.gitignore b/.gitignore index d449d8bb4..80cc98761 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,3 @@ src/Bcfg2 +*.pyc -- cgit v1.2.3-1-g7c22 From b6560930b190fa05970b594e8029c0b17e9e4f8f Mon Sep 17 00:00:00 2001 From: Raul Cuza Date: Tue, 5 Jul 2011 15:51:02 -0400 Subject: Add command line options to export tool. Run with -h to see all the options. --- tools/export2.py | 105 +++++++++++++++++++++++++++++++++++++++---------------- 1 file changed, 74 insertions(+), 31 deletions(-) diff --git a/tools/export2.py b/tools/export2.py index bb7ad23ca..886db0cf0 100755 --- a/tools/export2.py +++ b/tools/export2.py @@ -3,11 +3,14 @@ """ Second attempt to make our export script more portable than export.sh + """ import fileinput from subprocess import Popen, PIPE import sys +# This will need to be replaced with argsparse when we make a 2.7+/3.2+ version +import optparse # py3k compatibility try: @@ -15,6 +18,18 @@ try: except ImportError: from email.utils import formatdate +# In lieu of a config file +help_message = '''This script creates a tag in the Bcfg2 git repo and exports a tar file of the code +at that tag. + +This script must be run at the top of your git repository. +''' + + +pkgname = 'bcfg2' +ftphost = 'terra.mcs.anl.gov' +ftpdir = '/mcs/ftp/pub/bcfg' + def run(command): return Popen(command, shell=True, stdout=PIPE).communicate() @@ -29,19 +44,39 @@ def find_and_replace(f, iftest, rline, startswith=False): line = line.replace(line, rline) sys.stdout.write(line) - -if __name__ == '__main__': - pkgname = 'bcfg2' - ftphost = 'terra.mcs.anl.gov' - ftpdir = '/mcs/ftp/pub/bcfg' +def main(argv=None): + # This is where the options are set up + p = optparse.OptionParser(description = help_message, + prog = 'export2.py', + version = '0.1', + usage = '%prog [-h|--help] [-v|--version] [-n|--dry-run] [-d|--debug]') + p.add_option('--verbose', '-v', + action = 'store_true', + help = 'turns on verbose mode', + default = False, + dest = 'verbose') + p.add_option('--dry-run', '-n', + action = 'store_true', + help = 'run in dry-run mode; no changes will be made to the system', + default = False, + dest = 'dryrun') + p.add_option('--debug', '-d', + action = 'store_true', + help = 'run in debun mode', + default = False, + dest = 'debug') + options, arguments = p.parse_args() + + if options.debug: + print options # py3k compatibility try: - version = raw_input("Please enter the version you are tagging (e.g. 1.0.0): ") + version = raw_input("Please enter the Bcfg2 version you are tagging (e.g. 1.0.0): ") name = raw_input("Your name: ") email = raw_input("Your email: ") except NameError: - version = input("Please enter the version you are tagging (e.g. 1.0.0): ") + version = input("Please enter the Bcfg2 version you are tagging (e.g. 1.0.0): ") name = input("Your name: ") email = input("Your email: ") @@ -73,12 +108,17 @@ if __name__ == '__main__': # write out the new debian changelog - with open('debian/changelog', 'r+') as f: - old = f.read() - f.seek(0) - f.write(newchangelog + old) - f.close() - quit() + try: + with open('debian/changelog', 'r+') as f: + old = f.read() + f.seek(0) + f.write(newchangelog + old) + f.close() + except: + print "Problem opening debian/changelog" + print help_message + quit() + # Update redhat directory versions with open('redhat/VERSION', 'w') as f: f.write("%s\n" % majorver) @@ -109,28 +149,31 @@ if __name__ == '__main__': # tag the release #FIXME: do this using python-dulwich - cmd = "ggit commit -asm 'Version bump to %s'" % version - output = run(cmd)[0].strip() + commando = {} + + commando["vcs_commit"] = "git commit -asm 'Version bump to %s'" % version + # NOTE: This will use the default email address key. If you want to sign the tag # using a different key, you will need to set 'signingkey' to the proper # value in the [user] section of your git configuration. - cmd = "ggit tag -s v%s -m 'tagged %s release'" % (version, version) - output = run(cmd)[0].strip() - cmd = "ggit archive --format=tar --prefix=%s-%s/ v%s | gzip > %s" % \ - (pkgname, version, version, tarname) - output = run(cmd)[0].strip() - cmd = "gpg --armor --output %s.gpg --detach-sig %s" % (tarname, tarname) - output = run(cmd)[0].strip() - - # upload release to ftp - cmd = "scp %s* terra.mcs.anl.gov:/mcs/ftp/pub/bcfg/" % tarname - output = run(cmd)[0].strip() - - - - - + commando["vcs_tag"] = "git tag -s v%s -m 'tagged %s release'" % (version, version) + commando["create_archive"] = "git archive --format=tar --prefix=%s-%s/ v%s | gzip > %s" % \ + (pkgname, version, version, tarname) + commando["gpg_encrypt"] = "gpg --armor --output %s.gpg --detach-sig %s" % (tarname, tarname) + # upload release to ftp + commando["scp_archive"] = "scp %s* terra.mcs.anl.gov:/mcs/ftp/pub/bcfg/" % tarname + + # Execute the commands + commando_orders = ["vcs_commit","vcs_tag","create_archive","gpg_encrypt","scp_archive"] + if options.dryrun: + for cmd in commando_orders: + print "dry-run: %s" % commando[cmd] + else: + for cmd in commando_orders: + output = run(commando[cmd])[0].strip() +if __name__ == '__main__': + sys.exit(main()) -- cgit v1.2.3-1-g7c22 From cba35827f740d070b3493200a67632955c0e4c4a Mon Sep 17 00:00:00 2001 From: Raul Cuza Date: Tue, 5 Jul 2011 16:31:36 -0400 Subject: Extend dry-run mode to file replacement --- tools/export2.py | 54 +++++++++++++++++++++++++++++++++++------------------- 1 file changed, 35 insertions(+), 19 deletions(-) diff --git a/tools/export2.py b/tools/export2.py index 886db0cf0..25ac8335e 100755 --- a/tools/export2.py +++ b/tools/export2.py @@ -33,8 +33,13 @@ ftpdir = '/mcs/ftp/pub/bcfg' def run(command): return Popen(command, shell=True, stdout=PIPE).communicate() -def find_and_replace(f, iftest, rline, startswith=False): - for line in fileinput.input(f, inplace=1): +def find_and_replace(f, iftest, rline, startswith=False, dryrun=True): + if dryrun: + inplace=0 + print "*** dry-run: New '%s' will look like this:" % f + else: + inplace=1 + for line in fileinput.input(f, inplace): if startswith: if line.startswith(iftest): line = line.replace(line, rline) @@ -43,6 +48,8 @@ def find_and_replace(f, iftest, rline, startswith=False): if iftest in line and line != "Version: %{version}\n": line = line.replace(line, rline) sys.stdout.write(line) + if dryrun: + print "*** End '%s'" % f def main(argv=None): # This is where the options are set up @@ -108,24 +115,33 @@ def main(argv=None): # write out the new debian changelog - try: - with open('debian/changelog', 'r+') as f: - old = f.read() - f.seek(0) - f.write(newchangelog + old) - f.close() - except: - print "Problem opening debian/changelog" - print help_message - quit() + if options.dryrun: + print "*** Add the following to the top of debian/changelog:\n%s" % newchangelog + print "\n" + else: + try: + with open('debian/changelog', 'r+') as f: + old = f.read() + f.seek(0) + f.write(newchangelog + old) + f.close() + except: + print "Problem opening debian/changelog" + print help_message + quit() # Update redhat directory versions - with open('redhat/VERSION', 'w') as f: - f.write("%s\n" % majorver) - f.close() - with open('redhat/RELEASE', 'w') as f: - f.write("0.0%s\n" % minorver) - f.close() + if options.dryrun: + print "*** Replace redhat/VERIONS content with '%s'." % majorver + print "*** Replace redhat/RELEASE content with '%s'." % minorver + else: + with open('redhat/VERSION', 'w') as f: + f.write("%s\n" % majorver) + f.close() + with open('redhat/RELEASE', 'w') as f: + f.write("0.0%s\n" % minorver) + f.close() + # update solaris version find_and_replace('solaris/Makefile', 'VERS=', 'VERS=%s-1\n' % version, startswith=True) @@ -170,7 +186,7 @@ def main(argv=None): commando_orders = ["vcs_commit","vcs_tag","create_archive","gpg_encrypt","scp_archive"] if options.dryrun: for cmd in commando_orders: - print "dry-run: %s" % commando[cmd] + print "*** dry-run: %s" % commando[cmd] else: for cmd in commando_orders: output = run(commando[cmd])[0].strip() -- cgit v1.2.3-1-g7c22 From 2503fdd13aeec12294ede0cfa16b1e3ead6e2a88 Mon Sep 17 00:00:00 2001 From: Raul Cuza Date: Tue, 5 Jul 2011 23:55:47 -0400 Subject: Finish converting find_and_replace calls to use dryrun argument. Set default value to false. Made the find_and_replace calls multilined. --- tools/export2.py | 31 ++++++++++++++++++++++--------- 1 file changed, 22 insertions(+), 9 deletions(-) diff --git a/tools/export2.py b/tools/export2.py index 25ac8335e..0b8cc159d 100755 --- a/tools/export2.py +++ b/tools/export2.py @@ -33,7 +33,7 @@ ftpdir = '/mcs/ftp/pub/bcfg' def run(command): return Popen(command, shell=True, stdout=PIPE).communicate() -def find_and_replace(f, iftest, rline, startswith=False, dryrun=True): +def find_and_replace(f, iftest, rline, startswith=False, dryrun=False): if dryrun: inplace=0 print "*** dry-run: New '%s' will look like this:" % f @@ -144,24 +144,37 @@ def main(argv=None): # update solaris version find_and_replace('solaris/Makefile', 'VERS=', - 'VERS=%s-1\n' % version, startswith=True) + 'VERS=%s-1\n' % version, + startswith=True, + dryrun=options.dryrun) find_and_replace('solaris/pkginfo.bcfg2', 'VERSION=', - 'VERSION="%s"\n' % version, startswith=True) + 'VERSION="%s"\n' % version, + startswith=True, + dryrun=options.dryrun) find_and_replace('solaris/pkginfo.bcfg2-server', 'VERSION=', - 'VERSION="%s"\n' % version, startswith=True) + 'VERSION="%s"\n' % version, + startswith=True, + dryrun=options.dryrun) # set new version in setup.py - find_and_replace('setup.py', 'version=', ' version="%s",\n' % version) + find_and_replace('setup.py', 'version=', ' version="%s",\n' % version, + dryrun=options.dryrun) # replace version in misc/bcfg2.spec find_and_replace('misc/bcfg2.spec', 'Version:', - 'Version: %s\n' % version) + 'Version: %s\n' % version, + dryrun=options.dryrun) # update the version in reports find_and_replace('src/lib/Server/Reports/reports/templates/base.html', - 'Bcfg2 Version', ' Bcfg2 Version %s\n' % version) + 'Bcfg2 Version', ' Bcfg2 Version %s\n' % version, + dryrun=options.dryrun) # update the version in the docs find_and_replace('doc/conf.py', 'version =', - 'version = \'%s\'\n' % majorver[0:3], startswith=True) + 'version = \'%s\'\n' % majorver[0:3], + startswith=True, + dryrun=options.dryrun) find_and_replace('doc/conf.py', 'release =', - 'release = \'%s\'\n' % (majorver), startswith=True) + 'release = \'%s\'\n' % (majorver), + startswith=True, + dryrun=options.dryrun) # tag the release #FIXME: do this using python-dulwich -- cgit v1.2.3-1-g7c22