]>
git.ipfire.org Git - people/jschlag/pbs.git/blob - src/buildservice/uploads.py
3 from __future__
import division
12 import pakfire
.packages
16 from . import packages
18 from .constants
import *
19 from .decorators
import *
21 class Uploads(base
.Object
):
22 def _get_upload(self
, query
, *args
):
23 res
= self
.db
.get(query
, *args
)
26 return Upload(self
.backend
, res
.id, data
=res
)
28 def _get_uploads(self
, query
, *args
):
29 res
= self
.db
.query(query
, *args
)
32 yield Upload(self
.backend
, row
.id, data
=row
)
35 uploads
= self
._get
_uploads
("SELECT * FROM uploads ORDER BY time_started DESC")
37 return iter(sorted(uploads
))
39 def get_by_uuid(self
, _uuid
):
40 return self
._get
_upload
("SELECT * FROM uploads WHERE uuid = %s", uuid
)
42 def create(filename
, size
, hash, builder
=None, user
=None):
43 assert builder
or user
45 upload
= self
._get
_upload
("INSERT INTO uploads(uuid, filename, size, hash) \
46 VALUES(%s, %s, %s, %s) RETURNING *", "%s" % uuid
.uuid4(), filename
, size
, hash)
49 upload
.builder
= builder
54 # Create space to where we save the data.
55 dirname
= os
.path
.dirname(upload
.path
)
56 if not os
.path
.exists(dirname
):
60 f
= open(upload
.path
, "w")
66 for upload
in self
.get_all():
70 class Upload(base
.DataObject
):
83 return self
.data
.filename
87 return os
.path
.join(UPLOADS_DIR
, self
.uuid
, self
.filename
)
95 return self
.data
.progress
/ self
.size
99 def get_builder(self
):
100 if self
.data
.builder_id
:
101 return self
.backend
.builders
.get_by_id(self
.data
.builder_id
)
103 def set_builder(self
, builder
):
104 self
._set
_attribute
("builder", builder
.id)
105 self
.builder
= builder
107 builder
= lazy_property(get_builder
, set_builder
)
112 if self
.data
.user_id
:
113 return self
.backend
.users
.get_by_id(self
.data
.user_id
)
115 def set_user(self
, user
):
116 self
._set
_attribute
("user", user
.id)
119 user
= lazy_property(get_user
, set_user
)
121 def append(self
, data
):
122 # Check if the filesize was exceeded.
123 size
= os
.path
.getsize(self
.path
) + len(data
)
124 if size
> self
.data
.size
:
125 raise Exception, "Given filesize was exceeded for upload %s" % self
.uuid
127 logging
.debug("Writing %s bytes to %s" % (len(data
), self
.path
))
129 with
open(self
.path
, "ab") as f
:
132 self
._set
_attribute
("progress", size
)
135 size
= os
.path
.getsize(self
.path
)
136 if not size
== self
.data
.size
:
137 logging
.error("Filesize is not okay: %s" % (self
.uuid
))
140 # Calculate a hash to validate the upload.
141 hash = misc
.calc_hash1(self
.path
)
143 if not self
.hash == hash:
144 logging
.error("Hash did not match: %s != %s" % (self
.hash, hash))
151 Update the status of the upload in the database to "finished".
153 # Check if the file was completely uploaded and the hash is correct.
154 # If not, the upload has failed.
155 if not self
.validate():
158 self
._set
_attribute
("finished", True)
159 self
._set
_attribute
("time_finished", datetime
.datetime
.utcnow())
164 # Remove the uploaded data.
165 path
= os
.path
.dirname(self
.path
)
166 if os
.path
.exists(path
):
167 shutil
.rmtree(path
, ignore_errors
=True)
169 # Delete the upload from the database.
170 self
.db
.execute("DELETE FROM uploads WHERE id = %s", self
.id)
173 def time_started(self
):
174 return self
.data
.time_started
177 def time_running(self
):
178 # Get the seconds since we are running.
180 time_running
= datetime
.datetime
.utcnow() - self
.time_started
181 time_running
= time_running
.total_seconds()
189 if not self
.time_running
:
192 return self
.data
.progress
/ self
.time_running
195 # Remove uploads that are older than 2 hours.
196 if self
.time_running
>= 3600 * 2: