예제 #1
0
 def __init__(self, event_handler=None, popen=None, fs_util=None):
     if event_handler is None:
         event_handler = self._dummy
     self.event_handler = event_handler
     if popen is None:
         popen = RosePopener(event_handler)
     self.popen = popen
     if fs_util is None:
         fs_util = FileSystemUtil(event_handler)
     self.fs_util = fs_util
     self.post_commit_hook = RosieSvnPostCommitHook(
         event_handler=event_handler, popen=popen)
예제 #2
0
파일: db_create.py 프로젝트: kaday/rose
 def __init__(self, event_handler=None, popen=None, fs_util=None):
     if event_handler is None:
         event_handler = self._dummy
     self.event_handler = event_handler
     if popen is None:
         popen = RosePopener(event_handler)
     self.popen = popen
     if fs_util is None:
         fs_util = FileSystemUtil(event_handler)
     self.fs_util = fs_util
     self.post_commit_hook = RosieSvnPostCommitHook(event_handler=event_handler, popen=popen)
예제 #3
0
class RosieDatabaseInitiator(object):

    """Initiate a database file from the repository information."""

    LEN_DB_STRING = 1024
    LEN_STATUS = 2
    SQLITE_PREFIX = "sqlite:///"

    def __init__(self, event_handler=None, popen=None, fs_util=None):
        if event_handler is None:
            event_handler = self._dummy
        self.event_handler = event_handler
        if popen is None:
            popen = RosePopener(event_handler)
        self.popen = popen
        if fs_util is None:
            fs_util = FileSystemUtil(event_handler)
        self.fs_util = fs_util
        self.post_commit_hook = RosieSvnPostCommitHook(
            event_handler=event_handler,
            popen=popen)

    def _dummy(self, *args, **kwargs):
        """Does nothing."""
        pass

    def create_and_load(self, db_url, repos_path):
        """Create web service database and load content from repository."""
        try:
            self.create(db_url)
        except al.exc.OperationalError:
            pass
        else:
            self.load(repos_path)

    __call__ = create_and_load

    def handle_event(self, *args, **kwargs):
        """Handle an event using the runner's event handler."""
        if callable(self.event_handler):
            return self.event_handler(*args, **kwargs)

    def create(self, db_url):
        """Create database tables."""
        if db_url.startswith(self.SQLITE_PREFIX):
            db_url_dir = os.path.dirname(db_url[len(self.SQLITE_PREFIX):])
            self.fs_util.makedirs(db_url_dir)
        try:
            engine = al.create_engine(db_url)
            metadata = al.MetaData()
            db_string = al.String(self.LEN_DB_STRING)
            tables = []
            tables.append(al.Table(
                LATEST_TABLE_NAME, metadata,
                al.Column("idx", db_string, nullable=False,
                          primary_key=True),
                al.Column("branch", db_string, nullable=False,
                          primary_key=True),
                al.Column("revision", al.Integer, nullable=False,
                          primary_key=True)))
            tables.append(al.Table(
                MAIN_TABLE_NAME, metadata,
                al.Column("idx", db_string, nullable=False,
                          primary_key=True),
                al.Column("branch", db_string, nullable=False,
                          primary_key=True),
                al.Column("revision", al.Integer, nullable=False,
                          primary_key=True),
                al.Column("owner", db_string, nullable=False),
                al.Column("project", db_string, nullable=False),
                al.Column("title", db_string, nullable=False),
                al.Column("author", db_string, nullable=False),
                al.Column("date", al.Integer, nullable=False),
                al.Column("status", al.String(self.LEN_STATUS),
                          nullable=False),
                al.Column("from_idx", db_string)))
            tables.append(al.Table(
                OPTIONAL_TABLE_NAME, metadata,
                al.Column("idx", db_string, nullable=False,
                          primary_key=True),
                al.Column("branch", db_string, nullable=False,
                          primary_key=True),
                al.Column("revision", al.Integer, nullable=False,
                          primary_key=True),
                al.Column("name", db_string, nullable=False,
                          primary_key=True),
                al.Column("value", db_string)))
            tables.append(al.Table(
                META_TABLE_NAME, metadata,
                al.Column("name", db_string, primary_key=True,
                          nullable=False),
                al.Column("value", db_string)))
            for table in tables:
                table.create(engine)
            engine.connect()
            self.handle_event(RosieDatabaseCreateEvent(db_url))
        except al.exc.OperationalError as exc:
            self.handle_event(RosieDatabaseCreateSkipEvent(db_url))
            raise exc

    def load(self, repos_path):
        """Load database contents from a repository."""
        if not repos_path or not os.path.exists(repos_path):
            self.handle_event(RosieDatabaseLoadSkipEvent(repos_path))
            return
        repos_path = os.path.abspath(repos_path)
        youngest = int(self.popen("svnlook", "youngest", repos_path)[0])
        revision = 1
        while revision <= youngest:
            if sys.stdout.isatty():
                sys.stdout.write(
                    "\r%s... loading revision %d of %d" %
                    (Reporter.PREFIX_INFO, revision, youngest))
                sys.stdout.flush()
            self.post_commit_hook.run(
                repos_path, str(revision), no_notification=True)
            event = RosieDatabaseLoadEvent(repos_path, revision, youngest)
            if revision == youngest:
                # Check if any new revisions have been added.
                youngest = self.popen("svnlook", "youngest", repos_path)[0]
                youngest = int(youngest)
            if revision == youngest:
                event.level = event.DEFAULT
            if sys.stdout.isatty():
                sys.stdout.write("\r")
            self.handle_event(event)
            revision += 1
        return revision
예제 #4
0
class RosieDatabaseInitiator(object):
    """Initiate a database file from the repository information."""

    LEN_DB_STRING = 1024
    LEN_STATUS = 2
    SQLITE_PREFIX = "sqlite:///"

    def __init__(self, event_handler=None, popen=None, fs_util=None):
        if event_handler is None:
            event_handler = self._dummy
        self.event_handler = event_handler
        if popen is None:
            popen = RosePopener(event_handler)
        self.popen = popen
        if fs_util is None:
            fs_util = FileSystemUtil(event_handler)
        self.fs_util = fs_util
        self.post_commit_hook = RosieSvnPostCommitHook(
            event_handler=event_handler, popen=popen)

    def _dummy(self, *args, **kwargs):
        """Does nothing."""
        pass

    def create_and_load(self, db_url, repos_path):
        """Create web service database and load content from repository."""
        try:
            self.create(db_url)
        except al.exc.OperationalError:
            pass
        else:
            self.load(repos_path)

    __call__ = create_and_load

    def handle_event(self, *args, **kwargs):
        """Handle an event using the runner's event handler."""
        if callable(self.event_handler):
            return self.event_handler(*args, **kwargs)

    def create(self, db_url):
        """Create database tables."""
        if db_url.startswith(self.SQLITE_PREFIX):
            db_url_dir = os.path.dirname(db_url[len(self.SQLITE_PREFIX):])
            self.fs_util.makedirs(db_url_dir)
        try:
            engine = al.create_engine(db_url)
            metadata = al.MetaData()
            db_string = al.String(self.LEN_DB_STRING)
            tables = []
            tables.append(
                al.Table(
                    LATEST_TABLE_NAME, metadata,
                    al.Column("idx",
                              db_string,
                              nullable=False,
                              primary_key=True),
                    al.Column("branch",
                              db_string,
                              nullable=False,
                              primary_key=True),
                    al.Column("revision",
                              al.Integer,
                              nullable=False,
                              primary_key=True)))
            tables.append(
                al.Table(
                    MAIN_TABLE_NAME, metadata,
                    al.Column("idx",
                              db_string,
                              nullable=False,
                              primary_key=True),
                    al.Column("branch",
                              db_string,
                              nullable=False,
                              primary_key=True),
                    al.Column("revision",
                              al.Integer,
                              nullable=False,
                              primary_key=True),
                    al.Column("owner", db_string, nullable=False),
                    al.Column("project", db_string, nullable=False),
                    al.Column("title", db_string, nullable=False),
                    al.Column("author", db_string, nullable=False),
                    al.Column("date", al.Integer, nullable=False),
                    al.Column("status",
                              al.String(self.LEN_STATUS),
                              nullable=False),
                    al.Column("from_idx", db_string)))
            tables.append(
                al.Table(
                    OPTIONAL_TABLE_NAME, metadata,
                    al.Column("idx",
                              db_string,
                              nullable=False,
                              primary_key=True),
                    al.Column("branch",
                              db_string,
                              nullable=False,
                              primary_key=True),
                    al.Column("revision",
                              al.Integer,
                              nullable=False,
                              primary_key=True),
                    al.Column("name",
                              db_string,
                              nullable=False,
                              primary_key=True), al.Column("value",
                                                           db_string)))
            tables.append(
                al.Table(
                    META_TABLE_NAME, metadata,
                    al.Column("name",
                              db_string,
                              primary_key=True,
                              nullable=False), al.Column("value", db_string)))
            for table in tables:
                table.create(engine)
            engine.connect()
            self.handle_event(RosieDatabaseCreateEvent(db_url))
        except al.exc.OperationalError as exc:
            self.handle_event(RosieDatabaseCreateSkipEvent(db_url))
            raise exc

    def load(self, repos_path):
        """Load database contents from a repository."""
        if not repos_path or not os.path.exists(repos_path):
            self.handle_event(RosieDatabaseLoadSkipEvent(repos_path))
            return
        repos_path = os.path.abspath(repos_path)
        youngest = int(self.popen("svnlook", "youngest", repos_path)[0])
        revision = 1
        while revision <= youngest:
            if sys.stdout.isatty():
                sys.stdout.write("\r%s... loading revision %d of %d" %
                                 (Reporter.PREFIX_INFO, revision, youngest))
                sys.stdout.flush()
            self.post_commit_hook.run(repos_path,
                                      str(revision),
                                      no_notification=True)
            event = RosieDatabaseLoadEvent(repos_path, revision, youngest)
            if revision == youngest:
                # Check if any new revisions have been added.
                youngest = self.popen("svnlook", "youngest", repos_path)[0]
                youngest = int(youngest)
            if revision == youngest:
                event.level = event.DEFAULT
            if sys.stdout.isatty():
                sys.stdout.write("\r")
            self.handle_event(event)
            revision += 1
        return revision