diff options
| author | Sergey <zakharovsergey1000@gmail.com> | 2020-03-15 01:44:31 +0400 |
|---|---|---|
| committer | Sergey <zakharovsergey1000@gmail.com> | 2020-03-28 14:05:50 +0400 |
| commit | 0e8d3a7eab63186b51957bbc133f1420e3dd42f1 (patch) | |
| tree | b7ccfa9e1da6c465c2bcddcf9721753615a7e0cf | |
| parent | ea6ebde0ca303de0ad28e09191d263dc0be3f2c9 (diff) | |
| download | platform_tools_repohooks-0e8d3a7eab63186b51957bbc133f1420e3dd42f1.tar.gz platform_tools_repohooks-0e8d3a7eab63186b51957bbc133f1420e3dd42f1.tar.bz2 platform_tools_repohooks-0e8d3a7eab63186b51957bbc133f1420e3dd42f1.zip | |
Added keyword ${PREUPLOAD_FILES_PREFIXED}.
The keyword is expanded so that each file is preceded by
the same option that precedes this keyword.
Bug: 142016614
Test: unittests pass
Change-Id: I6ca7ee03f252dbc9dafe3bfba7b157e58f8fecd7
| -rw-r--r-- | README.md | 20 | ||||
| -rw-r--r-- | rh/hooks.py | 51 | ||||
| -rwxr-xr-x | rh/hooks_unittest.py | 23 |
3 files changed, 71 insertions, 23 deletions
@@ -103,6 +103,10 @@ such will be expanded correctly via argument positions, so do not try to force your own quote handling. * `${PREUPLOAD_FILES}`: List of files to operate on. +* `${PREUPLOAD_FILES_PREFIXED}`: A list of files to operate on. + Any string preceding/attached to the keyword ${PREUPLOAD_FILES_PREFIXED} + will be repeated for each file automatically. If no string is preceding/attached + to the keyword, the previous argument will be repeated before each file. * `${PREUPLOAD_COMMIT}`: Commit hash. * `${PREUPLOAD_COMMIT_MESSAGE}`: Commit message. @@ -113,6 +117,22 @@ are automatically expanded for you: * `${BUILD_OS}`: The string `darwin-x86` for macOS and the string `linux-x86` for Linux/x86. +### Examples + +Here are some examples of using the placeholders. +Consider this sample config file. +``` +[Hook Scripts] +lister = ls ${PREUPLOAD_FILES} +checker prefix = check --file=${PREUPLOAD_FILES_PREFIXED} +checker flag = check --file ${PREUPLOAD_FILES_PREFIXED} +``` +With a commit that changes `path1/file1` and `path2/file2`, then this will run +programs with the arguments: +* ['ls', 'path1/file1', 'path2/file2'] +* ['check', '--file=path1/file1', '--file=path2/file2'] +* ['check', '--file', 'path1/file1', '--file', 'path2/file2'] + ## [Options] This section allows for setting options that affect the overall behavior of the diff --git a/rh/hooks.py b/rh/hooks.py index bdfcaf0..4a45efa 100644 --- a/rh/hooks.py +++ b/rh/hooks.py @@ -69,26 +69,39 @@ class Placeholders(object): ret = [] for arg in args: - # First scan for exact matches - for key, val in replacements.items(): - var = '${%s}' % (key,) - if arg == var: - if isinstance(val, string_types): - ret.append(val) - else: - ret.extend(val) - # We break on first hit to avoid double expansion. - break + if arg.endswith('${PREUPLOAD_FILES_PREFIXED}'): + if arg == '${PREUPLOAD_FILES_PREFIXED}': + assert len(ret) > 1, ('PREUPLOAD_FILES_PREFIXED cannot be ' + 'the 1st or 2nd argument') + prev_arg = ret[-1] + ret = ret[0:-1] + for file in self.get('PREUPLOAD_FILES'): + ret.append(prev_arg) + ret.append(file) + else: + prefix = arg[0:-len('${PREUPLOAD_FILES_PREFIXED}')] + ret.extend( + prefix + file for file in self.get('PREUPLOAD_FILES')) else: - # If no exact matches, do an inline replacement. - def replace(m): - val = self.get(m.group(1)) - if isinstance(val, string_types): - return val - return ' '.join(val) - ret.append(re.sub(r'\$\{(%s)\}' % ('|'.join(all_vars),), - replace, arg)) - + # First scan for exact matches + for key, val in replacements.items(): + var = '${%s}' % (key,) + if arg == var: + if isinstance(val, string_types): + ret.append(val) + else: + ret.extend(val) + # We break on first hit to avoid double expansion. + break + else: + # If no exact matches, do an inline replacement. + def replace(m): + val = self.get(m.group(1)) + if isinstance(val, string_types): + return val + return ' '.join(val) + ret.append(re.sub(r'\$\{(%s)\}' % ('|'.join(all_vars),), + replace, arg)) return ret @classmethod diff --git a/rh/hooks_unittest.py b/rh/hooks_unittest.py index 9b48119..754a16e 100755 --- a/rh/hooks_unittest.py +++ b/rh/hooks_unittest.py @@ -97,7 +97,9 @@ class PlaceholderTests(unittest.TestCase): 'PREUPLOAD_COMMIT_MESSAGE': 'commit message', 'PREUPLOAD_COMMIT': '5c4c293174bb61f0f39035a71acd9084abfa743d', }) - self.replacer = rh.hooks.Placeholders() + self.replacer = rh.hooks.Placeholders( + [rh.git.RawDiffEntry(file=x) + for x in ['path1/file1', 'path2/file2']]) def tearDown(self): os.environ.clear() @@ -117,9 +119,13 @@ class PlaceholderTests(unittest.TestCase): # We also make sure that things in ${REPO_ROOT} are not double # expanded (which is why the return includes ${BUILD_OS}). '${REPO_ROOT}/some/prog/REPO_ROOT/ok', - # Verify lists are merged rather than inserted. In this case, the - # list is empty, but we'd hit an error still if we saw [] in args. + # Verify lists are merged rather than inserted. '${PREUPLOAD_FILES}', + # Verify each file is preceded with '--file=' prefix. + '--file=${PREUPLOAD_FILES_PREFIXED}', + # Verify each file is preceded with '--file' argument. + '--file', + '${PREUPLOAD_FILES_PREFIXED}', # Verify values with whitespace don't expand into multiple args. '${PREUPLOAD_COMMIT_MESSAGE}', # Verify multiple values get replaced. @@ -130,6 +136,14 @@ class PlaceholderTests(unittest.TestCase): output_args = self.replacer.expand_vars(input_args) exp_args = [ '/ ${BUILD_OS}/some/prog/REPO_ROOT/ok', + 'path1/file1', + 'path2/file2', + '--file=path1/file1', + '--file=path2/file2', + '--file', + 'path1/file1', + '--file', + 'path2/file2', 'commit message', '5c4c293174bb61f0f39035a71acd9084abfa743d^commit message', '${THIS_VAR_IS_GOOD}', @@ -154,7 +168,8 @@ class PlaceholderTests(unittest.TestCase): def testPREUPLOAD_FILES(self): """Verify handling of PREUPLOAD_FILES.""" - self.assertEqual(self.replacer.get('PREUPLOAD_FILES'), []) + self.assertEqual(self.replacer.get('PREUPLOAD_FILES'), + ['path1/file1', 'path2/file2']) @mock.patch.object(rh.git, 'find_repo_root', return_value='/repo!') def testREPO_ROOT(self, m): |
