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 63452c5
Show file tree
Hide file tree
Showing 5 changed files with 61 additions and 18 deletions.
59 changes: 55 additions & 4 deletions rpg/plugins/misc/find_file.py
Original file line number Diff line number Diff line change
@@ -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('**/*')))
2 changes: 2 additions & 0 deletions tests/project/installed/usr/bin/prog
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
#!/bin/bash
echo "Hello world!"
Empty file.
Empty file.
18 changes: 4 additions & 14 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 Down Expand Up @@ -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)
Expand All @@ -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()
Expand Down

0 comments on commit 63452c5

Please sign in to comment.