Python os.readlink() Examples

The following are 30 code examples of os.readlink(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module os , or try the search function .
Example #1
Source File: safe_uploader_repeater.py    From mysql_utils with GNU General Public License v2.0 6 votes vote down vote up
def kill_stdout_reader():
    """ Kill whatever is on the otherside of stdout """
    std_out_fd = '/proc/{pid}/fd/{stdout}'.format(pid=os.getpid(),
                                                  stdout=STDOUT)
    readlink = os.readlink(std_out_fd)
    pipe_node = re.match('pipe:\[([0-9]+)]', readlink).groups()[0]
    cmd = ("lsof | "
           "awk '{{if($4 == \"{stdin}r\" && $8 == {pipe_node}) print $2}}'"
           "".format(stdin=str(STDIN),
                     pipe_node=pipe_node))
    lsof = subprocess.Popen(cmd, shell=True,
                            stdout=subprocess.PIPE)
    lsof.wait()
    stdout_reader_pid = int(lsof.stdout.read())
    try:
        os.kill(stdout_reader_pid, 9)
    except:
        pass
        # Nothing really to be done here, it is probalby hopeless to try
        # to do anything more. 
Example #2
Source File: pdb.py    From ironpython2 with Apache License 2.0 6 votes vote down vote up
def lookupmodule(self, filename):
        """Helper function for break/clear parsing -- may be overridden.

        lookupmodule() translates (possibly incomplete) file or module name
        into an absolute file name.
        """
        if os.path.isabs(filename) and  os.path.exists(filename):
            return filename
        f = os.path.join(sys.path[0], filename)
        if  os.path.exists(f) and self.canonic(f) == self.mainpyfile:
            return f
        root, ext = os.path.splitext(filename)
        if ext == '':
            filename = filename + '.py'
        if os.path.isabs(filename):
            return filename
        for dirname in sys.path:
            while os.path.islink(dirname):
                dirname = os.readlink(dirname)
            fullname = os.path.join(dirname, filename)
            if os.path.exists(fullname):
                return fullname
        return None 
Example #3
Source File: topology.py    From InsightAgent with Apache License 2.0 6 votes vote down vote up
def netstat_tcp6():
    '''
    This function returns a list of tcp connections utilizing ipv6. Please note that in order to return the pid of of a
    network process running on the system, this script must be ran as root.
    '''
    tcpcontent = _tcp6load()
    tcpresult = []
    for line in tcpcontent:
        line_array = _remove_empty(line.split(' '))
        l_host, l_port = _convert_ipv6_port(line_array[1])
        r_host, r_port = _convert_ipv6_port(line_array[2])
        tcp_id = line_array[0]
        state = TCP_STATE[line_array[3]]
        uid = pwd.getpwuid(int(line_array[7]))[0]
        inode = line_array[9]
        pid = _get_pid_of_inode(inode)
        try:                                            # try read the process name.
            exe = os.readlink('/proc/' + pid + '/exe')
        except:
            exe = None

        nline = [tcp_id, uid, l_host + ':' + l_port,
                 r_host + ':' + r_port, state, pid, exe]
        tcpresult.append(nline)
    return tcpresult 
Example #4
Source File: setup.py    From poetry with MIT License 6 votes vote down vote up
def _find_symlinks(topdir, extension=""):
    """Find symlinks that should be maintained

    Maintained symlinks exist in the bin dir or are modules which have
    aliases.  Our heuristic is that they are a link in a certain path which
    point to a file in the same directory.
    """
    symlinks = defaultdict(list)
    for base_path, dirs, files in os.walk(topdir):
        for filename in files:
            filepath = os.path.join(base_path, filename)
            if os.path.islink(filepath) and filename.endswith(extension):
                target = os.readlink(filepath)
                if os.path.dirname(target) == "":
                    link = filepath[len(topdir) :]
                    if link.startswith("/"):
                        link = link[1:]
                    symlinks[os.path.basename(target)].append(link)
    return symlinks 
Example #5
Source File: utils.py    From zun with Apache License 2.0 6 votes vote down vote up
def get_function_by_ifname(ifname):
    """Get the function by the interface name

    Given the device name, returns the PCI address of a device
    and returns True if the address is in a physical function.
    """
    dev_path = "/sys/class/net/%s/device" % ifname
    sriov_totalvfs = 0
    if os.path.isdir(dev_path):
        try:
            # sriov_totalvfs contains the maximum possible VFs for this PF
            with open(os.path.join(dev_path, _SRIOV_TOTALVFS)) as fd:
                sriov_totalvfs = int(fd.read())
                return (os.readlink(dev_path).strip("./"),
                        sriov_totalvfs > 0)
        except (IOError, ValueError):
            return os.readlink(dev_path).strip("./"), False
    return None, False 
Example #6
Source File: utils.py    From zun with Apache License 2.0 6 votes vote down vote up
def get_vf_num_by_pci_address(pci_addr):
    """Get the VF number based on a VF's pci address

    A VF is associated with an VF number, which ip link command uses to
    configure it. This number can be obtained from the PCI device filesystem.
    """
    VIRTFN_RE = re.compile(r"virtfn(\d+)")
    virtfns_path = "/sys/bus/pci/devices/%s/physfn/virtfn*" % (pci_addr)
    vf_num = None
    try:
        for vf_path in glob.iglob(virtfns_path):
            if re.search(pci_addr, os.readlink(vf_path)):
                t = VIRTFN_RE.search(vf_path)
                vf_num = t.group(1)
                break
    except Exception:
        pass
    if vf_num is None:
        raise exception.PciDeviceNotFoundById(id=pci_addr)
    return vf_num 
Example #7
Source File: build.py    From alibuild with GNU General Public License v3.0 6 votes vote down vote up
def syncToRemote(self, p, spec):
    if not self.writeStore:
      return
    tarballNameWithRev = format("%(package)s-%(version)s-%(revision)s.%(architecture)s.tar.gz",
                                architecture=self.architecture,
                                **spec)
    cmd = format("cd %(workdir)s && "
                 "TARSHA256=`sha256sum %(storePath)s/%(tarballNameWithRev)s | awk '{ print $1 }'` && "
                 "s3cmd put -s -v --host s3.cern.ch --add-header \"x-amz-meta-sha256:$TARSHA256\" --host-bucket %(b)s.s3.cern.ch %(storePath)s/%(tarballNameWithRev)s s3://%(b)s/%(storePath)s/ 2>/dev/null || true\n"
                 "HASHEDURL=`readlink %(linksPath)s/%(tarballNameWithRev)s | sed -e's|^../../||'` &&"
                 "echo $HASHEDURL | s3cmd put -s -v --host s3.cern.ch --host-bucket %(b)s.s3.cern.ch --add-header=\"x-amz-website-redirect-location:https://s3.cern.ch/swift/v1/%(b)s/TARS/${HASHEDURL}\" - s3://%(b)s/%(linksPath)s/%(tarballNameWithRev)s 2>/dev/null || true\n",
                 workdir=self.workdir,
                 b=self.remoteStore,
                 storePath=spec["storePath"],
                 linksPath=spec["linksPath"],
                 tarballNameWithRev=tarballNameWithRev)
    err = execute(cmd)
    dieOnError(err, "Unable to upload tarball.")

# Creates a directory in the store which contains symlinks to the package
# and its direct / indirect dependencies 
Example #8
Source File: archives.py    From instaclone with Apache License 2.0 6 votes vote down vote up
def followlink(path, max_follows=10):
  """
  Dereference a symlink repeatedly to get a non-symlink (up to max_follows times,
  to avoid cycles).
  """
  orig_path = path
  count = 0
  if not os.path.exists(path):
    raise ValueError("Not found: %r" % path)
  while os.path.islink(path):
    # Note path.join handles it correctly if the second arg is an absolute path.
    path = os.path.normpath(os.path.join(os.path.dirname(path), os.readlink(path)))
    count += 1
    if count > max_follows:
      raise ValueError("Too many symlinks: %r" % orig_path)

  return path 
Example #9
Source File: cuckoo_analysis.py    From mquery with GNU Affero General Public License v3.0 6 votes vote down vote up
def extract(
        self, identifier: str, matched_fname: str, current_meta: Metadata
    ) -> Metadata:
        try:
            target = os.readlink(self.path + "{}/binary".format(identifier))
        except OSError:
            return {}

        binary_hash = target.split("/")[-1]

        obj = {
            "cuckoo_hash": {"value": binary_hash},
            "cuckoo_analysis": {
                "display_text": "cuckoo:{}".format(identifier),
                "value": identifier,
            },
        }
        return obj 
Example #10
Source File: factory-package-news-web.py    From openSUSE-release-tools with GNU General Public License v2.0 6 votes vote down vote up
def list():
    _dir = get_dir(request.url_root)
    fn = os.path.join(_dir, 'current')
    current = None
    if os.path.exists(fn):
        current = os.readlink(fn)

    ret = ''
    for i in sorted(os.listdir(_dir), reverse=True):
        if not digits_re.match(i):
            continue
        ret = ret + '<a href="diff/%s">%s</a>' % (i, i)
        if i == current:
            ret = ret + " &lt;--"
        ret = ret + '<br/>'
    return ret 
Example #11
Source File: factory-package-news-web.py    From openSUSE-release-tools with GNU General Public License v2.0 6 votes vote down vote up
def current():
    _dir = get_dir(request.url_root)
    fn = os.path.join(_dir, 'current')
    if request.method == 'POST':
        if not 'version' in request.form:
            return "missing version", 400
        version = request.form['version']
        if not digits_re.match(version):
            return "malformed version", 400
        if not os.path.exists(os.path.join(_dir, version)):
            return "invalid version", 400
        tmpfn = os.path.join(_dir, '.' + version)
        app.logger.debug(tmpfn)
        if os.path.exists(tmpfn):
            os.unlink(tmpfn)
        os.symlink(version, tmpfn)
        os.rename(tmpfn, fn)
        return "ok"
    else:
        if not os.path.exists(fn):
            return "", 404
        return os.readlink(fn) 
Example #12
Source File: tar.py    From ctw-baseline with MIT License 6 votes vote down vote up
def main():
    with tarfile.open(os.path.join(settings.PRODUCTS_ROOT, 'ctw-annotations.tar.gz'), 'w|gz') as tar:
        tar.add(settings.DATA_LIST, filter=reset)
        tar.add(settings.TRAIN, filter=reset)
        tar.add(settings.VAL, filter=reset)
        tar.add(settings.TEST_CLASSIFICATION, filter=reset)
    with open(settings.DATA_LIST) as f:
        data_list = json.load(f)
    all = (('trainval', data_list['train'] + data_list['val'], settings.TRAINVAL_IMAGE_DIR),
           ('test', data_list['test_cls'] + data_list['test_det'], settings.TEST_IMAGE_DIR))
    for prefix, meta, src_dir in all:
        meta.sort(key=operator.itemgetter('image_id'))
        delta = 1000
        n = len(range(0, len(meta), delta))
        for i in range(0, len(meta), delta):
            submeta = meta[i:i + delta]
            with tarfile.open(os.path.join(settings.PRODUCTS_ROOT, 'ctw-{}-{:02d}-of-{:02d}.tar'.format(prefix, i // delta + 1, n)), 'w') as tar:
                for o in submeta:
                    tar.add(os.readlink(os.path.join(src_dir, o['file_name'])), filter=reset) 
Example #13
Source File: posixpath.py    From meddle with MIT License 6 votes vote down vote up
def _resolve_link(path):
    """Internal helper function.  Takes a path and follows symlinks
    until we either arrive at something that isn't a symlink, or
    encounter a path we've seen before (meaning that there's a loop).
    """
    paths_seen = set()
    while islink(path):
        if path in paths_seen:
            # Already seen this path, so we must have a symlink loop
            return None
        paths_seen.add(path)
        # Resolve where the link points to
        resolved = os.readlink(path)
        if not isabs(resolved):
            dir = dirname(path)
            path = normpath(join(dir, resolved))
        else:
            path = normpath(resolved)
    return path 
Example #14
Source File: sriov.py    From kuryr-kubernetes with Apache License 2.0 6 votes vote down vote up
def _bind_device(self, pci, driver, old_driver=None):
        if not old_driver:
            old_driver_path = '/sys/bus/pci/devices/{}/driver'.format(pci)
            old_driver_link = os.readlink(old_driver_path)
            old_driver = os.path.basename(old_driver_link)
        if old_driver not in constants.MELLANOX_DRIVERS:
            unbind_path = '/sys/bus/pci/drivers/{}/unbind'.format(old_driver)
            bind_path = '/sys/bus/pci/drivers/{}/bind'.format(driver)

            with open(unbind_path, 'w') as unbind_fd:
                unbind_fd.write(pci)

            override = "/sys/bus/pci/devices/{}/driver_override".format(pci)
            with open(override, 'w') as override_fd:
                override_fd.write("\00")

            with open(override, 'w') as override_fd:
                override_fd.write(driver)

            with open(bind_path, 'w') as bind_fd:
                bind_fd.write(pci)

            LOG.info("Device %s was binded on driver %s. Old driver is %s",
                     pci, driver, old_driver)
        return old_driver 
Example #15
Source File: sriov.py    From kuryr-kubernetes with Apache License 2.0 6 votes vote down vote up
def _get_pci_info(self, pf, vf_index):
        pci_slot = ''
        pci_vendor_info = ''

        vendor_path = '/sys/class/net/{}/device/virtfn{}/vendor'.format(
            pf, vf_index)
        with open(vendor_path) as vendor_file:
            vendor_full = vendor_file.read()
            vendor = vendor_full.split('x')[1].strip()
        device_path = '/sys/class/net/{}/device/virtfn{}/device'.format(
            pf, vf_index)
        with open(device_path) as device_file:
            device_full = device_file.read()
            device = device_full.split('x')[1].strip()
        pci_vendor_info = '{}:{}'.format(vendor, device)

        vf_path = '/sys/class/net/{}/device/virtfn{}'.format(
            pf, vf_index)
        pci_slot_path = os.readlink(vf_path)
        pci_slot = pci_slot_path.split('/')[1]

        return {'pci_slot': pci_slot,
                'pci_vendor_info': pci_vendor_info} 
Example #16
Source File: pdb.py    From meddle with MIT License 6 votes vote down vote up
def lookupmodule(self, filename):
        """Helper function for break/clear parsing -- may be overridden.

        lookupmodule() translates (possibly incomplete) file or module name
        into an absolute file name.
        """
        if os.path.isabs(filename) and  os.path.exists(filename):
            return filename
        f = os.path.join(sys.path[0], filename)
        if  os.path.exists(f) and self.canonic(f) == self.mainpyfile:
            return f
        root, ext = os.path.splitext(filename)
        if ext == '':
            filename = filename + '.py'
        if os.path.isabs(filename):
            return filename
        for dirname in sys.path:
            while os.path.islink(dirname):
                dirname = os.readlink(dirname)
            fullname = os.path.join(dirname, filename)
            if os.path.exists(fullname):
                return fullname
        return None 
Example #17
Source File: fileutils.py    From codimension with GNU General Public License v3.0 6 votes vote down vote up
def resolveLink(path):
    """Resolves links and detects loops"""
    paths_seen = []
    while islink(path):
        if path in paths_seen:
            # Already seen this path, so we must have a symlink loop
            return path, True
        paths_seen.append(path)
        # Resolve where the link points to
        resolved = os.readlink(path)
        if not isabs(resolved):
            dir_name = dirname(path)
            path = normpath(dir_name + sep + resolved)
        else:
            path = normpath(resolved)
    return path, False 
Example #18
Source File: viewitems.py    From codimension with GNU General Public License v3.0 6 votes vote down vote up
def updateStatus(self):
        """Updates internal fields"""
        if os.path.exists(self._dirName):
            self.icon = getIcon('dirclosed.png')
            self.populated = False
            self.lazyPopulation = True

            if os.path.islink(self._dirName):
                self.isLink = True
                linkTo = os.readlink(self._dirName)
                realpath = os.path.realpath(self._dirName)
                self.toolTip = "-> " + linkTo + "  (" + realpath + ")"
                self.icon = getIcon('dirlink.png')
        else:
            self.icon = getIcon('dirbroken.png')
            self.populated = True
            self.lazyPopulation = False

            self.childItems = []
            self.childItemsSize = 0 
Example #19
Source File: local.py    From filesystem_spec with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
def info(self, path, **kwargs):
        path = self._strip_protocol(path)
        out = os.stat(path, follow_symlinks=False)
        dest = False
        if os.path.islink(path):
            t = "link"
            dest = os.readlink(path)
        elif os.path.isdir(path):
            t = "directory"
        elif os.path.isfile(path):
            t = "file"
        else:
            t = "other"
        result = {"name": path, "size": out.st_size, "type": t, "created": out.st_ctime}
        for field in ["mode", "uid", "gid", "mtime"]:
            result[field] = getattr(out, "st_" + field)
        if dest:
            result["destination"] = dest
            try:
                out2 = os.stat(path, follow_symlinks=True)
                result["size"] = out2.st_size
            except IOError:
                result["size"] = 0
        return result 
Example #20
Source File: install_venv.py    From rucio with Apache License 2.0 6 votes vote down vote up
def create_symlinks(venv=VENV, atlas_clients=False):
    print 'Installing binaries symlinks ...'
    bin_dir = os.path.join(ROOT, "bin")
    venv_bin_dir = os.path.join(venv, "bin")
    binaries = os.listdir(bin_dir)
    for binary in binaries:
        source = os.path.join(bin_dir, binary)
        link_name = os.path.join(venv_bin_dir, binary)
        try:
            os.path.exists(link_name) and source != os.readlink(link_name)
        except OSError, e:
            if e.errno == errno.EINVAL:
                print 'Delete broken symlink: %(link_name)s -> %(source)s' % locals()
                os.remove(link_name)
            else:
                raise e
        if not os.path.exists(link_name):
            print 'Create the symlink: %(link_name)s -> %(source)s' % locals()
            os.symlink(source, link_name) 
Example #21
Source File: auth_data.py    From vsphere-storage-for-docker with Apache License 2.0 5 votes vote down vote up
def get_info(self):
        """Returns a dict with useful info for misc. status commands"""

        link_location = db_location = "N/A"
        if self.mode == DBMode.MultiNode:
            db_location = os.readlink(self.db_path)
            link_location = self.db_path
        elif self.mode == DBMode.SingleNode:
            link_location = self.db_path
        elif self.mode == DBMode.BrokenLink:
            link_location = self.db_path

        return {"DB_Mode": str(self.mode),
                "DB_LocalPath": link_location,
                "DB_SharedLocation": db_location} 
Example #22
Source File: NetUtils.py    From pcocc with GNU General Public License v3.0 5 votes vote down vote up
def _iommu_group(self):
        iommu_group_path = '/sys/bus/pci/drivers/vfio-pci/{0}/iommu_group'.format(
            self._dev_addr)

        return os.path.basename(os.readlink(iommu_group_path)) 
Example #23
Source File: proc.py    From stem with GNU Lesser General Public License v3.0 5 votes vote down vote up
def cwd(pid: int) -> str:
  """
  Provides the current working directory for the given process.

  :param pid: process id of the process to be queried

  :returns: **str** with the path of the working directory for the process

  :raises: **IOError** if it can't be determined
  """

  start_time, parameter = time.time(), 'cwd'
  proc_cwd_link = '/proc/%s/cwd' % pid

  if pid == 0:
    cwd = ''
  else:
    try:
      cwd = os.readlink(proc_cwd_link)
    except OSError:
      exc = IOError('unable to read %s' % proc_cwd_link)
      _log_failure(parameter, exc)
      raise exc

  _log_runtime(parameter, proc_cwd_link, start_time)
  return cwd 
Example #24
Source File: FS.py    From arnold-usd with Apache License 2.0 5 votes vote down vote up
def readlink(self, file):
            return '' 
Example #25
Source File: NetUtils.py    From pcocc with GNU General Public License v3.0 5 votes vote down vote up
def _ibdev_perform(cls, ibdev_name, action, *args):
        device_path = "/sys/class/infiniband/%s/device" % (ibdev_name)
        bound_devices = cls._list_vfio_devices()

        vf_list = []
        for virtfn in os.listdir(device_path):
            m = re.match(r'virtfn(\d+)', virtfn)

            if not re.match(r'virtfn(\d+)', virtfn):
                continue

            vf_id = m.group(1)

            vf_addr = os.path.basename(os.readlink(
                os.path.join(device_path, virtfn)))

            if action == 'find' and vf_addr not in bound_devices:
                return vf_addr
            elif action == 'list' and vf_addr in bound_devices:
                vf_list.append(vf_addr)
            elif action == 'getid' and vf_addr == args[0]:
                return int(vf_id)


        if action == 'find':
            raise NetworkSetupError('unable to find a free '
                                    'VF for device %s' % ibdev_name)
        elif action=='list':
            return vf_list 
Example #26
Source File: __init__.py    From python-netsurv with MIT License 5 votes vote down vote up
def readlink(self, path):
        return os.readlink(path) 
Example #27
Source File: posixpath.py    From ironpython2 with Apache License 2.0 5 votes vote down vote up
def _joinrealpath(path, rest, seen):
    if isabs(rest):
        rest = rest[1:]
        path = sep

    while rest:
        name, _, rest = rest.partition(sep)
        if not name or name == curdir:
            # current dir
            continue
        if name == pardir:
            # parent dir
            if path:
                path, name = split(path)
                if name == pardir:
                    path = join(path, pardir, pardir)
            else:
                path = pardir
            continue
        newpath = join(path, name)
        if not islink(newpath):
            path = newpath
            continue
        # Resolve the symbolic link
        if newpath in seen:
            # Already seen this path
            path = seen[newpath]
            if path is not None:
                # use cached value
                continue
            # The symlink is not resolved, so we must have a symlink loop.
            # Return already resolved part + rest of the path unchanged.
            return join(newpath, rest), False
        seen[newpath] = None # not resolved symlink
        path, ok = _joinrealpath(path, os.readlink(newpath), seen)
        if not ok:
            return join(path, rest), False
        seen[newpath] = path # resolved symlink

    return path, True 
Example #28
Source File: local.py    From python-netsurv with MIT License 5 votes vote down vote up
def readlink(self):
        """ return value of a symbolic link. """
        return py.error.checked_call(os.readlink, self.strpath) 
Example #29
Source File: local.py    From python-netsurv with MIT License 5 votes vote down vote up
def copy(self, target, mode=False, stat=False):
        """ copy path to target.

            If mode is True, will copy copy permission from path to target.
            If stat is True, copy permission, last modification
            time, last access time, and flags from path to target.
        """
        if self.check(file=1):
            if target.check(dir=1):
                target = target.join(self.basename)
            assert self!=target
            copychunked(self, target)
            if mode:
                copymode(self.strpath, target.strpath)
            if stat:
                copystat(self, target)
        else:
            def rec(p):
                return p.check(link=0)
            for x in self.visit(rec=rec):
                relpath = x.relto(self)
                newx = target.join(relpath)
                newx.dirpath().ensure(dir=1)
                if x.check(link=1):
                    newx.mksymlinkto(x.readlink())
                    continue
                elif x.check(file=1):
                    copychunked(x, newx)
                elif x.check(dir=1):
                    newx.ensure(dir=1)
                if mode:
                    copymode(x.strpath, newx.strpath)
                if stat:
                    copystat(x, newx) 
Example #30
Source File: NetUtils.py    From pcocc with GNU General Public License v3.0 5 votes vote down vote up
def ibdev_enable_vf_driver(ibdev_name, driver_name):
    device_path = "/sys/class/infiniband/%s/device/virtfn0" % (ibdev_name)
    dev_addr = os.path.basename(os.readlink(device_path))
    pci_enable_driver(dev_addr, driver_name)