Skip to content

Commit

Permalink
adapt to AWS lambda environment
Browse files Browse the repository at this point in the history
  • Loading branch information
qingeng committed Aug 25, 2023
1 parent 901a813 commit 82b1d07
Show file tree
Hide file tree
Showing 8 changed files with 113 additions and 309 deletions.
13 changes: 8 additions & 5 deletions setup.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
import setuptools
from distutils.util import convert_path
from setuptools.command.install import install
import os
from os.path import expanduser

TIDY3D_DIR = f"{expanduser('~')}/.tidy3d"
from tidy3d.web.cli.constants import TIDY3D_DIR, TIDY3D_DIR2

PACKAGE_NAME = "tidy3d"
REPO_NAME = "tidy3d"
PIP_NAME = "tidy3d"
Expand Down Expand Up @@ -39,8 +38,12 @@ def read_requirements(req_file: str):


def create_config_folder():
if not os.path.exists(TIDY3D_DIR):
os.mkdir(TIDY3D_DIR)
if os.access(TIDY3D_DIR, os.W_OK):
if not os.path.exists(TIDY3D_DIR):
os.mkdir(TIDY3D_DIR)
else:
if not os.path.exists(TIDY3D_DIR2):
os.mkdir(TIDY3D_DIR2)


create_config_folder()
Expand Down
86 changes: 54 additions & 32 deletions tidy3d/plugins/mode/web.py
Original file line number Diff line number Diff line change
Expand Up @@ -212,8 +212,8 @@ def get(
cls,
task_id: str,
solver_id: str,
to_file: str = "mode_solver.json",
sim_file: str = "simulation.json",
to_file: str = "mode_solver.hdf5",
sim_file: str = "simulation.hdf5",
verbose: bool = True,
progress_callback: Callable[[float], None] = None,
) -> ModeSolverTask:
Expand All @@ -225,9 +225,9 @@ def get(
Unique identifier of the task on server.
solver_id: str
Unique identifier of the mode solver in the task.
to_file: str = "mode_solver.json"
to_file: str = "mode_solver.hdf5"
File to store the mode solver downloaded from the task.
sim_file: str = "simulation.json"
sim_file: str = "simulation.hdf5"
File to store the simulation downloaded from the task.
verbose: bool = True
Whether to display progress bars.
Expand Down Expand Up @@ -339,18 +339,18 @@ def abort(self):

def get_modesolver(
self,
to_file: str = "mode_solver.json",
sim_file: str = "simulation.json",
to_file: str = "mode_solver.hdf5",
sim_file: str = "simulation.hdf5",
verbose: bool = True,
progress_callback: Callable[[float], None] = None,
) -> ModeSolver:
"""Get mode solver associated with this task from the server.
Parameters
----------
to_file: str = "mode_solver.json"
to_file: str = "mode_solver.hdf5"
File to store the mode solver downloaded from the task.
sim_file: str = "simulation.json"
sim_file: str = "simulation.hdf5"
File to store the simulation downloaded from the task.
verbose: bool = True
Whether to display progress bars.
Expand All @@ -368,32 +368,54 @@ def get_modesolver(
stored in the same path as 'to_file', but with '.hdf5' extension, and neither 'to_file' or
'sim_file' will be created.
"""
if self.file_type == "Gz":
to_gz = pathlib.Path(to_file).with_suffix(".hdf5.gz")
to_hdf5 = pathlib.Path(to_file).with_suffix(".hdf5")
download_file(
self.task_id,
MODESOLVER_GZ,
to_file=to_gz,
verbose=verbose,
progress_callback=progress_callback,
if not os.access(to_file, os.W_OK):
raise RuntimeError(
f"No permission to write to {to_file}, please enter a path with permissions."
)
extract_gz_file(to_gz, to_hdf5)
to_file = str(to_hdf5)
mode_solver = ModeSolver.from_hdf5(to_hdf5)

elif self.file_type == "Hdf5":
to_hdf5 = pathlib.Path(to_file).with_suffix(".hdf5")
download_file(
self.solver_id,
MODESOLVER_HDF5,
to_file=to_hdf5,
verbose=verbose,
progress_callback=progress_callback,
if not os.access(sim_file, os.W_OK):
raise RuntimeError(
f"No permission to write to {sim_file}, please enter a path with permissions."
)
if self.file_type == "Gz":
hdf5_gz_file, hdf5_gz_file_path = tempfile.mkstemp()
os.close(hdf5_gz_file)
# keep hdf5_file_path
hdf5_file_path = tempfile.NamedTemporaryFile(suffix=".hdf5", delete=False).name
try:
download_file(
self.solver_id,
MODESOLVER_GZ,
to_file=hdf5_gz_file_path,
verbose=verbose,
progress_callback=progress_callback,
)
extract_gz_file(hdf5_gz_file_path, hdf5_file_path)
mode_solver = ModeSolver.from_hdf5(hdf5_file_path)
if to_file.endswith(".json"):
mode_solver.to_json(to_file)
if os.path.exists(hdf5_file_path):
os.remove(hdf5_file_path)
finally:
os.unlink(hdf5_gz_file_path)

to_file = str(to_hdf5)
mode_solver = ModeSolver.from_hdf5(to_hdf5)
elif self.file_type == "Hdf5":
hdf5_file_path = tempfile.NamedTemporaryFile(suffix=".hdf5", delete=False).name
try:
download_file(
self.solver_id,
MODESOLVER_HDF5,
to_file=hdf5_file_path,
verbose=verbose,
progress_callback=progress_callback,
)
mode_solver = ModeSolver.from_hdf5(hdf5_file_path)
if to_file.endswith(".json"):
mode_solver.to_json(to_file)
if os.path.exists(hdf5_file_path):
os.remove(hdf5_file_path)

finally:
pass

else:
download_file(
Expand All @@ -410,14 +432,14 @@ def get_modesolver(
verbose=verbose,
progress_callback=progress_callback,
)

mode_solver_dict = ModeSolver.dict_from_json(to_file)
mode_solver_dict["simulation"] = Simulation.from_json(sim_file)

mode_solver = ModeSolver.parse_obj(mode_solver_dict)

# Overwrite downloaded file with valid contents
mode_solver.to_file(to_file)

return mode_solver

def get_result(
Expand Down
6 changes: 6 additions & 0 deletions tidy3d/web/cli/constants.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
"""Constants for the CLI."""

from os.path import expanduser
import os

TIDY3D_DIR = f"{expanduser('~')}/.tidy3d"
TIDY3D_DIR2 = "/tmp/.tidy3d"
if not os.access(TIDY3D_DIR, os.W_OK):
TIDY3D_DIR = TIDY3D_DIR2

CONFIG_FILE = TIDY3D_DIR + "/config"
CREDENTIAL_FILE = TIDY3D_DIR + "/auth.json"
6 changes: 3 additions & 3 deletions tidy3d/web/http_management.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@

import os
from functools import wraps
from os.path import expanduser
from enum import Enum

import requests
import toml
from tidy3d.web.cli.constants import CONFIG_FILE

from .environment import Env
from ..exceptions import WebError
Expand All @@ -28,8 +28,8 @@ def api_key() -> None:

if os.environ.get(SIMCLOUD_APIKEY):
return os.environ.get(SIMCLOUD_APIKEY)
if os.path.exists(f"{expanduser('~')}/.tidy3d/config"):
with open(f"{expanduser('~')}/.tidy3d/config", encoding="utf-8") as config_file:
if os.path.exists(CONFIG_FILE):
with open(CONFIG_FILE, encoding="utf-8") as config_file:
config = toml.loads(config_file.read())
return config.get("apikey", "")

Expand Down
Loading

0 comments on commit 82b1d07

Please sign in to comment.