From 1d6d4a7c7e197072b289887ea3f28a8942a191a3 Mon Sep 17 00:00:00 2001 From: Googler Date: Tue, 14 Aug 2018 11:38:39 -0700 Subject: Skip __init__.py in __pycache__ dir. RELNOTES: None. PiperOrigin-RevId: 208683453 --- .../devtools/build/lib/rules/python/PythonUtils.java | 20 +++++++++++++++----- 1 file changed, 15 insertions(+), 5 deletions(-) diff --git a/src/main/java/com/google/devtools/build/lib/rules/python/PythonUtils.java b/src/main/java/com/google/devtools/build/lib/rules/python/PythonUtils.java index c524a3034c..8fe51ebcf2 100644 --- a/src/main/java/com/google/devtools/build/lib/rules/python/PythonUtils.java +++ b/src/main/java/com/google/devtools/build/lib/rules/python/PythonUtils.java @@ -36,6 +36,7 @@ import java.util.Set; public final class PythonUtils { public static final PathFragment INIT_PY = PathFragment.create("__init__.py"); public static final PathFragment INIT_PYC = PathFragment.create("__init__.pyc"); + public static final PathFragment PYCACHE = PathFragment.create("__pycache__"); private static final FileType REQUIRES_INIT_PY = FileType.of(".py", ".so", ".pyc"); @@ -60,18 +61,27 @@ public final class PythonUtils { */ public static Set getInitPyFiles(Set manifestFiles) { Set result = new HashSet<>(); + Set packagesWithInit = new HashSet<>(); + for (PathFragment source : manifestFiles) { + if (source.getBaseName().startsWith("__init__.")) { + packagesWithInit.add(source.getParentDirectory()); + } + } for (PathFragment source : manifestFiles) { // If we have a python or .so file at this level... if (REQUIRES_INIT_PY.matches(source)) { - // ...then record that we need an __init__.py in this directory... + // ...then record that we need an __init__.py in this and all parents directories... while (source.segmentCount() > 1) { source = source.getParentDirectory(); - PathFragment initpy = source.getRelative(INIT_PY); - PathFragment initpyc = source.getRelative(INIT_PYC); + // ...unless it's a Python .pyc cache or we already have __init__ there. + if (!source.endsWith(PYCACHE) && !packagesWithInit.contains(source)) { + PathFragment initpy = source.getRelative(INIT_PY); + PathFragment initpyc = source.getRelative(INIT_PYC); - if (!manifestFiles.contains(initpy) && !manifestFiles.contains(initpyc)) { - result.add(initpy); + if (!manifestFiles.contains(initpy) && !manifestFiles.contains(initpyc)) { + result.add(initpy); + } } } } -- cgit v1.2.3