Simplify python launcher, use __main__.py

Uses more python rather than C++, and skips less of Py_Main.

Test: build/soong/python/tests/runtests.sh
Change-Id: I03997d88e2e16047c96bb4e00e530229c42b3325
This commit is contained in:
Dan Willemsen 2018-11-29 21:39:59 -08:00
parent 7b88d7c9f6
commit 707542f298
4 changed files with 26 additions and 40 deletions

View file

@ -66,7 +66,6 @@ var (
stripDirEntries = flag.Bool("D", false, "strip directory entries from the output zip file")
manifest = flag.String("m", "", "manifest file to insert in jar")
pyMain = flag.String("pm", "", "__main__.py file to insert in par")
entrypoint = flag.String("e", "", "par entrypoint file to insert in par")
prefix = flag.String("prefix", "", "A file to prefix to the zip file")
ignoreDuplicates = flag.Bool("ignore-duplicates", false, "take each entry from the first zip it exists in and don't warn")
)
@ -79,7 +78,7 @@ func init() {
func main() {
flag.Usage = func() {
fmt.Fprintln(os.Stderr, "usage: merge_zips [-jpsD] [-m manifest] [--prefix script] [-e entrypoint] [-pm __main__.py] output [inputs...]")
fmt.Fprintln(os.Stderr, "usage: merge_zips [-jpsD] [-m manifest] [--prefix script] [-pm __main__.py] output [inputs...]")
flag.PrintDefaults()
}
@ -139,16 +138,12 @@ func main() {
log.Fatal(errors.New("must specify -j when specifying a manifest via -m"))
}
if *entrypoint != "" && !*emulatePar {
log.Fatal(errors.New("must specify -p when specifying a entrypoint via -e"))
}
if *pyMain != "" && !*emulatePar {
log.Fatal(errors.New("must specify -p when specifying a Python __main__.py via -pm"))
}
// do merge
err = mergeZips(readers, writer, *manifest, *entrypoint, *pyMain, *sortEntries, *emulateJar, *emulatePar,
err = mergeZips(readers, writer, *manifest, *pyMain, *sortEntries, *emulateJar, *emulatePar,
*stripDirEntries, *ignoreDuplicates, []string(stripFiles), []string(stripDirs), map[string]bool(zipsToNotStrip))
if err != nil {
log.Fatal(err)
@ -249,7 +244,7 @@ type fileMapping struct {
source zipSource
}
func mergeZips(readers []namedZipReader, writer *zip.Writer, manifest, entrypoint, pyMain string,
func mergeZips(readers []namedZipReader, writer *zip.Writer, manifest, pyMain string,
sortEntries, emulateJar, emulatePar, stripDirEntries, ignoreDuplicates bool,
stripFiles, stripDirs []string, zipsToNotStrip map[string]bool) error {
@ -289,22 +284,6 @@ func mergeZips(readers []namedZipReader, writer *zip.Writer, manifest, entrypoin
addMapping(jar.ManifestFile, fileSource)
}
if entrypoint != "" {
buf, err := ioutil.ReadFile(entrypoint)
if err != nil {
return err
}
fh := &zip.FileHeader{
Name: "entry_point.txt",
Method: zip.Store,
UncompressedSize64: uint64(len(buf)),
}
fh.SetMode(0700)
fh.SetModTime(jar.DefaultTime)
fileSource := bufferEntry{fh, buf}
addMapping("entry_point.txt", fileSource)
}
if pyMain != "" {
buf, err := ioutil.ReadFile(pyMain)
if err != nil {

View file

@ -221,7 +221,7 @@ func TestMergeZips(t *testing.T) {
out := &bytes.Buffer{}
writer := zip.NewWriter(out)
err := mergeZips(readers, writer, "", "", "",
err := mergeZips(readers, writer, "", "",
test.sort, test.jar, false, test.stripDirEntries, test.ignoreDuplicates,
test.stripFiles, test.stripDirs, test.zipsToNotStrip)

View file

@ -54,15 +54,14 @@ var (
embeddedPar = pctx.AndroidStaticRule("embeddedPar",
blueprint.RuleParams{
// `echo -n` to trim the newline, since the python code just wants the name.
// /bin/sh (used by ninja) on Mac turns off posix mode, and stops supporting -n.
// Explicitly use bash instead.
Command: `/bin/bash -c "echo -n '$main' > $entryPoint" &&` +
`$mergeParCmd -p --prefix $launcher -e $entryPoint $out $srcsZips && ` +
`chmod +x $out && (rm -f $entryPoint)`,
CommandDeps: []string{"$mergeParCmd"},
// `echo -n` to trim the newline, since the python code just wants the name
Command: `rm -f $out.main && ` +
`sed 's/ENTRY_POINT/$main/' build/soong/python/scripts/main.py >$out.main &&` +
`$mergeParCmd -p -pm $out.main --prefix $launcher $out $srcsZips && ` +
`chmod +x $out && rm -rf $out.main`,
CommandDeps: []string{"$mergeParCmd", "$parCmd", "build/soong/python/scripts/main.py"},
},
"main", "entryPoint", "srcsZips", "launcher")
"main", "srcsZips", "launcher")
)
func init() {
@ -108,19 +107,15 @@ func registerBuildActionForParFile(ctx android.ModuleContext, embeddedLauncher b
// added launcherPath to the implicits Ninja dependencies.
implicits = append(implicits, launcherPath.Path())
// .intermediate output path for entry_point.txt
entryPoint := android.PathForModuleOut(ctx, entryPointFile).String()
ctx.Build(pctx, android.BuildParams{
Rule: embeddedPar,
Description: "embedded python archive",
Output: binFile,
Implicits: implicits,
Args: map[string]string{
"main": strings.Replace(strings.TrimSuffix(main, pyExt), "/", ".", -1),
"entryPoint": entryPoint,
"srcsZips": strings.Join(srcsZips.Strings(), " "),
"launcher": launcherPath.String(),
"main": strings.Replace(strings.TrimSuffix(main, pyExt), "/", ".", -1),
"srcsZips": strings.Join(srcsZips.Strings(), " "),
"launcher": launcherPath.String(),
},
})
}

12
python/scripts/main.py Normal file
View file

@ -0,0 +1,12 @@
import runpy
import sys
sys.argv[0] = __loader__.archive
# Set sys.executable to None. The real executable is available as
# sys.argv[0], and too many things assume sys.executable is a regular Python
# binary, which isn't available. By setting it to None we get clear errors
# when people try to use it.
sys.executable = None
runpy._run_module_as_main("ENTRY_POINT", alter_argv=False)