]>
git.ipfire.org Git - people/jschlag/pbs.git/blob - src/buildservice/sources.py
15 from .constants
import *
16 from .decorators
import *
18 class Sources(base
.Object
):
19 def _get_source(self
, query
, *args
):
20 res
= self
.db
.get(query
, *args
)
23 return Source(self
.backend
, res
.id, data
=res
)
25 def _get_sources(self
, query
, *args
):
26 res
= self
.db
.query(query
, *args
)
29 yield Source(self
.backend
, row
.id, data
=row
)
31 def _get_commit(self
, query
, *args
):
32 res
= self
.db
.get(query
, *args
)
35 return Commit(self
.backend
, res
.id, data
=res
)
37 def _get_commits(self
, query
, *args
):
38 res
= self
.db
.query(query
, *args
)
41 yield Commit(self
.backend
, row
.id, data
=row
)
44 return self
._get
_sources
("SELECT * FROM sources")
46 def get_by_id(self
, id):
47 return self
._get
_source
("SELECT * FROM sources \
50 def get_by_distro(self
, distro
):
51 return self
._get
_sources
("SELECT * FROM sources \
52 WHERE distro_id = %s", distro
.id)
54 def update_revision(self
, source_id
, revision
):
55 query
= "UPDATE sources SET revision = %s WHERE id = %s"
57 return self
.db
.execute(query
, revision
, source_id
)
59 def get_commit_by_id(self
, commit_id
):
60 commit
= self
.db
.get("SELECT id FROM sources_commits WHERE id = %s", commit_id
)
63 return Commit(self
.pakfire
, commit
.id)
67 with git
.Repo(self
.backend
, source
, mode
="mirror") as repo
:
68 # Fetch the latest updates
71 # Import all new revisions
72 repo
.import_revisions()
75 # Walk through all source repositories
77 # Get access to the git repo
78 with git
.Repo(self
.pakfire
, source
) as repo
:
79 # Walk through all pending commits
80 for commit
in source
.pending_commits
:
81 commit
.state
= "running"
83 logging
.debug("Processing commit %s: %s" % (commit
.revision
, commit
.subject
))
85 # Navigate to the right revision.
86 repo
.checkout(commit
.revision
)
88 # Get all changed makefiles.
92 for file in repo
.changed_files(commit
.revision
):
93 # Don't care about files that are not a makefile.
94 if not file.endswith(".%s" % MAKEFILE_EXTENSION
):
97 if os
.path
.exists(file):
98 updated_files
.append(file)
100 deleted_files
.append(file)
103 # Create a temporary directory where to put all the files
104 # that are generated here.
105 pkg_dir
= tempfile
.mkdtemp()
108 config
= pakfire
.config
.Config(["general.conf",])
109 config
.parse(source
.distro
.get_config())
111 p
= pakfire
.PakfireServer(config
=config
)
114 for file in updated_files
:
116 pkg_file
= p
.dist(file, pkg_dir
)
117 pkgs
.append(pkg_file
)
121 # Import all packages in one swoop.
123 with self
.db
.transaction():
124 self
.backend
.builds
.create_from_source_package(pkg
,
125 source
.distro
, commit
=commit
, type="release")
129 commit
.state
= "failed"
134 if os
.path
.exists(pkg_dir
):
135 shutil
.rmtree(pkg_dir
)
137 for file in deleted_files
:
138 # Determine the name of the package.
139 name
= os
.path
.basename(file)
140 name
= name
[:len(MAKEFILE_EXTENSION
) + 1]
142 source
.distro
.delete_package(name
)
145 commit
.state
= "finished"
148 class Commit(base
.DataObject
):
149 table
= "sources_commits"
153 return self
.data
.revision
157 return self
.backend
.sources
.get_by_id(self
.data
.source_id
)
162 A shortcut to the distribution this commit
165 return self
.source
.distro
167 def set_state(self
, state
):
168 self
._set
_attribute
("state", state
)
170 state
= property(lambda s
: s
.data
.state
, set_state
)
174 return self
.data
.author
178 return self
.data
.committer
182 return self
.data
.subject
.strip()
186 return self
.data
.body
.strip()
189 def message_full(self
):
190 msg
= [self
.subject
, ""] + self
.message
.splitlines()
192 return "\n".join(msg
)
196 return self
.data
.date
200 return self
.backend
.packages
._get
_packages
("SELECT * FROM packages \
201 WHERE commit_id = %s", self
.id)
205 Removes all packages that have been created by this commit and
206 resets the state so it will be processed again.
208 # Remove all packages and corresponding builds.
209 for pkg
in self
.packages
:
210 # Check if there is a build associated with the package.
211 # If so, the whole build will be deleted.
216 # Delete the package.
222 # Reset the state to 'pending'.
223 self
.state
= "pending"
226 class Source(base
.DataObject
):
229 def __eq__(self
, other
):
230 return self
.id == other
.id
233 ret
= self
.db
.get("SELECT COUNT(*) AS len FROM sources_commits \
234 WHERE source_id = %s", self
.id)
238 def create_commit(self
, revision
, author
, committer
, subject
, body
, date
):
239 commit
= self
.backend
.sources
._get
_commit
("INSERT INTO sources_commits(source_id, \
240 revision, author, committer, subject, body, date) VALUES(%s, %s, %s, %s, %s, %s, %s) \
241 RETURNING *", self
.id, revision
, author
, committer
, subject
, body
, date
)
255 "targetpath" : self
.targetpath
,
256 "revision" : self
.revision
,
257 "branch" : self
.branch
,
262 return self
.data
.name
265 def identifier(self
):
266 return self
.data
.identifier
274 return self
.data
.gitweb
278 return self
.data
.revision
282 return self
.data
.branch
286 return self
.pakfire
.builds
.get_by_source(self
.id)
290 return self
.pakfire
.distros
.get_by_id(self
.data
.distro_id
)
293 def start_revision(self
):
294 return self
.data
.revision
297 def head_revision(self
):
298 return self
.backend
.sources
._get
_commit
("SELECT * FROM sources_commits \
299 WHERE source_id = %s ORDER BY id DESC LIMIT 1", self
.id)
301 def get_commits(self
, limit
=None, offset
=None):
302 return self
.backend
.sources
._get
_commits
("SELECT * FROM sources_commits \
303 WHERE source_id = %s ORDER BY id DESC LIMIT %s OFFSET %s", limit
, offset
)
305 def get_commit(self
, revision
):
306 commit
= self
.backend
.sources
._get
_commit
("SELECT * FROM sources_commits \
307 WHERE source_id = %s AND revision = %s", self
.id, revision
)
314 def pending_commits(self
):
315 return self
.backend
.sources
._get
_commits
("SELECT * FROM sources_commits \
316 WHERE state = %s ORDER BY imported_at", "pending")