diff options
author | Damien Martin-Guillerez <dmarting@google.com> | 2017-03-06 14:04:14 +0000 |
---|---|---|
committer | Vladimir Moskva <vladmos@google.com> | 2017-03-06 14:45:46 +0000 |
commit | ed7795234ca7ccd2567007f2c502f853cd947e50 (patch) | |
tree | 691713fc1edb1f1ae1c8809013d38fa7a17a45bb | |
parent | e64255874ae2441033d863e55dcadec434dc77a4 (diff) |
Flag to import external repositories in python import path
This flag will be used to turn off the feature until we
get support for --incompatible flag.
This flag is going to go away very fast, do not rely on it too much.
To be cherry-picked for 0.4.5 (#2472)
--
Change-Id: I2d3c79ae0c2c53089677573cffd40fa07e03c7e1
Reviewed-on: https://cr.bazel.build/9210
PiperOrigin-RevId: 149291628
MOS_MIGRATED_REVID=149291628
3 files changed, 24 insertions, 6 deletions
diff --git a/src/main/java/com/google/devtools/build/lib/bazel/rules/python/BazelPythonConfiguration.java b/src/main/java/com/google/devtools/build/lib/bazel/rules/python/BazelPythonConfiguration.java index fa5e21942a..d1768e1611 100644 --- a/src/main/java/com/google/devtools/build/lib/bazel/rules/python/BazelPythonConfiguration.java +++ b/src/main/java/com/google/devtools/build/lib/bazel/rules/python/BazelPythonConfiguration.java @@ -48,6 +48,12 @@ public class BazelPythonConfiguration extends BuildConfiguration.Fragment { category = "version", help = "Local path to the Python3 executable.") public String python3Path; + + @Option(name = "experimental_python_import_all_repositories", + defaultValue = "true", + category = "undocumented", + help = "Do not use.") + public boolean experimentalPythonImportAllRepositories; } /** @@ -85,4 +91,8 @@ public class BazelPythonConfiguration extends BuildConfiguration.Fragment { public String getPython3Path() { return options.python3Path; } + + public boolean getImportAllRepositories() { + return options.experimentalPythonImportAllRepositories; + } } diff --git a/src/main/java/com/google/devtools/build/lib/bazel/rules/python/BazelPythonSemantics.java b/src/main/java/com/google/devtools/build/lib/bazel/rules/python/BazelPythonSemantics.java index 06fe867fa0..a913927f8f 100644 --- a/src/main/java/com/google/devtools/build/lib/bazel/rules/python/BazelPythonSemantics.java +++ b/src/main/java/com/google/devtools/build/lib/bazel/rules/python/BazelPythonSemantics.java @@ -144,7 +144,9 @@ public class BazelPythonSemantics implements PythonSemantics { Substitution.of("%python_binary%", pythonBinary), Substitution.of("%imports%", Joiner.on(":").join(imports)), Substitution.of("%workspace_name%", ruleContext.getWorkspaceName()), - Substitution.of("%is_zipfile%", "False")), + Substitution.of("%is_zipfile%", "False"), + Substitution.of("%import_all%", + config.getImportAllRepositories() ? "True" : "False")), true)); } else { Artifact zipFile = getPythonZipArtifact(ruleContext, executable); @@ -160,7 +162,9 @@ public class BazelPythonSemantics implements PythonSemantics { Substitution.of("%python_binary%", pythonBinary), Substitution.of("%imports%", Joiner.on(":").join(imports)), Substitution.of("%workspace_name%", ruleContext.getWorkspaceName()), - Substitution.of("%is_zipfile%", "True")), + Substitution.of("%is_zipfile%", "True"), + Substitution.of("%import_all%", + config.getImportAllRepositories() ? "True" : "False")), true)); ruleContext.registerAction( diff --git a/src/main/java/com/google/devtools/build/lib/bazel/rules/python/stub_template.txt b/src/main/java/com/google/devtools/build/lib/bazel/rules/python/stub_template.txt index 00ebcc51fc..0e28ce16f7 100644 --- a/src/main/java/com/google/devtools/build/lib/bazel/rules/python/stub_template.txt +++ b/src/main/java/com/google/devtools/build/lib/bazel/rules/python/stub_template.txt @@ -99,6 +99,13 @@ def CreateModuleSpace(): zf.extractall(temp_dir) return os.path.join(temp_dir, ZIP_RUNFILES_DIRECTORY_NAME) +# Returns repository roots to add to the import path. +def GetRepositoriesImports(module_space, import_all): + if import_all: + repo_dirs = [os.path.join(module_space, d) for d in os.listdir(module_space)] + return [d for d in repo_dirs if os.path.isdir(d)] + return [os.path.join(module_space, "%workspace_name%")] + def Main(): args = sys.argv[1:] @@ -111,10 +118,7 @@ def Main(): python_imports = '%imports%' python_path_entries = CreatePythonPathEntries(python_imports, module_space) - - repo_dirs = [os.path.join(module_space, d) for d in os.listdir(module_space)] - repositories = [d for d in repo_dirs if os.path.isdir(d)] - python_path_entries += repositories + python_path_entries += GetRepositoriesImports(module_space, %import_all%) old_python_path = os.environ.get('PYTHONPATH') python_path = os.pathsep.join(python_path_entries) |