Skip to content

Commit 876ce39

Browse files
committed
Replace uses of deprecated pkg_resources
Follows https://importlib-resources.readthedocs.io/en/latest/migration.html to avoid use of `pkg_resources`, which has been deprecated.
1 parent 1269b52 commit 876ce39

File tree

2 files changed

+21
-13
lines changed

2 files changed

+21
-13
lines changed

src/cbmc_viewer/report.py

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
import os
88
import shutil
99

10-
import pkg_resources
10+
from importlib import resources as importlib_resources
1111

1212
from cbmc_viewer import markup_code
1313
from cbmc_viewer import markup_summary
@@ -37,10 +37,11 @@ def report(config, sources, symbols, results, coverage, traces, properties,
3737
trace_dir = os.path.join(report_dir, markup_trace.TRACES)
3838

3939
os.makedirs(report_dir, exist_ok=True)
40-
shutil.copy(pkg_resources.resource_filename(PACKAGE, VIEWER_CSS),
41-
report_dir)
42-
shutil.copy(pkg_resources.resource_filename(PACKAGE, VIEWER_JS),
43-
report_dir)
40+
41+
with importlib_resources.path(PACKAGE, VIEWER_CSS) as css_path:
42+
shutil.copy(css_path, report_dir)
43+
with importlib_resources.path(PACKAGE, VIEWER_JS) as js_path:
44+
shutil.copy(js_path, report_dir)
4445

4546
progress("Preparing report summary")
4647
markup_summary.Summary(

src/cbmc_viewer/templates.py

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
import jinja2
77
from jinja2 import select_autoescape
88

9-
import pkg_resources
9+
from importlib import resources as importlib_resources
1010

1111
PACKAGE = 'cbmc_viewer'
1212
TEMPLATES = 'templates'
@@ -24,13 +24,20 @@ def env():
2424
global ENV
2525

2626
if ENV is None:
27-
template_dir = pkg_resources.resource_filename(PACKAGE, TEMPLATES)
28-
ENV = jinja2.Environment(
29-
loader=jinja2.FileSystemLoader(template_dir),
30-
autoescape=select_autoescape(
31-
enabled_extensions=('html'),
32-
default_for_string=True)
33-
)
27+
try:
28+
ctxmgr =
29+
importlib_resources.as_file(importlib_resources.files(PACKAGE) /
30+
TEMPLATES)
31+
except AttributeError:
32+
# Python 3.7 and 3.8
33+
ctxmgr = importlib_resources.path(PACKAGE, TEMPLATES)
34+
with ctxmgr as templates_path:
35+
ENV = jinja2.Environment(
36+
loader=jinja2.FileSystemLoader(str(templates_path)),
37+
autoescape=select_autoescape(
38+
enabled_extensions=('html'),
39+
default_for_string=True)
40+
)
3441
return ENV
3542

3643
def render_summary(summary):

0 commit comments

Comments
 (0)