def checkDiscoveryResources(mapingFilesListFileName, userExtDir, localFramework, intermediatesDir): try: initialMappingFileNameList = [] mappingFileNameList = [] ## Get mapping file list mappingFilesListFile = File(mapingFilesListFileName) if mappingFilesListFile.exists() and mappingFilesListFile.canRead(): initialMappingFileNameList = getMappingFileNames(mapingFilesListFileName) if initialMappingFileNameList == None or len(initialMappingFileNameList) < 1: excInfo = ('No mapping files found in <%s>' % mapingFilesListFileName) localFramework.reportError(excInfo) logger.error(excInfo) return None else: excInfo = ('Error reading file <%s>' % mapingFilesListFileName) localFramework.reportError(excInfo) logger.error(excInfo) return None ## Make sure that at least one of the mapping files in the list above ## exists and is readable mappingFileExists = 'false' for mappingFileName in initialMappingFileNameList: mappingFileAbsolutePath = userExtDir + 'data\\' + mappingFileName + '.xml' mappingFile = File(mappingFileAbsolutePath) if mappingFile.exists() and mappingFile.canRead(): debugPrint(4, '[' + SCRIPT_NAME + ':checkDiscoveryResources] Mapping file <%s> found!' % mappingFileAbsolutePath) mappingFileExists = 'true' ## Add file with path to mappingFileNameList #mappingFileNameList.append(mappingFileAbsolutePath) mappingFileNameList.append(mappingFileName) else: logger.info('Mapping file <%s> NOT found!' % mappingFileAbsolutePath) if mappingFileExists == 'false': excInfo = 'Error reading mapping file(s)!' localFramework.reportError(excInfo) logger.warn(excInfo) return None ## Make sure intermediates directory exists and is writable intermediatesDirectory = File(intermediatesDir) if intermediatesDirectory.exists() and intermediatesDirectory.canRead() and intermediatesDirectory.canWrite(): debugPrint(5, '[' + SCRIPT_NAME + ':checkDiscoveryResources] Intermediates directory <%s> present and writable!' % intermediatesDir) ## Clean up intermediate XML directory ## TODO remove cleanUpDirectory(intermediatesDir) else: excInfo = ('Intermediates directory <%s> not found or is read-only' % intermediatesDir) localFramework.reportError(excInfo) logger.warn(excInfo) return None ## If we made it this far, all resources are good return mappingFileNameList except: excInfo = logger.prepareJythonStackTrace('') debugPrint('[' + SCRIPT_NAME + ':checkDiscoveryResources] Exception: <%s>' % excInfo) pass
def stat(path): """stat(path) -> stat result Perform a stat system call on the given path. The Java stat implementation only returns a small subset of the standard fields: size, modification time and change time. """ abs_path = sys.getPath(path) try: return stat_result.from_jnastat(_posix.stat(abs_path)) except NotImplementedError: pass except: raise f = File(abs_path) if not f.exists(): raise OSError(errno.ENOENT, strerror(errno.ENOENT), path) size = f.length() mtime = f.lastModified() / 1000.0 mode = 0 if f.isDirectory(): mode = _stat.S_IFDIR elif f.isFile(): mode = _stat.S_IFREG if f.canRead(): mode = mode | _stat.S_IREAD if f.canWrite(): mode = mode | _stat.S_IWRITE return stat_result((mode, 0, 0, 0, 0, 0, size, mtime, mtime, 0))
def access(path, mode): """access(path, mode) -> True if granted, False otherwise Use the real uid/gid to test for access to a path. Note that most operations will use the effective uid/gid, therefore this routine can be used in a suid/sgid environment to test if the invoking user has the specified access to the path. The mode argument can be F_OK to test existence, or the inclusive-OR of R_OK, W_OK, and X_OK. """ if not isinstance(mode, (int, long)): raise TypeError('an integer is required') f = File(sys.getPath(path)) result = True if not f.exists(): result = False if mode & R_OK and not f.canRead(): result = False if mode & W_OK and not f.canWrite(): result = False if mode & X_OK: # NOTE: always False without jna-posix stat try: result = (stat(path).st_mode & _stat.S_IEXEC) != 0 except OSError: result = False return result
def stat(path): """stat(path) -> stat result Perform a stat system call on the given path. The Java stat implementation only returns a small subset of the standard fields: size, modification time and change time. """ abs_path = sys.getPath(path) try: return stat_result.from_jnastat(_posix.stat(abs_path)) except NotImplementedError: pass except: raise f = File(abs_path) if not f.exists(): raise OSError(errno.ENOENT, errno.strerror(errno.ENOENT), path) size = f.length() mtime = f.lastModified() / 1000.0 mode = 0 if f.isDirectory(): mode = _stat.S_IFDIR elif f.isFile(): mode = _stat.S_IFREG if f.canRead(): mode = mode | _stat.S_IREAD if f.canWrite(): mode = mode | _stat.S_IWRITE return stat_result((mode, 0, 0, 0, 0, 0, size, mtime, mtime, 0))
def DiscoveryMain(Framework): logger.info('Start Phase 1 ... Pull from ARIS') # Set global framework global theFramework theFramework = Framework ## Make sure we have an input data file from ARIS ARISfileName = Framework.getParameter('ARIS_XML_file') or None ARISfile = File(ARISfileName) if not (ARISfile and ARISfile.exists() and ARISfile.canRead()): excInfo = ('ARIS XML input file is not specified or is invalid!') Framework.reportError(excInfo) logger.error(excInfo) return None ## Check that the language parameter is set - default to US English requestedLocaleID = Framework.getParameter('ARISLocaleId') or '&LocaleId.USen;' if not requestedLocaleID: logger.warn('ARIS LocaleID parameter is not set...defaulting to US English') requestedLocaleID = '&LocaleId.USen;' # File and directory names userExtDir = CollectorsParameters.BASE_PROBE_MGR_DIR + CollectorsParameters.getDiscoveryResourceFolder() + '\\TQLExport\\ARIS\\' intermediatesDir = userExtDir + 'inter\\' mapingFilesListFileName = userExtDir + 'tqls.txt' mappingFileNameList = checkDiscoveryResources(mapingFilesListFileName, userExtDir, Framework, intermediatesDir) if not mappingFileNameList: return None ## Get attribute names from mapping file(s) ## This is a list of extended attributes to be retrieved from ARIS for mappingFileName in mappingFileNameList: (requestedSourceObjectTypeList, requestedSourceRelationshipTypeList) = getMapping(userExtDir + 'data\\' + mappingFileName + '.xml') if requestedSourceObjectTypeList and requestedSourceRelationshipTypeList: arisObjectMap = processARISXML(ARISfile, requestedSourceObjectTypeList, requestedSourceRelationshipTypeList, requestedLocaleID) intermediateXmlDoc = None if arisObjectMap: intermediateXmlDoc = buildIntermediateXML(arisObjectMap) intermediateXmlLocation = intermediatesDir + mappingFileName + '.xml' else: Framework.reportWarning('No CIs found in the ARIS XML file') if intermediateXmlDoc: try: xmlOutputter = XMLOutputter() xmlOutputter.output(intermediateXmlDoc, FileOutputStream(intermediateXmlLocation)) except: excInfo = logger.prepareJythonStackTrace('') Framework.reportError('Error writing intermediate file: <%s>' % intermediateXmlLocation) logger.warn('[' + SCRIPT_NAME + ':DiscoveryMain] Exception: <%s>' % excInfo) pass else: Framework.reportWarning('Error creating intermediate XML') else: logger.warn('[' + SCRIPT_NAME + ':DiscoveryMain] Unable to process mapping file: <%s>' % mappingFileName) Framework.reportError(' Unable to process mapping file: <%s>' % mappingFileName) logger.info('End Phase 1.... Pull from ARIS')
def test_inputs(test_name): spec_file = File(SPEC_DIR, test_name + ".spec") inputs_file = File(INPUT_DIR, test_name + ".vm") # If the inputs do not exist, generate them # If the inputs are outdated, update them if the user wants to if ((inputs_file.canRead() and UPDATE_INPUTS_ON_SPEC_CHANGE and spec_file.canRead() and is_newer_than(spec_file, inputs_file)) or not inputs_file.canRead()): cmd = TestGeneratorCommand() cmd.parseArgs([ spec_file.getAbsolutePath(), str(NUMBER_INPUTS_ON_SPEC_CHANGE), "--output", inputs_file.getAbsolutePath() ]) cmd.run() return build_velocity_context(inputs_file)
from java.nio.file import Paths from java.nio.file import Path from java.io import File p=Paths.get("home","pi","Desktop","jython-prac-prog","stram_jython","file2.txt") x = p.toFile() x = File(x.getName()) print (x.isFile()) f = File("image1.png") print (f.getName()) print ("length",f.length()) print ("Execute",f.canExecute()) print ("read",f.canRead()) print ("write",f.canWrite()) print ("path",f.getPath()) print ("Directory",f.isDirectory()) print ("parent",f.getParent())
def checkDiscoveryResources(mapingFilesListFileName, userExtDir, localFramework, intermediatesDir): try: initialMappingFileNameList = [] mappingFileNameList = [] ## Get mapping file list mappingFilesListFile = File(mapingFilesListFileName) if mappingFilesListFile.exists() and mappingFilesListFile.canRead(): initialMappingFileNameList = getMappingFileNames( mapingFilesListFileName) if initialMappingFileNameList == None or len( initialMappingFileNameList) < 1: excInfo = ('No mapping files found in <%s>' % mapingFilesListFileName) localFramework.reportError(excInfo) logger.error(excInfo) return None else: excInfo = ('Error reading file <%s>' % mapingFilesListFileName) localFramework.reportError(excInfo) logger.error(excInfo) return None ## Make sure that at least one of the mapping files in the list above ## exists and is readable mappingFileExists = 'false' for mappingFileName in initialMappingFileNameList: mappingFileAbsolutePath = userExtDir + 'data\\' + mappingFileName + '.xml' mappingFile = File(mappingFileAbsolutePath) if mappingFile.exists() and mappingFile.canRead(): debugPrint( 4, '[' + SCRIPT_NAME + ':checkDiscoveryResources] Mapping file <%s> found!' % mappingFileAbsolutePath) mappingFileExists = 'true' ## Add file with path to mappingFileNameList #mappingFileNameList.append(mappingFileAbsolutePath) mappingFileNameList.append(mappingFileName) else: logger.info('Mapping file <%s> NOT found!' % mappingFileAbsolutePath) if mappingFileExists == 'false': excInfo = 'Error reading mapping file(s)!' localFramework.reportError(excInfo) logger.warn(excInfo) return None ## Make sure intermediates directory exists and is writable intermediatesDirectory = File(intermediatesDir) if intermediatesDirectory.exists() and intermediatesDirectory.canRead( ) and intermediatesDirectory.canWrite(): debugPrint( 5, '[' + SCRIPT_NAME + ':checkDiscoveryResources] Intermediates directory <%s> present and writable!' % intermediatesDir) ## Clean up intermediate XML directory ## TODO remove cleanUpDirectory(intermediatesDir) else: excInfo = ( 'Intermediates directory <%s> not found or is read-only' % intermediatesDir) localFramework.reportError(excInfo) logger.warn(excInfo) return None ## If we made it this far, all resources are good return mappingFileNameList except: excInfo = logger.prepareJythonStackTrace('') debugPrint('[' + SCRIPT_NAME + ':checkDiscoveryResources] Exception: <%s>' % excInfo) pass
def DiscoveryMain(Framework): logger.info('Start Phase 1 ... Pull from ARIS') # Set global framework global theFramework theFramework = Framework ## Make sure we have an input data file from ARIS ARISfileName = Framework.getParameter('ARIS_XML_file') or None ARISfile = File(ARISfileName) if not (ARISfile and ARISfile.exists() and ARISfile.canRead()): excInfo = ('ARIS XML input file is not specified or is invalid!') Framework.reportError(excInfo) logger.error(excInfo) return None ## Check that the language parameter is set - default to US English requestedLocaleID = Framework.getParameter( 'ARISLocaleId') or '&LocaleId.USen;' if not requestedLocaleID: logger.warn( 'ARIS LocaleID parameter is not set...defaulting to US English') requestedLocaleID = '&LocaleId.USen;' # File and directory names userExtDir = CollectorsParameters.BASE_PROBE_MGR_DIR + CollectorsParameters.getDiscoveryResourceFolder( ) + '\\TQLExport\\ARIS\\' intermediatesDir = userExtDir + 'inter\\' mapingFilesListFileName = userExtDir + 'tqls.txt' mappingFileNameList = checkDiscoveryResources(mapingFilesListFileName, userExtDir, Framework, intermediatesDir) if not mappingFileNameList: return None ## Get attribute names from mapping file(s) ## This is a list of extended attributes to be retrieved from ARIS for mappingFileName in mappingFileNameList: (requestedSourceObjectTypeList, requestedSourceRelationshipTypeList) = getMapping(userExtDir + 'data\\' + mappingFileName + '.xml') if requestedSourceObjectTypeList and requestedSourceRelationshipTypeList: arisObjectMap = processARISXML( ARISfile, requestedSourceObjectTypeList, requestedSourceRelationshipTypeList, requestedLocaleID) intermediateXmlDoc = None if arisObjectMap: intermediateXmlDoc = buildIntermediateXML(arisObjectMap) intermediateXmlLocation = intermediatesDir + mappingFileName + '.xml' else: Framework.reportWarning('No CIs found in the ARIS XML file') if intermediateXmlDoc: try: xmlOutputter = XMLOutputter() xmlOutputter.output( intermediateXmlDoc, FileOutputStream(intermediateXmlLocation)) except: excInfo = logger.prepareJythonStackTrace('') Framework.reportError( 'Error writing intermediate file: <%s>' % intermediateXmlLocation) logger.warn('[' + SCRIPT_NAME + ':DiscoveryMain] Exception: <%s>' % excInfo) pass else: Framework.reportWarning('Error creating intermediate XML') else: logger.warn( '[' + SCRIPT_NAME + ':DiscoveryMain] Unable to process mapping file: <%s>' % mappingFileName) Framework.reportError(' Unable to process mapping file: <%s>' % mappingFileName) logger.info('End Phase 1.... Pull from ARIS')