blob: a2a8f9f572faaaa756d723e9a53da0187e05c033 [file] [log] [blame]
[email protected]8adce542014-05-22 20:08:411#!/usr/bin/env python
2# Copyright 2014 The Chromium Authors. All rights reserved.
3# Use of this source code is governed by a BSD-style license that can be
4# found in the LICENSE file.
5
6"""Simple tool to generate NMF file by just reformatting given arguments.
7
8This tool is similar to native_client_sdk/src/tools/create_nmf.py.
9create_nmf.py handles most cases, with the exception of Non-SFI nexes.
10create_nmf.py tries to auto-detect nexe and pexe types based on their contents,
11but it does not work for Non-SFI nexes (which don't have a marker to
12distinguish them from SFI nexes).
13
14This script simply reformats the command line arguments into NMF JSON format.
15"""
16
17import argparse
18import collections
19import json
20import logging
21import os
mazdae1b899f2014-11-13 07:19:3122import sys
[email protected]8adce542014-05-22 20:08:4123
24_FILES_KEY = 'files'
25_PORTABLE_KEY = 'portable'
26_PROGRAM_KEY = 'program'
27_URL_KEY = 'url'
[email protected]8adce542014-05-22 20:08:4128
29def ParseArgs():
30 parser = argparse.ArgumentParser()
31 parser.add_argument(
32 '--program', metavar='FILE', help='Main program nexe')
mazdae1b899f2014-11-13 07:19:3133 parser.add_argument(
hidehikod3427852015-02-02 15:26:0534 '--arch', metavar='ARCH', choices=('x86-32', 'x86-64', 'arm'),
mazdae1b899f2014-11-13 07:19:3135 help='The archtecture of main program nexe')
[email protected]8adce542014-05-22 20:08:4136 # To keep compatibility with create_nmf.py, we use -x and --extra-files
37 # as flags.
38 parser.add_argument(
39 '-x', '--extra-files', action='append', metavar='KEY:FILE', default=[],
40 help=('Add extra key:file tuple to the "files" '
41 'section of the .nmf'))
42 parser.add_argument(
43 '--output', metavar='FILE', help='Path to the output nmf file.')
44
45 return parser.parse_args()
46
47
mazdae1b899f2014-11-13 07:19:3148def BuildNmfMap(root_path, program, arch, extra_files):
[email protected]8adce542014-05-22 20:08:4149 """Build simple map representing nmf json."""
mazdae1b899f2014-11-13 07:19:3150 nonsfi_key = arch + '-nonsfi'
[email protected]8adce542014-05-22 20:08:4151 result = {
52 _PROGRAM_KEY: {
mazdae1b899f2014-11-13 07:19:3153 nonsfi_key: {
[email protected]8adce542014-05-22 20:08:4154 # The program path is relative to the root_path.
55 _URL_KEY: os.path.relpath(program, root_path)
mazdae1b899f2014-11-13 07:19:3156 },
[email protected]8adce542014-05-22 20:08:4157 }
58 }
59
60 if extra_files:
61 files = {}
62 for named_file in extra_files:
63 name, path = named_file.split(':', 1)
64 files[name] = {
65 _PORTABLE_KEY: {
66 # Note: use path as is, unlike program path.
67 _URL_KEY: path
68 }
69 }
70 if files:
71 result[_FILES_KEY] = files
72
73 return result
74
75
76def OutputNmf(nmf_map, output_path):
77 """Writes the nmf to an output file at given path in JSON format."""
78 with open(output_path, 'w') as output:
79 json.dump(nmf_map, output, indent=2)
80
81
82def main():
83 args = ParseArgs()
84 if not args.program:
85 logging.error('--program is not specified.')
86 sys.exit(1)
mazdae1b899f2014-11-13 07:19:3187 if not args.arch:
88 logging.error('--arch is not specified.')
89 sys.exit(1)
[email protected]8adce542014-05-22 20:08:4190 if not args.output:
91 logging.error('--output is not specified.')
92 sys.exit(1)
93
94 nmf_map = BuildNmfMap(os.path.dirname(args.output),
mazdae1b899f2014-11-13 07:19:3195 args.program, args.arch, args.extra_files)
[email protected]8adce542014-05-22 20:08:4196 OutputNmf(nmf_map, args.output)
97
98
99if __name__ == '__main__':
100 main()