Exemplo n.º 1
0
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))
Exemplo n.º 2
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
Exemplo n.º 3
0
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))
Exemplo n.º 4
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
Exemplo n.º 5
0
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
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())

Exemplo n.º 7
0
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