summaryrefslogtreecommitdiffstats
path: root/native_client_sdk/src/tools/fix_manifest.py
blob: c9b892f589c421542dadf797f022cd39de64e3ad (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
#!/usr/bin/env python
# Copyright (c) 2014 The Chromium Authors. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.

# Disable the lint error for too-long lines for the URL below.
# pylint: disable=C0301

"""Fix Chrome App manifest.json files for use with multi-platform zip files.

See info about multi-platform zip files here:
https://developer.chrome.com/native-client/devguide/distributing#packaged-application

The manifest.json file needs to point to the correct platform-specific paths,
but we build all toolchains and configurations in the same tree. As a result,
we can't have one manifest.json for all combinations.

Instead, we update the top-level manifest.json file during the build:

  "platforms": [
    {
      "nacl_arch": "x86-64",
      "sub_package_path": "_platform_specific/x86-64/"
    },
    ...

Becomes

  "platforms": [
    {
      "nacl_arch": "x86-64",
      "sub_package_path": "<toolchain>/<config>/_platform_specific/x86-64/"
    },
    ...
"""

import collections
import json
import optparse
import os
import sys

if sys.version_info < (2, 6, 0):
  sys.stderr.write("python 2.6 or later is required run this script\n")
  sys.exit(1)


class Error(Exception):
  """Local Error class for this file."""
  pass


def Trace(msg):
  if Trace.verbose:
    sys.stderr.write(str(msg) + '\n')

Trace.verbose = False


def main(argv):
  parser = optparse.OptionParser(
      usage='Usage: %prog [options] manifest.json', description=__doc__)
  parser.add_option('-p', '--prefix',
                    help='Prefix to set for all sub_package_paths in the '
                    'manifest. If none is specified, the prefix will be '
                    'removed; i.e. the start of the path will be '
                    '"_platform_specific/..."')
  parser.add_option('-v', '--verbose',
                    help='Verbose output', action='store_true')

  options, args = parser.parse_args(argv)
  if options.verbose:
    Trace.verbose = True

  if not args:
    parser.error('Expected manifest file.')

  manifest = args[0]

  Trace('Reading %s' % manifest)
  with open(manifest) as f:
    # Keep the dictionary order. This is only supported on Python 2.7+
    if sys.version_info >= (2, 7, 0):
      data = json.load(f, object_pairs_hook=collections.OrderedDict)
    else:
      data = json.load(f)

  if 'platforms' not in data:
    raise Error('%s does not have "platforms" key.' % manifest)

  platforms = data['platforms']
  if type(platforms) is not list:
    raise Error('Expected "platforms" key to be array.')

  if options.prefix:
    prefix = options.prefix + '/'
  else:
    prefix = ''

  for platform in platforms:
    nacl_arch = platform.get('nacl_arch')

    if 'sub_package_path' not in platform:
      raise Error('Expected each platform to have "sub_package_path" key.')

    sub_package_path = platform['sub_package_path']
    index = sub_package_path.find('_platform_specific')
    if index == -1:
      raise Error('Could not find "_platform_specific" in the '
                  '"sub_package_path" key.')

    new_path = prefix + sub_package_path[index:]
    platform['sub_package_path'] = new_path

    Trace('  %s: "%s" -> "%s"' % (nacl_arch, sub_package_path, new_path))

  with open(manifest, 'w') as f:
    json.dump(data, f, indent=2)

  return 0


if __name__ == '__main__':
  try:
    rtn = main(sys.argv[1:])
  except Error, e:
    sys.stderr.write('%s: %s\n' % (os.path.basename(__file__), e))
    rtn = 1
  except KeyboardInterrupt:
    sys.stderr.write('%s: interrupted\n' % os.path.basename(__file__))
    rtn = 1
  sys.exit(rtn)