Convert fs_config_generator.py to python 3

Bug: 203436762
Test: ./test_fs_config_generator.py and m libc
Change-Id: I16ce5f5e273ade064acce8889a9eb3f2707910ae
This commit is contained in:
Cole Faust 2022-10-20 11:39:45 -07:00
parent f08ab3d098
commit 216978e7c4
3 changed files with 43 additions and 59 deletions

View file

@ -43,14 +43,6 @@ cc_binary_host {
python_binary_host {
name: "fs_config_generator",
srcs: ["fs_config_generator.py"],
version: {
py2: {
enabled: true,
},
py3: {
enabled: false,
},
},
}
python_test_host {
@ -60,14 +52,6 @@ python_test_host {
"test_fs_config_generator.py",
"fs_config_generator.py",
],
version: {
py2: {
enabled: true,
},
py3: {
enabled: false,
},
},
}
target_fs_config_gen_filegroup {

View file

@ -1,4 +1,4 @@
#!/usr/bin/env python2
#!/usr/bin/env python3
"""Generates config files for Android file system properties.
This script is used for generating configuration files for configuring
@ -11,7 +11,7 @@ Further documentation can be found in the README.
"""
import argparse
import ConfigParser
import configparser
import ctypes
import re
import sys
@ -463,7 +463,7 @@ class AIDHeaderParser(object):
# No core AIDs should be within any oem range.
for aid in self._aid_value_to_name:
for ranges in self._ranges.values():
if Utils.in_any_range(aid, ranges):
if Utils.in_any_range(int(aid, 0), ranges):
name = self._aid_value_to_name[aid]
raise ValueError(
'AID "%s" value: %u within reserved OEM Range: "%s"' %
@ -569,7 +569,7 @@ class FSConfigFileParser(object):
# override previous
# sections.
config = ConfigParser.ConfigParser()
config = configparser.ConfigParser()
config.read(file_name)
for section in config.sections():
@ -613,7 +613,7 @@ class FSConfigFileParser(object):
ranges = None
partitions = self._ranges.keys()
partitions = list(self._ranges.keys())
partitions.sort(key=len, reverse=True)
for partition in partitions:
if aid.friendly.startswith(partition):
@ -1073,7 +1073,7 @@ class FSConfigGen(BaseGenerator):
user_binary = bytearray(ctypes.c_uint16(int(user, 0)))
group_binary = bytearray(ctypes.c_uint16(int(group, 0)))
caps_binary = bytearray(ctypes.c_uint64(caps_value))
path_binary = ctypes.create_string_buffer(path,
path_binary = ctypes.create_string_buffer(path.encode(),
path_length_aligned_64).raw
out_file.write(length_binary)
@ -1169,21 +1169,21 @@ class AIDArrayGen(BaseGenerator):
hdr = AIDHeaderParser(args['hdrfile'])
max_name_length = max(len(aid.friendly) + 1 for aid in hdr.aids)
print AIDArrayGen._GENERATED
print
print AIDArrayGen._INCLUDE
print
print AIDArrayGen._STRUCT_FS_CONFIG % max_name_length
print
print AIDArrayGen._OPEN_ID_ARRAY
print(AIDArrayGen._GENERATED)
print()
print(AIDArrayGen._INCLUDE)
print()
print(AIDArrayGen._STRUCT_FS_CONFIG % max_name_length)
print()
print(AIDArrayGen._OPEN_ID_ARRAY)
for aid in hdr.aids:
print AIDArrayGen._ID_ENTRY % (aid.friendly, aid.identifier)
print(AIDArrayGen._ID_ENTRY % (aid.friendly, aid.identifier))
print AIDArrayGen._CLOSE_FILE_STRUCT
print
print AIDArrayGen._COUNT
print
print(AIDArrayGen._CLOSE_FILE_STRUCT)
print()
print(AIDArrayGen._COUNT)
print()
@generator('oemaid')
@ -1225,15 +1225,15 @@ class OEMAidGen(BaseGenerator):
parser = FSConfigFileParser(args['fsconfig'], hdr_parser.ranges)
print OEMAidGen._GENERATED
print(OEMAidGen._GENERATED)
print OEMAidGen._FILE_IFNDEF_DEFINE
print(OEMAidGen._FILE_IFNDEF_DEFINE)
for aid in parser.aids:
self._print_aid(aid)
print
print()
print OEMAidGen._FILE_ENDIF
print(OEMAidGen._FILE_ENDIF)
def _print_aid(self, aid):
"""Prints a valid #define AID identifier to stdout.
@ -1245,10 +1245,10 @@ class OEMAidGen(BaseGenerator):
# print the source file location of the AID
found_file = aid.found
if found_file != self._old_file:
print OEMAidGen._FILE_COMMENT % found_file
print(OEMAidGen._FILE_COMMENT % found_file)
self._old_file = found_file
print OEMAidGen._GENERIC_DEFINE % (aid.identifier, aid.value)
print(OEMAidGen._GENERIC_DEFINE % (aid.identifier, aid.value))
@generator('passwd')
@ -1292,7 +1292,7 @@ class PasswdGen(BaseGenerator):
return
aids_by_partition = {}
partitions = hdr_parser.ranges.keys()
partitions = list(hdr_parser.ranges.keys())
partitions.sort(key=len, reverse=True)
for aid in aids:
@ -1331,7 +1331,7 @@ class PasswdGen(BaseGenerator):
except ValueError as exception:
sys.exit(exception)
print "%s::%s:%s::/:%s" % (logon, uid, uid, aid.login_shell)
print("%s::%s:%s::/:%s" % (logon, uid, uid, aid.login_shell))
@generator('group')
@ -1356,7 +1356,7 @@ class GroupGen(PasswdGen):
except ValueError as exception:
sys.exit(exception)
print "%s::%s:" % (logon, uid)
print("%s::%s:" % (logon, uid))
@generator('print')
@ -1379,7 +1379,7 @@ class PrintGen(BaseGenerator):
aids.sort(key=lambda item: int(item.normalized_value))
for aid in aids:
print '%s %s' % (aid.identifier, aid.normalized_value)
print('%s %s' % (aid.identifier, aid.normalized_value))
def main():
@ -1393,7 +1393,7 @@ def main():
gens = generator.get()
# for each gen, instantiate and add them as an option
for name, gen in gens.iteritems():
for name, gen in gens.items():
generator_option_parser = subparser.add_parser(name, help=gen.__doc__)
generator_option_parser.set_defaults(which=name)

View file

@ -1,4 +1,4 @@
#!/usr/bin/env python
#!/usr/bin/env python3
"""Unit test suite for the fs_config_genertor.py tool."""
import tempfile
@ -64,7 +64,7 @@ class Tests(unittest.TestCase):
def test_aid_header_parser_good(self):
"""Test AID Header Parser good input file"""
with tempfile.NamedTemporaryFile() as temp_file:
with tempfile.NamedTemporaryFile(mode='w') as temp_file:
temp_file.write(
textwrap.dedent("""
#define AID_FOO 1000
@ -91,7 +91,7 @@ class Tests(unittest.TestCase):
def test_aid_header_parser_good_unordered(self):
"""Test AID Header Parser good unordered input file"""
with tempfile.NamedTemporaryFile() as temp_file:
with tempfile.NamedTemporaryFile(mode='w') as temp_file:
temp_file.write(
textwrap.dedent("""
#define AID_FOO 1000
@ -118,7 +118,7 @@ class Tests(unittest.TestCase):
def test_aid_header_parser_bad_aid(self):
"""Test AID Header Parser bad aid input file"""
with tempfile.NamedTemporaryFile() as temp_file:
with tempfile.NamedTemporaryFile(mode='w') as temp_file:
temp_file.write(
textwrap.dedent("""
#define AID_FOO "bad"
@ -131,7 +131,7 @@ class Tests(unittest.TestCase):
def test_aid_header_parser_bad_oem_range(self):
"""Test AID Header Parser bad oem range input file"""
with tempfile.NamedTemporaryFile() as temp_file:
with tempfile.NamedTemporaryFile(mode='w') as temp_file:
temp_file.write(
textwrap.dedent("""
#define AID_OEM_RESERVED_START 2900
@ -145,7 +145,7 @@ class Tests(unittest.TestCase):
def test_aid_header_parser_bad_oem_range_no_end(self):
"""Test AID Header Parser bad oem range (no end) input file"""
with tempfile.NamedTemporaryFile() as temp_file:
with tempfile.NamedTemporaryFile(mode='w') as temp_file:
temp_file.write(
textwrap.dedent("""
#define AID_OEM_RESERVED_START 2900
@ -158,7 +158,7 @@ class Tests(unittest.TestCase):
def test_aid_header_parser_bad_oem_range_no_start(self):
"""Test AID Header Parser bad oem range (no start) input file"""
with tempfile.NamedTemporaryFile() as temp_file:
with tempfile.NamedTemporaryFile(mode='w') as temp_file:
temp_file.write(
textwrap.dedent("""
#define AID_OEM_RESERVED_END 2900
@ -171,7 +171,7 @@ class Tests(unittest.TestCase):
def test_aid_header_parser_bad_oem_range_duplicated(self):
"""Test AID Header Parser bad oem range (no start) input file"""
with tempfile.NamedTemporaryFile() as temp_file:
with tempfile.NamedTemporaryFile(mode='w') as temp_file:
temp_file.write(
textwrap.dedent("""
#define AID_OEM_RESERVED_START 2000
@ -187,7 +187,7 @@ class Tests(unittest.TestCase):
def test_aid_header_parser_bad_oem_range_mismatch_start_end(self):
"""Test AID Header Parser bad oem range mismatched input file"""
with tempfile.NamedTemporaryFile() as temp_file:
with tempfile.NamedTemporaryFile(mode='w') as temp_file:
temp_file.write(
textwrap.dedent("""
#define AID_OEM_RESERVED_START 2900
@ -201,7 +201,7 @@ class Tests(unittest.TestCase):
def test_aid_header_parser_bad_duplicate_ranges(self):
"""Test AID Header Parser exits cleanly on duplicate AIDs"""
with tempfile.NamedTemporaryFile() as temp_file:
with tempfile.NamedTemporaryFile(mode='w') as temp_file:
temp_file.write(
textwrap.dedent("""
#define AID_FOO 100
@ -222,7 +222,7 @@ class Tests(unittest.TestCase):
- https://android-review.googlesource.com/#/c/313169
"""
with tempfile.NamedTemporaryFile() as temp_file:
with tempfile.NamedTemporaryFile(mode='w') as temp_file:
temp_file.write(
textwrap.dedent("""
#define AID_APP 10000 /* TODO: switch users over to AID_APP_START */
@ -257,7 +257,7 @@ class Tests(unittest.TestCase):
def test_fs_config_file_parser_good(self):
"""Test FSConfig Parser good input file"""
with tempfile.NamedTemporaryFile() as temp_file:
with tempfile.NamedTemporaryFile(mode='w') as temp_file:
temp_file.write(
textwrap.dedent("""
[/system/bin/file]
@ -305,7 +305,7 @@ class Tests(unittest.TestCase):
def test_fs_config_file_parser_bad(self):
"""Test FSConfig Parser bad input file"""
with tempfile.NamedTemporaryFile() as temp_file:
with tempfile.NamedTemporaryFile(mode='w') as temp_file:
temp_file.write(
textwrap.dedent("""
[/system/bin/file]
@ -319,7 +319,7 @@ class Tests(unittest.TestCase):
def test_fs_config_file_parser_bad_aid_range(self):
"""Test FSConfig Parser bad aid range value input file"""
with tempfile.NamedTemporaryFile() as temp_file:
with tempfile.NamedTemporaryFile(mode='w') as temp_file:
temp_file.write(
textwrap.dedent("""
[AID_OEM1]