def testDir(self): d = Directory() assert (os.path.isdir(d.path)) num = 10 for x in range(num): (fd, name) = mkstemp(dir=d.path) with os.fdopen(fd, 'w') as f: f.write(self.makeRandomContents()) d.scan() self.assertEqual(num, len(d.files)) for x in range(num): d.createFile(name=self.makeRandomContents(10)) d.createFile(path=self.makeRandomContents(10), name=self.makeRandomContents(10)) # Create a file that already exists d.createFile(pathName="clobber1") self.assertRaises(FileClobberError, d.createFile, pathName="clobber1") self.assertEqual(num * 3 + 1, len(d.files)) files = d.files[:] removeIndexes = list(range(0, len(files), 3)) [files[i].delete() for i in removeIndexes] self.assertEqual(len(d.files), len(files) - len(removeIndexes)) dirPath = d.path d = None self.assertFalse(os.path.isdir(dirPath)) print("COMPLETED makeRandomContents")
def testCreateLinuxBadPaths(self): d = Directory() self.assertEqual(d.createFile(pathName="/abspath/name1").pathName, 'abspath/name1') self.assertEqual(d.createFile(pathName="relpath/name2").pathName, "relpath/name2") self.assertRaises(AssertionError, d.createFile, pathName="/abspath/dir1/") self.assertRaises(AssertionError, d.createFile, pathName="relpath/dir2/") self.assertEqual(2, len(d.files)) print("COMPLETED testCreateLinuxBadPaths")
def testCreateWindowsBadPaths(self): d = Directory() self.assertEqual(d.createFile(pathName="\\abspath\\name1").pathName, 'abspath\\name1') self.assertEqual(d.createFile(pathName="relpath\\name2").pathName, "relpath\\name2") self.assertRaises(AssertionError, d.createFile, pathName="\\abspath\\dir1\\") self.assertRaises(AssertionError, d.createFile, pathName="relpath\\dir2\\") self.assertEqual(2, len(d.files)) print("COMPLETED testCreateWindowsBadPaths")
def _testPackageUpdate(self, directory: Directory) -> None: """ Test Package Update :param directory: The directory where the packages are extracted Since we ARE running on the server, we will test install these packages here first, this is done by creating a virtualenv. There isn't a lot of testing for the release at this stage. Currently we just use pip to try and install the packages off line, if it's happy we're happy. """ # Create the test virtualenv virtualEnvDir = Directory() virtualenv.main(['--site-packages', virtualEnvDir]) # Install all the packages from the directory args = [ 'install', # Install the packages '--ignore-installed ', # Reinstall if they already exist '--no-cache-dir', # Don't use the local pip cache '--no-index', # Work offline, don't use pypi '--find-links', directory.path, # Look in the directory for dependencies [f.name for f in directory] ] # We could use import pip, pip.main(..), except: # We want to capture, the output, and: # we can't tell it to use the virtualenv pipExec = os.path.join(virtualEnvDir.path, 'bin', 'pip') commandComplete = subprocess.run(['pip'] + args, executable=pipExec, stdout=PIPE, stderr=PIPE, shell=True) if commandComplete.returncode: raise PlatformInstallException( "Package install test failed", output=commandComplete.stdout.decode(), error=commandComplete.stderr.decode()) # Continue normally if it all succeeded logger.debug("Peek update successfully tested.")
def createApiDocs(modFileName): moduleName = os.path.basename(os.path.dirname(modFileName)) rootpath = os.path.abspath(os.path.dirname(modFileName)) realDstDir = os.path.join(os.path.dirname(__file__), 'doc_link', moduleName + "_api") tmpDir = Directory() opts = _Opts() opts.destdir = tmpDir.path if not os.path.isdir(opts.destdir): os.makedirs(opts.destdir) # modules = recurse_tree(rootpath, [], opts) # create_modules_toc_file(modules, opts) # Incrementally update files _syncFiles(tmpDir.path, realDstDir)
def makeRandomDirectory(cls): directory = Directory() dirs = [''] def addRecursiveDirs(path): if len(dirs) > 20: return for d in range(5): newPath = os.path.join(path, cls.makeRandomContents(10)) # print "Creating new path %s" % newPath dirs.append(newPath) addRecursiveDirs(newPath) for x in range(10): f = directory.createFile(path=newPath, name=cls.makeRandomContents(10)) with f.open(write=True) as fobj: fobj.write(cls.makeRandomContents(4000)) addRecursiveDirs('') return directory
def updateToTarFile(self, newSoftwareTar): dirName = tarfile.open(newSoftwareTar.name).getnames()[0] directory = Directory() try: with tarfile.open(newSoftwareTar.name) as tar: tar.extract("%s/%s" % (dirName, self.PAPP_VERSION_JSON), directory.path) except KeyError as e: raise Exception("Uploaded archive does not contain a Peek App sw_upload, %s" % e.message) directory.scan() pappVersion = directory.getFile(path=dirName, name=self.PAPP_VERSION_JSON) if '/' in pappVersion.path: raise Exception("Expected %s to be one level down, it's at %s" % (self.PAPP_VERSION_JSON, pappVersion.path)) # Example """ { "title": "Peek App - Noop", "name": "papp_noop", "company": "Synerty Pty Ltd", "website": "www.synerty.com", "version": "#PAPP_VER#", "buildNumber": "#PAPP_BUILD#", "buildDate": "#BUILD_DATE#" } """ peekAppInfo = PeekAppInfo() peekAppInfo.fileName = "%s.tar.bz2" % dirName peekAppInfo.dirName = dirName with pappVersion.open() as f: versionJson = json.load(f) peekAppInfo.title = versionJson["title"] peekAppInfo.name = versionJson["name"] peekAppInfo.creator = versionJson["creator"] peekAppInfo.website = versionJson["website"] peekAppInfo.version = versionJson["version"] peekAppInfo.buildNumber = versionJson["buildNumber"] peekAppInfo.buildDate = versionJson["buildDate"] pappName, pappVersion = peekAppInfo.name, peekAppInfo.version if not dirName.startswith(peekAppInfo.name): raise Exception("Peek app name '%s' does not match peek root dir name '%s" % (peekAppInfo.name, dirName)) newPath = os.path.join(peekServerConfig.pappSoftwarePath, dirName) # Install the TAR file newSoftwareTar.delete = False fullNewTarPath = os.path.join(peekServerConfig.pappSoftwarePath, peekAppInfo.fileName) shutil.move(newSoftwareTar.name, fullNewTarPath) from peek_server.storage import dbConn session = dbConn.ormSession existing = (session.query(PeekAppInfo) .filter(PeekAppInfo.name == peekAppInfo.name, PeekAppInfo.version == peekAppInfo.version) .all()) if existing: peekAppInfo.id = existing[0].id session.merge(peekAppInfo) else: session.add(peekAppInfo) session.commit() session.expunge_all() session.close() return pappName, pappVersion, fullNewTarPath
def updateToTarFile(self, newSoftwareTar): """ Update To Tar File This method inspects the tar file and finally extracts it to the platform path. """ directory = Directory() tarfile.open(newSoftwareTar).extractall(directory.path) directory.scan() self._testPackageUpdate(directory) platformVersionFile = [ f for f in directory.files if f.name == self.PEEK_PLATFORM_VERSION_JSON ] if len(platformVersionFile) != 1: raise Exception( "Uploaded archive does not contain a Peek Platform update" ", Expected 1 %s, got %s" % (self.PEEK_PLATFORM_VERSION_JSON, len(platformVersionFile))) platformVersionFile = platformVersionFile[0] if '/' in platformVersionFile.path: raise Exception( "Expected %s to be one level down, it's at %s" % (self.PEEK_PLATFORM_VERSION_JSON, platformVersionFile.path)) newVersionDir = platformVersionFile.path newVersion = newVersionDir.replace("peek_platform_", "") serverTarFile = newVersionDir.replace('_platform_', '_server_') + '.tar.bz2' agentTarFile = newVersionDir.replace('_platform_', '_agent_') + '.tar.bz2' workerTarFile = newVersionDir.replace('_platform_', '_worker_') + '.tar.bz2' if not directory.getFile(path=newVersionDir, name=serverTarFile): raise Exception( "Peek server software is missing from the platform update." " %s is missing." % serverTarFile) if not directory.getFile(path=newVersionDir, name=agentTarFile): raise Exception( "Peek server platform is missing from the platform update." " %s is missing." % agentTarFile) if not directory.getFile(path=newVersionDir, name=workerTarFile): raise Exception( "Peek server worker is missing from the platform update." " %s is missing." % workerTarFile) newPath = os.path.join(PeekPlatformConfig.config.platformSoftwarePath, newVersionDir) # Do we really need to keep the old version if it's the same build? if os.path.exists(newPath): shutil.rmtree(newPath) shutil.move(os.path.join(directory.path, newVersionDir), newPath) return newVersion