Skip to content

Commit b11d661

Browse files
Completes UNB-2896 - Add consistency check between module versions used in the Jupyter notebook and the ones specified in the requirements file
1 parent f41d0c7 commit b11d661

File tree

2 files changed

+59
-1
lines changed

2 files changed

+59
-1
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,10 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.
77

88
## [Unreleased]
99

10+
### Added
11+
12+
* Warnings if the dependencies from the `requirement_txt_file` and current environment are inconsistent.
13+
1014
### Changed
1115

1216
* Migrated package name from [openlayer](https://pypi.org/project/openlayer/) to [openlayer](https://pypi.org/project/openlayer/) due to a company name change.

openlayer/__init__.py

Lines changed: 55 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,15 @@
55
import tempfile
66
import traceback
77
import uuid
8+
import warnings
89
from typing import Callable, List, Optional
910

1011
import marshmallow as ma
1112
import pandas as pd
13+
import pkg_resources
1214
from bentoml.saved_bundle import bundler
1315
from bentoml.utils import tempdir
16+
from pkg_resources import DistributionNotFound, VersionConflict
1417

1518
from . import api, exceptions, schemas, utils
1619
from .datasets import Dataset
@@ -536,6 +539,7 @@ def add_model(
536539
raise exceptions.OpenlayerResourceError(
537540
f"File `{requirements_txt_file}` does not exist. \n"
538541
) from None
542+
self._check_dependencies(requirements_txt_file)
539543

540544
# Setup script
541545
if setup_script and not os.path.isfile(os.path.expanduser(setup_script)):
@@ -1208,7 +1212,7 @@ def add_dataframe(
12081212

12091213
@staticmethod
12101214
def _format_error_message(err) -> str:
1211-
"""Formats the error messaeges from Marshmallow"""
1215+
"""Formats the error messages from Marshmallow"""
12121216
error_msg = ""
12131217
for input, msg in err.messages.items():
12141218
if input == "_schema":
@@ -1221,3 +1225,53 @@ def _format_error_message(err) -> str:
12211225
temp_msg = list(msg.values())[0][0].lower()
12221226
error_msg += f"- `{input}` contains items that are {temp_msg} \n"
12231227
return error_msg
1228+
1229+
@staticmethod
1230+
def _check_dependencies(requirements_txt_file: str):
1231+
"""Checks the modules specified in the `requirements_txt_file` against
1232+
the ones installed in the current enviromnent
1233+
"""
1234+
# Read requirements file
1235+
with open(requirements_txt_file) as f:
1236+
lines = f.readlines()
1237+
1238+
# Parse the requirements file
1239+
dependencies = pkg_resources.parse_requirements(lines)
1240+
1241+
for requirement in dependencies:
1242+
requirement = str(requirement)
1243+
try:
1244+
pkg_resources.require(requirement)
1245+
except VersionConflict as err:
1246+
try:
1247+
warnings.warn(
1248+
"There is a version discrepancy between the current "
1249+
f"environment and the dependency `{requirement}`. \n"
1250+
f"`requirements_txt_file` specifies `{err.req}`, but the current "
1251+
f"environment contains `{err.dist}` installed. \n"
1252+
"There might be unexpected results once the model is in the platform. "
1253+
"Use at your own discretion.",
1254+
category=Warning,
1255+
)
1256+
return None
1257+
except AttributeError:
1258+
warnings.warn(
1259+
"There is a version discrepancy between the current "
1260+
f"environment and the dependency `{requirement}`. \n"
1261+
f"`requirements_txt_file` specifies `{requirement}`, but the current "
1262+
f"environment contains an incompatible version installed. \n"
1263+
"There might be unexpected results once the model is in the platform. "
1264+
"Use at your own discretion.",
1265+
category=Warning,
1266+
)
1267+
return None
1268+
except DistributionNotFound as err:
1269+
warnings.warn(
1270+
f"The dependency `{requirement}` specified in the `requirements_txt_file` "
1271+
"is not installed in the current environment. \n"
1272+
"There might be unexpected results once the model is in the platform. "
1273+
"Use at your own discretion.",
1274+
category=Warning,
1275+
)
1276+
return None
1277+
return None

0 commit comments

Comments
 (0)