88import json
99import logging
1010import os
11- import subprocess
1211import sys
1312from pathlib import Path
1413
14+ from plumbum .cmd import cat , sed , zig , git , reuse , nvchecker
15+
1516from rich .console import Console
1617from rich .logging import RichHandler
1718
1819GITHUB_ORG = "zig-devel"
20+ GITHUB_REPO = f"{ GITHUB_ORG } /.github"
1921INTERNAL_LICENSE = "0BSD"
2022
2123console = Console ()
2224
2325
24- def zig (cmd ):
25- cmd = f"zig { cmd } "
26- # zig prints all logs to stderr and adds a prefix like `info:` or `error:`
27- process = subprocess .Popen (
28- cmd , shell = True , text = True , stdout = subprocess .PIPE , stderr = subprocess .STDOUT
29- )
30- for line in process .stdout :
31- if line .startswith ("error: " ):
32- logging .error (line .strip ().removeprefix ("error: " ))
33- else :
34- logging .info (line .strip ().removeprefix ("info: " ))
35- process .wait ()
36- if process .returncode != 0 :
37- logging .critical (f"'{ cmd } ' failed with exit code { process .returncode } " )
38- exit (1 )
26+ def cmd (command ):
27+ (returncode , stdout , stderr ) = command .run (retcode = None )
3928
29+ stdout = stdout .strip ()
30+ if stdout != "" :
31+ logging .info (stdout )
4032
41- def system (cmd ):
42- process = subprocess .Popen (
43- cmd , shell = True , stdout = subprocess .PIPE , stderr = subprocess .STDOUT , text = True
44- )
45- for line in process .stdout :
46- logging .info (line .strip ())
47- process .wait ()
48- if process .returncode != 0 :
49- logging .critical (f"Failed with exit code { process .returncode } " )
33+ if returncode != 0 :
34+ logging .error (f"'{ command } ' failed with exit code { returncode } " )
35+ stderr = stderr .strip ()
36+ if stderr != "" :
37+ logging .error (stderr )
5038 exit (1 )
5139
5240
@@ -117,7 +105,7 @@ def _SetupGithubActions():
117105 jobs:
118106 build:
119107 name: Build and test library
120- uses: { GITHUB_ORG } /.github /.github/workflows/library.yml@latest
108+ uses: { GITHUB_REPO } /.github/workflows/library.yml@latest
121109 """ ,
122110 )
123111
@@ -134,7 +122,7 @@ def _SetupGithubActions():
134122 jobs:
135123 release:
136124 name: Prepare GitHub release
137- uses: { GITHUB_ORG } /.github /.github/workflows/release.yml@latest
125+ uses: { GITHUB_REPO } /.github/workflows/release.yml@latest
138126 permissions:
139127 contents: write
140128 """ ,
@@ -158,9 +146,8 @@ def _SetupAutoUpdate(git: str):
158146 )
159147
160148 logging .info ("Fetch latest version" )
161- system (
162- "nvchecker -c .nvchecker.toml -l error"
163- ) # TODO: use python api instead of subprocess
149+
150+ cmd (nvchecker ["-c" , ".nvchecker.toml" , "-l" , "error" ])
164151 os .rename (".github/newver.json" , ".github/oldver.json" )
165152
166153 git = git .removesuffix (".git" ).removesuffix ("/" )
@@ -205,20 +192,18 @@ def _SetupLicenses(project_licenses: list[str]):
205192 )
206193
207194 logging .info ("Download licenses files..." )
208- system ( " reuse download --all" )
195+ cmd ( reuse [ " download" , " --all"] )
209196
210197 return project_licenses
211198
212199
213200def _SetupZigPackage (name : str , version : str , git : str , revision : str ):
214201 logging .info ("Init zig package" )
215- zig ( "init --minimal" )
202+ cmd ( zig [ "init" , " --minimal"] )
216203
217- fingerprint = subprocess .check_output (
218- "cat build.zig.zon | sed -n 's/\\ s*\\ .fingerprint = \\ (.*\\ ),/\\ 1/p'" ,
219- text = True ,
220- shell = True ,
221- ).strip ()
204+ fingerprint = (
205+ cat ["build.zig.zon" ] | sed ["-n" , "s/\\ s*\\ .fingerprint = \\ (.*\\ ),/\\ 1/p" ]
206+ )().strip ()
222207 logging .info (f"Detect project fingerprint: { fingerprint } " )
223208
224209 logging .info ("Generate build.zig boilerplate" )
@@ -299,7 +284,7 @@ def _SetupZigPackage(name: str, version: str, git: str, revision: str):
299284 archive_link = f"{ git } /archive/{ revision } .tar.gz"
300285
301286 logging .info (f"Download upstream sources from { archive_link } " )
302- zig ( f "fetch --save={ name } { archive_link } " )
287+ cmd ( zig [ "fetch" , f" --save={ name } " , archive_link ] )
303288
304289
305290def _Licenselink (spdx : str ):
@@ -363,7 +348,7 @@ def main(argv):
363348 args = parser .parse_args (argv )
364349
365350 logging .basicConfig (
366- level = logging .NOTSET ,
351+ level = logging .INFO ,
367352 format = "%(message)s" ,
368353 datefmt = "[%X]" ,
369354 handlers = [RichHandler ()],
@@ -372,7 +357,7 @@ def main(argv):
372357 logging .basicConfig (level = logging .DEBUG )
373358
374359 console .print ("[bold]Init git repository...[/bold]" )
375- system ( f" git init { args .name } " )
360+ cmd ( git [ " init" , args .name ] )
376361 os .chdir (args .name )
377362
378363 console .print ("[bold]Setup git configs...[/bold]" )
0 commit comments