Commit e734bd59 authored by Xavier Thompson's avatar Xavier Thompson

pubsub: Fix Python3 incompatibilities

parent 95726826
......@@ -13,6 +13,8 @@ import time
from datetime import datetime
from hashlib import sha512
from slapos.util import bytes2str, str2bytes
from atomize import Entry
from atomize import Feed
from atomize import Content
......@@ -81,7 +83,7 @@ def notify(transaction_id):
try:
callback_filepath = os.path.join(app.config['CALLBACKS'],
sha512(str(feed.feed.id)).hexdigest())
sha512(feed.feed.id.encode()).hexdigest())
if not os.path.exists(callback_filepath):
abort(httplib.NOT_FOUND)
except AttributeError:
......@@ -90,17 +92,17 @@ def notify(transaction_id):
abort_it = False
for callback in io.open(callback_filepath, 'r', encoding='utf8'):
for callback in io.open(callback_filepath, 'rb'):
timestamp = int(math.floor(time.mktime(feed.feed.updated_parsed)))
equeue_request = json.dumps({
'command': '%s\0--transaction-id\0%s' % (callback, transaction_id),
'command': '%s\0--transaction-id\0%s' % (bytes2str(callback), transaction_id),
'timestamp': timestamp,
})
equeue_socket = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
equeue_socket.connect(app.config['EQUEUE_SOCKET'])
equeue_socket.send(equeue_request)
equeue_socket.send(str2bytes(equeue_request))
result = equeue_socket.recv(len(callback))
equeue_socket.close()
......
......@@ -3,6 +3,7 @@
from __future__ import print_function
import argparse
import cgi
import csv
import datetime
import json
......@@ -84,15 +85,19 @@ def main():
while args.max_run > 0:
try:
kwargs = {}
if str is not bytes:
kwargs['errors'] = 'replace'
content = subprocess.check_output(
args.executable[0],
stderr=subprocess.STDOUT
stderr=subprocess.STDOUT,
**kwargs
)
exit_code = 0
content = ("OK</br><p>%s ran successfully</p>"
"<p>Output is: </p><pre>%s</pre>" % (
args.executable[0],
content.replace('&', '&amp;').replace('<', '&lt;').replace('>', '&gt;')
cgi.escape(content)
))
saveStatus('FINISHED')
break
......@@ -105,7 +110,7 @@ def main():
"<p>Output is: </p><pre>%s</pre>" % (
args.executable[0],
exit_code,
content.replace('&', '&amp;').replace('<', '&lt;').replace('>', '&gt;')
cgi.escape(content)
))
print(content)
......@@ -163,7 +168,7 @@ def main():
transaction_id = args.transaction_id[0] if args.transaction_id else int(time.time()*1e6)
notification_path += str(transaction_id)
headers = {'Content-Type': feed.info().getheader('Content-Type')}
headers = {'Content-Type': feed.info().get('Content-Type')}
try:
notification = httplib.HTTPConnection(notification_url.hostname,
notification_port)
......@@ -189,4 +194,3 @@ def main():
if __name__ == '__main__':
main()
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment