diff options
Diffstat (limited to 'src/main/java/com/google/devtools')
-rw-r--r-- | src/main/java/com/google/devtools/build/lib/bazel/rules/python/BazelPythonSemantics.java | 50 | ||||
-rw-r--r-- | src/main/java/com/google/devtools/build/lib/bazel/rules/python/stub_template.txt | 9 |
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) |