Exemple #1
0
def apk_parser(filename):
    try:
        db_contents = open(filename).read()
        packages = db_contents.split('\n\n')
        logger.debug('Found {} APK packages'.format(len(packages)))
        for package in packages:
            if package:
                attributes = package.split('\n')
                name = ""
                version = ""
                architecture = ""
                size = ""
                for attribute in attributes:
                    if (attribute.startswith('P:')):
                        name = attribute[2:]
                    elif (attribute.startswith('V:')):
                        version = attribute[2:]
                    elif (attribute.startswith('A:')):
                        architecture = attribute[2:]
                    elif (attribute.startswith('S:')):
                        size = attribute[2:]
                yield (name,
                       PackageFeature(None, name, size, version, architecture))
    except IOError as e:
        logger.error('Failed to read APK database to obtain packages. '
                     'Check if %s is present.  [Exception: %s: %s]'
                     ' ' % (filename, type(e).__name__, e.strerror))
        raise
Exemple #2
0
def get_dpkg_packages(root_dir='/', dbpath='var/lib/dpkg', installed_since=0):

    if os.path.isabs(dbpath):
        logger.warning('dbpath: ' + dbpath +
                       ' is defined absolute. Ignoring prefix: ' + root_dir +
                       '.')

    # Update for a different route.

    dbpath = os.path.join(root_dir, dbpath)

    output = subprocess_run([
        'dpkg-query', '-W', '--admindir={0}'.format(dbpath),
        '-f=${Package}|${Version}'
        '|${Architecture}|${Installed-Size}\n'
    ],
                            shell=False)
    dpkglist = output.strip('\n')
    if dpkglist:
        for dpkginfo in dpkglist.split('\n'):
            (name, version, architecture, size) = dpkginfo.split(r'|')

            # dpkg does not provide any installtime field
            # feature_key = '{0}/{1}'.format(name, version) -->
            # changed to below per Suriya's request

            feature_key = '{0}'.format(name, version)
            yield (feature_key,
                   PackageFeature(None, name, size, version, architecture))
Exemple #3
0
 def test_get_rpm_packages_with_db_reload(self, mock_subprocess_run):
     pkgs = list(package_utils.get_rpm_packages(reload_needed=True))
     print pkgs
     assert pkgs == [
         ('pkg1',
          PackageFeature(
              installed='123',
              pkgname='pkg1',
              pkgsize='123',
              pkgversion='v1',
              pkgarchitecture='x86')),
         ('pkg1',
          PackageFeature(
              installed='123',
              pkgname='pkg1',
              pkgsize='123',
              pkgversion='v1',
              pkgarchitecture='x86'))]
Exemple #4
0
 def test_get_dpkg_packages(self, mock_subprocess_run):
     pkgs = list(package_utils.get_dpkg_packages())
     print pkgs
     assert pkgs == [
         ('pkg1',
          PackageFeature(
              installed=None,
              pkgname='pkg1',
              pkgsize='123',
              pkgversion='v1',
              pkgarchitecture='x86')),
         ('pkg2',
          PackageFeature(
              installed=None,
              pkgname='pkg2',
              pkgsize='123',
              pkgversion='v2',
              pkgarchitecture='x86'))]
Exemple #5
0
def get_rpm_packages(root_dir='/',
                     dbpath='var/lib/rpm',
                     installed_since=0,
                     reload_needed=False):

    if os.path.isabs(dbpath):
        logger.warning('dbpath: ' + dbpath +
                       ' is defined absolute. Ignoring prefix: ' + root_dir +
                       '.')

    # update for a different route

    dbpath = os.path.join(root_dir, dbpath)

    try:
        if reload_needed:
            reloaded_db_dir = tempfile.mkdtemp()
            _rpm_reload_db(root_dir, dbpath, reloaded_db_dir)
            dbpath = reloaded_db_dir

        output = subprocess_run([
            'rpm', '--dbpath', dbpath, '-qa', '--queryformat',
            '%{installtime}|%{name}|%{version}'
            '-%{release}|%{arch}|%{size}\n'
        ],
                                shell=False,
                                ignore_failure=True)
        # We ignore failures because sometimes rpm returns rc=1 but still
        # outputs all the data.
        rpmlist = output.strip('\n')
    finally:
        if reload_needed:
            logger.debug('Deleting directory: %s' % (reloaded_db_dir))
            shutil.rmtree(reloaded_db_dir)

    if rpmlist:
        for rpminfo in rpmlist.split('\n'):
            (installtime, name, version, architecture, size) = \
                rpminfo.split(r'|')
            """
            if int(installtime) <= installed_since: --> this
            barfs for sth like: 1376416422. Consider try: xxx
            except ValueError: pass
            """

            if installtime <= installed_since:
                continue
            """
            feature_key = '{0}/{1}'.format(name, version) -->
            changed to below per Suriya's request
            """

            feature_key = '{0}'.format(name, version)
            yield (feature_key,
                   PackageFeature(installtime, name, size, version,
                                  architecture))