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