| |
@@ -20,16 +20,21 @@
|
| |
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,21 +49,21 @@
|
| |
"""
|
| |
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)
|
| |
+ log.info("Package found in : %s", url)
|
| |
+ 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
|
| |
- log.debug('Error: %s' % ex)
|
| |
+ log.info("Not Found: %s in %s", name, url)
|
| |
+ except IOError as ex:
|
| |
+ print('Could not contact the repository at url: %s' % url)
|
| |
+ log.debug('Error: %s', ex)
|
| |
return None
|
| |
|
| |
|
| |
@@ -68,7 +73,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()
|
| |
@@ -84,11 +89,19 @@
|
| |
def determine_arch(self):
|
| |
""" Determine if the package is arch or noarch by looking at the
|
| |
sources.
|
| |
- Set arch to True if the package is arch dependant.
|
| |
+ Set arch to True if the package is arch dependent.
|
| |
Set arch to False if the package is noarch.
|
| |
Let arch to None if could not determine.
|
| |
"""
|
| |
- self.log.info('Determining if the package is arch dependant or not')
|
| |
+ self.log.info('Determining if the package is arch dependent or not')
|
| |
+ try:
|
| |
+ self.arch = self.description['NeedsCompilation'].lower() != 'no'
|
| |
+ except KeyError:
|
| |
+ pass
|
| |
+ else:
|
| |
+ self.log.info('Package is %sarch dependent',
|
| |
+ '' if self.arch else 'not ')
|
| |
+ return
|
| |
extensions = ['c', 'C', 'cp', 'cpp', 'h', 'H',]
|
| |
if os.path.exists(self.name):
|
| |
for root, dirs, files in os.walk(self.name):
|
| |
@@ -99,10 +112,10 @@
|
| |
or 'f' in extension \
|
| |
or 'F' in extension:
|
| |
self.arch = True
|
| |
- self.log.info('Package is arch dependant')
|
| |
+ self.log.info('Package is arch dependent')
|
| |
return
|
| |
self.arch = False
|
| |
- self.log.info('Package is not arch dependant')
|
| |
+ self.log.info('Package is not arch dependent')
|
| |
return
|
| |
else:
|
| |
self.log.info(
|
| |
@@ -129,26 +142,35 @@
|
| |
return
|
| |
|
| |
url = self.source0.rsplit('/', 1)[0]
|
| |
- url = '%s/%s' % (url,self.source)
|
| |
- self.log.info('Downloading %s' % url)
|
| |
+ 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. """
|
| |
+ def open_sources(self):
|
| |
+ """ Open the source tarball. """
|
| |
sourcedir = get_rpm_tag('_sourcedir')
|
| |
tarball = "%s/%s" % (sourcedir, self.source)
|
| |
- self.log.info("Opening: %s" % tarball)
|
| |
+ self.log.info("Opening: %s", tarball)
|
| |
try:
|
| |
- tar = tarfile.open(tarball)
|
| |
- tar.extractall()
|
| |
- tar.close()
|
| |
- except TarError, err:
|
| |
+ return tarfile.open(tarball)
|
| |
+ except tarfile.TarError as err:
|
| |
self.log.debug("Error while extracting the tarball")
|
| |
- self.log.debug("ERROR: %s" % err)
|
| |
+ self.log.debug("ERROR: %s", err)
|
| |
+
|
| |
+ def extract_sources(self):
|
| |
+ """ Extract the sources into the current directory. """
|
| |
+ tar = self.open_sources()
|
| |
+ if tar is None:
|
| |
+ return
|
| |
+ try:
|
| |
+ with tar:
|
| |
+ tar.extractall()
|
| |
+ except tarfile.TarError as err:
|
| |
+ self.log.debug("Error while extracting the tarball")
|
| |
+ self.log.debug("ERROR: %s", err)
|
| |
|
| |
def get(self, key):
|
| |
""" Retrieve the given key from the description information known
|
| |
@@ -158,7 +180,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 ''
|
| |
@@ -168,51 +190,58 @@
|
| |
from in them.
|
| |
"""
|
| |
description = '%s/DESCRIPTION' % self.name
|
| |
- self.log.info('Loading "%s"' % description)
|
| |
+ content = None
|
| |
if os.path.exists(self.name) and os.path.isfile(description):
|
| |
+ self.log.info('Loading "%s" from extracted sources', description)
|
| |
try:
|
| |
- stream = open(description, 'r')
|
| |
- content = stream.read()
|
| |
- stream.close()
|
| |
- except IOError, err:
|
| |
+ with open(description, 'rb') as stream:
|
| |
+ content = stream.read()
|
| |
+ except IOError as err:
|
| |
self.log.info(
|
| |
- 'An error occured while reading the DESCRIPTION file: %s' \
|
| |
- % description)
|
| |
- self.log.debug('ERROR: %s' % err)
|
| |
- key = None
|
| |
- for row in content.split('\n'):
|
| |
- if row.strip():
|
| |
- pattern = re.compile("\w:*")
|
| |
- if pattern.match(row):
|
| |
- key, value = row.split(':', 1)
|
| |
- self.description[key.strip()] = value.strip()
|
| |
- else:
|
| |
- self.description[key] = self.description[key] + ' ' + \
|
| |
- row.strip()
|
| |
+ 'An error occurred while reading the DESCRIPTION file: %s',
|
| |
+ description)
|
| |
+ self.log.debug('ERROR: %s', err)
|
| |
+ else:
|
| |
+ self.log.info('Loading "%s" from tarball', description)
|
| |
+ tar = self.open_sources()
|
| |
+ if tar is not None:
|
| |
+ try:
|
| |
+ with tar:
|
| |
+ with tar.extractfile(description) as stream:
|
| |
+ content = stream.read()
|
| |
+ except tarfile.TarError as err:
|
| |
+ self.log.debug("Error while extracting the DESCRIPTION "
|
| |
+ "file from the tarball")
|
| |
+ self.log.debug("ERROR: %s", err)
|
| |
+ else:
|
| |
+ self.log.info('Could not find a DESCRIPTION file "%s" to read',
|
| |
+ description)
|
| |
+ return
|
| |
+
|
| |
+ encoding = re.search(b'^Encoding: (.+)$', content, re.MULTILINE)
|
| |
+ if encoding is not None:
|
| |
+ content = content.decode(encoding.group(1).decode().strip())
|
| |
else:
|
| |
- self.log.info('Could not find a DESCRIPTION file "%s" to read' \
|
| |
- % description)
|
| |
+ content = content.decode('ascii')
|
| |
+ key = None
|
| |
+ for row in content.split('\n'):
|
| |
+ if row.strip():
|
| |
+ pattern = re.compile(r"\w:*")
|
| |
+ if pattern.match(row):
|
| |
+ key, value = row.split(':', 1)
|
| |
+ self.description[key.strip()] = value.strip()
|
| |
+ else:
|
| |
+ self.description[key] = self.description[key] + ' ' + \
|
| |
+ row.strip()
|
| |
|
| |
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
|
| |
|
| |
- def remove_sources(self):
|
| |
- """ Remove the source we extracted in the current working
|
| |
- directory.
|
| |
- """
|
| |
- self.log.info('Removing extracted sources: "%s"' % self.name)
|
| |
- try:
|
| |
- shutil.rmtree(self.name)
|
| |
- except (IOError, OSError), err:
|
| |
- self.log.info('Could not remove the extracted sources: "%s"'\
|
| |
- % self.name)
|
| |
- self.log.debug('ERROR: %s' % err)
|
| |
-
|
| |
def search_package_in_repo(self):
|
| |
""" Search a package in all R repositories listed in the general
|
| |
configuration file.
|
| |
@@ -228,8 +257,8 @@
|
| |
break
|
| |
if version is None:
|
| |
self.log.info(
|
| |
- 'Could not find package "%s" in any of the congifure repos' \
|
| |
- % self.name)
|
| |
+ 'Could not find package "%s" in any of the configured repos',
|
| |
+ self.name)
|
| |
raise R2specError(
|
| |
'Could not find package "%s" in any of the configured repos' \
|
| |
% self.name)
|
| |
@@ -237,9 +266,7 @@
|
| |
self.down_version = version.replace('-', '.')
|
| |
self.url = self.config.get(repo, 'url')
|
| |
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
|
| |
@@ -249,7 +276,6 @@
|
| |
"""
|
| |
for section in self.config.sections():
|
| |
if section == 'repo:%s' % reponame:
|
| |
- """ Not sure why this format is getting filled out..."""
|
| |
- self.url = self.config.get(section, 'url' % (self.name))
|
| |
+ self.url = self.config.get(section, 'url')
|
| |
self.source0 = self.config.get(section, 'source')
|
| |
break
|
| |
( and use these options instead of changes in template)