def test_control_file_creation(self): """Test control file creation.""" with Context() as context: directory = context.mkdtemp() # Use a non-existing subdirectory to verify that it's created. control_file = os.path.join(directory, 'DEBIAN', 'control') # Try to create a control file but omit some mandatory fields. self.assertRaises(ValueError, create_control_file, control_file, dict(Package='created-from-python')) # Now we'll provide all of the required fields to actually create the file. create_control_file( control_file, dict( Package='created-from-python', Description='whatever', Maintainer='Peter Odding', Version='1.0', )) # Load the control file to verify its contents. control_fields = load_control_file(control_file) # These fields were provided by us (the caller of create_control_file()). assert control_fields['Package'] == 'created-from-python' assert control_fields['Description'] == 'whatever' # This field was written as a default value. assert control_fields['Architecture'] == 'all'
def test_control_file_patching_and_loading(self): deb822_package = Deb822(['Package: unpatched-example', 'Depends: some-dependency']) with Context() as finalizers: control_file = tempfile.mktemp() finalizers.register(os.unlink, control_file) with open(control_file, 'wb') as handle: deb822_package.dump(handle) call('--patch=%s' % control_file, '--set=Package: patched-example', '--set=Depends: another-dependency') patched_fields = load_control_file(control_file) self.assertEqual(patched_fields['Package'], 'patched-example') self.assertEqual(str(patched_fields['Depends']), 'another-dependency, some-dependency')
def test_control_file_patching_and_loading(self): deb822_package = Deb822( ['Package: unpatched-example', 'Depends: some-dependency']) with Context() as finalizers: control_file = tempfile.mktemp() finalizers.register(os.unlink, control_file) with open(control_file, 'wb') as handle: deb822_package.dump(handle) call('--patch=%s' % control_file, '--set=Package: patched-example', '--set=Depends: another-dependency') patched_fields = load_control_file(control_file) self.assertEqual(patched_fields['Package'], 'patched-example') self.assertEqual(str(patched_fields['Depends']), 'another-dependency, some-dependency')
def parse_specdeb(self): # Get debian infos self.logger.info("Find informations from spec file") control_file_path = os.path.join(self.folder, 'sources', self.config['root_folder'], self.config['debian'], 'control') changelog_file_path = os.path.join(self.folder, 'sources', self.config['root_folder'], self.config['debian'], 'changelog') # Prepare datas self.config['deps'] = self.config.get('deps', []) self.config['deps_pip'] = self.config.get('deps_pip', []) self.config['ccache'] = self.config.get('ccache', False) self.config['version'] = '' self.config['release'] = '' self.config['name'] = '' self.config['source'] = '' deb_info = load_control_file(control_file_path) self.config['name'] = deb_info.get("Source") self.config['deps'] = parse_depends(deb_info.get('Build-Depends')).names version_release_pattern = re.compile("[^ ]* \(([^ ]*)\) .*") with open(changelog_file_path, 'r') as f: first_line = f.readline() match = version_release_pattern.match(first_line) if not match: return False version_release = match.group(1) self.config['version'], self.config['release'] = version_release.split("-", 1) # Clean version self.config['version'] = self.config['version'].split(":", 1)[-1] # Get source file self.config['source'] = self.config['name'] + "_" + self.config['version'] + ".orig.tar.gz" # Log informations self.logger.info("Name: %(name)s", self.config) self.logger.info("Source: %(source)s", self.config) self.logger.info("Version: %(version)s", self.config) self.logger.info("Release: %(release)s", self.config) self.logger.info("Builddepends: %s", ", ".join(self.config['deps'])) return True
def parse_specdeb(self): # Get debian infos self.logger.info("Find informations from spec file") control_file_path = os.path.join(self.folder, 'sources', self.config['root_folder'], self.config['debian'], 'control') changelog_file_path = os.path.join(self.folder, 'sources', self.config['root_folder'], self.config['debian'], 'changelog') # Prepare datas self.config['deps'] = self.config.get('deps', []) self.config['deps_pip'] = self.config.get('deps_pip', []) self.config['ccache'] = self.config.get('ccache', False) self.config['version'] = '' self.config['release'] = '' self.config['name'] = '' self.config['source'] = '' deb_info = load_control_file(control_file_path) self.config['name'] = deb_info.get("Source") self.config['deps'] += parse_depends(deb_info.get('Build-Depends')).names version_release_pattern = re.compile("[^ ]* \(([^ ]*)\) .*") with open(changelog_file_path, 'r') as f: first_line = f.readline() match = version_release_pattern.match(first_line) if not match: return False version_release = match.group(1) self.config['version'], self.config['release'] = version_release.split("-", 1) # Clean version self.config['version'] = self.config['version'].split(":", 1)[-1] # Get source file self.config['source'] = self.config['name'] + "_" + self.config['version'] + ".orig.tar.gz" # Log informations self.logger.info("Name: %(name)s", self.config) self.logger.info("Source: %(source)s", self.config) self.logger.info("Version: %(version)s", self.config) self.logger.info("Release: %(release)s", self.config) self.logger.info("Builddepends: %s", ", ".join(self.config['deps'])) return True
def test_control_file_patching_and_loading(self): """Test patching and loading of control files.""" deb822_package = Deb822(['Package: unpatched-example', 'Depends: some-dependency']) with Context() as finalizers: control_file = tempfile.mktemp() finalizers.register(os.unlink, control_file) with open(control_file, 'wb') as handle: deb822_package.dump(handle) returncode, output = run_cli( main, '--patch=%s' % control_file, '--set=Package: patched-example', '--set=Depends: another-dependency', ) assert returncode == 0 patched_fields = load_control_file(control_file) assert patched_fields['Package'] == 'patched-example' assert str(patched_fields['Depends']) == 'another-dependency, some-dependency'
def test_control_file_creation(self): with Context() as context: directory = context.mkdtemp() # Use a non-existing subdirectory to verify that it's created. control_file = os.path.join(directory, 'DEBIAN', 'control') # Try to create a control file but omit some mandatory fields. self.assertRaises(ValueError, create_control_file, control_file, dict(Package='created-from-python')) # Now we'll provide all of the required fields to actually create the file. create_control_file(control_file, dict( Package='created-from-python', Description='whatever', Maintainer='Peter Odding', Version='1.0', )) # Load the control file to verify its contents. control_fields = load_control_file(control_file) # These fields were provided by us (the caller of create_control_file()). assert control_fields['Package'] == 'created-from-python' assert control_fields['Description'] == 'whatever' # This field was written as a default value. assert control_fields['Architecture'] == 'all'
def test_conversion_of_simple_package(self): """ Convert a simple Python package without any dependencies. Converts coloredlogs_ and sanity checks the result. Performs several static checks on the metadata and contents of the resulting package archive. .. _coloredlogs: https://pypi.org/project/coloredlogs """ # Use a temporary directory as py2deb's repository directory so that we # can easily find the *.deb archive generated by py2deb. with TemporaryDirectory() as directory: # Run the conversion twice to check that existing archives are not overwritten. last_modified_time = 0 for i in range(2): # Prepare a control file to be patched. control_file = os.path.join(directory, 'control') with open(control_file, 'w') as handle: handle.write('Depends: vim\n') # Run the conversion command. exit_code, output = run_cli( main, '--verbose', '--yes', '--repository=%s' % directory, '--report-dependencies=%s' % control_file, 'coloredlogs==0.5', ) assert exit_code == 0 # Check that the control file was patched. control_fields = load_control_file(control_file) assert control_fields['Depends'].matches('vim') assert control_fields['Depends'].matches( fix_name_prefix('python-coloredlogs'), '0.5') # Find the generated Debian package archive. archives = glob.glob('%s/*.deb' % directory) logger.debug("Found generated archive(s): %s", archives) assert len(archives) == 1 # Verify that existing archives are not overwritten. if not last_modified_time: # Capture the last modified time of the archive in the first iteration. last_modified_time = os.path.getmtime(archives[0]) else: # Verify the last modified time of the archive in the second iteration. assert last_modified_time == os.path.getmtime(archives[0]) # Use deb-pkg-tools to inspect the generated package. metadata, contents = inspect_package(archives[0]) logger.debug("Metadata of generated package: %s", dict(metadata)) logger.debug("Contents of generated package: %s", dict(contents)) # Check the package metadata. assert metadata['Package'] == fix_name_prefix( 'python-coloredlogs') assert metadata['Version'].startswith('0.5') assert metadata['Architecture'] == 'all' # There should be exactly one dependency: some version of Python. assert metadata['Depends'].matches(python_version()) # Don't care about the format here as long as essential information is retained. assert 'Peter Odding' in metadata['Maintainer'] assert '*****@*****.**' in metadata['Maintainer'] # Check the package contents. # Check for the two *.py files that should be installed by the package. assert find_file( contents, '/usr/lib/py*/dist-packages/coloredlogs/__init__.py') assert find_file( contents, '/usr/lib/py*/dist-packages/coloredlogs/converter.py') # Make sure the file ownership and permissions are sane. archive_entry = find_file( contents, '/usr/lib/py*/dist-packages/coloredlogs/__init__.py') assert archive_entry.owner == 'root' assert archive_entry.group == 'root' assert archive_entry.permissions == '-rw-r--r--'
def test_conversion_of_simple_package(self): """ Convert a simple Python package without any dependencies. Converts coloredlogs_ and sanity checks the result. Performs several static checks on the metadata and contents of the resulting package archive. .. _coloredlogs: https://pypi.python.org/pypi/coloredlogs """ # Use a temporary directory as py2deb's repository directory so that we # can easily find the *.deb archive generated by py2deb. with TemporaryDirectory() as directory: # Run the conversion twice to check that existing archives are not overwritten. last_modified_time = 0 for i in range(2): # Prepare a control file to be patched. control_file = os.path.join(directory, 'control') with open(control_file, 'w') as handle: handle.write('Depends: vim\n') # Run the conversion command. py2deb('--verbose', '--yes', '--repository=%s' % directory, '--report-dependencies=%s' % control_file, 'coloredlogs==0.5') # Check that the control file was patched. control_fields = load_control_file(control_file) assert control_fields['Depends'].matches('vim') assert control_fields['Depends'].matches('python-coloredlogs', '0.5') # Find the generated Debian package archive. archives = glob.glob('%s/*.deb' % directory) logger.debug("Found generated archive(s): %s", archives) assert len(archives) == 1 # Verify that existing archives are not overwritten. if not last_modified_time: # Capture the last modified time of the archive in the first iteration. last_modified_time = os.path.getmtime(archives[0]) else: # Verify the last modified time of the archive in the second iteration. assert last_modified_time == os.path.getmtime(archives[0]) # Use deb-pkg-tools to inspect the generated package. metadata, contents = inspect_package(archives[0]) logger.debug("Metadata of generated package: %s", dict(metadata)) logger.debug("Contents of generated package: %s", dict(contents)) # Check the package metadata. assert metadata['Package'] == 'python-coloredlogs' assert metadata['Version'].startswith('0.5') assert metadata['Architecture'] == 'all' # There should be exactly one dependency: some version of Python. assert metadata['Depends'].matches('python%i.%i' % sys.version_info[:2]) # Don't care about the format here as long as essential information is retained. assert 'Peter Odding' in metadata['Maintainer'] assert '*****@*****.**' in metadata['Maintainer'] # Check the package contents. # Check for the two *.py files that should be installed by the package. assert find_file(contents, '/usr/lib/python*/dist-packages/coloredlogs/__init__.py') assert find_file(contents, '/usr/lib/python*/dist-packages/coloredlogs/converter.py') # Make sure the file ownership and permissions are sane. archive_entry = find_file(contents, '/usr/lib/python*/dist-packages/coloredlogs/__init__.py') assert archive_entry.owner == 'root' assert archive_entry.group == 'root' assert archive_entry.permissions == '-rw-r--r--'