示例#1
0
def _gethostbyaddr(name):
    # This is as close as I can get; at least the types are correct...
    addresses = java.net.InetAddress.getAllByName(gethostbyname(name))
    names = []
    addrs = []
    for addr in addresses:
      names.append(asPyString(addr.getHostName()))
      addrs.append(asPyString(addr.getHostAddress()))
    return (names, addrs)
示例#2
0
def normcase(path):
    """Normalize case of pathname.

    XXX Not done right under JDK.

    """
    path = _tostr(path, "normcase")
    return asPyString(File(path).getPath())
示例#3
0
def expanduser(path):
    if path[:1] == "~":
        c = path[1:2]
        if not c:
            return gethome()
        if c == os.sep:
            return asPyString(File(gethome(), path[2:]).getPath())
    return path
示例#4
0
def read(fd, buffersize):
    """read(fd, buffersize) -> string

    Read a file descriptor.
    """
    from org.python.core.util import StringUtil
    rawio = FileDescriptors.get(fd)
    buf = _handle_oserror(rawio.read, buffersize)
    return asPyString(StringUtil.fromBytes(buf))
示例#5
0
def dirname(path):
    """Return the directory component of a pathname"""
    path = _tostr(path, "dirname")
    result = asPyString(File(path).getParent())
    if not result:
        if isabs(path):
            result = path  # Must be root
        else:
            result = ""
    return result
示例#6
0
def get_os_type():
    """Return the name of the type of the underlying OS.

    Returns a value suitable for the os.name variable (though not
    necessarily intended to be for os.name Jython).  This value may be
    overwritten in the Jython registry.
    """
    os_name = sys.registry.getProperty('python.os')
    if os_name:
        return asPyString(os_name)

    os_name = asPyString(java.lang.System.getProperty('os.name'))
    os_type = None
    for type, (patterns, shell_commands) in _os_map.iteritems():
        for pattern in patterns:
            if os_name.startswith(pattern):
                # determine the shell_command later, when it's needed:
                # it requires os.path (which isn't setup yet)
                return type
    return 'posix'
示例#7
0
def join(path, *args):
    """Join two or more pathname components, inserting os.sep as needed"""
    path = _tostr(path, "join")
    f = File(path)
    for a in args:
        a = _tostr(a, "join")
        g = File(a)
        if g.isAbsolute() or len(f.getPath()) == 0:
            f = g
        else:
            if a == "":
                a = os.sep
            f = File(f, a)
    return asPyString(f.getPath())
示例#8
0
def listdir(path):
    """listdir(path) -> list_of_strings

    Return a list containing the names of the entries in the directory.

        path: path of directory to list

    The list is in arbitrary order.  It does not include the special
    entries '.' and '..' even if they are present in the directory.
    """
    l = File(sys.getPath(path)).list()
    if l is None:
        raise OSError(0, 'No such directory', path)
    return [asPyString(entry) for entry in l]
示例#9
0
def getaddrinfo(host, port, family=AF_INET, socktype=None, proto=0, flags=None):
    try:
        if not family in [AF_INET, AF_INET6, AF_UNSPEC]:
            raise gaierror(errno.EIO, 'ai_family not supported')
        filter_fns = []
        filter_fns.append({
            AF_INET:   lambda x: isinstance(x, java.net.Inet4Address),
            AF_INET6:  lambda x: isinstance(x, java.net.Inet6Address),
            AF_UNSPEC: lambda x: isinstance(x, java.net.InetAddress),
        }[family])
        # Cant see a way to support AI_PASSIVE right now.
        # if flags and flags & AI_PASSIVE:
        #     pass
        results = []
        for a in java.net.InetAddress.getAllByName(host):
            if len([f for f in filter_fns if f(a)]):
                family = {java.net.Inet4Address: AF_INET, java.net.Inet6Address: AF_INET6}[a.getClass()]
                # TODO: Include flowinfo and scopeid in a 4-tuple for IPv6 addresses
                canonname = asPyString(a.getCanonicalHostName())
                sockname = asPyString(a.getHostAddress())
                results.append((family, socktype, proto, canonname, (sockname, port)))
        return results
    except java.lang.Exception, jlx:
        raise _map_exception(jlx)
示例#10
0
def strerror(code):
    """strerror(code) -> string

    Translate an error code to a message string.
    """
    if not isinstance(code, (int, long)):
        raise TypeError('an integer is required')
    constant = Errno.valueOf(code)
    if constant is Errno.__UNKNOWN_CONSTANT__:
        return 'Unknown error: %d' % code
    if constant.name() == constant.description():
        # XXX: have constantine handle this fallback
        # Fake constant or just lacks a description, fallback to Linux's
        try:
            from org.python.constantine.platform.linux import Errno as LinuxErrno
        except ImportError:
            from com.kenai.constantine.platform.linux import Errno as LinuxErrno
        constant = getattr(LinuxErrno, constant.name(), None)
        if not constant:
            return 'Unknown error: %d' % code
    return asPyString(constant.toString())
示例#11
0
文件: socket.py 项目: jbalint/spark
def gethostname():
    try:
        return asPyString(java.net.InetAddress.getLocalHost().getHostName())
    except java.lang.Exception, jlx:
        raise _map_exception(jlx)
示例#12
0
def gethostbyname(name):
    try:
        return asPyString(java.net.InetAddress.getByName(name).getHostAddress())
    except java.lang.Exception, jlx:
        raise _map_exception(jlx)
示例#13
0
def gethostname():
    try:
        return asPyString(java.net.InetAddress.getLocalHost().getHostName())
    except java.lang.Exception, jlx:
        raise _map_exception(jlx)
示例#14
0
def basename(path):
    """Return the final component of a pathname"""
    path = _tostr(path, "basename")
    return asPyString(File(path).getName())
示例#15
0
def basename(path):
    """Return the final component of a pathname"""
    path = _tostr(path, "basename")
    return asPyString(File(path).getName())
示例#16
0
 def error(self, error, msg):
     err = getattr(errno, error.name(), None)
     if err is None:
         raise OSError('%s: %s' % (error, asPyString(msg)))
     raise OSError(err, strerror(err), asPyString(msg))
示例#17
0
def _realpath(path):
    try:
        return asPyString(File(sys.getPath(path)).getCanonicalPath())
    except java.io.IOException:
        return _abspath(path)
示例#18
0
def _abspath(path):
    # Must use normpath separately because getAbsolutePath doesn't normalize
    # and getCanonicalPath would eliminate symlinks.
    return normpath(asPyString(File(sys.getPath(path)).getAbsolutePath()))
示例#19
0
def getcwd():
    """getcwd() -> path

    Return a string representing the current working directory.
    """
    return asPyString(sys.getCurrentWorkingDir())
示例#20
0
def _abspath(path):
    # Must use normpath separately because getAbsolutePath doesn't normalize
    # and getCanonicalPath would eliminate symlinks.
    return normpath(asPyString(File(sys.getPath(path)).getAbsolutePath()))
示例#21
0
def _realpath(path):
    try:
        return asPyString(File(sys.getPath(path)).getCanonicalPath())
    except java.io.IOException:
        return _abspath(path)
示例#22
0
文件: socket.py 项目: jbalint/spark
def gethostbyname(name):
    try:
        return asPyString(
            java.net.InetAddress.getByName(name).getHostAddress())
    except java.lang.Exception, jlx:
        raise _map_exception(jlx)
示例#23
0
 def error(self, error, msg):
     err = getattr(errno, error.name(), None)
     if err is None:
         raise OSError('%s: %s' % (error, asPyString(msg)))
     raise OSError(err, strerror(err), asPyString(msg))
示例#24
0
def getcwd():
    """getcwd() -> path

    Return a string representing the current working directory.
    """
    return asPyString(sys.getCurrentWorkingDir())