File sub-pkgs.py of Package samba

#!/usr/bin/env python

import re
import sys
import yaml

KNOWN_TAGS = ['version', 'summary', 'license', 'buildarch', 'provides',
              'requires']

# Regexp to escape RPM macroses
R = re.compile(r'%({[^}]+})')

spec_inc = ''

with open(sys.argv[1], 'r') as yaml_source:
    spec_template = yaml.load(yaml_source)

for group in spec_template:
    group_name, group_members = group.popitem()

    for package in group_members:
        pkg_name, pkg_info = package.popitem()
        so_version = pkg_info.get('so_version', '')
        pkg_info['name'] = pkg_name
        pkg_name = pkg_info['name'] + str(so_version)

        # Begin package definition
        spec_inc += '\n%package -n {name}\n'.format(name=pkg_name)
        spec_inc += '%-*s%s\n' % (15, 'Group:', group_name)

        # Add known tags
        # !!! Note !!! # Package must have Summary: field
        for tag in [t for t in pkg_info.keys() if t.lower() in KNOWN_TAGS]:
            if isinstance(pkg_info[tag], list):
                for tag_val in pkg_info[tag]:
                    tag_val = R.sub(r'%{\1}', tag_val).format(**pkg_info)
                    spec_inc += '%-*s%s\n' % (15, tag + ':', tag_val)
            else:
                tag_val = R.sub(r'%{\1}', pkg_info[tag]).format(**pkg_info)
                spec_inc += '%-*s%s\n' % (15, tag + ':', tag_val)

        # Package must have description
        spec_inc += '\n%description -n {name}\n'.format(name=pkg_name)
        spec_inc += pkg_info.get('description', pkg_info['summary']) + '\n'

        # File list
        spec_inc += '\n%files -n {name}\n'.format(name=pkg_name)
        spec_inc += '%defattr(-,root,root)\n'
        if isinstance(pkg_info['files'], list):
            for file in pkg_info['files']:
                spec_inc += R.sub(r'%{\1}', file).format(**pkg_info)
                spec_inc += '\n'
        else:
            spec_inc += R.sub(r'%{\1}', pkg_info['files']).format(**pkg_info)
            spec_inc += '\n'

        # If package is some library add post-(un-)install scripts
        if pkg_info['name'].startswith('lib') \
           and not pkg_info['name'].endswith('-devel'):
            spec_inc += '\n'
            spec_inc += '%post   -n {name} -p /sbin/ldconfig\n'.format(
                name=pkg_name)
            spec_inc += '%postun -n {name} -p /sbin/ldconfig\n'.format(
                name=pkg_name)

print(spec_inc)
openSUSE Build Service is sponsored by