Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Merge branch 'master' of https://framagit.org/simgrid/simgrid into CRTP
[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
66 import socket
67 import ssl
68 import sys
69 import time
70
71
72 def appveyor_vars():
73     """
74     Return a dict of key value crafted from appveyor environment variables.
75     """
76     from os import environ
77
78     appveyor_url = environ.get('APPVEYOR_URL')
79     message_extended = environ.get('APPVEYOR_REPO_COMMIT_MESSAGE_EXTENDED')
80     configuration_name = environ.get('CONFIGURATION')
81     branch = environ.get('APPVEYOR_REPO_BRANCH')
82     author = environ.get('APPVEYOR_REPO_COMMIT_AUTHOR')
83     author_email = environ.get('APPVEYOR_REPO_COMMIT_AUTHOR_EMAIL')
84     timestamp = environ.get('APPVEYOR_REPO_COMMIT_TIMESTAMP')
85     repo_provider = environ.get('APPVEYOR_REPO_PROVIDER')
86     project_name = environ.get('APPVEYOR_PROJECT_NAME')
87     project_slug = environ.get('APPVEYOR_PROJECT_SLUG')
88     pull_request_title = environ.get('APPVEYOR_PULL_REQUEST_TITLE')
89     build_version = environ.get('APPVEYOR_BUILD_VERSION')
90     commit = environ.get('APPVEYOR_REPO_COMMIT')
91     message = environ.get('APPVEYOR_REPO_COMMIT_MESSAGE')
92     account_name = environ.get('APPVEYOR_ACCOUNT_NAME')
93     pull_request_number = environ.get('APPVEYOR_PULL_REQUEST_NUMBER')
94     repo_name = environ.get('APPVEYOR_REPO_NAME')
95
96     short_commit = commit[:7]
97     build_url = '{appveyor_url}/project/{account_name}/{project_slug}/build/{build_version}'.format(**locals())
98     commit_url = 'https://{repo_provider}.com/{repo_name}/commit/{commit}'.format(**locals())
99
100     apvy_vars = dict(
101         appveyor_url=appveyor_url,
102         account_name=account_name,
103         project_name=project_name,
104         project_slug=project_slug,
105         build_version=build_version,
106
107         build_url=build_url,
108
109         repo_provider=repo_provider,
110         repo_name=repo_name,
111         branch=branch,
112         configuration_name=configuration_name,
113         author=author,
114         author_email=author_email,
115         timestamp=timestamp,
116         commit=commit,
117         short_commit=short_commit,
118         message=message,
119         message_extended=message_extended,
120
121         pull_request_title=pull_request_title,
122         pull_request_number=pull_request_number,
123
124         commit_url=commit_url,
125
126         color_green='\x033',
127         color_red='\x034',
128         bold='\x02',
129         underline='\x1f',
130         plain='\x0f',
131     )
132     return apvy_vars
133
134
135 if __name__ == '__main__':
136     apvy_vars = appveyor_vars()
137
138     channel = sys.argv[1]
139     messages = sys.argv[2:]
140     messages = ' '.join(messages)
141     messages = messages.split(',')
142     messages = [msg.format(**apvy_vars).strip() for msg in messages]
143
144     irc_username = 'Appveyor'
145     irc_nick = irc_username + str(random.randint(1, 9999))
146
147     try:
148         # establish connection
149         irc_sock = ssl.wrap_socket(socket.socket(socket.AF_INET, socket.SOCK_STREAM))
150         irc_sock.connect((socket.gethostbyname('irc.oftc.net'), 6697))
151         irc_sock.send('NICK {0}\r\nUSER {0} * 0 :{0}\r\n'.format(irc_username).encode('utf_8'))
152         irc_file = irc_sock.makefile()
153
154         while irc_file:
155             line = irc_file.readline()
156             print(line.rstrip())
157             response = line.split()
158
159             if response[0] == 'PING':
160                 irc_file.send('PONG {}\r\n'.format(reponse[1]).encode('utf_8'))
161
162             elif response[1] == '433':
163                 irc_sock.send('NICK {}\r\n'.format(irc_nick).encode('utf_8'))
164
165             elif response[1] == '001':
166                 time.sleep(5)
167                 # join the channel
168                 irc_sock.send('JOIN #{}\r\n'.format(channel).encode('utf_8'))
169                 # send messages
170                 for msg in messages:
171                     print('PRIVMSG #{} :{}'.format(channel, msg))
172                     irc_sock.send('PRIVMSG #{} :{}\r\n'.format(channel, msg).encode('utf_8'))
173                 time.sleep(5)
174                 # leave the channel
175                 irc_sock.send('PART #{}\r\n'.format(channel).encode('utf_8'))
176                 sys.exit()
177     except BaseException:
178         e = sys.exc_info()[0]
179         print(e)
180         sys.exit()