All of lore.kernel.org
 help / color / mirror / Atom feed
From: "Alex Bennée" <alex.bennee@linaro.org>
To: peter.maydell@linaro.org
Cc: "Fam Zheng" <fam@euphon.net>,
	"Daniel P . Berrangé" <berrange@redhat.com>,
	"Philippe Mathieu-Daudé" <philmd@redhat.com>,
	qemu-devel@nongnu.org,
	"Marc-André Lureau" <marcandre.lureau@redhat.com>,
	"Alex Bennée" <alex.bennee@linaro.org>
Subject: [Qemu-devel] [PULL 12/12] tests/docker: upgrade docker.py to python3
Date: Mon,  2 Sep 2019 11:21:22 +0100	[thread overview]
Message-ID: <20190902102122.1128-13-alex.bennee@linaro.org> (raw)
In-Reply-To: <20190902102122.1128-1-alex.bennee@linaro.org>

The recent podman changes (9459f754134bb) imported enum which is part
of the python3 standard library but only available as an external
library for python2. This causes problems on the fairly restricted
environment such as shippable. Lets bite the bullet and make the
script a fully python3 one. To that end:

  - drop the from __future__ import (we are there now ;-)
  - avoid the StringIO import hack
  - be consistent with the mode we read/write dockerfiles
  - s/iteritems/items/
  - ensure check_output returns strings for processing

Signed-off-by: Alex Bennée <alex.bennee@linaro.org>
Reviewed-by: Daniel P. Berrangé <berrange@redhat.com>
Reviewed-by: Philippe Mathieu-Daudé <philmd@redhat.com>
Cc: Marc-André Lureau <marcandre.lureau@redhat.com>

diff --git a/tests/docker/docker.py b/tests/docker/docker.py
index ac5baab4cad..4bba29e104e 100755
--- a/tests/docker/docker.py
+++ b/tests/docker/docker.py
@@ -1,4 +1,4 @@
-#!/usr/bin/env python2
+#!/usr/bin/env python3
 #
 # Docker controlling module
 #
@@ -11,7 +11,6 @@
 # or (at your option) any later version. See the COPYING file in
 # the top-level directory.
 
-from __future__ import print_function
 import os
 import sys
 import subprocess
@@ -25,10 +24,7 @@ import tempfile
 import re
 import signal
 from tarfile import TarFile, TarInfo
-try:
-    from StringIO import StringIO
-except ImportError:
-    from io import StringIO
+from io import StringIO
 from shutil import copy, rmtree
 from pwd import getpwuid
 from datetime import datetime, timedelta
@@ -62,11 +58,13 @@ USE_ENGINE = EngineEnum.AUTO
 
 def _text_checksum(text):
     """Calculate a digest string unique to the text content"""
-    return hashlib.sha1(text).hexdigest()
+    return hashlib.sha1(text.encode('utf-8')).hexdigest()
 
+def _read_dockerfile(path):
+    return open(path, 'rt', encoding='utf-8').read()
 
 def _file_checksum(filename):
-    return _text_checksum(open(filename, 'rb').read())
+    return _text_checksum(_read_dockerfile(filename))
 
 
 def _guess_engine_command():
@@ -192,7 +190,7 @@ def _read_qemu_dockerfile(img_name):
 
     df = os.path.join(os.path.dirname(__file__), "dockerfiles",
                       img_name + ".docker")
-    return open(df, "r").read()
+    return _read_dockerfile(df)
 
 
 def _dockerfile_preprocess(df):
@@ -262,6 +260,7 @@ class Docker(object):
     def _output(self, cmd, **kwargs):
         return subprocess.check_output(self._command + cmd,
                                        stderr=subprocess.STDOUT,
+                                       encoding='utf-8',
                                        **kwargs)
 
     def inspect_tag(self, tag):
@@ -283,7 +282,9 @@ class Docker(object):
         if argv is None:
             argv = []
 
-        tmp_df = tempfile.NamedTemporaryFile(dir=docker_dir, suffix=".docker")
+        tmp_df = tempfile.NamedTemporaryFile(mode="w+t",
+                                             encoding='utf-8',
+                                             dir=docker_dir, suffix=".docker")
         tmp_df.write(dockerfile)
 
         if user:
@@ -396,7 +397,7 @@ class BuildCommand(SubCommand):
                             help="Dockerfile name")
 
     def run(self, args, argv):
-        dockerfile = open(args.dockerfile, "rb").read()
+        dockerfile = _read_dockerfile(args.dockerfile)
         tag = args.tag
 
         dkr = Docker()
@@ -442,7 +443,7 @@ class BuildCommand(SubCommand):
                 cksum += [(filename, _file_checksum(filename))]
 
             argv += ["--build-arg=" + k.lower() + "=" + v
-                     for k, v in os.environ.iteritems()
+                     for k, v in os.environ.items()
                      if k.lower() in FILTERED_ENV_NAMES]
             dkr.build_image(tag, docker_dir, dockerfile,
                             quiet=args.quiet, user=args.user, argv=argv,
@@ -611,7 +612,7 @@ class CheckCommand(SubCommand):
                 print("Need a dockerfile for tag:%s" % (tag))
                 return 1
 
-            dockerfile = open(args.dockerfile, "rb").read()
+            dockerfile = _read_dockerfile(args.dockerfile)
 
             if dkr.image_matches_dockerfile(tag, dockerfile):
                 if not args.quiet:
-- 
2.20.1



  parent reply	other threads:[~2019-09-02 10:36 UTC|newest]

Thread overview: 14+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2019-09-02 10:21 [Qemu-devel] [PULL 00/12] fixes for gdbstub, gitdm and testing Alex Bennée
2019-09-02 10:21 ` [Qemu-devel] [PULL 01/12] gdbstub: Fix handling of '!' packet with new infra Alex Bennée
2019-09-02 10:21 ` [Qemu-devel] [PULL 02/12] gdbstub: Fix handler for 'F' packet Alex Bennée
2019-09-02 10:21 ` [Qemu-devel] [PULL 03/12] contrib/gitdm: filetype interface is not in order, fix Alex Bennée
2019-09-02 10:21 ` [Qemu-devel] [PULL 04/12] contrib/gitdm: Add armbru@pond.sub.org to group-map-redhat Alex Bennée
2019-09-02 10:21 ` [Qemu-devel] [PULL 05/12] mailmap: Reorder by sections Alex Bennée
2019-09-02 10:21 ` [Qemu-devel] [PULL 06/12] mailmap: Update philmd email address Alex Bennée
2019-09-02 10:21 ` [Qemu-devel] [PULL 07/12] mailmap: Add many entries to improve 'git shortlog' statistics Alex Bennée
2019-09-02 10:21 ` [Qemu-devel] [PULL 08/12] .mailmap/aliases: add some further commentary Alex Bennée
2019-09-02 10:21 ` [Qemu-devel] [PULL 09/12] contrib/gitdm: Add RT-RK to the domain-map Alex Bennée
2019-09-02 10:21 ` [Qemu-devel] [PULL 10/12] build-sys: build ui-spice-app as a module Alex Bennée
2019-09-02 10:21 ` [Qemu-devel] [PULL 11/12] tests: fix modules-test with no default machine Alex Bennée
2019-09-02 10:21 ` Alex Bennée [this message]
2019-09-04 11:28 ` [Qemu-devel] [PULL 00/12] fixes for gdbstub, gitdm and testing Peter Maydell

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20190902102122.1128-13-alex.bennee@linaro.org \
    --to=alex.bennee@linaro.org \
    --cc=berrange@redhat.com \
    --cc=fam@euphon.net \
    --cc=marcandre.lureau@redhat.com \
    --cc=peter.maydell@linaro.org \
    --cc=philmd@redhat.com \
    --cc=qemu-devel@nongnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.