blob: 436513c80ef470f220d4875237d6b6c8c44429d3 [file] [log] [blame]
Dan Jacques3d9b5882017-07-12 22:14:26 +00001# Copyright 2016 The Chromium Authors. All rights reserved.
2# Use of this source code is governed by a BSD-style license that can be
3# found in the LICENSE file.
4
5import argparse
6import collections
7import contextlib
8import fnmatch
9import hashlib
10import logging
11import os
12import platform
13import posixpath
14import shutil
15import string
16import subprocess
17import sys
18import tempfile
19
20
21THIS_DIR = os.path.abspath(os.path.dirname(__file__))
22ROOT_DIR = os.path.abspath(os.path.join(THIS_DIR, '..', '..'))
23
24DEVNULL = open(os.devnull, 'w')
25
26BAT_EXT = '.bat' if sys.platform.startswith('win') else ''
27
28# Top-level stubs to generate that fall through to executables within the Git
29# directory.
30STUBS = {
31 'git.bat': 'cmd\\git.exe',
32 'gitk.bat': 'cmd\\gitk.exe',
33 'ssh.bat': 'usr\\bin\\ssh.exe',
34 'ssh-keygen.bat': 'usr\\bin\\ssh-keygen.exe',
35}
36
37
38# Accumulated template parameters for generated stubs.
39class Template(collections.namedtuple('Template', (
40 'PYTHON_RELDIR', 'PYTHON_BIN_RELDIR', 'PYTHON_BIN_RELDIR_UNIX',
41 'GIT_BIN_RELDIR', 'GIT_BIN_RELDIR_UNIX', 'GIT_PROGRAM',
42 ))):
43
44 @classmethod
45 def empty(cls):
46 return cls(**{k: None for k in cls._fields})
47
48 def maybe_install(self, name, dst_path):
49 """Installs template |name| to |dst_path| if it has changed.
50
51 This loads the template |name| from THIS_DIR, resolves template parameters,
52 and installs it to |dst_path|. See `maybe_update` for more information.
53
54 Args:
55 name (str): The name of the template to install.
56 dst_path (str): The destination filesystem path.
57
58 Returns (bool): True if |dst_path| was updated, False otherwise.
59 """
60 template_path = os.path.join(THIS_DIR, name)
61 with open(template_path, 'r') as fd:
62 t = string.Template(fd.read())
63 return maybe_update(t.safe_substitute(self._asdict()), dst_path)
64
65
66def maybe_update(content, dst_path):
67 """Writes |content| to |dst_path| if |dst_path| does not already match.
68
69 This function will ensure that there is a file at |dst_path| containing
70 |content|. If |dst_path| already exists and contains |content|, no operation
71 will be performed, preserving filesystem modification times and avoiding
72 potential write contention.
73
74 Args:
75 content (str): The file content.
76 dst_path (str): The destination filesystem path.
77
78 Returns (bool): True if |dst_path| was updated, False otherwise.
79 """
80 # If the path already exists and matches the new content, refrain from writing
81 # a new one.
82 if os.path.exists(dst_path):
83 with open(dst_path, 'r') as fd:
84 if fd.read() == content:
85 return False
86
87 logging.debug('Updating %r', dst_path)
88 with open(dst_path, 'w') as fd:
89 fd.write(content)
90 return True
91
92
93def maybe_copy(src_path, dst_path):
94 """Writes the content of |src_path| to |dst_path| if needed.
95
96 See `maybe_update` for more information.
97
98 Args:
99 src_path (str): The content source filesystem path.
100 dst_path (str): The destination filesystem path.
101
102 Returns (bool): True if |dst_path| was updated, False otherwise.
103 """
104 with open(src_path, 'r') as fd:
105 content = fd.read()
106 return maybe_update(content, dst_path)
107
108
109def call_if_outdated(stamp_path, stamp_version, fn):
110 """Invokes |fn| if the stamp at |stamp_path| doesn't match |stamp_version|.
111
112 This can be used to keep a filesystem record of whether an operation has been
113 performed. The record is stored at |stamp_path|. To invalidate a record,
114 change the value of |stamp_version|.
115
116 After |fn| completes successfully, |stamp_path| will be updated to match
117 |stamp_version|, preventing the same update from happening in the future.
118
119 Args:
120 stamp_path (str): The filesystem path of the stamp file.
121 stamp_version (str): The desired stamp version.
122 fn (callable): A callable to invoke if the current stamp version doesn't
123 match |stamp_version|.
124
125 Returns (bool): True if an update occurred.
126 """
127
128 stamp_version = stamp_version.strip()
129 if os.path.isfile(stamp_path):
130 with open(stamp_path, 'r') as fd:
131 current_version = fd.read().strip()
132 if current_version == stamp_version:
133 return False
134
135 fn()
136
137 with open(stamp_path, 'w') as fd:
138 fd.write(stamp_version)
139 return True
140
141
142def _in_use(path):
143 """Checks if a Windows file is in use.
144
145 When Windows is using an executable, it prevents other writers from
146 modifying or deleting that executable. We can safely test for an in-use
147 file by opening it in write mode and checking whether or not there was
148 an error.
149
150 Returns (bool): True if the file was in use, False if not.
151 """
152 try:
153 with open(path, 'r+'):
154 return False
155 except IOError:
156 return True
157
158
159def _toolchain_in_use(toolchain_path):
160 """Returns (bool): True if a toolchain rooted at |path| is in use.
161 """
162 # Look for Python files that may be in use.
163 for python_dir in (
164 os.path.join(toolchain_path, 'python', 'bin'), # CIPD
165 toolchain_path, # Legacy ZIP distributions.
166 ):
167 for component in (
168 os.path.join(python_dir, 'python.exe'),
169 os.path.join(python_dir, 'DLLs', 'unicodedata.pyd'),
170 ):
171 if os.path.isfile(component) and _in_use(component):
172 return True
173 return False
174
175
176
177def _check_call(argv, stdin_input=None, **kwargs):
178 """Wrapper for subprocess.check_call that adds logging."""
179 logging.info('running %r', argv)
180 if stdin_input is not None:
181 kwargs['stdin'] = subprocess.PIPE
182 proc = subprocess.Popen(argv, **kwargs)
183 proc.communicate(input=stdin_input)
184 if proc.returncode:
185 raise subprocess.CalledProcessError(proc.returncode, argv, None)
186
187
188def _safe_rmtree(path):
189 if not os.path.exists(path):
190 return
191
192 def _make_writable_and_remove(path):
193 st = os.stat(path)
194 new_mode = st.st_mode | 0200
195 if st.st_mode == new_mode:
196 return False
197 try:
198 os.chmod(path, new_mode)
199 os.remove(path)
200 return True
201 except Exception:
202 return False
203
204 def _on_error(function, path, excinfo):
205 if not _make_writable_and_remove(path):
206 logging.warning('Failed to %s: %s (%s)', function, path, excinfo)
207
208 shutil.rmtree(path, onerror=_on_error)
209
210
Dan Jacques3d9b5882017-07-12 22:14:26 +0000211def clean_up_old_installations(skip_dir):
212 """Removes Python installations other than |skip_dir|.
213
214 This includes an "in-use" check against the "python.exe" in a given directory
215 to avoid removing Python executables that are currently ruinning. We need
216 this because our Python bootstrap may be run after (and by) other software
217 that is using the bootstrapped Python!
218 """
219 root_contents = os.listdir(ROOT_DIR)
220 for f in ('win_tools-*_bin', 'python27*_bin', 'git-*_bin'):
221 for entry in fnmatch.filter(root_contents, f):
222 full_entry = os.path.join(ROOT_DIR, entry)
223 if full_entry == skip_dir or not os.path.isdir(full_entry):
224 continue
225
226 logging.info('Cleaning up old installation %r', entry)
227 if not _toolchain_in_use(full_entry):
228 _safe_rmtree(full_entry)
229 else:
230 logging.info('Toolchain at %r is in-use; skipping', full_entry)
231
232
Dan Jacques3d9b5882017-07-12 22:14:26 +0000233# Version of "git_postprocess" system configuration (see |git_postprocess|).
234GIT_POSTPROCESS_VERSION = '1'
235
236
237def git_get_mingw_dir(git_directory):
238 """Returns (str) The "mingw" directory in a Git installation, or None."""
239 for candidate in ('mingw64', 'mingw32'):
240 mingw_dir = os.path.join(git_directory, candidate)
241 if os.path.isdir(mingw_dir):
242 return mingw_dir
243 return None
244
245
246def git_postprocess(template, git_directory):
247 # Update depot_tools files for "git help <command>"
248 mingw_dir = git_get_mingw_dir(git_directory)
249 if mingw_dir:
250 docsrc = os.path.join(ROOT_DIR, 'man', 'html')
251 git_docs_dir = os.path.join(mingw_dir, 'share', 'doc', 'git-doc')
252 for name in os.listdir(docsrc):
253 maybe_copy(
254 os.path.join(docsrc, name),
255 os.path.join(git_docs_dir, name))
256 else:
257 logging.info('Could not find mingw directory for %r.', git_directory)
258
259 # Create Git templates and configure its base layout.
260 for stub_name, relpath in STUBS.iteritems():
261 stub_template = template._replace(GIT_PROGRAM=relpath)
262 stub_template.maybe_install(
263 'git.template.bat',
264 os.path.join(ROOT_DIR, stub_name))
265
266 # Set-up our system configuration environment. The following set of
267 # parameters is versioned by "GIT_POSTPROCESS_VERSION". If they change,
268 # update "GIT_POSTPROCESS_VERSION" accordingly.
269 def configure_git_system():
270 git_bat_path = os.path.join(ROOT_DIR, 'git.bat')
271 _check_call([git_bat_path, 'config', '--system', 'core.autocrlf', 'false'])
272 _check_call([git_bat_path, 'config', '--system', 'core.filemode', 'false'])
273 _check_call([git_bat_path, 'config', '--system', 'core.preloadindex',
274 'true'])
275 _check_call([git_bat_path, 'config', '--system', 'core.fscache', 'true'])
276
277 call_if_outdated(
278 os.path.join(git_directory, '.git_postprocess'),
279 GIT_POSTPROCESS_VERSION,
280 configure_git_system)
281
282
283def main(argv):
284 parser = argparse.ArgumentParser()
285 parser.add_argument('--verbose', action='store_true')
Dan Jacquesc4dd3e82017-07-13 23:46:20 +0000286 parser.add_argument('--win-tools-name', required=True,
287 help='The directory of the Python installation.')
Dan Jacques3d9b5882017-07-12 22:14:26 +0000288 parser.add_argument('--bleeding-edge', action='store_true',
289 help='Force bleeding edge Git.')
Dan Jacques3d9b5882017-07-12 22:14:26 +0000290 args = parser.parse_args(argv)
291
292 logging.basicConfig(level=logging.DEBUG if args.verbose else logging.WARN)
293
Dan Jacquesc4dd3e82017-07-13 23:46:20 +0000294 template = Template.empty()._replace(
295 PYTHON_RELDIR=os.path.join(args.win_tools_name, 'python'),
296 PYTHON_BIN_RELDIR=os.path.join(args.win_tools_name, 'python', 'bin'),
297 PYTHON_BIN_RELDIR_UNIX=posixpath.join(
298 args.win_tools_name, 'python', 'bin'),
299 GIT_BIN_RELDIR=os.path.join(args.win_tools_name, 'git'),
300 GIT_BIN_RELDIR_UNIX=posixpath.join(args.win_tools_name, 'git'))
Dan Jacques3d9b5882017-07-12 22:14:26 +0000301
Dan Jacquesc4dd3e82017-07-13 23:46:20 +0000302 win_tools_dir = os.path.join(ROOT_DIR, args.win_tools_name)
303 git_postprocess(template, os.path.join(win_tools_dir, 'git'))
Dan Jacques3d9b5882017-07-12 22:14:26 +0000304
Dan Jacquesc4dd3e82017-07-13 23:46:20 +0000305 # Clean up any old Python and Git installations.
306 clean_up_old_installations(win_tools_dir)
Dan Jacques3d9b5882017-07-12 22:14:26 +0000307
308 # Emit our Python bin depot-tools-relative directory. This is ready by
309 # "python.bat" to identify the path of the current Python installation.
310 #
311 # We use this indirection so that upgrades can change this pointer to
312 # redirect "python.bat" to a new Python installation. We can't just update
313 # "python.bat" because batch file executions reload the batch file and seek
314 # to the previous cursor in between every command, so changing the batch
315 # file contents could invalidate any existing executions.
316 #
317 # The intention is that the batch file itself never needs to change when
318 # switching Python versions.
319 maybe_update(
320 template.PYTHON_BIN_RELDIR,
321 os.path.join(ROOT_DIR, 'python_bin_reldir.txt'))
322
Dan Jacques642dd842017-07-19 19:32:18 -0700323 python_bat_template = ('python27.new.bat' if not args.bleeding_edge
324 else 'python27.bleeding_edge.bat')
325
Dan Jacques3d9b5882017-07-12 22:14:26 +0000326 # Re-evaluate and regenerate our root templated files.
327 for src_name, dst_name in (
328 ('git-bash.template.sh', 'git-bash'),
329 ('pylint.new.bat', 'pylint.bat'),
Dan Jacques642dd842017-07-19 19:32:18 -0700330 (python_bat_template, 'python.bat'),
Dan Jacques3d9b5882017-07-12 22:14:26 +0000331 ):
332 template.maybe_install(src_name, os.path.join(ROOT_DIR, dst_name))
333
334 return 0
335
336
337if __name__ == '__main__':
338 sys.exit(main(sys.argv[1:]))