Skip to content

Commit

Permalink
add dynamic install / import and requirements
Browse files Browse the repository at this point in the history
  • Loading branch information
jiashenC committed Aug 19, 2023
1 parent dcca782 commit 106896b
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 3 deletions.
47 changes: 44 additions & 3 deletions evadb/third_party/databases/interface.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,52 @@
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
from evadb.third_party.databases.postgres.postgres_handler import PostgresHandler
import os
import pip
import importlib

INSTALL_CACHE = []


def get_database_handler(engine: str, **kwargs):
"""
Return the database handler. User should modify this function for
their new integrated handlers.
"""

# Dynamically install dependencies.
dynamic_install(engine)

# Dynamically import the top module.
mod = dynamic_import(engine)

if engine == "postgres":
return PostgresHandler(engine, **kwargs)
return mod.PostgresHandler(engine, **kwargs)
else:
raise NotImplementedError(f"Engine {engine} is not supported")
raise NotImplementedError(f"Engine {engine} is not supported")


def dynamic_install(handler_dir):
"""
Dynamically install package from requirements.txt.
"""

# Skip installation
if handler_dir in INSTALL_CACHE:
return

INSTALL_CACHE.append(handler_dir)

req_file = os.path.join(handler_dir, "requirements.txt")
if os.path.isfile(req_file):
with open(req_file) as f:
for package in f.read().splitlines():
if hasattr(pip, "main"):
pip.main(["install", package])
else:
pip._internal.main(["install", package])


def dynamic_import(handler_dir):
import_path = f"evadb.third_party.databases.{handler_dir}.{handler_dir}_handler"
return importlib.import_module(import_path)
2 changes: 2 additions & 0 deletions evadb/third_party/databases/postgres/requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
psycopg<=2.9.7
pandas<=2.0.2

0 comments on commit 106896b

Please sign in to comment.