Skip to content

Commit cf613ff

Browse files
feat(magic): use tempfile.TemporaryDirectory for downloaded file
1 parent c89f411 commit cf613ff

File tree

2 files changed

+22
-25
lines changed

2 files changed

+22
-25
lines changed

singlestoredb/magics/run_personal.py

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import os
2-
import time
2+
import tempfile
33
from typing import Any
44

55
from IPython.core.interactiveshell import InteractiveShell
@@ -32,6 +32,7 @@ def run_personal(self, line: str, local_ns: Any = None) -> Any:
3232
%run_personal {{ sample_notebook_name }}
3333
3434
"""
35+
3536
template = Template(line.strip())
3637
personal_file = template.render(local_ns)
3738
if not personal_file:
@@ -42,16 +43,14 @@ def run_personal(self, line: str, local_ns: Any = None) -> Any:
4243
if not personal_file:
4344
raise ValueError('No personal file specified.')
4445

45-
local_filename = (
46-
f'{int(time.time() * 1_000_000)}_{personal_file}'.replace(' ', '_')
47-
)
48-
sql_command = f"DOWNLOAD PERSONAL FILE '{personal_file}' TO '{local_filename}'"
49-
50-
# Execute the SQL command
51-
self.shell.run_line_magic('sql', sql_command)
52-
# Run the downloaded file
53-
self.shell.run_line_magic('run', local_filename)
54-
55-
# Delete the local file after running it
56-
if os.path.exists(local_filename):
57-
os.remove(local_filename)
46+
with tempfile.TemporaryDirectory() as temp_dir:
47+
temp_file_path = os.path.join(temp_dir, personal_file)
48+
sql_command = (
49+
f"DOWNLOAD PERSONAL FILE '{personal_file}' "
50+
f"TO '{temp_file_path}'"
51+
)
52+
53+
# Execute the SQL command
54+
self.shell.run_line_magic('sql', sql_command)
55+
# Run the downloaded file
56+
self.shell.run_line_magic('run', f'"{temp_file_path}"')

singlestoredb/magics/run_shared.py

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import os
2-
import time
2+
import tempfile
33
from typing import Any
44

55
from IPython.core.interactiveshell import InteractiveShell
@@ -32,6 +32,7 @@ def run_shared(self, line: str, local_ns: Any = None) -> Any:
3232
%run_shared {{ sample_notebook_name }}
3333
3434
"""
35+
3536
template = Template(line.strip())
3637
shared_file = template.render(local_ns)
3738
if not shared_file:
@@ -42,14 +43,11 @@ def run_shared(self, line: str, local_ns: Any = None) -> Any:
4243
if not shared_file:
4344
raise ValueError('No personal file specified.')
4445

45-
local_filename = f'{int(time.time() * 1_000_000)}_{shared_file}'.replace(' ', '_')
46-
sql_command = f"DOWNLOAD SHARED FILE '{shared_file}' TO '{local_filename}'"
47-
48-
# Execute the SQL command
49-
self.shell.run_line_magic('sql', sql_command)
50-
# Run the downloaded file
51-
self.shell.run_line_magic('run', local_filename)
46+
with tempfile.TemporaryDirectory() as temp_dir:
47+
temp_file_path = os.path.join(temp_dir, shared_file)
48+
sql_command = f"DOWNLOAD SHARED FILE '{shared_file}' TO '{temp_file_path}'"
5249

53-
# Delete the local file after running it
54-
if os.path.exists(local_filename):
55-
os.remove(local_filename)
50+
# Execute the SQL command
51+
self.shell.run_line_magic('sql', sql_command)
52+
# Run the downloaded file
53+
self.shell.run_line_magic('run', f'"{temp_file_path}"')

0 commit comments

Comments
 (0)