0
Files
src/tools/remove_stale_pyc_files.py
Josip Sokcevic 825b1e98c3 [tools] Don't remove empty directories
Git creates empty directories for uninitilized git submodules. If such
directory gets removed (e.g. via rmdir), git will identify that as
removing git submodule.

When tools/remove_stale_pyc_files.py runs on tools/, and if src_internal
checkout is set to false, it results in removing tools/perf/data and
having dirty state (deleted: tools/perf/data).

Bug: 1475387
Change-Id: Ia6693e0d1682d29b46d330bac426baf6c2f38b13
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/4808459
Reviewed-by: Dirk Pranke <dpranke@google.com>
Commit-Queue: Josip Sokcevic <sokcevic@chromium.org>
Cr-Commit-Position: refs/heads/main@{#1187531}
2023-08-23 22:29:22 +00:00

34 lines
938 B
Python
Executable File

#!/usr/bin/env python
# Copyright 2014 The Chromium Authors
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.
import os
import sys
def RemoveAllStalePycFiles(base_dir):
"""Scan directories for old .pyc files without a .py file and delete them."""
for dirname, _, filenames in os.walk(base_dir, topdown=False):
if '.svn' in dirname or '.git' in dirname:
continue
for filename in filenames:
root, ext = os.path.splitext(filename)
if ext != '.pyc':
continue
pyc_path = os.path.join(dirname, filename)
py_path = os.path.join(dirname, root + '.py')
try:
if not os.path.exists(py_path):
os.remove(pyc_path)
except OSError:
# Wrap OS calls in try/except in case another process touched this file.
pass
if __name__ == '__main__':
for path in sys.argv[1:]:
RemoveAllStalePycFiles(path)