Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add keywords reordering mangler #47

Merged
merged 2 commits into from
Mar 25, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions src/pkgdev/mangle.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,11 @@
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."""
Expand Down Expand Up @@ -62,6 +67,24 @@ def _eof(self, change):
"""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():
Expand Down
23 changes: 22 additions & 1 deletion tests/scripts/test_pkgdev_commit.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
from unittest.mock import patch

import pytest
from pkgdev.mangle import copyright_regex
from pkgdev.mangle import copyright_regex, keywords_regex
from pkgdev.scripts import run
from snakeoil.contexts import chdir, os_environ
from snakeoil.osutils import pjoin
Expand Down Expand Up @@ -808,6 +808,27 @@ def commit(args):
assert mo.group('end') == str(datetime.today().year)
assert mo.group('holder') == 'Gentoo Authors'

for original, expected in (
('"arm64 amd64 x86"', 'amd64 arm64 x86'),
('"arm64 amd64 ~x86"', 'amd64 arm64 ~x86'),
('"arm64 ~x86 amd64"', 'amd64 arm64 ~x86'),
('"arm64 ~x86 ~amd64"', '~amd64 arm64 ~x86'),
('arm64 ~x86 ~amd64', '~amd64 arm64 ~x86'),
):
# munge the keywords
with open(ebuild_path, 'r+') as f:
lines = f.read().splitlines()
lines[-1] = f'KEYWORDS={original}'
f.seek(0)
f.truncate()
f.write('\n'.join(lines) + '\n')
commit(['-n', '-u', '-m', 'mangling'])
# verify the keywords were updated
with open(ebuild_path) as f:
lines = f.read().splitlines()
mo = keywords_regex.match(lines[-1])
assert mo.group('keywords') == expected

def test_scan(self, capsys, repo, make_git_repo):
git_repo = make_git_repo(repo.location)
repo.create_ebuild('cat/pkg-0')
Expand Down