]>
git.ipfire.org Git - people/jschlag/pbs.git/blob - backend/uploads.py
6 import pakfire
.packages
13 from constants
import *
15 class Uploads(base
.Object
):
16 def get_by_uuid(self
, _uuid
):
17 upload
= self
.db
.get("SELECT id FROM uploads WHERE uuid = %s", _uuid
)
19 return Upload(self
.pakfire
, upload
.id)
21 def new(self
, *args
, **kwargs
):
22 return Upload
.new(self
.pakfire
, *args
, **kwargs
)
25 uploads
= self
.db
.query("SELECT id FROM uploads")
27 return [Upload(self
.pakfire
, u
.id) for u
in uploads
]
30 for upload
in self
.get_all():
34 class Upload(base
.Object
):
35 def __init__(self
, pakfire
, id):
36 base
.Object
.__init
__(self
, pakfire
)
39 self
.data
= self
.db
.get("SELECT * FROM uploads WHERE id = %s", self
.id)
42 def new(cls
, pakfire
, builder
, filename
, size
, hash):
45 id = pakfire
.db
.execute("INSERT INTO uploads(uuid, builder, filename, size, hash)"
46 " VALUES(%s, %s, %s, %s, %s)", _uuid
, builder
.id, filename
, size
, hash)
48 upload
= cls(pakfire
, id)
50 # Create space to where we save the data.
51 dirname
= os
.path
.dirname(upload
.path
)
52 if not os
.path
.exists(dirname
):
56 f
= open(upload
.path
, "w")
71 return self
.data
.filename
75 return os
.path
.join(UPLOADS_DIR
, self
.uuid
, self
.filename
)
79 return self
.pakfire
.builders
.get_by_id(self
.data
.builder
)
81 def append(self
, data
):
82 logging
.debug("Writing %s bytes to %s" % (len(data
), self
.path
))
84 f
= open(self
.path
, "ab")
89 # Calculate a hash to validate the upload.
90 hash = misc
.calc_hash1(self
.path
)
92 ret
= self
.hash == hash
95 logging
.error("Hash did not match: %s != %s" % (self
.hash, hash))
100 # Remove the uploaded data.
101 if os
.path
.exists(self
.path
):
104 # Delete the upload from the database.
105 self
.db
.execute("DELETE FROM uploads WHERE id = %s", self
.id)
107 def time_start(self
):
108 return self
.data
.time_start
110 def commit(self
, build
):
111 # Find out what kind of file this is.
112 filetype
= misc
.guess_filetype(self
.path
)
114 # If the filetype is unhandled, we remove the file and raise an
116 if filetype
== "unknown":
118 raise Exception, "Cannot handle unknown file."
120 # If file is a package we open it and insert its information to the
122 if filetype
== "pkg":
123 logging
.debug("%s is a package file." % self
.path
)
124 file = pakfire
.packages
.open(None, None, self
.path
)
126 if file.type == "source":
127 packages
.Package
.new(self
.pakfire
, file, build
)
129 elif file.type == "binary":
130 build
.pkg
.add_file(file, build
)
132 elif filetype
== "log":
133 build
.add_log(self
.path
)
135 # Finally, remove the upload.
139 # Get the seconds since we are running.
141 time_running
= datetime
.datetime
.utcnow() - self
.time_start
142 time_running
= time_running
.total_seconds()
146 # Remove uploads that are older than 24 hours.
147 if time_running
>= 3600 * 24: