Changeset View
Changeset View
Standalone View
Standalone View
bin/swh-storage-add-dir
#!/usr/bin/env python3 | #!/usr/bin/env python3 | ||||
# Copyright (C) 2015 The Software Heritage developers | # Copyright (C) 2015 The Software Heritage developers | ||||
# See the AUTHORS file at the top-level directory of this distribution | # See the AUTHORS file at the top-level directory of this distribution | ||||
# License: GNU General Public License version 3, or any later version | # License: GNU General Public License version 3, or any later version | ||||
# See top-level LICENSE file for more information | # See top-level LICENSE file for more information | ||||
import logging | import logging | ||||
import os | import os | ||||
import sys | import sys | ||||
from swh import storage | from swh.storage.storage import Storage | ||||
from swh.core.hashutil import _hash_fname | from swh.model.hashutil import MultiHash | ||||
if __name__ == '__main__': | if __name__ == '__main__': | ||||
try: | try: | ||||
db_connstring = sys.argv[1] | db_connstring = sys.argv[1] | ||||
obj_root = sys.argv[2] | obj_root = sys.argv[2] | ||||
dirname = sys.argv[3] | dirname = sys.argv[3] | ||||
except IndexError: | except IndexError: | ||||
print('Usage: swh-storage-add-dir' + | print('Usage: swh-storage-add-dir' + | ||||
' DB_CONNSTRING OBJ_STORAGE_DIR DATA_DIR') | ' DB_CONNSTRING OBJ_STORAGE_DIR DATA_DIR') | ||||
print('Example: swh-storage-add-dir "dbname=swh user=foo"' + | print('Example: swh-storage-add-dir "dbname=swh user=foo"' + | ||||
' /srv/softwareheritage/objects /usr/src/linux-4.2') | ' /srv/softwareheritage/objects /usr/src/linux-4.2') | ||||
sys.exit(1) | sys.exit(1) | ||||
logging.basicConfig(level=logging.INFO) | logging.basicConfig(level=logging.INFO) | ||||
storage = storage.Storage(db_connstring, obj_root) | storage = Storage(db_connstring, obj_root) | ||||
def list_content(): | def list_content(): | ||||
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) | ||||
cont = _hash_fname(path) | cont = MultiHash.from_path(path).digest() | ||||
cont['data'] = open(path, 'rb').read() | cont['data'] = open(path, 'rb').read() | ||||
yield cont | yield cont | ||||
storage.content_add(list_content()) | storage.content_add(list_content()) |