Changeset View
Changeset View
Standalone View
Standalone View
swh/core/db/__init__.py
# Copyright (C) 2015-2019 The Software Heritage developers | # Copyright (C) 2015-2019 The Software Heritage developers | ||||
# See the AUTHORS file at the top-level directory of this distribution | # See the AUTHORS file at the top-level directory of this distribution | ||||
# License: GNU General Public License version 3, or any later version | # License: GNU General Public License version 3, or any later version | ||||
# See top-level LICENSE file for more information | # See top-level LICENSE file for more information | ||||
import binascii | import binascii | ||||
import datetime | import datetime | ||||
import enum | import enum | ||||
import json | import json | ||||
import logging | |||||
import os | import os | ||||
import threading | import threading | ||||
from contextlib import contextmanager | from contextlib import contextmanager | ||||
import psycopg2 | import psycopg2 | ||||
import psycopg2.extras | import psycopg2.extras | ||||
logger = logging.getLogger(__name__) | |||||
psycopg2.extras.register_uuid() | psycopg2.extras.register_uuid() | ||||
def escape(data): | def escape(data): | ||||
if data is None: | if data is None: | ||||
return '' | return '' | ||||
if isinstance(data, bytes): | if isinstance(data, bytes): | ||||
return '\\x%s' % binascii.hexlify(data).decode('ascii') | return '\\x%s' % binascii.hexlify(data).decode('ascii') | ||||
▲ Show 20 Lines • Show All 146 Lines • ▼ Show 20 Lines | def copy_to(self, items, tblname, columns, | ||||
write_thread = threading.Thread(target=writer) | write_thread = threading.Thread(target=writer) | ||||
write_thread.start() | write_thread.start() | ||||
try: | try: | ||||
with open(write_file, 'w') as f: | with open(write_file, 'w') as f: | ||||
for d in items: | for d in items: | ||||
if item_cb is not None: | if item_cb is not None: | ||||
item_cb(d) | item_cb(d) | ||||
line = [escape(d.get(k, default_values.get(k))) | line = [] | ||||
for k in columns] | for k in columns: | ||||
try: | |||||
vlorentz: This line can probably be moved outside the try. | |||||
value = d.get(k, default_values.get(k)) | |||||
line.append(escape(value)) | |||||
except Exception as e: | |||||
logger.error( | |||||
'Could not escape value `%r` for column `%s`:' | |||||
'Received exception: `%s`', | |||||
value, k, e | |||||
) | |||||
Not Done Inline ActionsYou may be able to do something equivalent with simpler code: except Exception: logger.exception( 'Could not escape value `%r` for column `%s`', value, k) raise vlorentz: You may be able to do something equivalent with simpler code:
```… | |||||
raise e from None | |||||
f.write(','.join(line)) | f.write(','.join(line)) | ||||
f.write('\n') | f.write('\n') | ||||
finally: | finally: | ||||
# No problem bubbling up exceptions, but we still need to make sure | # No problem bubbling up exceptions, but we still need to make sure | ||||
# we finish copying, even though we're probably going to cancel the | # we finish copying, even though we're probably going to cancel the | ||||
# transaction. | # transaction. | ||||
write_thread.join() | write_thread.join() | ||||
def mktemp(self, tblname, cur=None): | def mktemp(self, tblname, cur=None): | ||||
self.cursor(cur).execute('SELECT swh_mktemp(%s)', (tblname,)) | self.cursor(cur).execute('SELECT swh_mktemp(%s)', (tblname,)) |
This line can probably be moved outside the try.