Commit 94f4bd9c authored by dtu's avatar dtu Committed by Commit bot

[telemetry] Move stale .pyc check from Python import to gclient hook.

This check turns out to be fairly expensive on Windows (~150ms), and having
it at import time means we're taking the hit every time we launch
memory_cache_http_server. We only need to do this if the Python files have
changed, so move it to runhooks.

The reason this check exists in the first place is that we've had problems in
the past where e.g. someone renames foo.py to foo/__init__.py. foo.pyc remains
on the filesystem of every Chromium checkout and causes a name conflict in
Python imports - foo can refer to the compiled module or the package.

BUG=388256
TEST=git rm a file in src/tools that has a .pyc; git commit; gclient runhooks; ensure .pyc doesn't exist; undo commit

Review URL: https://codereview.chromium.org/489693002

Cr-Commit-Position: refs/heads/master@{#291825}
parent f8584c66
...@@ -778,4 +778,15 @@ hooks = [ ...@@ -778,4 +778,15 @@ hooks = [
'--running-as-hook', '--running-as-hook',
], ],
}, },
{
# Ensure that we don't accidentally reference any .pyc files whose
# corresponding .py files have already been deleted.
'name': 'remove_stale_pyc_files',
'pattern': 'src/tools/.*\\.py',
'action': [
'python',
'src/tools/remove_stale_pyc_files.py',
'src/tools',
],
},
] ]
#!/usr/bin/env python
# Copyright 2014 The Chromium Authors. All rights reserved.
# 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):
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
try:
os.removedirs(dirname)
except OSError:
# Wrap OS calls in try/except in case another process touched this dir.
pass
if __name__ == '__main__':
for path in sys.argv[1:]:
RemoveAllStalePycFiles(path)
...@@ -4,49 +4,18 @@ ...@@ -4,49 +4,18 @@
"""Hooks that apply globally to all scripts that import or use Telemetry.""" """Hooks that apply globally to all scripts that import or use Telemetry."""
import os
import signal import signal
import sys import sys
from telemetry.core import util
from telemetry.util import exception_formatter from telemetry.util import exception_formatter
def InstallHooks(): def InstallHooks():
RemoveAllStalePycFiles(util.GetTelemetryDir())
RemoveAllStalePycFiles(util.GetBaseDir())
InstallUnhandledExceptionFormatter() InstallUnhandledExceptionFormatter()
InstallStackDumpOnSigusr1() InstallStackDumpOnSigusr1()
InstallTerminationHook() InstallTerminationHook()
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):
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
try:
os.removedirs(dirname)
except OSError:
# Wrap OS calls in try/except in case another process touched this dir.
pass
def InstallUnhandledExceptionFormatter(): def InstallUnhandledExceptionFormatter():
"""Print prettier exceptions that also contain the stack frame's locals.""" """Print prettier exceptions that also contain the stack frame's locals."""
sys.excepthook = exception_formatter.PrintFormattedException sys.excepthook = exception_formatter.PrintFormattedException
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment