1
1
#!/usr/bin/env python3
2
2
3
3
import asyncio , sys
4
- from distutils .util import strtobool
4
+
5
+ # from distutils.util import strtobool
6
+ from utils_tddschn .sync .utils import strtobool
5
7
import datetime
6
8
from asyncio .subprocess import Process
7
9
from pathlib import Path
8
10
9
11
from git_pp .git_push_to_all_remotes import PathLike , git_push_to_all_remote
10
12
11
13
12
- async def run_command (* args ,
13
- cwd : PathLike | None = None ,
14
- silent : bool = False ) -> int :
14
+ async def run_command (* args , cwd : PathLike | None = None , silent : bool = False ) -> int :
15
15
"""Run a command in a subprocess and return the output.
16
16
17
17
Args:
@@ -21,10 +21,8 @@ async def run_command(*args,
21
21
str: output of the command
22
22
"""
23
23
process : Process = await asyncio .create_subprocess_exec (
24
- * args ,
25
- cwd = cwd ,
26
- stdout = asyncio .subprocess .PIPE ,
27
- stderr = asyncio .subprocess .PIPE )
24
+ * args , cwd = cwd , stdout = asyncio .subprocess .PIPE , stderr = asyncio .subprocess .PIPE
25
+ )
28
26
stdout , _ = await process .communicate ()
29
27
if not silent :
30
28
print (stdout .decode ())
@@ -34,84 +32,84 @@ async def run_command(*args,
34
32
35
33
36
34
def get_iso8601_timestamp ():
37
- """get current time in this format "%Y-%m-%dT%H-%M-%SZ"
38
- """
35
+ """get current time in this format "%Y-%m-%dT%H-%M-%SZ" """
39
36
# return datetime.datetime.utcnow().isoformat() + 'Z'
40
37
# return datetime.datetime.now().isoformat()
41
38
now = datetime .datetime .now ()
42
- return now .strftime (' %Y-%m-%dT%H-%M-%SZ' )
39
+ return now .strftime (" %Y-%m-%dT%H-%M-%SZ" )
43
40
44
41
45
- async def git_pre_pull (cwd : PathLike | None = None ,
46
- commit_message : str | None = None ,
47
- status_only : bool = False ) -> None :
42
+ async def git_pre_pull (
43
+ cwd : PathLike | None = None ,
44
+ commit_message : str | None = None ,
45
+ status_only : bool = False ,
46
+ ) -> None :
48
47
"""
49
48
Args:
50
49
cwd (PathLike | None): working directory
51
50
"""
52
51
print (f'📦 Pre-pulling { cwd or "." } ...' )
53
52
# https://unix.stackexchange.com/questions/46814/watch-command-not-showing-colors-for-git-status
54
- await run_command (' git' , '-c' , ' color.status=always' , ' status' , cwd = cwd )
55
- await run_command (' git' , ' add' , ' --all' , cwd = cwd , silent = status_only )
53
+ await run_command (" git" , "-c" , " color.status=always" , " status" , cwd = cwd )
54
+ await run_command (" git" , " add" , " --all" , cwd = cwd , silent = status_only )
56
55
await run_command (
57
- ' git' ,
58
- ' commit' ,
59
- '-m' ,
60
- f' { get_iso8601_timestamp () if not commit_message else commit_message } ' ,
56
+ " git" ,
57
+ " commit" ,
58
+ "-m" ,
59
+ f" { get_iso8601_timestamp () if not commit_message else commit_message } " ,
61
60
cwd = cwd ,
62
- silent = status_only )
61
+ silent = status_only ,
62
+ )
63
63
64
64
65
65
async def git_pre_pull_and_push_to_all_remote (
66
- commit_message : str
67
- | None = None ,
68
- status_only : bool = False ,
69
- remotes : list [str ]
70
- | None = None ,
71
- branch : str | None = None ,
72
- force = False ,
73
- timeout = None ,
74
- cwd : PathLike | None = None ) -> tuple [int , PathLike | None ]:
66
+ commit_message : str | None = None ,
67
+ status_only : bool = False ,
68
+ remotes : list [str ] | None = None ,
69
+ branch : str | None = None ,
70
+ force = False ,
71
+ timeout = None ,
72
+ cwd : PathLike | None = None ,
73
+ ) -> tuple [int , PathLike | None ]:
75
74
"""
76
75
Args:
77
76
force (bool): force push
78
77
timeout (int): timeout in seconds
79
78
cwd (PathLike | None): working directory
80
79
"""
81
- await git_pre_pull (commit_message = commit_message ,
82
- status_only = status_only ,
83
- cwd = cwd )
84
- rc , _ , _ = await git_push_to_all_remote (remotes = remotes ,
85
- branch = branch ,
86
- force = force ,
87
- timeout = timeout ,
88
- cwd = cwd )
80
+ await git_pre_pull (commit_message = commit_message , status_only = status_only , cwd = cwd )
81
+ rc , _ , _ = await git_push_to_all_remote (
82
+ remotes = remotes , branch = branch , force = force , timeout = timeout , cwd = cwd
83
+ )
89
84
return rc , cwd
90
85
91
86
92
- async def git_pre_pull_and_push_to_all_remote_C (dirs : list [ PathLike ],
93
- commit_message : str
94
- | None = None ,
95
- status_only : bool = False ,
96
- remotes : list [str ]
97
- | None = None ,
98
- branch : str | None = None ,
99
- force = False ,
100
- timeout = None ) -> int :
87
+ async def git_pre_pull_and_push_to_all_remote_C (
88
+ dirs : list [ PathLike ],
89
+ commit_message : str | None = None ,
90
+ status_only : bool = False ,
91
+ remotes : list [str ] | None = None ,
92
+ branch : str | None = None ,
93
+ force = False ,
94
+ timeout = None ,
95
+ ) -> int :
101
96
"""
102
97
Args:
103
98
force (bool): force push
104
99
timeout (int): timeout in seconds
105
100
cwd (PathLike | None): working directory
106
101
"""
107
102
coros = [
108
- git_pre_pull_and_push_to_all_remote (commit_message = commit_message ,
109
- status_only = status_only ,
110
- force = force ,
111
- remotes = remotes ,
112
- branch = branch ,
113
- timeout = timeout ,
114
- cwd = cwd ) for cwd in dirs
103
+ git_pre_pull_and_push_to_all_remote (
104
+ commit_message = commit_message ,
105
+ status_only = status_only ,
106
+ force = force ,
107
+ remotes = remotes ,
108
+ branch = branch ,
109
+ timeout = timeout ,
110
+ cwd = cwd ,
111
+ )
112
+ for cwd in dirs
115
113
]
116
114
# status_codes = asyncio.gather(*ts)
117
115
status_codes = []
@@ -121,20 +119,22 @@ async def git_pre_pull_and_push_to_all_remote_C(dirs: list[PathLike],
121
119
# ic(await td.get_coro())
122
120
status_code , cwd = await td
123
121
if status_code == 0 :
124
- print (f' ✓ Pre-pulled and pushed to all remotes of { cwd } .' )
122
+ print (f" ✓ Pre-pulled and pushed to all remotes of { cwd } ." )
125
123
else :
126
- print (f' 𐄂 Failed to pre-pull and push to all_remotes of { cwd } .' )
124
+ print (f" 𐄂 Failed to pre-pull and push to all_remotes of { cwd } ." )
127
125
status_codes .append (status_code )
128
126
dirs_s = list (map (str , dirs ))
129
127
if not any (status_codes ):
130
128
print (
131
129
f'✅ Pushed { "current branch" if branch is None else branch } of { dirs_s } to all of their remotes successfully.'
132
- + ('(FORCE)' if force else '' ))
130
+ + ("(FORCE)" if force else "" )
131
+ )
133
132
return 0
134
133
else :
135
134
print (
136
135
f'❌ Failed to push { "current branch" if branch is None else branch } of { dirs_s } to all of their remotes.'
137
- + ('(FORCE)' if force else '' ))
136
+ + ("(FORCE)" if force else "" )
137
+ )
138
138
return 1
139
139
# # all 0
140
140
# print(
@@ -153,11 +153,11 @@ async def main() -> None:
153
153
await git_pre_pull (
154
154
cwd = sys .argv [1 ], # type: ignore
155
155
commit_message = sys .argv [2 ] if len (sys .argv ) > 2 else None ,
156
- status_only = bool (strtobool (sys .argv [3 ]))
157
- if len ( sys . argv ) > 3 else False )
156
+ status_only = bool (strtobool (sys .argv [3 ])) if len ( sys . argv ) > 3 else False ,
157
+ )
158
158
else :
159
159
await git_pre_pull ()
160
160
161
161
162
- if __name__ == ' __main__' :
163
- asyncio .run (main ())
162
+ if __name__ == " __main__" :
163
+ asyncio .run (main ())
0 commit comments