Browse Source

Improve info.

master
Draklaw 1 year ago
parent
commit
e74eaf0408
  1. 2
      README.md
  2. 27
      src/bsv/command/info.py
  3. 26
      src/bsv/repository.py
  4. 4
      src/bsv/simple_cas/cas.py

2
README.md

@ -29,7 +29,7 @@ Basic features. Naïve CAS implementation that store everything in a single big
- [x] Simple CAS implementation (it's OK if it's naïve). - [x] Simple CAS implementation (it's OK if it's naïve).
- [x] Content-based chunking to store files. - [x] Content-based chunking to store files.
- [x] `bsv init` command to initialize bsv. - [x] `bsv init` command to initialize bsv.
- [ ] `bsv info` print useful information bsv configuration. - [x] `bsv info` print useful information bsv configuration.
- [ ] `bsv log` show the history of snapshots. - [ ] `bsv log` show the history of snapshots.
- [ ] `bsv show <digest>` show the object `digest`. - [ ] `bsv show <digest>` show the object `digest`.
- [ ] `bsv ls <bsv-path>` list files in a bsv directory. - [ ] `bsv ls <bsv-path>` list files in a bsv directory.

27
src/bsv/command/info.py

@ -19,6 +19,7 @@ from argparse import ArgumentParser
from pathlib import Path from pathlib import Path
from bsv import __version__ from bsv import __version__
from bsv.cli import get_console
from bsv.command import command from bsv.command import command
from bsv.repository import Repository from bsv.repository import Repository
@ -33,27 +34,29 @@ def init_parser(parser: ArgumentParser):
) )
@command(init_parser) @command(init_parser)
def info(repository_path: Path | None, verbosity: int=0) -> int: def info(config_path: Path, verbosity: int=0) -> int:
"""Print informations about bsv: config file used, known repository, file mapping... """Print informations about bsv: config file used, known repository, file mapping...
""" """
print(f"bsv v{__version__}") print = get_console().print
if repository_path is None: print(f"bsv [green]v{__version__}")
print("Repository path not found. Bsv is likely not setup on this device.")
if not config_path.exists():
print("bsv configuration not found. Bsv is likely not setup on this device.", style="red")
return 0 return 0
else:
print(f"Repository path: {repository_path}")
repo = Repository(repository_path) repo = Repository(config_path)
print(f"Repository name: {repo.name}") print(f"[blue]Config path: [bold yellow]{repo.config_path}")
print(f"[blue]Device name: [bold yellow]{repo.device_name}")
print(f"[blue]Local repository: [bold yellow]{repo._local_repository_path}")
if repo.path_map: print("[blue]Path map:[/blue] (bsv path <-> filesystem path)")
print("Path map: (bsv path <-> filesystem path)") if repo.path_map.pairs:
for pair in sorted(repo.path_map): for pair in sorted(repo.path_map.pairs):
print(f" {pair.bsv} <-> {pair.fs}") print(f" {pair.bsv} <-> {pair.fs}")
else: else:
print("Path map is empty.") print(" [bold yellow]No path mapped.")
return 0 return 0

26
src/bsv/repository.py

@ -58,7 +58,7 @@ class Repository:
_context_depth: int = 0 _context_depth: int = 0
def __init__(self, config_path: Path): def __init__(self, config_path: Path):
self._config_path = config_path self._config_path = config_path.resolve()
with self._config_path.open("rb") as stream: with self._config_path.open("rb") as stream:
config = tomllib.load(stream) config = tomllib.load(stream)
@ -209,6 +209,15 @@ class Repository:
# return self.add_snapshot(snapshot, dry_run=dry_run) # return self.add_snapshot(snapshot, dry_run=dry_run)
def get_ref(self, key: str) -> Digest | None:
return self._cas.get_ref(key)
def set_ref(self, key: str, digest: Digest):
self._cas.set_ref(key, digest)
def get_head_snapshot(self) -> Digest | None:
return self.get_ref("HEAD")
def _read(self, digest: Digest, object_type: bytes) -> tuple[ObjectInfo, Blob]: def _read(self, digest: Digest, object_type: bytes) -> tuple[ObjectInfo, Blob]:
obj = self._cas.read(digest, object_type=object_type) obj = self._cas.read(digest, object_type=object_type)
@ -528,6 +537,21 @@ class Tree:
self.write(stream) self.write(stream)
return stream.getvalue() return stream.getvalue()
def __len__(self) -> int:
return len(self.items)
def get(self, key: str) -> TreeItem | None:
for item in self.items:
if item.name == key:
return item
return None
def __getitem__(self, key: str) -> TreeItem:
item = self.get(key)
if item is None:
raise KeyError(f"{key} not found")
return item
@dataclass(frozen=True, order=True, slots=True) @dataclass(frozen=True, order=True, slots=True)
class TreeObject(ObjectInfo): class TreeObject(ObjectInfo):
tree: Tree tree: Tree

4
src/bsv/simple_cas/cas.py

@ -102,9 +102,7 @@ class SimpleCas:
hash = self._hash_factory() hash = self._hash_factory()
hash.update(object_type) hash.update(object_type)
hash.update(b"\0")
hash.update(len(data).to_bytes(4)) hash.update(len(data).to_bytes(4))
hash.update(b"\0")
hash.update(data) hash.update(data)
digest = Digest(hash.digest()) digest = Digest(hash.digest())
@ -153,7 +151,7 @@ class SimpleCas:
return self._root_dir / "refs" / key_path return self._root_dir / "refs" / key_path
@dataclass @dataclass(frozen=True)
class IndexItem: class IndexItem:
object_type: bytes object_type: bytes
offset: int offset: int

Loading…
Cancel
Save