]> git.ipfire.org Git - people/jschlag/pbs.git/commitdiff
Refactor distributions
authorMichael Tremer <michael.tremer@ipfire.org>
Sun, 8 Oct 2017 13:39:53 +0000 (14:39 +0100)
committerMichael Tremer <michael.tremer@ipfire.org>
Sun, 8 Oct 2017 13:44:30 +0000 (14:44 +0100)
Signed-off-by: Michael Tremer <michael.tremer@ipfire.org>
src/buildservice/distribution.py
src/buildservice/repository.py
src/database.sql
src/templates/distro-detail.html
src/templates/distro-list.html
src/web/handlers_distro.py
src/web/ui_modules.py

index edd4c8184f1865404f6ab8f3aca3acb475430544..ab087ac7d4e81f444875c61021581903e810cc3d 100644 (file)
@@ -2,32 +2,41 @@
 
 import logging
 
-from . import arches
 from . import base
 from . import builds
 from . import packages
 from . import sources
-from . import updates
 
 from .repository import Repository, RepositoryAux
 
+from .decorators import *
+
 class Distributions(base.Object):
-       def get_all(self):
-               distros = self.db.query("SELECT * FROM distributions ORDER BY name")
+       def _get_distribution(self, query, *args):
+               res = self.db.get(query, *args)
+
+               if res:
+                       return Distribution(self.backend, res.id, data=res)
 
-               return [Distribution(self.pakfire, d.id, d) for d in distros]
+       def _get_distributions(self, query, *args):
+               res = self.db.query(query, *args)
 
-       def get_by_id(self, id):
-               distro = self.db.get("SELECT * FROM distributions WHERE id = %s LIMIT 1", id)
+               for row in res:
+                       yield Distribution(self.backend, row.id, data=row)
 
-               if distro:
-                       return Distribution(self.pakfire, distro.id, distro)
+       def __iter__(self):
+               distros = self._get_distributions("SELECT * FROM distributions \
+                       WHERE deleted IS FALSE ORDER BY name")
 
-       def get_by_name(self, name):
-               distro = self.db.get("SELECT * FROM distributions WHERE sname = %s LIMIT 1", name)
+               return iter(distros)
 
-               if distro:
-                       return Distribution(self.pakfire, distro.id, distro)
+       def get_by_id(self, distro_id):
+               return self._get_distribution("SELECT * FROM distributions \
+                       WHERE id = %s", distro_id)
+
+       def get_by_name(self, sname):
+               return self._get_distribution("SELECT * FROM distributions \
+                       WHERE sname = %s AND deleted IS FALSE", sname)
 
        def get_by_ident(self, ident):
                return self.get_by_name(ident)
@@ -37,32 +46,12 @@ class Distributions(base.Object):
                return self.get_by_ident("ipfire3")
 
 
-class Distribution(base.Object):
-       def __init__(self, pakfire, id, data=None):
-               base.Object.__init__(self, pakfire)
-               self.id = id
-
-               self._data = data
-               self._arches = None
-               self._sources = None
+class Distribution(base.DataObject):
+       table = "distributions"
 
        def __repr__(self):
                return "<%s %s>" % (self.__class__.__name__, self.name)
 
-       @property
-       def data(self):
-               if self._data is None:
-                       self._data = self.db.get("SELECT * FROM distributions WHERE id = %s", self.id)
-
-               return self._data
-
-       def set(self, key, value):
-               self.db.execute("UPDATE distributions SET %s = %%s WHERE id = %%s" % key,
-                       value, self.id)
-
-               if self._data:
-                       self._data[key] = value
-
        @property
        def info(self):
                return {
@@ -110,30 +99,21 @@ class Distribution(base.Object):
                return self.data.slogan
 
        def get_arches(self):
-               if self._arches is None:
-                       _arches = self.db.query("SELECT arch_id AS id FROM distro_arches \
-                               WHERE distro_id = %s", self.id)
-
-                       self._arches = []
-                       for arch in _arches:
-                               arch = arches.Arch(self.pakfire, arch.id)
-                               self._arches.append(arch)
-
-                       # Sort architectures by their priority.
-                       self._arches.sort()
+               res = self.db.query("SELECT arch FROM distributions_arches \
+                       WHERE distro_id = %s ORDER BY arch", self.id)
 
-               return self._arches
+               return sorted((row.arch for row in res))
 
-       def set_arches(self, _arches):
+       def set_arches(self, arches):
                self.db.execute("DELETE FROM distro_arches WHERE distro_id = %s", self.id)
 
-               for arch in _arches:
-                       self.db.execute("INSERT INTO distro_arches(distro_id, arch_id) \
-                               VALUES(%s, %s)", self.id, arch.id)
+               for arch in arches:
+                       self.db.execute("INSERT INTO distro_arches(distro_id, arch) \
+                               VALUES(%s, %s)", self.id, arch)
 
-               self._arches = _arches
+               self.arches = sorted(arches)
 
-       arches = property(get_arches, set_arches)
+       arches = lazy_property(get_arches, set_arches)
 
        @property
        def vendor(self):
@@ -143,11 +123,7 @@ class Distribution(base.Object):
                return self.data.contact
 
        def set_contact(self, contact):
-               self.db.execute("UPDATE distributions SET contact = %s WHERE id = %s",
-                       contact, self.id)
-
-               if self._data:
-                       self._data["contact"] = contact
+               self._set_attribute("contact", contact)
 
        contact = property(get_contact, set_contact)
 
@@ -155,11 +131,7 @@ class Distribution(base.Object):
                return self.data.tag
 
        def set_tag(self, tag):
-               self.db.execute("UPDATE distributions SET tag = %s WHERE id = %s",
-                       tag, self.id)
-
-               if self._data:
-                       self._data["tag"] = tag
+               self._set_attribute("tag", tag)
 
        tag = property(get_tag, set_tag)
 
@@ -167,20 +139,20 @@ class Distribution(base.Object):
        def description(self):
                return self.data.description or ""
 
-       @property
+       @lazy_property
        def repositories(self):
-               _repos = self.db.query("SELECT id FROM repositories WHERE distro_id = %s", self.id)
+               _repositories = self.backend.repos._get_repositories("SELECT * FROM repositories \
+                       WHERE distro_id = %s", self.id)
 
-               repos = []
-               for repo in _repos:
-                       repo = Repository(self.pakfire, repo.id)
-                       repo._distro = self
+               # Cache
+               repositories = []
+               for repo in _repositories:
+                       repo.distro = self
+                       repositories.append(repo)
 
-                       repos.append(repo)
+               return sorted(repositories)
 
-               return sorted(repos)
-
-       @property
+       @lazy_property
        def repositories_aux(self):
                _repos = self.db.query("SELECT id FROM repositories_aux \
                        WHERE status = 'enabled' AND distro_id = %s", self.id)
@@ -195,14 +167,11 @@ class Distribution(base.Object):
                return sorted(repos)
 
        def get_repo(self, name):
-               repo = self.db.get("SELECT id FROM repositories WHERE distro_id = %s AND name = %s",
-                       self.id, name)
+               repo = self.backend.repos._get_repository("SELECT * FROM repositories \
+                       WHERE distro_id = %s AND name = %s", self.id, name)
 
-               if not repo:
-                       return
-
-               repo = Repository(self.pakfire, repo.id)
-               repo._distro = self
+               # Cache
+               repo.distro = self
 
                return repo
 
@@ -225,13 +194,6 @@ class Distribution(base.Object):
                if repos:
                        return self.repositories[-1]
 
-       @property
-       def comprehensive_repositories(self):
-               return [r for r in self.repositories if r.comprehensive]
-
-       def add_repository(self, name, description):
-               return Repository.new(self.pakfire, self, name, description)
-
        @property
        def log(self):
                return [] # TODO
@@ -265,22 +227,19 @@ class Distribution(base.Object):
        def delete_package(self, name):
                pass # XXX figure out what to do at this place
 
-       @property
+       @lazy_property
        def sources(self):
-               if self._sources is None:
-                       self._sources = []
-
-                       for source in self.db.query("SELECT id FROM sources WHERE distro_id = %s", self.id):
-                               source = sources.Source(self.pakfire, source.id)
-                               self._sources.append(source)
+               _sources = []
 
-                       self._sources.sort()
+               for source in self.db.query("SELECT id FROM sources WHERE distro_id = %s", self.id):
+                       source = sources.Source(self.pakfire, source.id)
+                       _sources.append(source)
 
-               return self._sources
+               return sorted(_sources)
 
-       def get_source(self, ident):
+       def get_source(self, identifier):
                for source in self.sources:
-                       if not source.identifier == ident:
+                       if not source.identifier == identifier:
                                continue
 
                        return source
index 3ba144e3ab6d83cddb750cba8f0ee4bd197fa183..1cd1886677264fb231694ca00f0570093dbbe280 100644 (file)
@@ -5,17 +5,30 @@ import os.path
 from . import base
 from . import logs
 
+from .decorators import *
+
 class Repositories(base.Object):
-       def get_all(self):
-               repos = self.db.query("SELECT * FROM repositories")
+       def _get_repository(self, query, *args):
+               res = self.db.get(query, *args)
 
-               return [Repository(self.pakfire, r.id, r) for r in repos]
+               if res:
+                       return Repository(self.backend, res.id, data=res)
 
-       def get_by_id(self, repo_id):
-               repo = self.db.get("SELECT * FROM repositories WHERE id = %s", repo_id)
+       def _get_repositories(self, query, *args):
+               res = self.db.query(query, *args)
+
+               for row in res:
+                       yield Repository(self.backend, row.id, data=row)
+
+       def __iter__(self):
+               repositories = self._get_repositories("SELECT * FROM repositories \
+                       ORDER BY distro_id, name")
 
-               if repo:
-                       return Repository(self.pakfire, repo.id, repo)
+               return iter(repositories)
+
+       def get_by_id(self, repo_id):
+               return self._get_repository("SELECT * FROM repositories \
+                       WHERE id = %s", repo_id)
 
        def get_needs_update(self, limit=None):
                query = "SELECT id FROM repositories WHERE needs_update = 'Y'"
@@ -117,13 +130,9 @@ class Repository(base.Object):
 
                return cls(pakfire, id)
 
-       @property
+       @lazy_property
        def distro(self):
-               if self._distro is None:
-                       self._distro = self.pakfire.distros.get_by_id(self.data.distro_id)
-                       assert self._distro
-
-               return self._distro
+               return self.backend.distros.get_by_id(self.data.distro_id)
 
        @property
        def info(self):
@@ -415,19 +424,16 @@ class Repository(base.Object):
                return self.pakfire.repos.get_history(**kwargs)
 
        def get_build_times(self):
-               noarch = self.pakfire.arches.get_by_name("noarch")
-               assert noarch
-
                times = []
-               for arch in self.pakfire.arches.get_all():
-                       time = self.db.get("SELECT SUM(UNIX_TIMESTAMP(jobs.time_finished) - UNIX_TIMESTAMP(jobs.time_started)) AS time FROM jobs \
+               for arch in self.arches:
+                       time = self.db.get("SELECT SUM(jobs.time_finished - jobs.time_started) AS time FROM jobs \
                                JOIN builds ON builds.id = jobs.build_id \
                                JOIN repositories_builds ON builds.id = repositories_builds.build_id \
-                               WHERE (jobs.arch_id = %s OR jobs.arch_id = %s) AND \
+                               WHERE (jobs.arch = %s OR jobs.arch = %s) AND \
                                jobs.type = 'build' AND \
-                               repositories_builds.repo_id = %s", arch.id, noarch.id, self.id)
+                               repositories_builds.repo_id = %s", arch, "noarch", self.id)
 
-                       times.append((arch, time.time))
+                       times.append((arch, time.time.total_seconds()))
 
                return times
 
index 62c8c0b825ece120c4af0b6175da77719b8de605..f3474801d6b492c318b8f1bebe3d0c2ab513bb14 100644 (file)
@@ -1183,12 +1183,26 @@ CREATE TABLE distributions (
     description text,
     vendor text NOT NULL,
     contact text,
-    tag text NOT NULL
+    tag text NOT NULL,
+    deleted boolean DEFAULT false NOT NULL
 );
 
 
 ALTER TABLE distributions OWNER TO pakfire;
 
+--
+-- Name: distributions_arches; Type: TABLE; Schema: public; Owner: pakfire; Tablespace: 
+--
+
+CREATE TABLE distributions_arches (
+    id integer NOT NULL,
+    distro_id integer NOT NULL,
+    arch text NOT NULL
+);
+
+
+ALTER TABLE distributions_arches OWNER TO pakfire;
+
 --
 -- Name: distributions_id_seq; Type: SEQUENCE; Schema: public; Owner: pakfire
 --
@@ -1210,19 +1224,6 @@ ALTER TABLE distributions_id_seq OWNER TO pakfire;
 ALTER SEQUENCE distributions_id_seq OWNED BY distributions.id;
 
 
---
--- Name: distro_arches; Type: TABLE; Schema: public; Owner: pakfire; Tablespace: 
---
-
-CREATE TABLE distro_arches (
-    id integer NOT NULL,
-    distro_id integer NOT NULL,
-    arch_id integer NOT NULL
-);
-
-
-ALTER TABLE distro_arches OWNER TO pakfire;
-
 --
 -- Name: distro_arches_id_seq; Type: SEQUENCE; Schema: public; Owner: pakfire
 --
@@ -1241,7 +1242,7 @@ ALTER TABLE distro_arches_id_seq OWNER TO pakfire;
 -- Name: distro_arches_id_seq; Type: SEQUENCE OWNED BY; Schema: public; Owner: pakfire
 --
 
-ALTER SEQUENCE distro_arches_id_seq OWNED BY distro_arches.id;
+ALTER SEQUENCE distro_arches_id_seq OWNED BY distributions_arches.id;
 
 
 --
@@ -1804,7 +1805,8 @@ CREATE TABLE repositories (
     time_min integer DEFAULT 0 NOT NULL,
     time_max integer DEFAULT 0 NOT NULL,
     update_started timestamp without time zone,
-    update_ended timestamp without time zone
+    update_ended timestamp without time zone,
+    deleted boolean DEFAULT false NOT NULL
 );
 
 
@@ -2338,7 +2340,7 @@ ALTER TABLE ONLY distributions ALTER COLUMN id SET DEFAULT nextval('distribution
 -- Name: id; Type: DEFAULT; Schema: public; Owner: pakfire
 --
 
-ALTER TABLE ONLY distro_arches ALTER COLUMN id SET DEFAULT nextval('distro_arches_id_seq'::regclass);
+ALTER TABLE ONLY distributions_arches ALTER COLUMN id SET DEFAULT nextval('distro_arches_id_seq'::regclass);
 
 
 --
@@ -2609,7 +2611,7 @@ ALTER TABLE ONLY distributions
 -- Name: idx_2198048_primary; Type: CONSTRAINT; Schema: public; Owner: pakfire; Tablespace: 
 --
 
-ALTER TABLE ONLY distro_arches
+ALTER TABLE ONLY distributions_arches
     ADD CONSTRAINT idx_2198048_primary PRIMARY KEY (id);
 
 
@@ -2842,6 +2844,20 @@ CREATE UNIQUE INDEX builders_name ON builders USING btree (name) WHERE (deleted
 CREATE INDEX builds_watchers_build_id ON builds_watchers USING btree (build_id);
 
 
+--
+-- Name: distributions_arches_distro_id; Type: INDEX; Schema: public; Owner: pakfire; Tablespace: 
+--
+
+CREATE INDEX distributions_arches_distro_id ON distributions_arches USING btree (distro_id);
+
+
+--
+-- Name: distributions_sname; Type: INDEX; Schema: public; Owner: pakfire; Tablespace: 
+--
+
+CREATE UNIQUE INDEX distributions_sname ON distributions USING btree (sname) WHERE (deleted IS FALSE);
+
+
 --
 -- Name: filelists_name; Type: INDEX; Schema: public; Owner: pakfire; Tablespace: 
 --
@@ -3237,18 +3253,18 @@ ALTER TABLE ONLY builds_watchers
 
 
 --
--- Name: distro_arches_arch_id; Type: FK CONSTRAINT; Schema: public; Owner: pakfire
+-- Name: distributions_arches_arch; Type: FK CONSTRAINT; Schema: public; Owner: pakfire
 --
 
-ALTER TABLE ONLY distro_arches
-    ADD CONSTRAINT distro_arches_arch_id FOREIGN KEY (arch_id) REFERENCES arches(id);
+ALTER TABLE ONLY distributions_arches
+    ADD CONSTRAINT distributions_arches_arch FOREIGN KEY (arch) REFERENCES arches(name);
 
 
 --
 -- Name: distro_arches_distro_id; Type: FK CONSTRAINT; Schema: public; Owner: pakfire
 --
 
-ALTER TABLE ONLY distro_arches
+ALTER TABLE ONLY distributions_arches
     ADD CONSTRAINT distro_arches_distro_id FOREIGN KEY (distro_id) REFERENCES distributions(id);
 
 
index 18f5a3f09b8a2afd734dd3ec5438adc5031c9182..1122c4c9c623835d4cf79958d4c1a7748daf29b8 100644 (file)
@@ -33,7 +33,7 @@
 
                        <p>
                                {{ _("Supported architectures") }}:
-                               {{ locale.list([a.name for a in distro.arches]) or _("None") }}
+                               {{ locale.list(distro.arches) or _("None") }}
                        </p>
 
                        <hr />
index a0405c1f9528de7295fdccf2dbd941af6f290e9e..1714785d5b279e78a0cdb10ee1f04fc7101900a6 100644 (file)
@@ -16,7 +16,6 @@
        <div class="page-header">
                <h1>
                        {{ _("Distributions") }}
-                       <small>({{ len(distros) }})</small>
                </h1>
        </div>
 
index 57a35b85b8742a99b9f04c26146442da3c968b27..1d1314cd50f201627abcbe5488e59aebb301c108 100644 (file)
@@ -4,9 +4,7 @@ from .handlers_base import *
 
 class DistributionListHandler(BaseHandler):
        def get(self):
-               distros = self.pakfire.distros.get_all()
-
-               self.render("distro-list.html", distros=distros)
+               self.render("distro-list.html", distros=self.backend.distros)
 
 
 class DistributionDetailHandler(BaseHandler):
index 9e9471e0aea193c09b3e7b95bd24179903801ac6..0a7b8ec237e3a8dd0ed930d6e3fdea1473a80e7f 100644 (file)
@@ -353,7 +353,7 @@ class JobStateModule(UIModule):
                        classes.append(cls)
 
                if show_arch:
-                       text = job.arch.name
+                       text = job.arch
 
                if show_icon and icon:
                        text = """<i class="%s"></i> %s""" % (icon, text)