polca / premise

Coupling Integrated Assessment Models output with Life Cycle Assessment.
BSD 3-Clause "New" or "Revised" License
110 stars 48 forks source link

Handle cache creation for multi-user envs differently #100

Closed tngTUDOR closed 12 months ago

tngTUDOR commented 1 year ago

in 1.5.0alpha7, we have again an error where the cache DIR_PATH is used to create cache for next runs, a multi-user install won't work because of permissions.

Example:

---------------------------------------------------------------------------
PermissionError                           Traceback (most recent call last)
Cell In[10], line 5
      1 scenarios = [
      2     {"model": "image", "pathway":"SSP2-Base", "year": 2040},
      3 ]
----> 5 ndb = NewDatabase(
      6         scenarios = scenarios,        
      7         source_db="ecoinvent-3.8-cutoff",
      8         source_version="3.8",
      9         key='M2A0R2C2H322d470110934871348AFPDOQERPI77,
     10 )
     11 ndb.update_electricity()
     12 #ndb.write_db_to_brightway("test ME")

File /opt/tljh/user/envs/premise-bw2/lib/python3.10/site-packages/premise/ecoinvent_modification.py:492, in NewDatabase.__init__(self, scenarios, source_version, source_type, key, source_db, source_file_path, additional_inventories, system_model, system_args, use_cached_inventories, use_cached_database, external_scenarios, quiet, keep_uncertainty_data, gains_scenario)
    490 print("\n//////////////////// EXTRACTING SOURCE DATABASE ////////////////////")
    491 if use_cached_database:
--> 492     self.database = self.__find_cached_db(
    493         source_db, keep_uncertainty_data=keep_uncertainty_data
    494     )
    495     print("Done!")
    496 else:

File /opt/tljh/user/envs/premise-bw2/lib/python3.10/site-packages/premise/ecoinvent_modification.py:547, in NewDatabase.__find_cached_db(self, db_name, keep_uncertainty_data)
    540 """
    541 If `use_cached_db` = True, then we look for a cached database.
    542 If cannot be found, we create a cache for next time.
    543 :param db_name: database name
    544 :return: database
    545 """
    546 # check that directory exists, otherwise create it
--> 547 Path(DIR_CACHED_DB).mkdir(parents=True, exist_ok=True)
    548 # build file path
    549 if db_name is None:

File /opt/tljh/user/envs/premise-bw2/lib/python3.10/pathlib.py:1175, in Path.mkdir(self, mode, parents, exist_ok)
   1171 """
   1172 Create a new directory at this given path.
   1173 """
   1174 try:
-> 1175     self._accessor.mkdir(self, mode)
   1176 except FileNotFoundError:
   1177     if not parents or self.parent == self:

PermissionError: [Errno 13] Permission denied: '/opt/tljh/user/envs/premise-bw2/lib/python3.10/site-packages/premise/data/cache'