Upstream version 10.38.208.0
[platform/framework/web/crosswalk.git] / src / xwalk / app / tools / android / util.py
1 #!/usr/bin/env python
2
3 # Copyright (c) 2014 Intel Corporation. All rights reserved.
4 # Use of this source code is governed by a BSD-style license that can be
5 # found in the LICENSE file.
6 # pylint: disable=F0401
7
8
9 import os
10 import re
11 import shutil
12 import subprocess
13 import sys
14
15
16 def CleanDir(path):
17   if os.path.exists(path):
18     shutil.rmtree(path)
19
20
21 def AllArchitectures():
22   return ("x86", "arm")
23
24
25 def RunCommand(command, verbose=False, shell=False):
26   """Runs the command list, print the output, and propagate its result."""
27   proc = subprocess.Popen(command, stdout=subprocess.PIPE,
28                           stderr=subprocess.STDOUT, shell=shell)
29   if not shell:
30     output = proc.communicate()[0]
31     result = proc.returncode
32     if verbose:
33       print(output.decode("utf-8").strip())
34     if result != 0:
35       print ('Command "%s" exited with non-zero exit code %d'
36              % (' '.join(command), result))
37       sys.exit(result)
38     return output.decode("utf-8")
39   else:
40     return None
41
42
43 def GetVersion(path):
44   """Get the version of this python tool."""
45   version_str = 'Crosswalk app packaging tool version is '
46   file_handle = open(path, 'r')
47   src_content = file_handle.read()
48   version_nums = re.findall(r'\d+', src_content)
49   version_str += ('.').join(version_nums)
50   file_handle.close()
51   return version_str
52
53
54 def CreateAndCopyDir(src_dir, dest_dir, delete_if_exists=False):
55   if not os.path.isdir(src_dir):
56     return False
57   # create path, except last directory (handled by copytree)
58   pre_dest_dir = os.path.dirname(dest_dir)
59   if not os.path.isdir(pre_dest_dir):
60     try:
61       os.makedirs(pre_dest_dir)  # throws exception on error
62     except OSError:
63       return False
64   if os.path.exists(dest_dir):
65     if delete_if_exists:
66       shutil.rmtree(dest_dir)
67     else:
68       return False
69   shutil.copytree(src_dir, dest_dir)
70   return True
71