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)
def normcase(path): """Normalize case of pathname. XXX Not done right under JDK. """ path = _tostr(path, "normcase") return asPyString(File(path).getPath())
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
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))
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
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'
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())
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]
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)
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())
def gethostname(): try: return asPyString(java.net.InetAddress.getLocalHost().getHostName()) except java.lang.Exception, jlx: raise _map_exception(jlx)
def gethostbyname(name): try: return asPyString(java.net.InetAddress.getByName(name).getHostAddress()) except java.lang.Exception, jlx: raise _map_exception(jlx)
def basename(path): """Return the final component of a pathname""" path = _tostr(path, "basename") return asPyString(File(path).getName())
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))
def _realpath(path): try: return asPyString(File(sys.getPath(path)).getCanonicalPath()) except java.io.IOException: return _abspath(path)
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()))
def getcwd(): """getcwd() -> path Return a string representing the current working directory. """ return asPyString(sys.getCurrentWorkingDir())
def gethostbyname(name): try: return asPyString( java.net.InetAddress.getByName(name).getHostAddress()) except java.lang.Exception, jlx: raise _map_exception(jlx)