• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1#!/usr/bin/env python
2# Copyright (c) 2014 The Chromium Authors. All rights reserved.
3# Use of this source code is governed by a BSD-style license that can be
4# found in the LICENSE file.
5
6import sys
7import os
8
9_TOP_PATH = os.path.abspath(os.path.join(
10    os.path.dirname(__file__), '..'))
11
12
13class Link(object):
14
15  def __init__(self, dst_path, src_path):
16    self.dst_path = dst_path
17    self.src_path = src_path
18
19  def Update(self):
20    full_src_path = os.path.join(_TOP_PATH, self.src_path)
21    full_dst_path = os.path.join(_TOP_PATH, self.dst_path)
22
23    full_dst_path_dirname = os.path.dirname(full_dst_path)
24
25    src_path_rel = os.path.relpath(full_src_path, full_dst_path_dirname)
26
27    assert os.path.exists(full_src_path)
28    if not os.path.exists(full_dst_path_dirname):
29      sys.stdout.write('ERROR\n\n')
30      sys.stdout.write(' dst dir doesn\'t exist\n' % self.full_dst_path_dirname)
31      sys.stdout.write('\n\n')
32      sys.exit(255)
33
34    if os.path.exists(full_dst_path) or os.path.islink(full_dst_path):
35      if not os.path.islink(full_dst_path):
36        sys.stdout.write('ERROR\n\n')
37        sys.stdout.write('  Cannot install %s, dst already exists:\n  %s\n' % (
38          os.path.basename(self.src_path), full_dst_path))
39        sys.stdout.write('\n\n')
40        sys.exit(255)
41
42      existing_src_path_rel = os.readlink(full_dst_path)
43      if existing_src_path_rel == src_path_rel:
44        return
45      else:
46        sys.stdout.write('ERROR\n\n')
47        sys.stdout.write(
48          '  Cannot install %s, because %s is linked elsewhere.\n' % (
49          os.path.basename(self.src_path),
50          os.path.relpath(full_dst_path)))
51        sys.stdout.write('\n\n')
52        sys.exit(255)
53
54    os.symlink(src_path_rel, full_dst_path)
55
56
57def InstallHooks():
58  """Installs the git pre-push hooks."""
59  if sys.platform == 'win32':
60    return
61
62  # Remove old pre-commit, see https://github.com/google/trace-viewer/issues/932
63  old_precommit = os.path.join(_TOP_PATH, '.git', 'hooks', 'pre-commit')
64  old_precommit_target = os.path.join(_TOP_PATH, 'hooks', 'pre_commit')
65  if (os.path.islink(old_precommit) and
66      os.path.abspath(os.readlink(old_precommit)) == old_precommit_target):
67    os.remove(old_precommit)
68
69  # The pre-push hook prevents forced pushes; see ./pre_push.
70  links = [
71      Link(os.path.join('.git', 'hooks', 'pre-push'),
72           os.path.join('hooks', 'pre_push'))
73  ]
74
75  for l in links:
76    l.Update()
77