Commit 481498bc authored by Jérome Perrin's avatar Jérome Perrin

XMLExportImport: WIP support pickle protocol 3 🚧

parent 2231fa1f
...@@ -79,6 +79,7 @@ from OFS import SimpleItem ...@@ -79,6 +79,7 @@ from OFS import SimpleItem
from OFS.Image import Pdata from OFS.Image import Pdata
import coverage import coverage
from io import BytesIO from io import BytesIO
from six.moves import StringIO
from copy import deepcopy from copy import deepcopy
from zExceptions import BadRequest from zExceptions import BadRequest
from Products.ERP5Type.XMLExportImport import exportXML, customImporters from Products.ERP5Type.XMLExportImport import exportXML, customImporters
...@@ -347,10 +348,11 @@ class BusinessTemplateArchive(object): ...@@ -347,10 +348,11 @@ class BusinessTemplateArchive(object):
try: try:
write = self._writeFile write = self._writeFile
except AttributeError: except AttributeError:
if not isinstance(obj, (bytes, str)): if hasattr(obj, 'read'):
obj.seek(0) obj.seek(0)
obj = obj.read() obj = obj.read()
elif not isinstance(obj, bytes): #import pdb; pdb.set_trace()
if not isinstance(obj, bytes):
obj = obj.encode('utf-8') obj = obj.encode('utf-8')
self.revision.hash(path, obj) self.revision.hash(path, obj)
self._writeString(obj, path) self._writeString(obj, path)
...@@ -865,9 +867,9 @@ class ObjectTemplateItem(BaseTemplateItem): ...@@ -865,9 +867,9 @@ class ObjectTemplateItem(BaseTemplateItem):
obj = self.removeProperties(obj, 1, keep_workflow_history = True) obj = self.removeProperties(obj, 1, keep_workflow_history = True)
transaction.savepoint(optimistic=True) transaction.savepoint(optimistic=True)
f = BytesIO() f = StringIO()
exportXML(obj._p_jar, obj._p_oid, f) exportXML(obj._p_jar, obj._p_oid, f)
bta.addObject(f, key, path=path) bta.addObject(f.getvalue().encode(), key, path=path)
if catalog_method_template_item: if catalog_method_template_item:
# add all datas specific to catalog inside one file # add all datas specific to catalog inside one file
...@@ -1083,8 +1085,8 @@ class ObjectTemplateItem(BaseTemplateItem): ...@@ -1083,8 +1085,8 @@ class ObjectTemplateItem(BaseTemplateItem):
for path, old_object in upgrade_list: for path, old_object in upgrade_list:
# compare object to see it there is changes # compare object to see it there is changes
new_object = self._objects[path] new_object = self._objects[path]
new_io = BytesIO() new_io = StringIO()
old_io = BytesIO() old_io = StringIO()
exportXML(new_object._p_jar, new_object._p_oid, new_io) exportXML(new_object._p_jar, new_object._p_oid, new_io)
new_obj_xml = new_io.getvalue() new_obj_xml = new_io.getvalue()
try: try:
......
...@@ -48,7 +48,7 @@ from lxml import etree ...@@ -48,7 +48,7 @@ from lxml import etree
from lxml.etree import Element, SubElement from lxml.etree import Element, SubElement
from xml_marshaller.xml_marshaller import Marshaller from xml_marshaller.xml_marshaller import Marshaller
from OFS.Image import Pdata from OFS.Image import Pdata
from base64 import standard_b64encode from base64 import standard_b64encode, encodebytes
from hashlib import sha1 from hashlib import sha1
from Products.ERP5Type.Utils import ensure_list from Products.ERP5Type.Utils import ensure_list
#from zLOG import LOG #from zLOG import LOG
...@@ -233,7 +233,6 @@ def Folder_asXML(object, omit_xml_declaration=True, root=None): ...@@ -233,7 +233,6 @@ def Folder_asXML(object, omit_xml_declaration=True, root=None):
## The code below was initially from OFS.XMLExportImport ## The code below was initially from OFS.XMLExportImport
from six import string_types as basestring from six import string_types as basestring
from base64 import encodebytes
from ZODB.serialize import referencesf from ZODB.serialize import referencesf
from ZODB.ExportImport import TemporaryFile, export_end_marker from ZODB.ExportImport import TemporaryFile, export_end_marker
from ZODB.utils import p64 from ZODB.utils import p64
...@@ -263,7 +262,6 @@ def reorderPickle(jar, p): ...@@ -263,7 +262,6 @@ def reorderPickle(jar, p):
new_oid=storage.new_oid): new_oid=storage.new_oid):
"Remap a persistent id to an existing ID and create a ghost for it." "Remap a persistent id to an existing ID and create a ghost for it."
if isinstance(ooid, tuple): ooid, klass = ooid if isinstance(ooid, tuple): ooid, klass = ooid
else: klass=None else: klass=None
...@@ -280,13 +278,23 @@ def reorderPickle(jar, p): ...@@ -280,13 +278,23 @@ def reorderPickle(jar, p):
unpickler.persistent_load=persistent_load unpickler.persistent_load=persistent_load
newp=BytesIO() newp=BytesIO()
pickler=OrderedPickler(newp,1) pickler = OrderedPickler(newp, 3)
pickler.persistent_id=persistent_id pickler.persistent_id=persistent_id
classdef = unpickler.load() classdef = unpickler.load()
obj = unpickler.load() obj = unpickler.load()
pickler.dump(classdef) pickler.dump(classdef)
pickler.dump(obj) pickler.dump(obj)
if 0: # debug
debugp = BytesIO()
debugpickler = OrderedPickler(debugp, 3)
debugpickler.persistent_id = persistent_id
debugpickler.dump(obj)
import pickletools
print(debugp.getvalue())
print(pickletools.dis(debugp.getvalue()))
p=newp.getvalue() p=newp.getvalue()
return obj, p return obj, p
...@@ -300,14 +308,13 @@ def _mapOid(id_mapping, oid): ...@@ -300,14 +308,13 @@ def _mapOid(id_mapping, oid):
def XMLrecord(oid, plen, p, id_mapping): def XMLrecord(oid, plen, p, id_mapping):
# Proceed as usual # Proceed as usual
q=ppml.ToXMLUnpickler f = BytesIO(p)
f=BytesIO(p) u = ppml.ToXMLUnpickler(f)
u=q(f)
u.idprefix, id, aka = _mapOid(id_mapping, oid) u.idprefix, id, aka = _mapOid(id_mapping, oid)
p=u.load(id_mapping=id_mapping).__str__(4) p = u.load(id_mapping=id_mapping).__str__(4)
if f.tell() < plen: if f.tell() < plen:
p=p+u.load(id_mapping=id_mapping).__str__(4) p=p+u.load(id_mapping=id_mapping).__str__(4)
String=' <record id="%s" aka="%s">\n%s </record>\n' % (id, aka, p) String=' <record id="%s" aka="%s">\n%s </record>\n' % (id, aka.decode(), p)
return String return String
def exportXML(jar, oid, file=None): def exportXML(jar, oid, file=None):
...@@ -344,15 +351,21 @@ def exportXML(jar, oid, file=None): ...@@ -344,15 +351,21 @@ def exportXML(jar, oid, file=None):
# Do real export # Do real export
if file is None: if file is None:
file = TemporaryFile() file = TemporaryFile(mode='w')
elif isinstance(file, basestring): elif isinstance(file, basestring):
file = open(file, 'w+b') file = open(file, 'w')
write = file.write write = file.write
write('<?xml version="1.0"?>\n<ZopeData>\n') write('<?xml version="1.0"?>\n<ZopeData>\n')
for oid in reordered_oid_list: for oid in reordered_oid_list:
p = getReorderedPickle(oid) p = getReorderedPickle(oid)
write(XMLrecord(oid, len(p), p, id_mapping)) write(XMLrecord(oid, len(p), p, id_mapping))
write('</ZopeData>\n') write('</ZopeData>\n')
if 0:
try:
print(file.getvalue())
except AttributeError:
pass
import pdb; pdb.set_trace()
return file return file
class zopedata: class zopedata:
......
This diff is collapsed.
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment