blob: 337d0690b20284535df86d22809e575735513bc7 [file] [log] [blame]
#!/usr/bin/env python
# Copyright 2014 The Chromium Authors. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.
"""Simple tool to generate NMF file by just reformatting given arguments.
This tool is similar to native_client_sdk/src/tools/ handles most cases, with the exception of Non-SFI nexes. tries to auto-detect nexe and pexe types based on their contents,
but it does not work for Non-SFI nexes (which don't have a marker to
distinguish them from SFI nexes).
This script simply reformats the command line arguments into NMF JSON format.
import argparse
import collections
import json
import logging
import os
_FILES_KEY = 'files'
_PORTABLE_KEY = 'portable'
_PROGRAM_KEY = 'program'
_URL_KEY = 'url'
_X86_32_NONSFI_KEY = 'x86-32-nonsfi'
def ParseArgs():
parser = argparse.ArgumentParser()
'--program', metavar='FILE', help='Main program nexe')
# To keep compatibility with, we use -x and --extra-files
# as flags.
'-x', '--extra-files', action='append', metavar='KEY:FILE', default=[],
help=('Add extra key:file tuple to the "files" '
'section of the .nmf'))
'--output', metavar='FILE', help='Path to the output nmf file.')
return parser.parse_args()
def BuildNmfMap(root_path, program, extra_files):
"""Build simple map representing nmf json."""
result = {
_X86_32_NONSFI_KEY: {
# The program path is relative to the root_path.
_URL_KEY: os.path.relpath(program, root_path)
if extra_files:
files = {}
for named_file in extra_files:
name, path = named_file.split(':', 1)
files[name] = {
# Note: use path as is, unlike program path.
_URL_KEY: path
if files:
result[_FILES_KEY] = files
return result
def OutputNmf(nmf_map, output_path):
"""Writes the nmf to an output file at given path in JSON format."""
with open(output_path, 'w') as output:
json.dump(nmf_map, output, indent=2)
def main():
args = ParseArgs()
if not args.program:
logging.error('--program is not specified.')
if not args.output:
logging.error('--output is not specified.')
nmf_map = BuildNmfMap(os.path.dirname(args.output),
args.program, args.extra_files)
OutputNmf(nmf_map, args.output)
if __name__ == '__main__':