aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/main/java/com/google/devtools/build
diff options
context:
space:
mode:
authorGravatar Yun Peng <pcloudy@google.com>2016-08-10 17:12:31 +0000
committerGravatar Yue Gan <yueg@google.com>2016-08-11 09:14:08 +0000
commitf5c46c4a99291d2c53d8cbd0fb079c77bf15d8ce (patch)
tree39e9eeadbe2d5549168cf1f3eadd9c9b897122f1 /src/main/java/com/google/devtools/build
parentc511cb53595cd724900fff026c45b2702950f115 (diff)
Put runfiles tree under 'runfiles' directory to avoid conflict
Currently, in python executable zip file, the default workspace name "__main__" conflicts with __main__.py file. This change fixes it. Also refactored the bazel_windows_example_test -- Change-Id: I8b9d64d72335148dba41032ce93643d34670a771 Reviewed-on: https://bazel-review.googlesource.com/#/c/5351 MOS_MIGRATED_REVID=129879570
Diffstat (limited to 'src/main/java/com/google/devtools/build')
-rw-r--r--src/main/java/com/google/devtools/build/lib/bazel/rules/python/BazelPythonSemantics.java50
-rw-r--r--src/main/java/com/google/devtools/build/lib/bazel/rules/python/stub_template.txt9
2 files changed, 24 insertions, 35 deletions
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 9083a510ff..80f9a58b4b 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
@@ -55,6 +55,8 @@ public class BazelPythonSemantics implements PythonSemantics {
FileTypeSet.of(BazelPyRuleClasses.PYTHON_SOURCE),
"srcs", "deps", "data");
+ public static final PathFragment ZIP_RUNFILES_DIRECTORY_NAME = new PathFragment("runfiles");
+
@Override
public void validate(RuleContext ruleContext, PyCommon common) {
}
@@ -148,16 +150,6 @@ public class BazelPythonSemantics implements PythonSemantics {
} else {
Artifact zipFile = getPythonZipArtifact(ruleContext, executable);
Artifact templateMain = getPythonTemplateMainArtifact(ruleContext, executable);
- PathFragment workspaceName = getWorkspaceNameForPythonZip(ruleContext.getWorkspaceName());
- main = workspaceName.getRelative(common.determineMainExecutableSource(false)).toString();
- PathFragment defaultWorkspacename = new PathFragment(Label.DEFAULT_REPOSITORY_DIRECTORY);
- List<PathFragment> importPaths = new ArrayList<>();
- for (PathFragment path : imports) {
- if (path.startsWith(defaultWorkspacename)) {
- path = new PathFragment(workspaceName, path.subFragment(1, path.segmentCount()));
- }
- importPaths.add(path);
- }
// The executable zip file will unzip itself into a tmp directory and then run from there
ruleContext.registerAction(
new TemplateExpansionAction(
@@ -167,7 +159,7 @@ public class BazelPythonSemantics implements PythonSemantics {
ImmutableList.of(
Substitution.of("%main%", main),
Substitution.of("%python_binary%", pythonBinary),
- Substitution.of("%imports%", Joiner.on(":").join(importPaths)),
+ Substitution.of("%imports%", Joiner.on(":").join(imports)),
Substitution.of("%workspace_name%", ruleContext.getWorkspaceName()),
Substitution.of("%is_zipfile%", "True")),
true));
@@ -205,31 +197,27 @@ public class BazelPythonSemantics implements PythonSemantics {
}
}
- // TODO(pcloudy): This is a temporary workaround
- private static PathFragment getWorkspaceNameForPythonZip(String workspaceName) {
- // Currently, the default workspace name "__main__" will causing python can't find __main__.py
- // in executable zip file. Rename it to "main"
- if (workspaceName.equals(Label.DEFAULT_REPOSITORY_DIRECTORY)) {
- return new PathFragment("__default__");
- }
- return new PathFragment(workspaceName);
- }
-
private static boolean isUnderWorkspace(PathFragment path) {
return !path.startsWith(Label.EXTERNAL_PACKAGE_NAME);
}
- private static String getRunfilesPath(PathFragment path, PathFragment workspaceName) {
+ private static String getZipRunfilesPath(PathFragment path, PathFragment workspaceName) {
+ String zipRunfilesPath;
if (isUnderWorkspace(path)) {
// If the file is under workspace, add workspace name as prefix
- return workspaceName.getRelative(path).normalize().toString();
+ zipRunfilesPath = workspaceName.getRelative(path).normalize().toString();
+ } else {
+ // If the file is in external package, strip "external"
+ zipRunfilesPath = path.relativeTo(Label.EXTERNAL_PACKAGE_NAME).normalize().toString();
}
- // If the file is in external package, strip "external"
- return path.relativeTo(Label.EXTERNAL_PACKAGE_NAME).normalize().toString();
+ // We put the whole runfiles tree under the ZIP_RUNFILES_DIRECTORY_NAME directory, by doing this
+ // , we avoid the conflict between default workspace name "__main__" and __main__.py file.
+ // Note: This name has to be the same with the one in stub_template.txt.
+ return ZIP_RUNFILES_DIRECTORY_NAME.getRelative(zipRunfilesPath).toString();
}
- private static String getRunfilesPath(String path, PathFragment workspaceName) {
- return getRunfilesPath(new PathFragment(path), workspaceName);
+ private static String getZipRunfilesPath(String path, PathFragment workspaceName) {
+ return getZipRunfilesPath(new PathFragment(path), workspaceName);
}
private static void createPythonZipAction(
@@ -241,16 +229,16 @@ public class BazelPythonSemantics implements PythonSemantics {
RunfilesSupport runfilesSupport) {
NestedSetBuilder<Artifact> inputsBuilder = NestedSetBuilder.stableOrder();
- PathFragment workspaceName = getWorkspaceNameForPythonZip(ruleContext.getWorkspaceName());
+ PathFragment workspaceName = runfilesSupport.getWorkspaceName();
CustomCommandLine.Builder argv = new CustomCommandLine.Builder();
inputsBuilder.add(templateMain);
argv.add("__main__.py=" + templateMain.getExecPathString());
// Creating __init__.py files under each directory
argv.add("__init__.py=");
- argv.add(getRunfilesPath("__init__.py", workspaceName) + "=");
+ argv.add(getZipRunfilesPath("__init__.py", workspaceName) + "=");
for (String path : runfilesSupport.getRunfiles().getEmptyFilenames()) {
- argv.add(getRunfilesPath(path, workspaceName) + "=");
+ argv.add(getZipRunfilesPath(path, workspaceName) + "=");
}
// Read each runfile from execute path, add them into zip file at the right runfiles path.
@@ -258,7 +246,7 @@ public class BazelPythonSemantics implements PythonSemantics {
for (Artifact artifact : runfilesSupport.getRunfiles().getArtifacts()) {
if (!artifact.equals(executable)) {
argv.add(
- getRunfilesPath(artifact.getRunfilesPath(), workspaceName)
+ getZipRunfilesPath(artifact.getRunfilesPath(), workspaceName)
+ "="
+ artifact.getExecPathString());
inputsBuilder.add(artifact);
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 d061accd90..04316fea4d 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
@@ -73,10 +73,11 @@ def FindModuleSpace():
# Create the runfiles tree by extracting the zip file
def CreateModuleSpace():
- module_space = tempfile.mkdtemp("", "Bazel.runfiles_")
+ ZIP_RUNFILES_DIRECTORY_NAME = "runfiles"
+ temp_dir = tempfile.mkdtemp("", "Bazel.runfiles_")
zf = zipfile.ZipFile(os.path.dirname(__file__))
- zf.extractall(module_space)
- return module_space
+ zf.extractall(temp_dir)
+ return os.path.join(temp_dir, ZIP_RUNFILES_DIRECTORY_NAME)
def Main():
args = sys.argv[1:]
@@ -125,7 +126,7 @@ def Main():
sys.stdout.flush()
if IsRunningFromZip():
retCode = subprocess.call(args)
- shutil.rmtree(module_space, True)
+ shutil.rmtree(os.path.dirname(module_space), True)
exit(retCode)
else:
os.execv(args[0], args)