Skip to content

Commit 4f842ee

Browse files
committed
Add keywords reordering mangler
Automatically reorder keywords to be sorted with OS and arch. Based on nattka's code at nattka/keyword.py Resolves: #46 Signed-off-by: Arthur Zamarin <arthurzam@gentoo.org>
1 parent 59e2320 commit 4f842ee

1 file changed

Lines changed: 23 additions & 0 deletions

File tree

src/pkgdev/mangle.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,11 @@
1414
copyright_regex = re.compile(
1515
r'^# Copyright (?P<date>(?P<begin>\d{4}-)?(?P<end>\d{4})) (?P<holder>.+)$')
1616

17+
keywords_regex = re.compile(
18+
r'^(?P<pre>[^#]*\bKEYWORDS=(?P<quote>[\'"]?))'
19+
r'(?P<keywords>.*)'
20+
r'(?P<post>(?P=quote).*)$')
21+
1722

1823
def mangle(name):
1924
"""Decorator to register file mangling methods."""
@@ -62,6 +67,24 @@ def _eof(self, change):
6267
"""Drop EOF whitespace and forcibly add EOF newline."""
6368
return change.update(change.data.rstrip() + '\n')
6469

70+
@mangle('keywords')
71+
def _keywords(self, change):
72+
"""Fix keywords order."""
73+
74+
def keywords_sort_key(kw):
75+
return tuple(reversed(kw.lstrip('-~').partition('-')))
76+
77+
lines = change.data.splitlines()
78+
for i, line in enumerate(lines):
79+
if mo := keywords_regex.match(line):
80+
kw = sorted(mo.group('keywords').split(), key=keywords_sort_key)
81+
new_kw = ' '.join(kw)
82+
if not mo.group('quote'):
83+
new_kw = f'"{new_kw}"'
84+
lines[i] = f'{mo.group("pre")}{new_kw}{mo.group("post")}'
85+
break
86+
return change.update('\n'.join(lines) + '\n')
87+
6588
def _kill_pipe(self, *args, error=None):
6689
"""Handle terminating the mangling process group."""
6790
if self._runner.is_alive():

0 commit comments

Comments
 (0)