...
 
Commits (6)
  • Leo Pound Singer's avatar
    Fix Python 3 type checking in file uploads · ceb3258f
    Leo Pound Singer authored
    Python 3 does not have the built-in `file` type. Instead it has
    a hierarchy of types in the `io` module. This fixes tracebacks like
    the following:
    
        Traceback (most recent call last):
          File "/private/tmp/gwcelery/lib/python3.6/site-packages/celery/app/trace.py", line 382, in trace_task
            R = retval = fun(*args, **kwargs)
          File "/private/tmp/gwcelery/lib/python3.6/site-packages/celery/app/trace.py", line 641, in __protected_call__
            return self.run(*args, **kwargs)
          File "/Users/lpsinger/src/gwcelery/gwcelery/tasks/gracedb.py", line 27, in upload
            graceid, message, filename, filecontents, tags)
          File "/private/tmp/gwcelery/lib/python3.6/site-packages/ligo/gracedb/rest.py", line 820, in writeLog
            elif isinstance(filecontents, file):
        NameError: name 'file' is not defined
    ceb3258f
  • Leo Pound Singer's avatar
    Fix spurious blank GraceDb logging entries in Python 3 · e346c368
    Leo Pound Singer authored
    In Python 3, we started emitting a blank GraceDb message after
    every logging entry. For example, see
    https://gracedb-playground.ligo.org/events/view/T0191.
    
    This is because of a change in `logging.StreamHandler` that caused
    a terminating newline to be written after every logging message,
    in a separate call to `write()`. See
    https://github.com/python/cpython/commit/2a20dfc2aabc3259d5b4276eeec91f83230fdcac.
    e346c368
  • Leo Pound Singer's avatar
    Fix Python 3 unicode exception in file uploads · 87fa27fc
    Leo Pound Singer authored
    This fixes the crash when attempting to create a new file in Python 3:
    
        $ gracedb Test gstlal HighMass coinc.xml
        Traceback (most recent call last):
          File "/Users/lpsinger/local/bin/gracedb", line 564, in <module>
            code = main()
          File "/Users/lpsinger/local/bin/gracedb", line 501, in main
            offline=options.offline, labels=options.labels)
          File "/Users/lpsinger/local/lib/python3.6/site-packages/ligo/gracedb/rest.py", line 619, in createEvent
            return self.post(uri, fields, files=files)
          File "/Users/lpsinger/local/lib/python3.6/site-packages/ligo/gracedb/rest.py", line 381, in post
            return self.post_or_put("POST", *args, **kwargs)
          File "/Users/lpsinger/local/lib/python3.6/site-packages/ligo/gracedb/rest.py", line 399, in post_or_put
            content_type, body = encode_multipart_formdata(body, files)
          File "/Users/lpsinger/local/lib/python3.6/site-packages/ligo/gracedb/rest.py", line 1285, in encode_multipart_formdata
            body = CRLF.join(L)
        TypeError: sequence item 20: expected str instance, bytes found
    87fa27fc
  • Tanner Prestegard's avatar
    Merge branch 'fix-multipart-encoding' into 'master' · 4ff41295
    Tanner Prestegard authored
    Fix Python 3 unicode exception in file uploads
    
    See merge request lscsoft/gracedb-client!19
    4ff41295
  • Tanner Prestegard's avatar
    Merge branch 'fix-file-uploads-types-python3' into 'master' · 32398f54
    Tanner Prestegard authored
    Fix Python 3 type checking in file uploads
    
    See merge request lscsoft/gracedb-client!20
    32398f54
  • Tanner Prestegard's avatar
    Merge branch 'fix-spuriuos-blank-logging-messages' into 'master' · 46d9bda1
    Tanner Prestegard authored
    Fix spurious blank GraceDb logging entries in Python 3
    
    See merge request lscsoft/gracedb-client!21
    46d9bda1
......@@ -86,10 +86,11 @@ class _GraceDbLogStream(object):
self._queue.task_done()
def write(self, text):
with self._lock:
if self._closed:
raise RuntimeError('Log handler is already closed')
self._queue.put(text)
if text.strip():
with self._lock:
if self._closed:
raise RuntimeError('Log handler is already closed')
self._queue.put(text)
def close(self):
with self._lock:
......
......@@ -754,7 +754,7 @@ class GraceDb(GsiRest):
filecontents = sys.stdin.read()
else:
filecontents = open(filename, "rb").read()
elif isinstance(filecontents, file):
elif hasattr(filecontents, 'read'):
# XXX Does not scale well.
filecontents = filecontents.read()
files = [('upload', os.path.basename(filename), filecontents)]
......@@ -817,7 +817,7 @@ class GraceDb(GsiRest):
filecontents = sys.stdin.read()
else:
filecontents = open(filename, "rb").read()
elif isinstance(filecontents, file):
elif hasattr(filecontents, 'read'):
# XXX Does not scale well.
filecontents = filecontents.read()
files = [('upload', os.path.basename(filename), filecontents)]
......@@ -1262,28 +1262,36 @@ def encode_multipart_formdata(fields, files):
files is a sequence of (name, filename, value) elements for data to be uploaded as files
Return (content_type, body) ready for httplib.HTTP instance
"""
BOUNDARY = '----------ThIs_Is_tHe_bouNdaRY_$'
CRLF = '\r\n'
BOUNDARY = b'----------ThIs_Is_tHe_bouNdaRY_$'
CRLF = b'\r\n'
L = []
for (key, value) in fields:
if value is None: continue
L.append('--' + BOUNDARY)
L.append('Content-Disposition: form-data; name="%s"' % key)
L.append('')
# str(value) in case it is unicode
L.append(str(value))
L.append(b'--' + BOUNDARY)
L.append(b'Content-Disposition: form-data; name="%s"' % key.encode())
L.append(b'')
# encode in case it is unicode
if isinstance(value, six.binary_type):
L.append(value)
elif isinstance(value, six.text_type):
L.append(value.encode())
else:
L.append(str(value).encode())
for (key, filename, value) in files:
if value is None: continue
L.append('--' + BOUNDARY)
L.append(b'--' + BOUNDARY)
# str(filename) in case it is unicode
L.append('Content-Disposition: form-data; name="%s"; filename="%s"' % (key, str(filename)))
L.append('Content-Type: %s' % get_content_type(filename))
L.append('')
L.append(value)
L.append('--' + BOUNDARY + '--')
L.append('')
L.append(b'Content-Disposition: form-data; name="%s"; filename="%s"' % (key.encode(), filename.encode()))
L.append(b'Content-Type: %s' % get_content_type(filename).encode())
L.append(b'')
if isinstance(value, six.text_type):
L.append(value.encode())
else:
L.append(value)
L.append(b'--' + BOUNDARY + b'--')
L.append(b'')
body = CRLF.join(L)
content_type = 'multipart/form-data; boundary=%s' % BOUNDARY
content_type = b'multipart/form-data; boundary=%s' % BOUNDARY
return content_type, body
def get_content_type(filename):
......