blob: 986d5c0c0402c7fcd92ef790a9095d15836fec18 [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
285def need_to_install_git(args, git_directory, legacy):
286 """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'))
291 if legacy:
292 if is_cipd_managed:
293 # Converting from non-legacy to legacy, need reinstall.
294 return True
295 if not os.path.exists(os.path.join(
296 git_directory, 'etc', 'profile.d', 'python.sh')):
297 return True
298 elif not is_cipd_managed:
299 # Converting from legacy to CIPD, need reinstall.
300 return True
301
302 git_exe_path = os.path.join(git_directory, 'bin', 'git.exe')
303 if not os.path.exists(git_exe_path):
304 return True
305 if subprocess.call(
306 [git_exe_path, '--version'],
307 stdout=DEVNULL, stderr=DEVNULL) != 0:
308 return True
309
310 gen_stubs = STUBS.keys()
311 gen_stubs.append('git-bash')
312 for stub in gen_stubs:
313 full_path = os.path.join(ROOT_DIR, stub)
314 if not os.path.exists(full_path):
315 return True
316 with open(full_path) as f:
317 if os.path.relpath(git_directory, ROOT_DIR) not in f.read():
318 return True
319
320 return False
321
322
323def install_git_legacy(args, git_version, git_directory, cipd_platform):
324 _safe_rmtree(git_directory)
325 with _tempdir() as temp_dir:
326 cipd_ensure(args, temp_dir,
327 package='infra/depot_tools/git_installer/%s' % cipd_platform,
328 version='v' + git_version.replace('.', '_'))
329
330 # 7-zip has weird expectations for command-line syntax. Pass it as a string
331 # to avoid subprocess module quoting breaking it. Also double-escape
332 # backslashes in paths.
333 _check_call(' '.join([
334 os.path.join(temp_dir, 'git-installer.exe'),
335 '-y',
336 '-InstallPath="%s"' % git_directory.replace('\\', '\\\\'),
337 '-Directory="%s"' % git_directory.replace('\\', '\\\\'),
338 ]))
339
340
341def install_git(args, git_version, git_directory, legacy):
342 """Installs |git_version| into |git_directory|."""
343 # TODO: Remove legacy version once everyone is on bundled Git.
344 cipd_platform = 'windows-%s' % ('amd64' if args.bits == 64 else '386')
345 if legacy:
346 install_git_legacy(args, git_version, git_directory, cipd_platform)
347 else:
348 # When migrating from legacy, we want to nuke this directory. In other
349 # cases, CIPD will handle the cleanup.
350 if not os.path.isdir(os.path.join(git_directory, '.cipd')):
351 logging.info('Deleting legacy Git directory: %s', git_directory)
352 _safe_rmtree(git_directory)
353
354 cipd_ensure(args, git_directory,
355 package='infra/git/%s' % (cipd_platform,),
356 version=git_version)
357
358 if legacy:
359 # The non-legacy Git bundle includes "python.sh".
360 #
361 # TODO: Delete "profile.d.python.sh" after legacy mode is removed.
362 shutil.copyfile(
363 os.path.join(THIS_DIR, 'profile.d.python.sh'),
364 os.path.join(git_directory, 'etc', 'profile.d', 'python.sh'))
365
366
367def ensure_git(args, template):
368 git_version = get_target_git_version(args)
369
370 git_directory_tag = git_version.split(':')
371 git_directory = os.path.join(
372 ROOT_DIR, 'git-%s-%s_bin' % (git_directory_tag[-1], args.bits))
373
374 clean_up_old_git_installations(git_directory, args.force)
375
376 git_bin_dir = os.path.relpath(git_directory, ROOT_DIR)
377 template = template._replace(
378 GIT_BIN_RELDIR=git_bin_dir,
379 GIT_BIN_RELDIR_UNIX=git_bin_dir)
380
381 # Modern Git versions use CIPD tags beginning with "version:". If the tag
382 # does not begin with that, use the legacy installer.
383 legacy = not git_version.startswith('version:')
384 if need_to_install_git(args, git_directory, legacy):
385 install_git(args, git_version, git_directory, legacy)
386
387 git_postprocess(template, git_directory)
388
389 return template
390
391
392# Version of "git_postprocess" system configuration (see |git_postprocess|).
393GIT_POSTPROCESS_VERSION = '1'
394
395
396def git_get_mingw_dir(git_directory):
397 """Returns (str) The "mingw" directory in a Git installation, or None."""
398 for candidate in ('mingw64', 'mingw32'):
399 mingw_dir = os.path.join(git_directory, candidate)
400 if os.path.isdir(mingw_dir):
401 return mingw_dir
402 return None
403
404
405def git_postprocess(template, git_directory):
406 # Update depot_tools files for "git help <command>"
407 mingw_dir = git_get_mingw_dir(git_directory)
408 if mingw_dir:
409 docsrc = os.path.join(ROOT_DIR, 'man', 'html')
410 git_docs_dir = os.path.join(mingw_dir, 'share', 'doc', 'git-doc')
411 for name in os.listdir(docsrc):
412 maybe_copy(
413 os.path.join(docsrc, name),
414 os.path.join(git_docs_dir, name))
415 else:
416 logging.info('Could not find mingw directory for %r.', git_directory)
417
418 # Create Git templates and configure its base layout.
419 for stub_name, relpath in STUBS.iteritems():
420 stub_template = template._replace(GIT_PROGRAM=relpath)
421 stub_template.maybe_install(
422 'git.template.bat',
423 os.path.join(ROOT_DIR, stub_name))
424
425 # Set-up our system configuration environment. The following set of
426 # parameters is versioned by "GIT_POSTPROCESS_VERSION". If they change,
427 # update "GIT_POSTPROCESS_VERSION" accordingly.
428 def configure_git_system():
429 git_bat_path = os.path.join(ROOT_DIR, 'git.bat')
430 _check_call([git_bat_path, 'config', '--system', 'core.autocrlf', 'false'])
431 _check_call([git_bat_path, 'config', '--system', 'core.filemode', 'false'])
432 _check_call([git_bat_path, 'config', '--system', 'core.preloadindex',
433 'true'])
434 _check_call([git_bat_path, 'config', '--system', 'core.fscache', 'true'])
435
436 call_if_outdated(
437 os.path.join(git_directory, '.git_postprocess'),
438 GIT_POSTPROCESS_VERSION,
439 configure_git_system)
440
441
442def main(argv):
443 parser = argparse.ArgumentParser()
444 parser.add_argument('--verbose', action='store_true')
445 parser.add_argument('--win-tools-name',
446 help='The directory of the Python installation. '
447 '(legacy) If missing, use legacy Windows tools '
448 'processing')
449 parser.add_argument('--bleeding-edge', action='store_true',
450 help='Force bleeding edge Git.')
451
452 group = parser.add_argument_group('legacy flags')
453 group.add_argument('--force', action='store_true',
454 help='Always re-install everything.')
455 group.add_argument('--bits', type=int, choices=(32,64),
456 help='Bitness of the client to install. Default on this'
457 ' system: %(default)s', default=get_os_bitness())
458 group.add_argument('--cipd-client',
459 help='Path to CIPD client binary. default: %(default)s',
460 default=os.path.join(ROOT_DIR, 'cipd'+BAT_EXT))
461 group.add_argument('--cipd-cache-directory',
462 help='Path to CIPD cache directory.')
463 args = parser.parse_args(argv)
464
465 logging.basicConfig(level=logging.DEBUG if args.verbose else logging.WARN)
466
467 template = Template.empty()
468 if not args.win_tools_name:
469 # Legacy support.
470 template = template._replace(
471 PYTHON_RELDIR='python276_bin',
472 PYTHON_BIN_RELDIR='python276_bin',
473 PYTHON_BIN_RELDIR_UNIX='python276_bin')
474 template = ensure_git(args, template)
475 else:
476 template = template._replace(
477 PYTHON_RELDIR=os.path.join(args.win_tools_name, 'python'),
478 PYTHON_BIN_RELDIR=os.path.join(args.win_tools_name, 'python', 'bin'),
479 PYTHON_BIN_RELDIR_UNIX=posixpath.join(
480 args.win_tools_name, 'python', 'bin'),
481 GIT_BIN_RELDIR=os.path.join(args.win_tools_name, 'git'),
482 GIT_BIN_RELDIR_UNIX=posixpath.join(args.win_tools_name, 'git'))
483
484 win_tools_dir = os.path.join(ROOT_DIR, args.win_tools_name)
485 git_postprocess(template, os.path.join(win_tools_dir, 'git'))
486
487 # Clean up any old Python installations.
488 clean_up_old_installations(win_tools_dir)
489
490 # Emit our Python bin depot-tools-relative directory. This is ready by
491 # "python.bat" to identify the path of the current Python installation.
492 #
493 # We use this indirection so that upgrades can change this pointer to
494 # redirect "python.bat" to a new Python installation. We can't just update
495 # "python.bat" because batch file executions reload the batch file and seek
496 # to the previous cursor in between every command, so changing the batch
497 # file contents could invalidate any existing executions.
498 #
499 # The intention is that the batch file itself never needs to change when
500 # switching Python versions.
501 maybe_update(
502 template.PYTHON_BIN_RELDIR,
503 os.path.join(ROOT_DIR, 'python_bin_reldir.txt'))
504
505 # Install our "python.bat" shim.
506 # TODO: Move this to generic shim installation once legacy support is
507 # removed and this code path is the only one.
508 template.maybe_install(
509 'python27.new.bat',
510 os.path.join(ROOT_DIR, 'python.bat'))
511
512 # Re-evaluate and regenerate our root templated files.
513 for src_name, dst_name in (
514 ('git-bash.template.sh', 'git-bash'),
515 ('pylint.new.bat', 'pylint.bat'),
516 ):
517 template.maybe_install(src_name, os.path.join(ROOT_DIR, dst_name))
518
519 return 0
520
521
522if __name__ == '__main__':
523 sys.exit(main(sys.argv[1:]))