Skip to content
This repository has been archived by the owner on Jan 14, 2024. It is now read-only.

Commit

Permalink
Fighting with IO/Subprocess
Browse files Browse the repository at this point in the history
  • Loading branch information
blackandred committed May 13, 2020
1 parent c075d08 commit b5bdc01
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 19 deletions.
2 changes: 1 addition & 1 deletion src/rkd/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ def main():
app.main(argv=sys.argv)
except TaskNotFoundException as e:
print(e)
sys.exit(1)
sys.exit(127)


if __name__ == '__main__':
Expand Down
15 changes: 15 additions & 0 deletions src/rkd/inputoutput.py
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,21 @@ def error_msg(self, text):
def info_msg(self, text):
self.opt_outln("\x1B[93m%s\x1B[0m" % text)

#
# Standard formatting
#
def h1(self, text):
self.opt_outln("\x1B[93m ##> %s\x1B[0m" % text)

def h2(self, text):
self.opt_outln("\x1B[93m ===> %s\x1B[0m" % text)

def h3(self, text):
self.opt_outln("\x1B[33m --> %s\x1B[0m" % text)

def h4(self, text):
self.opt_outln("\x1B[33m ... %s\x1B[0m" % text)


class SystemIO(IO):
""" Used for logging outside of tasks """
Expand Down
2 changes: 1 addition & 1 deletion src/rkd/standardlib/docker.py
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ def configure_argparse(self, parser: ArgumentParser):
parser.add_argument('--without-global-latest', '-wgl', help='Do not tag :latest', action='store_true')
parser.add_argument('--propagate', '-p', help='Propagate tags? eg. 1.0.0 -> 1.0 -> 1 -> latest', action='store_true')
parser.add_argument('--allowed-meta', '-m', help='Allowed meta part eg. rc, alpha, beta',
default='rc,alpha,stable,dev,prod,test,beta,build,b,snapshot')
default='rc,alpha,stable,dev,prod,test,beta,build,b')


class TagImageTask(DockerBaseTask):
Expand Down
36 changes: 19 additions & 17 deletions src/rkd/taskutil.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,27 +56,29 @@ def sh(self, cmd: str, capture: bool = False, verbose: bool = False, strict: boo
os.close(write)

if not capture:
process = Popen('bash', shell=True, stdin=read, stdout=SUBPROCESS_PIPE, stderr=SUBPROCESS_STDOUT)

stdout_thread = Thread(target=self._copy_stream, args=(process.stdout, sys.stdout, process))
stdout_thread.daemon = True
stdout_thread.start()
stderr = ''

exit_code = process.wait()

if exit_code > 0:
raise CalledProcessError(exit_code, cmd, None, stderr)

# process = Popen('bash', shell=True, stdin=read, stdout=SUBPROCESS_PIPE, stderr=SUBPROCESS_STDOUT, bufsize=1)
#
# stdout_thread = Thread(target=self._copy_stream, args=(process.stdout, sys.stdout, process))
# stdout_thread.daemon = True
# stdout_thread.start()
#
# exit_code = process.wait()
#
# if exit_code > 0:
# raise CalledProcessError(exit_code, cmd)

check_call('bash', shell=True, stdin=read)
return

return check_output('bash', shell=True, stdin=read).decode('utf-8')

@staticmethod
def _copy_stream(in_stream, out_stream, process: Popen):
while process.poll() is None:
for line in iter(in_stream.readline, ''):
out_stream.write(line.decode('utf-8'))
# @staticmethod
# def _copy_stream(in_stream, out_stream, process: Popen):
# while process.poll() is None:
# for line in iter(in_stream.readline, ''):
# out_stream.write(line.decode('utf-8'))
#
# out_stream.write(in_stream.read().decode('utf-8'))

def exec(self, cmd: str, capture: bool = False, background: bool = False) -> Union[str, None]:
""" Starts a process in shell. Throws exception on error.
Expand Down

0 comments on commit b5bdc01

Please sign in to comment.