#40 Make python code version 2/3 agnostic.
Merged 6 years ago by stefw. Opened 6 years ago by merlinm.
Unknown source py3  into  master

Make python code version 2/3 agnostic.
Merlin Mathesius • 6 years ago  
@@ -30,7 +30,7 @@

          else:

              data = list(opts.subjects, opts.docker_extra_args)

          sys.stdout.write(json.dumps(data, indent=4, separators=(',', ': ')))

-     except RuntimeError, ex:

+     except RuntimeError as ex:

          sys.stderr.write("{0}: {1}\n".format(os.path.basename(sys.argv[0]), str(ex)))

          return 1

  
@@ -81,7 +81,7 @@

      ]

      try:

          subprocess.check_call(cmd, stdout=sys.stderr.fileno())

-     except subprocess.CalledProcessError, ex:

+     except subprocess.CalledProcessError as ex:

          raise RuntimeError("Could not start docker service")

  

      # And launch the actual container
@@ -92,7 +92,7 @@

      ]

      try:

          subprocess.check_call(cmd, stdout=sys.stderr.fileno())

-     except subprocess.CalledProcessError, ex:

+     except subprocess.CalledProcessError as ex:

          raise RuntimeError("Could not start container image: {0}".format(image))

  

      # Read out the container environment variable
@@ -113,7 +113,7 @@

      ]

      try:

          subprocess.check_call(install, stdout=sys.stderr.fileno())

-     except subprocess.CalledProcessError, ex:

+     except subprocess.CalledProcessError as ex:

          raise RuntimeError("Could not install Ansible dependencies in launched container")

  

      # Directory to place artifacts

@@ -18,7 +18,7 @@

          else:

              data = list()

          sys.stdout.write(json.dumps(data, indent=4, separators=(',', ': ')))

-     except RuntimeError, ex:

+     except RuntimeError as ex:

          sys.stderr.write("{0}: {1}\n".format(os.path.basename(sys.argv[0]), str(ex)))

          return 1

  

@@ -71,7 +71,7 @@

          else:

              data = list(opts.subjects)

          sys.stdout.write(json.dumps(data, indent=4, separators=(',', ': ')))

-     except RuntimeError, ex:

+     except RuntimeError as ex:

          sys.stderr.write("{0}: {1}\n".format(os.path.basename(sys.argv[0]), str(ex)))

          return 1

  
@@ -118,7 +118,7 @@

      identity = os.path.join(directory, "identity")

      with open(identity, 'w') as f:

          f.write(IDENTITY)

-     os.chmod(identity, 0600)

+     os.chmod(identity, 0o600)

      metadata = os.path.join(directory, "meta-data")

      with open(metadata, 'w') as f:

          f.write("")

@@ -20,7 +20,7 @@

          else:

              data = list(opts.subjects)

          sys.stdout.write(json.dumps(data, indent=4, separators=(',', ': ')))

-     except RuntimeError, ex:

+     except RuntimeError as ex:

          sys.stderr.write("{0}: {1}\n".format(os.path.basename(sys.argv[0]), str(ex)))

          return 1

  

@@ -1,4 +1,4 @@

- #!/usr/bin/python

+ #!/usr/bin/env python

  

  # The purpose of this file is to satisfy /usr/bin/beakerlib-journalling use of "import rpm",

  # particularly on Atomic Host which doesn't have python2-rpm package. beakerlib-journalling

@@ -1,4 +1,4 @@

- #!/usr/bin/python2

+ #!/usr/bin/env python

  

  import json

  import os
@@ -37,7 +37,7 @@

          merged_data = merge_standard_inventories(argv[1:])

  

          # send merged data to parent via output pipe

-         os.write(pipeout, merged_data)

+         os.write(pipeout, merged_data.encode('utf-8'))

  

          # close the pipe so the parent knows we are done

          os.close(pipeout)
@@ -59,7 +59,7 @@

          if not data:

              os.close(pipein)

              break

-         sys.stdout.write(data)

+         sys.stdout.write(data.decode('utf-8'))

  

      return 0

  
@@ -89,7 +89,7 @@

          except subprocess.CalledProcessError as ex:

              raise RuntimeError("Could not run: {0}".format(str(cmd)))

  

-         merged.merge(inv_out)

+         merged.merge(inv_out.decode('utf-8'))

  

      return merged.dumps()

  

no initial comment

Note: With or without this PR, I still encounter a fatal python 3 related error when using ansible-playbook-3 to run tests.yml using a docker test subject. That error appears to be a known issue already being addressed by https://github.com/ansible/ansible/pull/24776#issuecomment-322269809.

Looks good to me. Tested with sed tests (docker and merge inventory) as well as rpm-ostree tests (qcow2). Works well.

Pull-Request has been merged by stefw

6 years ago