From 63ba8647fb31be14c13e833e25b66b630c0be8e3 Mon Sep 17 00:00:00 2001 From: Eren Dogan Date: Thu, 23 Feb 2023 15:05:26 -0800 Subject: [PATCH] Add common.py for top-level scripts --- common.py | 31 +++++++++++++++++++++++++++++++ sram_compiler.py | 19 +++++-------------- 2 files changed, 36 insertions(+), 14 deletions(-) create mode 100644 common.py diff --git a/common.py b/common.py new file mode 100644 index 00000000..e6ec2d73 --- /dev/null +++ b/common.py @@ -0,0 +1,31 @@ +# See LICENSE for licensing information. +# +# Copyright (c) 2016-2023 Regents of the University of California, Santa Cruz +# All rights reserved. +# +""" +Common functions for top-level scripts +""" + +import sys +import os + + +def make_openram_package(): + """ Make sure that OpenRAM can be used as a Python package. """ + + import importlib.util + + # Find the package loader from python/site-packages + openram_loader = importlib.util.find_spec("openram") + + # If openram library isn't found as a python package, import it from + # the $OPENRAM_HOME path. + if openram_loader is None: + OPENRAM_HOME = os.getenv("OPENRAM_HOME") + # Import using spec since the directory can be named something other + # than "openram". + spec = importlib.util.spec_from_file_location("openram", "{}/../__init__.py".format(OPENRAM_HOME)) + module = importlib.util.module_from_spec(spec) + sys.modules["openram"] = module + spec.loader.exec_module(module) diff --git a/sram_compiler.py b/sram_compiler.py index 9031621c..389418b0 100755 --- a/sram_compiler.py +++ b/sram_compiler.py @@ -19,20 +19,11 @@ a Liberty (.lib) file for timing analysis/optimization import sys import os import datetime -try: - import openram -except: - # If openram library isn't found as a python package, - # import it from the $OPENRAM_HOME path. - import importlib.util - OPENRAM_HOME = os.getenv("OPENRAM_HOME") - # Import using spec since the directory can be named something - # other than "openram". - spec = importlib.util.spec_from_file_location("openram", "{}/../__init__.py".format(OPENRAM_HOME)) - module = importlib.util.module_from_spec(spec) - sys.modules["openram"] = module - spec.loader.exec_module(module) - import openram + +# You don't need the next two lines if you're sure that openram package is installed +from common import * +make_openram_package() +import openram (OPTS, args) = openram.parse_args()