Open and extract data from rpm files
4633
private boolean handleRpmFile(String innerDir, String archiveFile) {
        boolean success = true;
        File rpmFile = new File(archiveFile);
        FileInputStream rpmFIS = null;
        try {
            rpmFIS = new FileInputStream(rpmFile.getPath());
        } catch (FileNotFoundException e) {
            success = false;
            logger.warn("File not found: {}", archiveFile);
        }

        Format format = null;
        ReadableByteChannel channel = Channels.newChannel(rpmFIS);
        ReadableChannelWrapper channelWrapper = new ReadableChannelWrapper(channel);
        try {
            format = new org.redline_rpm.Scanner().run(channelWrapper);
        } catch (IOException e) {
            success = false;
            logger.warn("Error reading RPM file {}: {}", archiveFile, e.getCause());
        }

        if (format != null) {
            Header header = format.getHeader();
            FileOutputStream cpioOS = null;
            FileOutputStream cpioEntryOutputStream = null;
            CpioArchiveInputStream cpioIn = null;
            File cpioFile = null;
            try {
                // extract all .cpio file
                // get input stream according to payload compressor type
                InputStream inputStream;
                AbstractHeader.Entry pcEntry = header.getEntry(Header.HeaderTag.PAYLOADCOMPRESSOR);
                String[] pc = (String[]) pcEntry.getValues();
                if (pc[0].equals(LZMA)) {
                    try {
                        inputStream = new LZMACompressorInputStream(rpmFIS);
                    } catch (Exception e) {
                        throw new IOException("Failed to load LZMA compression stream", e);
                    }
                } else {
                    inputStream = Util.openPayloadStream(header, rpmFIS);
                }

                cpioFile = new File(rpmFile.getPath() + CPIO);
                cpioOS = new FileOutputStream(cpioFile);
                IOUtils.copy(inputStream, cpioOS);
                // extract files from .cpio
                File extractDestination = new File(innerDir);
                extractDestination.mkdirs();
                cpioIn = new CpioArchiveInputStream(new FileInputStream(cpioFile));

                CpioArchiveEntry cpioEntry;
                while ((cpioEntry = (CpioArchiveEntry) cpioIn.getNextEntry()) != null) {
                    String entryName = cpioEntry.getName();
                    String lowercaseName = entryName.toLowerCase();
                    File file = new File(extractDestination, getFileName(entryName));
                    cpioEntryOutputStream = new FileOutputStream(file);
                    IOUtils.copy(cpioIn, cpioEntryOutputStream);
                    String innerExtractionDir;
                    if (lowercaseName.matches(TAR_EXTENSION_PATTERN)) {
                        innerExtractionDir = innerDir + File.separator + entryName + this.randomString;
                        unTar(file.getName(), innerExtractionDir, file.getPath());
                    } else if (lowercaseName.matches(ZIP_EXTENSION_PATTERN)) {
                        innerExtractionDir = innerDir + File.separator + entryName + this.randomString;
                        unZip(innerExtractionDir, file.getPath());
                    }
                    // close
                    closeResource(cpioEntryOutputStream);
                }
            } catch (IOException e) {
                logger.error("Error unpacking rpm file {}: {}", rpmFile.getName(), e.getMessage());
            } finally {
                closeResource(cpioEntryOutputStream);
                closeResource(cpioIn);
                closeResource(cpioOS);
                deleteFile(cpioFile);
            }
        }
        return success;
    }
1031938
def download_and_install(self):
        """"""
        if self.is_installed_from_bin:
            try:
                self.installer.install_from_rpm_py_package()
                return
            except RpmPyPackageNotFoundError as e:
                Log.warn('RPM Py Package not found. reason: {0}'.format(e))

                # Pass to try to install from the source.
                pass

        # Download and install from the source.
        top_dir_name = self.downloader.download_and_expand()
        rpm_py_dir = os.path.join(top_dir_name, 'python')

        setup_py_in_found = False
        with Cmd.pushd(rpm_py_dir):
            if self.installer.setup_py.exists_in_path():
                setup_py_in_found = True
                self.installer.run()

        if not setup_py_in_found:
            self.installer.install_from_rpm_py_package()
1312334
def info():
    
    installation_path = os.path.abspath(os.path.join(os.path.dirname(__file__), os.path.pardir))

    click.echo("colin {} {}".format(__version__, installation_path))
    click.echo("colin-cli {}\n".format(os.path.realpath(__file__)))

    # click.echo(get_version_of_the_python_package(module=conu))

    rpm_installed = is_rpm_installed()
    click.echo(get_version_msg_from_the_cmd(package_name="podman", use_rpm=rpm_installed))
    click.echo(get_version_msg_from_the_cmd(package_name="skopeo", use_rpm=rpm_installed))
    click.echo(get_version_msg_from_the_cmd(package_name="ostree",
                                            use_rpm=rpm_installed,
                                            max_lines_of_the_output=3))
1135573
def metadata_extractor(self):
        
        if not hasattr(self, '_local_file'):
            raise AttributeError("local_file attribute must be set before "
                                 "calling metadata_extractor")
        if not hasattr(self, '_metadata_extractor'):
            if self.local_file.endswith('.whl'):
                logger.info("Getting metadata from wheel using "
                            "WheelMetadataExtractor.")
                extractor_cls = metadata_extractors.WheelMetadataExtractor
            else:
                logger.info("Getting metadata from setup.py using "
                            "SetupPyMetadataExtractor.")
                extractor_cls = metadata_extractors.SetupPyMetadataExtractor

            base_python_version = (
                self.base_python_version or self.template_base_py_ver)

            self._metadata_extractor = extractor_cls(
                self.local_file,
                self.name,
                self.name_convertor,
                self.version,
                self.rpm_name,
                self.venv,
                base_python_version)

        return self._metadata_extractor
239998
def _extract_archive(file, target_dir):
    
    if file.endswith('.gz') or file.endswith('.tar') or file.endswith('.tgz'):
        archive = tarfile.open(file, 'r')
    elif file.endswith('.zip'):
        archive = zipfile.ZipFile(file, 'r')
    else:
        raise Exception('Unrecognized file type: ' + file)
    archive.extractall(path=target_dir)
    archive.close()
494656
def __get_cfg_pkgs_rpm(self):
        '''
        
        '''
        out, err = self._syscall('rpm', None, None, '-qa', '--configfiles',
                                 '--queryformat', '%{name}-%{version}-%{release}\\n')
        data = dict()
        pkg_name = None
        pkg_configs = []

        out = salt.utils.stringutils.to_str(out)
        for line in out.split(os.linesep):
            line = line.strip()
            if not line:
                continue
            if not line.startswith("/"):
                if pkg_name and pkg_configs:
                    data[pkg_name] = pkg_configs
                pkg_name = line
                pkg_configs = []
            else:
                pkg_configs.append(line)

        if pkg_name and pkg_configs:
            data[pkg_name] = pkg_configs

        return data
845861
def _create_rpmmacros(runas='root'):
    '''
    
    '''
    home = os.path.expanduser('~')
    rpmbuilddir = os.path.join(home, 'rpmbuild')
    if not os.path.isdir(rpmbuilddir):
        __salt__['file.makedirs_perms'](name=rpmbuilddir, user=runas, group='mock')

    mockdir = os.path.join(home, 'mock')
    if not os.path.isdir(mockdir):
        __salt__['file.makedirs_perms'](name=mockdir, user=runas, group='mock')

    rpmmacros = os.path.join(home, '.rpmmacros')
    with salt.utils.files.fopen(rpmmacros, 'w') as afile:
        afile.write(
            salt.utils.stringutils.to_str('%_topdir {0}\n'.format(rpmbuilddir))
        )
        afile.write('%signature gpg\n')
        afile.write('%_source_filedigest_algorithm 8\n')
        afile.write('%_binary_filedigest_algorithm 8\n')
        afile.write('%_gpg_name packaging@saltstack.com\n')
959790
def extract_tar(fileobj):
    """"""
    import time

    archive = tarfile.open(fileobj=fileobj)
    filenames = [info.name for info in archive.getmembers() if info.isfile()]

    for src_name, dst_name in filter_tzfiles(filenames):
        mtime = archive.getmember(src_name).mtime
        modified = tuple(time.gmtime(mtime)[:6])
        bytes = archive.extractfile(src_name).read()

        yield dst_name, modified, bytes
746365
def _open(file_, mode='r'):
  """"""
  if isinstance(file_, string_types):
    _, ext = path.splitext(file_)
    if ext in {'.bz2', '.gz'}:
      s = tarfile.open(file_)
      return s.extractfile(s.next())
    else:
      return open(file_, mode)
  return file_
3930
def extract_tarfile(archive_name, destpath):
    ""
    archive = tarfile.open(archive_name)
    archive.extractall(destpath)
903619
def extract_to_disk(self):
        """"""
        archive_name, extension = os.path.splitext(os.path.basename(self.file.name))
        if not os.path.isdir(os.path.join(os.getcwd(), archive_name)):
            os.mkdir(archive_name)
        os.chdir(archive_name)
        for filename, data in self.extract().items():
            f = open(filename, 'wb')
            f.write(data or b'')
            f.close()
1054963
def extract_dms (archive, compression, cmd, verbosity, interactive, outdir):
    """"""
    check_archive_ext(archive)
    cmdlist = [cmd, '-d', outdir]
    if verbosity > 1:
        cmdlist.append('-v')
    cmdlist.extend(['u', archive])
    return cmdlist
977760
def get_rpms(self):
        

        tags = [
            'NAME',
            'VERSION',
            'RELEASE',
            'ARCH',
            'EPOCH',
            'SIGMD5',
            'SIGPGP:pgpsig',
            'SIGGPG:pgpsig',
        ]

        cmd = "/bin/rpm " + rpm_qf_args(tags)
        try:
            # py3
            (status, output) = subprocess.getstatusoutput(cmd)
        except AttributeError:
            # py2
            with open('/dev/null', 'r+') as devnull:
                p = subprocess.Popen(cmd,
                                     shell=True,
                                     stdin=devnull,
                                     stdout=subprocess.PIPE,
                                     stderr=devnull)

                (stdout, stderr) = p.communicate()
                status = p.wait()
                output = stdout.decode()

        if status != 0:
            self.log.debug("%s: stderr output: %s", cmd, stderr)
            raise RuntimeError("%s: exit code %s" % (cmd, status))

        return parse_rpm_output(output.splitlines(), tags)
992520
def extract_tar (archive, compression, cmd, verbosity, interactive, outdir):
    """"""
    try:
        with tarfile.open(archive) as tfile:
            tfile.extractall(path=outdir)
    except Exception as err:
        msg = "error extracting %s: %s" % (archive, err)
        raise util.PatoolError(msg)
    return None
942599
def extract_zip(archive, compression, cmd, verbosity, interactive, outdir):
    """"""
    try:
        with zipfile.ZipFile(archive) as zfile:
            zfile.extractall(outdir)
    except Exception as err:
        msg = "error extracting %s: %s" % (archive, err)
        raise util.PatoolError(msg)
    return None
457713
def get_rpm_version(package_name):
    """"""
    version_result = subprocess.run(["rpm", "-q", package_name],
                                    stdout=subprocess.PIPE,
                                    stderr=subprocess.PIPE)
    if version_result.returncode == 0:
        return version_result.stdout.decode().rstrip()
    else:
        return None
1213128
def build_srpm(specfile, save_dir):
    
    logger.info('Starting rpmbuild to build: {0} SRPM.'.format(specfile))
    if save_dir != get_default_save_path():
        try:
            msg = subprocess.Popen(
                ['rpmbuild',
                 '--define', '_sourcedir {0}'.format(save_dir),
                 '--define', '_builddir {0}'.format(save_dir),
                 '--define', '_srcrpmdir {0}'.format(save_dir),
                 '--define', '_rpmdir {0}'.format(save_dir),
                 '-bs', specfile], stdout=subprocess.PIPE).communicate(
                 )[0].strip()
        except OSError:
            logger.error(
                "Rpmbuild failed for specfile: {0} and save_dir: {1}".format(
                    specfile, save_dir), exc_info=True)
            msg = 'Rpmbuild failed. See log for more info.'
        return msg
    else:
        if not os.path.exists(save_dir):
            raise IOError("Specify folder to store a file (SAVE_DIR) "
                          "or install rpmdevtools.")
        try:
            msg = subprocess.Popen(
                ['rpmbuild',
                 '--define', '_sourcedir {0}'.format(save_dir + '/SOURCES'),
                 '--define', '_builddir {0}'.format(save_dir + '/BUILD'),
                 '--define', '_srcrpmdir {0}'.format(save_dir + '/SRPMS'),
                 '--define', '_rpmdir {0}'.format(save_dir + '/RPMS'),
                 '-bs', specfile], stdout=subprocess.PIPE).communicate(
                )[0].strip()
        except OSError:
            logger.error("Rpmbuild failed for specfile: {0} and save_dir: "
                         "{1}".format(specfile, save_dir), exc_info=True)
            msg = 'Rpmbuild failed. See log for more info.'
        return msg