cleanup_links.py 3.8 KB

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