-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Restructure project and support all arch info in CLI
- Loading branch information
1 parent
ac33c71
commit c455ec2
Showing
7 changed files
with
311 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
import logging | ||
import os | ||
|
||
from pyarchitecture import models | ||
from pyarchitecture.cpu import main | ||
|
||
LOGGER = logging.getLogger(__name__) | ||
|
||
|
||
def _get_cpu_lib(user_input: str | os.PathLike) -> str: | ||
"""Get the CPU library for the appropriate OS. | ||
Args: | ||
user_input: CPU library input by user. | ||
""" | ||
cpu_lib = ( | ||
user_input | ||
or os.environ.get("cpu_lib") | ||
or os.environ.get("CPU_LIB") | ||
or models.default_cpu_lib()[models.OPERATING_SYSTEM] | ||
) | ||
assert os.path.isfile(cpu_lib), f"CPU library {cpu_lib!r} doesn't exist" | ||
return cpu_lib | ||
|
||
|
||
def get_cpu_name(cpu_lib: str | os.PathLike = None) -> str: | ||
"""OS-agnostic function to get all CPUs connected to the host system. | ||
Args: | ||
cpu_lib: Custom CPU library path. | ||
Returns: | ||
List[Dict[str, str]]: | ||
Returns CPU name. | ||
""" | ||
return main.get_name(_get_cpu_lib(cpu_lib)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
import logging | ||
import os | ||
import subprocess | ||
|
||
from pyarchitecture import models | ||
|
||
LOGGER = logging.getLogger(__name__) | ||
|
||
|
||
def _darwin(cpu_lib: str | os.PathLike) -> str: | ||
"""Get processor information for macOS.""" | ||
command = [cpu_lib, "-n", "machdep.cpu.brand_string"] | ||
return subprocess.check_output(command).decode().strip() | ||
|
||
|
||
def _linux(cpu_lib: str | os.PathLike) -> str: | ||
"""Get processor information for Linux.""" | ||
with open(cpu_lib) as file: | ||
for line in file: | ||
if "model name" in line: | ||
return line.split(":")[1].strip() | ||
|
||
|
||
def _windows(cpu_lib: str | os.PathLike) -> str: | ||
"""Get processor information for Windows.""" | ||
command = f"{cpu_lib} cpu get name" | ||
output = subprocess.check_output(command, shell=True).decode() | ||
return output.strip().split("\n")[1] | ||
|
||
|
||
def get_name(cpu_lib: str | os.PathLike) -> str | None: | ||
"""Get processor information for the host operating system. | ||
Returns: | ||
str: | ||
Returns the processor information as a string. | ||
""" | ||
os_map = { | ||
models.OperatingSystem.darwin: _darwin, | ||
models.OperatingSystem.linux: _linux, | ||
models.OperatingSystem.windows: _windows, | ||
} | ||
try: | ||
return os_map[models.OPERATING_SYSTEM](cpu_lib) | ||
except Exception as error: | ||
LOGGER.error(error) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
import logging | ||
import os | ||
from typing import Dict, List | ||
|
||
from pyarchitecture import models | ||
from pyarchitecture.gpu import main | ||
|
||
LOGGER = logging.getLogger(__name__) | ||
|
||
|
||
def _get_gpu_lib(user_input: str | os.PathLike) -> str: | ||
"""Get the GPU library for the appropriate OS. | ||
Args: | ||
user_input: GPU library input by user. | ||
""" | ||
gpu_lib = ( | ||
user_input | ||
or os.environ.get("gpu_lib") | ||
or os.environ.get("GPU_LIB") | ||
or models.default_gpu_lib()[models.OPERATING_SYSTEM] | ||
) | ||
assert os.path.isfile(gpu_lib), f"GPU library {gpu_lib!r} doesn't exist" | ||
return gpu_lib | ||
|
||
|
||
def get_gpu_names(gpu_lib: str | os.PathLike = None) -> List[Dict[str, str]]: | ||
"""OS-agnostic function to get all GPUs connected to the host system. | ||
Args: | ||
gpu_lib: Custom GPU library path. | ||
Returns: | ||
List[Dict[str, str]]: | ||
Returns the GPU model and vendor information as a list of key-value pairs. | ||
""" | ||
return main.get_names(_get_gpu_lib(gpu_lib)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,124 @@ | ||
import json | ||
import logging | ||
import os | ||
import subprocess | ||
from typing import Dict, List, Optional | ||
|
||
from pyarchitecture import models | ||
|
||
LOGGER = logging.getLogger(__name__) | ||
|
||
|
||
def _darwin(gpu_lib: str | os.PathLike) -> Optional[List[Dict[str, str]]]: | ||
"""Get GPU model and vendor information for Linux operating system. | ||
Returns: | ||
List[Dict[str, str]]: | ||
Returns a list of GPU model and vendor information. | ||
""" | ||
result = subprocess.run( | ||
[gpu_lib, "SPDisplaysDataType", "-json"], | ||
capture_output=True, | ||
text=True, | ||
) | ||
if result.stderr: | ||
LOGGER.debug(result.stderr) | ||
return | ||
displays = json.loads(result.stdout).get("SPDisplaysDataType", []) | ||
gpu_info = [] | ||
for display in displays: | ||
if "sppci_model" in display.keys(): | ||
gpu_info.append( | ||
dict( | ||
model=display.get("sppci_model"), | ||
cores=display.get("sppci_cores", "N/A"), | ||
memory=display.get( | ||
"sppci_vram", display.get("spdisplays_vram", "N/A") | ||
), | ||
vendor=display.get("sppci_vendor", "N/A"), | ||
) | ||
) | ||
return gpu_info | ||
|
||
|
||
def _linux(gpu_lib: str | os.PathLike) -> Optional[List[Dict[str, str]]]: | ||
"""Get GPU model and vendor information for Linux operating system. | ||
Returns: | ||
List[Dict[str, str]]: | ||
Returns a list of GPU model and vendor information. | ||
""" | ||
result = subprocess.run( | ||
[gpu_lib], | ||
capture_output=True, | ||
text=True, | ||
) | ||
if result.stderr: | ||
LOGGER.debug(result.stderr) | ||
return | ||
gpus = result.stdout.splitlines() | ||
gpu_info = [] | ||
for line in gpus: | ||
if "VGA" in line: | ||
gpu = line.split(":")[-1].strip() | ||
else: | ||
continue | ||
gpu_info.append( | ||
dict( | ||
model=gpu.split(":")[-1].strip(), | ||
) | ||
) | ||
return gpu_info | ||
|
||
|
||
def _windows(gpu_lib: str | os.PathLike) -> Optional[List[Dict[str, str]]]: | ||
"""Get GPU model and vendor information for Windows operating system. | ||
Returns: | ||
List[Dict[str, str]]: | ||
Returns a list of GPU model and vendor information. | ||
""" | ||
result = subprocess.run( | ||
[ | ||
gpu_lib, | ||
"path", | ||
"win32_videocontroller", | ||
"get", | ||
"Name,AdapterCompatibility", | ||
"/format:csv", | ||
], | ||
stdout=subprocess.PIPE, | ||
text=True, | ||
) | ||
if result.stderr: | ||
LOGGER.debug(result.stderr) | ||
return | ||
gpus_raw = [line for line in result.stdout.splitlines() if line.strip()] | ||
try: | ||
keys = ( | ||
gpus_raw[0] | ||
.replace("Node", "node") | ||
.replace("AdapterCompatibility", "vendor") | ||
.replace("Name", "model") | ||
.split(",") | ||
) | ||
values = "".join(gpus_raw[1:]).split(",") | ||
except ValueError as error: | ||
LOGGER.debug(error) | ||
return | ||
if len(values) >= len(keys): | ||
result = [] | ||
for i in range(0, len(values), len(keys)): | ||
result.append(dict(zip(keys, values[i : i + len(keys)]))) # noqa: E203 | ||
return result | ||
else: | ||
LOGGER.debug("ValueError: Not enough values for the keys") | ||
|
||
|
||
def get_names(gpu_lib: str | os.PathLike) -> List[Dict[str, str]]: | ||
"""Get list of GPU model and vendor information based on the operating system.""" | ||
fn_map = dict(linux=_linux, darwin=_darwin, windows=_windows) | ||
try: | ||
return fn_map[models.OPERATING_SYSTEM](gpu_lib) | ||
except (subprocess.SubprocessError, FileNotFoundError) as error: | ||
LOGGER.debug(error) |
Oops, something went wrong.