How to use the dbutils.Database function in DBUtils

To help you get started, we’ve selected a few DBUtils examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github jensl / critic / src / cli.py View on Github external
def init(user_id, authentication_labels):
    global db

    db = dbutils.Database.forUser()

    if user_id is None:
        user = dbutils.User.makeAnonymous()
    else:
        user = dbutils.User.fromId(db, user_id)

    db.setUser(user, authentication_labels)
github jensl / critic / installation / prefs.py View on Github external
% item)
                        print

                        remove = installation.input.yes_or_no(
                            "Would you like to remove it from the database?",
                            default=True)

                    if remove:
                        remove_preference(db, item)

        db.commit()

    import dbutils

    with installation.utils.as_critic_system_user():
        with dbutils.Database() as db:
            update_preferences(old_preferences,
                               new_preferences,
                               load_preferences(db))

    return True
github jensl / critic / quickstart.py View on Github external
for filename in filenames:
                if filename[0] != "." and filename.endswith(".py"):
                    path = os.path.join(dirpath, filename)
                    newest = max(os.stat(path).st_mtime, newest)
        return newest

    running_mtime = getNewestModificationTime()

    startTheSystem()

    if not arguments.testing:
        print "Listening at: http://%s:%s/" % (server_name, server_port)

        import dbutils

        db = dbutils.Database()
        db.cursor().execute("""UPDATE systemidentities
                                  SET hostname=?""",
                            ("%s:%s" % (server_name, server_port),))
        db.commit()
        db.close()
    else:
        print "HTTP=%s:%s" % (server_name, server_port)

    if not os.listdir(installation.paths.git_dir) and not arguments.testing:
        if not quiet:
            print
            print "Creating critic.git repository ..."

        pid_filename = os.path.join(
            state_dir, "run", "main", "branchtracker.pid")
github jensl / critic / src / background / branchtrackerhook.py View on Github external
import signal
import time

sys.path.insert(0, os.path.abspath(os.path.join(os.path.dirname(sys.argv[0]), "..")))

import dbutils
from textutils import json_encode, json_decode

if "--wait-for-update" in sys.argv:
    data = json_decode(sys.stdin.read())

    branch_id = data["branch_id"]
    timeout = data["timeout"]
    log_offset = data["log_offset"]

    db = dbutils.Database.forSystem()

    cursor = db.cursor()
    cursor.execute("SELECT MAX(time) FROM trackedbranchlog WHERE branch=%s", (branch_id,))
    last_log_entry = cursor.fetchone()[0]

    start = time.time()

    status = None
    output = ""

    while time.time() - start < timeout:
        time.sleep(0.5)

        db.commit()

        cursor = db.cursor()
github jensl / critic / src / background / highlight.py View on Github external
def __init__(self):
            service = configuration.services.HIGHLIGHT

            super(HighlightServer, self).__init__(service)

            self.db = dbutils.Database.forSystem()

            if "compact_at" in service:
                hour, minute = service["compact_at"]
                self.register_maintenance(hour=hour, minute=minute, callback=self.__compact)
github jensl / critic / src / background / branchtracker.py View on Github external
def run(self):
        self.db = dbutils.Database.forSystem()

        while not self.terminated:
            self.interrupted = False

            cursor = self.db.cursor()
            cursor.execute("""SELECT id, repository, local_name, remote, remote_name, forced
                                FROM trackedbranches
                               WHERE NOT disabled
                                 AND (next IS NULL OR next < NOW())
                            ORDER BY next ASC NULLS FIRST""")
            rows = cursor.fetchall()

            for trackedbranch_id, repository_id, local_name, remote, remote_name, forced in rows:
                if local_name == "*":
                    self.info("checking tags in %s" % remote)
                else:
github jensl / critic / src / api / impl / critic.py View on Github external
def startSession(for_user, for_system, for_testing):
    critic = api.critic.Critic(Critic())

    if for_user:
        database = dbutils.Database.forUser(critic)
    elif for_system:
        database = dbutils.Database.forSystem(critic)
    else:
        database = dbutils.Database.forTesting(critic)

    critic._impl.setDatabase(database)
    return critic
github jensl / critic / src / api / impl / critic.py View on Github external
def startSession(for_user, for_system, for_testing):
    critic = api.critic.Critic(Critic())

    if for_user:
        database = dbutils.Database.forUser(critic)
    elif for_system:
        database = dbutils.Database.forSystem(critic)
    else:
        database = dbutils.Database.forTesting(critic)

    critic._impl.setDatabase(database)
    return critic
github jensl / critic / src / background / maintenance.py View on Github external
def run(self):
        with dbutils.Database.forSystem() as db:
            # Do an initial load/update of timezones.
            #
            # The 'timezones' table initially (post-installation) only contains
            # the Universal/UTC timezone; this call adds all the others that the
            # PostgreSQL database server knows about.
            dbutils.loadTimezones(db)

        super(Maintenance, self).run()
github jensl / critic / src / background / maintenance.py View on Github external
def __maintenance(self):
        with dbutils.Database.forSystem() as db:
            cursor = db.cursor()

            # Update the UTC offsets of all timezones.
            #
            # The PostgreSQL database server has accurate (DST-adjusted) values,
            # but is very slow to query, so we cache the UTC offsets in our
            # 'timezones' table.  This call updates that cache every night.
            # (This is obviously a no-op most nights, but we don't want to have
            # to care about which nights it isn't.)
            self.debug("updating timezones")
            dbutils.updateTimezones(db)

            if self.terminated:
                return

            # Execute scheduled review branch archivals.