Commit cf95df81 authored by Léo-Paul Géneau's avatar Léo-Paul Géneau 👾

style: import UserError and use Recipe().install()

- import UserError from zc.buildout for shorter usage
- use Recipe(...).install() one-liner when recipe varible is uncessary
parent cb8165cb
Pipeline #17631 passed with stage
in 0 seconds
...@@ -8,10 +8,10 @@ import os ...@@ -8,10 +8,10 @@ import os
import re import re
import stat import stat
import subprocess import subprocess
import zc.buildout
import six.moves.urllib as urllib import six.moves.urllib as urllib
from zc.buildout import UserError
from zc.buildout.easy_install import allow_picked_versions from zc.buildout.easy_install import allow_picked_versions
from slapos.recipe import rmtree from slapos.recipe import rmtree
from slapos.recipe.downloadunpacked import Recipe as Download from slapos.recipe.downloadunpacked import Recipe as Download
...@@ -42,13 +42,12 @@ class Recipe(object): ...@@ -42,13 +42,12 @@ class Recipe(object):
if self.gems: if self.gems:
self.gems = self.gems.split() self.gems = self.gems.split()
else: else:
raise zc.buildout.UserError( raise UserError("Configuration error, 'gems' option is missing")
"Configuration error, 'gems' option is missing")
self.url = options.get('url') self.url = options.get('url')
if(self.url and options.get('version')): if(self.url and options.get('version')):
raise zc.buildout.UserError( raise UserError("Configuration error, "
"Configuration error, 'url' and 'version' options are mutually exclusive") "'url' and 'version' options are mutually exclusive")
# Allow to define specific ruby executable. If not, take just 'ruby' # Allow to define specific ruby executable. If not, take just 'ruby'
self.ruby_executable = options.get('ruby-executable', 'ruby') self.ruby_executable = options.get('ruby-executable', 'ruby')
...@@ -67,17 +66,14 @@ class Recipe(object): ...@@ -67,17 +66,14 @@ class Recipe(object):
try: try:
return subprocess.check_output(cmd, env=env, universal_newlines=True) return subprocess.check_output(cmd, env=env, universal_newlines=True)
except OSError as e: except OSError as e:
raise zc.buildout.UserError( raise UserError('System error, command failed: %s: %s' % (e, cmd))
'System error, command failed: %s: %s' % (e, cmd))
except subprocess.CalledProcessError as e: except subprocess.CalledProcessError as e:
self.log.error(e.output) self.log.error(e.output)
if e.returncode < 0: if e.returncode < 0:
raise zc.buildout.UserError( raise UserError('System error, command received signal %s: %s'
'System error, command received signal %s: %s' % (-e.returncode, e.cmd))
% (-e.returncode, e.cmd)
)
elif e.returncode > 0: elif e.returncode > 0:
raise zc.buildout.UserError( raise UserError(
'System error, command failed with exit code %s: %s' 'System error, command failed with exit code %s: %s'
% (e.returncode, e.cmd) % (e.returncode, e.cmd)
) )
...@@ -124,11 +120,11 @@ class Recipe(object): ...@@ -124,11 +120,11 @@ class Recipe(object):
% (' '.join(cons_dict.values()), dep_dict['gemname'], gemname) % (' '.join(cons_dict.values()), dep_dict['gemname'], gemname)
) )
raise zc.buildout.UserError( raise UserError(
'Configuration error, version %s for gem %s ' 'Configuration error, version %s for gem %s '
'does not satisfy dependency constraint %s of gem %s' 'does not satisfy dependency constraint %s of gem %s'
% (dep_dict['version'], dep_dict['gemname'], % (dep_dict['version'], dep_dict['gemname'],
' '.join(cons_dict.values()), gemname) ' '.join(cons_dict.values()), gemname)
) )
def _join_paths(self, *paths): def _join_paths(self, *paths):
...@@ -141,10 +137,8 @@ class Recipe(object): ...@@ -141,10 +137,8 @@ class Recipe(object):
map(strip, line.split('=', 1)) for line in env map(strip, line.split('=', 1)) for line in env
]]) ]])
except ValueError: # Unpacking impossible except ValueError: # Unpacking impossible
raise zc.buildout.UserError( raise UserError('Configuration error, '
'Configuration error, ' 'every environment line should contain a "=" sign')
'every environment line should contain a "=" sign'
)
return env return env
def _get_env(self): def _get_env(self):
...@@ -195,7 +189,7 @@ class Recipe(object): ...@@ -195,7 +189,7 @@ class Recipe(object):
f.close() f.close()
if r: if r:
return url or r.group(0), r.group(1) return url or r.group(0), r.group(1)
raise zc.buildout.UserError("Can't find rubygems version.") raise UserError("Can't find rubygems version.")
def _install_rubygems(self, url, version): def _install_rubygems(self, url, version):
srcdir = os.path.join(self.buildout['buildout']['parts-directory'], srcdir = os.path.join(self.buildout['buildout']['parts-directory'],
...@@ -204,8 +198,7 @@ class Recipe(object): ...@@ -204,8 +198,7 @@ class Recipe(object):
'url': url, 'url': url,
'destination': srcdir, 'destination': srcdir,
} }
recipe = Download(self.buildout, self.name, options) Download(self.buildout, self.name, options).install()
recipe.install()
current_dir = os.getcwd() current_dir = os.getcwd()
os.chdir(srcdir) os.chdir(srcdir)
...@@ -313,7 +306,7 @@ class Recipe(object): ...@@ -313,7 +306,7 @@ class Recipe(object):
url, version = self._get_rubygems_url_and_version() url, version = self._get_rubygems_url_and_version()
if int(version.split(".")[0]) < 2: if int(version.split(".")[0]) < 2:
raise zc.buildout.UserError("Rubygems version must be >= 2.0.0") raise UserError("Rubygems version must be >= 2.0.0")
self._install_rubygems(url, version) self._install_rubygems(url, version)
gem_executable = glob.glob(os.path.join(bindir, 'gem*'))[0] gem_executable = glob.glob(os.path.join(bindir, 'gem*'))[0]
...@@ -322,7 +315,7 @@ class Recipe(object): ...@@ -322,7 +315,7 @@ class Recipe(object):
for gem_dict in gem_dict_list: for gem_dict in gem_dict_list:
if self.deployment: if self.deployment:
if 'version' not in gem_dict: if 'version' not in gem_dict:
raise zc.buildout.UserError( raise UserError(
'Configuration error, ' 'Configuration error, '
'version for gem %s is missing' % gem_dict['gemname'] 'version for gem %s is missing' % gem_dict['gemname']
) )
...@@ -333,7 +326,7 @@ class Recipe(object): ...@@ -333,7 +326,7 @@ class Recipe(object):
match = [gem_d for gem_d in gem_dict_list match = [gem_d for gem_d in gem_dict_list
if dep_dict['gemname'] == gem_d['gemname']] if dep_dict['gemname'] == gem_d['gemname']]
if not match: if not match:
raise zc.buildout.UserError( raise UserError(
'Configuration error, ' 'Configuration error, '
'version for dependency %s is missing' 'version for dependency %s is missing'
% dep_dict['gemname'] % dep_dict['gemname']
......
This diff is collapsed.
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment