1
# Copyright (c) 2011 Google Inc. All rights reserved.
1
# Copyright (c) 2012 Google Inc. All rights reserved.
2
2
# Use of this source code is governed by a BSD-style license that can be
3
3
# found in the LICENSE file.
5
from __future__ import with_statement
347
349
def GetFlavor(params):
348
350
"""Returns |params.flavor| if it's set, the system's default flavor else."""
351
355
'sunos5': 'solaris',
352
356
'freebsd7': 'freebsd',
353
357
'freebsd8': 'freebsd',
358
'freebsd9': 'freebsd',
355
360
flavor = flavors.get(sys.platform, 'linux')
356
361
return params.get('flavor', flavor)
364
def CopyTool(flavor, out_path):
365
"""Finds (mac|sun|win)_tool.gyp in the gyp directory and copies it
367
prefix = { 'solaris': 'sun', 'mac': 'mac', 'win': 'win' }.get(flavor, None)
372
source_path = os.path.join(
373
os.path.dirname(os.path.abspath(__file__)), '%s_tool.py' % prefix)
374
with open(source_path) as source_file:
375
source = source_file.readlines()
377
# Add header and write it out.
378
tool_path = os.path.join(out_path, 'gyp-%s-tool' % prefix)
379
with open(tool_path, 'w') as tool_file:
381
''.join([source[0], '# Generated by gyp. Do not edit.\n'] + source[1:]))
383
# Make file executable.
384
os.chmod(tool_path, 0755)
359
387
# From Alex Martelli,
360
388
# http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/52560
361
389
# ASPN: Python Cookbook: Remove duplicates from a sequence
374
402
result.append(item)
406
class CycleError(Exception):
407
"""An exception raised when an unexpected cycle is detected."""
408
def __init__(self, nodes):
411
return 'CycleError: cycle involving: ' + str(self.nodes)
414
def TopologicallySorted(graph, get_edges):
415
"""Topologically sort based on a user provided edge definition.
418
graph: A list of node names.
419
get_edges: A function mapping from node name to a hashable collection
420
of node names which this node has outgoing edges to.
422
A list containing all of the node in graph in topological order.
423
It is assumed that calling get_edges once for each node and caching is
424
cheaper than repeatedly calling get_edges.
426
CycleError in the event of a cycle.
428
graph = {'a': '$(b) $(c)', 'b': 'hi', 'c': '$(b)'}
430
return re.findall(r'\$\(([^))]\)', graph[node])
431
print TopologicallySorted(graph.keys(), GetEdges)
435
get_edges = memoize(get_edges)
441
raise CycleError(visiting)
446
for neighbor in get_edges(node):
448
visiting.remove(node)
449
ordered_nodes.insert(0, node)
450
for node in sorted(graph):