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