1 |
commit: 52fd99ac5d2a5d37767e2ab3fdbaa23ce24badd8 |
2 |
Author: Mike Frysinger <vapier <AT> gentoo <DOT> org> |
3 |
AuthorDate: Wed Mar 8 00:05:24 2017 +0000 |
4 |
Commit: Mike Frysinger <vapier <AT> gentoo <DOT> org> |
5 |
CommitDate: Wed Mar 8 00:05:24 2017 +0000 |
6 |
URL: https://gitweb.gentoo.org/proj/gentoolkit.git/commit/?id=52fd99ac |
7 |
|
8 |
ekeyword: add support for regenerating manifests |
9 |
|
10 |
Since many ebuilds live in overlays with manifest checking turned on, |
11 |
add a flag to easily regen the manifest files after we modify them. |
12 |
|
13 |
src/ekeyword/ekeyword.py | 10 +++++++-- |
14 |
src/ekeyword/ekeyword_unittest.py | 47 ++++++++++++++++++++++++++++----------- |
15 |
2 files changed, 42 insertions(+), 15 deletions(-) |
16 |
|
17 |
diff --git a/src/ekeyword/ekeyword.py b/src/ekeyword/ekeyword.py |
18 |
index 6d09001..31225b0 100755 |
19 |
--- a/src/ekeyword/ekeyword.py |
20 |
+++ b/src/ekeyword/ekeyword.py |
21 |
@@ -44,6 +44,7 @@ import difflib |
22 |
import io |
23 |
import os |
24 |
import re |
25 |
+import subprocess |
26 |
import sys |
27 |
|
28 |
import portage |
29 |
@@ -297,7 +298,7 @@ def process_content(ebuild, data, ops, arch_status=None, verbose=0, |
30 |
|
31 |
|
32 |
def process_ebuild(ebuild, ops, arch_status=None, verbose=0, quiet=0, |
33 |
- dry_run=False, style='color-inline'): |
34 |
+ dry_run=False, style='color-inline', manifest=False): |
35 |
"""Process |ops| for |ebuild| |
36 |
|
37 |
Args: |
38 |
@@ -320,6 +321,8 @@ def process_ebuild(ebuild, ops, arch_status=None, verbose=0, quiet=0, |
39 |
if updated and not dry_run: |
40 |
with io.open(ebuild, 'w', encoding='utf8') as f: |
41 |
f.writelines(content) |
42 |
+ if manifest: |
43 |
+ subprocess.check_call(['ebuild', ebuild, 'manifest']) |
44 |
return updated |
45 |
|
46 |
|
47 |
@@ -461,6 +464,8 @@ def get_parser(): |
48 |
parser = argparse.ArgumentParser( |
49 |
description=__doc__, |
50 |
formatter_class=argparse.RawDescriptionHelpFormatter) |
51 |
+ parser.add_argument('-m', '--manifest', default=False, action='store_true', |
52 |
+ help='Run `ebuild manifest` on the ebuild after modifying it') |
53 |
parser.add_argument('-n', '--dry-run', default=False, action='store_true', |
54 |
help='Show what would be changed, but do not commit') |
55 |
parser.add_argument('-v', '--verbose', action='count', default=0, |
56 |
@@ -523,7 +528,8 @@ def main(argv): |
57 |
for ebuild, ops in work: |
58 |
process_ebuild(ebuild, ops, arch_status=arch_status, |
59 |
verbose=opts.verbose, quiet=opts.quiet, |
60 |
- dry_run=opts.dry_run, style=opts.style) |
61 |
+ dry_run=opts.dry_run, style=opts.style, |
62 |
+ manifest=opts.manifest) |
63 |
|
64 |
return os.EX_OK |
65 |
|
66 |
|
67 |
diff --git a/src/ekeyword/ekeyword_unittest.py b/src/ekeyword/ekeyword_unittest.py |
68 |
index 3465dfb..de40e7a 100755 |
69 |
--- a/src/ekeyword/ekeyword_unittest.py |
70 |
+++ b/src/ekeyword/ekeyword_unittest.py |
71 |
@@ -10,9 +10,12 @@ |
72 |
from __future__ import print_function |
73 |
|
74 |
import os |
75 |
+import subprocess |
76 |
import tempfile |
77 |
import unittest |
78 |
|
79 |
+import mock |
80 |
+ |
81 |
import ekeyword |
82 |
|
83 |
|
84 |
@@ -299,29 +302,47 @@ class TestProcessEbuild(unittest.TestCase): |
85 |
This is fairly light as most code is in process_content. |
86 |
""" |
87 |
|
88 |
- def _test(self, dry_run): |
89 |
- ops = ( |
90 |
- ekeyword.Op(None, 'arm', None), |
91 |
- ekeyword.Op('~', 'sparc', None), |
92 |
- ) |
93 |
+ def _process_ebuild(self, *args, **kwargs): |
94 |
+ """Set up a writable copy of an ebuild for process_ebuild()""" |
95 |
with tempfile.NamedTemporaryFile() as tmp: |
96 |
with open(tmp.name, 'wb') as fw: |
97 |
with open(os.path.join(TESTDIR, 'process-1.ebuild'), 'rb') as f: |
98 |
orig_content = f.read() |
99 |
fw.write(orig_content) |
100 |
- ekeyword.process_ebuild(tmp.name, ops, dry_run=dry_run) |
101 |
+ ekeyword.process_ebuild(tmp.name, *args, **kwargs) |
102 |
with open(tmp.name, 'rb') as f: |
103 |
- new_content = f.read() |
104 |
- if dry_run: |
105 |
- self.assertEqual(orig_content, new_content) |
106 |
- else: |
107 |
- self.assertNotEqual(orig_content, new_content) |
108 |
+ return (orig_content, f.read()) |
109 |
+ |
110 |
+ def _testSmoke(self, dry_run): |
111 |
+ ops = ( |
112 |
+ ekeyword.Op(None, 'arm', None), |
113 |
+ ekeyword.Op('~', 'sparc', None), |
114 |
+ ) |
115 |
+ orig_content, new_content = self._process_ebuild(ops, dry_run=dry_run) |
116 |
+ if dry_run: |
117 |
+ self.assertEqual(orig_content, new_content) |
118 |
+ else: |
119 |
+ self.assertNotEqual(orig_content, new_content) |
120 |
|
121 |
def testSmokeNotDry(self): |
122 |
- self._test(False) |
123 |
+ self._testSmoke(False) |
124 |
|
125 |
def testSmokeDry(self): |
126 |
- self._test(True) |
127 |
+ self._testSmoke(True) |
128 |
+ |
129 |
+ def testManifestUpdated(self): |
130 |
+ """Verify `ebuild ... manifest` runs on updated files""" |
131 |
+ with mock.patch.object(subprocess, 'check_call') as m: |
132 |
+ self._process_ebuild((ekeyword.Op('~', 'arm', None),), |
133 |
+ manifest=True) |
134 |
+ m.assert_called_once_with(['ebuild', mock.ANY, 'manifest']) |
135 |
+ |
136 |
+ def testManifestNotUpdated(self): |
137 |
+ """Verify we don't run `ebuild ... manifest` on unmodified files""" |
138 |
+ with mock.patch.object(subprocess, 'check_call') as m: |
139 |
+ self._process_ebuild((ekeyword.Op(None, 'arm', None),), |
140 |
+ manifest=True) |
141 |
+ self.assertEqual(m.call_count, 0) |
142 |
|
143 |
|
144 |
class TestLoadProfileData(unittest.TestCase): |