From e414979c9bdff3ed87e1b5d2913b877dfd052f58 Mon Sep 17 00:00:00 2001 From: Yikun Jiang Date: Tue, 27 Jul 2021 21:47:16 +0800 Subject: [PATCH] Add mappings --- action.yml | 4 ++++ entrypoint.sh | 3 ++- hub-mirror/__init__.py | 0 hub-mirror/hubmirror.py | 6 +++++- hub-mirror/tests/__init__.py | 0 hub-mirror/tests/test_utils.py | 15 +++++++++++++++ hub-mirror/utils.py | 12 ++++++++++++ 7 files changed, 38 insertions(+), 2 deletions(-) create mode 100644 hub-mirror/__init__.py create mode 100644 hub-mirror/tests/__init__.py create mode 100644 hub-mirror/tests/test_utils.py diff --git a/action.yml b/action.yml index 4db9575d..50af9875 100644 --- a/action.yml +++ b/action.yml @@ -44,6 +44,9 @@ inputs: timeout: description: "Set the timeout for every git command, like '600'=>600s, '30m'=>30 mins, '1h'=>1 hours" default: '30m' + mappings: + description: "The source repos mappings, such as 'A=>B, C=>CC', source repo name would be mapped follow the rule: A to B, C to CC. Mapping is not transitive." + default: '' runs: using: "docker" image: "Dockerfile" @@ -60,3 +63,4 @@ runs: - ${{ inputs.static_list }} - ${{ inputs.force_update}} - ${{ inputs.debug}} + - ${{ inputs.mappings}} diff --git a/entrypoint.sh b/entrypoint.sh index 26ca7f62..fd8603a3 100755 --- a/entrypoint.sh +++ b/entrypoint.sh @@ -22,7 +22,8 @@ python3 /hub-mirror/hubmirror.py --src "${INPUT_SRC}" --dst "${INPUT_DST}" \ --static-list "${INPUT_STATIC_LIST}" \ --force-update "${INPUT_FORCE_UPDATE}" \ --debug "${INPUT_DEBUG}" \ ---timeout "${INPUT_TIMEOUT}" +--timeout "${INPUT_TIMEOUT}" \ +--mappings "${INPUT_MAPPINGS}" # Skip original code exit $? diff --git a/hub-mirror/__init__.py b/hub-mirror/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/hub-mirror/hubmirror.py b/hub-mirror/hubmirror.py index 78e8821c..a4aa74ab 100644 --- a/hub-mirror/hubmirror.py +++ b/hub-mirror/hubmirror.py @@ -2,7 +2,7 @@ import sys import yaml -from utils import str2bool, str2list +from utils import str2bool, str2list, str2map from hub import Hub from mirror import Mirror @@ -14,6 +14,7 @@ def __init__(self): self.white_list = str2list(self.args.white_list) self.black_list = str2list(self.args.black_list) self.static_list = str2list(self.args.static_list) + self.mappings = str2map(self.args.mappings) def _create_parser(self): with open('/action.yml', 'r') as f: @@ -65,6 +66,9 @@ def run(self): total, success, skip = len(src_repos), 0, 0 failed_list = [] for repo in src_repos: + if repo in self.mappings: + print("Map %s to %s" % (repo, self.mappings[repo])) + repo = self.mappings[repo] if self.test_black_white_list(repo): print("Backup %s" % repo) try: diff --git a/hub-mirror/tests/__init__.py b/hub-mirror/tests/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/hub-mirror/tests/test_utils.py b/hub-mirror/tests/test_utils.py new file mode 100644 index 00000000..f50b1571 --- /dev/null +++ b/hub-mirror/tests/test_utils.py @@ -0,0 +1,15 @@ +import unittest +from utils import str2map + + +class TestUtils(unittest.TestCase): + + def test_str2map(self): + self.assertEqual(str2map("a=>b, c=>d"), {'a': 'b', 'c': 'd'}) + self.assertEqual(str2map("a=>b,c=>d"), {'a': 'b', 'c': 'd'}) + # No transitivity + self.assertEqual(str2map("cc=>c,c=>d"), {'cc': 'c', 'c': 'd'}) + + +if __name__ == '__main__': + unittest.main() diff --git a/hub-mirror/utils.py b/hub-mirror/utils.py index c505a414..87d6b0e2 100644 --- a/hub-mirror/utils.py +++ b/hub-mirror/utils.py @@ -35,3 +35,15 @@ def str2list(s): if not s: return [] return s.replace(' ', '').split(',') if s else [] + + +# "a=>b, c=>d" to {'a': 'b', 'c': 'd'} +def str2map(s): + if not s: + return {} + mappings = {} + mappings_list = str2list(s) + for maping in mappings_list: + old, new = maping.split("=>") + mappings[old] = new + return mappings