aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorArthur Zamarin <arthurzam@gentoo.org>2022-03-18 09:20:40 +0200
committerArthur Zamarin <arthurzam@gmail.com>2022-03-25 12:09:49 +0300
commit4f842ee64d59c04462270c16d2378582dc2a8830 (patch)
treec942abd0b002ed11e7bd7948ab9b2561d023d7a1
parentpkgdev commit: turn copyright headers into date range when appropriate (diff)
downloadpkgdev-4f842ee64d59c04462270c16d2378582dc2a8830.tar.gz
pkgdev-4f842ee64d59c04462270c16d2378582dc2a8830.tar.bz2
pkgdev-4f842ee64d59c04462270c16d2378582dc2a8830.zip
Add keywords reordering mangler
Automatically reorder keywords to be sorted with OS and arch. Based on nattka's code at nattka/keyword.py Resolves: https://github.com/pkgcore/pkgdev/issues/46 Signed-off-by: Arthur Zamarin <arthurzam@gentoo.org>
-rw-r--r--src/pkgdev/mangle.py23
1 files changed, 23 insertions, 0 deletions
diff --git a/src/pkgdev/mangle.py b/src/pkgdev/mangle.py
index bdeb54b..3355f7c 100644
--- a/src/pkgdev/mangle.py
+++ b/src/pkgdev/mangle.py
@@ -14,6 +14,11 @@ from snakeoil.mappings import OrderedSet
copyright_regex = re.compile(
r'^# Copyright (?P<date>(?P<begin>\d{4}-)?(?P<end>\d{4})) (?P<holder>.+)$')
+keywords_regex = re.compile(
+ r'^(?P<pre>[^#]*\bKEYWORDS=(?P<quote>[\'"]?))'
+ r'(?P<keywords>.*)'
+ r'(?P<post>(?P=quote).*)$')
+
def mangle(name):
"""Decorator to register file mangling methods."""
@@ -62,6 +67,24 @@ class Mangler:
"""Drop EOF whitespace and forcibly add EOF newline."""
return change.update(change.data.rstrip() + '\n')
+ @mangle('keywords')
+ def _keywords(self, change):
+ """Fix keywords order."""
+
+ def keywords_sort_key(kw):
+ return tuple(reversed(kw.lstrip('-~').partition('-')))
+
+ lines = change.data.splitlines()
+ for i, line in enumerate(lines):
+ if mo := keywords_regex.match(line):
+ kw = sorted(mo.group('keywords').split(), key=keywords_sort_key)
+ new_kw = ' '.join(kw)
+ if not mo.group('quote'):
+ new_kw = f'"{new_kw}"'
+ lines[i] = f'{mo.group("pre")}{new_kw}{mo.group("post")}'
+ break
+ return change.update('\n'.join(lines) + '\n')
+
def _kill_pipe(self, *args, error=None):
"""Handle terminating the mangling process group."""
if self._runner.is_alive():