blob: d796f0f6117c35e7dc66b2be38486aaadbb47fcb [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
211@contextlib.contextmanager
212def _tempdir():
213 tdir = None
214 try:
215 tdir = tempfile.mkdtemp()
216 yield tdir
217 finally:
218 _safe_rmtree(tdir)
219
220
221def get_os_bitness():
222 """Returns bitness of operating system as int."""
223 return 64 if platform.machine().endswith('64') else 32
224
225
226def get_target_git_version(args):
227 """Returns git version that should be installed."""
228 if args.bleeding_edge:
229 git_version_file = 'git_version_bleeding_edge.txt'
230 else:
231 git_version_file = 'git_version.txt'
232 with open(os.path.join(THIS_DIR, git_version_file)) as f:
233 return f.read().strip()
234
235
236def clean_up_old_git_installations(git_directory, force):
237 """Removes git installations other than |git_directory|."""
238 for entry in fnmatch.filter(os.listdir(ROOT_DIR), 'git-*_bin'):
239 full_entry = os.path.join(ROOT_DIR, entry)
240 if force or full_entry != git_directory:
241 logging.info('Cleaning up old git installation %r', entry)
242 _safe_rmtree(full_entry)
243
244
245def clean_up_old_installations(skip_dir):
246 """Removes Python installations other than |skip_dir|.
247
248 This includes an "in-use" check against the "python.exe" in a given directory
249 to avoid removing Python executables that are currently ruinning. We need
250 this because our Python bootstrap may be run after (and by) other software
251 that is using the bootstrapped Python!
252 """
253 root_contents = os.listdir(ROOT_DIR)
254 for f in ('win_tools-*_bin', 'python27*_bin', 'git-*_bin'):
255 for entry in fnmatch.filter(root_contents, f):
256 full_entry = os.path.join(ROOT_DIR, entry)
257 if full_entry == skip_dir or not os.path.isdir(full_entry):
258 continue
259
260 logging.info('Cleaning up old installation %r', entry)
261 if not _toolchain_in_use(full_entry):
262 _safe_rmtree(full_entry)
263 else:
264 logging.info('Toolchain at %r is in-use; skipping', full_entry)
265
266
267def cipd_ensure(args, dest_directory, package, version):
268 """Installs a CIPD package using "ensure"."""
269 logging.info('Installing CIPD package %r @ %r', package, version)
270 manifest_text = '%s %s\n' % (package, version)
271
272 cipd_args = [
273 args.cipd_client,
274 'ensure',
275 '-ensure-file', '-',
276 '-root', dest_directory,
277 ]
278 if args.cipd_cache_directory:
279 cipd_args.extend(['-cache-dir', args.cipd_cache_directory])
280 if args.verbose:
281 cipd_args.append('-verbose')
282 _check_call(cipd_args, stdin_input=manifest_text)
283
284
Dan Jacques3a8717e2017-07-11 18:09:20 -0700285def need_to_install_git(args, git_directory):
Dan Jacques3d9b5882017-07-12 22:14:26 +0000286 """Returns True if git needs to be installed."""
287 if args.force:
288 return True
289
290 is_cipd_managed = os.path.exists(os.path.join(git_directory, '.cipd'))
Dan Jacques3a8717e2017-07-11 18:09:20 -0700291 if not is_cipd_managed:
Dan Jacques3d9b5882017-07-12 22:14:26 +0000292 # Converting from legacy to CIPD, need reinstall.
293 return True
294
295 git_exe_path = os.path.join(git_directory, 'bin', 'git.exe')
296 if not os.path.exists(git_exe_path):
297 return True
298 if subprocess.call(
299 [git_exe_path, '--version'],
300 stdout=DEVNULL, stderr=DEVNULL) != 0:
301 return True
302
303 gen_stubs = STUBS.keys()
304 gen_stubs.append('git-bash')
305 for stub in gen_stubs:
306 full_path = os.path.join(ROOT_DIR, stub)
307 if not os.path.exists(full_path):
308 return True
309 with open(full_path) as f:
310 if os.path.relpath(git_directory, ROOT_DIR) not in f.read():
311 return True
312
313 return False
314
315
Dan Jacques3a8717e2017-07-11 18:09:20 -0700316def install_git(args, git_version, git_directory):
Dan Jacques3d9b5882017-07-12 22:14:26 +0000317 """Installs |git_version| into |git_directory|."""
318 # TODO: Remove legacy version once everyone is on bundled Git.
319 cipd_platform = 'windows-%s' % ('amd64' if args.bits == 64 else '386')
Dan Jacques3a8717e2017-07-11 18:09:20 -0700320 # When migrating from legacy, we want to nuke this directory. In other
321 # cases, CIPD will handle the cleanup.
322 if not os.path.isdir(os.path.join(git_directory, '.cipd')):
323 logging.info('Deleting legacy Git directory: %s', git_directory)
324 _safe_rmtree(git_directory)
Dan Jacques3d9b5882017-07-12 22:14:26 +0000325
Dan Jacques3a8717e2017-07-11 18:09:20 -0700326 cipd_ensure(args, git_directory,
327 package='infra/git/%s' % (cipd_platform,),
328 version=git_version)
Dan Jacques3d9b5882017-07-12 22:14:26 +0000329
330
331def ensure_git(args, template):
332 git_version = get_target_git_version(args)
333
334 git_directory_tag = git_version.split(':')
335 git_directory = os.path.join(
336 ROOT_DIR, 'git-%s-%s_bin' % (git_directory_tag[-1], args.bits))
337
338 clean_up_old_git_installations(git_directory, args.force)
339
340 git_bin_dir = os.path.relpath(git_directory, ROOT_DIR)
341 template = template._replace(
342 GIT_BIN_RELDIR=git_bin_dir,
343 GIT_BIN_RELDIR_UNIX=git_bin_dir)
344
Dan Jacques3a8717e2017-07-11 18:09:20 -0700345 if need_to_install_git(args, git_directory):
346 install_git(args, git_version, git_directory)
Dan Jacques3d9b5882017-07-12 22:14:26 +0000347
348 git_postprocess(template, git_directory)
349
350 return template
351
352
353# Version of "git_postprocess" system configuration (see |git_postprocess|).
354GIT_POSTPROCESS_VERSION = '1'
355
356
357def git_get_mingw_dir(git_directory):
358 """Returns (str) The "mingw" directory in a Git installation, or None."""
359 for candidate in ('mingw64', 'mingw32'):
360 mingw_dir = os.path.join(git_directory, candidate)
361 if os.path.isdir(mingw_dir):
362 return mingw_dir
363 return None
364
365
366def git_postprocess(template, git_directory):
367 # Update depot_tools files for "git help <command>"
368 mingw_dir = git_get_mingw_dir(git_directory)
369 if mingw_dir:
370 docsrc = os.path.join(ROOT_DIR, 'man', 'html')
371 git_docs_dir = os.path.join(mingw_dir, 'share', 'doc', 'git-doc')
372 for name in os.listdir(docsrc):
373 maybe_copy(
374 os.path.join(docsrc, name),
375 os.path.join(git_docs_dir, name))
376 else:
377 logging.info('Could not find mingw directory for %r.', git_directory)
378
379 # Create Git templates and configure its base layout.
380 for stub_name, relpath in STUBS.iteritems():
381 stub_template = template._replace(GIT_PROGRAM=relpath)
382 stub_template.maybe_install(
383 'git.template.bat',
384 os.path.join(ROOT_DIR, stub_name))
385
386 # Set-up our system configuration environment. The following set of
387 # parameters is versioned by "GIT_POSTPROCESS_VERSION". If they change,
388 # update "GIT_POSTPROCESS_VERSION" accordingly.
389 def configure_git_system():
390 git_bat_path = os.path.join(ROOT_DIR, 'git.bat')
391 _check_call([git_bat_path, 'config', '--system', 'core.autocrlf', 'false'])
392 _check_call([git_bat_path, 'config', '--system', 'core.filemode', 'false'])
393 _check_call([git_bat_path, 'config', '--system', 'core.preloadindex',
394 'true'])
395 _check_call([git_bat_path, 'config', '--system', 'core.fscache', 'true'])
396
397 call_if_outdated(
398 os.path.join(git_directory, '.git_postprocess'),
399 GIT_POSTPROCESS_VERSION,
400 configure_git_system)
401
402
403def main(argv):
404 parser = argparse.ArgumentParser()
405 parser.add_argument('--verbose', action='store_true')
406 parser.add_argument('--win-tools-name',
407 help='The directory of the Python installation. '
408 '(legacy) If missing, use legacy Windows tools '
409 'processing')
410 parser.add_argument('--bleeding-edge', action='store_true',
411 help='Force bleeding edge Git.')
412
413 group = parser.add_argument_group('legacy flags')
414 group.add_argument('--force', action='store_true',
415 help='Always re-install everything.')
416 group.add_argument('--bits', type=int, choices=(32,64),
417 help='Bitness of the client to install. Default on this'
418 ' system: %(default)s', default=get_os_bitness())
419 group.add_argument('--cipd-client',
420 help='Path to CIPD client binary. default: %(default)s',
421 default=os.path.join(ROOT_DIR, 'cipd'+BAT_EXT))
422 group.add_argument('--cipd-cache-directory',
423 help='Path to CIPD cache directory.')
424 args = parser.parse_args(argv)
425
426 logging.basicConfig(level=logging.DEBUG if args.verbose else logging.WARN)
427
428 template = Template.empty()
429 if not args.win_tools_name:
Dan Jacques3a8717e2017-07-11 18:09:20 -0700430 # Legacy (non-CIPD) support.
Dan Jacques3d9b5882017-07-12 22:14:26 +0000431 template = template._replace(
432 PYTHON_RELDIR='python276_bin',
433 PYTHON_BIN_RELDIR='python276_bin',
434 PYTHON_BIN_RELDIR_UNIX='python276_bin')
435 template = ensure_git(args, template)
436 else:
437 template = template._replace(
438 PYTHON_RELDIR=os.path.join(args.win_tools_name, 'python'),
439 PYTHON_BIN_RELDIR=os.path.join(args.win_tools_name, 'python', 'bin'),
440 PYTHON_BIN_RELDIR_UNIX=posixpath.join(
441 args.win_tools_name, 'python', 'bin'),
442 GIT_BIN_RELDIR=os.path.join(args.win_tools_name, 'git'),
443 GIT_BIN_RELDIR_UNIX=posixpath.join(args.win_tools_name, 'git'))
444
445 win_tools_dir = os.path.join(ROOT_DIR, args.win_tools_name)
446 git_postprocess(template, os.path.join(win_tools_dir, 'git'))
447
448 # Clean up any old Python installations.
449 clean_up_old_installations(win_tools_dir)
450
451 # Emit our Python bin depot-tools-relative directory. This is ready by
452 # "python.bat" to identify the path of the current Python installation.
453 #
454 # We use this indirection so that upgrades can change this pointer to
455 # redirect "python.bat" to a new Python installation. We can't just update
456 # "python.bat" because batch file executions reload the batch file and seek
457 # to the previous cursor in between every command, so changing the batch
458 # file contents could invalidate any existing executions.
459 #
460 # The intention is that the batch file itself never needs to change when
461 # switching Python versions.
462 maybe_update(
463 template.PYTHON_BIN_RELDIR,
464 os.path.join(ROOT_DIR, 'python_bin_reldir.txt'))
465
466 # Install our "python.bat" shim.
467 # TODO: Move this to generic shim installation once legacy support is
468 # removed and this code path is the only one.
469 template.maybe_install(
470 'python27.new.bat',
471 os.path.join(ROOT_DIR, 'python.bat'))
472
473 # Re-evaluate and regenerate our root templated files.
474 for src_name, dst_name in (
475 ('git-bash.template.sh', 'git-bash'),
476 ('pylint.new.bat', 'pylint.bat'),
477 ):
478 template.maybe_install(src_name, os.path.join(ROOT_DIR, dst_name))
479
480 return 0
481
482
483if __name__ == '__main__':
484 sys.exit(main(sys.argv[1:]))