Ejemplo n.º 1
0
 def test_control_field_parsing(self):
     """Test the parsing of control file fields."""
     deb822_package = Deb822([
         'Package: python-py2deb',
         'Depends: python-deb-pkg-tools, python-pip, python-pip-accel',
         'Installed-Size: 42'
     ])
     parsed_info = parse_control_fields(deb822_package)
     assert parsed_info == {
         'Package':
         'python-py2deb',
         'Depends':
         RelationshipSet(Relationship(name=u'python-deb-pkg-tools'),
                         Relationship(name=u'python-pip'),
                         Relationship(name=u'python-pip-accel')),
         'Installed-Size':
         42
     }
     # Test backwards compatibility with the old interface where `Depends'
     # like fields were represented as a list of strings (shallow parsed).
     parsed_info['Depends'] = [text_type(r) for r in parsed_info['Depends']]
     assert unparse_control_fields(parsed_info) == deb822_package
     # Test compatibility with fields like `Depends' containing a string.
     parsed_info['Depends'] = deb822_package['Depends']
     assert unparse_control_fields(parsed_info) == deb822_package
def main():
    module = AnsibleModule(
        argument_spec={
            'path': {'type': 'path', 'required': True}
        }
    )

    if not HAS_DEB_PKG_TOOLS:
        module.fail_json(msg='The deb_pkg_tools is required for this module')

    with open(module.params['path']) as f:
        paragraphs = list(Deb822.iter_paragraphs(f))

    return_data = {
        'sources': {},
        'packages': {}
    }

    for paragraph in paragraphs:
        data = control.parse_control_fields(paragraph)
        package = data.get('Package')
        source = data.get('Source')
        key = 'packages' if package else 'sources'

        return_data[key][package or source] = item = {}

        for field in control.DEPENDS_LIKE_FIELDS:
            item[field] = list(getattr(data.get(field), 'names', []))

        for field in data:
            if field in control.DEPENDS_LIKE_FIELDS:
                continue
            item[field] = data.get(field)

    module.exit_json(**return_data)
Ejemplo n.º 3
0
    def setUp(self):
        SRC_DIR = os.path.join(os.path.dirname(os.path.realpath(__file__)), '../../../')
        self.control_file = os.path.join(SRC_DIR, 'debian/control')
        self.requirements_file = os.path.join(SRC_DIR, 'requirements.txt')

        self.dpkg_name = {
            'msgpack-python': 'python3-msgpack',
            'pyyaml': 'python3-yaml'
            }

        with open(self.control_file) as handle:
            control = handle.read()

        faucet_dpkg = str()
        for line in control.split("\n"):
            if line.startswith("Package: python3-faucet"):
                faucet_dpkg += line
            elif faucet_dpkg:
                if not line:
                    break
                faucet_dpkg += "{}\n".format(line)

        faucet_dpkg = parse_control_fields(deb822_from_string(faucet_dpkg))
        self.faucet_dpkg_deps = {}
        for dep in faucet_dpkg['Depends']:
            if isinstance(dep, VersionedRelationship):
                if dep.name not in self.faucet_dpkg_deps:
                    self.faucet_dpkg_deps[dep.name] = []
                self.faucet_dpkg_deps[dep.name].append("{}{}".format(dep.operator, dep.version))
Ejemplo n.º 4
0
 def test_control_field_parsing(self):
     deb822_package = Deb822([
         'Package: python-py2deb',
         'Depends: python-deb-pkg-tools, python-pip, python-pip-accel',
         'Installed-Size: 42'
     ])
     parsed_info = parse_control_fields(deb822_package)
     self.assertEqual(
         parsed_info, {
             'Package':
             'python-py2deb',
             'Depends':
             RelationshipSet(Relationship(name=u'python-deb-pkg-tools'),
                             Relationship(name=u'python-pip'),
                             Relationship(name=u'python-pip-accel')),
             'Installed-Size':
             42
         })
     # Test backwards compatibility with the old interface where `Depends'
     # like fields were represented as a list of strings (shallow parsed).
     parsed_info['Depends'] = [unicode(r) for r in parsed_info['Depends']]
     self.assertEqual(unparse_control_fields(parsed_info), deb822_package)
     # Test compatibility with fields like `Depends' containing a string.
     parsed_info['Depends'] = deb822_package['Depends']
     self.assertEqual(unparse_control_fields(parsed_info), deb822_package)
Ejemplo n.º 5
0
    def setUp(self):
        src_dir = os.path.join(os.path.dirname(os.path.realpath(__file__)),
                               '../../../')
        self.control_file = os.path.join(src_dir, 'debian/control')
        self.requirements_file = os.path.join(src_dir, 'requirements.txt')

        self.dpkg_name = {'pyyaml': 'python3-yaml'}

        with open(self.control_file, 'r', encoding='utf-8') as handle:
            control = handle.read()

        faucet_dpkg = str()
        for line in control.split("\n"):
            if line.startswith("Package: python3-faucet"):
                faucet_dpkg += line
            elif faucet_dpkg:
                if not line:
                    break
                faucet_dpkg += "\n{}".format(line)

        faucet_dpkg = parse_control_fields(parse_deb822(faucet_dpkg))
        self.faucet_dpkg_deps = {}
        for dep in faucet_dpkg['Depends']:
            if isinstance(dep, VersionedRelationship):
                if dep.name not in self.faucet_dpkg_deps:
                    self.faucet_dpkg_deps[dep.name] = []
                self.faucet_dpkg_deps[dep.name].append("{}{}".format(
                    dep.operator, dep.version))
Ejemplo n.º 6
0
def inspect_package_fields(archive, cache=None):
    r"""
    Get the fields (metadata) from a ``*.deb`` archive.

    :param archive: The pathname of an existing ``*.deb`` archive.
    :param cache: The :class:`.PackageCache` to use (defaults to :data:`None`).
    :returns: A dictionary with control file fields (the result of
              :func:`.parse_control_fields()`).

    Here's an example:

    >>> from deb_pkg_tools.package import inspect_package_fields
    >>> print(repr(inspect_package_fields('python3.4-minimal_3.4.0-1+precise1_amd64.deb')))
    {'Architecture': u'amd64',
     'Conflicts': RelationshipSet(VersionedRelationship(name=u'binfmt-support', operator=u'<<', version=u'1.1.2')),
     'Depends': RelationshipSet(VersionedRelationship(name=u'libpython3.4-minimal', operator=u'=', version=u'3.4.0-1+precise1'),
                                VersionedRelationship(name=u'libexpat1', operator=u'>=', version=u'1.95.8'),
                                VersionedRelationship(name=u'libgcc1', operator=u'>=', version=u'1:4.1.1'),
                                VersionedRelationship(name=u'zlib1g', operator=u'>=', version=u'1:1.2.0')),
     'Description': u'Minimal subset of the Python language (version 3.4)\n This package contains the interpreter and some essential modules.  It can\n be used in the boot process for some basic tasks.\n See /usr/share/doc/python3.4-minimal/README.Debian for a list of the modules\n contained in this package.',
     'Installed-Size': 3586,
     'Maintainer': u'Felix Krull <*****@*****.**>',
     'Multi-Arch': u'allowed',
     'Original-Maintainer': u'Matthias Klose <*****@*****.**>',
     'Package': u'python3.4-minimal',
     'Pre-Depends': RelationshipSet(VersionedRelationship(name=u'libc6', operator=u'>=', version=u'2.15')),
     'Priority': u'optional',
     'Recommends': u'python3.4',
     'Section': u'python',
     'Source': u'python3.4',
     'Suggests': RelationshipSet(Relationship(name=u'binfmt-support')),
     'Version': u'3.4.0-1+precise1'}

    """
    if cache:
        entry = cache.get_entry('control-fields', archive)
        value = entry.get_value()
        if value is not None:
            return value
    listing = execute('dpkg-deb', '-f', archive, logger=logger, capture=True)
    fields = parse_control_fields(deb822_from_string(listing))
    if cache:
        entry.set_value(fields)
    return fields
Ejemplo n.º 7
0
def inspect_package_fields(archive, cache=None):
    r"""
    Get the fields (metadata) from a ``*.deb`` archive.

    :param archive: The pathname of an existing ``*.deb`` archive.
    :param cache: The :class:`.PackageCache` to use (defaults to :data:`None`).
    :returns: A dictionary with control file fields (the result of
              :func:`.parse_control_fields()`).

    Here's an example:

    >>> from deb_pkg_tools.package import inspect_package_fields
    >>> print(repr(inspect_package_fields('python3.4-minimal_3.4.0-1+precise1_amd64.deb')))
    {'Architecture': u'amd64',
     'Conflicts': RelationshipSet(VersionedRelationship(name=u'binfmt-support', operator=u'<<', version=u'1.1.2')),
     'Depends': RelationshipSet(VersionedRelationship(name=u'libpython3.4-minimal', operator=u'=', version=u'3.4.0-1+precise1'),
                                VersionedRelationship(name=u'libexpat1', operator=u'>=', version=u'1.95.8'),
                                VersionedRelationship(name=u'libgcc1', operator=u'>=', version=u'1:4.1.1'),
                                VersionedRelationship(name=u'zlib1g', operator=u'>=', version=u'1:1.2.0')),
     'Description': u'Minimal subset of the Python language (version 3.4)\n This package contains the interpreter and some essential modules.  It can\n be used in the boot process for some basic tasks.\n See /usr/share/doc/python3.4-minimal/README.Debian for a list of the modules\n contained in this package.',
     'Installed-Size': 3586,
     'Maintainer': u'Felix Krull <*****@*****.**>',
     'Multi-Arch': u'allowed',
     'Original-Maintainer': u'Matthias Klose <*****@*****.**>',
     'Package': u'python3.4-minimal',
     'Pre-Depends': RelationshipSet(VersionedRelationship(name=u'libc6', operator=u'>=', version=u'2.15')),
     'Priority': u'optional',
     'Recommends': u'python3.4',
     'Section': u'python',
     'Source': u'python3.4',
     'Suggests': RelationshipSet(Relationship(name=u'binfmt-support')),
     'Version': u'3.4.0-1+precise1'}

    """
    if cache:
        entry = cache.get_entry('control-fields', archive)
        value = entry.get_value()
        if value is not None:
            return value
    listing = execute('dpkg-deb', '-f', archive, logger=logger, capture=True)
    fields = parse_control_fields(deb822_from_string(listing))
    if cache:
        entry.set_value(fields)
    return fields
Ejemplo n.º 8
0
 def test_control_field_parsing(self):
     deb822_package = Deb822(['Package: python-py2deb',
                              'Depends: python-deb-pkg-tools, python-pip, python-pip-accel',
                              'Installed-Size: 42'])
     parsed_info = parse_control_fields(deb822_package)
     self.assertEqual(parsed_info,
                      {'Package': 'python-py2deb',
                       'Depends': RelationshipSet(
                           Relationship(name=u'python-deb-pkg-tools'),
                           Relationship(name=u'python-pip'),
                           Relationship(name=u'python-pip-accel')),
                       'Installed-Size': 42})
     # Test backwards compatibility with the old interface where `Depends'
     # like fields were represented as a list of strings (shallow parsed).
     parsed_info['Depends'] = [unicode(r) for r in parsed_info['Depends']]
     self.assertEqual(unparse_control_fields(parsed_info), deb822_package)
     # Test compatibility with fields like `Depends' containing a string.
     parsed_info['Depends'] = deb822_package['Depends']
     self.assertEqual(unparse_control_fields(parsed_info), deb822_package)
Ejemplo n.º 9
0
 def test_control_field_parsing(self):
     """Test the parsing of control file fields."""
     deb822_package = Deb822(['Package: python-py2deb',
                              'Depends: python-deb-pkg-tools, python-pip, python-pip-accel',
                              'Installed-Size: 42'])
     parsed_info = parse_control_fields(deb822_package)
     assert parsed_info == {'Package': 'python-py2deb',
                            'Depends': RelationshipSet(
                                Relationship(name=u'python-deb-pkg-tools'),
                                Relationship(name=u'python-pip'),
                                Relationship(name=u'python-pip-accel')),
                            'Installed-Size': 42}
     # Test backwards compatibility with the old interface where `Depends'
     # like fields were represented as a list of strings (shallow parsed).
     parsed_info['Depends'] = [text_type(r) for r in parsed_info['Depends']]
     assert unparse_control_fields(parsed_info) == deb822_package
     # Test compatibility with fields like `Depends' containing a string.
     parsed_info['Depends'] = deb822_package['Depends']
     assert unparse_control_fields(parsed_info) == deb822_package
Ejemplo n.º 10
0
    def _parse_deb_control(self, control_file):
        with open(control_file, 'r', encoding='utf-8') as handle:
            control = handle.read()

        faucet_dpkg = str()
        for line in control.split("\n"):
            if line.startswith("Package: python3-faucet"):
                faucet_dpkg += line
            elif faucet_dpkg:
                if not line:
                    break
                faucet_dpkg += "\n{}".format(line)

        faucet_dpkg = parse_control_fields(parse_deb822(faucet_dpkg))
        self.faucet_dpkg_deps = {}
        for dep in faucet_dpkg['Depends']:
            if isinstance(dep, VersionedRelationship):
                if dep.name not in self.faucet_dpkg_deps:
                    self.faucet_dpkg_deps[dep.name] = []
                self.faucet_dpkg_deps[dep.name].append("{}{}".format(
                    dep.operator, dep.version))
Ejemplo n.º 11
0
    def test_requirements_match(self):
        """Test all requirements are listed as apt package dependencies."""

        SRC_DIR = os.path.join(os.path.dirname(os.path.realpath(__file__)),
                               '../../../')
        control_file = os.path.join(SRC_DIR, 'debian/control')
        requirements_file = os.path.join(SRC_DIR, 'requirements.txt')

        real_name = {
            'msgpack-python': 'python3-msgpack',
            'pyyaml': 'python3-yaml'
        }

        with open(control_file) as handle:
            control = handle.read()

        faucet_dpkg = str()
        for line in control.split("\n"):
            if line.startswith("Package: python3-faucet"):
                faucet_dpkg += line
            elif len(faucet_dpkg) > 0:
                if not line:
                    break
                faucet_dpkg += "{}\n".format(line)

        faucet_dpkg = parse_control_fields(deb822_from_string(faucet_dpkg))
        faucet_dpkg_deps = [x.name for x in faucet_dpkg['Depends']]

        for item in pip.req.parse_requirements(requirements_file,
                                               session="unittest"):
            if isinstance(item, pip.req.InstallRequirement):
                if item.name in real_name:
                    self.assertIn(real_name[item.name], faucet_dpkg_deps)
                else:
                    self.assertIn("python3-{}".format(item.name),
                                  faucet_dpkg_deps)
Ejemplo n.º 12
0
def parse_pkgs_gz(file):
    fields = []
    s = gzip.open(file, 'rb').read()
    for i in s.strip().split('\n\n'):
        fields.append(parse_control_fields(deb822_from_string(i)))
    return fields
Ejemplo n.º 13
0
            relation = edgeCol.createDocument(d).save()
        else:
            print("Unknown relationshitp: " + repr(oneDep))


#
# Main import routine
#

onePackage = ''
for line in open('/tmp/allpackages', encoding='utf-8'):
    # Package blocks are separated by new lines.
    if len(line) == 1 and len(onePackage) > 4:
        pkg = deb822_from_string(onePackage)
        pname = pkg['Package']
        pkg1 = parse_control_fields(pkg)
        p = PackageToDict(pkg1)
        try:
            packagesCol.createDocument(p).save()
            for key in pkg1.keys():
                # filter for fields with relations:
                if isinstance(pkg1[key], deb_pkg_tools.deps.RelationshipSet):
                    # save one relation set to field:
                    saveDependencyToEdgeCol(getEdgeCol(key), pkg1[key], pname,
                                            False)
            onePackage = ''
        except CreationError:
            pass
    else:
        onePackage += line