[email protected] | 8adce54 | 2014-05-22 20:08:41 | [diff] [blame] | 1 | #!/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 | |
| 8 | This tool is similar to native_client_sdk/src/tools/create_nmf.py. |
| 9 | create_nmf.py handles most cases, with the exception of Non-SFI nexes. |
| 10 | create_nmf.py tries to auto-detect nexe and pexe types based on their contents, |
| 11 | but it does not work for Non-SFI nexes (which don't have a marker to |
| 12 | distinguish them from SFI nexes). |
| 13 | |
| 14 | This script simply reformats the command line arguments into NMF JSON format. |
| 15 | """ |
| 16 | |
| 17 | import argparse |
| 18 | import collections |
| 19 | import json |
| 20 | import logging |
| 21 | import os |
mazda | e1b899f | 2014-11-13 07:19:31 | [diff] [blame] | 22 | import sys |
[email protected] | 8adce54 | 2014-05-22 20:08:41 | [diff] [blame] | 23 | |
| 24 | _FILES_KEY = 'files' |
| 25 | _PORTABLE_KEY = 'portable' |
| 26 | _PROGRAM_KEY = 'program' |
| 27 | _URL_KEY = 'url' |
[email protected] | 8adce54 | 2014-05-22 20:08:41 | [diff] [blame] | 28 | |
| 29 | def ParseArgs(): |
| 30 | parser = argparse.ArgumentParser() |
| 31 | parser.add_argument( |
| 32 | '--program', metavar='FILE', help='Main program nexe') |
mazda | e1b899f | 2014-11-13 07:19:31 | [diff] [blame] | 33 | parser.add_argument( |
hidehiko | d342785 | 2015-02-02 15:26:05 | [diff] [blame] | 34 | '--arch', metavar='ARCH', choices=('x86-32', 'x86-64', 'arm'), |
mazda | e1b899f | 2014-11-13 07:19:31 | [diff] [blame] | 35 | help='The archtecture of main program nexe') |
[email protected] | 8adce54 | 2014-05-22 20:08:41 | [diff] [blame] | 36 | # 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 | |
mazda | e1b899f | 2014-11-13 07:19:31 | [diff] [blame] | 48 | def BuildNmfMap(root_path, program, arch, extra_files): |
[email protected] | 8adce54 | 2014-05-22 20:08:41 | [diff] [blame] | 49 | """Build simple map representing nmf json.""" |
mazda | e1b899f | 2014-11-13 07:19:31 | [diff] [blame] | 50 | nonsfi_key = arch + '-nonsfi' |
[email protected] | 8adce54 | 2014-05-22 20:08:41 | [diff] [blame] | 51 | result = { |
| 52 | _PROGRAM_KEY: { |
mazda | e1b899f | 2014-11-13 07:19:31 | [diff] [blame] | 53 | nonsfi_key: { |
[email protected] | 8adce54 | 2014-05-22 20:08:41 | [diff] [blame] | 54 | # The program path is relative to the root_path. |
| 55 | _URL_KEY: os.path.relpath(program, root_path) |
mazda | e1b899f | 2014-11-13 07:19:31 | [diff] [blame] | 56 | }, |
[email protected] | 8adce54 | 2014-05-22 20:08:41 | [diff] [blame] | 57 | } |
| 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 | |
| 76 | def 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 | |
| 82 | def main(): |
| 83 | args = ParseArgs() |
| 84 | if not args.program: |
| 85 | logging.error('--program is not specified.') |
| 86 | sys.exit(1) |
mazda | e1b899f | 2014-11-13 07:19:31 | [diff] [blame] | 87 | if not args.arch: |
| 88 | logging.error('--arch is not specified.') |
| 89 | sys.exit(1) |
[email protected] | 8adce54 | 2014-05-22 20:08:41 | [diff] [blame] | 90 | 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), |
mazda | e1b899f | 2014-11-13 07:19:31 | [diff] [blame] | 95 | args.program, args.arch, args.extra_files) |
[email protected] | 8adce54 | 2014-05-22 20:08:41 | [diff] [blame] | 96 | OutputNmf(nmf_map, args.output) |
| 97 | |
| 98 | |
| 99 | if __name__ == '__main__': |
| 100 | main() |