Skip to content

Commit ed77952

Browse files
damienmgvladmos
authored andcommitted
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 (bazelbuild#2472) -- Change-Id: I2d3c79ae0c2c53089677573cffd40fa07e03c7e1 Reviewed-on: https://cr.bazel.build/9210 PiperOrigin-RevId: 149291628 MOS_MIGRATED_REVID=149291628
1 parent e642558 commit ed77952

3 files changed

Lines changed: 24 additions & 6 deletions

File tree

src/main/java/com/google/devtools/build/lib/bazel/rules/python/BazelPythonConfiguration.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,12 @@ public static final class Options extends FragmentOptions {
4848
category = "version",
4949
help = "Local path to the Python3 executable.")
5050
public String python3Path;
51+
52+
@Option(name = "experimental_python_import_all_repositories",
53+
defaultValue = "true",
54+
category = "undocumented",
55+
help = "Do not use.")
56+
public boolean experimentalPythonImportAllRepositories;
5157
}
5258

5359
/**
@@ -85,4 +91,8 @@ public String getPython2Path() {
8591
public String getPython3Path() {
8692
return options.python3Path;
8793
}
94+
95+
public boolean getImportAllRepositories() {
96+
return options.experimentalPythonImportAllRepositories;
97+
}
8898
}

src/main/java/com/google/devtools/build/lib/bazel/rules/python/BazelPythonSemantics.java

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,9 @@ public void createExecutable(
144144
Substitution.of("%python_binary%", pythonBinary),
145145
Substitution.of("%imports%", Joiner.on(":").join(imports)),
146146
Substitution.of("%workspace_name%", ruleContext.getWorkspaceName()),
147-
Substitution.of("%is_zipfile%", "False")),
147+
Substitution.of("%is_zipfile%", "False"),
148+
Substitution.of("%import_all%",
149+
config.getImportAllRepositories() ? "True" : "False")),
148150
true));
149151
} else {
150152
Artifact zipFile = getPythonZipArtifact(ruleContext, executable);
@@ -160,7 +162,9 @@ public void createExecutable(
160162
Substitution.of("%python_binary%", pythonBinary),
161163
Substitution.of("%imports%", Joiner.on(":").join(imports)),
162164
Substitution.of("%workspace_name%", ruleContext.getWorkspaceName()),
163-
Substitution.of("%is_zipfile%", "True")),
165+
Substitution.of("%is_zipfile%", "True"),
166+
Substitution.of("%import_all%",
167+
config.getImportAllRepositories() ? "True" : "False")),
164168
true));
165169

166170
ruleContext.registerAction(

src/main/java/com/google/devtools/build/lib/bazel/rules/python/stub_template.txt

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,13 @@ def CreateModuleSpace():
9999
zf.extractall(temp_dir)
100100
return os.path.join(temp_dir, ZIP_RUNFILES_DIRECTORY_NAME)
101101

102+
# Returns repository roots to add to the import path.
103+
def GetRepositoriesImports(module_space, import_all):
104+
if import_all:
105+
repo_dirs = [os.path.join(module_space, d) for d in os.listdir(module_space)]
106+
return [d for d in repo_dirs if os.path.isdir(d)]
107+
return [os.path.join(module_space, "%workspace_name%")]
108+
102109
def Main():
103110
args = sys.argv[1:]
104111

@@ -111,10 +118,7 @@ def Main():
111118

112119
python_imports = '%imports%'
113120
python_path_entries = CreatePythonPathEntries(python_imports, module_space)
114-
115-
repo_dirs = [os.path.join(module_space, d) for d in os.listdir(module_space)]
116-
repositories = [d for d in repo_dirs if os.path.isdir(d)]
117-
python_path_entries += repositories
121+
python_path_entries += GetRepositoriesImports(module_space, %import_all%)
118122

119123
old_python_path = os.environ.get('PYTHONPATH')
120124
python_path = os.pathsep.join(python_path_entries)

0 commit comments

Comments
 (0)