Skip to content

Commit

Permalink
Restructure project and enable CLI access
Browse files Browse the repository at this point in the history
  • Loading branch information
dormant-user committed Jan 1, 2025
1 parent 235a997 commit 9d4bb27
Show file tree
Hide file tree
Showing 10 changed files with 136 additions and 49 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,5 @@
.idea
venv
build
temp.py
PhyDisk.egg-info
53 changes: 51 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -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
}
}
}
```
72 changes: 72 additions & 0 deletions phydisk/__init__.py
Original file line number Diff line number Diff line change
@@ -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)
File renamed without changes.
2 changes: 1 addition & 1 deletion pydisk/macOS.py → phydisk/macOS.py
Original file line number Diff line number Diff line change
Expand Up @@ -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__)

Expand Down
File renamed without changes.
File renamed without changes.
2 changes: 1 addition & 1 deletion pydisk/windows.py → phydisk/windows.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import subprocess
from typing import Dict, List, Tuple

from pydisk import squire
from phydisk import squire

LOGGER = logging.getLogger(__name__)

Expand Down
34 changes: 0 additions & 34 deletions pydisk/__init__.py

This file was deleted.

19 changes: 8 additions & 11 deletions pyproject.toml
Original file line number Diff line number Diff line change
@@ -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" }]
Expand All @@ -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"]
Expand All @@ -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"

0 comments on commit 9d4bb27

Please sign in to comment.