Skip to content

Commit

Permalink
remove last usage of pkg_resources
Browse files Browse the repository at this point in the history
  • Loading branch information
mr-c committed Nov 23, 2023
1 parent 90ce873 commit 918d77b
Show file tree
Hide file tree
Showing 13 changed files with 33 additions and 22 deletions.
2 changes: 1 addition & 1 deletion cwltool/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@
import argcomplete
import coloredlogs
import ruamel.yaml
from importlib_resources import files
from ruamel.yaml.comments import CommentedMap, CommentedSeq
from ruamel.yaml.main import YAML
from schema_salad.exceptions import ValidationException
Expand Down Expand Up @@ -108,6 +107,7 @@
CWLOutputType,
HasReqsHints,
adjustDirObjs,
files,
normalizeFilesDirs,
processes_to_kill,
trim_listing,
Expand Down
2 changes: 1 addition & 1 deletion cwltool/process.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@
)

from cwl_utils import expression
from importlib_resources import files
from mypy_extensions import mypyc_attr
from rdflib import Graph
from ruamel.yaml.comments import CommentedMap, CommentedSeq
Expand Down Expand Up @@ -70,6 +69,7 @@
aslist,
cmp_like_py2,
ensure_writable,
files,
get_listing,
normalizeFilesDirs,
random_outdir,
Expand Down
15 changes: 11 additions & 4 deletions cwltool/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,12 +53,19 @@
from schema_salad.exceptions import ValidationException
from schema_salad.ref_resolver import Loader

if sys.version_info >= (3, 9):
from importlib.resources import as_file, files
else:
from importlib_resources import as_file, files

if TYPE_CHECKING:
from .command_line_tool import CallbackJob, ExpressionJob
from .job import CommandLineJob, JobBase
from .stdfsaccess import StdFsAccess
from .workflow_job import WorkflowJob

__all__ = ["files", "as_file"]

__random_outdir: Optional[str] = None

CONTENT_LIMIT = 64 * 1024
Expand Down Expand Up @@ -368,8 +375,8 @@ def add_writable_flag(p: str) -> None:
if os.path.isdir(path):
if include_root:
add_writable_flag(path)
for root, dirs, files in os.walk(path):
for name in files:
for root, dirs, files_ in os.walk(path):
for name in files_:
add_writable_flag(os.path.join(root, name))
for name in dirs:
add_writable_flag(os.path.join(root, name))
Expand All @@ -380,8 +387,8 @@ def add_writable_flag(p: str) -> None:
def ensure_non_writable(path: str) -> None:
"""Attempt to change permissions to ensure that a path is not writable."""
if os.path.isdir(path):
for root, dirs, files in os.walk(path):
for name in files:
for root, dirs, files_ in os.walk(path):
for name in files_:
j = os.path.join(root, name)
st = os.stat(j)
mode = stat.S_IMODE(st.st_mode)
Expand Down
2 changes: 1 addition & 1 deletion cwltool/validate_js.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
from cwl_utils.errors import SubstitutionError
from cwl_utils.expression import scanner as scan_expression
from cwl_utils.sandboxjs import code_fragment_to_js, exec_js_process
from importlib_resources import files
from ruamel.yaml.comments import CommentedMap, CommentedSeq
from schema_salad.avro.schema import (
ArraySchema,
Expand All @@ -33,6 +32,7 @@

from .errors import WorkflowException
from .loghandler import _logger
from .utils import files


def is_expression(tool: Any, schema: Optional[Schema]) -> bool:
Expand Down
3 changes: 1 addition & 2 deletions docs/requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,5 @@ sphinx >= 2.2
sphinx-rtd-theme==1.3.0
sphinx-autoapi
sphinx-autodoc-typehints
typed_ast;python_version<'3.8'
sphinxcontrib-autoprogram
importlib_resources
importlib_resources;python_version<'3.9'
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ requires = [
"mypy==1.7.1", # also update mypy-requirements.txt
"types-requests",
"types-psutil",
"importlib_resources>=1.4", # equivalent to Python 3.9
"importlib_resources>=1.4;python_version<'3.9'",
"ruamel.yaml>=0.16.0,<0.18",
"schema-salad>=8.4.20230426093816,<9",
"cwl-utils>=0.22,!=0.30",
Expand Down
2 changes: 1 addition & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ schema-salad>=8.4.20230426093816,<9
prov==1.5.1
mypy-extensions
psutil>=5.6.6
importlib_resources>=1.4 # equivalent to Python 3.9
importlib_resources>=1.4;python_version<'3.9'
coloredlogs
pydot>=1.4.1
argcomplete>=1.12.0
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@
"prov == 1.5.1",
"mypy-extensions",
"psutil >= 5.6.6",
"importlib_resources>=1.4",
"importlib_resources>=1.4;python_version<'3.9'",
"coloredlogs",
"pydot >= 1.4.1",
"argcomplete",
Expand Down
4 changes: 2 additions & 2 deletions tests/test_cuda.py
Original file line number Diff line number Diff line change
Expand Up @@ -284,7 +284,7 @@ def test_cuda_job_setup_check_err_wrong_type_cuda_version(


def test_cuda_eval_resource_range() -> None:
with open(get_data("cwltool/extensions-v1.1.yml")) as res:
with open(get_data("extensions-v1.1.yml")) as res:
use_custom_schema("v1.2", "http://commonwl.org/cwltool", res.read())

joborder = {} # type: CWLObjectType
Expand All @@ -301,7 +301,7 @@ def test_cuda_eval_resource_range() -> None:


def test_cuda_eval_resource_max() -> None:
with open(get_data("cwltool/extensions-v1.1.yml")) as res:
with open(get_data("extensions-v1.1.yml")) as res:
use_custom_schema("v1.2", "http://commonwl.org/cwltool", res.read())

joborder = {} # type: CWLObjectType
Expand Down
2 changes: 1 addition & 1 deletion tests/test_load_tool.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ def test_load_graph_fragment_from_packed() -> None:
loadingContext = LoadingContext()
uri = Path(get_data("tests/wf/packed-with-loadlisting.cwl")).as_uri() + "#main"
try:
with open(get_data("cwltool/extensions.yml")) as res:
with open(get_data("extensions.yml")) as res:
use_custom_schema("v1.0", "http://commonwl.org/cwltool", res.read())

# The updater transforms LoadListingRequirement from an
Expand Down
2 changes: 1 addition & 1 deletion tests/test_mpi.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
from typing import Any, Generator, List, MutableMapping, Optional, Tuple

import pytest
from importlib_resources import files
from ruamel.yaml.comments import CommentedMap, CommentedSeq
from schema_salad.avro.schema import Names
from schema_salad.utils import yaml_no_ts
Expand All @@ -19,6 +18,7 @@
from cwltool.context import LoadingContext, RuntimeContext
from cwltool.main import main
from cwltool.mpi import MpiConfig, MPIRequirementName
from cwltool.utils import files

from .util import get_data, working_directory

Expand Down
16 changes: 11 additions & 5 deletions tests/util.py
Original file line number Diff line number Diff line change
@@ -1,19 +1,22 @@
"""Test functions."""
import atexit
import contextlib
import io
import json
import os
import shutil
import subprocess
import sys
from contextlib import ExitStack
from pathlib import Path
from typing import Dict, Generator, List, Mapping, Optional, Tuple, Union

import pytest
from pkg_resources import Requirement, ResolutionError, resource_filename

from cwltool.env_to_stdout import deserialize_env
from cwltool.main import main
from cwltool.singularity import is_version_2_6, is_version_3_or_newer
from cwltool.utils import as_file, files


def force_default_container(default_container_id: str, _: str) -> str:
Expand All @@ -25,12 +28,15 @@ def get_data(filename: str) -> str:
filename = os.path.normpath(filename)
filepath = None
try:
filepath = resource_filename(Requirement.parse("cwltool"), filename)
except ResolutionError:
file_manager = ExitStack()
atexit.register(file_manager.close)
traversable = files("cwltool") / filename
filepath = file_manager.enter_context(as_file(traversable))
except ModuleNotFoundError:
pass
if not filepath or not os.path.isfile(filepath):
filepath = os.path.join(os.path.dirname(__file__), os.pardir, filename)
return str(Path(filepath).resolve())
filepath = Path(os.path.dirname(__file__)) / ".." / filename
return str(filepath.resolve())


needs_docker = pytest.mark.skipif(
Expand Down
1 change: 0 additions & 1 deletion tox.ini
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,6 @@ deps =
py3{8,9,10,11,12}-{unit,mypy}: -rtest-requirements.txt
py3{8,9,10,11,12}-lint: -rlint-requirements.txt
py3{8,9,10,11,12}-bandit: bandit
py3{8,9,10,11,12}-bandit: importlib_metadata != 4.8.0
py3{8,9,10,11,12}-mypy: -rmypy-requirements.txt
py312-pydocstyle: pydocstyle
py312-pydocstyle: diff-cover
Expand Down

0 comments on commit 918d77b

Please sign in to comment.