From 97673fced25285630203f0a23d0ecfb4d67c9f76 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jan=20Pokorn=C3=BD?= Date: Wed, 28 Jan 2015 17:09:12 +0100 Subject: [PATCH] distill-spec: split out and re-add as git submodule MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Jan Pokorný --- .gitmodules | 3 + misc/distill-spec | 141 +----------------------------------------- misc/git-distill-spec | 1 + 3 files changed, 5 insertions(+), 140 deletions(-) create mode 100644 .gitmodules mode change 100755 => 120000 misc/distill-spec create mode 160000 misc/git-distill-spec diff --git a/.gitmodules b/.gitmodules new file mode 100644 index 0000000..0d1074f --- /dev/null +++ b/.gitmodules @@ -0,0 +1,3 @@ +[submodule "misc/git-distill-spec"] + path = misc/git-distill-spec + url = ../distill-spec diff --git a/misc/distill-spec b/misc/distill-spec deleted file mode 100755 index bd40aa4..0000000 --- a/misc/distill-spec +++ /dev/null @@ -1,140 +0,0 @@ -#!/usr/bin/env python -# Copyright 2015 Red Hat, Inc. -# Part of clufter project -# Licensed under GPLv2+ (a copy included | http://gnu.org/licenses/gpl-2.0.txt) - -# Script to distill plain specfile from meta (but still usable!) specfile. -# -# TODO: -# - allow external macro definitions, just like: rpm{,build} --define 'name def' - -from base64 import b64encode, b64decode -from ctypes import cdll, c_char_p, c_int, c_void_p -from re import compile as re_compile, escape as re_escape, X as re_X -from sys import argv, stdin -from tempfile import NamedTemporaryFile - -from rpm import RPMSPEC_ANYARCH, RPMSPEC_FORCE, RPMBUILD_NONE, _rpmb, addMacro - - -# ctypes magic - -rpmb = cdll[_rpmb.__file__] - -rpmSpecParse = rpmb.rpmSpecParse -rpmSpecParse.argtypes = [c_char_p, c_int, c_void_p] -rpmSpecParse.restype = c_void_p - -rpmSpecGetSection = rpmb.rpmSpecGetSection -rpmSpecGetSection.argtypes = [c_void_p, c_int] -rpmSpecGetSection.restype = c_char_p - - -# re stuff - -# temporary wrapping must start with '_' or '/' because of Requires context: -# error: line 87: -# Dependency tokens must begin with alpha-numeric, '_' or '/': -# Requires: @@_bindir@@/nano -neutral_mark = "_._" -nm_parens = ((neutral_mark, ) * 2) -nm_parens_re = ((re_escape(neutral_mark), ) * 2) - -fragile = '_isa', 'name', 'version', 'release' -fragile_upper = tuple(f.upper() for f in fragile) - -re_noexpand_fragile = re_compile("""%({{)?(?P\??(?P(?:{0} - )))(?=\W)(?(1)}})""".format('|'.join - (fragile)), - re_X) -re_noexpand = re_compile("""%({)?(?!\??(?:clufter\w* - |if|else|endif - |global|define - |package|description - |prep|build|install|check - |files|defattr|attr|doc|license|exclude - |pre|post|preun|postun - |changelog - )\W) - (?P\??(?P[^{(\s}]*)) - (?(1)})""", - re_X) -re_nonexpmacro_wrapped = re_compile("(?P\S*?)".join(nm_parens_re)) -re_multiline = re_compile('\n\n+') - - -def get_parsed(f): - spec = rpmSpecParse(f, RPMSPEC_ANYARCH | RPMSPEC_FORCE, 0) - s = rpmSpecGetSection(spec, RPMBUILD_NONE) - return s - -#def add_macro(name, spec): -# print "adding:", name, spec -# addMacro(name, spec) - - -def redef_macros(f): - s = None - with open(f, 'r') as fo: - s = fo.read() - s = re_noexpand.sub( - lambda m: addMacro(m.group('m'), b64encode(m.group('g')) - .replace('=', '_').join(nm_parens)) or m.group(0), - re_noexpand_fragile.sub(lambda m: m.group(0).upper(), s) - ) - with open(f, 'w') as fo: - fo.write(s) - return f - - -def restore_macros(s): - return re_nonexpmacro_wrapped.sub(lambda m: "%{{{0}}}".format( - tuple( - n.lower() if any(f in n for f - in fragile_upper) - else n for n in (b64decode( - m.group('m').replace('_', '=') - ), ) - )[0]), - s) - - -def squeeze_newlines(s): - #return re_multiline.sub('\n\n', s).lstrip('\n') - return '\n'.join(reduce( - lambda prev, new: prev[:-1] + [new] if not prev[-1] - and (prev[-2].split(':', 1)[0] == new.split(':', 1)[0] - or not prev[-2]) - else prev + [new], s.splitlines(), ['', ''] - )[1:]) - - -if __name__ == "__main__": - f = stdin if len(argv) < 2 else argv[1] - if f is not stdin: - f = open(f, 'r') - s = f.read() - if f is not stdin: - f.close() - with NamedTemporaryFile('w') as f: - f.write(s) - f.flush() - f.seek(0) - # first redefine macros in following way: - # 1. replace name, version, ... with upper-cased version, because - # the are the automatic macros that's get (re)defined when parsing - # the specfile -- this way we can preserve them in combinarion w/ 2. - # 2. redefine macros to be preserved (i.e., not native directives/tags - # and clufter* macros that we wan't to expand right now) with - # base64-encoded original macro declaration (i.e., with initial - # question mark, etc.) so that it will be replaced like this - # in the specfile preprocessing phase (XXX why not to replace it - # right away then?) - #s = open(redef_macros(f.name)).read() - # second, preprocess the specfile (with new macros) - #s = get_parsed(redef_macros(f.name)) - # third, restore those now base64-encoded macros - #s = restore_macros(get_parsed(redef_macros(f.name))) - # fourth, smart-squeeze the newlines (2+ x \n --> 2 x \n, ...) - s = squeeze_newlines(restore_macros(get_parsed(redef_macros(f.name)))) - print s diff --git a/misc/distill-spec b/misc/distill-spec new file mode 120000 index 0000000..20d93b4 --- /dev/null +++ b/misc/distill-spec @@ -0,0 +1 @@ +git-distill-spec/distill-spec \ No newline at end of file diff --git a/misc/git-distill-spec b/misc/git-distill-spec new file mode 160000 index 0000000..3be3136 --- /dev/null +++ b/misc/git-distill-spec @@ -0,0 +1 @@ +Subproject commit 3be313608521c42ce811501e30685eb3b543d4c9