• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1#!/usr/bin/env python
2# Copyright 2017 The LibYuv Project Authors. All rights reserved.
3#
4# Use of this source code is governed by a BSD-style license
5# that can be found in the LICENSE file in the root of the source
6# tree. An additional intellectual property rights grant can be found
7# in the file PATENTS. All contributing project authors may
8# be found in the AUTHORS file in the root of the source tree.
9
10# This is a copy of the file from WebRTC in:
11# https://chromium.googlesource.com/external/webrtc/+/master/cleanup_links.py
12
13"""Script to cleanup symlinks created from setup_links.py.
14
15Before 177567c518b121731e507e9b9c4049c4dc96e4c8 (#15754) we had a Chromium
16checkout which we created symlinks into. In order to do clean syncs after
17landing that change, this script cleans up any old symlinks, avoiding annoying
18manual cleanup needed in order to complete gclient sync.
19"""
20
21import logging
22import optparse
23import os
24import shelve
25import subprocess
26import sys
27
28
29ROOT_DIR = os.path.dirname(os.path.abspath(__file__))
30LINKS_DB = 'links'
31
32# Version management to make future upgrades/downgrades easier to support.
33SCHEMA_VERSION = 1
34
35class WebRTCLinkSetup(object):
36  def __init__(self, links_db, dry_run=False):
37    self._dry_run = dry_run
38    self._links_db = links_db
39
40  def CleanupLinks(self):
41    logging.debug('CleanupLinks')
42    for source, link_path  in self._links_db.iteritems():
43      if source == 'SCHEMA_VERSION':
44        continue
45      if os.path.islink(link_path) or sys.platform.startswith('win'):
46        # os.path.islink() always returns false on Windows
47        # See http://bugs.python.org/issue13143.
48        logging.debug('Removing link to %s at %s', source, link_path)
49        if not self._dry_run:
50          if os.path.exists(link_path):
51            if sys.platform.startswith('win') and os.path.isdir(link_path):
52              subprocess.check_call(['rmdir', '/q', '/s', link_path],
53                                    shell=True)
54            else:
55              os.remove(link_path)
56          del self._links_db[source]
57
58
59def _initialize_database(filename):
60  links_database = shelve.open(filename)
61  # Wipe the database if this version of the script ends up looking at a
62  # newer (future) version of the links db, just to be sure.
63  version = links_database.get('SCHEMA_VERSION')
64  if version and version != SCHEMA_VERSION:
65    logging.info('Found database with schema version %s while this script only '
66                 'supports %s. Wiping previous database contents.', version,
67                 SCHEMA_VERSION)
68    links_database.clear()
69  links_database['SCHEMA_VERSION'] = SCHEMA_VERSION
70  return links_database
71
72
73def main():
74  parser = optparse.OptionParser()
75  parser.add_option('-d', '--dry-run', action='store_true', default=False,
76                    help='Print what would be done, but don\'t perform any '
77                         'operations. This will automatically set logging to '
78                         'verbose.')
79  parser.add_option('-v', '--verbose', action='store_const',
80                    const=logging.DEBUG, default=logging.INFO,
81                    help='Print verbose output for debugging.')
82  options, _ = parser.parse_args()
83
84  if options.dry_run:
85    options.verbose = logging.DEBUG
86  logging.basicConfig(format='%(message)s', level=options.verbose)
87
88  # Work from the root directory of the checkout.
89  script_dir = os.path.dirname(os.path.abspath(__file__))
90  os.chdir(script_dir)
91
92  # The database file gets .db appended on some platforms.
93  db_filenames = [LINKS_DB, LINKS_DB + '.db']
94  if any(os.path.isfile(f) for f in db_filenames):
95    links_database = _initialize_database(LINKS_DB)
96    try:
97      symlink_creator = WebRTCLinkSetup(links_database, options.dry_run)
98      symlink_creator.CleanupLinks()
99    finally:
100      for f in db_filenames:
101        if os.path.isfile(f):
102          os.remove(f)
103  return 0
104
105
106if __name__ == '__main__':
107  sys.exit(main())
108