Skip to content

Commit

Permalink
Add workarounds for loading mke2fs.conf with bazel-built apexer
Browse files Browse the repository at this point in the history
There are a couple issues when running
pkgutil.get_data('apexer', 'mke2fs.conf') from a bazel-built
apexer binary:

- Soong will take the path to mke2fs.conf as being relative
  to the filegroup that included it, so mke2fs.conf ends up at
  the root of the python zip file, and next to apexer.py.
  Bazel doesn't do this, and instead keeps the mke2fs.conf file
  under a system/extras/ext4_utils folder. Use a genrule to
  explicitly copy mke2fs.conf to the current directory to work
  around this issue.

- pkgutil.get_data() uses python's normal rules for locating
  modules in order to find where the file is. This means it will
  look through sys.path again to find the apexer module. Bazel
  currently has a bug where the first entry in sys.path is the
  location of apexer.py in the source tree, not in the runfiles
  directory. So pkgutil will find apexer.py there, and then
  start looking for mke2fs.conf next to it, and not find it.
  Work around this by deleting sys.path[0].
  bazelbuild/rules_python#382

Bug: 204244290
Test: Presubmits
Change-Id: I9fb9502bff2590abfde95f7f7cc9ebf31157836d
  • Loading branch information
Colecf committed Jun 8, 2022
1 parent cf23bed commit 1ddc920
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 2 deletions.
11 changes: 10 additions & 1 deletion apexer/Android.bp
Original file line number Diff line number Diff line change
Expand Up @@ -49,14 +49,23 @@ python_library_host {
],
}

// This is a hack to move mke2fs_conf into the current folder for bazel builds.
// Normally bazel would keep it under a system/extras/ext4_utils/ folder.
genrule {
name: "mke2fs_conf_for_apexer",
srcs: [":mke2fs_conf"],
out: ["mke2fs.conf"],
cmd: "cp $(in) $(out)"
}

python_binary_host {
name: "apexer",
srcs: [
"apexer.py",
],
// TODO(b/157625953) mke2fs.conf can't embedded directly.
data: [
":mke2fs_conf",
":mke2fs_conf_for_apexer",
],
version: {
py3: {
Expand Down
11 changes: 10 additions & 1 deletion apexer/apexer.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,16 @@
"""

import sys

if len(sys.path) >= 2 and "/execroot/__main__/" in sys.path[1] and "/execroot/__main__/" not in sys.path[0]:
# TODO(b/235287972): Remove this hack. Bazel currently has a bug where a path outside
# of the execroot is added to the beginning of sys.path, because the python interpreter
# will add the directory of the main file to the path, following symlinks as it does.
# This can be fixed with the -P option or the PYTHONSAFEPATH environment variable in
# python 3.11.0, which is not yet released.
del sys.path[0]

import apex_build_info_pb2
import argparse
import hashlib
Expand All @@ -28,7 +38,6 @@
import shlex
import shutil
import subprocess
import sys
import tempfile
import uuid
import xml.etree.ElementTree as ET
Expand Down

0 comments on commit 1ddc920

Please sign in to comment.