#11 Add support for Python 3
Opened a year ago by qulogic. Modified a year ago
qulogic/r2spec py3  into  master

file modified
+4 -2

@@ -19,6 +19,8 @@ 

  

  """ R2rpm launcher script """

  

+ from __future__ import absolute_import, division, print_function

+ 

  from r2spec.r2spec_obj import R2rpm, setup_parser

  from r2spec import R2specError

  

@@ -27,5 +29,5 @@ 

      ARG = PARSER.parse_args()

      try:

          R2rpm().main(ARG)

-     except R2specError, err:

-         print err

+     except R2specError as err:

+         print(err)

file modified
+4 -2

@@ -19,6 +19,8 @@ 

  

  """ R2spec launcher script """

  

+ from __future__ import absolute_import, division, print_function

+ 

  from r2spec.r2spec_obj import R2spec, setup_parser

  from r2spec import R2specError

  

@@ -27,5 +29,5 @@ 

      ARG = PARSER.parse_args()

      try:

          R2spec().main(ARG)

-     except R2specError, err:

-         print err

+     except R2specError as err:

+         print(err)

file modified
+6 -3

@@ -19,6 +19,7 @@ 

  

  """ Miscellaneous functions used in R2spec. """

  

+ from __future__ import absolute_import, division, print_function

  

  import logging

  import shutil

@@ -40,7 +41,8 @@ 

      Code from José Matos.

      :arg tag, the rpm tag to find the value of

      """

-     dirname = Popen(["rpm", "-E", '%' + tag], stdout=PIPE).stdout.read()[:-1]

+     dirname = Popen(["rpm", "-E", '%' + tag],

+                     stdout=PIPE, universal_newlines=True).stdout.read()[:-1]

      return dirname

  

  

@@ -48,7 +50,8 @@ 

      """" Calls mock to retrieve the root path and return it minus the last

      two levels.

      """

-     dirname = Popen(["mock", "--print-root-path"], stdout=PIPE).stdout.read()[:-1]

+     dirname = Popen(["mock", "--print-root-path"],

+                     stdout=PIPE, universal_newlines=True).stdout.read()[:-1]

      return dirname.rsplit('/', 3)[0] + '/'

  

  

@@ -64,7 +67,7 @@ 

      shutil.copyfile(fullpath, dest)

  

  

- class R2specError:

+ class R2specError(Exception):

      """ R2specError class

      Template for all the error of the project

      """

file modified
+4 -3

@@ -19,6 +19,7 @@ 

  

  """ Build related class and methods. """

  

+ from __future__ import absolute_import, division, print_function

  

  import os

  import subprocess

@@ -108,7 +109,7 @@ 

                          self.rpm.append(rpm)

                      else:

                          self.rpm.append(rpm)

-         except IOError, err:

+         except IOError as err:

              self.log.info('Could not read the file "%s"' % self.buildlog)

              self.log.debug('ERROR: %s' % err)

  

@@ -131,10 +132,10 @@ 

              stream = open(self.buildlog, 'r')

              log = stream.read()

              stream.close()

-         except IOError, err:

+         except IOError as err:

              self.log.info('An error occured during the build')

              self.log.debug('ERROR: %s' % err)

-             print 'Stopping'

+             print('Stopping')

              return(1)

  

          logs = log.split('\n')

file modified
+21 -17

@@ -20,15 +20,19 @@ 

  Main functions for R2spec

  """

  

+ from __future__ import absolute_import, division, print_function

  

  import argparse

- import ConfigParser

  import logging

  import os

  import re

  import sys

  import pwd 

- from urlparse import urlparse

+ 

+ try:

+     import configparser

+ except ImportError:

+     import ConfigParser as configparser

  

  from r2spec.build import Build

  from r2spec.rpackage import RPackage

@@ -135,7 +139,7 @@ 

      except OSError:

          # os.getlogin() raises an exception when the session is not

          # in /var/run/utmp (non-login sessions)

-         if os.environ.has_key('LOGNAME'):

+         if 'LOGNAME' in os.environ:

              packager = os.environ['LOGNAME']

          else:

              packager = pwd.getpwuid(os.getuid())[0]

@@ -160,7 +164,7 @@ 

          :arg configfile, name of the configuration file loaded.

          :arg sec, section of the configuration retrieved.

          """

-         parser = ConfigParser.ConfigParser()

+         parser = configparser.RawConfigParser()

          configfile = os.environ['HOME'] + "/" + configfile

          is_new = create_conf(configfile)

          parser.read(configfile)

@@ -176,7 +180,7 @@ 

          :arg key, name of the parameter to set from the settings.

          :arg value, value of the parameter to set from the settings.

          """

-         if not key in self._dict.keys():

+         if key not in self._dict:

              raise KeyError(key)

          self._dict[key] = value

  

@@ -185,7 +189,7 @@ 

  

          :arg key, name of the parameter to retrieve from the settings.

          """

-         if not key in self._dict.keys():

+         if key not in self._dict:

              raise KeyError(key)

          return self._dict[key]

  

@@ -200,7 +204,7 @@ 

          else:

              opts = set()

  

-         for name in self._dict.iterkeys():

+         for name in self._dict:

              value = None

              if name in opts:

                  value = parser.get(section, name)

@@ -232,7 +236,7 @@ 

              settings.set('email', args.email)

  

          if args.package:

-             pack = RPackage(re.sub('^R-', '', args.package))

+             pack = RPackage(re.sub(r'^R-', '', args.package))

              pack.search_package_in_repo()

          elif args.url:

              pack = RPackage(source0=args.url)

@@ -312,8 +316,8 @@ 

              self.log.info("RPM %s done" % specfile)

              # Get the list of rpm generated

              self.build.get_rpm()

-             print "RPM generated:"

-             print "\n".join(self.build.rpm)

+             print("RPM generated:")

+             print("\n".join(self.build.rpm))

              return False

          else:

              return True

@@ -327,7 +331,7 @@ 

  

          specfile = ''

          if args.package:

-             self.pack.name = re.sub('^R-', '', args.package)

+             self.pack.name = re.sub(r'^R-', '', args.package)

              self.spec.package = self.pack

              specfile = self.spec.get_specfile()

              self.pack.search_package_in_repo()

@@ -374,7 +378,7 @@ 

          self.spec.clean_files_section()

          self.spec.write_spec()

  

-         print 'Building... %s' % self.pack.name

+         print('Building... %s' % self.pack.name)

          self.build.build(specfile, mock_config=args.mock_config,

              mock_resultdir=args.mock_resultdir)

          if self.__check_build_output():

@@ -389,16 +393,16 @@ 

  

                  # Rebuild the package when build has failed the first time

                  if self.build.outcode:  # == 1 when build failed and 0 when build passed

-                     print 'Re-building... %s' % self.pack.name

+                     print('Re-building... %s' % self.pack.name)

                      self.build.build(specfile, mock_config=args.mock_config,

                          mock_resultdir=args.mock_resultdir)

                      self.__check_build_output()

-             except BuildDepencenciesError, err:

-                 print 'Missing dependencies to build %s' % self.pack.name

+             except BuildDepencenciesError as err:

+                 print('Missing dependencies to build %s' % self.pack.name)

                  self.log.info(err)

                  return 1

-             except BuildError, err:

-                 print 'An error occured during the build of %s' % self.pack.name

+             except BuildError as err:

+                 print('An error occured during the build of %s' % self.pack.name)

                  self.log.debug('Build ERROR: %s' % err)

                  return 1

          if not args.keep_logs:

file modified
+28 -21

@@ -20,17 +20,25 @@ 

  R packages class.

  """

  

+ from __future__ import absolute_import, division, print_function

  

- import ConfigParser

  import os

  import re

  import shutil

  import sys

  import tarfile

- import urllib2

  

  from tarfile import TarError

  

+ try:

+     import configparser

+ except ImportError:

+     import ConfigParser as configparser

+ try:

+     import urllib.request as urllib_request

+ except ImportError:

+     import urllib2 as urllib_request

+ 

  from r2spec import get_logger, get_rpm_tag, R2specError

  

  

@@ -44,20 +52,20 @@ 

      """

      log = get_logger()

      try:

-         stream = urllib2.urlopen(url)

-         content = stream.read()

+         stream = urllib_request.urlopen(url)

+         content = stream.read().decode()

          stream.close()

-         sourcemotif = re.compile("Package:\s+%s\n" % name)

+         sourcemotif = re.compile(r"Package:\s+%s\n" % name)

          result = sourcemotif.search(content)

          if result is not None:

              log.info("Package found in : %s" % url)

-             versionmotif = re.compile("Package:\s+%s\nVersion:(.*)" % name)

+             versionmotif = re.compile(r"Package:\s+%s\nVersion:(.*)" % name)

              version = versionmotif.search(content).group(1).strip()

              return (version)

          else:

              log.info("Not Found: %s in %s" % (name, url))

-     except IOError, ex:

-         print 'Could not contact the repository at url: %s' % url

+     except IOError as ex:

+         print('Could not contact the repository at url: %s' % url)

          log.debug('Error: %s' % ex)

      return None

  

@@ -68,7 +76,7 @@ 

      def __init__(self, name=None, url=None, source0=None):

          """ Constructor. """

          self.name = name

-         parser = ConfigParser.ConfigParser()

+         parser = configparser.RawConfigParser()

          parser.read('/etc/R2spec/repos.cfg')

          self.config = parser

          self.log = get_logger()

@@ -129,13 +137,12 @@ 

              return

  

          url = self.source0.rsplit('/', 1)[0]

-         url = '%s/%s' % (url,self.source)

+         url = '%s/%s' % (url, self.source)

          self.log.info('Downloading %s' % url)

  

-         remotefile = urllib2.urlopen(url)

-         localfile = open(sources, 'w')

-         localfile.write(remotefile.read())

-         localfile.close()

+         remotefile = urllib_request.urlopen(url)

+         with open(sources, 'wb') as localfile:

+             localfile.write(remotefile.read())

  

      def extract_sources(self):

          """ Extract the sources into the current directory. """

@@ -146,7 +153,7 @@ 

              tar = tarfile.open(tarball)

              tar.extractall()

              tar.close()

-         except TarError, err:

+         except TarError as err:

              self.log.debug("Error while extracting the tarball")

              self.log.debug("ERROR: %s" % err)

  

@@ -158,7 +165,7 @@ 

          :arg key, the key to retrieve from the DESCRIPTION file of the R

          package

          """

-         if key and key in self.description.keys():

+         if key and key in self.description:

              return self.description[key]

          else:

              return ''

@@ -174,7 +181,7 @@ 

                  stream = open(description, 'r')

                  content = stream.read()

                  stream.close()

-             except IOError, err:

+             except IOError as err:

                  self.log.info(

                  'An error occured while reading the DESCRIPTION file: %s' \

                  % description)

@@ -182,7 +189,7 @@ 

              key = None

              for row in content.split('\n'):

                  if row.strip():

-                     pattern = re.compile("\w:*")

+                     pattern = re.compile(r"\w:*")

                      if pattern.match(row):

                          key, value = row.split(':', 1)

                          self.description[key.strip()] = value.strip()

@@ -196,7 +203,7 @@ 

      def read_config(self):

          """ Read the general configuration containing the repo information

          """

-         parser = ConfigParser.ConfigParser()

+         parser = configparser.RawConfigParser()

          configfile = '/etc/R2spec/config'

          parser.read(configfile)

          self.config = parser

@@ -208,7 +215,7 @@ 

          self.log.info('Removing extracted sources: "%s"' % self.name)

          try:

              shutil.rmtree(self.name)

-         except (IOError, OSError), err:

+         except (IOError, OSError) as err:

              self.log.info('Could not remove the extracted sources: "%s"'\

                  % self.name)

              self.log.debug('ERROR: %s' % err)

@@ -239,7 +246,7 @@ 

          self.source0 = self.config.get(repo, 'source')

          if self.up_version != self.down_version:

              self.source0 = self.source0.replace('%{version}', self.up_version)

-         self.source = '%s_%s.tar.gz' % (self.name,self.up_version)

+         self.source = '%s_%s.tar.gz' % (self.name, self.up_version)

  

      def set_repo(self, reponame):

          """ This function find the URL and Source0 tag for the spec file

file modified
+7 -6

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

  Spec class, handles the read/write of the spec file

  """

  

+ from __future__ import absolute_import, division, print_function

  

  import datetime

  import os

@@ -53,7 +54,7 @@ 

      ignorelist = ['R']

      # Regular expression used to determine whether the string is a

      # version number

-     versionmotif = re.compile('\d\.\d\.?\d?')

+     versionmotif = re.compile(r'\d\.\d\.?\d?')

      char = {

              '\r': '',

              '(': ' ',

@@ -62,7 +63,7 @@ 

              '  ': ' ',

              }

  

-     for key in char.keys():

+     for key in char:

          dependencies = dependencies.replace(key, char[key])

      dep_list = []

  

@@ -228,7 +229,7 @@ 

              stream.close()

              mytemplate = Template(tplfile)

              self.spec = mytemplate.render(self.__dict)

-         except IOError, err:

+         except IOError as err:

              self.log.debug('ERROR: %s' % err)

              raise R2specError('Cannot read the file %s' % template)

  

@@ -249,7 +250,7 @@ 

                  stream = open(specfile, 'r')

                  self.spec = stream.read()

                  stream.close()

-             except IOError, err:

+             except IOError as err:

                  self.log.info('Cannot read the file %s' % specfile)

                  self.log.debug('ERROR: %s' % err)

  

@@ -264,7 +265,7 @@ 

              stream.close()

              self.log.debug('Spec file writen: %s' % specfile)

              if verbose:

-                 print 'Spec file writen: %s' % specfile

-         except IOError, err:

+                 print('Spec file writen: %s' % specfile)

+         except IOError as err:

              self.log.info('Cannot write the file %s' % specfile)

              self.log.debug('ERROR: %s' % err)

file modified
+2

@@ -3,6 +3,8 @@ 

  Setup script

  """

  

+ from __future__ import absolute_import, division, print_function

+ 

  from distutils.core import setup

  from r2spec import VERSION

  

file modified
+10 -6

@@ -19,6 +19,7 @@ 

  

  """ R2rpm tests script """

  

+ from __future__ import absolute_import, division, print_function

  

  # Cases:

  # -p <package>

@@ -41,7 +42,11 @@ 

  import shutil

  import sys

  import unittest

- import urllib2

+ 

+ try:

+     import urllib.request as urllib_request

+ except ImportError:

+     import urllib2 as urllib_request

  

  sys.path.insert(0, os.path.abspath('../'))

  from r2spec.r2spec_obj import R2rpm, setup_parser

@@ -67,10 +72,9 @@ 

      :arg url, url to the object to download

      """

      sources = url.rsplit('/', 1)[1]

-     remotefile = urllib2.urlopen(url)

-     localfile = open(sources, 'w')

-     localfile.write(remotefile.read())

-     localfile.close()

+     remotefile = urllib_request.urlopen(url)

+     with open(sources, 'wb') as localfile:

+         localfile.write(remotefile.read())

      return sources

  

  

@@ -95,7 +99,7 @@ 

          arg = parser.parse_args()

          try:

              R2rpm().main(arg)

-         except R2specError, err:

+         except R2specError as err:

              self.assertEqual('Not enough argument given, see -h/--help',

                  err.value)

  

file modified
+10 -6

@@ -19,6 +19,7 @@ 

  

  """ R2spec tests script """

  

+ from __future__ import absolute_import, division, print_function

  

  # Cases:

  # -p <package>

@@ -42,7 +43,11 @@ 

  import shutil

  import sys

  import unittest

- import urllib2

+ 

+ try:

+     import urllib.request as urllib_request

+ except ImportError:

+     import urllib2 as urllib_request

  

  sys.path.insert(0, os.path.abspath('../'))

  from r2spec.r2spec_obj import R2spec, setup_parser

@@ -68,10 +73,9 @@ 

      :arg url, url to the object to download

      """

      sources = url.rsplit('/', 1)[1]

-     remotefile = urllib2.urlopen(url)

-     localfile = open(sources, 'w')

-     localfile.write(remotefile.read())

-     localfile.close()

+     remotefile = urllib_request.urlopen(url)

+     with open(sources, 'wb') as localfile:

+         localfile.write(remotefile.read())

      return sources

  

  

@@ -96,7 +100,7 @@ 

          arg = parser.parse_args()

          try:

              R2spec().main(arg)

-         except R2specError, err:

+         except R2specError as err:

              self.assertEqual('Not enough argument given, see -h/--help',

                  err.value)

  

file modified
+2

@@ -19,6 +19,8 @@ 

  

  """ R2spec launcher script """

  

+ from __future__ import absolute_import, division, print_function

+ 

  import unittest

  from test.test_R2spec import R2spectests

  from test.test_R2rpm import R2rpmtests

Pretty straightforward. All tests that were passing before continue to pass (a couple were already broken.)

8 new commits added

  • Decode urllib results where necessary.
  • Decode output from Popen.
  • Add other future imports everywhere.
  • Import standard library bits from new locations.
  • Use print function instead of statement.
  • Add whitespace after comma.
  • Update dict calls.
  • Use modern exception handling syntax.
a year ago

1 new commit added

  • Use raw strings for regular expressions.
a year ago