Repository navigation
Expand file tree
/
Copy pathcli.py
More file actions
189 lines (165 loc) · 5.5 KB
/
Copy pathcli.py
File metadata and controls
189 lines (165 loc) · 5.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
"""Command-line entrypoint for the `dlthub-init` CLI."""
from __future__ import annotations
import argparse
import sys
from pathlib import Path
from . import strings, telemetry
from .collisions import Flags
from .config import DEFAULT_SCAFFOLD, DISTRIBUTION_NAME
from .display import (
console,
err_console,
print_collision,
print_header,
print_next_steps,
print_summary,
substep,
substep_detail,
)
from .errors import CollisionError, UvError, WorkspaceError
from .prompts import confirm, stdin_is_interactive
from .scaffold import apply_scaffold, resolve_target
from .skills import install_skills, skills_source
from .uv import execute_uv_install, find_uv, run_uv_sync
def _ensure_utf8_io_on_windows() -> None:
if sys.platform != "win32":
return
for stream in (sys.stdout, sys.stderr):
reconfigure = getattr(stream, "reconfigure", None)
if reconfigure is None:
continue
try:
reconfigure(encoding="utf-8")
except (OSError, ValueError):
pass
def build_parser() -> argparse.ArgumentParser:
parser = argparse.ArgumentParser(
prog=DISTRIBUTION_NAME,
description="Scaffold a dltHub workspace into a new or existing directory.",
)
parser.add_argument(
"project_dir",
nargs="?",
default=None,
help="Directory to initialize. Defaults to the current directory.",
)
parser.add_argument(
"--yes",
"-y",
action="store_true",
help=argparse.SUPPRESS,
)
parser.add_argument(
"--verbose",
"-v",
action="store_true",
help="Stream output from uv.",
)
parser.add_argument(
"--no-sync",
action="store_true",
help="Scaffold files only; do not create a virtual environment or install dependencies.",
)
parser.add_argument(
"--force",
action="store_true",
help="Overwrite existing generated files (never secrets).",
)
parser.add_argument(
"--merge",
action="store_true",
help="Append missing entries to an existing .gitignore instead of skipping it.",
)
parser.add_argument(
"--no-pyproject",
action="store_true",
help="Skip pyproject.toml.",
)
parser.add_argument(
"--no-gitignore",
action="store_true",
help="Skip .gitignore.",
)
parser.add_argument(
"--no-telemetry",
action="store_true",
help="Disable usage telemetry for this run.",
)
return parser
def main(argv: list[str] | None = None) -> int:
_ensure_utf8_io_on_windows()
args = build_parser().parse_args(argv)
telemetry.init(no_telemetry=args.no_telemetry)
try:
run(args)
except KeyboardInterrupt:
telemetry.track_run("cancelled")
console.print(strings.MSG_CANCELLED)
return 130
except CollisionError as exc:
telemetry.track_run("failed", error_code=type(exc).__name__)
print_collision(exc.conflicts)
return 2
except WorkspaceError as exc:
telemetry.track_run("failed", error_code=type(exc).__name__)
console.print(strings.MSG_ERROR_PREFIX.format(message=exc))
return 1
except Exception as exc:
telemetry.track_run("failed", error_code=type(exc).__name__)
console.print(strings.MSG_UNEXPECTED_ERROR.format(message=exc))
if args.verbose:
console.print_exception()
else:
console.print(strings.MSG_UNEXPECTED_ERROR_HINT)
return 1
telemetry.track_run("success")
return 0
def run(args: argparse.Namespace) -> None:
telemetry.show_first_run_notice()
if args.yes:
err_console.print(strings.MSG_TESTING_SHORTCUT_NOTE)
verbose = args.verbose
scaffold = DEFAULT_SCAFFOLD
flags = Flags(
force=args.force,
merge=args.merge,
no_pyproject=args.no_pyproject,
no_gitignore=args.no_gitignore,
)
project_dir = resolve_target(args.project_dir)
print_header(project_dir)
plan = apply_scaffold(project_dir, scaffold=scaffold, flags=flags)
installed_skills = install_skills(project_dir)
print_summary(plan)
if installed_skills:
substep_detail(strings.MSG_SKILLS_INSTALLED.format(count=len(installed_skills)))
synced = _maybe_sync(project_dir, args, verbose=verbose)
print_next_steps(
project_dir,
synced=synced,
uv_installed=find_uv() is not None,
interactive=stdin_is_interactive(),
skills_available=skills_source() is not None,
)
def _maybe_sync(project_dir: Path, args: argparse.Namespace, *, verbose: bool) -> bool:
if args.no_sync:
console.print(strings.MSG_SKIPPED_SYNC)
return False
uv_executable = find_uv()
if uv_executable is None and not (args.yes or confirm(strings.PROMPT_INSTALL_UV, default=True)):
console.print(strings.MSG_SKIPPED_SYNC)
return
3FE0
False
if not args.yes and not confirm(strings.PROMPT_CREATE_VENV, default=True):
console.print(strings.MSG_SKIPPED_SYNC)
return False
try:
if uv_executable is None:
uv_executable = execute_uv_install(verbose=verbose)
with substep(strings.MSG_INSTALLING_DEPS, strings.MSG_INSTALLED_DEPS, verbose=verbose):
run_uv_sync(uv_executable, project_dir, verbose=verbose)
except UvError as exc:
console.print(strings.MSG_SYNC_FAILED.format(message=exc))
return False
return True
if __name__ == "__main__":
sys.exit(main())