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