@@ -21,62 +21,28 @@ async def execute(
21
21
cmdline : "List[str]" ,
22
22
cwd : "Optional[str]" = None ,
23
23
env : "Optional[Dict[str, str]]" = None ,
24
+ username : "Optional[str]" = None ,
25
+ password : "Optional[str]" = None ,
24
26
) -> "Tuple[int, str, str]" :
25
27
"""Asynchronously execute a command.
26
28
27
29
Args:
28
30
cmdline (List[str]): Command line to be executed
29
31
cwd (Optional[str]): Current working directory
30
- env (Optional[Dict[str, str]]): Defines the environment variables for the new process.
32
+ env (Optional[Dict[str, str]]): Defines the environment variables for the new process
33
+ username (Optional[str]): User name
34
+ password (Optional[str]): User password
31
35
Returns:
32
36
(int, str, str): (return code, stdout, stderr)
33
37
"""
34
38
35
- def call_subprocess (
36
- cmdline : "List[str]" ,
37
- cwd : "Optional[str]" = None ,
38
- env : "Optional[Dict[str, str]]" = None ,
39
- ) -> "Tuple[int, bytes, bytes]" :
40
- process = subprocess .Popen (
41
- cmdline , stdout = subprocess .PIPE , stderr = subprocess .PIPE , cwd = cwd , env = env
42
- )
43
- output , error = process .communicate ()
44
- return (process .returncode , output , error )
45
-
46
- current_loop = tornado .ioloop .IOLoop .current ()
47
- returncode , output , error = await current_loop .run_in_executor (
48
- None , call_subprocess , cmdline , cwd , env
49
- )
50
-
51
- return returncode , output .decode ("utf-8" ), error .decode ("utf-8" )
52
-
53
-
54
- async def execute_with_authentication (
55
- cmdline : "List[str]" ,
56
- username : "str" ,
57
- password : "str" ,
58
- cwd : "Optional[str]" = None ,
59
- env : "Optional[Dict[str, str]]" = None ,
60
- ) -> "Tuple[int, str]" :
61
- """Asynchronously execute a command.
62
-
63
- Args:
64
- cmdline (List[str]): Command line to be executed
65
- username (str): User name
66
- password (str): User password
67
- cwd (Optional[str]): Current working directory
68
- env (Optional[Dict[str, str]]): Defines the environment variables for the new process.
69
- Returns:
70
- (int, str): (return code, output)
71
- """
72
-
73
- def call_subprocess (
39
+ def call_subprocess_with_authentication (
74
40
cmdline : "List[str]" ,
75
41
username : "str" ,
76
42
password : "str" ,
77
43
cwd : "Optional[str]" = None ,
78
44
env : "Optional[Dict[str, str]]" = None ,
79
- ) -> "Tuple[int, str]" :
45
+ ) -> "Tuple[int, str, str ]" :
80
46
try :
81
47
p = pexpect .spawn (
82
48
cmdline [0 ], cmdline [1 :], cwd = cwd , env = env , encoding = "utf-8"
@@ -101,19 +67,34 @@ def call_subprocess(
101
67
returncode = p .wait ()
102
68
p .close ()
103
69
104
- return returncode , response
70
+ return returncode , "" , response
105
71
except pexpect .exceptions .EOF : # In case of pexpect failure
106
72
response = p .before
107
73
returncode = p .exitstatus
108
74
p .close () # close process
109
- return returncode , response
75
+ return returncode , "" , response
110
76
111
- current_loop = tornado .ioloop .IOLoop .current ()
112
- returncode , output = await current_loop .run_in_executor (
113
- None , call_subprocess , cmdline , username , password , cwd , env
114
- )
115
77
116
- return returncode , output
78
+ def call_subprocess (
79
+ cmdline : "List[str]" ,
80
+ cwd : "Optional[str]" = None ,
81
+ env : "Optional[Dict[str, str]]" = None ,
82
+ ) -> "Tuple[int, str, str]" :
83
+ process = subprocess .Popen (
84
+ cmdline , stdout = subprocess .PIPE , stderr = subprocess .PIPE , cwd = cwd , env = env
85
+ )
86
+ output , error = process .communicate ()
87
+ return (process .returncode , output .decode ("utf-8" ), error .decode ("utf-8" ))
88
+
89
+ current_loop = tornado .ioloop .IOLoop .current ()
90
+ if username is not None and password is not None :
91
+ return await current_loop .run_in_executor (
92
+ None , call_subprocess_with_authentication , cmdline , username , password , cwd , env
93
+ )
94
+ else :
95
+ return await current_loop .run_in_executor (
96
+ None , call_subprocess , cmdline , cwd , env
97
+ )
117
98
118
99
119
100
class Git :
@@ -219,7 +200,7 @@ async def clone(self, current_path, repo_url, auth=None):
219
200
env = os .environ .copy ()
220
201
if auth :
221
202
env ["GIT_TERMINAL_PROMPT" ] = "1"
222
- code , error = await execute_with_authentication (
203
+ code , _ , error = await execute (
223
204
["git" , "clone" , unquote (repo_url ), "-q" ],
224
205
username = auth ["username" ],
225
206
password = auth ["password" ],
@@ -722,7 +703,7 @@ async def pull(self, curr_fb_path, auth=None):
722
703
env = os .environ .copy ()
723
704
if auth :
724
705
env ["GIT_TERMINAL_PROMPT" ] = "1"
725
- code , error = await execute_with_authentication (
706
+ code , _ , error = await execute (
726
707
["git" , "pull" , "--no-commit" ],
727
708
username = auth ["username" ],
728
709
password = auth ["password" ],
@@ -751,7 +732,7 @@ async def push(self, remote, branch, curr_fb_path, auth=None):
751
732
env = os .environ .copy ()
752
733
if auth :
753
734
env ["GIT_TERMINAL_PROMPT" ] = "1"
754
- code , error = await execute_with_authentication (
735
+ code , _ , error = await execute (
755
736
["git" , "push" , remote , branch ],
756
737
username = auth ["username" ],
757
738
password = auth ["password" ],
0 commit comments