summaryrefslogtreecommitdiffstats
path: root/tools/isolate/merge_isolate.py
blob: 07eb8676cc46cdd55d6060e2bbe12ddba463d849 (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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
#!/usr/bin/env python
# Copyright (c) 2012 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.

"""Merges multiple OS-specific gyp dependency lists into one that works on all
of them.


The logic is relatively simple. Takes the current conditions, add more
condition, find the strict subset. Done.
"""

import copy
import logging
import optparse
import re
import sys

import trace_inputs
# Create shortcuts.
from trace_inputs import KEY_TRACKED, KEY_UNTRACKED


def union(lhs, rhs):
  """Merges two compatible datastructures composed of dict/list/set."""
  assert lhs is not None or rhs is not None
  if lhs is None:
    return copy.deepcopy(rhs)
  if rhs is None:
    return copy.deepcopy(lhs)
  assert type(lhs) == type(rhs), (lhs, rhs)
  if hasattr(lhs, 'union'):
    # Includes set, OSSettings and Configs.
    return lhs.union(rhs)
  if isinstance(lhs, dict):
    return dict((k, union(lhs.get(k), rhs.get(k))) for k in set(lhs).union(rhs))
  elif isinstance(lhs, list):
    # Do not go inside the list.
    return lhs + rhs
  assert False, type(lhs)


def eval_content(content):
  """Evaluates a GYP file and return the value defined in it."""
  globs = {'__builtins__': None}
  locs = {}
  value = eval(content, globs, locs)
  assert locs == {}, locs
  assert globs == {'__builtins__': None}, globs
  return value


def verify_variables(variables):
  """Verifies the |variables| dictionary is in the expected format."""
  VALID_VARIABLES = [
    KEY_TRACKED,
    KEY_UNTRACKED,
    'command',
    'read_only',
  ]
  assert isinstance(variables, dict), variables
  assert set(VALID_VARIABLES).issuperset(set(variables)), variables.keys()
  for name, value in variables.iteritems():
    if name == 'read_only':
      assert value in (True, False, None), value
    else:
      assert isinstance(value, list), value
      assert all(isinstance(i, basestring) for i in value), value


def verify_condition(condition):
  """Verifies the |condition| dictionary is in the expected format."""
  VALID_INSIDE_CONDITION = ['variables']
  assert isinstance(condition, list), condition
  assert 2 <= len(condition) <= 3, condition
  assert re.match(r'OS==\"([a-z]+)\"', condition[0]), condition[0]
  for c in condition[1:]:
    assert isinstance(c, dict), c
    assert set(VALID_INSIDE_CONDITION).issuperset(set(c)), c.keys()
    verify_variables(c.get('variables', {}))


def verify_root(value):
  VALID_ROOTS = ['variables', 'conditions']
  assert isinstance(value, dict), value
  assert set(VALID_ROOTS).issuperset(set(value)), value.keys()
  verify_variables(value.get('variables', {}))

  conditions = value.get('conditions', [])
  assert isinstance(conditions, list), conditions
  for condition in conditions:
    verify_condition(condition)


class OSSettings(object):
  """Represents the dependencies for an OS. The structure is immutable."""
  def __init__(self, name, values):
    self.name = name
    verify_variables(values)
    self.tracked = sorted(values.get(KEY_TRACKED, []))
    self.untracked = sorted(values.get(KEY_UNTRACKED, []))
    self.command = values.get('command', [])[:]
    self.read_only = values.get('read_only')

  def union(self, rhs):
    assert self.name == rhs.name
    assert not (self.command and rhs.command)
    var = {
      KEY_TRACKED: sorted(self.tracked + rhs.tracked),
      KEY_UNTRACKED: sorted(self.untracked + rhs.untracked),
      'command': self.command or rhs.command,
      'read_only': rhs.read_only if self.read_only is None else self.read_only,
    }
    return OSSettings(self.name, var)

  def flatten(self):
    out = {}
    if self.command:
      out['command'] = self.command
    if self.tracked:
      out[KEY_TRACKED] = self.tracked
    if self.untracked:
      out[KEY_UNTRACKED] = self.untracked
    if self.read_only is not None:
      out['read_only'] = self.read_only
    return out


class Configs(object):
  """Represents all the OS-specific configurations.

  The self.per_os[None] member contains all the 'else' clauses plus the default
  values. It is not included in the flatten() result.
  """
  def __init__(self, oses):
    self.per_os = {
        None: OSSettings(None, {}),
    }
    self.per_os.update(dict((name, OSSettings(name, {})) for name in oses))

  def union(self, rhs):
    items = list(set(self.per_os.keys() + rhs.per_os.keys()))
    out = Configs(items)
    for key in items:
      out.per_os[key] = union(self.per_os.get(key), rhs.per_os.get(key))
    return out

  def add_globals(self, values):
    for key in self.per_os:
      self.per_os[key] = self.per_os[key].union(OSSettings(key, values))

  def add_values(self, for_os, values):
    self.per_os[for_os] = self.per_os[for_os].union(OSSettings(for_os, values))

  def add_negative_values(self, for_os, values):
    """Includes the variables to all OSes except |for_os|.

    This includes 'None' so unknown OSes gets it too.
    """
    for key in self.per_os:
      if key != for_os:
        self.per_os[key] = self.per_os[key].union(OSSettings(key, values))

  def flatten(self):
    """Returns a flat dictionary representation of the configuration.

    Skips None pseudo-OS.
    """
    return dict(
        (k, v.flatten()) for k, v in self.per_os.iteritems() if k is not None)


def invert_map(variables):
  """Converts a dict(OS, dict(deptype, list(dependencies)) to a flattened view.

  Returns a tuple of:
    1. dict(deptype, dict(dependency, set(OSes)) for easier processing.
    2. All the OSes found as a set.
  """
  KEYS = (
    KEY_TRACKED,
    KEY_UNTRACKED,
    'command',
    'read_only',
  )
  out = dict((key, {}) for key in KEYS)
  for os_name, values in variables.iteritems():
    for key in (KEY_TRACKED, KEY_UNTRACKED):
      for item in values.get(key, []):
        out[key].setdefault(item, set()).add(os_name)

    # command needs special handling.
    command = tuple(values.get('command', []))
    out['command'].setdefault(command, set()).add(os_name)

    # read_only needs special handling.
    out['read_only'].setdefault(values.get('read_only'), set()).add(os_name)
  return out, set(variables)


def reduce_inputs(values, oses):
  """Reduces the invert_map() output to the strictest minimum list.

  1. Construct the inverse map first.
  2. Look at each individual file and directory, map where they are used and
     reconstruct the inverse dictionary.
  3. Do not convert back to negative if only 2 OSes were merged.

  Returns a tuple of:
    1. the minimized dictionary
    2. oses passed through as-is.
  """
  KEYS = (
    KEY_TRACKED,
    KEY_UNTRACKED,
    'command',
    'read_only',
  )
  out = dict((key, {}) for key in KEYS)
  assert all(oses), oses
  if len(oses) > 2:
    for key in KEYS:
      for item, item_oses in values.get(key, {}).iteritems():
        # Converts all oses.difference('foo') to '!foo'.
        assert all(item_oses), item_oses
        missing = oses.difference(item_oses)
        if len(missing) == 1:
          # Replace it with a negative.
          out[key][item] = set(['!' + tuple(missing)[0]])
        elif not missing:
          out[key][item] = set([None])
        else:
          out[key][item] = set(item_oses)
  else:
    for key in KEYS:
      for item, item_oses in values.get(key, {}).iteritems():
        # Converts all oses.difference('foo') to '!foo'.
        assert None not in item_oses, item_oses
        out[key][item] = set(item_oses)
  return out, oses


def convert_map_to_gyp(values, oses):
  """Regenerates back a gyp-like configuration dict from files and dirs
  mappings generated from reduce_inputs().
  """
  # First, inverse the mapping to make it dict first.
  config = {}
  for key in values:
    for item, oses in values[key].iteritems():
      if item is None:
        # For read_only default.
        continue
      for cond_os in oses:
        cond_key = None if cond_os is None else cond_os.lstrip('!')
        # Insert the if/else dicts.
        condition_values = config.setdefault(cond_key, [{}, {}])
        # If condition is negative, use index 1, else use index 0.
        cond_value = condition_values[int((cond_os or '').startswith('!'))]
        variables = cond_value.setdefault('variables', {})

        if item in (True, False):
          # One-off for read_only.
          variables[key] = item
        else:
          if isinstance(item, tuple) and item:
            # One-off for command.
            # Do not merge lists and do not sort!
            # Note that item is a tuple.
            assert key not in variables
            variables[key] = list(item)
          elif item:
            # The list of items (files or dirs). Append the new item and keep
            # the list sorted.
            l = variables.setdefault(key, [])
            l.append(item)
            l.sort()

  out = {}
  for o in sorted(config):
    d = config[o]
    if o is None:
      assert not d[1]
      out = union(out, d[0])
    else:
      c = out.setdefault('conditions', [])
      if d[1]:
        c.append(['OS=="%s"' % o] + d)
      else:
        c.append(['OS=="%s"' % o] + d[0:1])
  return out


def load_gyp(value):
  """Parses one gyp skeleton and returns a Configs() instance.

  |value| is the loaded dictionary that was defined in the gyp file.

  The expected format is strict, anything diverting from the format below will
  throw an assert:
  {
    'variables': {
      'command': [
        ...
      ],
      'isolate_dependency_tracked': [
        ...
      ],
      'isolate_dependency_untracked': [
        ...
      ],
      'read_only': False,
    },
    'conditions': [
      ['OS=="<os>"', {
        'variables': {
          ...
        },
      }, {  # else
        'variables': {
          ...
        },
      }],
      ...
    ],
  }
  """
  verify_root(value)

  # Scan to get the list of OSes.
  conditions = value.get('conditions', [])
  oses = set(re.match(r'OS==\"([a-z]+)\"', c[0]).group(1) for c in conditions)
  configs = Configs(oses)

  # Global level variables.
  configs.add_globals(value.get('variables', {}))

  # OS specific variables.
  for condition in conditions:
    condition_os = re.match(r'OS==\"([a-z]+)\"', condition[0]).group(1)
    configs.add_values(condition_os, condition[1].get('variables', {}))
    if len(condition) > 2:
      configs.add_negative_values(
          condition_os, condition[2].get('variables', {}))
  return configs


def load_gyps(items):
  """Parses each gyp file and returns the merged results.

  It only loads what load_gyp() can process.

  Return values:
    files: dict(filename, set(OS where this filename is a dependency))
    dirs:  dict(dirame, set(OS where this dirname is a dependency))
    oses:  set(all the OSes referenced)
    """
  configs = Configs([])
  for item in items:
    logging.debug('loading %s' % item)
    new_config = load_gyp(eval_content(open(item, 'rb').read()))
    logging.debug('has OSes: %s' % ','.join(k for k in new_config.per_os if k))
    configs = union(configs, new_config)
  logging.debug('Total OSes: %s' % ','.join(k for k in configs.per_os if k))
  return configs


def main(args=None):
  parser = optparse.OptionParser(
      usage='%prog <options> [file1] [file2] ...')
  parser.add_option(
      '-v', '--verbose', action='count', default=0, help='Use multiple times')

  options, args = parser.parse_args(args)
  level = [logging.ERROR, logging.INFO, logging.DEBUG][min(2, options.verbose)]
  logging.basicConfig(
        level=level,
        format='%(levelname)5s %(module)15s(%(lineno)3d):%(message)s')

  trace_inputs.pretty_print(
      convert_map_to_gyp(
          *reduce_inputs(
              *invert_map(
                  load_gyps(args).flatten()))),
      sys.stdout)
  return 0


if __name__ == '__main__':
  sys.exit(main())