def test_returncode(self): cmd = [ "python3", "-c", "import sys; print('Truffula trees.'); sys.exit(1)" ] with self.assertRaises(CalledProcessError): execWithCapture(cmd[0], cmd[1:], raise_err=True)
def umount(mnt, lazy=False, maxretry=3, retrysleep=1.0): '''Unmount the given mountpoint. If lazy is True, do a lazy umount (-l). If the mount was a temporary dir created by mount, it will be deleted. raises CalledProcessError if umount fails.''' umount = ["umount"] if lazy: umount += ["-l"] umount += [mnt] count = 0 while maxretry > 0: try: rv = runcmd(umount) except CalledProcessError: count += 1 if count == maxretry: raise logger.warn("failed to unmount %s. retrying (%d/%d)...", mnt, count, maxretry) if logger.getEffectiveLevel() <= logging.DEBUG: fuser = execWithCapture("fuser", ["-vm", mnt]) logger.debug("fuser -vm:\n%s\n", fuser) sleep(retrysleep) else: break if 'lorax.imgutils' in mnt: os.rmdir(mnt) logger.debug("remove tmp mountdir %s", mnt) return (rv == 0)
def test_exec_filter_stderr(self): cmd = [ "python3", "-c", "import sys; print('Truffula trees.', file=sys.stderr); sys.exit(0)" ] stdout = execWithCapture(cmd[0], cmd[1:], filter_stderr=True) self.assertEqual(stdout.strip(), "")
def test_execWithCapture(self): cmd = [ "python3", "-c", "import sys; print('Truffula trees.', end=''); sys.exit(0)" ] stdout = execWithCapture(cmd[0], cmd[1:], callback=lambda p: True) self.assertEqual(stdout.strip(), "Truffula trees.")
def umount(mnt, lazy=False, maxretry=3, retrysleep=1.0): '''Unmount the given mountpoint. If lazy is True, do a lazy umount (-l). If the mount was a temporary dir created by mount, it will be deleted. raises CalledProcessError if umount fails.''' cmd = ["umount"] if lazy: cmd += ["-l"] cmd += [mnt] count = 0 while maxretry > 0: try: rv = runcmd(cmd) except CalledProcessError: count += 1 if count == maxretry: raise logger.warn("failed to unmount %s. retrying (%d/%d)...", mnt, count, maxretry) if logger.getEffectiveLevel() <= logging.DEBUG: fuser = execWithCapture("fuser", ["-vm", mnt]) logger.debug("fuser -vm:\n%s\n", fuser) sleep(retrysleep) else: break if 'lorax.imgutils' in mnt: os.rmdir(mnt) logger.debug("remove tmp mountdir %s", mnt) return (rv == 0)
def get_iso_label(self): """ Get the iso's label using isoinfo Sets self.label if one is found """ isoinfo_output = execWithCapture("isoinfo", ["-d", "-i", self.iso_path]) log.debug(isoinfo_output) for line in isoinfo_output.splitlines(): if line.startswith("Volume id: "): self.label = line[11:] return
def verify(self): '''Ensure that contents of the installroot can run''' status = True ELF_MAGIC = b'\x7fELF' # Iterate over all files in /usr/bin and /usr/sbin # For ELF files, gather them into a list and we'll check them all at # the end. For files with a #!, check them as we go elf_files = [] usr_bin = Path(self.vars.root + '/usr/bin') usr_sbin = Path(self.vars.root + '/usr/sbin') for path in (str(x) for x in itertools.chain(usr_bin.iterdir(), usr_sbin.iterdir()) \ if x.is_file()): with open(path, "rb") as f: magic = f.read(4) if magic == ELF_MAGIC: # Save the path, minus the chroot prefix elf_files.append(path[len(self.vars.root):]) elif magic[:2] == b'#!': # Reopen the file as text and read the first line. # Open as latin-1 so that stray 8-bit characters don't make # things blow up. We only really care about ASCII parts. with open(path, "rt", encoding="latin-1") as f_text: # Remove the #!, split on space, and take the first part shabang = f_text.readline()[2:].split()[0] # Does the path exist? if not os.path.exists(self.vars.root + shabang): logger.error('%s, needed by %s, does not exist', shabang, path) status = False # Now, run ldd on all the ELF files # Just run ldd once on everything so it isn't logged a million times. # At least one thing in the list isn't going to be a dynamic executable, # so use execWithCapture to ignore the exit code. filename = '' for line in execWithCapture('ldd', elf_files, root=self.vars.root, log_output=False, filter_stderr=True).split('\n'): if line and not line[0].isspace(): # New filename header, strip the : at the end and save filename = line[:-1] elif 'not found' in line: logger.error('%s, needed by %s, not found', line.split()[0], filename) status = False return status