Esempio n. 1
0
 def test_package_building(self, repository=None, overrides={}, contents={}):
     """Test building of Debian binary packages."""
     with Context() as finalizers:
         build_directory = finalizers.mkdtemp()
         control_fields = merge_control_fields(TEST_PACKAGE_FIELDS, overrides)
         # Create the package template.
         os.mkdir(os.path.join(build_directory, 'DEBIAN'))
         with open(os.path.join(build_directory, 'DEBIAN', 'control'), 'wb') as handle:
             control_fields.dump(handle)
         if contents:
             for filename, data in contents.items():
                 filename = os.path.join(build_directory, filename)
                 directory = os.path.dirname(filename)
                 makedirs(directory)
                 with open(filename, 'w') as handle:
                     handle.write(data)
         else:
             with open(os.path.join(build_directory, 'DEBIAN', 'conffiles'), 'wb') as handle:
                 handle.write(b'/etc/file1\n')
                 handle.write(b'/etc/file2\n')
             # Create the directory with configuration files.
             os.mkdir(os.path.join(build_directory, 'etc'))
             touch(os.path.join(build_directory, 'etc', 'file1'))
             touch(os.path.join(build_directory, 'etc', 'file3'))
             # Create a directory that should be cleaned up by clean_package_tree().
             makedirs(os.path.join(build_directory, 'tmp', '.git'))
             # Create a file that should be cleaned up by clean_package_tree().
             with open(os.path.join(build_directory, 'tmp', '.gitignore'), 'w') as handle:
                 handle.write('\n')
         # Build the package (without any contents :-).
         returncode, output = run_cli(main, '--build', build_directory)
         assert returncode == 0
         package_file = os.path.join(tempfile.gettempdir(),
                                     '%s_%s_%s.deb' % (control_fields['Package'],
                                                       control_fields['Version'],
                                                       control_fields['Architecture']))
         assert os.path.isfile(package_file)
         if repository:
             shutil.move(package_file, repository)
             return os.path.join(repository, os.path.basename(package_file))
         else:
             finalizers.register(os.unlink, package_file)
             # Verify the package metadata.
             fields, contents = inspect_package(package_file)
             for name in TEST_PACKAGE_FIELDS:
                 assert fields[name] == TEST_PACKAGE_FIELDS[name]
             # Verify that the package contains the `/' and `/tmp'
             # directories (since it doesn't contain any actual files).
             assert contents['/'].permissions[0] == 'd'
             assert contents['/'].permissions[1:] == 'rwxr-xr-x'
             assert contents['/'].owner == 'root'
             assert contents['/'].group == 'root'
             assert contents['/tmp/'].permissions[0] == 'd'
             assert contents['/tmp/'].owner == 'root'
             assert contents['/tmp/'].group == 'root'
             # Verify that clean_package_tree() cleaned up properly
             # (`/tmp/.git' and `/tmp/.gitignore' have been cleaned up).
             assert '/tmp/.git/' not in contents
             assert '/tmp/.gitignore' not in contents
             return package_file
Esempio n. 2
0
 def test_package_building(self, repository=None, overrides={}, contents={}):
     """Test building of Debian binary packages."""
     with Context() as finalizers:
         build_directory = finalizers.mkdtemp()
         control_fields = merge_control_fields(TEST_PACKAGE_FIELDS, overrides)
         # Create the package template.
         os.mkdir(os.path.join(build_directory, 'DEBIAN'))
         with open(os.path.join(build_directory, 'DEBIAN', 'control'), 'wb') as handle:
             control_fields.dump(handle)
         if contents:
             for filename, data in contents.items():
                 filename = os.path.join(build_directory, filename)
                 directory = os.path.dirname(filename)
                 makedirs(directory)
                 with open(filename, 'w') as handle:
                     handle.write(data)
         else:
             with open(os.path.join(build_directory, 'DEBIAN', 'conffiles'), 'wb') as handle:
                 handle.write(b'/etc/file1\n')
                 handle.write(b'/etc/file2\n')
             # Create the directory with configuration files.
             os.mkdir(os.path.join(build_directory, 'etc'))
             touch(os.path.join(build_directory, 'etc', 'file1'))
             touch(os.path.join(build_directory, 'etc', 'file3'))
             # Create a directory that should be cleaned up by clean_package_tree().
             makedirs(os.path.join(build_directory, 'tmp', '.git'))
             # Create a file that should be cleaned up by clean_package_tree().
             with open(os.path.join(build_directory, 'tmp', '.gitignore'), 'w') as handle:
                 handle.write('\n')
         # Build the package (without any contents :-).
         returncode, output = run_cli(main, '--build', build_directory)
         assert returncode == 0
         package_file = os.path.join(tempfile.gettempdir(),
                                     '%s_%s_%s.deb' % (control_fields['Package'],
                                                       control_fields['Version'],
                                                       control_fields['Architecture']))
         assert os.path.isfile(package_file)
         if repository:
             shutil.move(package_file, repository)
             return os.path.join(repository, os.path.basename(package_file))
         else:
             finalizers.register(os.unlink, package_file)
             # Verify the package metadata.
             fields, contents = inspect_package(package_file)
             for name in TEST_PACKAGE_FIELDS:
                 assert fields[name] == TEST_PACKAGE_FIELDS[name]
             # Verify that the package contains the `/' and `/tmp'
             # directories (since it doesn't contain any actual files).
             assert contents['/'].permissions[0] == 'd'
             assert contents['/'].permissions[1:] == 'rwxr-xr-x'
             assert contents['/'].owner == 'root'
             assert contents['/'].group == 'root'
             assert contents['/tmp/'].permissions[0] == 'd'
             assert contents['/tmp/'].owner == 'root'
             assert contents['/tmp/'].group == 'root'
             # Verify that clean_package_tree() cleaned up properly
             # (`/tmp/.git' and `/tmp/.gitignore' have been cleaned up).
             assert '/tmp/.git/' not in contents
             assert '/tmp/.gitignore' not in contents
             return package_file
Esempio n. 3
0
    def load_control_field_overrides(self, control_fields):
        """
        Apply user defined control field overrides.

        :param control_fields:

            The control field defaults constructed by py2deb (a
            :class:`deb_pkg_tools.deb822.Deb822` object).

        :returns:

            The merged defaults and overrides (a
            :class:`deb_pkg_tools.deb822.Deb822` object).

        Looks for an ``stdeb.cfg`` file inside the Python package's source
        distribution and if found it merges the overrides into the control
        fields that will be embedded in the generated Debian binary package.

        This method first applies any overrides defined in the ``DEFAULT``
        section and then it applies any overrides defined in the section whose
        normalized name (see :func:`~py2deb.utils.package_names_match()`)
        matches that of the Python package.
        """
        py2deb_cfg = os.path.join(self.requirement.source_directory,
                                  'stdeb.cfg')
        if not os.path.isfile(py2deb_cfg):
            logger.debug("Control field overrides file not found (%s).",
                         py2deb_cfg)
        else:
            logger.debug("Loading control field overrides from %s ..",
                         py2deb_cfg)
            parser = configparser.RawConfigParser()
            parser.read(py2deb_cfg)
            # Prepare to load the overrides from the DEFAULT section and
            # the section whose name matches that of the Python package.
            # DEFAULT is processed first on purpose.
            section_names = ['DEFAULT']
            # Match the normalized package name instead of the raw package
            # name because `python setup.py egg_info' normalizes
            # underscores in package names to dashes which can bite
            # unsuspecting users. For what it's worth, PEP-8 discourages
            # underscores in package names but doesn't forbid them:
            # https://www.python.org/dev/peps/pep-0008/#package-and-module-names
            section_names.extend(
                section_name for section_name in parser.sections()
                if package_names_match(section_name, self.python_name))
            for section_name in section_names:
                if parser.has_section(section_name):
                    overrides = dict(parser.items(section_name))
                    logger.debug(
                        "Found %i control file field override(s) in section %s of %s: %r",
                        len(overrides), section_name, py2deb_cfg, overrides)
                    control_fields = merge_control_fields(
                        control_fields, overrides)
        return control_fields
Esempio n. 4
0
 def test_control_field_merging(self):
     defaults = Deb822(['Package: python-py2deb',
                        'Depends: python-deb-pkg-tools',
                        'Architecture: all'])
     # The field names of the dictionary with overrides are lower case on
     # purpose; control file merging should work properly regardless of
     # field name casing.
     overrides = Deb822(dict(version='1.0',
                             depends='python-pip, python-pip-accel',
                             architecture='amd64'))
     self.assertEqual(merge_control_fields(defaults, overrides),
                      Deb822(['Package: python-py2deb',
                              'Version: 1.0',
                              'Depends: python-deb-pkg-tools, python-pip, python-pip-accel',
                              'Architecture: amd64']))
Esempio n. 5
0
 def test_control_field_merging(self):
     """Test the merging of control file fields."""
     defaults = Deb822(['Package: python-py2deb',
                        'Depends: python-deb-pkg-tools',
                        'Architecture: all'])
     # The field names of the dictionary with overrides are lower case on
     # purpose; control file merging should work properly regardless of
     # field name casing.
     overrides = Deb822(dict(version='1.0',
                             depends='python-pip, python-pip-accel',
                             architecture='amd64'))
     assert merge_control_fields(defaults, overrides) == \
         Deb822(['Package: python-py2deb',
                 'Version: 1.0',
                 'Depends: python-deb-pkg-tools, python-pip, python-pip-accel',
                 'Architecture: amd64'])
Esempio n. 6
0
    def load_control_field_overrides(self, control_fields):
        """
        Apply user defined control field overrides.

        Looks for an ``stdeb.cfg`` file inside the Python package's source
        distribution and if found it merges the overrides into the control
        fields that will be embedded in the generated Debian binary package.

        This method first applies any overrides defined in the ``DEFAULT``
        section and then it applies any overrides defined in the section whose
        normalized name (see :py:func:`~py2deb.utils.package_names_match()`)
        matches that of the Python package.

        :param control_fields: The control field defaults constructed by py2deb
                               (a :py:class:`debian.deb822.Deb822` object).
        :returns: The merged defaults and overrides (a
                  :py:class:`debian.deb822.Deb822` object).
        """
        py2deb_cfg = os.path.join(self.requirement.source_directory, 'stdeb.cfg')
        if not os.path.isfile(py2deb_cfg):
            logger.debug("Control field overrides file not found (%s).", py2deb_cfg)
        else:
            logger.debug("Loading control field overrides from %s ..", py2deb_cfg)
            parser = configparser.RawConfigParser()
            parser.read(py2deb_cfg)
            # Prepare to load the overrides from the DEFAULT section and
            # the section whose name matches that of the Python package.
            # DEFAULT is processed first on purpose.
            section_names = ['DEFAULT']
            # Match the normalized package name instead of the raw package
            # name because `python setup.py egg_info' normalizes
            # underscores in package names to dashes which can bite
            # unsuspecting users. For what it's worth, PEP-8 discourages
            # underscores in package names but doesn't forbid them:
            # https://www.python.org/dev/peps/pep-0008/#package-and-module-names
            section_names.extend(section_name for section_name in parser.sections()
                                 if package_names_match(section_name, self.python_name))
            for section_name in section_names:
                if parser.has_section(section_name):
                    overrides = dict(parser.items(section_name))
                    logger.debug("Found %i control file field override(s) in section %s of %s: %r",
                                 len(overrides), section_name, py2deb_cfg, overrides)
                    control_fields = merge_control_fields(control_fields, overrides)
        return control_fields