Restart from scratch.
This commit is contained in:
64
src/bsv.bak/command/__init__.py
Normal file
64
src/bsv.bak/command/__init__.py
Normal file
@@ -0,0 +1,64 @@
|
||||
# bsv - Backup, Synchronization, Versioning
|
||||
# Copyright (C) 2023 Simon Boyé
|
||||
#
|
||||
# This program is free software: you can redistribute it and/or modify
|
||||
# it under the terms of the GNU Affero General Public License as published by
|
||||
# the Free Software Foundation, either version 3 of the License, or
|
||||
# (at your option) any later version.
|
||||
#
|
||||
# This program is distributed in the hope that it will be useful,
|
||||
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
# GNU Affero General Public License for more details.
|
||||
#
|
||||
# You should have received a copy of the GNU Affero General Public License
|
||||
# along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
from __future__ import annotations
|
||||
|
||||
from argparse import ArgumentParser
|
||||
from dataclasses import dataclass
|
||||
from textwrap import dedent
|
||||
from typing import Callable
|
||||
|
||||
|
||||
@dataclass
|
||||
class Command:
|
||||
init_parser: Callable[[ArgumentParser], None]
|
||||
function: Callable[..., int]
|
||||
|
||||
|
||||
commands: dict[str, Command] = {}
|
||||
def register_command(
|
||||
name: str,
|
||||
init_parser: Callable[[ArgumentParser], None],
|
||||
function: Callable[..., int],
|
||||
):
|
||||
commands[name] = Command(init_parser, function)
|
||||
|
||||
def command(init_parser: Callable[[ArgumentParser], None]=lambda p: None):
|
||||
def decorator(fn: Callable[..., int]):
|
||||
register_command(fn.__name__, init_parser, fn)
|
||||
return fn
|
||||
return decorator
|
||||
|
||||
|
||||
def init_commands(parser: ArgumentParser, parent_parsers: list[ArgumentParser]=[]):
|
||||
import bsv.command.info
|
||||
import bsv.command.init
|
||||
|
||||
subparsers = parser.add_subparsers(
|
||||
metavar = "COMMAND",
|
||||
help = "Sub-command to run.",
|
||||
required = True,
|
||||
)
|
||||
|
||||
for name, command in commands.items():
|
||||
subparser = subparsers.add_parser(
|
||||
name,
|
||||
parents = parent_parsers,
|
||||
help = dedent(command.function.__doc__ or "").strip()
|
||||
)
|
||||
subparser.set_defaults(
|
||||
command = command.function,
|
||||
)
|
||||
command.init_parser(subparser)
|
||||
62
src/bsv.bak/command/info.py
Normal file
62
src/bsv.bak/command/info.py
Normal file
@@ -0,0 +1,62 @@
|
||||
# bsv - Backup, Synchronization, Versioning
|
||||
# Copyright (C) 2023 Simon Boyé
|
||||
#
|
||||
# This program is free software: you can redistribute it and/or modify
|
||||
# it under the terms of the GNU Affero General Public License as published by
|
||||
# the Free Software Foundation, either version 3 of the License, or
|
||||
# (at your option) any later version.
|
||||
#
|
||||
# This program is distributed in the hope that it will be useful,
|
||||
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
# GNU Affero General Public License for more details.
|
||||
#
|
||||
# You should have received a copy of the GNU Affero General Public License
|
||||
# along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
from __future__ import annotations
|
||||
|
||||
from argparse import ArgumentParser
|
||||
from pathlib import Path
|
||||
|
||||
from bsv import __version__
|
||||
from bsv.cli import get_console
|
||||
from bsv.command import command
|
||||
from bsv.repository import Repository
|
||||
|
||||
|
||||
def init_parser(parser: ArgumentParser):
|
||||
parser.add_argument(
|
||||
"--verbose", "-v",
|
||||
action = "count",
|
||||
default = 0,
|
||||
help = "Verbosity level. Specify several times to increase verbosity.",
|
||||
dest = "verbosity",
|
||||
)
|
||||
|
||||
@command(init_parser)
|
||||
def info(config_path: Path, verbosity: int=0) -> int:
|
||||
"""Print informations about bsv: config file used, known repository, file mapping...
|
||||
"""
|
||||
|
||||
print = get_console().print
|
||||
|
||||
print(f"bsv [green]v{__version__}")
|
||||
|
||||
if not config_path.exists():
|
||||
print("bsv configuration not found. Bsv is likely not setup on this device.", style="red")
|
||||
return 0
|
||||
|
||||
repo = Repository(config_path)
|
||||
|
||||
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}")
|
||||
|
||||
print("[blue]Path map:[/blue] (bsv path <-> filesystem path)")
|
||||
if repo.path_map.pairs:
|
||||
for pair in sorted(repo.path_map.pairs):
|
||||
print(f" {pair.bsv} <-> {pair.fs}")
|
||||
else:
|
||||
print(" [bold yellow]No path mapped.")
|
||||
|
||||
return 0
|
||||
118
src/bsv.bak/command/init.py
Normal file
118
src/bsv.bak/command/init.py
Normal file
@@ -0,0 +1,118 @@
|
||||
# bsv - Backup, Synchronization, Versioning
|
||||
# Copyright (C) 2023 Simon Boyé
|
||||
#
|
||||
# This program is free software: you can redistribute it and/or modify
|
||||
# it under the terms of the GNU Affero General Public License as published by
|
||||
# the Free Software Foundation, either version 3 of the License, or
|
||||
# (at your option) any later version.
|
||||
#
|
||||
# This program is distributed in the hope that it will be useful,
|
||||
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
# GNU Affero General Public License for more details.
|
||||
#
|
||||
# You should have received a copy of the GNU Affero General Public License
|
||||
# along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
from __future__ import annotations
|
||||
|
||||
from argparse import ArgumentParser
|
||||
from pathlib import Path
|
||||
import platform
|
||||
|
||||
from bsv.command import command
|
||||
from bsv.repository import check_config_path, check_device_name, check_local_repository_path, create_repository
|
||||
from bsv.util import default_local_repository_path
|
||||
|
||||
|
||||
def init_parser(parser: ArgumentParser):
|
||||
parser.add_argument(
|
||||
"--interactive", "-i",
|
||||
default = False,
|
||||
action = "store_true",
|
||||
help = "Prompt the user for configuration choices.",
|
||||
)
|
||||
parser.add_argument(
|
||||
"--local-repository", "-l",
|
||||
type = Path,
|
||||
default = default_local_repository_path(),
|
||||
nargs = "?",
|
||||
dest = "local_repository_path",
|
||||
help = "Path to a non-existing or empty folder where bsv data will be stored.",
|
||||
)
|
||||
parser.add_argument(
|
||||
"--device-name", "-n",
|
||||
default = platform.node(),
|
||||
help = "Name of the device. Default to system hostname.",
|
||||
)
|
||||
|
||||
@command(init_parser)
|
||||
def init(
|
||||
config_path: Path,
|
||||
device_name: str,
|
||||
local_repository_path: Path,
|
||||
interactive: bool = False,
|
||||
) -> int:
|
||||
"""Initialize a new bsv repository.
|
||||
"""
|
||||
from datetime import datetime as DateTime
|
||||
import tomlkit
|
||||
|
||||
from bsv.cli import get_console, get_error_console, prompt, prompt_confirmation
|
||||
|
||||
print = get_console().print
|
||||
|
||||
def make_config_path(value: str) -> Path:
|
||||
path = Path(value.strip())
|
||||
if not path.is_absolute():
|
||||
path = path.resolve()
|
||||
check_config_path(path)
|
||||
return path
|
||||
|
||||
def make_device_name(value: str) -> str:
|
||||
device_name = value.strip()
|
||||
check_device_name(device_name)
|
||||
return device_name
|
||||
|
||||
def make_local_repository_path(value: str) -> Path:
|
||||
path = Path(value)
|
||||
if not path.is_absolute():
|
||||
path = path.resolve()
|
||||
check_local_repository_path(path)
|
||||
return path
|
||||
|
||||
if interactive:
|
||||
config_path = prompt("Bsv configuration file", make_config_path, default=config_path)
|
||||
device_name = prompt("Device name", make_device_name, default=device_name)
|
||||
local_repository_path = prompt("Destination", make_local_repository_path, default=local_repository_path)
|
||||
|
||||
if not config_path.is_absolute():
|
||||
config_path = config_path.resolve()
|
||||
if not local_repository_path.is_absolute():
|
||||
local_repository_path = local_repository_path.resolve()
|
||||
|
||||
try:
|
||||
check_config_path(config_path)
|
||||
check_device_name(device_name)
|
||||
check_local_repository_path(local_repository_path)
|
||||
except ValueError as err:
|
||||
get_error_console().print(err, style="bold red")
|
||||
return 1
|
||||
|
||||
print("Bsv repository will be created with the following settings:", style="green")
|
||||
print("")
|
||||
print(f"\t[blue]:page_facing_up: Config path[/blue]: [bold yellow]{config_path}")
|
||||
print(f"\t[blue]:computer: Device name[/blue]: [bold yellow]{device_name}")
|
||||
print(f"\t[blue]:floppy_disk: Local repository[/blue]: [bold yellow]{local_repository_path}")
|
||||
print("")
|
||||
|
||||
if interactive:
|
||||
if not prompt_confirmation("Create repository ?"):
|
||||
return 1
|
||||
|
||||
create_repository(
|
||||
config_path = config_path,
|
||||
device_name = device_name,
|
||||
local_repository_path = local_repository_path,
|
||||
)
|
||||
|
||||
return 0
|
||||
Reference in New Issue
Block a user