-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpython_package_folder.py
More file actions
409 lines (367 loc) · 15.5 KB
/
python_package_folder.py
File metadata and controls
409 lines (367 loc) · 15.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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
"""
Main entry point for the python-package-folder package.
This module provides the command-line interface for the package.
It can be invoked via:
- The `python-package-folder` command (after installation)
- `python -m python_package_folder`
- Direct import and call to main()
"""
from __future__ import annotations
import subprocess
import sys
from pathlib import Path
try:
from importlib import resources
except ImportError:
import importlib_resources as resources # type: ignore[no-redef]
from .manager import BuildManager
from .utils import find_project_root, find_source_directory
def resolve_version_via_semantic_release(
project_root: Path,
subfolder_path: Path | None = None,
package_name: str | None = None,
) -> str | None:
"""
Resolve the next version using semantic-release via Node.js script.
Args:
project_root: Root directory of the project
subfolder_path: Optional path to subfolder (relative to project_root) for Workflow 1
package_name: Optional package name for subfolder builds
Returns:
Version string if a release is determined, None if no release or error
"""
# Try to find the script in multiple locations:
# 1. Project root / scripts (for development or when script is in repo)
# 2. Package installation directory / scripts (for installed package)
# - For normal installs: direct file path
# - For zip/pex installs: extract to temporary file using as_file()
# Track temporary file context for cleanup
temp_script_context = None
try:
# First, try project root (development)
dev_script = project_root / "scripts" / "get-next-version.cjs"
if dev_script.exists():
script_path = dev_script
else:
# Try to locate script in installed package using importlib.resources
script_path = None
try:
package = resources.files("python_package_folder")
script_resource = package / "scripts" / "get-next-version.cjs"
if script_resource.is_file():
# Try direct path conversion first (normal file system install)
try:
script_path_candidate = Path(str(script_resource))
if script_path_candidate.exists():
script_path = script_path_candidate
except (TypeError, ValueError):
pass
# If direct path didn't work, try as_file() for zip/pex installs
if script_path is None:
try:
temp_script_context = resources.as_file(script_resource)
script_path = temp_script_context.__enter__()
except (TypeError, ValueError, OSError):
pass
except (ImportError, ModuleNotFoundError, TypeError, AttributeError, OSError):
pass
# Fallback: try relative to package directory
if script_path is None:
package_dir = Path(__file__).parent
fallback_script = package_dir / "scripts" / "get-next-version.cjs"
if fallback_script.exists():
script_path = fallback_script
if not script_path:
return None
# Build command arguments
cmd = ["node", str(script_path), str(project_root)]
if subfolder_path and package_name:
# Workflow 1: subfolder build
rel_path = (
subfolder_path.relative_to(project_root)
if subfolder_path.is_absolute()
else subfolder_path
)
cmd.extend([str(rel_path), package_name])
# Workflow 2: main package (no additional args needed)
result = subprocess.run(
cmd,
capture_output=True,
text=True,
cwd=project_root,
check=False,
)
if result.returncode != 0:
# Log error details for debugging
if result.stderr:
print(
f"Warning: semantic-release version resolution failed: {result.stderr}",
file=sys.stderr,
)
elif result.stdout:
print(
f"Warning: semantic-release version resolution failed: {result.stdout}",
file=sys.stderr,
)
return None
version = result.stdout.strip()
if version and version != "none":
return version
return None
except FileNotFoundError:
# Node.js not found
print(
"Warning: Node.js not found. Cannot resolve version via semantic-release.",
file=sys.stderr,
)
return None
except Exception as e:
# Other errors (e.g., permission issues, script not found)
print(
f"Warning: Error resolving version via semantic-release: {e}",
file=sys.stderr,
)
return None
finally:
# Clean up temporary file if we extracted from zip/pex
# This must be at function level to ensure cleanup even on early return
if temp_script_context is not None:
try:
temp_script_context.__exit__(None, None, None)
except Exception:
pass
def main() -> int:
"""
Main entry point for the build script.
Parses command-line arguments and runs the build process with
external dependency management.
Returns:
Exit code (0 for success, non-zero for errors)
"""
import argparse
parser = argparse.ArgumentParser(
description="Build Python package with external dependency management"
)
parser.add_argument(
"--project-root",
type=Path,
help="Root directory of the project (auto-detected from pyproject.toml if not specified)",
)
parser.add_argument(
"--src-dir",
type=Path,
help="Source directory to build (default: auto-detected from current directory or project_root/src)",
)
parser.add_argument(
"--analyze-only",
action="store_true",
help="Only analyze imports, don't run build",
)
parser.add_argument(
"--build-command",
default="uv build",
help="Command to run for building (default: 'uv build')",
)
parser.add_argument(
"--publish",
choices=["pypi", "testpypi", "azure"],
help="Publish to repository after building (pypi, testpypi, or azure)",
)
parser.add_argument(
"--repository-url",
help="Custom repository URL (required for Azure Artifacts)",
)
parser.add_argument(
"--username",
help="Username for publishing (will prompt if not provided)",
)
parser.add_argument(
"--password",
help="Password/token for publishing (will prompt if not provided)",
)
parser.add_argument(
"--skip-existing",
action="store_true",
help="Skip files that already exist on the repository",
)
parser.add_argument(
"--version",
help="Set a specific version before building (PEP 440 format, e.g., '1.2.3'). Optional: if omitted, version will be resolved via semantic-release when needed.",
)
parser.add_argument(
"--package-name",
help="Package name for subfolder builds (default: derived from source directory name)",
)
parser.add_argument(
"--dependency-group",
dest="dependency_group",
help="Dependency group name from parent pyproject.toml to include in subfolder build",
)
parser.add_argument(
"--no-restore-versioning",
action="store_true",
help="Don't restore dynamic versioning after build (keeps static version)",
)
parser.add_argument(
"--exclude-pattern",
action="append",
dest="exclude_patterns",
help="Additional directory/file patterns to exclude from copying (e.g., '_SS', '__sandbox'). Can be specified multiple times.",
)
args = parser.parse_args()
try:
# Auto-detect project root if not specified
if args.project_root:
project_root = Path(args.project_root).resolve()
else:
project_root = find_project_root()
if project_root is None:
print(
"Error: Could not find project root (pyproject.toml not found).\n"
"Please run from a directory with pyproject.toml or specify --project-root",
file=sys.stderr,
)
return 1
print(f"Auto-detected project root: {project_root}")
# Determine source directory
if args.src_dir:
src_dir = Path(args.src_dir).resolve()
else:
# Auto-detect: use current directory if it has Python files, otherwise use project_root/src
current_dir = Path.cwd()
src_dir = find_source_directory(project_root, current_dir=current_dir)
if src_dir:
print(f"Auto-detected source directory: {src_dir}")
else:
src_dir = project_root / "src"
manager = BuildManager(project_root, src_dir, exclude_patterns=args.exclude_patterns)
if args.analyze_only:
external_deps = manager.prepare_build()
print(f"\nFound {len(external_deps)} external dependencies:")
for dep in external_deps:
print(f" {dep.import_name}: {dep.source_path} -> {dep.target_path}")
manager.cleanup()
return 0
def build_cmd() -> None:
# Run build command from project root to ensure pyproject.toml is found
result = subprocess.run(
args.build_command,
shell=True,
check=False,
cwd=project_root,
)
if result.returncode != 0:
sys.exit(result.returncode)
# Check if building a subfolder (not the main src/)
# A subfolder must be within the project root but not the main src/ directory
is_subfolder = (
src_dir.is_relative_to(project_root)
and src_dir != project_root / "src"
and src_dir != project_root
)
# Resolve version via semantic-release if not provided and needed
resolved_version = args.version
if not resolved_version and not args.analyze_only:
# Version is needed for subfolder builds or when publishing main package
if is_subfolder or args.publish:
print("No --version provided, attempting to resolve via semantic-release...")
if is_subfolder:
# Workflow 1: subfolder build
# src_dir is guaranteed to be relative to project_root due to is_subfolder check
package_name = args.package_name or src_dir.name.replace("_", "-").replace(
" ", "-"
).lower().strip("-")
subfolder_rel_path = src_dir.relative_to(project_root)
resolved_version = resolve_version_via_semantic_release(
project_root, subfolder_rel_path, package_name
)
else:
# Workflow 2: main package
resolved_version = resolve_version_via_semantic_release(project_root)
if resolved_version:
print(f"Resolved version via semantic-release: {resolved_version}")
else:
error_msg = (
"Could not resolve version via semantic-release.\n"
"This could mean:\n"
" - No release is needed (no relevant commits)\n"
" - semantic-release is not installed or configured\n"
" - Node.js is not available\n\n"
"Please either:\n"
" - Install semantic-release: npm install -g semantic-release"
)
if is_subfolder:
error_msg += "\n - Install semantic-release-commit-filter: npm install -g semantic-release-commit-filter"
error_msg += "\n - Or provide --version explicitly"
print(f"Error: {error_msg}", file=sys.stderr)
return 1
# Use resolved version for the rest of the flow
if resolved_version:
args.version = resolved_version
if args.publish:
manager.build_and_publish(
build_cmd,
repository=args.publish,
repository_url=args.repository_url,
username=args.username,
password=args.password,
skip_existing=args.skip_existing,
version=args.version,
restore_versioning=not args.no_restore_versioning,
package_name=args.package_name,
dependency_group=args.dependency_group,
)
else:
# Handle version setting even without publishing
if args.version:
# Check if subfolder build
if is_subfolder:
from .subfolder_build import SubfolderBuildConfig
package_name = args.package_name or src_dir.name.replace("_", "-").replace(
" ", "-"
).lower().strip("-")
subfolder_config = SubfolderBuildConfig(
project_root=project_root,
src_dir=src_dir,
package_name=package_name,
version=args.version,
dependency_group=args.dependency_group,
)
try:
subfolder_config.create_temp_pyproject()
manager.run_build(build_cmd)
if not args.no_restore_versioning:
subfolder_config.restore()
print("Restored original pyproject.toml")
except Exception as e:
print(f"Error managing subfolder build: {e}", file=sys.stderr)
if subfolder_config:
subfolder_config.restore()
raise
else:
from .version import VersionManager
version_manager = VersionManager(project_root)
original_version = version_manager.get_current_version()
try:
print(f"Setting version to {args.version}...")
version_manager.set_version(args.version)
manager.run_build(build_cmd)
if not args.no_restore_versioning:
if original_version:
version_manager.set_version(original_version)
else:
version_manager.restore_dynamic_versioning()
print("Restored versioning configuration")
except Exception as e:
print(f"Error managing version: {e}", file=sys.stderr)
raise
else:
manager.run_build(build_cmd)
return 0
except Exception as e:
print(f"Error: {e}", file=sys.stderr)
import traceback
traceback.print_exc()
return 1
if __name__ == "__main__":
sys.exit(main())