Ejemplo n.º 1
0
Archivo: dak.py Proyecto: Debian/dak
def main():
    """Launch dak functionality."""

    try:
        logger = Logger('dak top-level', print_starting=False)
    except CantOpenError:
        logger = None

    functionality = init()
    modules = [command for (command, _) in functionality]

    if len(sys.argv) == 0:
        daklib.utils.fubar("err, argc == 0? how is that possible?")
    elif (len(sys.argv) == 1
          or (len(sys.argv) == 2
              and (sys.argv[1] == "--help" or sys.argv[1] == "-h"))):
        usage(functionality)

    # First see if we were invoked with/as the name of a module
    cmdname = sys.argv[0]
    cmdname = cmdname[cmdname.rfind("/") + 1:]
    if cmdname in modules:
        pass
    # Otherwise the argument is the module
    else:
        cmdname = sys.argv[1]
        sys.argv = [sys.argv[0] + " " + sys.argv[1]] + sys.argv[2:]
        if cmdname not in modules:
            match = []
            for name in modules:
                if name.startswith(cmdname):
                    match.append(name)
            if len(match) == 1:
                cmdname = match[0]
            elif len(match) > 1:
                daklib.utils.warn("ambiguous command '%s' - could be %s"
                           % (cmdname, ", ".join(match)))
                usage(functionality, 1)
            else:
                daklib.utils.warn("unknown command '%s'" % (cmdname))
                usage(functionality, 1)

    # Invoke the module
    module = __import__(cmdname.replace("-", "_"))

    try:
        module.main()
    except KeyboardInterrupt:
        msg = 'KeyboardInterrupt caught; exiting'
        print(msg)
        if logger:
            logger.log([msg])
        sys.exit(1)
    except SystemExit:
        raise
    except:
        if logger:
            for line in traceback.format_exc().split('\n')[:-1]:
                logger.log(['exception', line])
        raise
Ejemplo n.º 2
0
 def __init__(self, filename, data, log=None):
     if log is None:
         from daklib.daklog import Logger
         log = Logger()
     self.cc = []
     self.result = []
     self.log = log
     self.filename = filename
     self.data = data
Ejemplo n.º 3
0
def main():
    """Launch dak functionality."""

    try:
        logger = Logger('dak top-level', print_starting=False)
    except CantOpenError:
        logger = None

    functionality = init()
    modules = [command for (command, _) in functionality]

    if len(sys.argv) == 0:
        daklib.utils.fubar("err, argc == 0? how is that possible?")
    elif (len(sys.argv) == 1
          or (len(sys.argv) == 2 and
              (sys.argv[1] == "--help" or sys.argv[1] == "-h"))):
        usage(functionality)

    # First see if we were invoked with/as the name of a module
    cmdname = sys.argv[0]
    cmdname = cmdname[cmdname.rfind("/") + 1:]
    if cmdname in modules:
        pass
    # Otherwise the argument is the module
    else:
        cmdname = sys.argv[1]
        sys.argv = [sys.argv[0] + " " + sys.argv[1]] + sys.argv[2:]
        if cmdname not in modules:
            match = []
            for name in modules:
                if name.startswith(cmdname):
                    match.append(name)
            if len(match) == 1:
                cmdname = match[0]
            elif len(match) > 1:
                daklib.utils.warn("ambiguous command '%s' - could be %s" %
                                  (cmdname, ", ".join(match)))
                usage(functionality, 1)
            else:
                daklib.utils.warn("unknown command '%s'" % (cmdname))
                usage(functionality, 1)

    # Invoke the module
    module = __import__(cmdname.replace("-", "_"))

    try:
        module.main()
    except KeyboardInterrupt:
        msg = 'KeyboardInterrupt caught; exiting'
        print(msg)
        if logger:
            logger.log([msg])
        sys.exit(1)
    except SystemExit:
        raise
    except:
        if logger:
            for line in traceback.format_exc().split('\n')[:-1]:
                logger.log(['exception', line])
        raise
Ejemplo n.º 4
0
    def update_db(self):
        # Ok, try and find the configuration table
        print("Determining dak database revision ...")
        cnf = Config()
        logger = Logger('update-db')
        modules = []

        try:
            # Build a connect string
            if "DB::Service" in cnf:
                connect_str = "service=%s" % cnf["DB::Service"]
            else:
                connect_str = "dbname=%s" % (cnf["DB::Name"])
                if "DB::Host" in cnf and cnf["DB::Host"] != '':
                    connect_str += " host=%s" % (cnf["DB::Host"])
                if "DB::Port" in cnf and cnf["DB::Port"] != '-1':
                    connect_str += " port=%d" % (int(cnf["DB::Port"]))

            self.db = psycopg2.connect(connect_str)

            db_role = cnf.get("DB::Role")
            if db_role:
                self.db.cursor().execute('SET ROLE "{}"'.format(db_role))

        except Exception as e:
            print("FATAL: Failed connect to database (%s)" % str(e))
            sys.exit(1)

        database_revision = int(self.get_db_rev())
        logger.log(
            ['transaction id before update: %s' % self.get_transaction_id()])

        if database_revision == -1:
            print("dak database schema predates update-db.")
            print("")
            print(
                "This script will attempt to upgrade it to the lastest, but may fail."
            )
            print(
                "Please make sure you have a database backup handy. If you don't, press Ctrl-C now!"
            )
            print("")
            print("Continuing in five seconds ...")
            time.sleep(5)
            print("")
            print("Attempting to upgrade pre-zero database to zero")

            self.update_db_to_zero()
            database_revision = 0

        dbfiles = glob(
            os.path.join(os.path.dirname(__file__), 'dakdb/update*.py'))
        required_database_schema = max(
            map(int, findall('update(\d+).py', " ".join(dbfiles))))

        print("dak database schema at %d" % database_revision)
        print("dak version requires schema %d" % required_database_schema)

        if database_revision < required_database_schema:
            print("\nUpdates to be applied:")
            for i in range(database_revision, required_database_schema):
                i += 1
                dakdb = __import__("dakdb", globals(), locals(),
                                   ['update' + str(i)])
                update_module = getattr(dakdb, "update" + str(i))
                print(
                    "Update %d: %s" %
                    (i, next(s
                             for s in update_module.__doc__.split("\n") if s)))
                modules.append((update_module, i))
            if not Config().find_b("Update-DB::Options::Yes", False):
                prompt = "\nUpdate database? (y/N) "
                answer = utils.our_raw_input(prompt)
                if answer.upper() != 'Y':
                    sys.exit(0)
        else:
            print("no updates required")
            logger.log(["no updates required"])
            sys.exit(0)

        for module in modules:
            (update_module, i) = module
            try:
                update_module.do_update(self)
                message = "updated database schema from %d to %d" % (
                    database_revision, i)
                print(message)
                logger.log([message])
            except DBUpdateError as e:
                # Seems the update did not work.
                print("Was unable to update database schema from %d to %d." %
                      (database_revision, i))
                print("The error message received was %s" % (e))
                logger.log(["DB Schema upgrade failed"])
                logger.close()
                utils.fubar("DB Schema upgrade failed")
            database_revision += 1
        logger.close()
Ejemplo n.º 5
0
def main(argv=None):
    if argv is None:
        argv = sys.argv

    arguments = [('h', 'help', 'Process-Commands::Options::Help'),
                 ('d', 'directory', 'Process-Commands::Options::Directory',
                  'HasArg')]

    cnf = Config()
    cnf['Process-Commands::Options::Dummy'] = ''
    filenames = apt_pkg.parse_commandline(cnf.Cnf, arguments, argv)
    options = cnf.subtree('Process-Commands::Options')

    if 'Help' in options or (len(filenames) == 0
                             and 'Directory' not in options):
        usage()
        sys.exit(0)

    log = Logger('command')

    now = datetime.datetime.now()
    donedir = os.path.join(cnf['Dir::Done'], now.strftime('%Y/%m/%d'))
    rejectdir = cnf['Dir::Reject']

    if len(filenames) == 0:
        cdir = options['Directory']
        filenames = [
            os.path.join(cdir, fn) for fn in os.listdir(cdir)
            if fn.endswith('.dak-commands')
        ]

    for fn in filenames:
        basename = os.path.basename(fn)
        if not fn.endswith('.dak-commands'):
            log.log(['unexpected filename', basename])
            continue

        with open(fn, 'r') as fh:
            data = fh.read()

        try:
            command = CommandFile(basename, data, log)
            command.evaluate()
        except CommandError as e:
            created = os.stat(fn).st_mtime
            now = time.time()
            too_new = (now - created < int(cnf.get('Dinstall::SkipTime',
                                                   '60')))
            if too_new:
                log.log(['skipped (too new)'])
                continue
            log.log(['reject', basename, e])
        except Exception as e:
            log.log_traceback('Exception while processing %s:' % (basename), e)
            dst = find_next_free(os.path.join(rejectdir, basename))
        else:
            log.log(['done', basename])
            dst = find_next_free(os.path.join(donedir, basename))

        with FilesystemTransaction() as fs:
            fs.unlink(fn)
            fs.create(dst, mode=0o644).write(data)
            fs.commit()

    log.close()
Ejemplo n.º 6
0
def main(argv=None):
    if argv is None:
        argv = sys.argv

    arguments = [('h', 'help', 'Process-Commands::Options::Help'),
                 ('d', 'directory', 'Process-Commands::Options::Directory', 'HasArg')]

    cnf = Config()
    cnf['Process-Commands::Options::Dummy'] = ''
    filenames = apt_pkg.parse_commandline(cnf.Cnf, arguments, argv)
    options = cnf.subtree('Process-Commands::Options')

    if 'Help' in options or (len(filenames) == 0 and 'Directory' not in options):
        usage()
        sys.exit(0)

    log = Logger('command')

    now = datetime.datetime.now()
    donedir = os.path.join(cnf['Dir::Done'], now.strftime('%Y/%m/%d'))
    rejectdir = cnf['Dir::Reject']

    if len(filenames) == 0:
        filenames = [ fn for fn in os.listdir(options['Directory']) if fn.endswith('.dak-commands') ]

    for fn in filenames:
        basename = os.path.basename(fn)
        if not fn.endswith('.dak-commands'):
            log.log(['unexpected filename', basename])
            continue

        with open(fn, 'r') as fh:
            data = fh.read()

        try:
            command = CommandFile(basename, data, log)
            command.evaluate()
        except:
            created = os.stat(fn).st_mtime
            now = time.time()
            too_new = (now - created < int(cnf.get('Dinstall::SkipTime', '60')))
            if too_new:
                log.log(['skipped (too new)'])
                continue
            log.log(['reject', basename])
            dst = find_next_free(os.path.join(rejectdir, basename))
        else:
            log.log(['done', basename])
            dst = find_next_free(os.path.join(donedir, basename))

        with FilesystemTransaction() as fs:
            fs.unlink(fn)
            fs.create(dst, mode=0o644).write(data)
            fs.commit()

    log.close()
Ejemplo n.º 7
0
    def update_db(self):
        # Ok, try and find the configuration table
        print "Determining dak database revision ..."
        cnf = Config()
        logger = Logger('update-db')

        try:
            # Build a connect string
            if cnf.has_key("DB::Service"):
                connect_str = "service=%s" % cnf["DB::Service"]
            else:
                connect_str = "dbname=%s"% (cnf["DB::Name"])
                if cnf.has_key("DB::Host") and cnf["DB::Host"] != '':
                    connect_str += " host=%s" % (cnf["DB::Host"])
                if cnf.has_key("DB::Port") and cnf["DB::Port"] != '-1':
                    connect_str += " port=%d" % (int(cnf["DB::Port"]))

            self.db = psycopg2.connect(connect_str)

        except Exception as e:
            print "FATAL: Failed connect to database (%s)" % str(e)
            sys.exit(1)

        database_revision = int(self.get_db_rev())
        logger.log(['transaction id before update: %s' % self.get_transaction_id()])

        if database_revision == -1:
            print "dak database schema predates update-db."
            print ""
            print "This script will attempt to upgrade it to the lastest, but may fail."
            print "Please make sure you have a database backup handy. If you don't, press Ctrl-C now!"
            print ""
            print "Continuing in five seconds ..."
            time.sleep(5)
            print ""
            print "Attempting to upgrade pre-zero database to zero"

            self.update_db_to_zero()
            database_revision = 0

        print "dak database schema at %d" % database_revision
        print "dak version requires schema %d"  % required_database_schema

        if database_revision == required_database_schema:
            print "no updates required"
            logger.log(["no updates required"])
            sys.exit(0)

        for i in range (database_revision, required_database_schema):
            try:
                dakdb = __import__("dakdb", globals(), locals(), ['update'+str(i+1)])
                update_module = getattr(dakdb, "update"+str(i+1))
                update_module.do_update(self)
                message = "updated database schema from %d to %d" % (database_revision, i+1)
                print message
                logger.log([message])
            except DBUpdateError as e:
                # Seems the update did not work.
                print "Was unable to update database schema from %d to %d." % (database_revision, i+1)
                print "The error message received was %s" % (e)
                logger.log(["DB Schema upgrade failed"])
                logger.close()
                utils.fubar("DB Schema upgrade failed")
            database_revision += 1
        logger.close()
Ejemplo n.º 8
0
    def update_db(self):
        # Ok, try and find the configuration table
        print("Determining dak database revision ...")
        cnf = Config()
        logger = Logger('update-db')
        modules = []

        try:
            # Build a connect string
            if "DB::Service" in cnf:
                connect_str = "service=%s" % cnf["DB::Service"]
            else:
                connect_str = "dbname=%s" % (cnf["DB::Name"])
                if "DB::Host" in cnf and cnf["DB::Host"] != '':
                    connect_str += " host=%s" % (cnf["DB::Host"])
                if "DB::Port" in cnf and cnf["DB::Port"] != '-1':
                    connect_str += " port=%d" % (int(cnf["DB::Port"]))

            self.db = psycopg2.connect(connect_str)

            db_role = cnf.get("DB::Role")
            if db_role:
                self.db.cursor().execute('SET ROLE "{}"'.format(db_role))

        except Exception as e:
            print("FATAL: Failed connect to database (%s)" % str(e))
            sys.exit(1)

        database_revision = int(self.get_db_rev())
        logger.log(['transaction id before update: %s' % self.get_transaction_id()])

        if database_revision == -1:
            print("dak database schema predates update-db.")
            print("")
            print("This script will attempt to upgrade it to the lastest, but may fail.")
            print("Please make sure you have a database backup handy. If you don't, press Ctrl-C now!")
            print("")
            print("Continuing in five seconds ...")
            time.sleep(5)
            print("")
            print("Attempting to upgrade pre-zero database to zero")

            self.update_db_to_zero()
            database_revision = 0

        dbfiles = glob(os.path.join(os.path.dirname(__file__), 'dakdb/update*.py'))
        required_database_schema = max(int(x) for x in findall(r'update(\d+).py', " ".join(dbfiles)))

        print("dak database schema at %d" % database_revision)
        print("dak version requires schema %d" % required_database_schema)

        if database_revision < required_database_schema:
            print("\nUpdates to be applied:")
            for i in range(database_revision, required_database_schema):
                i += 1
                dakdb = __import__("dakdb", globals(), locals(), ['update' + str(i)])
                update_module = getattr(dakdb, "update" + str(i))
                print("Update %d: %s" % (i, next(s for s in update_module.__doc__.split("\n") if s)))
                modules.append((update_module, i))
            if not Config().find_b("Update-DB::Options::Yes", False):
                prompt = "\nUpdate database? (y/N) "
                answer = utils.our_raw_input(prompt)
                if answer.upper() != 'Y':
                    sys.exit(0)
        else:
            print("no updates required")
            logger.log(["no updates required"])
            sys.exit(0)

        for module in modules:
            (update_module, i) = module
            try:
                update_module.do_update(self)
                message = "updated database schema from %d to %d" % (database_revision, i)
                print(message)
                logger.log([message])
            except DBUpdateError as e:
                # Seems the update did not work.
                print("Was unable to update database schema from %d to %d." % (database_revision, i))
                print("The error message received was %s" % (e))
                logger.log(["DB Schema upgrade failed"])
                logger.close()
                utils.fubar("DB Schema upgrade failed")
            database_revision += 1
        logger.close()