Given C{docname}, return a new docname that is not already attached to
the record.
@param docname: the reference docname.
@type docname: string
@return: a docname not already attached.
@rtype: string
"""
docname=normalize_docname(docname)
goodname=docname
i=1
whilegoodnameinself.get_bibdoc_names():
i+=1
goodname="%s_%s"%(docname,i)
returngoodname
defmerge_bibdocs(self,docname1,docname2):
"""
This method merge C{docname2} into C{docname1}.
1. Given all the formats of the latest version of the files
attached to C{docname2}, these files are added as new formats
into C{docname1}.
2. C{docname2} is marked as deleted.
@raise InvenioBibDocFileError: if at least one format in C{docname2}
already exists in C{docname1}. (In this case the two bibdocs are
preserved)
@note: comments and descriptions are also copied.
@note: if C{docname2} has a I{restriction}(i.e. if the I{status} is
set) and C{docname1} doesn't, the restriction is imported.
"""
bibdoc1=self.get_bibdoc(docname1)
bibdoc2=self.get_bibdoc(docname2)
## Check for possibility
forbibdocfileinbibdoc2.list_latest_files():
format=bibdocfile.get_format()
ifbibdoc1.format_already_exists_p(format):
raiseInvenioBibDocFileError('Format %s already exists in bibdoc %s of record %s. It\'s impossible to merge bibdoc %s into it.'%(format,docname1,self.id,docname2))
Algorithm that transform a broken/old bibdoc into a coherent one.
Think of it as being the fsck of BibDocs.
- All the files in the bibdoc directory will be renamed according
to the document name. Proper .recid, .type, .md5 files will be
created/updated.
- In case of more than one file with the same format version a new
bibdoc will be created in order to put does files.
@param docname: the document name that need to be fixed.
@type docname: string
@return: the list of newly created bibdocs if any.
@rtype: list of BibDoc
@raise InvenioBibDocFileError: in case of issues that can not be
fixed automatically.
"""
bibdoc=self.get_bibdoc(docname)
versions={}
res=[]
new_bibdocs=[]# List of files with the same version/format of
# existing file which need new bibdoc.
counter=0
zero_version_bug=False
ifos.path.exists(bibdoc.basedir):
forfilenameinos.listdir(bibdoc.basedir):
iffilename[0]!='.'and';'infilename:
name,version=filename.split(';')
try:
version=int(version)
exceptValueError:
# Strange name
register_exception()
raiseInvenioBibDocFileError,"A file called %s exists under %s. This is not a valid name. After the ';' there must be an integer representing the file version. Please, manually fix this file either by renaming or by deleting it."%(filename,bibdoc.basedir)
raiseInvenioBibDocFileError,"Error in renaming '%s' to '%s': '%s'"%('%s/%s'%(bibdoc.basedir,filename),'%s/%s'%(bibdoc.basedir,new_name),e)
ifversions[version].has_key(format):
new_bibdocs.append((new_name,version))
else:
versions[version][format]=new_name
counter+=1
eliffilename[0]!='.':
# Strange name
register_exception()
raiseInvenioBibDocFileError,"A file called %s exists under %s. This is not a valid name. There should be a ';' followed by an integer representing the file version. Please, manually fix this file either by renaming or by deleting it."%(filename,bibdoc.basedir)