From 63452c5c4a1bf0243ee9d795ea206e9d8bdbf9ad Mon Sep 17 00:00:00 2001 From: Miroslav Cibulka Date: Thu, 5 Nov 2015 12:20:01 +0100 Subject: [PATCH] Find files wil now replace system paths with rpm macros (fix #351) --- rpg/plugins/misc/find_file.py | 59 +++++++++++++++++-- tests/project/installed/usr/bin/prog | 2 + tests/project/installed/usr/include/inc.h | 0 .../usr/lib/python3.4/site-packages/pck.py | 0 tests/unit/test_plugins.py | 18 ++---- 5 files changed, 61 insertions(+), 18 deletions(-) create mode 100755 tests/project/installed/usr/bin/prog create mode 100644 tests/project/installed/usr/include/inc.h create mode 100644 tests/project/installed/usr/lib/python3.4/site-packages/pck.py diff --git a/rpg/plugins/misc/find_file.py b/rpg/plugins/misc/find_file.py index dc6ee3d..2e69f29 100644 --- a/rpg/plugins/misc/find_file.py +++ b/rpg/plugins/misc/find_file.py @@ -1,12 +1,63 @@ from rpg.plugin import Plugin +import re class FindFilePlugin(Plugin): + MACROS = [ + ("/usr/share/javadoc", "%{_javadocdir}"), + ("/usr/share/java", "%{_javadir}"), + ("/usr/share/man", "%{_mandir}"), + (r"/usr/local/lib64/perl\d", "%{perl_sitearch}"), + (r"/usr/local/share/perl\d", "${perl_sitelib}"), + (r"/usr/lib64/python2.\d/site-packages", "%{python_sitearch}"), + (r"/usr/lib/python2.\d/site-packages", "%{python_sitelib}"), + (r"/usr/lib64/python3.\d/site-packages", "%{python3_sitearch}"), + (r"/usr/lib/python3.\d/site-packages", "%{python3_sitelib}"), + ("/usr/share/doc", "%{_defaultdocdir}"), + ("/usr/share/licenses", "%{_defaultlicencedir}"), + ("/usr/lib/rpm/fileattrs", "%{_fileattrsdir}"), + ("/usr/lib64/gfortran/modules", "%{fmoddir}"), + ("/usr/lib/gfortran/modules", "%{fmoddir}"), + ("/usr/include", "%{_includedir}"), + ("/usr/sbin", "%{_sbindir}"), + ("/usr/bin", "%{_bindir}"), + ("/usr/lib64", "%{_libdir}"), + ("/usr/libexec", "%{_libexecdir}"), + ("/usr/lib", "%{_libdir}"), + ("/usr/share/info", "%{_infodir}"), + ("/usr/share", "%{_datadir}"), + ("/var/lib", "%{_sharedstatedir}"), + ("/var", "%{_localstatedir}"), + ("/etc", "%{_sysconfdir}"), + ] + def installed(self, project_dir, spec, sack): """ Finds files that will be installed and appends them to files macro """ - for item in list(project_dir.glob('**/*')): - if item.is_file(): - spec.files.add(("/" + str(item.relative_to(project_dir)), - None, None)) + + def relative(_file, _relative): + return "/" + str(_file.relative_to(_relative)) + + def append(_file, _spec): + _spec.files.add((_file, None, None)) + + def find_n_replace(_file, f, *r): + match = re.search("^" + f[0], _file) + if match: + return f[1] + _file[len(match.group(0)):] + else: + return find_n_replace(_file, *r) if r else _file + + def ite(_spec, _proj_dir, f, *r): + if f.is_file(): + append(find_n_replace(relative(f, _proj_dir), *self.MACROS), + _spec) + if r: + ite(_spec, _proj_dir, *r) + + def iterate(_spec, _proj_dir, _glob): + if _glob: + ite(_spec, _proj_dir, *_glob) + + iterate(spec, project_dir, list(project_dir.glob('**/*'))) diff --git a/tests/project/installed/usr/bin/prog b/tests/project/installed/usr/bin/prog new file mode 100755 index 0000000..2fda930 --- /dev/null +++ b/tests/project/installed/usr/bin/prog @@ -0,0 +1,2 @@ +#!/bin/bash +echo "Hello world!" diff --git a/tests/project/installed/usr/include/inc.h b/tests/project/installed/usr/include/inc.h new file mode 100644 index 0000000..e69de29 diff --git a/tests/project/installed/usr/lib/python3.4/site-packages/pck.py b/tests/project/installed/usr/lib/python3.4/site-packages/pck.py new file mode 100644 index 0000000..e69de29 diff --git a/tests/unit/test_plugins.py b/tests/unit/test_plugins.py index 888b9b3..09a0ad2 100644 --- a/tests/unit/test_plugins.py +++ b/tests/unit/test_plugins.py @@ -7,7 +7,6 @@ from rpg.plugins.misc.files_to_pkgs import FilesToPkgsPlugin from rpg.plugins.lang.c import CPlugin from rpg.spec import Spec -from unittest import mock from rpg.plugins.project_builder.cmake import CMakePlugin from rpg.plugins.project_builder.setuptools import SetuptoolsPlugin from rpg.plugins.project_builder.autotools import AutotoolsPlugin @@ -29,15 +28,6 @@ def __init__(self, package): files = [] -class MockedLogging: - - called = 0 - - @classmethod - def log(cls, *args, **kwargs): - cls.called += 1 - - class MockedDNFQuery: def filter(self, **kwd): @@ -74,11 +64,12 @@ def test_find_patch(self): def test_find_files(self): plugin = FindFilePlugin() - plugin.installed(self.test_project_dir / "setuptools", + plugin.installed(self.test_project_dir / "installed", self.spec, self.sack) files = sorted([ - ("/setup.py", None, None), - ("/testscript.py", None, None) + ("%{_bindir}/prog", None, None), + ('%{_includedir}/inc.h', None, None), + ('%{python3_sitelib}/spck.py', None, None) ]) self.assertEqual(sorted(list(self.spec.files)), files) @@ -105,7 +96,6 @@ def test_python_find_requires(self): imports = glob("/usr/lib*/python*/lib-dynload/math.cpython-*m.so") self.assertEqual(self.spec.required_files, set(imports)) - @mock.patch("logging.log", new=MockedLogging.log) def test_files_to_pkgs(self): ftpp = FilesToPkgsPlugin() self.spec.Requires = set()