Skip to content

Commit

Permalink
python interpret is now default as plugin attribute
Browse files Browse the repository at this point in the history
  • Loading branch information
onionka committed Feb 24, 2016
1 parent be2a7f1 commit 01bbdae
Show file tree
Hide file tree
Showing 4 changed files with 10 additions and 27 deletions.
3 changes: 3 additions & 0 deletions rpg/plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,6 @@ def package_build(self, package_path, spec, sack):
def mock_recover(self, log, spec):
pass
"""

# default python interpret - may be changed
python_interpret = "python3"
29 changes: 2 additions & 27 deletions rpg/plugins/lang/python.py
Original file line number Diff line number Diff line change
@@ -1,29 +1,6 @@
from rpg.plugin import Plugin
from rpg.command import Command
from rpg.utils import rpm_dir_dump
import re


def getPythonInterpret(proj_path):
""" finds shebang and extract python interpret from it.
Every python project should have at least one shebang ... """
for py_file in proj_path.glob('**/*.py'):
with open(str(py_file), 'r') as f_py:
match = re.search(r'^#!(.*)', f_py.read(50))
if match:
return match.group(1)
raise RuntimeError("shebang missing in python project files")


def getPythonVersion(proj_path):
""" gets version of python interpret used in project (proj_path) """
python_interpret = getPythonInterpret(proj_path)
if python_interpret:
return Command(
python_interpret + " -c " +
"\"import sys; "
"sys.stdout.write('%s' % (sys.version_info.major))\""
).execute()


MACROS = rpm_dir_dump()
Expand All @@ -40,10 +17,9 @@ class PythonPlugin(Plugin):

def patched(self, project_dir, spec, sack):
""" Find python dependencies """
python_interpret = getPythonInterpret(project_dir)
for item in list(project_dir.glob('**/*.py')):
spec.required_files |= set(Command(
python_interpret + " -c \"" +
self.python_interpret + " -c \"" +
"from modulefinder import ModuleFinder; " +
"mod = ModuleFinder(); " +
"mod.run_script('" + str(item) + "'); " +
Expand All @@ -56,9 +32,8 @@ def patched(self, project_dir, spec, sack):
def installed(self, project_dir, spec, sack):
""" Compiles all python files depending on which python version they
are and appends them into files macro """
python = getPythonInterpret(project_dir)
for py_file in list(project_dir.glob('**/*.py')):
Command([python + ' ' + sw +
Command([self.python_interpret + ' ' + sw +
' -c \'import compileall; compileall.compile_file("' +
str(py_file) + '")\'' for sw in ["-O", ""]]).execute()
spec.files.update([
Expand Down
4 changes: 4 additions & 0 deletions tests/mock_build/test_build_hawkey.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
from tests.support import RpgTestCase
from rpg.plugin import Plugin
from rpg import Base
import os


class BuildHawkeyTest(RpgTestCase):

def test_hawkey_project(self):
python_interpret_backup = Plugin.python_interpret
Plugin.python_interpret = "python2"
self.base = Base()
self.base.sack = self.base.load_dnf_sack()
self.base.load_plugins()
Expand Down Expand Up @@ -38,3 +41,4 @@ def test_hawkey_project(self):
self.base.target_distro, self.base.target_arch)
os.remove(str(self.base.srpm_path))
os.remove(str(self.base.spec_path))
Plugin.python_interpret = python_interpret_backup
1 change: 1 addition & 0 deletions tests/unit/test_plugins.py
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,7 @@ def test_find_library(self):

def test_python_find_objects(self):
plugin = PythonPlugin()
plugin.python_interpret = "python"
objfiles = [
self.test_project_dir / "py" / "requires" / "sourcecode2.pyo",
self.test_project_dir / "py" / "requires" / "sourcecode2.pyc"
Expand Down

0 comments on commit 01bbdae

Please sign in to comment.