From 0da94333239f5a82c8b06324df66d59d5cb78fc4 Mon Sep 17 00:00:00 2001 From: Mike Bonnet Date: Jul 15 2021 16:14:57 +0000 Subject: [PATCH 1/3] kojikamid.py: return command output as str, not bytes Under Python 3, output from Popen will be returned as bytes by default. Pass text=True so they're returned as str instead. --- diff --git a/vm/kojikamid.py b/vm/kojikamid.py index 4460e35..b773465 100755 --- a/vm/kojikamid.py +++ b/vm/kojikamid.py @@ -548,11 +548,11 @@ def run(cmd, chdir=None, fatal=False, log=True): logger = logging.getLogger('koji.vm') logger.info('$ %s', ' '.join(cmd)) proc = subprocess.Popen(cmd, stdout=logfd, stderr=subprocess.STDOUT, - close_fds=True) + close_fds=True, text=True) ret = proc.wait() else: proc = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.STDOUT, - close_fds=True) + close_fds=True, text=True) output, dummy = proc.communicate() ret = proc.returncode if olddir: From ddcdde4795ff5fe86ca4f20b3807d25dd0fea0e7 Mon Sep 17 00:00:00 2001 From: Mike Bonnet Date: Jul 15 2021 16:15:12 +0000 Subject: [PATCH 2/3] kojikamid.py: pass the cwd parameter to Popen() Instead of manually changing the directory of the entire process with os.chdir() before and after calling Popen(), pass the cwd parameter to Popen() so only the subprocess runs with a modified working directory. --- diff --git a/vm/kojikamid.py b/vm/kojikamid.py index b773465..c6f27d6 100755 --- a/vm/kojikamid.py +++ b/vm/kojikamid.py @@ -540,23 +540,17 @@ class WindowsBuild(object): def run(cmd, chdir=None, fatal=False, log=True): global logfd output = '' - olddir = None - if chdir: - olddir = os.getcwd() - os.chdir(chdir) if log: logger = logging.getLogger('koji.vm') logger.info('$ %s', ' '.join(cmd)) proc = subprocess.Popen(cmd, stdout=logfd, stderr=subprocess.STDOUT, - close_fds=True, text=True) + close_fds=True, text=True, cwd=chdir) ret = proc.wait() else: proc = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.STDOUT, - close_fds=True, text=True) + close_fds=True, text=True, cwd=chdir) output, dummy = proc.communicate() ret = proc.returncode - if olddir: - os.chdir(olddir) if ret and fatal: msg = 'error running: %s, return code was %s' % (' '.join(cmd), ret) if log: From 6b64215ad941b41dbfa1b413fb4b4eb0144d654e Mon Sep 17 00:00:00 2001 From: Mike Bonnet Date: Jul 15 2021 16:15:12 +0000 Subject: [PATCH 3/3] kojikamid.py: stream log data to the hub as a str base64.b64encode() returns bytes, but the hub API expects data to be provided as a str. Call decode() on the output of b64encode() before uploading it to the hub. --- diff --git a/vm/kojikamid.py b/vm/kojikamid.py index c6f27d6..0911a1c 100755 --- a/vm/kojikamid.py +++ b/vm/kojikamid.py @@ -694,7 +694,7 @@ def stream_logs(server, handler, builds): contents = fd.read(65536) if contents: size = len(contents) - data = base64.b64encode(contents) + data = base64.b64encode(contents).decode() digest = hashlib.sha256(contents).hexdigest() del contents try: