Exemplo n.º 1
0
    def _ReadChrootLink(self, path):
        """Convert a chroot symlink to its absolute path.

    This contains defaults/edge cases assumptions for chroot paths. Not
    recommended for non-chroot paths.

    Args:
      path (str|None): The path to resolve.

    Returns:
      str|None: The resolved path if the provided path is a symlink, None
        otherwise.
    """
        # Mainly for the "if self._source_from_path_repo:" branch in _GetChrootPath.
        # _GetSourcePathChroot can return None, so double check it here.
        if not path:
            return None

        abs_path = os.path.abspath(path)
        link = osutils.ResolveSymlink(abs_path)

        # ResolveSymlink returns the passed path when the path isn't a symlink. We
        # can skip some redundant work when its falling back on the link when the
        # chroot is not a symlink.
        if link == abs_path:
            return None

        return link
Exemplo n.º 2
0
    def BlockDevices(self, device=''):
        """Collects information about block devices.

    This method combines information from:
      (1) Reading through the SYSTEMFILE_DEV_DISKBY directories.
      (2) Executing the 'lsblk' command provided by osutils.ListBlockDevices.

    Returns:
      A list of named tuples. Each tuple has the following fields:
        device: The name of the block device.
        size: The size of the block device in bytes.
        ids: A list of ids assigned to this device.
        labels: A list of labels assigned to this device.
    """
        devicefilter = os.path.basename(device)

        # Data collected from the SYSTEMFILE_DEV_DISKBY directories.
        ids = {}
        labels = {}

        # Data collected from 'lsblk'.
        sizes = {}

        # Collect diskby information.
        for prop, diskdir in SYSTEMFILE_DEV_DISKBY.iteritems():
            cmd = ['find', diskdir, '-lname', '*%s' % devicefilter]
            cmd_result = cros_build_lib.RunCommand(cmd, log_output=True)

            if not cmd_result.output:
                continue

            results = cmd_result.output.split()
            for result in results:
                devicename = os.path.abspath(osutils.ResolveSymlink(result))
                result = os.path.basename(result)

                # Ensure that each of our data dicts have the same keys.
                ids.setdefault(devicename, [])
                labels.setdefault(devicename, [])
                sizes.setdefault(devicename, 0)

                if 'ids' == prop:
                    ids[devicename].append(result)
                elif 'labels' == prop:
                    labels[devicename].append(result)

        # Collect lsblk information.
        for device in osutils.ListBlockDevices(in_bytes=True):
            devicename = os.path.join('/dev', device.NAME)
            if devicename in ids:
                sizes[devicename] = int(device.SIZE)

        return [
            RESOURCE_BLOCKDEVICE(device, sizes[device], ids[device],
                                 labels[device]) for device in ids.iterkeys()
        ]
Exemplo n.º 3
0
    def TestValidInterpreter(self):
        """Fail if a script's interpreter is not found, or not executable.

    A script interpreter is anything after the #! sign, up to the end of line
    or the first space.
    """
        failures = []

        for root, _, file_names in os.walk(image_test_lib.ROOT_A):
            for file_name in file_names:
                full_name = os.path.join(root, file_name)
                file_stat = os.lstat(full_name)
                if (not stat.S_ISREG(file_stat.st_mode)
                        or (file_stat.st_mode & 0111) == 0):
                    continue

                with open(full_name, 'rb') as f:
                    if f.read(2) != '#!':
                        continue
                    line = '#!' + f.readline().strip()

                try:
                    # Ignore arguments to the interpreter.
                    interp, _ = filetype.SplitShebang(line)
                except ValueError:
                    failures.append(
                        'File %s has an invalid interpreter path: "%s".' %
                        (full_name, line))

                # Absolute path to the interpreter.
                interp = os.path.join(image_test_lib.ROOT_A,
                                      interp.lstrip('/'))
                # Interpreter could be a symlink. Resolve it.
                interp = osutils.ResolveSymlink(interp, image_test_lib.ROOT_A)
                if not os.path.isfile(interp):
                    failures.append(
                        'File %s uses non-existing interpreter %s.' %
                        (full_name, interp))
                elif (os.stat(interp).st_mode & 0111) == 0:
                    failures.append('Interpreter %s is not executable.' %
                                    interp)

        self.assertFalse(failures, '\n'.join(failures))
Exemplo n.º 4
0
 def testRecursionWithAbsoluteLink(self):
   os.symlink('target', 'link1')
   os.symlink('/link1', 'link2')
   self.assertEqual(osutils.ResolveSymlink('link2', '.'), './target')
   os.unlink('link2')
   os.unlink('link1')
Exemplo n.º 5
0
 def testRecursion(self):
   os.symlink('target', 'link1')
   os.symlink('link1', 'link2')
   self.assertEqual(osutils.ResolveSymlink('link2'), 'target')
   os.unlink('link2')
   os.unlink('link1')
Exemplo n.º 6
0
 def testAbsoluteLink(self):
   os.symlink('/target', 'link')
   self.assertEqual(osutils.ResolveSymlink('link'), '/target')
   self.assertEqual(osutils.ResolveSymlink('link', '/root'), '/root/target')
   os.unlink('link')
Exemplo n.º 7
0
 def testRelativeLink(self):
   os.symlink('target', 'link')
   self.assertEqual(osutils.ResolveSymlink('link'), 'target')
   os.unlink('link')
Exemplo n.º 8
0
 def testRelativeResolution(self):
   """Test relative path resolutions."""
   self.assertEqual(self.file_path,
                    osutils.ResolveSymlink(self.rel_file_symlink))
   self.assertEqual(self.dir_path,
                    osutils.ResolveSymlink(self.rel_dir_symlink))
Exemplo n.º 9
0
 def testAbsoluteResolution(self):
   """Test absolute path resolutions."""
   self.assertEqual(self.file_path,
                    osutils.ResolveSymlink(self.abs_file_symlink))
   self.assertEqual(self.dir_path,
                    osutils.ResolveSymlink(self.abs_dir_symlink))