Skip to content

Commit

Permalink
Flag to import external repositories in python import path
Browse files Browse the repository at this point in the history
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
  • Loading branch information
damienmg authored and vladmos committed Mar 6, 2017
1 parent e642558 commit ed77952
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,12 @@ public static final class Options extends FragmentOptions {
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;
}

/**
Expand Down Expand Up @@ -85,4 +91,8 @@ public String getPython2Path() {
public String getPython3Path() {
return options.python3Path;
}

public boolean getImportAllRepositories() {
return options.experimentalPythonImportAllRepositories;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,9 @@ public void createExecutable(
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);
Expand All @@ -160,7 +162,9 @@ public void createExecutable(
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(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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:]

Expand All @@ -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)
Expand Down

0 comments on commit ed77952

Please sign in to comment.