def getTimestamp(self, path): ''' @see ICDM.getTimestamp ''' assert isinstance(path, str), 'Invalid content path %s' % path path, entryPath = self._validatePath(path) if isdir(entryPath) or isfile(entryPath): return datetime.fromtimestamp(os.stat(entryPath).st_mtime) linkPath = entryPath repPathLen = len(self.delivery.getRepositoryPath()) while len(linkPath.lstrip(os.sep)) > repPathLen: linkFile = linkPath + self._linkExt if isfile(linkFile): subPath = entryPath[len(linkPath):].lstrip(os.sep) with open(linkFile) as f: links = json.load(f) for link in links: if link[0] == self._fsHeader and self._isValidFSLink(link, subPath): fullPath = join(link[1], subPath) if subPath else link[1] return datetime.fromtimestamp(os.stat(fullPath).st_mtime) elif link[0] == self._zipHeader and self._isValidZIPLink(link, subPath): return datetime.fromtimestamp(os.stat(link[1]).st_mtime) raise PathNotFound(path) nextLinkPath = dirname(linkPath) if nextLinkPath == linkPath: break linkPath = nextLinkPath else: raise PathNotFound(path)
def remove(self, path): ''' @see ICDM.remove ''' path, entryPath = self._validatePath(path) if isfile(entryPath.rstrip(os.sep)): return os.remove(entryPath) linkPath = entryPath repPathLen = len(self.delivery.getRepositoryPath()) while len(linkPath.lstrip(os.sep)) > repPathLen: linkFile = linkPath + self._linkExt if isfile(linkFile): subPath = entryPath[len(linkPath):].lstrip(os.sep) with open(linkFile) as f: links = json.load(f) count = 0 for link in links: if link[0] == self._fsHeader and self._isValidFSLink(link, subPath): self._removeFSLink(link, path, entryPath, subPath); count += 1 break elif link[0] == self._zipHeader and self._isValidZIPLink(link, subPath): self._removeZiplink(link, path, entryPath, subPath); count += 1 break if count == 0: raise PathNotFound(path) break nextLinkPath = dirname(linkPath) if nextLinkPath == linkPath: break linkPath = nextLinkPath else: raise PathNotFound(path) if len(subPath.strip(os.sep)) == 0 and isdir(linkPath): rmtree(linkPath)
def getTimestamp(self, path): ''' @see ICDM.getTimestamp ''' assert isinstance(path, str), 'Invalid content path %s' % path path, itemPath = self._validatePath(path) if not isdir(itemPath) and not isfile(itemPath): raise PathNotFound(path) return datetime.fromtimestamp(os.stat(itemPath).st_mtime)
def _removeFSLink(self, link, path, entryPath, subPath): ''' Removes a link to a file or directory on the file system. @param link: list Link description in JSON format. @param path: string The repository path to remove. @param entryPath: string The full path for which to create the deletion mark. @param subPath: string The path inside the linked directory (if needed) ''' if isdir(link[1]): filePath = join(link[1], subPath) if isfile(filePath) or isdir(filePath): self._createDelMark(entryPath) else: raise PathNotFound(path) elif subPath: raise PathNotFound(path)
def remove(self, path): ''' @see ICDM.remove ''' path, itemPath = self._validatePath(path) if isdir(itemPath): rmtree(itemPath) elif isfile(itemPath): os.remove(itemPath) else: raise PathNotFound(path) assert log.debug('Success removing path %s', path) or True
def republish(self, oldPath, newPath): ''' @see ICDM.republish ''' oldPath, oldFullPath = self._validatePath(oldPath) if isdir(oldFullPath): raise PathNotFound(oldPath) newPath, newFullPath = self._validatePath(newPath) if isdir(newFullPath) or isfile(newFullPath): raise ValueError('New path %s is already in use' % newPath) dstDir = dirname(newFullPath) if not isdir(dstDir): os.makedirs(dstDir) move(oldFullPath, newFullPath)
def _removeZiplink(self, link, path, entryPath, subPath): ''' Removes a link to a file or directory in a ZIP archive. @param link: list Link description in JSON format. @param path: string The repository path to remove. @param entryPath: string The full path for which to create the deletion mark. @param subPath: string The path inside the linked ZIP archive. ''' zipFilePath = normOSPath(link[1]) inFilePath = normOSPath(link[2], True) zipFile = ZipFile(zipFilePath) inZipFile = normZipPath(join(inFilePath, subPath)) if not inZipFile in zipFile.NameToInfo: raise PathNotFound(path) self._createDelMark(entryPath)
def _validatePath(self, path): path = normZipPath(path) fullPath = normOSPath(self._getItemPath(path), True) if not fullPath.startswith(self.delivery.getRepositoryPath()): raise PathNotFound(path) return (path, fullPath)