def confirm_pid(run_folder): """ TBD """ import sys, os, signal, __main__ name = prefix(".", os.path.basename(__main__.__file__)) log.info("Attempting to launch daemon %s..." % name) pid = str(os.getpid()) pidfile = "%s%s.pid" % (run_folder, name) if os.path.isfile(pidfile): old_pid = open(pidfile).read() log.warning("--> pidfile already exists for %s, attempting to kill process..." % old_pid) try: result = os.kill(int(old_pid), signal.SIGKILL) except OSError, e: if e.args[0] == 3: log.warning("--> no process with pid %s" % old_pid) else: log.error(e) exit() else: log.info("--> killed process %s" % old_pid) try: os.unlink(pidfile) except OSError, e: log.error("--> could not remove pidfile, %s" % pidfile) exit()
def confirm_pid(run_folder): """ TBD """ import sys, os, signal, __main__ name = prefix('.', os.path.basename(__main__.__file__)) log.info("Attempting to launch daemon %s..." % name) pid = str(os.getpid()) pidfile = "%s%s.pid" % (run_folder, name) if os.path.isfile(pidfile): old_pid = open(pidfile).read() log.warning( "--> pidfile already exists for %s, attempting to kill process..." % old_pid) try: result = os.kill(int(old_pid), signal.SIGKILL) except OSError, e: if e.args[0] == 3: log.warning("--> no process with pid %s" % old_pid) else: log.error(e) exit() else: log.info("--> killed process %s" % old_pid) try: os.unlink(pidfile) except OSError, e: log.error("--> could not remove pidfile, %s" % pidfile) exit()
def send_from_template(cls, addresses, subject, template_name, template_values=None, attachment=None, from_name=None, from_address=None, **kwags): """ Create HTML and text emails from template, then send. """ log.info("Emailer.send_from_template (%s)" % template_name) # Render email template as HTML. try: html = cls.render(template_name, template_values) except TemplateNotFound: log.warning("HTML template not found.") html = None try: text = cls.render(template_name, template_values, suffix="txt") except TemplateNotFound: log.warning("Text template not found.") text = None log.debug(html) return cls.send(addresses, subject, text, html, attachment, from_name, from_address)
def warning(self, message): log.warning("400: %s" % message) return self.render('error', { 'error_code': 400, 'error_message': message }, status='400 %s' % message)
def removeDbRecord(self, db, id): try: db.query("DELETE FROM files WHERE id=$id", {'id': id}) log.warning("--> removed id %s" % id) return True except Exception, e: log.error(e) return False
def deobfuscate(token): """ Base 64 decodes the given string and returns the leading digits of the decoded string. """ try: token = base64.b64decode(token) except Exception, e: log.warning("Deobfuscate failed!") return None
def clean(message): message = message.strip() if len(message) > 160: log.warning("--> message is too long! will be cut off!") message = message[:160] for c in range(len(message)): if ord(message[c]) > 127: log.warning("--> message contains a weird character that will be converted to '?'") message = message[:c] + '?' + message[c+1:] return message
def __init__(self): """ Constructor to define beanstalkd queue. """ try: self.queue = beanstalkc.Connection( host=Config.get('beanstalk')['address'], port=Config.get('beanstalk')['port']) except Exception, e: log.warning("Could not create queue.") self.queue = None
def __init__(self): """ Constructor to define beanstalkd queue. """ try: self.queue = beanstalkc.Connection( host=Config.get("beanstalk")["address"], port=Config.get("beanstalk")["port"] ) except Exception, e: log.warning("Could not create queue.") self.queue = None
def __exit__(self, e_type=None, e_val=None, e_tb=None): from traceback import format_exception if e_type == web.HTTPError: log.debug("*** web.HTTPError with the ORM") log.warning(''.join(format_exception(e_type, e_val, e_tb))) self.orm.commit() elif e_type: log.debug("*** Other exception with the ORM") log.error(''.join(format_exception(e_type, e_val, e_tb))) OrmHolder.invalidate() else: log.debug("*** Finishing up with the ORM %r" % self.orm) self.orm.commit()
def add(self, tube=None, func=None, data=None, timeout=120): """ Add a task to queue and use specific tube if provided. """ if self.queue is None: log.warning("Attempted to add task, but task queue is not running.") return if tube is not None: self.queue.use(tube) log.info("Tasks.add tube[%s] func[%s]" % (self.queue.using(), func)) self.queue.put(pickle.dumps(Task(func, data)), ttr=timeout)
def __exit__(self, e_type=None, e_val=None, e_tb=None): # Since load_sqla is a processor, it catches bubbled-up exceptions from traceback import format_exception if e_type == web.HTTPError: log.debug("*** web.HTTPError with the ORM") log.warning(''.join(format_exception(e_type, e_val, e_tb))) self.orm.commit() elif e_type: log.debug("*** Unhandled exception - check console logs for details") log.error(''.join(format_exception(e_type, e_val, e_tb))) OrmHolder.invalidate() else: log.debug("*** Finishing up with the ORM %r" % self.orm) self.orm.commit()
def add(self, tube=None, func=None, data=None, timeout=120): """ Add a task to queue and use specific tube if provided. """ if self.queue is None: log.warning( "Attempted to add task, but task queue is not running.") return if tube is not None: self.queue.use(tube) log.info("Tasks.add tube[%s] func[%s]" % (self.queue.using(), func)) self.queue.put(pickle.dumps(Task(func, data)), ttr=timeout)
def authenticateUser(db, email, password): sql = "select user_id, email, password, salt from user where email = $email and is_active = 1" data = db.query(sql, {'email': email}) if (len(data) > 0): user = list(data)[0] hashed_password = makePassword(password, user.salt) if (hashed_password[0] == user.password): log.info("*** User authenticated") return user.user_id else: log.warning("*** User not authenticated for email = %s" % email) return None else: log.warning("*** No record for email= %s" % email) return None
def authenticateUser(db, email, password): sql = "select user_id, email, password, salt from user where email = $email and is_active = 1" data = db.query(sql, {'email':email}) if (len(data) > 0): user = list(data)[0] hashed_password = makePassword(password, user.salt) if (hashed_password[0] == user.password): log.info("*** User authenticated") return user.user_id else: log.warning("*** User not authenticated for email = %s" % email) return None else: log.warning("*** No record for email= %s" % email) return None
def get_fake_session(controller): """ Get the session manually (like from a request variable) instead of a cookie. Flash cant consistently get cookie data. """ import os, base64, pickle session_id = controller.request('session_id') path = "sessions/%s" % session_id if not os.path.exists(path): log.warning("--> get_fake_session: key path (%s) doesnt exist" % path) return {} try: raw = open(path).read() pickled = base64.decodestring(raw) fake_session = pickle.loads(pickled) except Exception, e: log.error("--> get_fake_session error: %s" % e) return {}
def authGetUser(db, email, password): sql = "select user_id, first_name, last_name, affiliation, group_membership_bitmask, image_id, email, password, salt from user where email = $email and is_active = 1" data = db.query(sql, {'email': email}) if (len(data) > 0): user = list(data)[0] hashed_password = makePassword(password, user.salt) if (hashed_password[0] == user.password): log.info("*** User authenticated") return mProject.smallUserDisplay( user.user_id, mProject.userNameDisplay( user.first_name, user.last_name, user.affiliation, mProject.isFullLastName(user.group_membership_bitmask)), user.image_id) else: log.warning("*** User not authenticated for email = %s" % email) return None else: log.warning("*** No record for email= %s" % email) return None
def authGetUser(db, email, password): sql = "select user_id, first_name, last_name, affiliation, group_membership_bitmask, image_id, email, password, salt from user where email = $email and is_active = 1" data = db.query(sql, {'email':email}) if (len(data) > 0): user = list(data)[0] hashed_password = makePassword(password, user.salt) if (hashed_password[0] == user.password): log.info("*** User authenticated") return mProject.smallUserDisplay(user.user_id, mProject.userNameDisplay(user.first_name, user.last_name, user.affiliation, mProject.isFullLastName(user.group_membership_bitmask)), user.image_id) else: log.warning("*** User not authenticated for email = %s" % email) return None else: log.warning("*** No record for email= %s" % email) return None
def safeuni(s): """ Tries to convert an object to a unicode string, handling common edge cases. """ #if unicode if isinstance(s, unicode): return s #if not a string if not isinstance(s, basestring): #if can be converted to unicode, then go for it if hasattr(s, '__unicode__'): return unicode(s) else: return str(s).decode('utf-8') try: # assume a utf-8 string, then try to convert it # unicode() is expecting a utf-8 bytestring (unicode itself is not utf-8 or anything else) s = unicode(s, errors='strict', encoding='utf-8') except UnicodeDecodeError, e: log.warning(e) # dump anything that doesnt make sense in utf-8 s = unicode(s, errors='ignore', encoding='utf-8')
def safeuni(s): """ Tries to convert an object to a unicode string, handling common edge cases. """ # if unicode if isinstance(s, unicode): return s # if not a string if not isinstance(s, basestring): # if can be converted to unicode, then go for it if hasattr(s, "__unicode__"): return unicode(s) else: return str(s).decode("utf-8") try: # assume a utf-8 string, then try to convert it # unicode() is expecting a utf-8 bytestring (unicode itself is not utf-8 or anything else) s = unicode(s, errors="strict", encoding="utf-8") except UnicodeDecodeError, e: log.warning(e) # dump anything that doesnt make sense in utf-8 s = unicode(s, errors="ignore", encoding="utf-8")
# Create file. f = open(path, "wb") f.write(data) f.close() # Get image object from new file. image = Image.open(path) except Exception, e: log.error(e) try: db.query("DELETE FROM images WHERE id=$id", {"id": id}) os.remove(path) except Exception, e: log.error(e) log.warning("--> removed id %s" % id) return None if image.format != "PNG": log.info("--> converting %s to PNG" % image.format) if max_size and (image.size[0] > max_size[0] or image.size[1] > max_size[1]): image = ImageServer.cropToBox(image) image = image.resize(max_size) if grayscale: image = ImageOps.grayscale(image) if thumb_max_size: thumbImage = ImageServer.resizeToFit(image, thumb_max_size) thumbPath = "".join([path[:-4], "_thumb.png"]) try: image.save(path, "PNG") if thumb_max_size:
# Create file. f = open(path, "wb") f.write(data) f.close() # Get image object from new file. image = Image.open(path) except Exception, e: log.error(e) try: db.query("DELETE FROM images WHERE id=$id", {'id': id}) os.remove(path) except Exception, e: log.error(e) log.warning("--> removed id %s" % id) return None if image.format != "PNG": log.info("--> converting %s to PNG" % image.format) if max_size and (image.size[0] > max_size[0] or image.size[1] > max_size[1]): image = ImageServer.cropToBox(image) image = image.resize(max_size) if grayscale: image = ImageOps.grayscale(image) if thumb_max_size: thumbImage = ImageServer.resizeToFit(image, thumb_max_size) thumbPath = ''.join([path[:-4], "_thumb.png"]) try: image.save(path, "PNG")