Skip to content

Commit

Permalink
Add mappings
Browse files Browse the repository at this point in the history
  • Loading branch information
Yikun committed Jul 27, 2021
1 parent 132b206 commit eb2917b
Show file tree
Hide file tree
Showing 8 changed files with 55 additions and 14 deletions.
4 changes: 4 additions & 0 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -60,3 +63,4 @@ runs:
- ${{ inputs.static_list }}
- ${{ inputs.force_update}}
- ${{ inputs.debug}}
- ${{ inputs.mappings}}
3 changes: 2 additions & 1 deletion entrypoint.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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 $?
Empty file added hub-mirror/__init__.py
Empty file.
17 changes: 11 additions & 6 deletions hub-mirror/hubmirror.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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:
Expand Down Expand Up @@ -64,12 +65,16 @@ def run(self):

total, success, skip = len(src_repos), 0, 0
failed_list = []
for repo in src_repos:
if self.test_black_white_list(repo):
print("Backup %s" % repo)
for src_repo in src_repos:
dst_repo = src_repo
if src_repo in self.mappings:
dst_repo = self.mappings[src_repo]
print("Map %s to %s" % (src_repo, dst_repo))
if self.test_black_white_list(src_repo):
print("Backup %s" % src_repo)
try:
mirror = Mirror(
hub, repo,
hub, src_repo, dst_repo,
cache=self.args.cache_path,
timeout=self.args.timeout,
force_update=self.args.force_update,
Expand All @@ -80,7 +85,7 @@ def run(self):
success += 1
except Exception as e:
print(e)
failed_list.append(repo)
failed_list.append(src_repo)
else:
skip += 1
failed = total - success - skip
Expand Down
18 changes: 11 additions & 7 deletions hub-mirror/mirror.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,16 @@


class Mirror(object):
def __init__(self, hub, name, cache='.', timeout='0', force_update=False):
def __init__(
self, hub, src_name, dst_name,
cache='.', timeout='0', force_update=False
):
self.hub = hub
self.name = name
self.src_url = hub.src_repo_base + '/' + name + ".git"
self.dst_url = hub.dst_repo_base + '/' + name + ".git"
self.repo_path = cache + '/' + name
self.src_name = src_name
self.dst_name = dst_name
self.src_url = hub.src_repo_base + '/' + src_name + ".git"
self.dst_url = hub.dst_repo_base + '/' + dst_name + ".git"
self.repo_path = cache + '/' + src_name
if re.match(r"^\d+[dhms]?$", timeout):
self.timeout = cov2sec(timeout)
else:
Expand All @@ -38,7 +42,7 @@ def _update(self, local_repo):
local_repo.git.pull(kill_after_timeout=self.timeout)
except git.exc.GitCommandError:
# Cleanup local repo and re-clone
print('Updating failed, re-clone %s' % self.name)
print('Updating failed, re-clone %s' % self.src_name)
shutil.rmtree(local_repo.working_dir)
self._clone()

Expand All @@ -55,7 +59,7 @@ def download(self):

def create(self):
print("(2/3) Creating...")
self.hub.create_dst_repo(self.name)
self.hub.create_dst_repo(self.dst_name)

def _check_empty(self, repo):
cmd = ["-n", "1", "--all"]
Expand Down
Empty file added hub-mirror/tests/__init__.py
Empty file.
15 changes: 15 additions & 0 deletions hub-mirror/tests/test_utils.py
Original file line number Diff line number Diff line change
@@ -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()
12 changes: 12 additions & 0 deletions hub-mirror/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit eb2917b

Please sign in to comment.