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/.
7 LIBFFI_DIRS
= (('js/ctypes/libffi', 'libffi'),)
8 HG_EXCLUSIONS
= ['.hg', '.hgignore', '.hgtags']
10 CVSROOT_LIBFFI
= ':pserver:anoncvs@sources.redhat.com:/cvs/libffi'
17 from optparse
import OptionParser
18 from subprocess
import check_call
20 topsrcdir
= os
.path
.dirname(__file__
)
24 def check_call_noisy(cmd
, *args
, **kwargs
):
25 print "Executing command:", cmd
26 check_call(cmd
, *args
, **kwargs
)
28 def 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
])
34 cmd
= [hg
, 'pull', '-u', '-R', fulldir
]
35 if repository
is not None:
36 cmd
.append(repository
)
38 check_call([hg
, 'parent', '-R', fulldir
,
39 '--template=Updated to revision {node}.\n'])
41 def do_hg_replace(dir, repository
, tag
, exclusions
, hg
):
43 Replace the contents of dir with the contents of repository, except for
44 files matching exclusions.
46 fulldir
= os
.path
.join(topsrcdir
, dir)
47 if os
.path
.exists(fulldir
):
48 shutil
.rmtree(fulldir
)
50 assert not os
.path
.exists(fulldir
)
51 check_call_noisy([hg
, 'clone', '-u', tag
, repository
, fulldir
])
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
)
60 def 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'),)
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
)
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")
80 def 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()
85 print >>sys
.stderr
, "unexpected short file"
88 if not lines
[-1].strip():
89 # trailing line is blank, removing it
90 open(depname
, "wb").writelines(lines
[:-1])
93 open(depname
, "ab").write("\n")
95 def get_trailing_blank_line_state(depname
):
96 lines
= open(depname
, "r").readlines()
98 print >>sys
.stderr
, "unexpected short file"
99 return "no blank line"
101 if not lines
[-1].strip():
102 return "has blank line"
104 return "no blank line"
106 def update_nspr_or_nss(tag
, depfile
, destination
, hgpath
):
107 destination
= destination
.rstrip('/')
108 permanent_patch_dir
= destination
+ '/patches'
109 temporary_patch_dir
= destination
+ '.patches'
110 if os
.path
.exists(temporary_patch_dir
):
111 print "please clean up leftover directory " + temporary_patch_dir
113 warn_if_patch_exists(permanent_patch_dir
)
114 # protect patch directory from being removed by do_hg_replace
115 if os
.path
.exists(permanent_patch_dir
):
116 shutil
.move(permanent_patch_dir
, temporary_patch_dir
)
117 # now update the destination
118 print "reverting to HG version of %s to get its blank line state" % depfile
119 check_call_noisy([options
.hg
, 'revert', depfile
])
120 old_state
= get_trailing_blank_line_state(depfile
)
121 print "old state of %s is: %s" % (depfile
, old_state
)
122 do_hg_replace(destination
, hgpath
, tag
, HG_EXCLUSIONS
, options
.hg
)
123 new_state
= get_trailing_blank_line_state(depfile
)
124 print "new state of %s is: %s" % (depfile
, new_state
)
125 if old_state
== new_state
:
126 print "toggling blank line in: ", depfile
127 toggle_trailing_blank_line(depfile
)
128 tag_file
= destination
+ "/TAG-INFO"
129 print >>file(tag_file
, "w"), tag
130 # move patch directory back to a subdirectory
131 if os
.path
.exists(temporary_patch_dir
):
132 shutil
.move(temporary_patch_dir
, permanent_patch_dir
)
134 def warn_if_patch_exists(path
):
135 # If the given patch directory exists and contains at least one file,
136 # then print warning and wait for the user to acknowledge.
137 if os
.path
.isdir(path
) and os
.listdir(path
):
138 print "========================================"
139 print "WARNING: At least one patch file exists"
140 print "in directory: " + path
141 print "You must manually re-apply all patches"
142 print "after this script has completed!"
143 print "========================================"
144 raw_input("Press Enter to continue...")
147 o
= OptionParser(usage
="client.py [options] update_nspr tagname | update_nss tagname | update_libffi tagname")
148 o
.add_option("--skip-mozilla", dest
="skip_mozilla",
149 action
="store_true", default
=False,
152 o
.add_option("--cvs", dest
="cvs", default
=os
.environ
.get('CVS', 'cvs'),
153 help="The location of the cvs binary")
154 o
.add_option("--cvsroot", dest
="cvsroot",
155 help="The CVSROOT for libffi (default : %s)" % CVSROOT_LIBFFI
)
156 o
.add_option("--hg", dest
="hg", default
=os
.environ
.get('HG', 'hg'),
157 help="The location of the hg binary")
158 o
.add_option("--repo", dest
="repo",
159 help="the repo to update from (default: upstream repo)")
162 options
, args
= o
.parse_args()
168 if action
in ('checkout', 'co'):
169 print >>sys
.stderr
, "Warning: client.py checkout is obsolete."
171 elif action
in ('update_nspr'):
173 depfile
= "nsprpub/config/prdepend.h"
175 options
.repo
= 'https://hg.mozilla.org/projects/nspr'
176 update_nspr_or_nss(tag
, depfile
, 'nsprpub', options
.repo
)
177 elif action
in ('update_nss'):
179 depfile
= "security/nss/coreconf/coreconf.dep"
181 options
.repo
= 'https://hg.mozilla.org/projects/nss'
182 update_nspr_or_nss(tag
, depfile
, 'security/nss', options
.repo
)
183 elif action
in ('update_libffi'):
185 if not options
.cvsroot
:
186 options
.cvsroot
= CVSROOT_LIBFFI
187 do_cvs_export(LIBFFI_DIRS
, tag
, options
.cvsroot
, options
.cvs
)