def toPython(ctrlfile): root = ctrlfile.getRoot() htmlfile = File("%s.%s" % (root, 'html')) textfile = File("%s.%s" % (root, 'txt')) try: ctrl = FileReader(ctrlfile).read() html = FileReader(htmlfile).read() text = FileReader(textfile).read() return "%s%shtml=\"\"\"%s\"\"\"%stext=\"\"\"%s\"\"\"" % ( ctrl, os.linesep, html, os.linesep, text) except (OSError, IOError) as e: raise "Reading one of files %s.(py|html|txt) failed: %s" % (root, e)
def testFileFilter(self): properties = Properties() propertyLoader = PropertyLoader(properties) file = File("project.conf") fileReader = FileReader(file) propertyLoader.load(fileReader) excludedFiles = properties.getPropertyValues('EXCLUDED_FILES') self.assertEquals(excludedFiles, ['install.sql'], "install.sql expected") # install.sql is an excluded file. filters = Filters() for excludedFile in excludedFiles: ef = File(excludedFile) ff = FileFilter(ef) filters.add(ff) file = File('test.sql') self.assertEquals(filters.accept(file), False, "test.sql is in filter.") file = File('install.sql') self.assertEquals(filters.accept(file), True, "install.sql is not in filter.")
def testUpdatePass(self): properties = Properties() propertyLoader = PropertyLoader(properties) file = File("myproject.conf") fileReader = FileReader(file) propertyLoader.load(fileReader) properties.setProperty("noora.dir", NOORA_DIR) properties.setProperty( "plugin.dir", NOORA_DIR + os.sep + "org" + os.sep + "noora" + os.sep + "plugin") properties.setProperty("current.dir", os.path.abspath('.')) properties.setProperty( "alter.dir", properties.getPropertyValue("current.dir") + os.sep + "alter") properties.setProperty( "create.dir", properties.getPropertyValue("current.dir") + os.sep + "create") #connectable=MysqlConnector() updatePlugin = UpdatePlugin() print(updatePlugin.getRevision()) options = updatePlugin.getOptions(properties) arguments = ['-h=localhost', '-e=dev', '-v=1.0.1'] parser = Parser() commandLine = parser.parse(options, arguments) parser.checkRequiredOptions() parser.checkRequiredArguments() updatePlugin.execute(commandLine, properties)
def testVersionLoader(self): properties = Properties() propertyLoader = PropertyLoader(properties) file = File("myproject.conf") fileReader = FileReader(file) propertyLoader.load(fileReader) properties.setProperty("current.dir", os.path.abspath('.')) properties.setProperty("project.file", "myproject.conf") properties.setProperty("alter.dir",Path.path(properties.getPropertyValue("current.dir"),"alter")) properties.setProperty("create.dir",Path.path(properties.getPropertyValue("current.dir"),"create")) print "current.dir",properties.getPropertyValue("current.dir") #print "alter.dir",properties.getPropertyValue("alter.dir") #print "default_version :" + properties.getPropertyValues("DEFAULT_VERSION") # a File object is not a Version object # versions = Versions() versionLoader = VersionLoader(versions) versionLoader.load(properties) versions.sort() #versions.sort() #print "versions",versions.getVersions() v = Version('1.0.1') print versions.previous(v).getValue() print versions.last().toString() print versions.next().toString() versionGuesser=VersionGuesser(properties, versions) nextVersion = versionGuesser.guess(None).toString() print versionGuesser.toFolder(nextVersion)
def testFileExtensionFilter(self): properties = Properties() propertyLoader = PropertyLoader(properties) file = File("project.conf") fileReader = FileReader(file) propertyLoader.load(fileReader) excludedExtensions = properties.getPropertyValues( 'EXCLUDED_EXTENSIONS') self.assertEquals(excludedExtensions, ['bak', '~', 'pyc', 'log'], "'bak', '~', 'pyc', 'log' expected") filters = Filters() for excludedExtension in excludedExtensions: ef = File("*." + excludedExtension) ff = FileExtensionFilter(ef) filters.add(ff) file = File('install.sql') self.assertEquals(filters.accept(file), False, "install.sql is in filter.") file = File('install.pyc') self.assertEquals(filters.accept(file), True, "install.pyc is not in filter.")
def execute(self, executable, properties): script = executable.getScript() scriptReader = FileReader(script) cp = Properties() cp.setProperty('database', executable.getDatabase()) cp.setProperty('environment', properties.getPropertyValue('environment')) cp.setProperty('previous', properties.getPropertyValue('previous')) parser = Parser(scriptReader, cp) preProcessor = PreProcessor() stream = preProcessor.parse(parser) tmp = File("tmp.sql") tmpWriter = FileWriter(tmp) tmpWriter.write(stream) tmpWriter.close() scriptReader = FileReader(tmp) feedback = File('feedback.log') feedbackWriter = FileWriter(feedback) startupInfo = StartupInfoFactory.newStartupInfo() cp = Properties() cp.setProperty(Processor.STDERR, feedbackWriter) cp.setProperty(Processor.STDIN, scriptReader) cp.setProperty(Processor.STDOUT, feedbackWriter) cp.setProperty(Processor.STARTUPINFO, startupInfo) cp.setProperty(Processor.ARGUMENT, [ "mysql", "--show-warnings", "--host=" + executable.getHost(), "--user="******"--password=" + executable.getPassword(), executable.getDatabase() ]) cp.setProperty(Processor.SHELL, False) mysqlCall = Call(cp) processor = Processor() processorResult = processor.call(mysqlCall) self.setProcessorResult(processorResult) logger = logging.getLogger('NoOraLogger') logger.info(executable.getScript())
def testPropertyLoader(self): properties = Properties() propertyLoader = PropertyLoader(properties) file = File("test.conf") fileReader = FileReader(file) propertyLoader.load(fileReader) self.assertEqual(properties.size(), 4, "invalid size") property = properties.getProperty("ORACLE_USERS") self.assertEqual(len(eval(property.getValue())), 2, "invalid size")
def execute(self, mysqlHost, mysqlDatabase, mysqlUser, mysqlPasswd, mysqlScript, paramA, paramB, ignoreErrors): #try: script = File(mysqlScript) scriptReader = FileReader(script) properties = Properties() properties.setProperty('database', mysqlDatabase) parser = Parser(scriptReader, properties) preProcessor = PreProcessor() stream = preProcessor.parse(parser) tmp = File("tmp.sql") tmpWriter = FileWriter(tmp) tmpWriter.write(stream) tmpWriter.close() scriptReader = FileReader(tmp) feedback = File('feedback.log') feedbackWriter = FileWriter(feedback) startupInfo = StartupInfoFactory.newStartupInfo() properties = Properties() properties.setProperty(Processor.STDERR, feedbackWriter) properties.setProperty(Processor.STDIN, scriptReader) properties.setProperty(Processor.STDOUT, feedbackWriter) properties.setProperty(Processor.STARTUPINFO, startupInfo) properties.setProperty(Processor.ARGUMENT, [ "mysql", "--host=" + str(mysqlHost), "--user="******"--password=" + str(mysqlPasswd), str(mysqlDatabase) ]) properties.setProperty(Processor.SHELL, False) mysqlCall = Call(properties) processor = Processor() processor.call(mysqlCall) logger = logging.getLogger('NoOraLogger') logger.info(mysqlScript)
def testProcessor(self): script = File("drop_tables.sql") scriptReader = FileReader(script) properties = Properties() properties.setProperty('database', 'orcl') parser = Parser(scriptReader, properties) preProcessor = PreProcessor() stream = preProcessor.parse(parser) self.assertEqual(stream, 'orcl', "invalid transformation")
def call(self, callable): try: shell = callable.getProperty(Processor.SHELL) stdout = callable.getProperty(Processor.STDOUT) stdin = callable.getProperty(Processor.STDIN) stderr = callable.getProperty(Processor.STDERR) startupinfo = callable.getProperty(Processor.STARTUPINFO) argument = callable.getProperty(Processor.ARGUMENT) result = subprocess.call(argument, shell=shell, stdout=stdout, stderr=stderr, stdin=stdin, startupinfo=startupinfo) if result != 0: file = stderr.getFile() reader = FileReader(file) stream = reader.read() raise ProcessorException(stream) file = stdout.getFile() reader = FileReader(file) stream = reader.read() return ProcessorResult(stream) except OSError: raise ProcessorException( "Could not execute call. Is it installed and in your path?")
def testNoOraAppPass(self): NOORA_DIR = os.path.abspath('.').split('test')[0] + "src" sys.path.append(NOORA_DIR) CURRENT_DIR = os.path.abspath('.') properties = Properties() propertyLoader = PropertyLoader(properties) properties.setProperty("noora.dir", NOORA_DIR) properties.setProperty("current.dir", CURRENT_DIR) properties.setProperty( "plugin.dir", NOORA_DIR + os.sep + 'org' + os.sep + 'noora' + os.sep + 'plugin') app = NoOraApp() file = app.getConfigFile(properties) fileReader = FileReader(file) propertyLoader.load(fileReader) classLoader = ClassLoader() options = Options() pluginManager = PluginManager(classLoader, options) pluginsProperty = properties.getProperty('PLUGINS') pluginManager.load(pluginsProperty) options = pluginManager.getOptions() arguments = [ 'drop', 'create', 'update', '-h=localhost', '-d=orcl', '-e=dev', '-v=1.0.1' ] parser = Parser() commands = parser.parse(options, arguments) for command in commands.getOptions().getOptions(): plugin = pluginManager.findByType(command.getType()) options = plugin.getOptions(properties) commandLine = parser.parse(options, arguments) parser.checkRequiredOptions() parser.checkRequiredArguments() plugin.execute(commandLine, properties)
def testCreatePass(self): properties = Properties() propertyLoader = PropertyLoader(properties) file = File("myproject.conf") fileReader = FileReader(file) propertyLoader.load(fileReader) properties.setProperty("noora.dir", NOORA_DIR) #connectable=MysqlConnector() createPlugin = CreatePlugin() print(createPlugin.getRevision()) options = createPlugin.getOptions(properties) arguments = ['-h=localhost', '-e=dev'] parser = Parser() commandLine = parser.parse(options, arguments) parser.checkRequiredOptions() parser.checkRequiredArguments() createPlugin.execute(commandLine, properties)
def testDropPass(self): NOORA_DIR = os.path.abspath('.').split('test')[0] + "src" properties = Properties() propertyLoader = PropertyLoader(properties) file = File("myproject.conf") fileReader = FileReader(file) propertyLoader.load(fileReader) properties.setProperty("noora.dir", NOORA_DIR) #connectable=MysqlConnector() dropPlugin = DropPlugin() options = dropPlugin.getOptions(properties) arguments = ['-h=localhost', '-d=orcl', '-e=dev'] parser = Parser() commandLine = parser.parse(options, arguments) dropPlugin.execute(commandLine, properties)
def testOracleConnector(self): properties = Properties() propertyLoader = PropertyLoader(properties) file = File("project.conf") fileReader = FileReader(file) propertyLoader.load(fileReader) properties.setProperty("noora.dir", NOORA_DIR) properties.setProperty("noora.script.dir", NOORA_DIR + os.sep + 'scripts') connector = OracleConnector() execute = ExecuteFactory.newOracleExecute() execute.setHost('orcl') execute.setUsername('apps') execute.setPassword('apps') file = File('application_properties.sql') execute.setScript(file) connector.execute(execute, properties)
def testFileFolderFilter(self): properties = Properties() propertyLoader = PropertyLoader(properties) file = File("project.conf") fileReader = FileReader(file) propertyLoader.load(fileReader) excludedFolders = properties.getPropertyValues('EXCLUDED_FOLDERS') self.assertEquals(excludedFolders, ['.svn', 'hotfix'], "'.svn', 'hotfix' expected") filters = Filters() for excludedFolder in excludedFolders: ef = File(excludedFolder) ff = FileFolderFilter(ef) filters.add(ff) file = File(NOORA_DIR + os.sep + 'org/noora/plugin/mysql/drop/.svn') self.assertEquals(filters.accept(file), True, ".svn is in filter.") file = File('.svn') self.assertEquals(filters.accept(file), True, ".svn is in filter.")
def testDropPluginPass(self): NOORA_DIR = os.path.abspath('.').split('test')[0] + "src" properties = Properties() propertyLoader = PropertyLoader(properties) file = File("project.conf") fileReader = FileReader(file) propertyLoader.load(fileReader) properties.setProperty("noora.dir", NOORA_DIR) properties.setProperty("noora.script.dir", NOORA_DIR + os.sep + "scripts") connectable = OracleConnectorStub() dropPlugin = PluginFactory.newOracleDropPlugin(connectable) options = dropPlugin.getOptions(properties) arguments = ['-s=orcl', '-e=dev'] parser = Parser() commandLine = parser.parse(options, arguments) dropPlugin.execute(commandLine, properties)
def testZipPass(self): properties = Properties() propertyLoader = PropertyLoader(properties) file = File("myproject.conf") fileReader = FileReader(file) propertyLoader.load(fileReader) properties.setProperty("current.dir", os.path.abspath('.')) properties.setProperty( "alter.dir", properties.getPropertyValue("current.dir") + os.sep + "alter") properties.setProperty( "create.dir", properties.getPropertyValue("current.dir") + os.sep + "create") versions = Versions() versionLoader = VersionLoader(versions) versionLoader.load(properties) versions.sort() #print versions.list() versionGuesser = VersionGuesser(properties, versions) version = versionGuesser.toFolder('1.0.0') currentFile = File(os.path.abspath('.')) current = Path.path(currentFile.getPath(), currentFile.getName()) targetPath = Path.path(current, 'target') targetFile = File(targetPath) if not targetFile.exists(): os.makedirs(targetPath) zipHandle = ZipFile( targetPath + os.sep + 'example_' + version + '.zip', 'w') excludedFolders = properties.getPropertyValues('EXCLUDED_FOLDERS') files = Files.list(currentFile, True) filters = Filters() excludedFolders = properties.getPropertyValues('EXCLUDED_FOLDERS') for excludedFolder in excludedFolders: ef = File(excludedFolder) ff = FileFolderFilter(ef) filters.add(ff) for file in files: result = False for excludedFolder in excludedFolders: if excludedFolder in file.getPath() + file.getName(): result = True break #if filters.accept(file)==False: if not result: source = Path.path(file.getPath(), file.getName()) target = source.replace(current, '') print file.getPath(), file.getName() zipHandle.write(source, target, zipfile.ZIP_DEFLATED)
properties.setProperty("noora.dir", NOORA_DIR) properties.setProperty("current.dir", CURRENT_DIR) properties.setProperty( "plugin.dir", NOORA_DIR + os.sep + 'org' + os.sep + 'noora' + os.sep + 'plugin') properties.setProperty("project.file", "pgproject.conf") properties.setProperty( "alter.dir", properties.getPropertyValue("current.dir") + os.sep + "alter") properties.setProperty( "create.dir", properties.getPropertyValue("current.dir") + os.sep + "create") app = NoOraApp() file = app.getConfigFile(properties) fileReader = FileReader(file) propertyLoader.load(fileReader) classLoader = ClassLoader() options = Options() pluginManager = PluginManager(classLoader, options) pluginsProperty = properties.getProperty('PLUGINS') pluginManager.load(pluginsProperty) options = pluginManager.getOptions() #arguments = ['drop','create','update','-h=localhost','-d=orcl','-e=dev','-v=1.0.1'] arguments = sys.argv[1:] parser = Parser() commands = parser.parse(options, arguments)
def getFileReader(self, fileName): file = File(pathName=fileName) fileReader = FileReader(file) return fileReader