def insertData(db, data): #logging.debug(data) metadata=json.dumps(data["metadata"]) results = db.query("SELECT fid FROM file WHERE sdp='%s' or md5='%s'"% (data['sdp'], data['md5'])) res=results.first() if res: fid=res['fid'] else: fid=0 assert(data["path"] in ("games", "maps")) assert("/" not in data["filename"]) assert(data["uid"] > 0) data["cid"] = upqdb.getCID(db, data["category_name"]) if fid<=0: fid=db.insert("file", { "name": data["name"], "version": data["version"], "sdp": data['sdp'], "cid": data["cid"], "metadata": metadata, "uid": data['uid'], "path": data["path"], "filename": data["filename"], "timestamp": data["timestamp"], "size": data["size"], "status": 1, "md5": data["md5"], "sha1": data["sha1"], "sha256": data["sha256"], }) else: db.query("UPDATE file SET name='%s', version='%s', sdp='%s', cid=%s, metadata='%s', md5='%s', sha1='%s', sha256='%s', status=1, filename='%s', path='%s' WHERE fid=%s" %( data['name'], data['version'], data['sdp'], data["cid"], metadata, data["md5"], data["sha1"], data["sha256"], data["filename"], data["path"], fid )) # remove already existing depends db.query("DELETE FROM file_depends WHERE fid = %s" % (fid) ) for depend in data["metadata"]['Depends']: res=db.query("SELECT fid FROM file WHERE CONCAT(name,' ',version)='%s'" % (depend)) row=res.first() if not row: id=0 else: id=row['fid'] try: db.insert("file_depends", {"fid":fid, "depends_string": depend, "depends_fid": id}) logging.info("Added '%s' version '%s' to the mirror-system" % (data['name'], data['version'])) except upqdb.UpqDBIntegrityError: pass return fid
def update(prefix, db, data, mid): """ data is an array with md5 filectime version branch filesize os path """ if data['version'] == "testing": return filename = escape(data['path'][data['path'].rfind("/")+1:]) category = "engine_" + data['os'] branch = data['branch'] version = data['version'] if not branch in ('master'): version = version + ' ' + branch url = prefix +'/' + data['path'] cid = upqdb.getCID(db, category) #print "%s %s %s %s" % (filename, version, category, url) res = db.query("SELECT fid, md5, timestamp from file WHERE version='%s' and cid=%s" % (version, cid)) fileinfo = res.first() timestamp = datetime.datetime.fromtimestamp(data['filectime']) if fileinfo: fid = fileinfo[0] assert(fid > 0) if data["md5"] != fileinfo[1] or timestamp != fileinfo[2]: db.query("UPDATE file set md5='%s', timestamp='%s' WHERE fid=%s"% (data['md5'], timestamp, fid)) else: fid = db.insert("file", { "filename" : filename, "name": "spring", "version": version, "cid" : cid, "md5" : data['md5'], "timestamp": timestamp, "size": data['filesize'], "status": 1, }) res = db.query("SELECT mfid FROM mirror_file WHERE mid=%s AND fid=%s" % (mid, fid)) mfid = res.first() if mfid: mfid = mfid[0] assert(mid > 0) db.query("UPDATE mirror_file SET lastcheck=NOW(), path='%s' WHERE mfid = %s"% (data['path'], mfid)) else: mfid = db.insert("mirror_file", { "mid" : mid, "path": data['path'], "status": 1, "fid": fid, "lastcheck": upqdb.now() })
def UpdateSDP(db, sdp): """ values = { "tag"=sdp[0], "md5"=sdp[1], "depends"=sdp[2], "name"=sdp[3], } """ #check if file is already known res = db.query("SELECT f.fid FROM file f \ LEFT JOIN tag t ON t.fid=f.fid \ WHERE sdp='%s'" % (sdp[1])) row = res.first() if row: #file is already known #delete tag from existing files db.query("DELETE FROM tag WHERE tag='%s'" % (sdp[0])) #insert updated tag db.query("INSERT INTO tag (fid, tag) VALUES (%s, '%s')" % (row['fid'], sdp[0])) #logging.info("updated %s %s %s %s",sdp[3],sdp[2],sdp[1],sdp[0]) return if not sdp[3]: #without a name, we can't do anything! return cid = upqdb.getCID(db, "game") try: fid = db.insert( "file", { "filename": sdp[3] + " (not available as sdz)", "name": sdp[3], "cid": cid, "md5": sdp[1], "sdp": sdp[1], "size": 0, "status": 4, # special status for files that can only be downloaded via rapid "uid": 0, "path": "", }) db.query("INSERT INTO tag (fid, tag) VALUES (%s, '%s')" % (fid, sdp[0])) #logging.info("inserted %s %s %s %s",sdp[3],sdp[2],sdp[1],sdp[0]) except Exception as e: logging.error(str(e)) logging.error("Error from sdp: %s %s %s %s", sdp[3], sdp[2], sdp[1], sdp[0]) res = db.query("SELECT * FROM file f WHERE name='%s'" % sdp[3]) if res: row = res.first() logging.error( "a file with this name already exists, fid=%s, sdp=%s" % (row['fid'], row['sdp']))
def update(self, data, mid): """ data is an array with md5 filectime version branch filesize os path """ if data['version'] == "testing": return filename = self.escape(data['path'][data['path'].rfind("/") + 1:]) category = "engine_" + data['os'] branch = data['branch'] version = data['version'] if not data['branch'] in ('master'): version = data['version'] + ' ' + data['branch'] url = self.prefix + '/' + data['path'] cid = upqdb.getCID(self.db, category) #print "%s %s %s %s" % (filename, version, category, url) try: fid = upqdb.UpqDB().insert( "file", { "filename": filename, "name": "spring", "version": version, "cid": cid, "md5": data['md5'], "timestamp": datetime.datetime.fromtimestamp( data['filectime']), #"timestamp": data['filectime'], "size": data['filesize'], "status": 1 }) except upqdb.UpqDBIntegrityError as e: try: res = upqdb.UpqDB().query( "SELECT fid from file WHERE version='%s' and cid=%s" % (version, cid)) fid = res.first()[0] upqdb.UpqDB().query("UPDATE file set md5='%s' WHERE fid=%s" % (data['md5'], fid)) except Exception as e: logging.error("Error %s %s %s", version, cid, e) return relpath = self.escape(url[len(self.prefix) + 1:]) try: id = upqdb.UpqDB().insert( "mirror_file", { "mid": mid, "path": relpath, "status": 1, "fid": fid, "lastcheck": upqdb.UpqDB().now() }) except upqdb.UpqDBIntegrityError: res = upqdb.UpqDB().query( "SELECT mfid FROM mirror_file WHERE mid=%s AND fid=%s" % (mid, fid)) id = res.first()[0] upqdb.UpqDB().query( "UPDATE mirror_file SET lastcheck=NOW() WHERE mfid = %s" % (id))