from __future__ import division
from __future__ import print_function
from __future__ import unicode_literals
import os
import random
import socket
import ssl
import sys
import time
def appveyor_vars():
vars = dict([
(
v.replace('APPVEYOR_', '').lower(),
os.getenv(v, '').decode('utf-8')
) for v in [
'APPVEYOR_ACCOUNT_NAME',
'APPVEYOR_BUILD_VERSION',
'APPVEYOR_PROJECT_NAME',
'APPVEYOR_PULL_REQUEST_HEAD_COMMIT',
'APPVEYOR_PULL_REQUEST_HEAD_REPO_BRANCH',
'APPVEYOR_PULL_REQUEST_HEAD_REPO_NAME',
'APPVEYOR_PULL_REQUEST_NUMBER',
'APPVEYOR_PULL_REQUEST_TITLE',
'APPVEYOR_REPO_BRANCH',
'APPVEYOR_REPO_COMMIT',
'APPVEYOR_REPO_COMMIT_AUTHOR',
'APPVEYOR_REPO_COMMIT_AUTHOR_EMAIL',
'APPVEYOR_REPO_COMMIT_MESSAGE',
'APPVEYOR_REPO_COMMIT_MESSAGE_EXTENDED',
'APPVEYOR_REPO_COMMIT_TIMESTAMP',
'APPVEYOR_REPO_NAME',
'APPVEYOR_REPO_PROVIDER',
'APPVEYOR_REPO_TAG_NAME',
'APPVEYOR_URL',
]
])
BUILD_FMT = u'{url}/project/{account_name}/{project_name}/build/{build_version}'
if vars["repo_tag_name"]:
BRANCH_FMT = u'{repo_name} {repo_tag_name} {short_commit}'
else:
BRANCH_FMT = u'{repo_name} {repo_branch} {short_commit}'
vars.update(head_commit=vars["repo_commit"])
if vars["repo_provider"].lower().startswith('github'):
COMMIT_FMT = u'https://github.com/{repo_name}/commit/{repo_commit}'
if vars["pull_request_number"]:
vars.update(head_commit=vars["pull_request_head_commit"])
BRANCH_FMT = u'{repo_name} {repo_branch} pull {pull_request_head_repo_name} {pull_request_head_repo_branch} {short_commit}'
COMMIT_FMT = u'https://github.com/{pull_request_head_repo_name}/commit/{pull_request_head_commit}'
PULL_FMT = u'https://github.com/{repo_name}/pull/{pull_request_number}'
vars.update(pull_url=PULL_FMT.format(**vars))
vars.update(commit_url=COMMIT_FMT.format(**vars))
vars.update(short_commit=vars["head_commit"][:10])
vars.update(
build_url=BUILD_FMT.format(**vars),
branch_detail=BRANCH_FMT.format(**vars),
)
return vars
def notify():
apvy_vars = appveyor_vars()
server, port = sys.argv[1].rsplit(":", 1)
channel = sys.argv[2]
success = sys.argv[3] == "success"
failure = sys.argv[3] == "failure"
if success or failure:
messages = []
messages.append(u"{branch_detail} - {repo_commit_author}: {repo_commit_message}")
if success:
messages.append(u"Build #{build_version} passed. Details: {build_url}")
if failure:
messages.append(u"Build #{build_version} failed. Details: {build_url}")
if "commit_url" in apvy_vars:
messages.append(u"Commit: {commit_url}")
if "pull_url" in apvy_vars:
messages.append(u"Pull: {pull_url}")
else:
messages = sys.argv[3:]
messages = ' '.join(messages)
messages = messages.decode("utf-8").split(',')
print(repr(apvy_vars))
messages = [msg.format(**apvy_vars).strip() for msg in messages]
irc_username = 'appveyor-ci'
irc_nick = irc_username
irc_sock = ssl.wrap_socket(socket.socket(socket.AF_INET, socket.SOCK_STREAM))
irc_sock.connect((socket.gethostbyname(server), int(port)))
irc_sock.send('NICK {0}\r\nUSER {0} * 0 :{0}\r\n'.format(irc_username).encode())
irc_sock.send('JOIN #{0}\r\n'.format(channel).encode())
irc_file = irc_sock.makefile()
while irc_file:
line = irc_file.readline()
print(line.rstrip())
response = line.split()
if response[0] == 'PING':
irc_file.send('PONG {}\r\n'.format(response[1]).encode())
elif response[1] == '433':
irc_sock.send('NICK {}\r\n'.format(irc_nick).encode())
elif response[1] == '001':
time.sleep(5)
for msg in messages:
print(u'PRIVMSG #{} :{}'.format(channel, msg).encode("utf-8"))
irc_sock.send(u'PRIVMSG #{} :{}\r\n'.format(channel, msg).encode("utf-8"))
time.sleep(5)
return
if __name__ == '__main__':
try:
notify()
except:
import traceback
print('ERROR: Failed to send notification: \n' + traceback.format_exc())