Skip to content

Commit ab5847a

Browse files
authored
Merge pull request #17 from coherent-oss/feature/cli
Add a CLI
2 parents 0f94da1 + 564c5a6 commit ab5847a

File tree

1 file changed

+55
-0
lines changed

1 file changed

+55
-0
lines changed

__main__.py

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
"""
2+
CLI for coherent.deps: emits dependencies for Python files given as globs.
3+
4+
Usage:
5+
pipx run coherent.deps [options] <glob> [<glob> ...]
6+
py -m coherent.deps [options] <glob> [<glob> ...]
7+
8+
For each file matching the globs, parses dependencies and emits them in the requested format.
9+
"""
10+
11+
import glob
12+
import pathlib
13+
14+
from jaraco.ui.main import main
15+
from more_itertools import consume, flatten
16+
17+
from . import imports, pypi
18+
19+
20+
def emit_plain(deps):
21+
yield from deps
22+
23+
24+
def emit_toml(deps):
25+
yield "[project]\nrequires = ["
26+
yield from map(lambda d: f' "{d}",', deps)
27+
yield "]"
28+
29+
30+
def emit_inline(deps):
31+
yield """# ///\n# requires-python = ">=3.8"\n# dependencies = ["""
32+
yield from map(lambda d: f'# "{d}",', deps)
33+
yield "# ]\n# ///"
34+
35+
36+
def emit_python(deps):
37+
yield """__requires__ = ["""
38+
yield from map(lambda d: f' "{d}",', deps)
39+
yield "]"
40+
41+
42+
def parse_glob(spec: str):
43+
return map(pathlib.Path, glob.glob(spec))
44+
45+
46+
@main
47+
def main(
48+
globs: list[str],
49+
format: str = 'plain',
50+
):
51+
files = flatten(map(parse_glob, globs))
52+
imps = flatten(map(imports.get_module_imports, files))
53+
deps = (pypi.distribution_for(imp) for imp in imps if not imp.excluded())
54+
lines = globals()[f'emit_{format}'](deps)
55+
consume(map(print, lines))

0 commit comments

Comments
 (0)