ocrd workspace rename-group: don't clobber existing ID
@@ -276,7 +276,7 @@ class Workspace():
url_replacements = {}
log.info("Moving files")
for mets_file in self.mets.find_files(fileGrp=old, local_only=True):
- new_url = mets_file.url
+ new_url = old_url = mets_file.url
# Directory part
new_url = sub(r'^%s/' % old, r'%s/' % new, new_url)
# File part
@@ -287,7 +287,13 @@ class Workspace():
# change the url of ``mets:file``
mets_file.url = new_url
# change the file ID and update structMap
- mets_file.ID = sub(r'^%s' % old, r'%s' % new, mets_file.ID)
+ # change the file ID and update structMap
+ new_id = sub(r'^%s' % old, r'%s' % new, mets_file.ID)
+ try:
+ next(self.mets.find_files(ID=new_id))
+ log.warning("ID %s already exists, not changing ID while renaming %s -> %s" % (new_id, old_url, new_url))
+ except StopIteration:
+ mets_file.ID = new_id
# change file paths in PAGE-XML imageFilename and filename attributes
for page_file in self.mets.find_files(mimetype=MIMETYPE_PAGE, local_only=True):
log.info("Renaming file references in PAGE-XML %s" % page_file)
