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