Ease selection dragging between large/small text lines
[chromium-blink-merge.git] / ppapi / tests / create_nonsfi_test_nmf.py
blob3ac4e502854e66229ff9663f383b838feadbf1f4
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.
6 """Simple tool to generate NMF file by just reformatting given arguments.
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).
14 This script simply reformats the command line arguments into NMF JSON format.
15 """
17 import argparse
18 import collections
19 import json
20 import logging
21 import os
22 import sys
24 _FILES_KEY = 'files'
25 _PORTABLE_KEY = 'portable'
26 _PROGRAM_KEY = 'program'
27 _URL_KEY = 'url'
29 def ParseArgs():
30 parser = argparse.ArgumentParser()
31 parser.add_argument(
32 '--program', metavar='FILE', help='Main program nexe')
33 parser.add_argument(
34 '--arch', metavar='ARCH', choices=('x86-32', 'arm'),
35 help='The archtecture of main program nexe')
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.')
45 return parser.parse_args()
48 def BuildNmfMap(root_path, program, arch, extra_files):
49 """Build simple map representing nmf json."""
50 nonsfi_key = arch + '-nonsfi'
51 result = {
52 _PROGRAM_KEY: {
53 nonsfi_key: {
54 # The program path is relative to the root_path.
55 _URL_KEY: os.path.relpath(program, root_path)
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
70 if files:
71 result[_FILES_KEY] = files
73 return result
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)
82 def main():
83 args = ParseArgs()
84 if not args.program:
85 logging.error('--program is not specified.')
86 sys.exit(1)
87 if not args.arch:
88 logging.error('--arch is not specified.')
89 sys.exit(1)
90 if not args.output:
91 logging.error('--output is not specified.')
92 sys.exit(1)
94 nmf_map = BuildNmfMap(os.path.dirname(args.output),
95 args.program, args.arch, args.extra_files)
96 OutputNmf(nmf_map, args.output)
99 if __name__ == '__main__':
100 main()