Upstream version 9.37.193.0
[platform/framework/web/crosswalk.git] / src / xwalk / tools / utils.py
1 #!/usr/bin/env python
2
3 # Copyright (c) 2012 The Chromium Authors. All rights reserved.
4 # Copyright (c) 2013 Intel Corporation. All rights reserved.
5 # Use of this source code is governed by a BSD-style license that can be
6 # found in the LICENSE file.
7
8 ''' This script provides utils for python scripts in crosswalk.
9 '''
10
11 import os
12 import sys
13 import subprocess
14
15 def TryAddDepotToolsToPythonPath():
16   depot_tools = FindDepotToolsInPath()
17   if depot_tools:
18     sys.path.append(depot_tools)
19     python_path = os.environ.get('PYTHONPATH')
20     if python_path:
21       os.environ['PYTHONPATH'] = os.path.pathsep.join(
22           python_path.split(os.path.pathsep)+[depot_tools])
23     else:
24       os.environ['PYTHONPATH'] = depot_tools
25
26 def FindDepotToolsInPath():
27   paths = os.getenv('PATH').split(os.path.pathsep)
28   for path in paths:
29     if os.path.basename(path) == '':
30       # path is end with os.path.pathsep
31       path = os.path.dirname(path)
32     if os.path.basename(path) == 'depot_tools':
33       return path
34   return None
35
36 def IsWindows():
37   return sys.platform == 'cygwin' or sys.platform.startswith('win')
38
39 def IsLinux():
40   return sys.platform.startswith('linux')
41
42 def IsMac():
43   return sys.platform.startswith('darwin')
44
45 def GitExe():
46   if IsWindows():
47     return 'git.bat'
48   else:
49     return 'git'
50
51 def GetCommandOutput(command, cwd=None):
52   proc = subprocess.Popen(command, stdout=subprocess.PIPE,
53                           stderr=subprocess.STDOUT, bufsize=1,
54                           cwd=cwd)
55   output = proc.communicate()[0]
56   result = proc.returncode
57   if result:
58     raise Exception('%s: %s' % (subprocess.list2cmdline(command), output))
59   return output