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/.
6 HG_EXCLUSIONS
= [".hg", ".hgignore", ".hgtags"]
12 from optparse
import OptionParser
13 from subprocess
import check_call
15 topsrcdir
= os
.path
.dirname(__file__
)
20 def check_call_noisy(cmd
, *args
, **kwargs
):
21 print("Executing command:", cmd
)
22 check_call(cmd
, *args
, **kwargs
)
25 def do_hg_pull(dir, repository
, hg
):
26 fulldir
= os
.path
.join(topsrcdir
, dir)
27 # clone if the dir doesn't exist, pull if it does
28 if not os
.path
.exists(fulldir
):
29 check_call_noisy([hg
, "clone", repository
, fulldir
])
31 cmd
= [hg
, "pull", "-u", "-R", fulldir
]
32 if repository
is not None:
33 cmd
.append(repository
)
36 [hg
, "parent", "-R", fulldir
, "--template=Updated to revision {node}.\n"]
40 def do_hg_replace(dir, repository
, tag
, exclusions
, hg
):
42 Replace the contents of dir with the contents of repository, except for
43 files matching exclusions.
45 fulldir
= os
.path
.join(topsrcdir
, dir)
46 if os
.path
.exists(fulldir
):
47 shutil
.rmtree(fulldir
)
49 assert not os
.path
.exists(fulldir
)
50 check_call_noisy([hg
, "clone", "-u", tag
, repository
, fulldir
])
52 for thing
in exclusions
:
53 for excluded
in glob
.iglob(os
.path
.join(fulldir
, thing
)):
54 if os
.path
.isdir(excluded
):
55 shutil
.rmtree(excluded
)
60 def toggle_trailing_blank_line(depname
):
61 """If the trailing line is empty, then we'll delete it.
62 Otherwise we'll add a blank line."""
63 lines
= open(depname
, "rb").readlines()
65 print("unexpected short file", file=sys
.stderr
)
68 if not lines
[-1].strip():
69 # trailing line is blank, removing it
70 open(depname
, "wb").writelines(lines
[:-1])
73 open(depname
, "ab").write(b
"\n")
76 def get_trailing_blank_line_state(depname
):
77 lines
= open(depname
, "r").readlines()
79 print("unexpected short file", file=sys
.stderr
)
80 return "no blank line"
82 if not lines
[-1].strip():
83 return "has blank line"
84 return "no blank line"
87 def update_nspr_or_nss(tag
, depfile
, destination
, hgpath
):
88 destination
= destination
.rstrip("/")
89 permanent_patch_dir
= destination
+ "/patches"
90 temporary_patch_dir
= destination
+ ".patches"
91 if os
.path
.exists(temporary_patch_dir
):
92 print("please clean up leftover directory " + temporary_patch_dir
)
94 warn_if_patch_exists(permanent_patch_dir
)
95 # protect patch directory from being removed by do_hg_replace
96 if os
.path
.exists(permanent_patch_dir
):
97 shutil
.move(permanent_patch_dir
, temporary_patch_dir
)
98 # now update the destination
99 print("reverting to HG version of %s to get its blank line state" % depfile
)
100 check_call_noisy([options
.hg
, "revert", depfile
])
101 old_state
= get_trailing_blank_line_state(depfile
)
102 print("old state of %s is: %s" % (depfile
, old_state
))
103 do_hg_replace(destination
, hgpath
, tag
, HG_EXCLUSIONS
, options
.hg
)
104 new_state
= get_trailing_blank_line_state(depfile
)
105 print("new state of %s is: %s" % (depfile
, new_state
))
106 if old_state
== new_state
:
107 print("toggling blank line in: ", depfile
)
108 toggle_trailing_blank_line(depfile
)
109 tag_file
= destination
+ "/TAG-INFO"
110 with
open(tag_file
, "w") as f
:
112 # move patch directory back to a subdirectory
113 if os
.path
.exists(temporary_patch_dir
):
114 shutil
.move(temporary_patch_dir
, permanent_patch_dir
)
117 def warn_if_patch_exists(path
):
118 # If the given patch directory exists and contains at least one file,
119 # then print warning and wait for the user to acknowledge.
120 if os
.path
.isdir(path
) and os
.listdir(path
):
121 print("========================================")
122 print("WARNING: At least one patch file exists")
123 print("in directory: " + path
)
124 print("You must manually re-apply all patches")
125 print("after this script has completed!")
126 print("========================================")
127 input("Press Enter to continue...")
131 o
= OptionParser(usage
="client.py [options] update_nspr tagname | update_nss tagname")
143 default
=os
.environ
.get("HG", "hg"),
144 help="The location of the hg binary",
147 "--repo", dest
="repo", help="the repo to update from (default: upstream repo)"
151 options
, args
= o
.parse_args()
157 if action
in ("checkout", "co"):
158 print("Warning: client.py checkout is obsolete.", file=sys
.stderr
)
160 elif action
in ("update_nspr"):
162 depfile
= "nsprpub/config/prdepend.h"
164 options
.repo
= "https://hg.mozilla.org/projects/nspr"
165 update_nspr_or_nss(tag
, depfile
, "nsprpub", options
.repo
)
166 elif action
in ("update_nss"):
168 depfile
= "security/nss/coreconf/coreconf.dep"
170 options
.repo
= "https://hg.mozilla.org/projects/nss"
171 update_nspr_or_nss(tag
, depfile
, "security/nss", options
.repo
)