55import shutil
66import tarfile
77import tempfile
8+ from pathlib import Path
89from typing import Mapping
910
1011from pdm .cli .commands .base import BaseCommand
@@ -32,6 +33,7 @@ def do_build(
3233 wheel : bool = True ,
3334 dest : str = "dist" ,
3435 clean : bool = True ,
36+ verbose : int = 0 ,
3537 config_settings : Mapping [str , str ] | None = None ,
3638 hooks : HookManager | None = None ,
3739 ) -> None :
@@ -57,29 +59,56 @@ def do_build(
5759 hooks .try_emit ("pre_build" , dest = dest , config_settings = config_settings )
5860 artifacts : list [str ] = []
5961 with project .core .ui .logging ("build" ):
60- if sdist :
61- project .core .ui .echo ("[info]Building sdist..." )
62- sdist_file = SdistBuilder (project .root , project .environment ).build (dest )
63- project .core .ui .echo (f"[info]Built sdist at { sdist_file } " )
64- artifacts .append (sdist_file )
65- if wheel :
62+ if not project .config ["use_uv" ]:
6663 if sdist :
67- project .core .ui .echo ("[info]Building wheel from sdist..." )
68- sdist_out = tempfile .mkdtemp (prefix = "pdm-build-via-sdist-" )
69- try :
70- with tarfile .open (sdist_file , "r:gz" ) as tf :
71- tf .extractall (sdist_out )
72- sdist_name = os .path .basename (sdist_file )[: - len (".tar.gz" )]
73- whl = WheelBuilder (os .path .join (sdist_out , sdist_name ), project .environment ).build (dest )
74- project .core .ui .echo (f"[info]Built wheel at { whl } " )
75- artifacts .append (whl )
76- finally :
77- shutil .rmtree (sdist_out , ignore_errors = True )
78- else :
79- project .core .ui .echo ("[info]Building wheel..." )
80- whl = WheelBuilder (project .root , project .environment ).build (dest )
81- project .core .ui .echo (f"[info]Built wheel at { whl } " )
82- artifacts .append (whl )
64+ project .core .ui .echo ("[info]Building sdist..." )
65+ sdist_file = SdistBuilder (project .root , project .environment ).build (dest )
66+ project .core .ui .echo (f"[info]Built sdist at { sdist_file } " )
67+ artifacts .append (sdist_file )
68+ if wheel :
69+ if sdist :
70+ project .core .ui .echo ("[info]Building wheel from sdist..." )
71+ sdist_out = tempfile .mkdtemp (prefix = "pdm-build-via-sdist-" )
72+ try :
73+ with tarfile .open (sdist_file , "r:gz" ) as tf :
74+ tf .extractall (sdist_out )
75+ sdist_name = os .path .basename (sdist_file )[: - len (".tar.gz" )]
76+ whl = WheelBuilder (os .path .join (sdist_out , sdist_name ), project .environment ).build (dest )
77+ project .core .ui .echo (f"[info]Built wheel at { whl } " )
78+ artifacts .append (whl )
79+ finally :
80+ shutil .rmtree (sdist_out , ignore_errors = True )
81+ else :
82+ project .core .ui .echo ("[info]Building wheel..." )
83+ whl = WheelBuilder (project .root , project .environment ).build (dest )
84+ project .core .ui .echo (f"[info]Built wheel at { whl } " )
85+ artifacts .append (whl )
86+ else :
87+ import subprocess
88+
89+ dest_dir = Path (dest ).absolute ()
90+
91+ uv_build_cmd = [* project .core .uv_cmd , "build" , "--out-dir" , str (dest_dir )]
92+ if verbose == - 1 :
93+ uv_build_cmd .append ("-q" )
94+ elif verbose > 0 :
95+ uv_build_cmd .append (f"-{ 'v' * verbose } " )
96+ subprocess .run (uv_build_cmd , check = True )
97+
98+ # pdm build doesn't include .gitignore, and pdm publish would fail with .gitignore
99+ (dest_dir / ".gitignore" ).unlink (missing_ok = True )
100+ for sdist_fp in dest_dir .glob ("*.tar.gz" ):
101+ if sdist is False :
102+ sdist_fp .unlink (missing_ok = True )
103+ else :
104+ artifacts .append (str (sdist_fp ))
105+
106+ for whl_file in dest_dir .glob ("*.whl" ):
107+ if wheel is False :
108+ whl_file .unlink (missing_ok = True )
109+ else :
110+ artifacts .append (str (whl_file ))
111+
83112 hooks .try_emit ("post_build" , artifacts = artifacts , config_settings = config_settings )
84113
85114 def add_arguments (self , parser : argparse .ArgumentParser ) -> None :
@@ -113,5 +142,6 @@ def handle(self, project: Project, options: argparse.Namespace) -> None:
113142 wheel = options .wheel ,
114143 dest = options .dest ,
115144 clean = options .clean ,
145+ verbose = options .verbose ,
116146 hooks = HookManager (project , options .skip ),
117147 )
0 commit comments