2019-12-19 10:34:19 +00:00
|
|
|
"""Onionr - Private P2P Communication.
|
2019-06-01 16:54:36 +00:00
|
|
|
|
2019-12-19 10:34:19 +00:00
|
|
|
This file handles the command for exporting blocks to disk
|
|
|
|
"""
|
|
|
|
import sys
|
|
|
|
|
|
|
|
import logger
|
|
|
|
import onionrstorage
|
|
|
|
from utils import createdirs
|
|
|
|
from onionrutils import stringvalidators
|
2020-03-30 08:23:59 +00:00
|
|
|
from etc.onionrvalues import BLOCK_EXPORT_FILE_EXT
|
2019-12-19 10:34:19 +00:00
|
|
|
import filepaths
|
2020-02-08 09:07:07 +00:00
|
|
|
|
|
|
|
import os
|
|
|
|
from coredb import blockmetadb
|
2019-12-19 10:34:19 +00:00
|
|
|
"""
|
2019-06-01 16:54:36 +00:00
|
|
|
This program is free software: you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU 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 General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
along with this program. If not, see <https://www.gnu.org/licenses/>.
|
2019-12-19 10:34:19 +00:00
|
|
|
"""
|
|
|
|
|
|
|
|
|
|
|
|
def _do_export(b_hash):
|
2019-07-27 02:42:55 +00:00
|
|
|
createdirs.create_dirs()
|
2019-12-19 10:34:19 +00:00
|
|
|
data = onionrstorage.getData(b_hash)
|
2020-03-30 08:23:59 +00:00
|
|
|
with open('%s/%s%s' % (filepaths.export_location,
|
|
|
|
b_hash, BLOCK_EXPORT_FILE_EXT), 'wb') as export:
|
2019-12-19 10:34:19 +00:00
|
|
|
export.write(data)
|
2019-06-20 00:59:05 +00:00
|
|
|
logger.info('Block exported as file', terminal=True)
|
2019-06-01 16:54:36 +00:00
|
|
|
|
2019-12-19 10:34:19 +00:00
|
|
|
|
2020-03-30 08:23:59 +00:00
|
|
|
def export_block(*args):
|
2019-12-19 10:34:19 +00:00
|
|
|
"""Export block based on hash from stdin or argv."""
|
2020-03-30 08:23:59 +00:00
|
|
|
if args:
|
|
|
|
b_hash = args[0]
|
2019-06-01 16:54:36 +00:00
|
|
|
else:
|
2020-03-30 08:23:59 +00:00
|
|
|
try:
|
|
|
|
if not stringvalidators.validate_hash(sys.argv[2]):
|
|
|
|
raise ValueError
|
|
|
|
except (IndexError, ValueError):
|
|
|
|
logger.error('No valid block hash specified.', terminal=True)
|
|
|
|
sys.exit(1)
|
|
|
|
else:
|
|
|
|
b_hash = sys.argv[2]
|
|
|
|
_do_export(b_hash)
|
2019-12-19 10:34:19 +00:00
|
|
|
|
2019-09-21 05:06:49 +00:00
|
|
|
|
2019-12-19 10:34:19 +00:00
|
|
|
export_block.onionr_help = "<block hash>: Export block to " # type: ignore
|
|
|
|
export_block.onionr_help += "a file. Export directory is in " # type: ignore
|
|
|
|
export_block.onionr_help += "Onionr home under block-export" # type: ignore
|