• 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.
5import sys
6import os
7
8_TOP_PATH = os.path.abspath(os.path.join(
9    os.path.dirname(__file__), '..'))
10
11class Link(object):
12  def __init__(self, dst_path, src_path):
13    self.dst_path = dst_path
14    self.src_path = src_path
15
16  def Update(self):
17    full_src_path = os.path.join(_TOP_PATH, self.src_path)
18    full_dst_path = os.path.join(_TOP_PATH, self.dst_path)
19
20    full_dst_path_dirname = os.path.dirname(full_dst_path)
21
22    src_path_rel = os.path.relpath(full_src_path, full_dst_path_dirname)
23
24    assert os.path.exists(full_src_path)
25    if not os.path.exists(full_dst_path_dirname):
26      sys.stdout.write('ERROR\n\n')
27      sys.stdout.write(' dst dir doesn\'t exist\n' % self.full_dst_path_dirname)
28      sys.stdout.write('\n\n')
29      sys.exit(255)
30
31    if os.path.exists(full_dst_path) or os.path.islink(full_dst_path):
32      if not os.path.islink(full_dst_path):
33        sys.stdout.write('ERROR\n\n')
34        sys.stdout.write('  Cannot install %s, dst already exists:\n  %s\n' % (
35          os.path.basename(self.src_path), full_dst_path))
36        sys.stdout.write('\n\n')
37        sys.exit(255)
38
39      existing_src_path_rel = os.readlink(full_dst_path)
40      if existing_src_path_rel == src_path_rel:
41        return
42      else:
43        sys.stdout.write('ERROR\n\n')
44        sys.stdout.write('  Cannot install %s, because %s is linked elsewhere.\n' % (
45          os.path.basename(self.src_path),
46          os.path.relpath(full_dst_path)))
47        sys.stdout.write('\n\n')
48        sys.exit(255)
49
50    os.symlink(src_path_rel, full_dst_path)
51
52def InstallHooks():
53  if sys.platform == 'win32':
54    return
55
56  # Remove old pre-commit, see https://github.com/google/trace-viewer/issues/932
57  old_precommit = os.path.join(_TOP_PATH, '.git', 'hooks', 'pre-commit')
58  old_precommit_target = os.path.join(_TOP_PATH, 'hooks', 'pre_commit')
59  if (os.path.islink(old_precommit) and
60      os.path.abspath(os.readlink(old_precommit)) == old_precommit_target):
61    os.remove(old_precommit)
62
63  links = []
64  links.append(Link(os.path.join('.git', 'hooks', 'pre-push'),
65                    os.path.join('hooks/pre_push')))
66
67  for l in links:
68    l.Update()
69