11from __future__ import annotations
22
33import logging
4+ import subprocess
45from pathlib import Path
56from typing import Callable , Dict , List , Optional , Tuple , Union
67
7- import procrunner
8-
98from murfey .util import Processor
109from murfey .util .file_monitor import Monitor
1110
@@ -32,7 +31,7 @@ def __init__(
3231 self .received_bytes = 0
3332 self .byte_rate : float = 0
3433 self .total_size = 0
35- self .runner_return : List [procrunner . ReturnObject ] = []
34+ self .runner_return : List [subprocess . CompletedProcess ] = []
3635 self ._root = root
3736 self ._sub_structure : Optional [Path ] = None
3837 self ._notify = notify or (lambda f : None )
@@ -53,7 +52,7 @@ def _run_rsync(
5352 retry : bool = True ,
5453 ):
5554 """
56- Run rsync -v on a list of files using procrunner .
55+ Run rsync -v on a list of files using subprocess .
5756
5857 :param root: root path of files for transferring; structure below the root is preserved
5958 :type root: pathlib.Path object
@@ -109,69 +108,64 @@ def _single_rsync(
109108 else :
110109 cmd .append (str (self ._finaldir / sub_struct ) + "/" )
111110 self ._transferring = True
112- runner = procrunner .run (
111+ runner = subprocess .run (
113112 cmd ,
114- callback_stdout = self ._parse_rsync_stdout ,
115- callback_stderr = self ._parse_rsync_stderr ,
113+ capture_output = True ,
116114 )
115+ for line in runner .stdout .decode ("utf-8" , "replace" ).split ("\n " ):
116+ self ._parse_rsync_stdout (line )
117+ for line in runner .stderr .decode ("utf-8" , "replace" ).split ("\n " ):
118+ self ._parse_rsync_stderr (line )
117119 self .runner_return .append (runner )
118120 self .failed .extend (root / sub_struct / f for f in self ._failed_tmp )
119121 if retry :
120122 self ._in .put (root / sub_struct / f for f in self ._failed_tmp )
121123
122- def _parse_rsync_stdout (self , stdout : bytes ):
124+ def _parse_rsync_stdout (self , line : str ):
123125 """
124126 Parse rsync stdout to collect information such as the paths of transferred
125127 files and the amount of data transferred.
126128
127129 :param stdout: stdout of rsync process
128130 :type stdout: bytes
129131 """
130- stringy_stdout = str (stdout )
131- if stringy_stdout :
132- if self ._transferring :
133- if stringy_stdout .startswith ("sent" ):
134- self ._transferring = False
135- byte_info = stringy_stdout .split ()
136- self .sent_bytes = int (
137- byte_info [byte_info .index ("sent" ) + 1 ].replace ("," , "" )
138- )
139- self .received_bytes = int (
140- byte_info [byte_info .index ("received" ) + 1 ].replace ("," , "" )
141- )
142- self .byte_rate = float (
143- byte_info [byte_info .index ("bytes/sec" ) - 1 ].replace ("," , "" )
144- )
145- elif len (stringy_stdout .split ()) == 1 :
146- if self ._root and self ._sub_structure :
147- self ._notify (
148- self ._finaldir / self ._sub_structure / stringy_stdout
149- )
150- self ._out .put (self ._root / self ._sub_structure / stringy_stdout )
151- else :
152- logger .warning (
153- f"root or substructure not set for transfer of { stringy_stdout } "
154- )
155- else :
156- if "total size" in stringy_stdout :
157- self .total_size = int (
158- stringy_stdout .replace ("total size" , "" ).split ()[1 ]
132+ if self ._transferring :
133+ if line .startswith ("sent" ):
134+ self ._transferring = False
135+ byte_info = line .split ()
136+ self .sent_bytes = int (
137+ byte_info [byte_info .index ("sent" ) + 1 ].replace ("," , "" )
138+ )
139+ self .received_bytes = int (
140+ byte_info [byte_info .index ("received" ) + 1 ].replace ("," , "" )
141+ )
142+ self .byte_rate = float (
143+ byte_info [byte_info .index ("bytes/sec" ) - 1 ].replace ("," , "" )
144+ )
145+ elif len (line .split ()) == 1 :
146+ if self ._root and self ._sub_structure :
147+ self ._notify (self ._finaldir / self ._sub_structure / line )
148+ self ._out .put (self ._root / self ._sub_structure / line )
149+ else :
150+ logger .warning (
151+ f"root or substructure not set for transfer of { line } "
159152 )
153+ else :
154+ if "total size" in line :
155+ self .total_size = int (line .replace ("total size" , "" ).split ()[1 ])
160156
161- def _parse_rsync_stderr (self , stderr : bytes ):
157+ def _parse_rsync_stderr (self , line : str ):
162158 """
163159 Parse rsync stderr to collect information on any files that failed to transfer.
164160
165161 :param stderr: stderr of rsync process
166162 :type stderr: bytes
167163 """
168- stringy_stderr = str (stderr )
169- if stringy_stderr :
170- if (
171- stringy_stderr .startswith ("rsync: link_stat" )
172- or stringy_stderr .startswith ("rsync: [sender] link_stat" )
173- ) and "failed" in stringy_stderr :
174- failed_msg = stringy_stderr .split ()
175- self ._failed_tmp .append (
176- failed_msg [failed_msg .index ("failed:" ) - 1 ].replace ('"' , "" )
177- )
164+ if (
165+ line .startswith ("rsync: link_stat" )
166+ or line .startswith ("rsync: [sender] link_stat" )
167+ ) and "failed" in line :
168+ failed_msg = line .split ()
169+ self ._failed_tmp .append (
170+ failed_msg [failed_msg .index ("failed:" ) - 1 ].replace ('"' , "" )
171+ )
0 commit comments