1010import sys
1111from pathlib import Path
1212from urllib .parse import urlparse
13+ import pwd
14+ import grp
1315
1416
1517LOG_PREFIX = "CI/CD Pipeline:"
@@ -135,6 +137,27 @@ def latest_package(pattern: str) -> Path:
135137 return matches [0 ]
136138
137139
140+ def ensure_writable_dir (path : Path ) -> None :
141+ try :
142+ path .mkdir (parents = True , exist_ok = True )
143+ return
144+ except PermissionError :
145+ pass
146+
147+ user = pwd .getpwuid (os .getuid ()).pw_name
148+ group = grp .getgrgid (os .getgid ()).gr_name
149+ try :
150+ subprocess .run (["sudo" , "mkdir" , "-p" , str (path )], check = True )
151+ subprocess .run (["sudo" , "chown" , "-R" , f"{ user } :{ group } " , str (path )], check = True )
152+ except subprocess .CalledProcessError as exc :
153+ fail (f"cannot create writable directory { path } : { exc } " )
154+
155+ try :
156+ path .mkdir (parents = True , exist_ok = True )
157+ except PermissionError as exc :
158+ fail (f"cannot write to { path } : { exc } " )
159+
160+
138161def parse_args (argv : list [str ]) -> argparse .Namespace :
139162 parser = argparse .ArgumentParser (add_help = False )
140163 parser .add_argument ("--always-build-backend" , action = "store_true" )
@@ -157,6 +180,7 @@ def main(argv: list[str] | None = None) -> None:
157180 fail (f"BB_PACKAGE_ROOT must be an absolute path (got '{ package_root } ')" )
158181 branch_or_tag = resolve_branch_or_tag ()
159182 branch_or_tag_path = branch_or_tag .replace ("/" , "-" )
183+ branch_root = Path (package_root ) / branch_or_tag_path
160184
161185 log ("requested coins: " + " " .join (args ))
162186 log (f"always_build_backend={ int (always_build_backend )} " )
@@ -165,6 +189,8 @@ def main(argv: list[str] | None = None) -> None:
165189 log (f"branch_or_tag={ branch_or_tag } -> path={ branch_or_tag_path } " )
166190 log (f"package_root={ package_root } " )
167191
192+ ensure_writable_dir (branch_root )
193+
168194 coins : list [str ] = []
169195 blockbook_package_names : list [str ] = []
170196 backend_package_names : list [str ] = []
@@ -211,7 +237,7 @@ def main(argv: list[str] | None = None) -> None:
211237 log (f"removing previous packages matching build/{ backend_package_name } _*.deb" )
212238 for path in Path ("build" ).glob (f"{ backend_package_name } _*.deb" ):
213239 path .unlink ()
214- shutil .rmtree (Path ( package_root ) / branch_or_tag_path / coin , ignore_errors = True )
240+ shutil .rmtree (branch_root / coin , ignore_errors = True )
215241
216242 log ("starting build: make " + " " .join (make_targets ))
217243 try :
@@ -228,7 +254,7 @@ def main(argv: list[str] | None = None) -> None:
228254 if build_backend :
229255 backend_package_file = latest_package (f"{ backend_package_name } _*.deb" )
230256
231- target_dir = Path ( package_root ) / branch_or_tag_path / coin
257+ target_dir = branch_root / coin
232258 target_dir .mkdir (parents = True , exist_ok = True )
233259
234260 staged_blockbook = target_dir / blockbook_package_file .name
0 commit comments