forked from DeepPSP/torch_ecg
-
Notifications
You must be signed in to change notification settings - Fork 0
/
exec_git.py
100 lines (89 loc) · 2.2 KB
/
exec_git.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
"""
to cope with DNS spoofing, automatically retry a git command until success
"""
import argparse
import os
import time
import warnings
from typing import NoReturn
_CMD = {
"submodule": "git submodule update --remote --recursive --merge",
"push": "git push origin --all",
"fetch": "git fetch --all",
}
def get_parser() -> dict:
""" """
description = (
"predefined and custom git operations, in order to cope with DNS spoofing"
)
parser = argparse.ArgumentParser(
description=description,
formatter_class=argparse.ArgumentDefaultsHelpFormatter,
)
parser.add_argument(
"-s",
"--submodule",
action="store_true",
help="submodule update",
dest="submodule",
)
parser.add_argument(
"-f",
"--fetch",
action="store_true",
help="fetch origin",
dest="fetch",
)
parser.add_argument(
"-p",
"--push",
action="store_true",
help="push origin",
dest="push",
)
parser.add_argument(
"-c",
"--custom",
type=str,
help='custom git command, enclosed within "',
dest="custom",
)
args = vars(parser.parse_args())
return args
def run(action: str) -> NoReturn:
"""
Parameters
----------
action: str,
git command (action, operation) to be executed
"""
cmd = _CMD.get(action.lower(), action.lower())
ret_code = None
n_iter = 0
while ret_code != 0:
if n_iter > 0:
print(f"""retry {n_iter} {"times" if n_iter > 1 else "time"}""")
time.sleep(1)
ret_code = os.system(cmd)
n_iter += 1
print("Execution success")
if __name__ == "__main__":
args = get_parser()
custom = args.get("custom", None)
args = [
k
for k, v in args.items()
if v
and k
not in [
"custom",
]
]
if custom:
if len(args) > 0:
warnings.warn("custom command is given, additional arguments are ignored!")
run(custom)
exit(0)
if len(args) != 1:
raise ValueError("one and only one predefined command can be executed!")
run(args[0])