2019-08-29 08:17:09 -04:00
|
|
|
#!/usr/bin/env python3
|
|
|
|
|
|
|
|
import csv
|
|
|
|
import datetime
|
|
|
|
from io import StringIO
|
|
|
|
import os
|
|
|
|
from pathlib import Path
|
|
|
|
import zipfile
|
|
|
|
|
|
|
|
import psycopg2
|
|
|
|
|
|
|
|
|
2019-09-09 12:23:24 -04:00
|
|
|
class ZipFileExistsError(Exception):
|
|
|
|
pass
|
|
|
|
|
2019-08-29 08:17:09 -04:00
|
|
|
def get_connection():
|
|
|
|
return psycopg2.connect(
|
|
|
|
host=os.environ['PGHOST'],
|
|
|
|
dbname=os.environ['PGDATABASE'],
|
|
|
|
user=os.environ['PGUSER'],
|
|
|
|
password=os.environ['PGPASSWORD']
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
def get_extract_zip_file_path(current_time):
|
|
|
|
base_dir = Path(os.environ['EXTRACTS_DIRECTORY'])
|
2019-08-30 09:11:25 -04:00
|
|
|
file_name = f"data-extract-{current_time:%Y-%m-%d-%H_%M_%S}.zip"
|
2019-08-29 08:17:09 -04:00
|
|
|
return base_dir / file_name
|
|
|
|
|
|
|
|
|
|
|
|
def add_extract_record_to_database(connection, zip_file_path, extracted_time):
|
|
|
|
with connection.cursor() as cur:
|
|
|
|
truncated_time = extracted_time.replace(second=0, microsecond=0)
|
|
|
|
cur.execute('''INSERT INTO
|
|
|
|
bulk_extracts (extracted_on, extract_path)
|
|
|
|
VALUES
|
|
|
|
(%s, %s)
|
|
|
|
''', (truncated_time, str(zip_file_path)))
|
|
|
|
|
|
|
|
connection.commit()
|
|
|
|
|
|
|
|
|
|
|
|
def read_sql(rel_path_from_script):
|
|
|
|
script_directory = Path(__file__).resolve().parent
|
|
|
|
sql_path = script_directory / rel_path_from_script
|
|
|
|
return sql_path.read_text()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def make_data_extract(current_time, connection, zip_file_path):
|
2019-09-09 12:23:24 -04:00
|
|
|
if zip_file_path.exists():
|
|
|
|
raise ZipFileExistsError('Archive file under specified name already exists')
|
|
|
|
|
2019-10-02 10:03:54 -04:00
|
|
|
# Execute data dump as Postgres COPY commands, write from server to /tmp
|
|
|
|
with connection.cursor() as cur:
|
|
|
|
cur.execute(read_sql('./export_attributes.sql'))
|
|
|
|
|
|
|
|
with connection.cursor() as cur:
|
|
|
|
cur.execute(read_sql('./export_uprns.sql'))
|
|
|
|
|
|
|
|
with connection.cursor() as cur:
|
|
|
|
cur.execute(read_sql('./export_edit_history.sql'))
|
|
|
|
|
2019-08-29 08:17:09 -04:00
|
|
|
zip_file_path.parent.mkdir(parents=True, exist_ok=True)
|
|
|
|
|
|
|
|
try:
|
|
|
|
with zipfile.ZipFile(zip_file_path, mode='w') as newzip:
|
2019-10-02 10:03:54 -04:00
|
|
|
newzip.write('README.txt')
|
|
|
|
newzip.write('/tmp/building_attributes.csv', arcname='building_attributes.csv')
|
|
|
|
newzip.write('/tmp/building_uprns.csv', arcname='building_uprns.csv')
|
|
|
|
newzip.write('/tmp/edit_history.csv', arcname='edit_history.csv')
|
2019-08-29 08:17:09 -04:00
|
|
|
|
|
|
|
add_extract_record_to_database(connection, zip_file_path, current_time)
|
|
|
|
except:
|
|
|
|
zip_file_path.unlink()
|
|
|
|
raise
|
|
|
|
|
|
|
|
|
|
|
|
def main():
|
|
|
|
current_time = datetime.datetime.utcnow()
|
|
|
|
conn = get_connection()
|
|
|
|
zip_file_path = get_extract_zip_file_path(current_time)
|
|
|
|
make_data_extract(current_time, conn, zip_file_path)
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
main()
|