Skip to content

Commit

Permalink
Find files wil now replace system paths with rpm macros (fix #351)
Browse files Browse the repository at this point in the history
  • Loading branch information
onionka committed Nov 6, 2015
1 parent 4b321ab commit f91873a
Show file tree
Hide file tree
Showing 2 changed files with 107 additions and 21 deletions.
81 changes: 77 additions & 4 deletions rpg/plugins/misc/find_file.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,85 @@
from rpg.plugin import Plugin
import re


class FindFilePlugin(Plugin):

MACROS = [
("/usr/local/share/javadoc", "%{_javadocdir}"),
("/usr/share/javadoc", "%{_javadocdir}"),
("/usr/local/share/java", "%{_javadir}"),
("/usr/share/java", "%{_javadir}"),
("/usr/local/share/man", "%{_mandir}"),
("/usr/share/man", "%{_mandir}"),
(r"/usr/local/lib64/perl\d", "%{perl_sitearch}"),
(r"/usr/local/lib64/perl\d", "%{perl_sitearch}"),
(r"/usr/local/share/perl\d", "${perl_sitelib}"),
(r"/usr/local/share/perl\d", "${perl_sitelib}"),
(r"/usr/local/lib64/python2.\d/site-packages", "%{python_sitearch}"),
(r"/usr/lib64/python2.\d/site-packages", "%{python_sitearch}"),
(r"/usr/local/lib/python2.\d/site-packages", "%{python_sitelib}"),
(r"/usr/lib/python2.\d/site-packages", "%{python_sitelib}"),
(r"/usr/local/lib64/python3.\d/site-packages", "%{python3_sitearch}"),
(r"/usr/lib64/python3.\d/site-packages", "%{python3_sitearch}"),
(r"/usr/local/lib/python3.\d/site-packages", "%{python3_sitelib}"),
(r"/usr/lib/python3.\d/site-packages", "%{python3_sitelib}"),
("/usr/local/share/doc", "%{_defaultdocdir}"),
("/usr/share/doc", "%{_defaultdocdir}"),
("/usr/local/share/licenses", "%{_defaultlicencedir}"),
("/usr/share/licenses", "%{_defaultlicencedir}"),
("/usr/local/lib/rpm/fileattrs", "%{_fileattrsdir}"),
("/usr/lib/rpm/fileattrs", "%{_fileattrsdir}"),
("/usr/local/lib64/gfortran/modules", "%{fmoddir}"),
("/usr/lib64/gfortran/modules", "%{fmoddir}"),
("/usr/local/lib/gfortran/modules", "%{fmoddir}"),
("/usr/lib/gfortran/modules", "%{fmoddir}"),
("/usr/local/include", "%{_includedir}"),
("/usr/include", "%{_includedir}"),
("/usr/local/sbin", "%{_sbindir}"),
("/usr/sbin", "%{_sbindir}"),
("/usr/local/bin", "%{_bindir}"),
("/usr/bin", "%{_bindir}"),
("/usr/local/lib64", "%{_libdir}"),
("/usr/lib64", "%{_libdir}"),
("/usr/local/libexec", "%{_libexecdir}"),
("/usr/libexec", "%{_libexecdir}"),
("/usr/local/lib", "%{_libdir}"),
("/usr/lib", "%{_libdir}"),
("/usr/local/share/info", "%{_infodir}"),
("/usr/share/info", "%{_infodir}"),
("/usr/local/share", "%{_datadir}"),
("/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('**/*')))
47 changes: 30 additions & 17 deletions tests/unit/test_plugins.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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):
Expand All @@ -50,6 +40,33 @@ def available(self):
return self


class MockedPath:

MACROED_PATHS = {
'usr/bin/p': "%{_bindir}/p",
'usr/include/i.h': "%{_includedir}/i.h",
'usr/lib/python3.4/site-packages/f.py': "%{python3_sitelib}/f.py",
'usr/lib/python2.7/site-packages/f.py': "%{python_sitelib}/f.py",
"usr/lib/libl.so": "%{_libdir}/libl.so",
"usr/local/include/befa/file.hpp": "%{_includedir}/befa/file.hpp"
}

def __init__(self, path):
self.path = path

def __str__(self):
return self.path

def glob(self, *args, **kwargs):
return [MockedPath(f) for f in self.MACROED_PATHS.keys()]

def is_file(self, *args, **kwargs):
return self.path != '~'

def relative_to(self, *args, **kwargs):
return self.path


class FindPatchPluginTest(PluginTestCase):

def setUp(self):
Expand All @@ -74,14 +91,11 @@ def test_find_patch(self):

def test_find_files(self):
plugin = FindFilePlugin()
plugin.installed(self.test_project_dir / "setuptools",
plugin.installed(MockedPath(self.test_project_dir / "installed"),
self.spec, self.sack)
files = sorted([
("/setup.py", None, None),
("/testscript.py", None, None)
])
self.assertEqual(sorted(list(self.spec.files)),
files)
sorted([(MockedPath.MACROED_PATHS[f], None, None)
for f in MockedPath.MACROED_PATHS]))

def test_find_translation_file(self):
plugin = FindTranslationPlugin()
Expand All @@ -105,7 +119,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()
Expand Down

0 comments on commit f91873a

Please sign in to comment.