From 9d4bb27936de11327c328216f8a47c695d95a2de Mon Sep 17 00:00:00 2001 From: Vignesh Rao Date: Tue, 31 Dec 2024 19:46:20 -0600 Subject: [PATCH] Restructure project and enable CLI access --- .gitignore | 3 ++ README.md | 53 ++++++++++++++++++++++++- phydisk/__init__.py | 72 ++++++++++++++++++++++++++++++++++ {pydisk => phydisk}/linux.py | 0 {pydisk => phydisk}/macOS.py | 2 +- {pydisk => phydisk}/models.py | 0 {pydisk => phydisk}/squire.py | 0 {pydisk => phydisk}/windows.py | 2 +- pydisk/__init__.py | 34 ---------------- pyproject.toml | 19 ++++----- 10 files changed, 136 insertions(+), 49 deletions(-) create mode 100644 phydisk/__init__.py rename {pydisk => phydisk}/linux.py (100%) rename {pydisk => phydisk}/macOS.py (99%) rename {pydisk => phydisk}/models.py (100%) rename {pydisk => phydisk}/squire.py (100%) rename {pydisk => phydisk}/windows.py (99%) delete mode 100644 pydisk/__init__.py diff --git a/.gitignore b/.gitignore index e04276f..9c659c2 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,5 @@ .idea venv +build +temp.py +PhyDisk.egg-info diff --git a/README.md b/README.md index 1820e2a..85aaa5a 100644 --- a/README.md +++ b/README.md @@ -1,2 +1,51 @@ -# PyDisk -PyDisk +# PhyDisk (Physical Disk) + +PhyDisk is an ultra lightweight python module to get all physical disks connected to a host machine. + +## Installation + +```shell +pip install PhyDisk +``` + +## Usage + +```python +import phydisk + +if __name__ == '__main__': + all_disks = phydisk.get_all_disks() + print(all_disks) +``` + +### Source Commands + +**Linux** + +```shell +/usr/bin/lsblk -o NAME,SIZE,TYPE,MODEL,MOUNTPOINT -J +``` + +**macOS** + +```shell +/usr/sbin/diskutil info -all +``` + +**Windows** + +```shell +C:\\Program Files\\PowerShell\\7\\pwsh.exe -Command +Get-PhysicalDisk | ForEach-Object { + $disk = $_ + $partitions = Get-Partition -DiskNumber $disk.DeviceID + $partitions | ForEach-Object { + [PSCustomObject]@{ + DiskNumber = $disk.DeviceID + Partition = $_.PartitionNumber + DriveLetter = (Get-Volume -Partition $_).DriveLetter + MountPoint = (Get-Volume -Partition $_).DriveLetter + } + } +} +``` diff --git a/phydisk/__init__.py b/phydisk/__init__.py new file mode 100644 index 0000000..b06a118 --- /dev/null +++ b/phydisk/__init__.py @@ -0,0 +1,72 @@ +import logging +import os +import sys +from typing import Dict, List + +from . import linux, macOS, windows, models + +version = "0.0.0-a1" + +LOGGER = logging.getLogger(__name__) + + +def get_disk_lib(user_input: str | os.PathLike): + disk_lib = ( + user_input or + os.environ.get("disk_lib") or + os.environ.get("DISK_LIB") or + models.default_disk_lib()[models.OPERATING_SYSTEM] + ) + assert os.path.isfile(disk_lib), f"Disk library {disk_lib!r} doesn't exist" + return disk_lib + + +def get_all_disks(disk_lib: str | os.PathLike = None) -> List[Dict[str, str]]: + """OS-agnostic function to get all disks connected to the host system.""" + os_map = { + models.OperatingSystem.darwin: macOS.drive_info, + models.OperatingSystem.linux: linux.drive_info, + models.OperatingSystem.windows: windows.drive_info, + } + try: + disk_lib = get_disk_lib(disk_lib) + return os_map[models.OperatingSystem(models.OPERATING_SYSTEM)](disk_lib) + except Exception as error: + LOGGER.error(error) + + +def commandline() -> None: + """Starter function to invoke PhyDisk via CLI commands. + + **Flags** + - ``--version | -V``: Prints the version. + - ``--help | -H``: Prints the help section. + """ + assert sys.argv[0].lower().endswith("phydisk"), "Invalid commandline trigger!!" + + print_ver = "--version" in sys.argv or "-V" in sys.argv + get_help = "--help" in sys.argv or "-H" in sys.argv + + options = { + "--version | -V": "Prints the version.", + "--help | -H": "Prints the help section.", + "phydisk": "Prints all the physical drives and their information", + } + # weird way to increase spacing to keep all values monotonic + _longest_key = len(max(options.keys())) + _pretext = "\n\t* " + choices = _pretext + _pretext.join( + f"{k} {'·' * (_longest_key - len(k) + 8)}→ {v}".expandtabs() + for k, v in options.items() + ) + if print_ver: + print(f"PhyDisk {version}") + sys.exit(0) + if get_help: + print( + f"\nUsage: phydisk [arbitrary-command]\n\nOptions (and corresponding behavior):{choices}" + ) + sys.exit(0) + + for disk in get_all_disks(): + print(disk) diff --git a/pydisk/linux.py b/phydisk/linux.py similarity index 100% rename from pydisk/linux.py rename to phydisk/linux.py diff --git a/pydisk/macOS.py b/phydisk/macOS.py similarity index 99% rename from pydisk/macOS.py rename to phydisk/macOS.py index a937e2f..92f02e5 100644 --- a/pydisk/macOS.py +++ b/phydisk/macOS.py @@ -5,7 +5,7 @@ from collections import defaultdict from typing import Dict, List -from pydisk import squire +from phydisk import squire LOGGER = logging.getLogger(__name__) diff --git a/pydisk/models.py b/phydisk/models.py similarity index 100% rename from pydisk/models.py rename to phydisk/models.py diff --git a/pydisk/squire.py b/phydisk/squire.py similarity index 100% rename from pydisk/squire.py rename to phydisk/squire.py diff --git a/pydisk/windows.py b/phydisk/windows.py similarity index 99% rename from pydisk/windows.py rename to phydisk/windows.py index 50db5c6..78727e6 100644 --- a/pydisk/windows.py +++ b/phydisk/windows.py @@ -6,7 +6,7 @@ import subprocess from typing import Dict, List, Tuple -from pydisk import squire +from phydisk import squire LOGGER = logging.getLogger(__name__) diff --git a/pydisk/__init__.py b/pydisk/__init__.py deleted file mode 100644 index f08fd0e..0000000 --- a/pydisk/__init__.py +++ /dev/null @@ -1,34 +0,0 @@ -import logging -import os -from typing import Dict, List - -from . import linux, macOS, windows, models - -version = "0.0.0-a1" - -LOGGER = logging.getLogger(__name__) - - -def get_disk_lib(user_input: str | os.PathLike): - disk_lib = ( - user_input or - os.environ.get("disk_lib") or - os.environ.get("DISK_LIB") or - models.default_disk_lib()[models.OPERATING_SYSTEM] - ) - assert os.path.isfile(disk_lib), f"Disk library {disk_lib!r} doesn't exist" - return disk_lib - - -def get_all_disks(disk_lib: str | os.PathLike = None) -> List[Dict[str, str]]: - """OS-agnostic function to get all disks connected to the host system.""" - os_map = { - models.OperatingSystem.darwin: macOS.drive_info, - models.OperatingSystem.linux: linux.drive_info, - models.OperatingSystem.windows: windows.drive_info, - } - try: - disk_lib = get_disk_lib(disk_lib) - return os_map[models.OperatingSystem(models.OPERATING_SYSTEM)](disk_lib) - except Exception as error: - LOGGER.error(error) diff --git a/pyproject.toml b/pyproject.toml index 096040c..1883e49 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -1,6 +1,6 @@ [project] -name = "PyDisk" -dynamic = ["version", "dependencies"] +name = "PhyDisk" +dynamic = ["version"] description = "Python module to get physical drives connected to a host machine" readme = "README.md" authors = [{ name = "Vignesh Rao", email = "svignesh1793@gmail.com" }] @@ -12,19 +12,16 @@ classifiers = [ "Operating System :: OS Independent", "Topic :: System :: Hardware", ] -keywords = ["physical-drives", "PyDisk"] +keywords = ["physical-drives", "PhyDisk"] requires-python = ">=3.10" [tool.setuptools] packages = [ - "pydisk" + "phydisk" ] -#[tool.setuptools.package-data] -#"pydisk.templates" = ["*.html"] [tool.setuptools.dynamic] -version = {attr = "pydisk.version"} -dependencies = { file = ["requirements.txt"] } +version = {attr = "phydisk.version"} [project.optional-dependencies] dev = ["pre-commit"] @@ -36,12 +33,12 @@ standard = [ [project.scripts] # sends all the args to commandline function, where the arbitary commands as processed accordingly -pydisk = "pydisk:commandline" +phydisk = "phydisk:commandline" [build-system] requires = ["setuptools", "wheel"] build-backend = "setuptools.build_meta" [project.urls] -Homepage = "https://github.com/thevickypedia/PyDisk" -Source = "https://github.com/thevickypedia/PyDisk" +Homepage = "https://github.com/thevickypedia/PhyDisk" +Source = "https://github.com/thevickypedia/PhyDisk"