Changeset View
Changeset View
Standalone View
Standalone View
swh/objstorage/cli.py
Show First 20 Lines • Show All 88 Lines • ▼ Show 20 Lines | |||||
@objstorage_cli_group.command("import") | @objstorage_cli_group.command("import") | ||||
@click.argument("directory", required=True, nargs=-1) | @click.argument("directory", required=True, nargs=-1) | ||||
@click.pass_context | @click.pass_context | ||||
def import_directories(ctx, directory): | def import_directories(ctx, directory): | ||||
"""Import a local directory in an existing objstorage.""" | """Import a local directory in an existing objstorage.""" | ||||
from swh.objstorage.factory import get_objstorage | from swh.objstorage.factory import get_objstorage | ||||
from swh.objstorage.objstorage import compute_hash | |||||
objstorage = get_objstorage(**ctx.obj["config"]["objstorage"]) | objstorage = get_objstorage(**ctx.obj["config"]["objstorage"]) | ||||
nobj = 0 | nobj = 0 | ||||
volume = 0 | volume = 0 | ||||
t0 = time.time() | t0 = time.time() | ||||
for dirname in directory: | for dirname in directory: | ||||
for root, _dirs, files in os.walk(dirname): | for root, _dirs, files in os.walk(dirname): | ||||
for name in files: | for name in files: | ||||
path = os.path.join(root, name) | path = os.path.join(root, name) | ||||
with open(path, "rb") as f: | with open(path, "rb") as f: | ||||
objstorage.add(f.read()) | content = f.read() | ||||
objstorage.add(content, obj_id=compute_hash(content)) | |||||
volume += os.stat(path).st_size | volume += os.stat(path).st_size | ||||
nobj += 1 | nobj += 1 | ||||
click.echo( | click.echo( | ||||
"Imported %d files for a volume of %s bytes in %d seconds" | "Imported %d files for a volume of %s bytes in %d seconds" | ||||
% (nobj, volume, time.time() - t0) | % (nobj, volume, time.time() - t0) | ||||
) | ) | ||||
@objstorage_cli_group.command("fsck") | @objstorage_cli_group.command("fsck") | ||||
@click.pass_context | @click.pass_context | ||||
Show All 18 Lines |