Add --preserve-uuids to `be import-xml`.
authorW. Trevor King <wking@drexel.edu>
Sat, 16 Apr 2011 19:19:31 +0000 (15:19 -0400)
committerW. Trevor King <wking@drexel.edu>
Sat, 16 Apr 2011 19:19:31 +0000 (15:19 -0400)
libbe/bug.py
libbe/command/import_xml.py
libbe/comment.py

index adccf21712ae7593cf345cb470f27eb8aca68163..bbe698020c148033aa14761a9b149826fb51ef4a 100644 (file)
@@ -337,7 +337,7 @@ class Bug (settings_object.SavedSettingsObject):
         sep = '\n' + istring
         return istring + sep.join(lines).rstrip('\n')
 
-    def from_xml(self, xml_string, verbose=True):
+    def from_xml(self, xml_string, preserve_uuids=False, verbose=True):
         u"""
         Note: If a bug uuid is given, set .alt_id to it's value.
         >>> bugA = Bug(uuid="0123", summary="Need to test Bug.from_xml()")
@@ -362,6 +362,10 @@ class Bug (settings_object.SavedSettingsObject):
         ['severity', 'status', 'creator', 'time', 'summary']
         >>> len(list(bugB.comments()))
         3
+        >>> bugC = Bug()
+        >>> bugC.from_xml(xml, preserve_uuids=True)
+        >>> bugC.uuid == bugA.uuid
+        True
         """
         if type(xml_string) == types.UnicodeType:
             xml_string = xml_string.strip().encode('unicode_escape')
@@ -383,7 +387,8 @@ class Bug (settings_object.SavedSettingsObject):
                 pass
             elif child.tag == 'comment':
                 comm = comment.Comment(bug=self)
-                comm.from_xml(child)
+                comm.from_xml(
+                    child, preserve_uuids=preserve_uuids, verbose=verbose)
                 comments.append(comm)
                 continue
             elif child.tag in tags:
@@ -392,7 +397,7 @@ class Bug (settings_object.SavedSettingsObject):
                 else:
                     text = xml.sax.saxutils.unescape(child.text)
                     text = text.decode('unicode_escape').strip()
-                if child.tag == 'uuid':
+                if child.tag == 'uuid' and not preserve_uuids:
                     uuid = text
                     continue # don't set the bug's uuid tag.
                elif child.tag == 'created':
index b4da2fd980223cf34803da87ade653ab8dfc0c25..44205cbee30965de97c2c0c8e26ff7e852c6b18b 100644 (file)
@@ -76,6 +76,8 @@ class Import_XML (libbe.command.Command):
                     help="If any comment's <in-reply-to> refers to a non-existent comment, ignore it (instead of raising an exception)."),
                 libbe.command.Option(name='add-only', short_name='a',
                     help='If any bug or comment listed in the XML file already exists in the bug repository, do not alter the repository version.'),
+                libbe.command.Option(name='preserve-uuids', short_name='p',
+                    help='Preserve UUIDs for trusted input (potential name collisions).'),
                 libbe.command.Option(name='comment-root', short_name='c',
                     help='Supply a bug or comment ID as the root of any <comment> elements that are direct children of the <be-xml> element.  If any such <comment> elements exist, you are required to set this option.',
                     arg=libbe.command.Argument(
@@ -131,11 +133,11 @@ class Import_XML (libbe.command.Command):
         for child in be_xml.getchildren():
             if child.tag == 'bug':
                 new = libbe.bug.Bug(bugdir=bugdir)
-                new.from_xml(child)
+                new.from_xml(child, preserve_uuids=params['preserve-uuids'])
                 root_bugs.append(new)
             elif child.tag == 'comment':
                 new = libbe.comment.Comment(croot_bug)
-                new.from_xml(child)
+                new.from_xml(child, preserve_uuids=params['preserve-uuids'])
                 root_comments.append(new)
             elif child.tag == 'version':
                 for gchild in child.getchildren():
index 6350e2c60acff4aaec14ae7fa845e6f24e7b35d3..8ffb3cdd0594825b9fdc4542ba564c23579be566 100644 (file)
@@ -340,7 +340,7 @@ class Comment (Tree, settings_object.SavedSettingsObject):
         sep = '\n' + istring
         return istring + sep.join(lines).rstrip('\n')
 
-    def from_xml(self, xml_string, verbose=True):
+    def from_xml(self, xml_string, preserve_uuids=False, verbose=True):
         u"""
         Note: If alt-id is not given, translates any <uuid> fields to
         <alt-id> fields.
@@ -360,6 +360,10 @@ class Comment (Tree, settings_object.SavedSettingsObject):
         >>> commB.alt_id = None
         >>> commB.xml() == xml
         True
+        >>> commC = Comment()
+        >>> commC.from_xml(xml, preserve_uuids=True)
+        >>> commC.uuid == commA.uuid
+        True
         """
         if type(xml_string) == types.UnicodeType:
             xml_string = xml_string.strip().encode('unicode_escape')
@@ -385,7 +389,7 @@ class Comment (Tree, settings_object.SavedSettingsObject):
                 else:
                     text = xml.sax.saxutils.unescape(child.text)
                     text = text.decode('unicode_escape').strip()
-                if child.tag == 'uuid':
+                if child.tag == 'uuid' and not preserve_uuids:
                     uuid = text
                     continue # don't set the comment's uuid tag.
                 elif child.tag == 'body':