1- import os
2- import shutil
3- import subprocess
41import sys
5- from pathlib import Path
62
7- from nvibrant import __version__
3+ from nvibrant import (
4+ RESOURCES ,
5+ __version__ ,
6+ current_driver ,
7+ shell ,
8+ )
89
9- # Common paths
10- PACKAGE = Path (__file__ ).parent
11- NVIBRANT = (PACKAGE .parent )
12- RESOURCES = (PACKAGE / "resources" )
13- OPEN_GPU = (NVIBRANT / "open-gpu" )
14- RELEASE = (NVIBRANT / "release" )
15- BUILD = (NVIBRANT / "build" )
16-
17- # Common tools
18- PYTHON = (sys .executable ,)
19- MESON = (* PYTHON , "-m" , "mesonbuild.mesonmain" )
20- NINJA = (* PYTHON , "-m" , "ninja" )
21-
22- # #
23-
24- def shell (* command : str , echo : bool = True , ** kwargs ) -> subprocess .CompletedProcess :
25- command = tuple (map (str , command ))
26- if echo : print (f"• Call { command } " )
27- return subprocess .run (command , ** kwargs )
28-
29- # # Repositories
30-
31- def get_tags (repo : Path ) -> list [str ]:
32- return list (filter (None ,
33- subprocess .check_output (("git" , "tag" ), cwd = repo )
34- .decode ("utf-8" ).splitlines ()))
35-
36- def checkout_tag (repo : Path , tag : str ) -> subprocess .CompletedProcess :
37- return shell ("git" , "checkout" , "-f" , tag , cwd = repo )
38-
39- # # Directories
40-
41- def mkdir (path : Path ) -> Path :
42- Path (path ).mkdir (parents = True , exist_ok = True )
43- return Path (path )
44-
45- def rmdir (path : Path ) -> Path :
46- shutil .rmtree (path , ignore_errors = True )
47- return Path (path )
48-
49- def reset_dir (path : Path ) -> Path :
50- return mkdir (rmdir (path ))
51-
52- # # Drivers
53-
54- def current_driver () -> str :
55-
56- # Safety fallback or override with environment variable
57- if (force := os .getenv ("NVIDIA_DRIVER_VERSION" )):
58- return force
59-
60- # Seems to be a common and stable path to get the information
61- elif (file := Path ("/sys/module/nvidia/version" )).exists ():
62- return file .read_text ("utf8" ).strip ()
63-
64- print ("Could not find the current driver version at /sys/module/nvidia/version" )
65- print ("• Run with 'NVIDIA_DRIVER_VERSION=x.y.z nvibrant' to set or force it" )
66- sys .exit (1 )
67-
68- # ------------------------------------------------------------------------------------------------ #
69- # Entry points
70-
71- def actions () -> None :
72- for (key , value ) in dict (
73- GHA_VERSION = __version__ ,
74- ).items ():
75- print (f"{ key } ={ value } " )
76-
77- def build () -> None :
78- mkdir (RESOURCES )
79-
80- # Internal structs may differ between versions,
81- # compile the project for all nvidia drivers
82- for driver in reversed (get_tags (OPEN_GPU )):
83- checkout_tag (OPEN_GPU , driver )
84-
85- # Configure and compile cpp project
86- shell (* MESON , "setup" , BUILD ,
87- "--buildtype" , "release" ,
88- "--reconfigure" )
89- shell (* NINJA , "-C" , BUILD )
90-
91- # Name the binary release
92- nvibrant = (BUILD / "nvibrant" )
93- nvibrant .rename (RESOURCES / (
94- f"nvibrant"
95- f"-linux"
96- f"-amd64"
97- f"-{ driver } "
98- f"-v{ __version__ } "
99- f".bin"
100- ))
101-
102- # Revert back to the main branch
103- checkout_tag (OPEN_GPU , "main" )
10410
10511def main () -> None :
10612 driver = current_driver ()
@@ -127,5 +33,6 @@ def main() -> None:
12733 shell ("chmod" , "+x" , nvibrant , echo = False )
12834 shell (nvibrant , * sys .argv [1 :], echo = False )
12935
130- if __name__ == "__main__" :
36+
37+ if (__name__ == "__main__" ):
13138 main ()
0 commit comments