Source code for uw.local.deploy.bin.dump_binary

"""Extract a single value from the database to a file.

This script takes a query and a filename.  It runs the query, which must
produce a single value that is either bytes or a string, and saves the
result to the file.  This is intended primarily for debugging and diagnosis
purposes.
"""

import argparse
from pathlib import Path
from sys import exit

from uw.sql.wrap import open_psycopg2_db_service_cursor

[docs]def main (): parser = argparse.ArgumentParser () parser.add_argument ('query', type=str) parser.add_argument ('file', type=str) args = parser.parse_args () cursor = open_psycopg2_db_service_cursor () value = cursor.execute_required_value (args.query) if isinstance (value, str): content = value.encode () print ('String of length %d found.' % len (value)) elif isinstance (value, (bytes, memoryview)): content = value print ('Bytes found.') else: print ('Unexpected value of type %s found: %r' % (type (value), value)) exit (-1) print ('Writing %d bytes to %s.' % (len (content), args.file)) Path (args.file).write_bytes (content)