Skip to content

Commit

Permalink
Merge pull request #8 from DurhamARC/master
Browse files Browse the repository at this point in the history
Changes to work with python3
  • Loading branch information
GraemeWatt committed Sep 29, 2020
2 parents 1bb95d5 + 7c7cf4f commit 09374ae
Show file tree
Hide file tree
Showing 6 changed files with 46 additions and 23 deletions.
10 changes: 5 additions & 5 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
language: python

python:
- '2.7'
- '3.8'

sudo: required

Expand All @@ -17,15 +17,15 @@ install:

script:
- export CURRENT_PATH=`pwd`
- docker run -v $CURRENT_PATH:$CURRENT_PATH hepdata/hepdata-converter /bin/bash -c "cd $CURRENT_PATH && pip install -I -e .[tests] && coverage run -m unittest discover hepdata_converter_ws/testsuite 'test_*'"
- docker run -v $CURRENT_PATH:$CURRENT_PATH hepdata/hepdata-converter /bin/bash -c "cd $CURRENT_PATH && pip install -I -e . && hepdata-converter-ws -v"
- docker run -v $CURRENT_PATH:$CURRENT_PATH hepdata/hepdata-converter /bin/bash -c "cd $CURRENT_PATH && pip3 install -I -e .[tests] && coverage run -m unittest discover hepdata_converter_ws/testsuite 'test_*'"
- docker run -v $CURRENT_PATH:$CURRENT_PATH hepdata/hepdata-converter /bin/bash -c "cd $CURRENT_PATH && pip3 install -I -e . && hepdata-converter-ws -v"

after_success:
- coveralls

before_deploy:
- export CURRENT_PATH=`pwd`
- docker run -v $CURRENT_PATH:$CURRENT_PATH hepdata/hepdata-converter /bin/bash -c "cd $CURRENT_PATH && rm -rf build hepdata_converter_ws.egg-info"
- docker run -v $CURRENT_PATH:$CURRENT_PATH hepdata/hepdata-converter /bin/bash -c "cd $CURRENT_PATH && rm -rf build hepdata_converter_ws.egg-info hepdata_converter_ws/__pycache__ hepdata_converter_ws/*/__pycache__"

deploy:
provider: pypi
Expand All @@ -35,4 +35,4 @@ deploy:
distributions: "sdist bdist_wheel"
on:
tags: true
repo: HEPData/hepdata-converter-ws
repo: HEPData/hepdata-converter-ws
9 changes: 8 additions & 1 deletion hepdata_converter_ws/__init__.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
# -*- encoding: utf-8 -*-

from flask.app import Flask
import sentry_sdk
from sentry_sdk.integrations.flask import FlaskIntegration

import sys
import hepdata_converter_ws
from hepdata_converter_ws import version
Expand All @@ -13,6 +16,10 @@
USERNAME = 'admin'
PASSWORD = 'default'

sentry_sdk.init(
integrations=[FlaskIntegration()]
)


def create_app(config_filename=None):
app = Flask(__name__)
Expand All @@ -26,7 +33,7 @@ def create_app(config_filename=None):

def main():
if '-v' in sys.argv or '--version' in sys.argv:
print "hepdata-converter-ws version %s" % version.__version__
print("hepdata-converter-ws version %s" % version.__version__)
sys.exit()

app = create_app()
Expand Down
11 changes: 8 additions & 3 deletions hepdata_converter_ws/api.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# -*- encoding: utf-8 -*-
import StringIO
from io import BytesIO
import base64
import os
import tarfile
Expand All @@ -16,6 +16,11 @@
SINGLEFILE_FORMATS = ['root', 'yoda']


@api.route('/debug-sentry')
def trigger_error():
raise Exception('Testing that Sentry picks up this error')


@api.route('/ping', methods=['GET'])
def ping():
return Response('OK')
Expand All @@ -28,7 +33,7 @@ def convert():
archive_name = kwargs['options'].get('filename', 'hepdata-converter-ws-data')
output_format = kwargs['options'].get('output_format', '')

output, os_handle = StringIO.StringIO(), None
output, os_handle = BytesIO(), None
if output_format.lower() in SINGLEFILE_FORMATS or 'table' in kwargs['options']:
os_handle, tmp_output = tempfile.mkstemp()
else:
Expand All @@ -39,7 +44,7 @@ def convert():
conversion_input = os.path.abspath(tmp_dir)
conversion_output = os.path.abspath(tmp_output)

with tarfile.open(mode="r:gz", fileobj=StringIO.StringIO(base64.decodestring(input_tar))) as tar:
with tarfile.open(mode="r:gz", fileobj=BytesIO(base64.b64decode(input_tar))) as tar:
tar.extractall(path=conversion_input)

# one file - treat it as one file input format
Expand Down
6 changes: 3 additions & 3 deletions hepdata_converter_ws/testsuite/__init__.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# -*- encoding: utf-8 -*-
import base64
import tarfile
import cStringIO
from io import BytesIO
from hepdata_converter.testsuite import _parse_path_arguments, construct_testdata_path

__author__ = 'Michał Szostak'
Expand All @@ -14,7 +14,7 @@ def __init__(self, *sample_file_name, **kwargs):

def __call__(self, function):
def _inner(*args, **kwargs):
data_stream = cStringIO.StringIO()
data_stream = BytesIO()
with tarfile.open(mode='w:gz', fileobj=data_stream) as data:
data.add(construct_testdata_path(self.sample_file_name), arcname=self.arcname)

Expand All @@ -32,7 +32,7 @@ def __init__(self, *sample_file_name, **kwargs):

def __call__(self, function):
def _inner(*args, **kwargs):
data_stream = cStringIO.StringIO()
data_stream = BytesIO()
with tarfile.open(mode='w:gz', fileobj=data_stream) as data:
data.add(construct_testdata_path(self.sample_file_name), arcname=self.arcname)

Expand Down
26 changes: 18 additions & 8 deletions hepdata_converter_ws/testsuite/test_server.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# -*- encoding: utf-8 -*-
import cStringIO
from io import BytesIO
from distlib._backport import tarfile
import os
import tarfile
Expand Down Expand Up @@ -27,12 +27,12 @@ def tearDown(self):
def assertMultiLineAlmostEqual(self, first, second, msg=None):
if hasattr(first, 'readlines'):
lines = first.readlines()
elif isinstance(first, (str, unicode)):
elif isinstance(first, str):
lines = first.split('\n')

if hasattr(second, 'readlines'):
orig_lines = second.readlines()
elif isinstance(second, (str, unicode)):
elif isinstance(second, str):
orig_lines = second.split('\n')

# Remove blank lines at end of files
Expand All @@ -43,18 +43,18 @@ def assertMultiLineAlmostEqual(self, first, second, msg=None):
orig_lines.pop()

self.assertEqual(len(lines), len(orig_lines))
for i in xrange(len(lines)):
for i in range(len(lines)):
self.assertEqual(lines[i].strip(), orig_lines[i].strip())


@insert_data_as_tar_base64('oldhepdata/sample.input')
@insert_path('oldhepdata/yaml')
def test_convert(self, hepdata_input_tar, yaml_path):
r = self.app_client.get('/convert', data=json.dumps({'input': hepdata_input_tar,
r = self.app_client.get('/convert', data=json.dumps({'input': hepdata_input_tar.decode('utf-8'),
'options': {'input_format': 'oldhepdata', 'output_format': 'yaml'}}),
headers={'content-type': 'application/json'})

with tarfile.open(mode='r:gz', fileobj=cStringIO.StringIO(r.data)) as tar:
with tarfile.open(mode='r:gz', fileobj=BytesIO(r.data)) as tar:
tar.extractall(path=self.current_tmp)

self.assertDirsEqual(os.path.join(self.current_tmp, 'hepdata-converter-ws-data'), yaml_path)
Expand All @@ -78,7 +78,7 @@ def test_convert_yaml_v0(self, hepdata_input_tar, csv_content, yaml_path):
}),
headers={'content-type': 'application/json'})

with tarfile.open(mode='r:gz', fileobj=cStringIO.StringIO(r.data)) as tar:
with tarfile.open(mode='r:gz', fileobj=BytesIO(r.data)) as tar:
tar.extractall(path=self.current_tmp)

self.assertEqual(len(os.listdir(self.current_tmp)), 1)
Expand Down Expand Up @@ -107,4 +107,14 @@ def test_convert_yaml_invalid_v1(self, hepdata_input_tar, csv_content, yaml_path
}),
headers={'content-type': 'application/json'})

self.assertTrue("did not pass validation" in e.exception.message)
self.assertTrue("did not pass validation" in str(e.exception))

def test_ping(self):
r = self.app_client.get('/ping')
self.assertTrue("OK" in str(r.data))

def test_debug_sentry(self):
with self.assertRaises(Exception) as e:
r = self.app_client.get('/debug-sentry')

self.assertTrue(str(e.exception) == "Testing that Sentry picks up this error")
7 changes: 4 additions & 3 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,9 @@ def get_version():
name='hepdata-converter-ws',
version=get_version(),
install_requires=[
'hepdata-converter>=0.1.35,<0.2',
'flask>=1.1.1,<2'
'hepdata-converter>=0.2',
'flask>=1.1.1,<2',
'sentry-sdk[flask]==0.15.1'
],
extras_require=extras_require,
tests_require=test_requirements,
Expand All @@ -48,5 +49,5 @@ def get_version():
download_url='https://github.com/HEPData/hepdata-converter-ws/tarball/%s' % get_version(),
long_description=long_description,
long_description_content_type='text/markdown',
python_requires='<3'
python_requires='>=3.7'
)

0 comments on commit 09374ae

Please sign in to comment.