~hjd/ubuntu/wily/gyp/debian-merged

« back to all changes in this revision

Viewing changes to pylib/gyp/common.py

  • Committer: Package Import Robot
  • Author(s): Giuseppe Iuculano, 6f75d51
  • Date: 2012-05-28 14:44:34 UTC
  • mfrom: (1.1.9) (6.1.6 sid)
  • Revision ID: package-import@ubuntu.com-20120528144434-l69hqgvkxa5eic5m
Tags: 0.1~svn1395-1
[6f75d51] Imported Upstream version 0.1~svn1395

Show diffs side-by-side

added added

removed removed

Lines of Context:
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.
4
4
 
 
5
from __future__ import with_statement
 
6
 
5
7
import errno
6
8
import filecmp
7
9
import os.path
347
349
def GetFlavor(params):
348
350
  """Returns |params.flavor| if it's set, the system's default flavor else."""
349
351
  flavors = {
 
352
    'cygwin': 'win',
 
353
    'win32': 'win',
350
354
    'darwin': 'mac',
351
355
    'sunos5': 'solaris',
352
356
    'freebsd7': 'freebsd',
353
357
    'freebsd8': 'freebsd',
 
358
    'freebsd9': 'freebsd',
354
359
  }
355
360
  flavor = flavors.get(sys.platform, 'linux')
356
361
  return params.get('flavor', flavor)
357
362
 
358
363
 
 
364
def CopyTool(flavor, out_path):
 
365
  """Finds (mac|sun|win)_tool.gyp in the gyp directory and copies it
 
366
  to |out_path|."""
 
367
  prefix = { 'solaris': 'sun', 'mac': 'mac', 'win': 'win' }.get(flavor, None)
 
368
  if not prefix:
 
369
    return
 
370
 
 
371
  # Slurp input file.
 
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()
 
376
 
 
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:
 
380
    tool_file.write(
 
381
        ''.join([source[0], '# Generated by gyp. Do not edit.\n'] + source[1:]))
 
382
 
 
383
  # Make file executable.
 
384
  os.chmod(tool_path, 0755)
 
385
 
 
386
 
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
364
392
 
365
393
def uniquer(seq, idfun=None):
366
394
    if idfun is None:
367
 
        def idfun(x): return x
 
395
        idfun = lambda x: x
368
396
    seen = {}
369
397
    result = []
370
398
    for item in seq:
373
401
        seen[marker] = 1
374
402
        result.append(item)
375
403
    return result
 
404
 
 
405
 
 
406
class CycleError(Exception):
 
407
  """An exception raised when an unexpected cycle is detected."""
 
408
  def __init__(self, nodes):
 
409
    self.nodes = nodes
 
410
  def __str__(self):
 
411
    return 'CycleError: cycle involving: ' + str(self.nodes)
 
412
 
 
413
 
 
414
def TopologicallySorted(graph, get_edges):
 
415
  """Topologically sort based on a user provided edge definition.
 
416
 
 
417
  Args:
 
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.
 
421
  Returns:
 
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.
 
425
  Raises:
 
426
    CycleError in the event of a cycle.
 
427
  Example:
 
428
    graph = {'a': '$(b) $(c)', 'b': 'hi', 'c': '$(b)'}
 
429
    def GetEdges(node):
 
430
      return re.findall(r'\$\(([^))]\)', graph[node])
 
431
    print TopologicallySorted(graph.keys(), GetEdges)
 
432
    ==>
 
433
    ['a', 'c', b']
 
434
  """
 
435
  get_edges = memoize(get_edges)
 
436
  visited = set()
 
437
  visiting = set()
 
438
  ordered_nodes = []
 
439
  def Visit(node):
 
440
    if node in visiting:
 
441
      raise CycleError(visiting)
 
442
    if node in visited:
 
443
      return
 
444
    visited.add(node)
 
445
    visiting.add(node)
 
446
    for neighbor in get_edges(node):
 
447
      Visit(neighbor)
 
448
    visiting.remove(node)
 
449
    ordered_nodes.insert(0, node)
 
450
  for node in sorted(graph):
 
451
    Visit(node)
 
452
  return ordered_nodes