Skip to content

Commit

Permalink
Add initial command-line interface
Browse files Browse the repository at this point in the history
  • Loading branch information
Zedeldi committed Oct 12, 2024
1 parent e83f1ee commit 837e75d
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 3 deletions.
38 changes: 38 additions & 0 deletions igelfs/cli.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
"""Command-line interface for IGEL filesystem operations."""

import json
from argparse import ArgumentParser
from pprint import pprint

from igelfs.filesystem import Filesystem
from igelfs.lxos import LXOSParser


def get_parser() -> ArgumentParser:
"""Return argument parser instance."""
parser = ArgumentParser(
prog="igelfs",
description="Python implementation of the IGEL filesystem",
epilog="Copyright (C) 2024 Zack Didcott",
)
parser.add_argument("path", help="path to the IGEL filesystem image")
parser.add_argument("--inf", help="path to lxos.inf configuration file")
parser.add_argument("--json", action="store_true", help="format result as JSON")
return parser


def main() -> None:
"""Main entry-point for command-line interface."""
parser = get_parser()
args = parser.parse_args()
filesystem = Filesystem(args.path)
lxos_config = LXOSParser(args.inf) if args.inf else None
info = filesystem.get_info(lxos_config)
if args.json:
print(json.dumps(info))
else:
pprint(info)


if __name__ == "__main__":
main()
3 changes: 2 additions & 1 deletion igelfs/convert.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ class Disk:

def __init__(self, path: str | Path) -> None:
"""Initialize disk instance."""
self.path = Path(path).absolute()
self.path = Path(path).resolve()

def allocate(self, size: int, zero: bool = False) -> None:
"""Create empty file of specified size."""
Expand Down Expand Up @@ -87,6 +87,7 @@ def from_filesystem(
disk.write(filesystem)
return disk


@contextmanager
def loop_device(path: str | Path) -> Iterator[str]:
"""Context manager to attach path as loop device, then detach on closing."""
Expand Down
20 changes: 18 additions & 2 deletions igelfs/filesystem.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import os
from functools import cached_property
from pathlib import Path
from typing import Iterator
from typing import Any, Iterator

from igelfs.constants import (
DIR_OFFSET,
Expand All @@ -14,6 +14,7 @@
IGF_SECTION_SIZE,
SectionSize,
)
from igelfs.lxos import LXOSParser
from igelfs.models import (
BootRegistryHeader,
DataModelCollection,
Expand All @@ -30,7 +31,7 @@ class Filesystem:

def __init__(self, path: str | Path) -> None:
"""Initialise instance."""
self.path = Path(path).absolute()
self.path = Path(path).resolve()

def __getitem__(self, index: int | slice) -> Section | DataModelCollection[Section]:
"""Implement getitem method."""
Expand Down Expand Up @@ -206,3 +207,18 @@ def find_partition_by_hash(self, hash_: bytes | str) -> Partition | None:
if partition.header.update_hash == hash_:
return partition
return None

def get_info(self, lxos_config: LXOSParser | None = None) -> dict[str, Any]:
"""Return information about filesystem."""
info = {
"path": self.path.as_posix(),
"size": self.size,
"section_count": self.section_count,
"partition_minors": sorted(self.partition_minors),
}
if lxos_config:
info["partition_names"] = [
lxos_config.find_name_by_partition_minor(partition_minor)
for partition_minor in info["partition_minors"]
]
return info

0 comments on commit 837e75d

Please sign in to comment.