client.py

Wed, 31 Dec 2014 06:55:50 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:55:50 +0100
changeset 2
7e26c7da4463
permissions
-rwxr-xr-x

Added tag UPSTREAM_283F7C6 for changeset ca08bd8f51b2

michael@0 1 #!/usr/bin/python
michael@0 2 # This Source Code Form is subject to the terms of the Mozilla Public
michael@0 3 # License, v. 2.0. If a copy of the MPL was not distributed with this
michael@0 4 # file, You can obtain one at http://mozilla.org/MPL/2.0/.
michael@0 5
michael@0 6
michael@0 7 LIBFFI_DIRS = (('js/ctypes/libffi', 'libffi'),)
michael@0 8 HG_EXCLUSIONS = ['.hg', '.hgignore', '.hgtags']
michael@0 9
michael@0 10 CVSROOT_LIBFFI = ':pserver:anoncvs@sources.redhat.com:/cvs/libffi'
michael@0 11
michael@0 12 import os
michael@0 13 import sys
michael@0 14 import datetime
michael@0 15 import shutil
michael@0 16 import glob
michael@0 17 from optparse import OptionParser
michael@0 18 from subprocess import check_call
michael@0 19
michael@0 20 topsrcdir = os.path.dirname(__file__)
michael@0 21 if topsrcdir == '':
michael@0 22 topsrcdir = '.'
michael@0 23
michael@0 24 def check_call_noisy(cmd, *args, **kwargs):
michael@0 25 print "Executing command:", cmd
michael@0 26 check_call(cmd, *args, **kwargs)
michael@0 27
michael@0 28 def do_hg_pull(dir, repository, hg):
michael@0 29 fulldir = os.path.join(topsrcdir, dir)
michael@0 30 # clone if the dir doesn't exist, pull if it does
michael@0 31 if not os.path.exists(fulldir):
michael@0 32 check_call_noisy([hg, 'clone', repository, fulldir])
michael@0 33 else:
michael@0 34 cmd = [hg, 'pull', '-u', '-R', fulldir]
michael@0 35 if repository is not None:
michael@0 36 cmd.append(repository)
michael@0 37 check_call_noisy(cmd)
michael@0 38 check_call([hg, 'parent', '-R', fulldir,
michael@0 39 '--template=Updated to revision {node}.\n'])
michael@0 40
michael@0 41 def do_hg_replace(dir, repository, tag, exclusions, hg):
michael@0 42 """
michael@0 43 Replace the contents of dir with the contents of repository, except for
michael@0 44 files matching exclusions.
michael@0 45 """
michael@0 46 fulldir = os.path.join(topsrcdir, dir)
michael@0 47 if os.path.exists(fulldir):
michael@0 48 shutil.rmtree(fulldir)
michael@0 49
michael@0 50 assert not os.path.exists(fulldir)
michael@0 51 check_call_noisy([hg, 'clone', '-u', tag, repository, fulldir])
michael@0 52
michael@0 53 for thing in exclusions:
michael@0 54 for excluded in glob.iglob(os.path.join(fulldir, thing)):
michael@0 55 if os.path.isdir(excluded):
michael@0 56 shutil.rmtree(excluded)
michael@0 57 else:
michael@0 58 os.remove(excluded)
michael@0 59
michael@0 60 def do_cvs_export(modules, tag, cvsroot, cvs):
michael@0 61 """Check out a CVS directory without CVS metadata, using "export"
michael@0 62 modules is a list of directories to check out and the corresponding
michael@0 63 cvs module, e.g. (('js/ctypes/libffi', 'libffi'),)
michael@0 64 """
michael@0 65 for module_tuple in modules:
michael@0 66 module = module_tuple[0]
michael@0 67 cvs_module = module_tuple[1]
michael@0 68 fullpath = os.path.join(topsrcdir, module)
michael@0 69 if os.path.exists(fullpath):
michael@0 70 print "Removing '%s'" % fullpath
michael@0 71 shutil.rmtree(fullpath)
michael@0 72
michael@0 73 (parent, leaf) = os.path.split(module)
michael@0 74 print "CVS export begin: " + datetime.datetime.utcnow().strftime("%Y-%m-%d %H:%M:%S UTC")
michael@0 75 check_call_noisy([cvs, '-d', cvsroot,
michael@0 76 'export', '-r', tag, '-d', leaf, cvs_module],
michael@0 77 cwd=os.path.join(topsrcdir, parent))
michael@0 78 print "CVS export end: " + datetime.datetime.utcnow().strftime("%Y-%m-%d %H:%M:%S UTC")
michael@0 79
michael@0 80 def toggle_trailing_blank_line(depname):
michael@0 81 """If the trailing line is empty, then we'll delete it.
michael@0 82 Otherwise we'll add a blank line."""
michael@0 83 lines = open(depname, "r").readlines()
michael@0 84 if not lines:
michael@0 85 print >>sys.stderr, "unexpected short file"
michael@0 86 return
michael@0 87
michael@0 88 if not lines[-1].strip():
michael@0 89 # trailing line is blank, removing it
michael@0 90 open(depname, "wb").writelines(lines[:-1])
michael@0 91 else:
michael@0 92 # adding blank line
michael@0 93 open(depname, "ab").write("\n")
michael@0 94
michael@0 95 def get_trailing_blank_line_state(depname):
michael@0 96 lines = open(depname, "r").readlines()
michael@0 97 if not lines:
michael@0 98 print >>sys.stderr, "unexpected short file"
michael@0 99 return "no blank line"
michael@0 100
michael@0 101 if not lines[-1].strip():
michael@0 102 return "has blank line"
michael@0 103 else:
michael@0 104 return "no blank line"
michael@0 105
michael@0 106 def update_nspr_or_nss(tag, depfile, destination, hgpath):
michael@0 107 print "reverting to HG version of %s to get its blank line state" % depfile
michael@0 108 check_call_noisy([options.hg, 'revert', depfile])
michael@0 109 old_state = get_trailing_blank_line_state(depfile)
michael@0 110 print "old state of %s is: %s" % (depfile, old_state)
michael@0 111 do_hg_replace(destination, hgpath, tag, HG_EXCLUSIONS, options.hg)
michael@0 112 new_state = get_trailing_blank_line_state(depfile)
michael@0 113 print "new state of %s is: %s" % (depfile, new_state)
michael@0 114 if old_state == new_state:
michael@0 115 print "toggling blank line in: ", depfile
michael@0 116 toggle_trailing_blank_line(depfile)
michael@0 117 tag_file = destination + "/TAG-INFO"
michael@0 118 print >>file(tag_file, "w"), tag
michael@0 119
michael@0 120 o = OptionParser(usage="client.py [options] update_nspr tagname | update_nss tagname | update_libffi tagname")
michael@0 121 o.add_option("--skip-mozilla", dest="skip_mozilla",
michael@0 122 action="store_true", default=False,
michael@0 123 help="Obsolete")
michael@0 124
michael@0 125 o.add_option("--cvs", dest="cvs", default=os.environ.get('CVS', 'cvs'),
michael@0 126 help="The location of the cvs binary")
michael@0 127 o.add_option("--cvsroot", dest="cvsroot",
michael@0 128 help="The CVSROOT for libffi (default : %s)" % CVSROOT_LIBFFI)
michael@0 129 o.add_option("--hg", dest="hg", default=os.environ.get('HG', 'hg'),
michael@0 130 help="The location of the hg binary")
michael@0 131 o.add_option("--repo", dest="repo",
michael@0 132 help="the repo to update from (default: upstream repo)")
michael@0 133
michael@0 134 try:
michael@0 135 options, args = o.parse_args()
michael@0 136 action = args[0]
michael@0 137 except IndexError:
michael@0 138 o.print_help()
michael@0 139 sys.exit(2)
michael@0 140
michael@0 141 if action in ('checkout', 'co'):
michael@0 142 print >>sys.stderr, "Warning: client.py checkout is obsolete."
michael@0 143 pass
michael@0 144 elif action in ('update_nspr'):
michael@0 145 tag, = args[1:]
michael@0 146 depfile = "nsprpub/config/prdepend.h"
michael@0 147 if not options.repo:
michael@0 148 options.repo = 'https://hg.mozilla.org/projects/nspr'
michael@0 149 update_nspr_or_nss(tag, depfile, 'nsprpub', options.repo)
michael@0 150 elif action in ('update_nss'):
michael@0 151 tag, = args[1:]
michael@0 152 depfile = "security/nss/coreconf/coreconf.dep"
michael@0 153 if not options.repo:
michael@0 154 options.repo = 'https://hg.mozilla.org/projects/nss'
michael@0 155 update_nspr_or_nss(tag, depfile, 'security/nss', options.repo)
michael@0 156 elif action in ('update_libffi'):
michael@0 157 tag, = args[1:]
michael@0 158 if not options.cvsroot:
michael@0 159 options.cvsroot = CVSROOT_LIBFFI
michael@0 160 do_cvs_export(LIBFFI_DIRS, tag, options.cvsroot, options.cvs)
michael@0 161 else:
michael@0 162 o.print_help()
michael@0 163 sys.exit(2)

mercurial