Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixes determining resource path #811

Merged
merged 6 commits into from
Nov 18, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 2 additions & 8 deletions pcmdi_metrics/diurnal/scripts/savg_fourier.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,9 @@
import cdms2
import cdutil
import MV2
import pkg_resources

import pcmdi_metrics
from pcmdi_metrics import resources
from pcmdi_metrics.diurnal.common import P, monthname_d, populateStringConstructor


Expand Down Expand Up @@ -244,13 +244,7 @@ def spacevavg(tvarb1, tvarb2, sftlf, model):
OUT = pcmdi_metrics.io.base.Base(
os.path.abspath(args.results_dir), os.path.basename(jsonname)
)
try:
egg_pth = pkg_resources.resource_filename(
pkg_resources.Requirement.parse("pcmdi_metrics"), "share/pmp"
)
except Exception:
# python 2 seems to fail when ran in home directory of source?
egg_pth = os.path.join(os.getcwd(), "share", "pmp")
egg_pth = resources.resource_path()
disclaimer = open(os.path.join(egg_pth, "disclaimer.txt")).read()
metrics_dictionary["DISCLAIMER"] = disclaimer
metrics_dictionary[
Expand Down
10 changes: 2 additions & 8 deletions pcmdi_metrics/diurnal/scripts/std_of_dailymeans.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,9 @@
import cdp
import cdutil
import numpy.ma
import pkg_resources

import pcmdi_metrics
from pcmdi_metrics import resources
from pcmdi_metrics.diurnal.common import (
INPUT,
P,
Expand Down Expand Up @@ -148,13 +148,7 @@ def compute(param):
stats_dic = metrics_dictionary["RESULTS"]

OUT = pcmdi_metrics.io.base.Base(os.path.abspath(args.results_dir), jsonFile())
try:
egg_pth = pkg_resources.resource_filename(
pkg_resources.Requirement.parse("pcmdi_metrics"), "share/pmp"
)
except Exception:
# python 2 seems to fail when ran in home directory of source?
egg_pth = os.path.join(os.getcwd(), "share", "pmp")
egg_pth = resources.resource_path()
disclaimer = open(os.path.join(egg_pth, "disclaimer.txt")).read()
metrics_dictionary["DISCLAIMER"] = disclaimer
metrics_dictionary["REFERENCE"] = (
Expand Down
10 changes: 2 additions & 8 deletions pcmdi_metrics/diurnal/scripts/std_of_hourlyvalues.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,9 @@
import cdp
import cdutil
import numpy.ma
import pkg_resources

import pcmdi_metrics
from pcmdi_metrics import resources
from pcmdi_metrics.diurnal.common import (
INPUT,
P,
Expand Down Expand Up @@ -159,13 +159,7 @@ def compute(param):
stats_dic = metrics_dictionary["RESULTS"]

OUT = pcmdi_metrics.io.base.Base(os.path.abspath(args.results_dir), jsonFile())
try:
egg_pth = pkg_resources.resource_filename(
pkg_resources.Requirement.parse("pcmdi_metrics"), "share/pmp"
)
except Exception:
# python 2 seems to fail when ran in home directory of source?
egg_pth = os.path.join(os.getcwd(), "share", "pmp")
egg_pth = resources.resource_path()
disclaimer = open(os.path.join(egg_pth, "disclaimer.txt")).read()
metrics_dictionary["DISCLAIMER"] = disclaimer
metrics_dictionary["REFERENCE"] = (
Expand Down
10 changes: 2 additions & 8 deletions pcmdi_metrics/diurnal/scripts/std_of_meandiurnalcycle.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,9 @@
import cdp
import cdutil
import genutil
import pkg_resources

import pcmdi_metrics
from pcmdi_metrics import resources
from pcmdi_metrics.diurnal.common import (
INPUT,
P,
Expand Down Expand Up @@ -161,13 +161,7 @@ def compute(param):
stats_dic = metrics_dictionary["RESULTS"]

OUT = pcmdi_metrics.io.base.Base(os.path.abspath(args.results_dir), jsonFile())
try:
egg_pth = pkg_resources.resource_filename(
pkg_resources.Requirement.parse("pcmdi_metrics"), "share/pmp"
)
except Exception:
# python 2 seems to fail when ran in home directory of source?
egg_pth = os.path.join(os.getcwd(), "share", "pmp")
egg_pth = resources.resource_path()
disclaimer = open(os.path.join(egg_pth, "disclaimer.txt")).read()
metrics_dictionary["DISCLAIMER"] = disclaimer
metrics_dictionary["REFERENCE"] = (
Expand Down
10 changes: 2 additions & 8 deletions pcmdi_metrics/driver/dataset.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@

import cdms2
import cdutil
import pkg_resources
from six import with_metaclass

from pcmdi_metrics import resources
from pcmdi_metrics.io.base import Base


Expand Down Expand Up @@ -120,13 +120,7 @@ def get(self):
@staticmethod
def load_path_as_file_obj(name):
"""Returns a File object for the file named name."""
try:
egg_pth = pkg_resources.resource_filename(
pkg_resources.Requirement.parse("pcmdi_metrics"), "share/pmp"
)
except Exception:
# python 2 seems to fail when ran in home directory of source?
egg_pth = os.path.join(os.getcwd(), "share", "pmp")
egg_pth = resources.resource_path()
file_path = os.path.join(egg_pth, name)
opened_file = None
try:
Expand Down
6 changes: 2 additions & 4 deletions pcmdi_metrics/driver/pmp_parser.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import os

import cdp.cdp_parser
import pkg_resources

import pcmdi_metrics.driver.pmp_parameter
from pcmdi_metrics import resources

try:
basestring # noqa
Expand All @@ -13,9 +13,7 @@

def path_to_default_args():
"""Returns path to Default Common Input Arguments in package egg."""
egg_pth = pkg_resources.resource_filename(
pkg_resources.Requirement.parse("pcmdi_metrics"), "share/pmp"
)
egg_pth = resources.resource_path()
file_path = os.path.join(egg_pth, "DefArgsCIA.json")
return file_path

Expand Down
9 changes: 2 additions & 7 deletions pcmdi_metrics/enso/enso_driver.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
import time

import cdms2
import pkg_resources
from EnsoMetrics.EnsoCollectionsLib import (
CmipVariables,
ReferenceObservations,
Expand All @@ -18,6 +17,7 @@
from EnsoMetrics.EnsoComputeMetricsLib import ComputeCollection
from genutil import StringConstructor

from pcmdi_metrics import resources
from pcmdi_metrics.enso.lib import (
AddParserArgument,
CLIVAR_LargeEnsemble_Variables,
Expand Down Expand Up @@ -120,12 +120,7 @@
# Prepare loop iteration
# -------------------------------------------------
# Environmental setup
try:
egg_pth = pkg_resources.resource_filename(
pkg_resources.Requirement.parse("pcmdi_metrics"), "share/pmp"
)
except Exception:
egg_pth = os.path.join(sys.prefix, "share", "pmp")
egg_pth = resources.resource_path()
print("egg_pth:", egg_pth)

# Create output directory
Expand Down
10 changes: 2 additions & 8 deletions pcmdi_metrics/enso/scripts_pcmdi/enso_driver_obsOnly.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,12 @@
import glob
import json
import os
import sys

import pkg_resources
from EnsoMetrics.EnsoCollectionsLib import ReferenceObservations, defCollection
from EnsoMetrics.EnsoComputeMetricsLib import ComputeCollection_ObsOnly
from genutil import StringConstructor

from pcmdi_metrics import resources
from pcmdi_metrics.enso.lib import AddParserArgument, metrics_to_json

# To avoid below error when using multi cores
Expand Down Expand Up @@ -100,12 +99,7 @@
# Prepare loop iteration
# -------------------------------------------------
# Environmental setup
try:
egg_pth = pkg_resources.resource_filename(
pkg_resources.Requirement.parse("pcmdi_metrics"), "share/pmp"
)
except Exception:
egg_pth = os.path.join(sys.prefix, "share", "pmp")
egg_pth = resources.resource_path()
print("egg_pth:", egg_pth)

# Create output directory
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,9 +53,9 @@
import matplotlib.pyplot as plt
import MV2
import numpy as np
import pkg_resources

import pcmdi_metrics
from pcmdi_metrics import resources
from pcmdi_metrics.monsoon_sperber.lib import (
AddParserArgument,
YearCheck,
Expand Down Expand Up @@ -202,9 +202,7 @@ def tree():
# Loop start for given models
# -------------------------------------------------
regions_specs = {}
egg_pth = pkg_resources.resource_filename(
pkg_resources.Requirement.parse("pcmdi_metrics"), "share/pmp"
)
egg_pth = resources.resource_path()
exec(
compile(
open(os.path.join(egg_pth, "default_regions.py")).read(),
Expand Down
10 changes: 2 additions & 8 deletions pcmdi_metrics/monsoon_wang/monsoon_wang_driver.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@
import cdms2
import genutil
import numpy
import pkg_resources
from genutil import statistics

import pcmdi_metrics
from pcmdi_metrics import resources
from pcmdi_metrics.monsoon_wang import mpd, mpi_skill_scores
from pcmdi_metrics.pcmdi.pmp_parser import PMPParser

Expand Down Expand Up @@ -151,13 +151,7 @@ def monsoon_wang_runner(args):
raise RuntimeError("No model file found!")
#########################################

try:
egg_pth = pkg_resources.resource_filename(
pkg_resources.Requirement.parse("pcmdi_metrics"), "share/pmp"
)
except Exception:
# python 2 seems to fail when ran in home directory of source?
egg_pth = os.path.join(os.getcwd(), "share", "pmp")
egg_pth = resources.resource_path()
globals = {}
locals = {}
exec(
Expand Down
26 changes: 26 additions & 0 deletions pcmdi_metrics/resources.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import os

import pkg_resources


def resource_path():
if "CONDA_PREFIX" in os.environ:
res_path = os.path.join(os.environ["CONDA_PREFIX"], "share", "pmp")

# Handle edge case when in conda env but the package is not installed
# with conda
if os.path.exists(res_path):
return res_path

try:
res_path = pkg_resources.resource_filename(
pkg_resources.Requirement.parse("pcmdi_metrics"),
"share/pmp",
)
except Exception:
res_path = os.path.join(os.getcwd(), "share", "pmp")

# Should never fail this
assert os.path.exists(res_path)

return res_path
6 changes: 2 additions & 4 deletions pcmdi_metrics/variability_mode/variability_modes_driver.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,10 +58,10 @@
import cdtime
import cdutil
import MV2
import pkg_resources
from genutil import StringConstructor

import pcmdi_metrics
from pcmdi_metrics import resources
from pcmdi_metrics.variability_mode.lib import (
AddParserArgument,
VariabilityModeCheck,
Expand Down Expand Up @@ -95,9 +95,7 @@
os.environ["UVCDAT_ANONYMOUS_LOG"] = "no"

regions_specs = {}
egg_pth = pkg_resources.resource_filename(
pkg_resources.Requirement.parse("pcmdi_metrics"), "share/pmp"
)
egg_pth = resources.resource_path()
exec(
compile(
open(os.path.join(egg_pth, "default_regions.py")).read(),
Expand Down
Empty file added tests/__init__.py
Empty file.
8 changes: 0 additions & 8 deletions tests/test_placeholder.py

This file was deleted.

47 changes: 47 additions & 0 deletions tests/test_resources.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
import os
from unittest import mock

from pcmdi_metrics import resources


def test_conda_env(tmpdir):
conda_prefix = os.path.join(tmpdir, "conda")

pmp_share_path = os.path.join(conda_prefix, "share", "pmp")

os.makedirs(pmp_share_path)

with mock.patch.dict(os.environ, {"CONDA_PREFIX": conda_prefix}):
path = resources.resource_path()

assert path == os.path.join(tmpdir, "conda", "share", "pmp")


@mock.patch("os.getcwd")
@mock.patch("pkg_resources.resource_filename")
def test_conda_env_no_exist(resource_filename, getcwd, tmpdir):
# Fix issue when tests are ran against an installed package
resource_filename.side_effect = Exception()

conda_prefix = os.path.join(tmpdir, "conda")

getcwd_path = os.path.join(tmpdir, "share", "pmp")

os.makedirs(getcwd_path)

getcwd.return_value = tmpdir

with mock.patch.dict(os.environ, {"CONDA_PREFIX": conda_prefix}):
path = resources.resource_path()

assert path == os.path.join(tmpdir, "share", "pmp")


@mock.patch("pkg_resources.Requirement.parse")
@mock.patch("pkg_resources.resource_filename")
def test_pkg_resources(resource_filename, parse, tmpdir):
resource_filename.return_value = str(tmpdir)

path = resources.resource_path()

assert path == str(tmpdir)