Beispiel #1
0
    def set_version(self, version):

        """
        Method to update the version in the SPEC file

        :param version: string with new version
        :return: None
        """
        version_re = re.compile('Version:\s*(.+)')
        for index, line in enumerate(self.spec_content):
            match = version_re.search(line)
            if match:
                logger.debug("Updating version in SPEC from '%s' with '%s'", self.get_version(), version)

                # search for used macros in spec file scope
                for m in MacroHelper.get_macros(level=-1, used=True):
                    if m['name'] in match.group(1):
                        # redefine the macro, don't touch Version tag
                        self._set_macro(m['name'], version)
                        return

                self.spec_content[index] = line.replace(match.group(1), version)
                break
        #  save changes to the disc
        self.save()
Beispiel #2
0
    def update_setup_dirname(self, dirname):
        """
        Update %setup or %autosetup dirname argument if needed

        :param dirname: required dirname
        """
        for index, line in enumerate(self.spec_content):
            if line.startswith('%setup') or line.startswith('%autosetup'):
                args = line.split()
                macro = args[0]

                # parse macro arguments, care only about -T and -n
                parser = argparse.ArgumentParser()
                parser.add_argument('-T', action='store_true')
                parser.add_argument('-n', default='%{name}-%{version}')

                namespace, unknown = parser.parse_known_args(args[1:])

                if namespace.T:
                    # -T means not to extract Source0, so this macro instance
                    # can be ignored
                    continue

                # test if modification is really necessary
                if dirname != rpm.expandMacro(namespace.n):
                    new_dirname = dirname

                    # get %{name} and %{version} macros
                    macros = [m for m in MacroHelper.get_macros(level=-3) if m['name'] in ('name', 'version')]
                    # add all macros from spec file scope
                    macros.extend(MacroHelper.get_macros(level=-1))
                    # ensure maximal greediness
                    macros.sort(key=lambda k: len(k['value']), reverse=True)

                    # substitute tokens with macros
                    for m in macros:
                        if m['value'] and m['value'] in dirname:
                            new_dirname = new_dirname.replace(m['value'], '%{{{}}}'.format(m['name']))

                    args = [macro]
                    args.extend(unknown)
                    args.append('-n')
                    args.append(new_dirname)

                    self.spec_content[index] = '#{0}'.format(line)
                    self.spec_content.insert(index + 1, ' '.join(args))
                    self.save()