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