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 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
120 o
= OptionParser(usage
="client.py [options] update_nspr tagname | update_nss tagname | update_libffi tagname")
121 o
.add_option("--skip-mozilla", dest
="skip_mozilla",
122 action
="store_true", default
=False,
125 o
.add_option("--cvs", dest
="cvs", default
=os
.environ
.get('CVS', 'cvs'),
126 help="The location of the cvs binary")
127 o
.add_option("--cvsroot", dest
="cvsroot",
128 help="The CVSROOT for libffi (default : %s)" % CVSROOT_LIBFFI
)
129 o
.add_option("--hg", dest
="hg", default
=os
.environ
.get('HG', 'hg'),
130 help="The location of the hg binary")
131 o
.add_option("--repo", dest
="repo",
132 help="the repo to update from (default: upstream repo)")
135 options
, args
= o
.parse_args()
141 if action
in ('checkout', 'co'):
142 print >>sys
.stderr
, "Warning: client.py checkout is obsolete."
144 elif action
in ('update_nspr'):
146 depfile
= "nsprpub/config/prdepend.h"
148 options
.repo
= 'https://hg.mozilla.org/projects/nspr'
149 update_nspr_or_nss(tag
, depfile
, 'nsprpub', options
.repo
)
150 elif action
in ('update_nss'):
152 depfile
= "security/nss/coreconf/coreconf.dep"
154 options
.repo
= 'https://hg.mozilla.org/projects/nss'
155 update_nspr_or_nss(tag
, depfile
, 'security/nss', options
.repo
)
156 elif action
in ('update_libffi'):
158 if not options
.cvsroot
:
159 options
.cvsroot
= CVSROOT_LIBFFI
160 do_cvs_export(LIBFFI_DIRS
, tag
, options
.cvsroot
, options
.cvs
)