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

feat: script improvements #13

Merged
merged 1 commit into from
Oct 31, 2024
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
4 changes: 4 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,10 @@ dependencies = [
[project.scripts]
lint-dependency-groups = "dependency_groups._lint_dependency_groups:main"
pip-install-dependency-groups = "dependency_groups._pip_wrapper:main"
dependency-groups = "dependency_groups.__main__:main"

[project.optional-dependencies]
cli = ["tomli; python_version<'3.11'"]

[project.urls]
source = "https://github.com/sirosen/dependency-groups"
Expand Down
93 changes: 56 additions & 37 deletions src/dependency_groups/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,43 +9,62 @@
except ImportError:
try:
import tomli as tomllib # type: ignore[no-redef]
except ImportError:
tomllib = None # type: ignore[assignment]
except ModuleNotFoundError:
print(
"Usage error: dependency-groups CLI requires tomli or Python 3.11+",
file=sys.stderr,
)
raise SystemExit(2) from None
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just curious, does explicitly raising a SystemExit behave significantly differently from sys.exit? (Will it capture the outer error in __cause__ by default?)

If I need to adjust my habits around this, it's a good opportunity to learn.

Copy link
Contributor Author

@henryiii henryiii Oct 31, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It doesn't require an import, is microscopically faster, and is a bit more explicitly obvious as a control flow statement than a NoReturn function. If someone does explicitly catch it, like in tests, you can inspect the __cause__/__context__/__suppress_context__, as well, but it doesn't show the cause with the default exception hook.

See https://www.youtube.com/watch?v=ZbeSPc5wL0g.

Edit: ignore what I put here originally, exception suppression works as you'd expect if you inspect it, I just forgot how it worked for a minute.


if not tomllib:
print(
"Usage error: dependency-groups CLI requires tomli or Python 3.11+",
file=sys.stderr,
)
sys.exit(2)

parser = argparse.ArgumentParser(
description=(
"A dependency-groups CLI. Prints out a resolved group, newline-delimited."
def main() -> None:
parser = argparse.ArgumentParser(
description=(
"A dependency-groups CLI. Prints out a resolved group, newline-delimited."
)
)
parser.add_argument(
"GROUP_NAME", nargs="*", help="The dependency group(s) to resolve."
)
parser.add_argument(
"-f",
"--pyproject-file",
default="pyproject.toml",
help="The pyproject.toml file. Defaults to trying in the current directory.",
)
)
parser.add_argument("GROUP_NAME", help="The dependency group to resolve.")
parser.add_argument(
"-f",
"--pyproject-file",
default="pyproject.toml",
help="The pyproject.toml file. Defaults to trying in the current directory.",
)
parser.add_argument(
"-o",
"--output",
help="An output file. Defaults to stdout.",
)
args = parser.parse_args()

with open(args.pyproject_file, "rb") as fp:
pyproject = tomllib.load(fp)

dependency_groups_raw = pyproject.get("dependency-groups", {})
content = "\n".join(resolve(dependency_groups_raw, args.GROUP_NAME))

if args.output is None or args.output == "-":
print(content)
else:
with open(args.output, "w") as fp:
print(content, file=fp)
parser.add_argument(
"-o",
"--output",
help="An output file. Defaults to stdout.",
)
parser.add_argument(
"-l",
"--list",
action="store_true",
help="List the available dependency groups",
)
args = parser.parse_args()

with open(args.pyproject_file, "rb") as fp:
pyproject = tomllib.load(fp)

dependency_groups_raw = pyproject.get("dependency-groups", {})

if args.list:
print(*dependency_groups_raw.keys())
return
if not args.GROUP_NAME:
print("A GROUP_NAME is required", file=sys.stderr)
raise SystemExit(3)

content = "\n".join(resolve(dependency_groups_raw, *args.GROUP_NAME))

if args.output is None or args.output == "-":
print(content)
else:
with open(args.output, "w") as fp:
print(content, file=fp)


if __name__ == "__main__":
main()
Loading