Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
add an example of speed profile in the platform
[simgrid.git] / tools / appveyor-irc-notify.py
1 """
2 From: https://raw.githubusercontent.com/wesnoth/wesnoth/b28d8d469af047cbbb20b18757c07b1bfc6afa31/utils/appveyor/irc-notify.py
3 which is from: https://raw.githubusercontent.com/nexB/scancode-toolkit/48aeaf76ce9f53d02223c41c1b2ad1d1ad73b851/etc/scripts/irc-notify.py
4
5 Copyright (C) 2015-2016 Christopher R. Wood
6
7 This program is free software; you can redistribute it and/or modify it under the
8 terms of the GNU General Public License as published by the Free Software Foundation;
9 either version 2 of the License, or (at your option) any later version.
10
11 This program is distributed in the hope that it will be useful, but WITHOUT ANY
12 WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
13 PARTICULAR PURPOSE. See the GNU General Public License for more details.
14
15 You should have received a copy of the GNU General Public License along with this
16 program; if not, write to the Free Software Foundation, Inc., 51 Franklin Street,
17 Fifth Floor, Boston, MA 02110-1301 USA.
18
19 Simple AppVeyor IRC notification script.
20
21 Modified by nexB on October 2016:
22  - rework the handling of environment variables.
23  - made the script use functions
24  - support only Appveyor loading its environment variable to craft IRC notices.
25
26 Modified by Jyrki Vesterinen on November 2016:
27
28  - join the channel instead of sending an external message.
29
30 The first argument is a Freenode channel. Other arguments passed to the script will be
31 sent as notice messages content and any {var}-formatted environment variables will be
32 expanded automatically, replaced with a corresponding Appveyor environment variable
33 value. se commas to delineate multiple messages.
34
35 Modified by Martin Quinson on June 2018:
36
37  - Use OFTC instead of Freenode
38
39 Example:
40 export APPVEYOR_URL=https://ci.appveyor.com
41 export APPVEYOR_PROJECT_NAME=attributecode
42 export APPVEYOR_REPO_COMMIT_AUTHOR=pombredanne
43 export APPVEYOR_REPO_COMMIT_TIMESTAMP=2016-10-31
44 export APPVEYOR_REPO_PROVIDER=gihub
45 export APPVEYOR_REPO_BRANCH=repo_branch
46 export APPVEYOR_PULL_REQUEST_TITLE=pull_request_title
47 export APPVEYOR_BUILD_VERSION=1
48 export APPVEYOR_REPO_COMMIT=22c95b72e29248dc4de9b85e590ee18f6f587de8
49 export APPVEYOR_REPO_COMMIT_MESSAGE="some IRC test"
50 export APPVEYOR_ACCOUNT_NAME=nexB
51 export APPVEYOR_PULL_REQUEST_NUMBER=pull_request_number
52 export APPVEYOR_REPO_NAME=nexB/attributecode
53 python etc/scripts/irc-notify.py aboutcode '[{project_name}:{branch}] {short_commit}: "{message}" ({author}) {color_red}Succeeded','Details: {build_url} | Commit: {commit_url}'
54
55 See also https://github.com/gridsync/gridsync/blob/master/appveyor.yml for examples
56 in Appveyor's YAML:
57
58     on_success:
59       - "python etc/scripts/irc-notify.py channel [{project_name}:{branch}] {short_commit}: \"{message}\" ({author}) {color_green}Succeeded,Details: {build_url},Commit: {commit_url}"
60     on_failure:
61       - "python etc/scripts/irc-notify.py channel [{project_name}:{branch}] {short_commit}: \"{message}\" ({author}) {color_red}Failed,Details: {build_url},Commit: {commit_url}"
62
63 """
64
65 import random, socket, ssl, sys, time
66
67
68 def appveyor_vars():
69     """
70     Return a dict of key value crafted from appveyor environment variables.
71     """
72     from os import environ
73
74     appveyor_url = environ.get('APPVEYOR_URL')
75     message_extended = environ.get('APPVEYOR_REPO_COMMIT_MESSAGE_EXTENDED')
76     configuration_name = environ.get('CONFIGURATION')
77     branch = environ.get('APPVEYOR_REPO_BRANCH')
78     author = environ.get('APPVEYOR_REPO_COMMIT_AUTHOR')
79     author_email = environ.get('APPVEYOR_REPO_COMMIT_AUTHOR_EMAIL')
80     timestamp = environ.get('APPVEYOR_REPO_COMMIT_TIMESTAMP')
81     repo_provider = environ.get('APPVEYOR_REPO_PROVIDER')
82     project_name = environ.get('APPVEYOR_PROJECT_NAME')
83     project_slug = environ.get('APPVEYOR_PROJECT_SLUG')
84     pull_request_title = environ.get('APPVEYOR_PULL_REQUEST_TITLE')
85     build_version = environ.get('APPVEYOR_BUILD_VERSION')
86     commit = environ.get('APPVEYOR_REPO_COMMIT')
87     message = environ.get('APPVEYOR_REPO_COMMIT_MESSAGE')
88     account_name = environ.get('APPVEYOR_ACCOUNT_NAME')
89     pull_request_number = environ.get('APPVEYOR_PULL_REQUEST_NUMBER')
90     repo_name = environ.get('APPVEYOR_REPO_NAME')
91
92     short_commit = commit[:7]
93     build_url = '{appveyor_url}/project/{account_name}/{project_slug}/build/{build_version}'.format(**locals())
94     commit_url = 'https://{repo_provider}.com/{repo_name}/commit/{commit}'.format(**locals())
95
96     apvy_vars = dict(
97         appveyor_url=appveyor_url,
98         account_name=account_name,
99         project_name=project_name,
100         project_slug=project_slug,
101         build_version=build_version,
102
103         build_url=build_url,
104
105         repo_provider=repo_provider,
106         repo_name=repo_name,
107         branch=branch,
108         configuration_name=configuration_name,
109         author=author,
110         author_email=author_email,
111         timestamp=timestamp,
112         commit=commit,
113         short_commit=short_commit,
114         message=message,
115         message_extended=message_extended,
116
117         pull_request_title=pull_request_title,
118         pull_request_number=pull_request_number,
119
120         commit_url=commit_url,
121
122         color_green='\x033',
123         color_red='\x034',
124         bold='\x02',
125         underline='\x1f',
126         plain='\x0f',
127     )
128     return apvy_vars
129
130
131 if __name__ == '__main__':
132     apvy_vars = appveyor_vars()
133
134     channel = sys.argv[1]
135     messages = sys.argv[2:]
136     messages = ' '.join(messages)
137     messages = messages.split(',')
138     messages = [msg.format(**apvy_vars).strip() for msg in messages]
139
140     irc_username = 'Appveyor'
141     irc_nick = irc_username + str(random.randint(1, 9999))
142
143     try:
144         # establish connection
145         irc_sock = ssl.wrap_socket(socket.socket(socket.AF_INET, socket.SOCK_STREAM))
146         irc_sock.connect((socket.gethostbyname('irc.oftc.net'), 6697))
147         irc_sock.send('NICK {0}\r\nUSER {0} * 0 :{0}\r\n'.format(irc_username).encode('utf_8'))
148         irc_file = irc_sock.makefile()
149
150         while irc_file:
151             line = irc_file.readline()
152             print(line.rstrip())
153             response = line.split()
154
155             if response[0] == 'PING':
156                 irc_file.send('PONG {}\r\n'.format(reponse[1]).encode('utf_8'))
157
158             elif response[1] == '433':
159                 irc_sock.send('NICK {}\r\n'.format(irc_nick).encode('utf_8'))
160
161             elif response[1] == '001':
162                 time.sleep(5)
163                 # join the channel
164                 irc_sock.send('JOIN #{}\r\n'.format(channel).encode('utf_8'))
165                 # send messages
166                 for msg in messages:
167                     print('PRIVMSG #{} :{}'.format(channel, msg))
168                     irc_sock.send('PRIVMSG #{} :{}\r\n'.format(channel, msg).encode('utf_8'))
169                 time.sleep(5)
170                 # leave the channel
171                 irc_sock.send('PART #{}\r\n'.format(channel).encode('utf_8'))
172                 sys.exit()
173     except:
174         e = sys.exc_info()[0]
175         print(e)
176         sys.exit()