11#!/usr/bin/env python3
22
33import asyncio , sys
4- from distutils .util import strtobool
4+
5+ # from distutils.util import strtobool
6+ from utils_tddschn .sync .utils import strtobool
57import datetime
68from asyncio .subprocess import Process
79from pathlib import Path
810
911from git_pp .git_push_to_all_remotes import PathLike , git_push_to_all_remote
1012
1113
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 :
1515 """Run a command in a subprocess and return the output.
1616
1717 Args:
@@ -21,10 +21,8 @@ async def run_command(*args,
2121 str: output of the command
2222 """
2323 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+ )
2826 stdout , _ = await process .communicate ()
2927 if not silent :
3028 print (stdout .decode ())
@@ -34,84 +32,84 @@ async def run_command(*args,
3432
3533
3634def 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" """
3936 # return datetime.datetime.utcnow().isoformat() + 'Z'
4037 # return datetime.datetime.now().isoformat()
4138 now = datetime .datetime .now ()
42- return now .strftime (' %Y-%m-%dT%H-%M-%SZ' )
39+ return now .strftime (" %Y-%m-%dT%H-%M-%SZ" )
4340
4441
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 :
4847 """
4948 Args:
5049 cwd (PathLike | None): working directory
5150 """
5251 print (f'📦 Pre-pulling { cwd or "." } ...' )
5352 # 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 )
5655 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 } " ,
6160 cwd = cwd ,
62- silent = status_only )
61+ silent = status_only ,
62+ )
6363
6464
6565async 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 ]:
7574 """
7675 Args:
7776 force (bool): force push
7877 timeout (int): timeout in seconds
7978 cwd (PathLike | None): working directory
8079 """
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+ )
8984 return rc , cwd
9085
9186
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 :
10196 """
10297 Args:
10398 force (bool): force push
10499 timeout (int): timeout in seconds
105100 cwd (PathLike | None): working directory
106101 """
107102 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
115113 ]
116114 # status_codes = asyncio.gather(*ts)
117115 status_codes = []
@@ -121,20 +119,22 @@ async def git_pre_pull_and_push_to_all_remote_C(dirs: list[PathLike],
121119 # ic(await td.get_coro())
122120 status_code , cwd = await td
123121 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 } ." )
125123 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 } ." )
127125 status_codes .append (status_code )
128126 dirs_s = list (map (str , dirs ))
129127 if not any (status_codes ):
130128 print (
131129 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+ )
133132 return 0
134133 else :
135134 print (
136135 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+ )
138138 return 1
139139 # # all 0
140140 # print(
@@ -153,11 +153,11 @@ async def main() -> None:
153153 await git_pre_pull (
154154 cwd = sys .argv [1 ], # type: ignore
155155 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+ )
158158 else :
159159 await git_pre_pull ()
160160
161161
162- if __name__ == ' __main__' :
163- asyncio .run (main ())
162+ if __name__ == " __main__" :
163+ asyncio .run (main ())
0 commit comments