Upstream version 7.36.149.0
[platform/framework/web/crosswalk.git] / src / tools / swarming_client / example / common.py
1 # Copyright 2012 The Swarming Authors. All rights reserved.
2 # Use of this source code is governed under the Apache License, Version 2.0 that
3 # can be found in the LICENSE file.
4
5 import datetime
6 import getpass
7 import optparse
8 import os
9 import subprocess
10 import sys
11
12
13 ROOT_DIR = os.path.dirname(os.path.abspath(__file__))
14 sys.path.append(os.path.join(ROOT_DIR, '..', 'third_party'))
15
16 import colorama
17
18
19 CHROMIUM_SWARMING_OSES = {
20     'darwin': 'Mac',
21     'cygwin': 'Windows',
22     'linux2': 'Linux',
23     'win32': 'Windows',
24 }
25
26
27 ISOLATE_OSES = {
28     'darwin': 'mac',
29     'cygwin': 'win',
30     'linux2': 'linux',
31     'win32': 'win',
32 }
33
34
35 def parse_args(use_isolate_server, use_swarming):
36   """Process arguments for the example scripts."""
37   os.chdir(ROOT_DIR)
38   colorama.init()
39
40   parser = optparse.OptionParser(description=sys.modules['__main__'].__doc__)
41   if use_isolate_server:
42     parser.add_option(
43         '-I', '--isolate-server',
44         metavar='URL', default=os.environ.get('ISOLATE_SERVER', ''),
45         help='Isolate server to use')
46   if use_swarming:
47     parser.add_option(
48         '-S', '--swarming',
49         metavar='URL', default=os.environ.get('SWARMING_SERVER', ''),
50         help='Swarming server to use')
51     parser.add_option(
52         '-o', '--os', default=sys.platform,
53         help='Swarming slave OS to request. Should be one of the valid '
54              'sys.platform values like darwin, linux2 or win32 default: '
55              '%default.')
56   parser.add_option('-v', '--verbose', action='count', default=0)
57   options, args = parser.parse_args()
58
59   if args:
60     parser.error('Unsupported argument %s' % args)
61   if use_isolate_server and not options.isolate_server:
62     parser.error('--isolate-server is required.')
63   if use_swarming:
64     if not options.swarming:
65       parser.error('--swarming is required.')
66     options.isolate_os = ISOLATE_OSES[options.os]
67     options.swarming_os = CHROMIUM_SWARMING_OSES[options.os]
68     del options.os
69
70   return options
71
72
73 def unique_task_name():
74   """Makes sure this gets run as an unique task run."""
75   return '%s-%s-hello_world' % (
76       getpass.getuser(),
77       datetime.datetime.now().strftime('%Y-%m-%d-%H-%M-%S'))
78
79
80 def note(text):
81   """Prints a formatted note."""
82   print(
83       colorama.Fore.YELLOW + colorama.Style.BRIGHT + '\n-> ' + text +
84       colorama.Fore.RESET)
85
86
87 def run(cmd, verbose):
88   """Prints the command it runs then run it."""
89   cmd = cmd[:]
90   cmd.extend(['--verbose'] * verbose)
91   print(
92       'Running: %s%s%s' %
93       (colorama.Fore.GREEN, ' '.join(cmd), colorama.Fore.RESET))
94   cmd = [sys.executable, os.path.join('..', cmd[0])] + cmd[1:]
95   if sys.platform != 'win32':
96     cmd = ['time', '-p'] + cmd
97   subprocess.check_call(cmd)
98
99
100 def capture(cmd):
101   """Prints the command it runs then return stdout."""
102   print(
103       'Running: %s%s%s' %
104       (colorama.Fore.GREEN, ' '.join(cmd), colorama.Fore.RESET))
105   cmd = [sys.executable, os.path.join('..', cmd[0])] + cmd[1:]
106   return subprocess.check_output(cmd)