#23 Improve requirements generation
Merged 4 years ago by pingou. Opened 4 years ago by qulogic.
qulogic/r2spec full-version-reqs  into  master

file modified
+3 -3
@@ -66,8 +66,8 @@ 

          help='Path to local sources of a R package.')

      parser.add_argument('--no-check', action='store_true',

          help='Do not include the %%check section in the generated spec file.')

-     parser.add_argument('--no-suggest', action='store_true',

-         help='Do not include the dependencies marked as \'Suggest\' by the source as dependencies of the RPM.')

+     parser.add_argument('--with-deps', action='store_true',

+         help='Include the dependencies marked by the source as dependencies of the RPM.')

      parser.add_argument('--force-dl', action='store_true',

          help='Enforce the download of the source, even if they are already on the system.')

      parser.add_argument('--keep-sources', action='store_true',
@@ -285,7 +285,7 @@ 

              pack.remove_sources()

  

          spec = Spec(settings, pack, no_check=args.no_check,

-             no_suggest=args.no_suggest)

+                     with_deps=args.with_deps)

          spec.fill_spec_info()

          spec.get_template()

          spec.write_spec(True)

file modified
+40 -30
@@ -24,25 +24,37 @@ 

  

  import datetime

  import os

- import re

  import sys

  import textwrap

- from jinja2 import Template

+ from collections import namedtuple

+ 

+ from jinja2 import Environment, FileSystemLoader

  from r2spec import get_logger, get_rpm_tag, R2specError

  

  

+ class Package(namedtuple('Package', 'name version')):

+     def __str__(self):

+         if self.version:

+             return self.name + ' ' + self.version

+         else:

+             return self.name

+ 

+     @property

+     def rpm_version(self):

+         if self.version:

+             return self.name + ' ' + self.version.replace('-', '.')

+         else:

+             return self.name

+ 

+ 

  def format_dependencies(dependencies):

      """ Format the dependencies cleanning them as much as possible for rpm.

      """

      ignorelist = ['R']

-     # Regular expression used to determine whether the string is a

-     # version number

-     versionmotif = re.compile(r'\d\.\d\.?\d?')

      char = {

              '\r': '',

              '(': ' ',

              ')': ' ',

-             ',': ' ',

              '  ': ' ',

              }

  
@@ -50,17 +62,19 @@ 

          dependencies = dependencies.replace(key, char[key])

      dep_list = []

  

-     for dep in dependencies.split(' '):

-         if dep.strip():

-             if  not ">" in dep \

-             and not "<" in dep \

-             and not "=" in dep \

-             and len(versionmotif.findall(dep)) == 0 \

-             and dep.strip() not in ignorelist:

-                 dep = 'R-%s' % dep.strip()

-                 dep_list.append(dep)

+     for dep in dependencies.split(','):

+         dep = dep.strip()

+         if dep:

+             if ' ' in dep:

+                 name, version = dep.split(' ', 1)

+             else:

+                 name = dep

+                 version = ''

+             if name not in ignorelist:

+                 name = 'R-%s' % name.strip()

+                 dep_list.append(Package(name, version.strip()))

  

-     return ' '.join(dep_list).strip()

+     return dep_list

  

  

  class Spec:
@@ -70,16 +84,17 @@ 

      """

  

      def __init__(self, settings, package=None, no_check=False,

-         no_suggest=False):

+                  with_deps=False):

          """ Constructor.

          """

          self.package = package

          self.settings = settings

-         self.__dict = {}

+         self.__dict = {

+             'with_deps': with_deps

+         }

          self.log = get_logger()

          self.spec = None

          self.no_check = no_check

-         self.no_suggest = no_suggest

  

      def add_files(self, files):

          """ Add to a spec file the given files list.
@@ -145,11 +160,8 @@ 

              self.package.get('Depends'))

          self.__dict['imports'] = format_dependencies(

              self.package.get('Imports'))

-         if not self.no_suggest:

-             self.__dict['suggests'] = format_dependencies(

-                 self.package.get('Suggests'))

-         else:

-             self.__dict['suggests'] = ""

+         self.__dict['suggests'] = format_dependencies(

+             self.package.get('Suggests'))

          self.__dict['description'] = textwrap.fill(

              self.package.get('Description'),

              width=75)
@@ -205,17 +217,15 @@ 

          """ Read the empty template and fills it with the information

          retrieved.

          """

-         template = '%s/specfile.tpl' % os.path.dirname(__file__)

          self.log.info('Filling spec template')

+         loader = FileSystemLoader(os.path.dirname(__file__))

+         env = Environment(loader=loader, lstrip_blocks=True, trim_blocks=True)

          try:

-             stream = open(template, 'r')

-             tplfile = stream.read()

-             stream.close()

-             mytemplate = Template(tplfile)

+             mytemplate = env.get_template('specfile.tpl')

              self.spec = mytemplate.render(self.__dict)

          except IOError as err:

              self.log.debug('ERROR: %s', err)

-             raise R2specError('Cannot read the file %s' % template)

+             raise R2specError('Cannot read the file %s' % (err, ))

  

      def get_specfile(self):

          """ Return the path to the spec file.

file modified
+35 -14
@@ -1,6 +1,8 @@ 

  %global packname  {{ packname }}

- {% if (arch == False) %}%global rlibdir  %{_datadir}/R/library

- {% else %}%global rlibdir  %{_libdir}/R/library

+ {% if arch %}

+ %global rlibdir  %{_libdir}/R/library

+ {% else %}

+ %global rlibdir  %{_datadir}/R/library

  {% endif %}

  

  Name:             R-%{packname}
@@ -13,40 +15,59 @@ 

  Source0:          {{source0}}

  

  # Here's the R view of the dependencies world:

- # Depends:   {{depends}}

- # Imports:   {{imports}}

- # Suggests:  {{suggests}}

+ # Depends:   {{ depends | join(', ') }}

+ # Imports:   {{ imports | join(', ') }}

+ # Suggests:  {{ suggests | join(', ') }}

  # LinkingTo:

  # Enhances:

  

- {% if (arch == False) %}BuildArch:        noarch

+ {% if not arch %}

+ BuildArch:        noarch

+ {% if with_deps %}

  Requires:         R-core

  {% endif %}

- {% if depends != "" %}Requires:         {{depends}}{% endif %}

- {% if imports != "" %}Requires:         {{imports}}{% endif %}

- {% if suggests != "" %}Requires:         {{suggests}}{% endif %}

- BuildRequires:    R-devel tex(latex) {{depends}}

- {% if imports != "" %}BuildRequires:    {{imports}}{% endif %}

- {% if suggests != "" %}BuildRequires:   {{suggests}}{% endif %}

+ {% endif %}

+ {% if with_deps %}

+ {% for dep in depends + imports %}

+ Requires:         {{dep.rpm_version}}

+ {% endfor %}

+ {% for dep in suggests %}

+ Suggests:         {{dep.rpm_version}}

+ {% endfor %}

+ {% endif %}

+ BuildRequires:    R-devel

+ BuildRequires:    tex(latex)

+ {% for dep in depends + imports %}

+ BuildRequires:    {{dep.rpm_version}}

+ {% endfor %}

+ {% for dep in suggests %}

+ BuildRequires:    {{dep.rpm_version}}

+ {% endfor %}

  

  %description

  {{description}}

  

+ 

  %prep

  %setup -q -c -n %{packname}

  

+ 

  %build

  

+ 

  %install

  mkdir -p %{buildroot}%{rlibdir}

  %{_bindir}/R CMD INSTALL -l %{buildroot}%{rlibdir} %{packname}

  test -d %{packname}/src && (cd %{packname}/src; rm -f *.o *.so)

  rm -f %{buildroot}%{rlibdir}/R.css

- {% if (no_check == False) %}

+ 

+ 

+ {% if not no_check %}

  %check

  %{_bindir}/R CMD check %{packname}

- {% endif %}

  

+ 

+ {% endif %}

  %files

  %dir %{rlibdir}/%{packname}

  %doc %{rlibdir}/%{packname}/doc

This does three things:

  • Adds full version information on dependencies (as provided in DESCRIPTION)
  • Switch suggests to Suggests instead of Requires
  • Disables all of the above by default, because rpm macros on F31 do that for us automatically (but it can be enabled with the option for older versions or other rpm-based systems.)

Note, this is based on #22 due to conflicts.

rebased onto 2e21ec0

4 years ago

Pull-Request has been merged by pingou

4 years ago