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

Replace sys.exit with CondaBuildUserError exceptions #5255

Draft
wants to merge 33 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
33 commits
Select commit Hold shift + click to select a range
1f7bf92
Add test for copy_readme()
beeankha Mar 25, 2024
df1ec1f
Update test_filter_files
kenodegard Mar 27, 2024
1889cd5
Add test_wsl_unsupported
kenodegard Mar 28, 2024
7160322
Add news file
beeankha Apr 3, 2024
0bb2264
Replace sys.exit in handle_anaconda_upload
kenodegard Apr 3, 2024
36b2d2e
Raise CondaBuildUserError in check_external
kenodegard Apr 4, 2024
e39d11f
Format strings
kenodegard Apr 4, 2024
32047d4
Fix check_dist_info_version
kenodegard Apr 4, 2024
29f73fd
Fix inspect tests
kenodegard Apr 4, 2024
bae28a8
Fix SystemExit tests
kenodegard Apr 4, 2024
d45ed53
Remove sys.exit from find_lib
kenodegard Apr 4, 2024
869364b
Replace sys.exit in osx_ch_link
kenodegard May 1, 2024
81ad52d
Update post.py
kenodegard May 1, 2024
ae717ce
Deprecate exception_on_error
kenodegard May 1, 2024
7eed5a7
Remove sys.exit from check_symlinks
kenodegard May 1, 2024
c6e50da
Remove ImportError for PyYAML since it is an explicit dependency
kenodegard May 1, 2024
9537647
Fix test
kenodegard May 2, 2024
7b3ba47
Remove sys.exit from select_lines
kenodegard May 2, 2024
359f8ae
Remove sys.exit from sanitize/_git_clean
kenodegard May 2, 2024
f2dc2f7
Use PrefixData instead of custom get_installed_packages
kenodegard May 2, 2024
75cca21
Jinaj2 is always installed
kenodegard May 2, 2024
5b0fa11
Replace sys.exit in _get_contents
kenodegard May 2, 2024
5c65084
Move test back
kenodegard Jul 11, 2024
5922c6e
Undo
kenodegard Jul 11, 2024
8141971
Revert _DIR_PATHS
kenodegard Jul 23, 2024
e902ad2
Revert
kenodegard Jul 23, 2024
76f1cd7
Delete 5255-remove-sys.exit-calls
kenodegard Jul 23, 2024
9c9b50b
nullcontext
kenodegard Jul 23, 2024
28df04b
Combine TYPE_CHECKING & remove duplicate
kenodegard Jul 23, 2024
aa8e895
Remove unused import
kenodegard Jul 23, 2024
9599a0a
Bump deprecation versions
kenodegard Jul 23, 2024
35c0f8a
Add `conda.testing` fixtures
kenodegard Jul 25, 2024
d6f0312
Remove outdated test
kenodegard Jul 25, 2024
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
14 changes: 9 additions & 5 deletions conda_build/build.py
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,9 @@
set_language_env_vars,
)

if TYPE_CHECKING:
from typing import Iterable

if on_win:
from . import windows

Expand Down Expand Up @@ -3472,10 +3475,11 @@ def test(
env["CONDA_BUILD_STATE"] = "TEST"

if config.test_run_post:
from .utils import get_installed_packages

installed = get_installed_packages(metadata.config.test_prefix)
files = installed[metadata.meta["package"]["name"]]["files"]
files = (
PrefixData(metadata.config.test_prefix)
.get(metadata.meta["package"]["name"])
.files
)
replacements = get_all_replacements(metadata.config)
try_download(metadata, False, True)
create_info_files(metadata, replacements, files, metadata.config.test_prefix)
Expand Down Expand Up @@ -3578,7 +3582,7 @@ def tests_failed(
_delegated_update_index(
os.path.dirname(os.path.dirname(pkg)), verbose=config.debug, threads=1
)
raise CondaBuildUserError("TESTS FAILED: " + os.path.basename(pkg))
raise CondaBuildUserError(f"TESTS FAILED: {os.path.basename(pkg)}")


@deprecated(
Expand Down
69 changes: 19 additions & 50 deletions conda_build/metadata.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,11 @@
from os.path import isdir, isfile, join
from typing import TYPE_CHECKING, NamedTuple, overload

import jinja2
import yaml
from bs4 import UnicodeDammit
from conda.base.context import locate_prefix_by_name
from conda.core.prefix_data import PrefixData
from conda.gateways.disk.read import compute_sum
from conda.models.match_spec import MatchSpec
from frozendict import deepfreeze
Expand All @@ -31,7 +33,6 @@
DependencyNeedsBuildingError,
RecipeError,
UnableToParse,
UnableToParseMissingJinja2,
)
from .features import feature_list
from .license_family import ensure_valid_license_family
Expand All @@ -40,7 +41,6 @@
ensure_list,
expand_globs,
find_recipe,
get_installed_packages,
insert_variant_versions,
on_win,
)
Expand All @@ -61,14 +61,6 @@
OutputDict = dict[str, Any]
OutputTuple = tuple[OutputDict, "MetaData"]

try:
import yaml
except ImportError:
sys.exit(
"Error: could not import yaml (required to read meta.yaml "
"files of conda recipes)"
)

try:
Loader = yaml.CLoader
except AttributeError:
Expand Down Expand Up @@ -360,13 +352,6 @@ def yamlize(data):
try:
return yaml.load(data, Loader=StringifyNumbersLoader)
except yaml.error.YAMLError as e:
if "{{" in data:
try:
import jinja2

jinja2 # Avoid pyflakes failure: 'jinja2' imported but unused
except ImportError:
raise UnableToParseMissingJinja2(original=e)
print("Problematic recipe:", file=sys.stderr)
print(data, file=sys.stderr)
raise UnableToParse(original=e)
Expand Down Expand Up @@ -739,12 +724,7 @@ def _git_clean(source_meta):
If more than one field is used to specified, exit
and complain.
"""
git_rev_tags_old = ("git_branch", "git_tag")
git_rev = "git_rev"

git_rev_tags = (git_rev,) + git_rev_tags_old
has_rev_tags = tuple(bool(source_meta.get(tag, "")) for tag in git_rev_tags)

keys = [key for key in (git_rev, "git_branch", "git_tag") if key in source_meta]
if not keys:
# git_branch, git_tag, nor git_rev specified, return as-is
Expand All @@ -754,14 +734,7 @@ def _git_clean(source_meta):

# make a copy of the input so we have no side-effects
ret_meta = source_meta.copy()
# loop over the old versions
for key, has in zip(git_rev_tags[1:], has_rev_tags[1:]):
# update if needed
if has:
ret_meta[git_rev_tags[0]] = ret_meta[key]
# and remove
ret_meta.pop(key, None)

ret_meta[git_rev] = ret_meta.pop(keys[0])
return ret_meta


Expand Down Expand Up @@ -868,17 +841,24 @@ def _get_env_path(
)


def _get_dependencies_from_environment(env_name_or_path):
path = _get_env_path(env_name_or_path)
def _get_dependencies_from_environment(
env_name_or_path: str | os.PathLike | Path,
) -> dict[str, dict[str, list[str]]]:
# construct build requirements that replicate the given bootstrap environment
# and concatenate them to the build requirements from the recipe
bootstrap_metadata = get_installed_packages(path)
bootstrap_requirements = []
for package, data in bootstrap_metadata.items():
bootstrap_requirements.append(
"{} {} {}".format(package, data["version"], data["build"])
)
return {"requirements": {"build": bootstrap_requirements}}
prefix = (
env_name_or_path
if isdir(env_name_or_path)
else locate_prefix_by_name(env_name_or_path)
)
return {
"requirements": {
"build": [
f"{prec.name} {prec.version} {prec.build}"
for prec in PrefixData(prefix).iter_records()
]
}
}


def _toposort_outputs(output_tuples: list[OutputTuple]) -> list[OutputTuple]:
Expand Down Expand Up @@ -1925,17 +1905,6 @@ def _get_contents(
permit_undefined_jinja: If True, *any* use of undefined jinja variables will
evaluate to an emtpy string, without emitting an error.
"""
try:
import jinja2
except ImportError:
print("There was an error importing jinja2.", file=sys.stderr)
print(
"Please run `conda install jinja2` to enable jinja template support",
file=sys.stderr,
) # noqa
with open(self.meta_path) as fd:
return fd.read()

from .jinja_context import (
FilteredLoader,
UndefinedNeverFail,
Expand Down
Loading
Loading