]>
git.ipfire.org Git - people/jschlag/pbs.git/blob - src/buildservice/sources.py
13 from . import database
16 from .constants
import *
17 from .decorators
import *
19 class Sources(base
.Object
):
20 def _get_source(self
, query
, *args
):
21 res
= self
.db
.get(query
, *args
)
24 return Source(self
.backend
, res
.id, data
=res
)
26 def _get_sources(self
, query
, *args
):
27 res
= self
.db
.query(query
, *args
)
30 yield Source(self
.backend
, row
.id, data
=row
)
32 def _get_commit(self
, query
, *args
):
33 res
= self
.db
.get(query
, *args
)
36 return Commit(self
.backend
, res
.id, data
=res
)
38 def _get_commits(self
, query
, *args
):
39 res
= self
.db
.query(query
, *args
)
42 yield Commit(self
.backend
, row
.id, data
=row
)
45 return self
._get
_sources
("SELECT * FROM sources")
47 def get_by_id(self
, id):
48 return self
._get
_source
("SELECT * FROM sources \
51 def get_by_distro(self
, distro
):
52 return self
._get
_sources
("SELECT * FROM sources \
53 WHERE distro_id = %s", distro
.id)
55 def update_revision(self
, source_id
, revision
):
56 query
= "UPDATE sources SET revision = %s WHERE id = %s"
58 return self
.db
.execute(query
, revision
, source_id
)
60 def get_commit_by_id(self
, commit_id
):
61 commit
= self
.db
.get("SELECT id FROM sources_commits WHERE id = %s", commit_id
)
64 return Commit(self
.pakfire
, commit
.id)
68 with git
.Repo(self
.backend
, source
, mode
="mirror") as repo
:
69 # Fetch the latest updates
72 # Import all new revisions
73 repo
.import_revisions()
76 # Walk through all source repositories
78 # Get access to the git repo
79 with git
.Repo(self
.pakfire
, source
) as repo
:
80 # Walk through all pending commits
81 for commit
in source
.pending_commits
:
82 commit
.state
= "running"
84 logging
.debug("Processing commit %s: %s" % (commit
.revision
, commit
.subject
))
86 # Navigate to the right revision.
87 repo
.checkout(commit
.revision
)
89 # Get all changed makefiles.
93 for file in repo
.changed_files(commit
.revision
):
94 # Don't care about files that are not a makefile.
95 if not file.endswith(".%s" % MAKEFILE_EXTENSION
):
98 if os
.path
.exists(file):
99 updated_files
.append(file)
101 deleted_files
.append(file)
104 # Create a temporary directory where to put all the files
105 # that are generated here.
106 pkg_dir
= tempfile
.mkdtemp()
109 config
= pakfire
.config
.Config(["general.conf",])
110 config
.parse(source
.distro
.get_config())
112 p
= pakfire
.PakfireServer(config
=config
)
115 for file in updated_files
:
117 pkg_file
= p
.dist(file, pkg_dir
)
118 pkgs
.append(pkg_file
)
122 # Import all packages in one swoop.
124 # Import the package file and create a build out of it.
126 builds
.import_from_package(_pakfire
, pkg
,
127 distro
=source
.distro
, commit
=commit
, type="release")
131 commit
.state
= "failed"
136 if os
.path
.exists(pkg_dir
):
137 shutil
.rmtree(pkg_dir
)
139 for file in deleted_files
:
140 # Determine the name of the package.
141 name
= os
.path
.basename(file)
142 name
= name
[:len(MAKEFILE_EXTENSION
) + 1]
144 source
.distro
.delete_package(name
)
147 commit
.state
= "finished"
150 class Commit(base
.DataObject
):
151 table
= "sources_commits"
155 return self
.data
.revision
159 return self
.backend
.sources
.get_by_id(self
.data
.source_id
)
164 A shortcut to the distribution this commit
167 return self
.source
.distro
169 def set_state(self
, state
):
170 self
._set
_attribute
("state", state
)
172 state
= property(lambda s
: s
.data
.state
, set_state
)
176 return self
.data
.author
180 return self
.data
.committer
184 return self
.data
.subject
.strip()
188 return self
.data
.body
.strip()
191 def message_full(self
):
192 msg
= [self
.subject
, ""] + self
.message
.splitlines()
194 return "\n".join(msg
)
198 return self
.data
.date
202 return self
.backend
.packages
._get
_packages
("SELECT * FROM packages \
203 WHERE commit_id = %s", self
.id)
207 Removes all packages that have been created by this commit and
208 resets the state so it will be processed again.
210 # Remove all packages and corresponding builds.
211 for pkg
in self
.packages
:
212 # Check if there is a build associated with the package.
213 # If so, the whole build will be deleted.
218 # Delete the package.
224 # Reset the state to 'pending'.
225 self
.state
= "pending"
228 class Source(base
.DataObject
):
231 def __eq__(self
, other
):
232 return self
.id == other
.id
235 ret
= self
.db
.get("SELECT COUNT(*) AS len FROM sources_commits \
236 WHERE source_id = %s", self
.id)
240 def create_commit(self
, revision
, author
, committer
, subject
, body
, date
):
241 commit
= self
.backend
.sources
._get
_commit
("INSERT INTO sources_commits(source_id, \
242 revision, author, committer, subject, body, date) VALUES(%s, %s, %s, %s, %s, %s, %s) \
243 RETURNING *", self
.id, revision
, author
, committer
, subject
, body
, date
)
257 "targetpath" : self
.targetpath
,
258 "revision" : self
.revision
,
259 "branch" : self
.branch
,
264 return self
.data
.name
267 def identifier(self
):
268 return self
.data
.identifier
276 return self
.data
.gitweb
280 return self
.data
.revision
284 return self
.data
.branch
288 return self
.pakfire
.builds
.get_by_source(self
.id)
292 return self
.pakfire
.distros
.get_by_id(self
.data
.distro_id
)
295 def start_revision(self
):
296 return self
.data
.revision
299 def head_revision(self
):
300 return self
.backend
.sources
._get
_commit
("SELECT * FROM sources_commits \
301 WHERE source_id = %s ORDER BY id DESC LIMIT 1", self
.id)
303 def get_commits(self
, limit
=None, offset
=None):
304 return self
.backend
.sources
._get
_commits
("SELECT * FROM sources_commits \
305 WHERE source_id = %s ORDER BY id DESC LIMIT %s OFFSET %s", limit
, offset
)
307 def get_commit(self
, revision
):
308 commit
= self
.backend
.sources
._get
_commit
("SELECT * FROM sources_commits \
309 WHERE source_id = %s AND revision = %s", self
.id, revision
)
316 def pending_commits(self
):
317 return self
.backend
.sources
._get
_commits
("SELECT * FROM sources_commits \
318 WHERE state = %s ORDER BY imported_at", "pending")