Commit 15b28012 authored by Aurélien Dunand's avatar Aurélien Dunand Committed by Sébastien Luttringer
Browse files

Raise ISException instead of Exception



ISException is an Installsystems exception which can handle an exception and
his information (type, value, traceback).

Signed-off-by: default avatarSébastien Luttringer <sebastien.luttringer@smartjog.com>
parent 4e0b0b83
Loading
Loading
Loading
Loading
+17 −10
Original line number Diff line number Diff line
@@ -34,6 +34,7 @@ import locale
import installsystems
import installsystems.printer
import installsystems.tools as istools
from installsystems.exception import *
from installsystems.printer import *
from installsystems.repository import Repository
from installsystems.repository import RepositoryManager
@@ -87,11 +88,11 @@ def get_images(patterns, repoman, local=True, min=None, max=None):
            ans += sorted(repoman.select_images([pattern]).items())
    # check selected images cound
    if min is not None and len(ans) < min:
        raise Exception(u"%s images found. Should be at least %s" % (
        raise ISError(u"%s images found. Should be at least %s" % (
                len(ans), min))
    # check max selected images
    if max is not None and  len(ans) > max:
        raise Exception(u"Too many selected images: %s. Max is %s" % (
        raise ISError(u"Too many selected images: %s. Max is %s" % (
                ", ".join([n[0] for n in ans]), max))
    for item in ans:
        if item[1] is None:
@@ -194,7 +195,7 @@ def c_copy(args):
            out(u"  %s/%s:%s" % (repo.config.name, img.name, img.version))
        out(u"Inside repository: #l##b#%s#R#" % dstrepo.config.name)
        if not confirm():
            raise Exception("Aborted!")
            raise ISError("Aborted!")
    # copy it for real
    for srcimg, srcrepo in todo:
        arrow("Copying %s v%s from repository %s to %s" %
@@ -213,7 +214,7 @@ def c_del(args):
    # check all source repository are local (need by deletion)
    for img, repo in todo:
        if not repo.local:
            raise Exception("Repository %s is not local. Unable to delete" %
            raise ISError("Repository %s is not local. Unable to delete" %
                            repo.config.name)
    # check user really want to this
    if not args.force:
@@ -221,7 +222,7 @@ def c_del(args):
        for img, repo in todo:
            out(u"  %s/%s:%s" % (repo.config.name, img.name, img.version))
        if not confirm():
            raise Exception("Aborted!")
            raise ISError("Aborted!")
    # delete it for real
    for img, repo in todo:
        arrow("Deleting %s v%s from repository %s" %
@@ -306,7 +307,11 @@ def c_install(args):
    image.run_parser(parser=subparser)
    # call parser again, with extended attributes
    arrow("Parsing arguments")
    # Catch exception in custom argparse action
    try:
        args = args.parser.parse_args()
    except Exception as e:
        raise ISError("Parsing error", e)
    # run setup scripts
    if not args.dry_run:
        image.run_setup(namespace=args)
@@ -339,7 +344,7 @@ def c_move(args):
    # check all source repository are local (need by deletion)
    for img, repo in todo:
        if not repo.local:
            raise Exception("Repository %s is not local. Unable to move" %
            raise ISError("Repository %s is not local. Unable to move" %
                            repo.config.name)
    # check user really want to this
    if not args.force:
@@ -348,7 +353,7 @@ def c_move(args):
            out(u"  %s/%s:%s" % (repo.config.name, img.name, img.version))
        out(u"Inside repository: #l##b#%s#R#" % dstrepo.config.name)
        if not confirm():
            raise Exception("Aborted!")
            raise ISError("Aborted!")
    # move it for real
    for srcimg, srcrepo in todo:
        arrow("Moving %s v%s from repository %s to %s" %
@@ -689,7 +694,7 @@ def main():
        try:
            args = [ unicode(x, encoding=locale.getpreferredencoding()) for x in sys.argv[1:]]
        except UnicodeDecodeError as e:
            raise Exception("Invalid character encoding in command line")
            raise ISError("Invalid character encoding in command line")
        # first partial parsing, to get early debug and config path
        options = arg_parser.parse_known_args(args=args)[0]
        # set early command line verbosity and color
@@ -743,8 +748,10 @@ def main():
    except KeyboardInterrupt:
        warn("Keyboard Interrupted")
        exit(1)
    except ISException as e:
        error(u"%s.%s" % (e, e.get_tb()))
    except Exception as e:
        error(u"%s." % e)
        error(u"Unknown error, please report it: %s." % e)

# Entry point
if __name__ == '__main__':
+3 −2
Original line number Diff line number Diff line
@@ -25,6 +25,7 @@ import os
import sys
from argparse import Namespace
from ConfigParser import RawConfigParser
from installsystems.exception import *
from installsystems.printer import *
from installsystems.repository import RepositoryConfig

@@ -104,7 +105,7 @@ class MainConfigFile(ConfigFile):
            if cp.has_section(self.prefix):
                self._config.update(cp.items(self.prefix))
        except Exception as e:
            raise Exception(u"Unable load main config file %s: %s" % (self.path, e))
            raise ISError(u"Unable load main config file %s" % self.path, e)

    def parse(self, namespace=None):
        '''
@@ -216,7 +217,7 @@ class RepoConfigFile(ConfigFile):
                # get all options in repo
                self._repos.append(RepositoryConfig(rep, **dict(cp.items(rep))))
        except Exception as e:
            raise Exception(u"Unable to load repository file %s: %s" % (self.path, e))
            raise ISError(u"Unable to load repository file %s" % self.path, e)

    @property
    def repos(self):
+8 −7
Original line number Diff line number Diff line
@@ -25,6 +25,7 @@ import sqlite3
import installsystems.tools as istools
import installsystems.template as istemplate
from installsystems.tarball import Tarball
from installsystems.exception import *
from installsystems.printer import *

class Database(object):
@@ -38,10 +39,10 @@ class Database(object):
        arrow("Creating repository database")
        # check locality
        if not istools.isfile(path):
            raise Exception("Database creation must be local")
            raise ISError("Database creation must be local")
        path = os.path.abspath(path)
        if os.path.exists(path):
            raise Exception("Database already exists. Remove it before")
            raise ISError("Database already exists. Remove it before")
        try:
            conn = sqlite3.connect(path, isolation_level=None)
            conn.execute("PRAGMA foreign_keys = ON")
@@ -49,16 +50,16 @@ class Database(object):
            conn.commit()
            conn.close()
        except Exception as e:
            raise Exception(u"Create database failed: %s" % e)
            raise ISError(u"Create database failed", e)
        return cls(path)

    def __init__(self, path):
        # check locality
        if not istools.isfile(path):
            raise Exception("Database must be local")
            raise ISError("Database must be local")
        self.path = os.path.abspath(path)
        if not os.path.exists(self.path):
            raise Exception("Database not exists")
            raise ISError("Database not exists")
        self.conn = sqlite3.connect(self.path, isolation_level=None)
        self.conn.execute("PRAGMA foreign_keys = ON")
        # get database version
@@ -72,13 +73,13 @@ class Database(object):
        # we only support database v1
        if self.version >= 2.0:
            debug(u"Invalid database format: %s" % self.version)
            raise Exception("Invalid database format")
            raise ISError("Invalid database format")
        # we make a query to be sure format is valid
        try:
            self.ask("SELECT * FROM image")
        except:
            debug(u"Invalid database format: %s" % self.version)
            raise Exception("Invalid database format")
            raise ISError("Invalid database format")

    def begin(self):
        '''
+65 −0
Original line number Diff line number Diff line
# -*- python -*-
# -*- coding: utf-8 -*-

# Installsystems - Python installation framework
# Copyright © 2011-2012 Smartjog S.A
# Copyright © 2011-2012 Sébastien Luttringer
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.

'''
InstallSystems Exceptions
'''

import StringIO
import sys
import traceback
import installsystems

class ISException(Exception):
    '''
    Base exception class
    '''
    def __init__(self, message='', exception=None):
        self.message = message
        self.exception = exception
        if exception:
            self.exc_info = sys.exc_info()

    def __str__(self):
        if self.exception:
            return "%s: %s" % (self.message, self.exception)
        else:
            return self.message

    def get_tb(self):
        if self.exception and installsystems.verbosity > 1:
            sio = StringIO.StringIO()
            traceback.print_exception(self.exc_info[0], self.exc_info[1],
                                      self.exc_info[2], file=sio)
            str = "\n#R#%s" % sio.getvalue()
            sio.close()
            return str
        return ""

class ISError(ISException):
    '''
    Installsystems error; this exception will stop execution
    '''

class ISWarning(ISException):
    '''
    Installsystems warning; this exception do not stop execution
    '''
+63 −67

File changed.

Preview size limit exceeded, changes collapsed.

Loading