1bd521e3fb281510133faeca1faf36eea153285c
[WebKit-https.git] / Tools / BuildSlaveSupport / ews-app / ews / common / buildbot.py
1 # Copyright (C) 2018 Apple Inc. All rights reserved.
2 #
3 # Redistribution and use in source and binary forms, with or without
4 # modification, are permitted provided that the following conditions
5 # are met:
6 # 1.  Redistributions of source code must retain the above copyright
7 #     notice, this list of conditions and the following disclaimer.
8 # 2.  Redistributions in binary form must reproduce the above copyright
9 #     notice, this list of conditions and the following disclaimer in the
10 #     documentation and/or other materials provided with the distribution.
11 #
12 # THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS'' AND
13 # ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
14 # WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
15 # DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS BE LIABLE FOR
16 # ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
17 # DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
18 # SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
19 # CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
20 # OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
21 # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
22
23 from future.utils import lrange
24
25 import logging
26 import os
27 import re
28 import subprocess
29
30 import ews.common.util as util
31 import ews.config as config
32
33 _log = logging.getLogger(__name__)
34
35
36 class Buildbot():
37     # Buildbot status codes referenced from https://github.com/buildbot/buildbot/blob/master/master/buildbot/process/results.py
38     ALL_RESULTS = lrange(7)
39     SUCCESS, WARNINGS, FAILURE, SKIPPED, EXCEPTION, RETRY, CANCELLED = ALL_RESULTS
40
41     @classmethod
42     def send_patch_to_buildbot(cls, patch_path, properties=[]):
43         command = ['buildbot', 'try',
44                    '--connect=pb',
45                    '--master={}:{}'.format(config.BUILDBOT_SERVER_HOST, config.BUILDBOT_SERVER_PORT),
46                    '--username={}'.format(config.BUILDBOT_PB_USERNAME),
47                    '--passwd={}'.format(config.BUILDBOT_PB_PASSWORD),
48                    '--diff={}'.format(patch_path),
49                    '--repository=']
50
51         for property in properties:
52             command.append('--property={}'.format(property))
53
54         _log.debug('Executing command: {}'.format(command))
55         return_code = subprocess.call(command)
56         if return_code:
57             _log.warn('Error executing: {}, return code={}'.format(command, return_code))
58
59         return return_code
60
61     @classmethod
62     def get_builder_id_to_name_mapping(cls):
63         builder_id_to_name_mapping = {}
64         builder_url = 'http://{}/api/v2/builders'.format(config.BUILDBOT_SERVER_HOST)
65         builders_data = util.fetch_data_from_url(builder_url)
66         if not builders_data:
67             return {}
68         for builder in builders_data.json().get('builders', []):
69             builder_id = builder['builderid']
70             builder_name = builder.get('name')
71             display_name = builder.get('description')
72             if not display_name:
73                 display_name = Buildbot._get_display_name_from_builder_name(builder_name)
74             builder_id_to_name_mapping[builder_id] = {'builder_name': builder_name, 'display_name': display_name}
75         return builder_id_to_name_mapping
76
77     @classmethod
78     def _get_display_name_from_builder_name(cls, builder_name):
79         words = re.split('[, \-_:()]+', builder_name)
80         if not words:
81             return builder_name
82         return words[0].lower()