michael@0: #!/usr/bin/python michael@0: # This Source Code Form is subject to the terms of the Mozilla Public michael@0: # License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: # file, You can obtain one at http://mozilla.org/MPL/2.0/. michael@0: michael@0: michael@0: LIBFFI_DIRS = (('js/ctypes/libffi', 'libffi'),) michael@0: HG_EXCLUSIONS = ['.hg', '.hgignore', '.hgtags'] michael@0: michael@0: CVSROOT_LIBFFI = ':pserver:anoncvs@sources.redhat.com:/cvs/libffi' michael@0: michael@0: import os michael@0: import sys michael@0: import datetime michael@0: import shutil michael@0: import glob michael@0: from optparse import OptionParser michael@0: from subprocess import check_call michael@0: michael@0: topsrcdir = os.path.dirname(__file__) michael@0: if topsrcdir == '': michael@0: topsrcdir = '.' michael@0: michael@0: def check_call_noisy(cmd, *args, **kwargs): michael@0: print "Executing command:", cmd michael@0: check_call(cmd, *args, **kwargs) michael@0: michael@0: def do_hg_pull(dir, repository, hg): michael@0: fulldir = os.path.join(topsrcdir, dir) michael@0: # clone if the dir doesn't exist, pull if it does michael@0: if not os.path.exists(fulldir): michael@0: check_call_noisy([hg, 'clone', repository, fulldir]) michael@0: else: michael@0: cmd = [hg, 'pull', '-u', '-R', fulldir] michael@0: if repository is not None: michael@0: cmd.append(repository) michael@0: check_call_noisy(cmd) michael@0: check_call([hg, 'parent', '-R', fulldir, michael@0: '--template=Updated to revision {node}.\n']) michael@0: michael@0: def do_hg_replace(dir, repository, tag, exclusions, hg): michael@0: """ michael@0: Replace the contents of dir with the contents of repository, except for michael@0: files matching exclusions. michael@0: """ michael@0: fulldir = os.path.join(topsrcdir, dir) michael@0: if os.path.exists(fulldir): michael@0: shutil.rmtree(fulldir) michael@0: michael@0: assert not os.path.exists(fulldir) michael@0: check_call_noisy([hg, 'clone', '-u', tag, repository, fulldir]) michael@0: michael@0: for thing in exclusions: michael@0: for excluded in glob.iglob(os.path.join(fulldir, thing)): michael@0: if os.path.isdir(excluded): michael@0: shutil.rmtree(excluded) michael@0: else: michael@0: os.remove(excluded) michael@0: michael@0: def do_cvs_export(modules, tag, cvsroot, cvs): michael@0: """Check out a CVS directory without CVS metadata, using "export" michael@0: modules is a list of directories to check out and the corresponding michael@0: cvs module, e.g. (('js/ctypes/libffi', 'libffi'),) michael@0: """ michael@0: for module_tuple in modules: michael@0: module = module_tuple[0] michael@0: cvs_module = module_tuple[1] michael@0: fullpath = os.path.join(topsrcdir, module) michael@0: if os.path.exists(fullpath): michael@0: print "Removing '%s'" % fullpath michael@0: shutil.rmtree(fullpath) michael@0: michael@0: (parent, leaf) = os.path.split(module) michael@0: print "CVS export begin: " + datetime.datetime.utcnow().strftime("%Y-%m-%d %H:%M:%S UTC") michael@0: check_call_noisy([cvs, '-d', cvsroot, michael@0: 'export', '-r', tag, '-d', leaf, cvs_module], michael@0: cwd=os.path.join(topsrcdir, parent)) michael@0: print "CVS export end: " + datetime.datetime.utcnow().strftime("%Y-%m-%d %H:%M:%S UTC") michael@0: michael@0: def toggle_trailing_blank_line(depname): michael@0: """If the trailing line is empty, then we'll delete it. michael@0: Otherwise we'll add a blank line.""" michael@0: lines = open(depname, "r").readlines() michael@0: if not lines: michael@0: print >>sys.stderr, "unexpected short file" michael@0: return michael@0: michael@0: if not lines[-1].strip(): michael@0: # trailing line is blank, removing it michael@0: open(depname, "wb").writelines(lines[:-1]) michael@0: else: michael@0: # adding blank line michael@0: open(depname, "ab").write("\n") michael@0: michael@0: def get_trailing_blank_line_state(depname): michael@0: lines = open(depname, "r").readlines() michael@0: if not lines: michael@0: print >>sys.stderr, "unexpected short file" michael@0: return "no blank line" michael@0: michael@0: if not lines[-1].strip(): michael@0: return "has blank line" michael@0: else: michael@0: return "no blank line" michael@0: michael@0: def update_nspr_or_nss(tag, depfile, destination, hgpath): michael@0: print "reverting to HG version of %s to get its blank line state" % depfile michael@0: check_call_noisy([options.hg, 'revert', depfile]) michael@0: old_state = get_trailing_blank_line_state(depfile) michael@0: print "old state of %s is: %s" % (depfile, old_state) michael@0: do_hg_replace(destination, hgpath, tag, HG_EXCLUSIONS, options.hg) michael@0: new_state = get_trailing_blank_line_state(depfile) michael@0: print "new state of %s is: %s" % (depfile, new_state) michael@0: if old_state == new_state: michael@0: print "toggling blank line in: ", depfile michael@0: toggle_trailing_blank_line(depfile) michael@0: tag_file = destination + "/TAG-INFO" michael@0: print >>file(tag_file, "w"), tag michael@0: michael@0: o = OptionParser(usage="client.py [options] update_nspr tagname | update_nss tagname | update_libffi tagname") michael@0: o.add_option("--skip-mozilla", dest="skip_mozilla", michael@0: action="store_true", default=False, michael@0: help="Obsolete") michael@0: michael@0: o.add_option("--cvs", dest="cvs", default=os.environ.get('CVS', 'cvs'), michael@0: help="The location of the cvs binary") michael@0: o.add_option("--cvsroot", dest="cvsroot", michael@0: help="The CVSROOT for libffi (default : %s)" % CVSROOT_LIBFFI) michael@0: o.add_option("--hg", dest="hg", default=os.environ.get('HG', 'hg'), michael@0: help="The location of the hg binary") michael@0: o.add_option("--repo", dest="repo", michael@0: help="the repo to update from (default: upstream repo)") michael@0: michael@0: try: michael@0: options, args = o.parse_args() michael@0: action = args[0] michael@0: except IndexError: michael@0: o.print_help() michael@0: sys.exit(2) michael@0: michael@0: if action in ('checkout', 'co'): michael@0: print >>sys.stderr, "Warning: client.py checkout is obsolete." michael@0: pass michael@0: elif action in ('update_nspr'): michael@0: tag, = args[1:] michael@0: depfile = "nsprpub/config/prdepend.h" michael@0: if not options.repo: michael@0: options.repo = 'https://hg.mozilla.org/projects/nspr' michael@0: update_nspr_or_nss(tag, depfile, 'nsprpub', options.repo) michael@0: elif action in ('update_nss'): michael@0: tag, = args[1:] michael@0: depfile = "security/nss/coreconf/coreconf.dep" michael@0: if not options.repo: michael@0: options.repo = 'https://hg.mozilla.org/projects/nss' michael@0: update_nspr_or_nss(tag, depfile, 'security/nss', options.repo) michael@0: elif action in ('update_libffi'): michael@0: tag, = args[1:] michael@0: if not options.cvsroot: michael@0: options.cvsroot = CVSROOT_LIBFFI michael@0: do_cvs_export(LIBFFI_DIRS, tag, options.cvsroot, options.cvs) michael@0: else: michael@0: o.print_help() michael@0: sys.exit(2)