• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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
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
22
23_FILES_KEY = 'files'
24_PORTABLE_KEY = 'portable'
25_PROGRAM_KEY = 'program'
26_URL_KEY = 'url'
27_X86_32_NONSFI_KEY = 'x86-32-nonsfi'
28
29
30def ParseArgs():
31  parser = argparse.ArgumentParser()
32  parser.add_argument(
33      '--program', metavar='FILE', help='Main program nexe')
34  # To keep compatibility with create_nmf.py, we use -x and --extra-files
35  # as flags.
36  parser.add_argument(
37      '-x', '--extra-files', action='append', metavar='KEY:FILE', default=[],
38      help=('Add extra key:file tuple to the "files" '
39            'section of the .nmf'))
40  parser.add_argument(
41      '--output', metavar='FILE', help='Path to the output nmf file.')
42
43  return parser.parse_args()
44
45
46def BuildNmfMap(root_path, program, extra_files):
47  """Build simple map representing nmf json."""
48  result = {
49    _PROGRAM_KEY: {
50      _X86_32_NONSFI_KEY: {
51        # The program path is relative to the root_path.
52        _URL_KEY: os.path.relpath(program, root_path)
53      }
54    }
55  }
56
57  if extra_files:
58    files = {}
59    for named_file in extra_files:
60      name, path = named_file.split(':', 1)
61      files[name] = {
62        _PORTABLE_KEY: {
63          # Note: use path as is, unlike program path.
64          _URL_KEY: path
65        }
66      }
67    if files:
68      result[_FILES_KEY] = files
69
70  return result
71
72
73def OutputNmf(nmf_map, output_path):
74  """Writes the nmf to an output file at given path in JSON format."""
75  with open(output_path, 'w') as output:
76    json.dump(nmf_map, output, indent=2)
77
78
79def main():
80  args = ParseArgs()
81  if not args.program:
82    logging.error('--program is not specified.')
83    sys.exit(1)
84  if not args.output:
85    logging.error('--output is not specified.')
86    sys.exit(1)
87
88  nmf_map = BuildNmfMap(os.path.dirname(args.output),
89                        args.program, args.extra_files)
90  OutputNmf(nmf_map, args.output)
91
92
93if __name__ == '__main__':
94  main()
95