Commit 25c6d5b9 authored by Kunshan Wang's avatar Kunshan Wang

Merge branch 'mar-as-func' into 'master'

mar.py as a importable function

Put the functionality of mar.py as a importable function that can be used in RPython compiling tool chain.

See merge request !1
parents 2e5c2f30 e4b223c6
......@@ -5,66 +5,70 @@ import zipfile
import tempfile
import shutil
parser = argparse.ArgumentParser(
description='''Set additional boot-image metadata.''',
epilog='''Example:
./tools/mar.py -n @foo -l libm.so:/path/to/mylib.so my-boot-img.muref
'''
)
entry_point_group = parser.add_mutually_exclusive_group()
entry_point_group.add_argument('-i', '--entry-point-id',
metavar="MuID",
type=int,
help='set the entry point by ID'
)
entry_point_group.add_argument('-n', '--entry-point-name',
metavar="MuName",
help='set the entry point by name'
)
parser.add_argument('-l', '--extra-libraries',
metavar="colon-separated-libs",
help='''extra libraries to load at boot-image loading time. It is a
colon-separated list of libraries.''')
parser.add_argument('bootimg', help='path to the boot image')
def mu_meta_set(bootimg, entry_point_id=None, entry_point_name=None, extra_libraries=None):
with tempfile.NamedTemporaryFile(delete=False) as tf:
tmpfilename = tf.name
with zipfile.ZipFile(tf, 'w', compression=zipfile.ZIP_DEFLATED) as zo:
with zipfile.ZipFile(bootimg, "a") as zi:
il = zi.infolist()
entrypoint = None
extralibs = None
for info in il:
name = info.filename
if name == 'entrypoint':
entrypoint = zi.read(info)
elif name == 'extralibs':
extralibs = zi.read(info)
else:
oldcontent = zi.read(info)
zo.writestr(name, oldcontent)
if entry_point_id is not None:
entrypoint = "id {}".format(entry_point_id).encode("utf8")
if entry_point_name is not None:
entrypoint = "name {}".format(entry_point_name).encode("utf8")
if extra_libraries is not None:
extralibs = extra_libraries.encode("utf8")
if entrypoint is not None:
zo.writestr("entrypoint", entrypoint)
if extralibs is not None:
zo.writestr("extralibs", extralibs)
shutil.move(tmpfilename, bootimg)
args = parser.parse_args()
with tempfile.NamedTemporaryFile(delete=False) as tf:
tmpfilename = tf.name
with zipfile.ZipFile(tf, 'w', compression=zipfile.ZIP_DEFLATED) as zo:
with zipfile.ZipFile(args.bootimg, "a") as zi:
il = zi.infolist()
if __name__ == "__main__":
parser = argparse.ArgumentParser(
description='''Set additional boot-image metadata.''',
epilog='''Example:
./tools/mar.py -n @foo -l libm.so:/path/to/mylib.so my-boot-img.muref
'''
)
entrypoint = None
extralibs = None
entry_point_group = parser.add_mutually_exclusive_group()
for info in il:
name = info.filename
if name == 'entrypoint':
entrypoint = zi.read(info)
elif name == 'extralibs':
extralibs = zi.read(info)
else:
oldcontent = zi.read(info)
zo.writestr(name, oldcontent)
entry_point_group.add_argument('-i', '--entry-point-id',
metavar="MuID",
type=int,
help='set the entry point by ID'
)
if args.entry_point_id is not None:
entrypoint = "id {}".format(args.entry_point_id).encode("utf8")
if args.entry_point_name is not None:
entrypoint = "name {}".format(args.entry_point_name).encode("utf8")
if args.extra_libraries is not None:
extralibs = args.extra_libraries.encode("utf8")
entry_point_group.add_argument('-n', '--entry-point-name',
metavar="MuName",
help='set the entry point by name'
)
if entrypoint is not None:
zo.writestr("entrypoint", entrypoint)
if extralibs is not None:
zo.writestr("extralibs", extralibs)
parser.add_argument('-l', '--extra-libraries',
metavar="colon-separated-libs",
help='''extra libraries to load at boot-image loading time. It is a
colon-separated list of libraries.''')
shutil.move(tmpfilename, args.bootimg)
parser.add_argument('bootimg', help='path to the boot image')
args = parser.parse_args()
mu_meta_set(args.bootimg, args.entry_point_id, args.entry_point_name, args.extra_libraries)
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment