Skip to content

Commit

Permalink
WIP: DandiExport logger 10
Browse files Browse the repository at this point in the history
  • Loading branch information
CBroz1 committed Mar 25, 2024
1 parent a6b14a2 commit 3fac33a
Showing 1 changed file with 48 additions and 4 deletions.
52 changes: 48 additions & 4 deletions src/spyglass/utils/dj_graph.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
from pathlib import Path
from subprocess import run as subprocess_run
from tempfile import NamedTemporaryFile
from typing import Dict, List

from datajoint import FreeTable
from datajoint import config as dj_config
from datajoint.condition import make_condition
from datajoint.table import Table

Expand Down Expand Up @@ -271,7 +275,47 @@ def as_dict(self) -> List[Dict[str, str]]:
if self._get_restr(table)
]

def export(self):
raise NotImplementedError("Export to file not implemented")
# for table in self._all_ft:
# where = table.where_clause()
def _write_sql_cnf(self):
"""Write SQL cnf file to avoid password prompt"""
cnf_path = ".my.cnf"
if Path(cnf_path).exists():
logger.error(f"File {cnf_path} already exists. Not overwriting.")
return
thisuser = dj_config["database.user"]
thispass = dj_config["database.password"]
thishost = dj_config["database.host"]
credenials = (
f"[client]\nuser={thisuser}\npassword={thispass}\nhost={thishost}\n"
)
with open(".my.cnf", "w") as file:
file.write(credenials)

def _write_mysqldump(
self, dump_fname: str, docker_id=None
) -> NamedTemporaryFile:
"""Write mysqlmdump to a temporary file and return the file object"""
# TODO: How do we want to organize outputs?
# Separate sql files for each schema?
# Do they live in a new SPYGLASS_EXPORT_DIR ?
prefix = f"docker exec -i {docker_id}" if docker_id else ""
file = NamedTemporaryFile(mode="w")
file.write("#!/bin/bash\n")
for table in self._all_ft:
full_name = table.full_table_name
if not (where := table.where_clause()):
continue
database, table_name = full_name.split(".")
cmd = (
f"{prefix}mysqldump {database} {table_name} "
+ f'--where="{where}" >> {dump_fname}\n'
)
file.write(cmd)
file.flush()

return file

def export(self, dump_fname: str = "./temp_export.sql", docker_id=None):
self._write_sql_cnf()
dump_file = self._write_mysqldump(dump_fname, docker_id)
subprocess_run(["chmod", "+x", dump_file.name])
return subprocess_run([dump_file.name])

0 comments on commit 3fac33a

Please sign in to comment.