File: nodes.py

package info (click to toggle)
chromium-browser 41.0.2272.118-1
  • links: PTS, VCS
  • area: main
  • in suites: jessie-kfreebsd
  • size: 2,189,132 kB
  • sloc: cpp: 9,691,462; ansic: 3,341,451; python: 712,689; asm: 518,779; xml: 208,926; java: 169,820; sh: 119,353; perl: 68,907; makefile: 28,311; yacc: 13,305; objc: 11,385; tcl: 3,186; cs: 2,225; sql: 2,217; lex: 2,215; lisp: 1,349; pascal: 1,256; awk: 407; ruby: 155; sed: 53; php: 14; exp: 11
file content (219 lines) | stat: -rw-r--r-- 5,296 bytes parent folder | download
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
# Copyright (c) 2014 The Native Client Authors. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.

from conditions import *

"""Nodes for scons to gn

Nodes class provides a means to store, explore and write out a tree
describing the table generated by interating over the scons file.  The
tree is created in the form of:

  TopNode
    Condition
      Object
        Condition
          Property

"""


REMAP = {
  'arm' : 'arm',
  'AND' : 'android',
  'CHR' : 'chrome',
  'LIN' : 'linux',
  'MAC' : 'mac',
  'IOS' : 'ios',
  'WIN' : 'win',
  'x86' : 'x86',
  'x64' : 'x64',
  'bionic' : 'bionic',
  'glibc' : 'glibc',
  'newlib' : 'newlib',
  'pnacl' : 'pnacl'
}

def Express(use, avail, tag):
  use = sorted([REMAP[x] for x in use])
  avail = sorted([REMAP[x] for x in avail])
  if use == avail:
    return None

  if len(use) == 1:
    return '(%s == "%s")' % (tag, use[0])

  luse = len(use)
  lavail = len(avail)
  if luse > (lavail - luse):
    items = ['%s != "%s"' % (tag, i) for i in avail if i not in use]
    return '(' + ' || '.join(items) + ')'

  items = ['%s == "%s"' % (tag, i) for i in use]
  return '(' + ' || '.join(items) + ')'


def Condition(os_use, os_avail, cpu_use, cpu_avail):
  # We can not hash a list, so we sometimes use  a space sperated string.
  if isinstance(cpu_use, str):
    cpu_use = cpu_use.split(' ')
  if isinstance(cpu_avail, str):
    cpu_avail = cpu_avail.split(' ')

  o_cond = Express(os_use, os_avail, 'os')
  c_cond = Express(cpu_use, cpu_avail, 'cpu_arch')

  if not o_cond and not c_cond:
    return None

  if o_cond and c_cond:
    return 'if (%s && %s) {' % (o_cond, c_cond)

  if o_cond:
    return 'if %s {' % o_cond
  return 'if %s {' % c_cond

#
# TopNode
#   Condition
#     Object
#       Condition
#         Property
#
class Node(object):
  def __init__(self, name=''):
    self.children = []
    self.name = name
    self.parent = None

  def DumpInfo(self, depth=0):
    print '%s%s(%s)' % ('  ' * depth, str(type(self)), self.name)
    for child in self.children:
      child.DumpInfo(depth+1)

  def Write(self, fileobj, depth, text):
    for line in text.split('\n'):
      string = '  ' * depth + line + '\n'
      fileobj.write(string)

  def Dump(self, fileobj, depth):
    adjust = self.DumpStart(fileobj, depth)
    for idx, child in enumerate(self.children):
      self.DumpChild(fileobj, child, adjust)
      if idx != len(self.children) - 1:
        fileobj.write('\n')
    self.DumpEnd(fileobj, depth)

  def DumpStart(self, fileobj, depth):
    self.Write(fileobj, depth, self.name)
    return depth

  def DumpEnd(self, fileobj, depth):
    pass

  def DumpChild(self, fileobj, child, depth):
    child.Dump(fileobj, depth)

  def AddChild(self, child):
    self.children.append(child)
    child.parent = self

  def Examine(self, obj):
    obj.Enter(self)
    for child in self.children:
      child.Examine(obj)
    obj.Exit(self)


class TopNode(Node):
  def __init__(self, name):
    Node.__init__(self, name)

  def DumpStart(self, fileobj, depth):
    self.Write(fileobj, depth, "#  Autogenerated from %s.\n\n" % self.name)
    return depth


class ConditionNode(Node):
  def __init__(self, os_use, os_avail, cpu_use, cpu_avail):
    name = Condition(os_use, os_avail, cpu_use, cpu_avail)
    Node.__init__(self, name)

  def Dump(self, fileobj, depth):
    if self.name:
      self.Write(fileobj, depth, self.name)
      depth += 1
    for child in self.children:
      child.Dump(fileobj, depth)
    if self.name:
      self.Write(fileobj, depth - 1, '}')


class ObjectNode(Node):
  def __init__(self, name, obj_type):
    Node.__init__(self, name)
    self.obj_type = obj_type
    self.conditional = set()
    self.unconditional = set()

  def DumpStart(self, fileobj, depth):
    self.Write(fileobj, depth, '%s("%s") {' % (self.obj_type, self.name))
    depth += 1

    # For every conditional only property, set and empty array
    for cond in self.conditional:
      if cond not in self.unconditional:
        self.Write(fileobj, depth, '%s = []' % cond)
    return depth

  def DumpEnd(self, fileobj, depth):
    self.Write(fileobj, depth, '}')


class PropertyNode(Node):
  def __init__(self, name):
    Node.__init__(self, name)

  def Dump(self, fileobj, depth):
    if self.parent.name:
      self.Write(fileobj, depth, '%s += [' % self.name)
    else:
      self.Write(fileobj, depth, '%s = [' % self.name)

    for child in self.children:
      child.Dump(fileobj, depth + 1)

    self.Write(fileobj, depth, ']')


class ValueNode(Node):
  def __init__(self, name):
    Node.__init__(self,  name)

  def Dump(self, fileobj, depth):
    self.Write(fileobj, depth, '"%s",' % self.name)


class OrganizeProperties(object):
  def __init__(self):
    self.cond = None
    self.obj = None
    pass

  def Enter(self, node):
    if isinstance(node, ObjectNode):
      self.obj = node

    if isinstance(node, ConditionNode):
      self.cond = node.name

    if isinstance(node, PropertyNode):
      if self.cond == None:
        self.obj.unconditional |= set([node.name])
      else:
        self.obj.conditional |= set([node.name])
      node.children = sorted(node.children, key=lambda x: x.name)

  def Exit(self, node):
    pass