1
0
Fork 0
mirror of https://github.com/Kozea/Radicale.git synced 2025-06-26 16:45:52 +00:00

Export: Print invalid collection and component

This commit is contained in:
Unrud 2017-07-24 02:14:55 +02:00
parent ee79db30e6
commit 4a0964529f

View file

@ -54,44 +54,75 @@ def export_storage(config, path, debug=False):
temp = tempfile.mkdtemp(prefix="Radicale.export.") temp = tempfile.mkdtemp(prefix="Radicale.export.")
try: try:
os.mkdir(os.path.join(temp, "root")) os.mkdir(os.path.join(temp, "root"))
remaining_collections = list(ical.Collection.from_path("/", depth="0")) try:
remaining_collections = list(
ical.Collection.from_path("/", depth="0"))
except Exception as e:
print("ERROR: Failed to find child collections of %r: %s" %
("/", e))
if debug:
traceback.print_exc()
exit(1)
while remaining_collections: while remaining_collections:
collection = remaining_collections.pop(0) collection = remaining_collections.pop(0)
try: try:
filesystem_path = pathutils.path_to_filesystem( try:
collection.path, filesystem_path = pathutils.path_to_filesystem(
os.path.join(temp, "root", "collection-root")) collection.path,
except ValueError as e: os.path.join(temp, "root", "collection-root"))
print( except ValueError as e:
"WARNING: Skipping unsafe collection %r: %s" % print(
("/" + collection.path, e)) "WARNING: Skipping unsafe collection %r: %s" %
("/" + collection.path, e))
if debug:
traceback.print_exc()
continue
try:
remaining_collections.extend(collection.children(
collection.path))
except Exception as e:
print("ERROR: Failed to find child collections of %r: %s" %
("/", e))
if debug:
traceback.print_exc()
exit(1)
os.makedirs(filesystem_path)
with collection.props as props:
if props:
props_filename = os.path.join(
filesystem_path, ".Radicale.props")
with open(props_filename, "w") as f:
json.dump(props, f)
for component in collection.components:
try:
if not pathutils.is_safe_filesystem_path_component(
component.name):
print("WARNING: Skipping unsafe item '%s' from "
"collection %r" %
("/" + component.name, collection.path))
continue
items = [component]
if collection.resource_type == "calendar":
items.extend(collection.timezones)
text = ical.serialize(
collection.tag, collection.headers, items)
component_filename = os.path.join(
filesystem_path, component.name)
with open(component_filename, "wb") as f:
f.write(text.encode("utf-8"))
except Exception as e:
print("ERROR: Failed to export component %r from "
"collection %r: %s" %
(component.name, "/" + collection.path, e))
if debug:
traceback.print_exc()
exit(1)
except Exception as e:
print("ERROR: Failed to export collection %r: %s" %
("/" + collection.path, e))
if debug: if debug:
traceback.print_exc() traceback.print_exc()
continue exit(1)
remaining_collections.extend(collection.children(collection.path))
os.makedirs(filesystem_path)
with collection.props as props:
if props:
props_filename = os.path.join(
filesystem_path, ".Radicale.props")
with open(props_filename, "w") as f:
json.dump(props, f)
for component in collection.components:
if not pathutils.is_safe_filesystem_path_component(
component.name):
print("WARNING: Skipping unsafe item '%s' from "
"collection %r" %
("/" + component.name, collection.path))
continue
items = [component]
if collection.resource_type == "calendar":
items.extend(collection.timezones)
text = ical.serialize(
collection.tag, collection.headers, items)
component_filename = os.path.join(
filesystem_path, component.name)
with open(component_filename, "wb") as f:
f.write(text.encode("utf-8"))
try: try:
shutil.move(os.path.join(temp, "root"), path) shutil.move(os.path.join(temp, "root"), path)
except (OSError, shutil.Error) as e: except (OSError, shutil.Error) as e: