def deploy(source, destination, systemName=None, machineName=None): #TODO: Mugur: add comments and explain what is going one here. # THis should not be placed here a separate plugin needs to be created. assert isinstance(source, str), 'Invalid source path %s' % source assert isinstance(destination, str), 'Invalid destination path %s' % destination assert not systemName or isinstance(systemName, str), 'Invalid system name %s' % systemName assert not machineName or isinstance(machineName, str), 'Invalid machine name %s' % machineName systemName = systemName if systemName else system() machineName = machineName if machineName else machine() systems = {SYSTEM_ALL:True} if systemName == SYSTEM_ALL else {systemName:True, SYSTEM_ALL:False} machines = {MACHINE_ALL:True} if machineName == MACHINE_ALL else {machineName:True, MACHINE_ALL:False} for systemName, systemRequired in systems.items(): for machineName, machineRequired in machines.items(): srcDir = join(source, systemName, machineName) if not isdir(srcDir): try: getZipFilePath(srcDir) except IOError: if systemRequired and machineRequired: raise IOError('Cannot locate required dependency \'%s\' for system %s with architecture %s' % (srcDir, systemName, machineName)) else: continue synchronizeURIToDir(srcDir, destination)
def publishFromDir(self, path, dirPath): ''' @see ICDM.publishFromDir ''' assert isinstance(path, str) and len(path) > 0, 'Invalid content path %s' % path assert isinstance(dirPath, str), 'Invalid directory path value %s' % dirPath path, fullPath = self._validatePath(path) if not isdir(dirPath): # not a directory, see if it's a entry in a zip file zipFilePath, inDirPath = getZipFilePath(dirPath, self.delivery.getRepositoryPath()) zipFile = ZipFile(zipFilePath) if not inDirPath.endswith(ZIPSEP): inDirPath = inDirPath + ZIPSEP fileInfo = zipFile.getinfo(inDirPath) if not fileInfo.filename.endswith(ZIPSEP): raise IOError('Trying to publish a file from a ZIP directory path: %s' % fileInfo.filename) self._copyZipDir(zipFilePath, inDirPath, fullPath) assert log.debug('Success publishing ZIP dir %s (%s) to path %s', inDirPath, zipFilePath, path) or True return dirPath = normpath(dirPath) assert os.access(dirPath, os.R_OK), 'Unable to read the directory path %s' % dirPath for root, _dirs, files in os.walk(dirPath): relPath = relpath(root, dirPath) for file in files: publishPath = join(path, relPath.lstrip(os.sep), file) filePath = join(root, file) self.publishFromFile(publishPath, filePath) assert log.debug('Success publishing directory %s to path %s', dirPath, path) or True
def publishFromFile(self, path, filePath): ''' @see ICDM.publishFromFile ''' assert isinstance(path, str) and len(path) > 0, 'Invalid content path %s' % path if not isinstance(filePath, str) and hasattr(filePath, 'read'): return self._publishFromFileObj(path, filePath) assert isinstance(filePath, str), 'Invalid file path value %s' % filePath path, dstFilePath = self._validatePath(path) dstDir = dirname(dstFilePath) if not isdir(dstDir): os.makedirs(dstDir) if not isfile(filePath): # not a file, see if it's a entry in a zip file zipFilePath, inFilePath = getZipFilePath(filePath, self.delivery.getRepositoryPath()) zipFile = ZipFile(zipFilePath) fileInfo = zipFile.getinfo(inFilePath) if fileInfo.filename.endswith(ZIPSEP): raise IOError('Trying to publish a file from a ZIP directory path: %s' % fileInfo.filename) if not self._isSyncFile(zipFilePath, dstFilePath): copyfileobj(zipFile.open(inFilePath), open(dstFilePath, 'w+b')) assert log.debug('Success publishing ZIP file %s (%s) to path %s', inFilePath, zipFilePath, path) or True return assert os.access(filePath, os.R_OK), 'Unable to read the file path %s' % filePath if not self._isSyncFile(filePath, dstFilePath): copyfile(filePath, dstFilePath) assert log.debug('Success publishing file %s to path %s', filePath, path) or True
def publishFromDir(self, path, dirPath): ''' @see ICDM.publishFromDir ''' assert isinstance( path, str) and len(path) > 0, 'Invalid content path %s' % path assert isinstance(dirPath, str), 'Invalid directory path value %s' % dirPath path, fullPath = self._validatePath(path) if not isdir(dirPath): # not a directory, see if it's a entry in a zip file zipFilePath, inDirPath = getZipFilePath( dirPath, self.delivery.getRepositoryPath()) if not inDirPath.endswith(ZIPSEP): inDirPath = inDirPath + ZIPSEP self._copyZipDir(zipFilePath, inDirPath, fullPath) assert log.debug('Success publishing ZIP dir %s (%s) to path %s', inDirPath, zipFilePath, path) or True return dirPath = normpath(dirPath) assert os.access( dirPath, os.R_OK), 'Unable to read the directory path %s' % dirPath for root, _dirs, files in os.walk(dirPath): relPath = relpath(root, dirPath) for file in files: publishPath = join(normOSPath(path), relPath.lstrip(os.sep), file) filePath = join(root, file) self.publishFromFile(publishPath, filePath) assert log.debug('Success publishing directory %s to path %s', dirPath, path) or True
def synchronizeURIToDir(path, dirPath): ''' Publishes the entire contents from the URI path to the provided directory path. @param path: string The path to a resource: a file system path, a ZIP path @param dirPath: string The directory path to synchronize with. ''' assert isinstance(path, str) and path, 'Invalid content path %s' % path assert isinstance(dirPath, str), 'Invalid directory path value %s' % dirPath if not isdir(path): # not a directory, see if it's a entry in a zip file zipFilePath, inDirPath = getZipFilePath(path) zipFile = ZipFile(zipFilePath) if not inDirPath.endswith(ZIPSEP): inDirPath = inDirPath + ZIPSEP tmpDir = TemporaryDirectory() lenPath, zipTime = len(inDirPath), datetime.fromtimestamp(stat(zipFilePath).st_mtime) for zipInfo in zipFile.filelist: assert isinstance(zipInfo, ZipInfo), 'Invalid zip info %s' % zipInfo if zipInfo.filename.startswith(inDirPath): if zipInfo.filename[0] == '/': dest = zipInfo.filename[1:] else: dest = zipInfo.filename dest = normpath(join(dirPath, dest[lenPath:])) if exists(dest) and zipTime <= datetime.fromtimestamp(stat(dest).st_mtime): continue destDir = dirname(dest) if not exists(destDir): makedirs(destDir) zipFile.extract(zipInfo.filename, tmpDir.name) move(join(tmpDir.name, normOSPath(zipInfo.filename)), dest) if zipInfo.filename.endswith('.exe'): os.chmod(dest, stat(dest).st_mode | S_IEXEC) return path = normpath(path) assert os.access(path, os.R_OK), 'Unable to read the directory path %s' % path lenPath = len(path) + 1 for root, _dirs, files in os.walk(path): for file in files: src, dest = join(root, file), join(dirPath, root[lenPath:], file) if exists(dest) and \ datetime.fromtimestamp(stat(src).st_mtime) <= datetime.fromtimestamp(stat(dest).st_mtime): continue destDir = dirname(dest) if not exists(destDir): makedirs(destDir) copy(src, dest) if file.endswith('.exe'): os.chmod(dest, stat(dest).st_mode | S_IEXEC)
def timestampURI(path): ''' Returns the last modified time stamp for the given path. @param path: string The path to a resource: a file system path, a ZIP path @return: datetime The last modified time stamp. ''' path = normOSPath(path) if isfile(path): return datetime.fromtimestamp(stat(path).st_mtime) zipFilePath, _inZipPath = getZipFilePath(path) return datetime.fromtimestamp(stat(zipFilePath).st_mtime)
def deploy(source, destination, systemName=None, machineName=None): ''' Deploys the tool found at the source directory to the destination directory. @param source: src The directory from which to deploy the tool @param destination: src The path where to deploy the tool @param systemName: src The name of the system for which to deploy (e.g. Darwin, Windows, Ubuntu) @param machineName: src The machine name (e.g.: x86, x86_64) @return: tuple Tuple containing the system information (name, release, version) and True if the tool was deployed, False if it wasn't ''' # TODO: This should not be placed here a separate plugin needs to be created. assert isinstance(source, str), 'Invalid source path %s' % source assert isinstance(destination, str), 'Invalid destination path %s' % destination assert not systemName or isinstance( systemName, str), 'Invalid system name %s' % systemName assert not machineName or isinstance( machineName, str), 'Invalid machine name %s' % machineName if not systemName: systemName, rel, ver = systemInfo() machineName = machineName if machineName else machine() systems = (SYSTEM_ALL) if systemName == SYSTEM_ALL else (systemName, SYSTEM_ALL) machines = (MACHINE_ALL) if machineName == MACHINE_ALL else (machineName, MACHINE_ALL) deployed = False for systemName in systems: for machineName in machines: srcDir = join(source, systemName, machineName) if not isdir(srcDir): try: zipPath, inPath = getZipFilePath(srcDir) validateInZipPath(ZipFile(zipPath), inPath + ZIPSEP) except (IOError, KeyError): continue synchronizeURIToDir(srcDir, destination) deployed = True return (systemName, rel, ver, deployed)
def openURI(path): """ Returns a read file object for the given path. @param path: string The path to a resource: a file system path, a ZIP path @return: byte file A file like object that delivers bytes. """ path = normOSPath(path) if isfile(path): return open(path, "rb") zipFilePath, inZipPath = getZipFilePath(path) zipFile = ZipFile(zipFilePath) if inZipPath in zipFile.NameToInfo and not inZipPath.endswith(ZIPSEP) and inZipPath != "": return zipFile.open(inZipPath) raise IOError("Invalid file path %s" % path)
def _publishFromFile(self, path, filePath): assert isinstance(path, str) and len(path) > 0, 'Invalid content path %s' % path assert isinstance(filePath, str), 'Invalid file path value %s' % filePath dstDir = dirname(self._getItemPath(path)) if not isdir(dstDir): os.makedirs(dstDir) if isfile(filePath) or isdir(filePath): filePath = normpath(filePath) assert os.access(filePath, os.R_OK), 'Unable to read file path %s' % filePath self._createLinkToFileOrDir(path, filePath) return # not a file, see if it's a entry in a zip file zipFilePath, inFilePath = getZipFilePath(filePath, self.delivery.getRepositoryPath()) assert isfile(zipFilePath) and os.access(zipFilePath, os.R_OK), \ 'Unable to read file path %s' % filePath zipFile = ZipFile(zipFilePath) validateInZipPath(zipFile, inFilePath) self._createLinkToZipFile(path, zipFilePath, inFilePath)
def openURI(path, byteMode=True): ''' Returns a read file object for the given path. @param path: string The path to a resource: a file system path, a ZIP path @return: byte file A file like object that delivers bytes. ''' path = normOSPath(path) mode = 'rb' if byteMode else 'rt' if isfile(path): return open(path, mode) zipFilePath, inZipPath = getZipFilePath(path) zipFile = ZipFile(zipFilePath) if inZipPath in zipFile.NameToInfo and not inZipPath.endswith(ZIPSEP) and inZipPath != '': f = zipFile.open(inZipPath) if byteMode: return f else: return StringIO(f.read().decode()) raise IOError('Invalid file path %s' % path)
def openURI(path, byteMode=True): ''' Returns a read file object for the given path. @param path: string The path to a resource: a file system path, a ZIP path @return: byte file A file like object that delivers bytes. ''' path = normOSPath(path) mode = 'rb' if byteMode else 'rt' if isfile(path): return open(path, mode) zipFilePath, inZipPath = getZipFilePath(path) zipFile = ZipFile(zipFilePath) if inZipPath in zipFile.NameToInfo and not inZipPath.endswith( ZIPSEP) and inZipPath != '': f = zipFile.open(inZipPath) if byteMode: return f else: return StringIO(f.read().decode()) raise IOError('Invalid file path %s' % path)
def deploy(source, destination, systemName=None, machineName=None): ''' Deploys the tool found at the source directory to the destination directory. @param source: src The directory from which to deploy the tool @param destination: src The path where to deploy the tool @param systemName: src The name of the system for which to deploy (e.g. Darwin, Windows, Ubuntu) @param machineName: src The machine name (e.g.: x86, x86_64) @return: tuple Tuple containing the system information (name, release, version) and True if the tool was deployed, False if it wasn't ''' # TODO: This should not be placed here a separate plugin needs to be created. assert isinstance(source, str), 'Invalid source path %s' % source assert isinstance(destination, str), 'Invalid destination path %s' % destination assert not systemName or isinstance(systemName, str), 'Invalid system name %s' % systemName assert not machineName or isinstance(machineName, str), 'Invalid machine name %s' % machineName if not systemName: systemName, rel, ver = systemInfo() machineName = machineName if machineName else machine() systems = (SYSTEM_ALL) if systemName == SYSTEM_ALL else (systemName, SYSTEM_ALL) machines = (MACHINE_ALL) if machineName == MACHINE_ALL else (machineName, MACHINE_ALL) deployed = False for systemName in systems: for machineName in machines: srcDir = join(source, systemName, machineName) if not isdir(srcDir): try: zipPath, inPath = getZipFilePath(srcDir) validateInZipPath(ZipFile(zipPath), inPath + ZIPSEP) except (IOError, KeyError): continue synchronizeURIToDir(srcDir, destination) deployed = True return (systemName, rel, ver, deployed)
def synchronizeURIToDir(path, dirPath): ''' Publishes the entire contents from the URI path to the provided directory path. @param path: string The path to a resource: a file system path, a ZIP path @param dirPath: string The directory path to synchronize with. ''' assert isinstance(path, str) and path, 'Invalid content path %s' % path assert isinstance(dirPath, str), 'Invalid directory path value %s' % dirPath if not isdir(path): # not a directory, see if it's a entry in a zip file zipFilePath, inDirPath = getZipFilePath(path) zipFile = ZipFile(zipFilePath) if not inDirPath.endswith(ZIPSEP): inDirPath = inDirPath + ZIPSEP tmpDir = TemporaryDirectory() lenPath, zipTime = len(inDirPath), datetime.fromtimestamp( stat(zipFilePath).st_mtime) for zipInfo in zipFile.filelist: assert isinstance(zipInfo, ZipInfo), 'Invalid zip info %s' % zipInfo if zipInfo.filename.startswith(inDirPath): if zipInfo.filename[0] == '/': dest = zipInfo.filename[1:] else: dest = zipInfo.filename dest = normpath(join(dirPath, dest[lenPath:])) if exists(dest) and zipTime <= datetime.fromtimestamp( stat(dest).st_mtime): continue destDir = dirname(dest) if not exists(destDir): makedirs(destDir) zipFile.extract(zipInfo.filename, tmpDir.name) move(join(tmpDir.name, normOSPath(zipInfo.filename)), dest) if zipInfo.filename.endswith('.exe'): os.chmod(dest, stat(dest).st_mode | S_IEXEC) return path = normpath(path) assert os.access(path, os.R_OK), 'Unable to read the directory path %s' % path lenPath = len(path) + 1 for root, _dirs, files in os.walk(path): for file in files: src, dest = join(root, file), join(dirPath, root[lenPath:], file) if exists(dest) and \ datetime.fromtimestamp(stat(src).st_mtime) <= datetime.fromtimestamp(stat(dest).st_mtime): continue destDir = dirname(dest) if not exists(destDir): makedirs(destDir) copy(src, dest) if file.endswith('.exe'): os.chmod(dest, stat(dest).st_mode | S_IEXEC)