diff options
author | Googler <noreply@google.com> | 2018-08-14 11:38:39 -0700 |
---|---|---|
committer | Copybara-Service <copybara-piper@google.com> | 2018-08-14 11:40:25 -0700 |
commit | 1d6d4a7c7e197072b289887ea3f28a8942a191a3 (patch) | |
tree | f4191b9279568cb519a967dee23b466f32e009a1 | |
parent | 529ef17f8712feb43fd145c99091288e4668e32b (diff) |
Skip __init__.py in __pycache__ dir.
RELNOTES: None.
PiperOrigin-RevId: 208683453
-rw-r--r-- | src/main/java/com/google/devtools/build/lib/rules/python/PythonUtils.java | 20 |
1 files 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<PathFragment> getInitPyFiles(Set<PathFragment> manifestFiles) { Set<PathFragment> result = new HashSet<>(); + Set<PathFragment> 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); + } } } } |