Commit e734bd59 authored by Xavier Thompson's avatar Xavier Thompson

pubsub: Fix Python3 incompatibilities

parent 95726826
Pipeline #17091 failed with stage
in 0 seconds
...@@ -13,6 +13,8 @@ import time ...@@ -13,6 +13,8 @@ import time
from datetime import datetime from datetime import datetime
from hashlib import sha512 from hashlib import sha512
from slapos.util import bytes2str, str2bytes
from atomize import Entry from atomize import Entry
from atomize import Feed from atomize import Feed
from atomize import Content from atomize import Content
...@@ -81,7 +83,7 @@ def notify(transaction_id): ...@@ -81,7 +83,7 @@ def notify(transaction_id):
try: try:
callback_filepath = os.path.join(app.config['CALLBACKS'], 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): if not os.path.exists(callback_filepath):
abort(httplib.NOT_FOUND) abort(httplib.NOT_FOUND)
except AttributeError: except AttributeError:
...@@ -90,17 +92,17 @@ def notify(transaction_id): ...@@ -90,17 +92,17 @@ def notify(transaction_id):
abort_it = False 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))) timestamp = int(math.floor(time.mktime(feed.feed.updated_parsed)))
equeue_request = json.dumps({ 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, 'timestamp': timestamp,
}) })
equeue_socket = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM) equeue_socket = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
equeue_socket.connect(app.config['EQUEUE_SOCKET']) equeue_socket.connect(app.config['EQUEUE_SOCKET'])
equeue_socket.send(equeue_request) equeue_socket.send(str2bytes(equeue_request))
result = equeue_socket.recv(len(callback)) result = equeue_socket.recv(len(callback))
equeue_socket.close() equeue_socket.close()
......
...@@ -3,6 +3,7 @@ ...@@ -3,6 +3,7 @@
from __future__ import print_function from __future__ import print_function
import argparse import argparse
import cgi
import csv import csv
import datetime import datetime
import json import json
...@@ -84,15 +85,19 @@ def main(): ...@@ -84,15 +85,19 @@ def main():
while args.max_run > 0: while args.max_run > 0:
try: try:
kwargs = {}
if str is not bytes:
kwargs['errors'] = 'replace'
content = subprocess.check_output( content = subprocess.check_output(
args.executable[0], args.executable[0],
stderr=subprocess.STDOUT stderr=subprocess.STDOUT,
**kwargs
) )
exit_code = 0 exit_code = 0
content = ("OK</br><p>%s ran successfully</p>" content = ("OK</br><p>%s ran successfully</p>"
"<p>Output is: </p><pre>%s</pre>" % ( "<p>Output is: </p><pre>%s</pre>" % (
args.executable[0], args.executable[0],
content.replace('&', '&amp;').replace('<', '&lt;').replace('>', '&gt;') cgi.escape(content)
)) ))
saveStatus('FINISHED') saveStatus('FINISHED')
break break
...@@ -105,7 +110,7 @@ def main(): ...@@ -105,7 +110,7 @@ def main():
"<p>Output is: </p><pre>%s</pre>" % ( "<p>Output is: </p><pre>%s</pre>" % (
args.executable[0], args.executable[0],
exit_code, exit_code,
content.replace('&', '&amp;').replace('<', '&lt;').replace('>', '&gt;') cgi.escape(content)
)) ))
print(content) print(content)
...@@ -163,7 +168,7 @@ def main(): ...@@ -163,7 +168,7 @@ def main():
transaction_id = args.transaction_id[0] if args.transaction_id else int(time.time()*1e6) transaction_id = args.transaction_id[0] if args.transaction_id else int(time.time()*1e6)
notification_path += str(transaction_id) notification_path += str(transaction_id)
headers = {'Content-Type': feed.info().getheader('Content-Type')} headers = {'Content-Type': feed.info().get('Content-Type')}
try: try:
notification = httplib.HTTPConnection(notification_url.hostname, notification = httplib.HTTPConnection(notification_url.hostname,
notification_port) notification_port)
...@@ -189,4 +194,3 @@ def main(): ...@@ -189,4 +194,3 @@ def main():
if __name__ == '__main__': if __name__ == '__main__':
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