def run(self, options): logs.begin( saveLog=stampedAPI._logsDB.saveLog, saveStat=stampedAPI._statsDB.addStat, nodeName=stampedAPI.node_name ) logs.async_request('alerts') lock = os.path.join(base, 'alerts.lock') if os.path.exists(lock): logs.warning('Locked - aborting') return try: open(lock, 'w').close() self.buildAlerts(limit=options.limit, noop=options.noop) self.buildInvitations(limit=options.limit, noop=options.noop) if len(self._emailQueue) > 0: self.sendEmails(noop=options.noop) if len(self._pushQueue) > 0: self.sendPush(noop=options.noop) self.cleanupPush(noop=options.noop) except Exception as e: logs.warning('Exception: %s' % e) logs.warning(utils.getFormattedException()) logs.error(500) finally: os.remove(lock) try: logs.save() except Exception: print '\n\n\nWARNING: UNABLE TO SAVE LOGS\n\n\n'
def login(): input_info = input("请输入用户名/密码:") info = input_info.split("/") if len(info) != 2: logs.error("您的输入格式错误,请按照:用户名/密码") return if info[0] not in page.users or info[1] != page.users[info[0]]: logs.error("您输入的用户名或密码错误!") return if info[0] in page.admins: page.model_level01[1] = "删除用户" page.model_level01[3] = "查询所有用户" page.model_level01[5] = "导出用户信息csv格式" else: if 3 in page.model_level01.keys(): page.model_level01.pop(1) page.model_level01.pop(3) page.model_level01.pop(5) logs.info(info[0] + "登录成功") while True: in2 = page.page.home01(page.model_level01) op = page.page.print_model(in2, page.model_level01, info[0]) if op == "exit": return
def getFromCache(self, limit, offset=0, **kwargs): """ Pull the requested data from cache if it exists there, otherwise pull the data from db Returns a tuple of (the data list, bool indicating if the end of the collection stream was reached) """ t0 = time.time() t1 = t0 if offset == 0: self._clearCacheForKeyParams(**kwargs) data = [] while len(data) < limit: curOffset = ((offset + len(data)) // self._blockSize) * self._blockSize key = self._generateKey(curOffset, **kwargs) try: newData = self._cache[key] except KeyError: newData = self._updateCache(offset=curOffset, **kwargs) except Exception: logs.error('Error retrieving data from memcached. Is memcached running?') newData = self._updateCache(curOffset, **kwargs) start = (offset+len(data)) % self._blockSize data += newData[start:start+limit] if len(newData) < self._blockSize: break logs.debug('Time for getFromCache: %s' % (time.time() - t1)) t1 = time.time() logs.debug('total returned: %s' % len(data)) return data[:limit], len(data) < limit
def write_uer(user_message): with open('user.txt', 'r+') as fd: note = fd.read() # 如果用户表为空,则手动赋值,避免loads报错 if note == '': note = '{}' try: # 初始化用户信息 dict1 = json.loads(note) except Exception as e: print("\033[1;31m用户文件 user.txt 存在异常,请手动修复\033[0m") logs.error(e) return [False, None] # 判断用户是否存在 if user_message[1] in dict1.keys(): return [ False, "\033[1;31m用户{}已存在,添加失败!!\033[0m".format(user_message[1]) ] # 拼凑插入字典的value for i in range(len(title)): user_tem[title[i]] = user_message[i] dict1[user_message[1]] = user_tem # 格式化数据,并写入 str1 = json.dumps(dict1, indent=4) with open('user.txt', 'w') as fd: fd.write(str1) return [True, "\033[1;32m用户{}添加成功\033[0m".format(user_message[1])]
def list_user(user): if user in admins.keys(): print(" 查看所有用户 ".center(len(h), "*")) for i in users.keys(): out = "userName: {}".format(i).ljust(10) print(out.center(len(h))) else: logs.error("非管理员不能查看所有用户信息")
def list_user(user): if user in page.admins.keys(): print(" 查看所有用户 ".center(len(page.h), "*")) for i in page.users.keys(): out = "userName: {:<12}".format(i) print(out.center(len(page.h))) else: logs.error("非管理员不能查看所有用户信息")
def error(message, parser=None): logs.error(message) if parser: print("") parser.print_help() sys.exit(1)
def user(info_list, role): # 检查用户权限 if role != 'admin': print("\033[1;31mpermission denied\033[0m") return # 检查用户输入内容 if len(info_list) != 2: print("\033[1;31m输入有误,请检查输入内容 eg: delete monkey\033[0m") return # 检查用户是否存在 result, message, user_id = check.user(info_list[1]) if result is False: print(message) return elif result is None: return # 读取用户信息 with open('user.txt', 'r') as user_fd: user_note = user_fd.read() if user_note == '': user_note = '{}' # 写入修改后信息 try: user_dict = json.loads(user_note) user_dict.pop(message) except Exception as e: print("\033[1;31m 用户文件 user.txt存在异常,请手动修复\033[0m") logs.error(e) return # 读取ID信息 with open('id_info.txt', 'r') as id_fd: try: id_note = id_fd.read() id_dict = json.loads(id_note) except Exception as e: logs.error(e) print("\033[1;31m id文件 id_info.txt存在异常,请手动修复\033[0m") return # 回收ID if len(id_dict['CAN_USE_ID']) == 0: id_list = [user_id] id_dict['CAN_USE_ID'] = id_list else: id_list = id_dict['CAN_USE_ID'] id_list.append(user_id) id_dict['CAN_USE_ID'] = id_list with open('id_info.txt', 'w') as id_fd_rw: id_fd_rw.write(json.dumps(id_dict, indent=4)) with open('user.txt', 'w+') as user_fd_rw: user_fd_rw.write(json.dumps(user_dict, indent=4)) print("\033[1;32m用户{}删除成功\033[0m".format(message))
def wrapper(worker, job): try: task_id, key, data, extra = pickleDecoder(job.data) handler(task_id, key, data, **extra) except Exception as e: basic_message = "Invalid job: %s %s" % (job.task, job.unique) logs.error(basic_message) logs.report() _warningEmail(basic_message) return ''
def user_dict(): with open('user.txt', 'r') as user_fd: user_note = user_fd.read() if user_note == '': return False, "\033[1;31m用户列表为空\033[0m" try: user_info_dict = json.loads(user_note) return True, user_info_dict except Exception as e: logs.error(e) return False, "\033[1;31m id文件 user.txt存在异常,请手动修复\033[0m"
def delete_user(user): if user in page.admins.keys(): print(" 查看所有用户 ".center(len(page.h), "*")) for i in page.users.keys(): out = "userName: {}".format(i).ljust(10) print(out.center(len(page.h))) input_info = input("请输入您需要删除的用户:") if input_info not in page.users.keys(): logs.error("该用户不存在!") return page.users.pop(input_info) logs.info("删除成功!")
def passwordReset(request, **kwargs): token = kwargs.pop('token', None) errorMsg = 'An error occurred. Please try again.' try: # Verify token is valid authUserId = stampedAuth.verifyPasswordResetToken(token) # Check if a form exists with data if request.method == 'POST': # User submitted the form data = request.POST # Verify passwords match and len > 0 if not data['password']: logs.warning("Invalid password: %s" % data['password']) raise StampedInvalidPasswordError("Invalid password") if data['password'] != data['confirm']: logs.warning("Password match failed") raise StampedInvalidPasswordError("Password match failed") # Store password stampedAuth.updatePassword(authUserId, data['password']) # Return success response = HttpResponseRedirect('/settings/password/success') else: # Display 'change password' form account = stampedAPI.getAccount(authUserId) params = { 'token': token, 'form': True, } response = render_to_response('password-reset.html', params) return response except Exception as e: logs.begin( addLog=stampedAPI._logsDB.addLog, saveLog=stampedAPI._logsDB.saveLog, saveStat=stampedAPI._statsDB.addStat, requestData=request, ) logs.request(request) logs.warning("500 Error: %s" % e) logs.error(500) logs.save() return render_to_response('password-reset.html', {'error': errorMsg})
def save_file(): fd = None try: fd = open("homework01_user_manager.txt", 'w') # 覆盖之前的存档,需要该步骤 fd.write(json.dumps(page.users)) logs.info("成功保存持久化数据:" + json.dumps(page.users)) except Exception as e: logs.error(str(e)) finally: if fd is not None: fd.close()
def get_details(self, tmdb_id, library_type='movie'): if library_type not in ('movie', 'tv'): raise Exception("Library type should be 'movie' or 'tv'") # Use cache cache = shelve.open(self.cache_file) if str(tmdb_id) in cache: try: cache_item = cache[str(tmdb_id)] except (EOFError, UnpicklingError): # Cache file error, clear cache.close() cache = shelve.open(self.cache_file, 'n') except: # Unknown cache file error, clear logs.error(u"Error in loading cache: {}".format(e)) cache.close() cache = shelve.open(self.cache_file, 'n') else: if (cache_item['cached'] + 3600 * 24) > int(time.time()): cache.close() return cache_item # Wait 10 seconds for the TMDb rate limit if self.request_count >= 40: logs.info(u"Waiting 10 seconds for the TMDb rate limit...") time.sleep(10) self.request_count = 0 params = { 'api_key': self.api_key, } if library_type == 'movie': params['append_to_response'] = 'release_dates' url = "https://api.themoviedb.org/3/movie/{tmdb_id}".format( tmdb_id=tmdb_id) else: url = "https://api.themoviedb.org/3/tv/{tmdb_id}".format( tmdb_id=tmdb_id) r = requests.get(url, params=params) self.request_count += 1 if r.status_code == 200: item = json.loads(r.text) item['cached'] = int(time.time()) cache[str(tmdb_id)] = item cache.close() return item else: return None
def passwordForgot(request): errorMsg = 'An error occurred. Please try again.' try: # Check if a form exists with data if request.method == 'POST': # User submitted the form data = request.POST # Validate email address email = str(data['forgotemail']).lower().strip() if not utils.validate_email(email): msg = "Invalid format for email address" logs.warning(msg) raise StampedInvalidEmailError("Invalid email address") # Verify account exists try: user = stampedAPI.checkAccount(email) except: ### TODO: Display appropriate error message errorMsg = 'No account information was found for that email address.' raise StampedHTTPError(404, msg="Email address not found", kind='invalid_input') # Send email stampedAuth.forgotPassword(email) # Return success response = HttpResponseRedirect('/settings/password/sent') else: # Display 'submit email' form response = render_to_response('password-forgot.html', None) return response except Exception as e: logs.begin( addLog=stampedAPI._logsDB.addLog, saveLog=stampedAPI._logsDB.saveLog, saveStat=stampedAPI._statsDB.addStat, requestData=request, ) logs.request(request) logs.warning("500 Error: %s" % e) logs.error(500) logs.save() return render_to_response('password-forgot.html', {'error': errorMsg}) return True
def export_csv(user): if user in page.admins.keys(): logs.info('正在导出csv文件...') title = ['用户名', '密码'] with open('users.csv', 'w', newline='') as users_csv: writer = csv.writer(users_csv) writer.writerow(title) for i in page.users: u = [i, page.users[i]] writer.writerow(u) logs.info('csv文件导出成功!请移步查看...') else: logs.error("非管理员不能导出用户信息")
def search_user(user): input_info = input("请输入您需要搜索的用户名:") if input_info not in page.users.keys(): logs.error("您输入的用户不存在!") return print(" 搜索用户 ".center(len(page.h), "*")) if user in page.admins.keys(): out = "userName: {:<6} password: {:<6}".format( input_info, page.users[input_info]) else: out = "userName: {:<6}".format(input_info) print(out.center(len(page.h)))
def search_user(user): input_info = input("请输入您需要搜索的用户名:") if input_info not in users.keys(): logs.error("您输入的用户不存在!") return print(" 搜索用户 ".center(len(h), "*")) if user in admins.keys(): out = "userName: {}, password: {}".format(input_info, users[input_info]).ljust(10) else: out = "userName: {}".format(input_info).ljust(10) print(out.center(len(h)))
def alertSettings(request, **kwargs): try: # Check token tokenId = request.GET.get('token', None) authUserId = stampedAuth.verifyEmailAlertToken(tokenId) # Display 'change settings' form account = stampedAPI.getAccount(authUserId) alerts = HTTPAccountAlerts().importSchema(account) user = HTTPUser().importSchema(account) image_url = user['image_url'].replace('.jpg', '-31x31.jpg') stamp_url = 'http://static.stamped.com/logos/%s-%s-credit-18x18.png' % \ (user.color_primary, user.color_secondary) settings = { 'email_alert_credit': alerts.email_alert_credit, 'email_alert_like': alerts.email_alert_like, 'email_alert_fav': alerts.email_alert_fav, 'email_alert_mention': alerts.email_alert_mention, 'email_alert_comment': alerts.email_alert_comment, 'email_alert_reply': alerts.email_alert_reply, 'email_alert_follow': alerts.email_alert_follow, } params = { 'screen_name': user.screen_name, 'name': user.name, 'image_url': image_url, 'stamp_url': stamp_url, 'action_token': tokenId, 'json_settings': json.dumps(settings, sort_keys=True) } return render_to_response('notifications.html', params) except Exception as e: logs.begin( addLog=stampedAPI._logsDB.addLog, saveLog=stampedAPI._logsDB.saveLog, saveStat=stampedAPI._statsDB.addStat, requestData=request, ) logs.request(request) logs.warning("500 Error: %s" % e) logs.error(500) logs.save() ### TODO: CHANGE URL return render_to_response('password-reset.html', {'error': 'FAIL'})
def handleStampedExceptions(e, handlers=None): if isinstance(e, StampedHTTPError): exceptions = [(StampedHTTPError, e.code, e.kind, e.msg)] elif handlers is not None: exceptions = handlers + defaultExceptions else: exceptions = defaultExceptions for (exception, code, kind, msg) in exceptions: if isinstance(e, exception): logs.warning("%s Error (%s): %s" % (code, kind, msg)) logs.warning(utils.getFormattedException()) logs.error(code) kind = kind if kind is None: kind = 'stamped_error' message = msg if message is None and e.msg is not None: message = e.msg error = {} error['error'] = kind if message is not None: error['message'] = unicode(message) return transformOutput(error, status=code) else: error = { 'error' : 'stamped_error', 'message' : "An error occurred. Please try again later.", } logs.warning("500 Error: %s" % e) logs.warning(utils.getFormattedException()) logs.error(500) # Email dev if a 500 occurs if libs.ec2_utils.is_ec2(): try: email = {} email['from'] = 'Stamped <*****@*****.**>' email['to'] = '*****@*****.**' email['subject'] = '%s - 500 Error - %s' % (stampedAPI.node_name, datetime.utcnow().isoformat()) email['body'] = logs.getHtmlFormattedLog() utils.sendEmail(email, format='html') except Exception as e: logs.warning('UNABLE TO SEND EMAIL: %s') return transformOutput(error, status=500)
def register(): input_info = input("请输入您注册的用户名/密码:") info = input_info.split("/") if len(info) != 2: logs.error("您的输入格式错误,请按照:用户名/密码") retry = input("重试(y), 回到主页面(other)") if retry == "y": return user.user.register() return if info[0] in page.users.keys(): logs.error("您注册的用户已经存在!") return page.users[info[0]] = info[1] logs.info(info[0] + "注册成功!")
def update_user(user): if user in page.admins.keys(): print(" 查看所有用户 ".center(len(page.h), "*")) for i in page.users.keys(): out = "userName: {}".format(i).ljust(10) print(out.center(len(page.h))) input_info = input("请输入您需要修改的用户名:") if input_info not in page.users.keys(): logs.error("该用户不存在!") return else: input_info = user page.users[input_info] = input("请输入新的密码:") logs.info("修改成功!")
def endless(self): while not config['ABORT']: buffer = buffer_input.get() # lint:ok likely = self.likely(buffer['msg']) for unit in likely: if config['DEBUG']: logs.info('%s: Posible comando procesado: %s' % (__name__, unit)) unit = self.command(unit) try: if unit is not None: unit['function'](buffer['self'], unit['regex']) except NameError: error = sys.exc_info() logs.error('%s: Unexpected error %s %s' % (__name__, error[0], error[1])) logs.warning('%s: No se estan procesando los comandos.' % __name__)
def _updateCache(self, offset, **kwargs): t0 = time.time() t1 = t0 prevBlockOffset = offset - self._blockSize if prevBlockOffset < 0: prevBlockOffset = None # get the modified time of the last item of the previous data cache block. We use it for the slice before = None if prevBlockOffset is not None: prevBlockKey = self._generateKey(prevBlockOffset, **kwargs) try: prevBlock = self._cache[prevBlockKey] except KeyError: # recursively fill previous blocks if they have expired prevBlock = self._updateCache(prevBlockOffset, **kwargs) except Exception as e: logs.error('Error retrieving data from memcached. Is memcached running?') prevBlock = self._updateCache(prevBlockOffset, **kwargs) if len(prevBlock) < self._blockSize: raise StampedCollectionCacheError("Previous data block was final") assert(len(prevBlock) > 0) lastItem = prevBlock[-1] before = lastItem.timestamp.modified - timedelta(microseconds=1) # Pull the data from the database and prune it. limit = self._blockSize + self._blockBufferSize data = [] while len(data) < self._blockSize: newData, final = self._getFromDB(limit, before, **kwargs) newData = self._prune(newData, **kwargs) data += newData if final: break before = newData[-1].timestamp.modified - timedelta(microseconds=1) key = self._generateKey(offset, **kwargs) data = data[:self._blockSize] try: self._cache[key] = data except Exception: logs.error('Error storing activity items to memcached. Is memcached running?') logs.debug('Time for updateCache: %s' % (time.time() - t1)) return data
def print_model(inp, model, u): inp.strip() result = inp.isdigit() if result: pass else: if inp == "exit" and 100 in model: sys.exit(0) elif inp == "exit": return "exit" logs.error("只能输入数字,请重新输入:") return inp = int(inp) if inp in model.keys(): if model[inp] == "登录": auth.auth.login() pass elif model[inp] == "注册": auth.auth.register() pass elif model[inp] == "关于": print(about_sys) elif model[inp] == "持久化数据": user.user.save_file() pass elif model[inp] == "导出用户信息csv格式": user.user.export_csv(u) pass elif model[inp] == "设置": print("功能缺失!") elif model[inp] == "退出系统": sys.exit(0) elif model[inp] == "删除用户": user.user.delete_user(u) pass elif model[inp] == "修改用户": user.user.update_user(u) pass elif model[inp] == "查询所有用户": user.user.list_user(u) pass elif model[inp] == "搜索用户": user.user.search_user(u) pass else: print("您的输入模块不存在,请重新输入:") input("输入任意字符回主页:")
def user_login(username, password): with open('system_user.txt', 'r+') as fd: note = fd.read() # 如果用户表为空,则手动赋值 if note == '': note = '{}' try: user_info_dict = json.loads(note) except Exception as e: print("\033[1;31m 系统文件 system_user.txt 存在异常,请手动修复\033[0m") logs.error(e) return False, None if username not in user_info_dict.keys( ) or password != user_info_dict[username]['password']: return False, None return True, user_info_dict[username]['role']
def alertSettingsUpdate(request, **kwargs): try: # Check token tokenId = request.POST.get('token', None) authUserId = stampedAuth.verifyEmailAlertToken(tokenId) def _checkBool(v): if v in ['True', 'true', '1', 1, True]: return True return False # Get settings alerts = { 'email_alert_credit': _checkBool(request.POST.get('email_alert_credit', False)), 'email_alert_like': _checkBool(request.POST.get('email_alert_like', False)), 'email_alert_fav': _checkBool(request.POST.get('email_alert_fav', False)), 'email_alert_mention': _checkBool(request.POST.get('email_alert_mention', False)), 'email_alert_comment': _checkBool(request.POST.get('email_alert_comment', False)), 'email_alert_reply': _checkBool(request.POST.get('email_alert_reply', False)), 'email_alert_follow': _checkBool(request.POST.get('email_alert_follow', False)), } stampedAPI.updateAlerts(authUserId, alerts) params = {} params.setdefault('content_type', 'text/javascript; charset=UTF-8') params.setdefault('mimetype', 'application/json') output_json = json.dumps(alerts, sort_keys=True) output = HttpResponse(output_json, **params) return output except Exception as e: logs.begin( addLog=stampedAPI._logsDB.addLog, saveLog=stampedAPI._logsDB.saveLog, saveStat=stampedAPI._statsDB.addStat, requestData=request, ) logs.request(request) logs.warning("500 Error: %s" % e) logs.error(500) logs.save() return HttpResponse("internal server error", status=500)
def user(user_info, role, tag=None): # 检查用户权限 if role != 'admin': print("\033[1;31mpermission denied\033[0m") return if len(user_info) != 5: print( "\033[1;31m输入有误,eg: add monkey 18 13987654321 [email protected]\033[0m") return # 检测用户输入的年龄、手机号、邮箱是否合法 check_result = check.input_args(username=user_info[1], age=user_info[2], phone=user_info[3], mail=user_info[4]) if check_result is not True: print("\033[1;31m用户\033[0m" + "\033[1;31m{}\033[0m".format(user_info[1]) + "\033[1;31m添加失败,\033[0m" + check_result) return # 检测可用id 表是否为空,如果有则取对应的值如果没有则选用MAX_ID + 1的值做id with open('id_info.txt', 'r+') as id_fd: try: id_dict = json.loads(id_fd.read()) except Exception as e: print("\033[1;31m id文件 id_info.txt存在异常,请手动修复\033[0m") logs.error(e) return if len(id_dict['CAN_USE_ID']) > 0: # 增加id字段 user_info.insert(1, id_dict['CAN_USE_ID'][0]) # 增加用户信息 user_list.append(user_info[1:]) tag = 'USED' else: user_info.insert(1, id_dict['MAX_ID']) tag = 'MAX' result = write_uer(user_info[1:]) if result[0]: write_id(id_dict, tag) print(result[1]) elif result[1] is None: pass else: print(result[1])
def sync(api, arr): if api == None: logs.critical("sync state api is nil") st = [operator] for op in arr: st.append(op) j = json.dumps(st).strip() for i in range(0, 60): try: r = requests.patch(api, data=j) if r.status_code != 200: logs.critical( "can't synchronize the migration status and wait for 1 minute to try again: %s", r.reason) else: return except requests.exceptions.ConnectionError as ce: logs.error("can't connect to tidb-operator, retry after 60s: %s", ce) time.sleep(60) logs.critical("retry 60 times after exiting")
def user(info_list, role): # 检查用户权限 if role != 'admin': print("\033[1;31mpermission denied\033[0m") return # 检查用户输入内容 # update monkey set age = 18 if len(info_list) != 6: print("\033[1;31m输入长度有误,请检查输入内容 eg: update monkey set age = 18\033[0m") return if info_list[2] != 'set' or info_list[4] != '=': print("\033[1;31m输入长度有误,请检查输入内容 eg: update monkey set age = 18\033[0m") return # 检查用户是否存在 result, message, user_id = check.user(info_list[1]) if result is False: print(message) return elif result is None: return if info_list[3] not in ['username', 'age', 'tel', 'email']: print("\033[1;31m更新字段有误,请检查\033[0m") return tag = check.user_input(tag=info_list[3], check_world=info_list[-1]) if tag: with open('user.txt', 'r') as user_fd: user_note = user_fd.read() if user_note == '': user_note = '{}' try: user_dict = json.loads(user_note) except Exception as e: print("\033[1;31m id文件 user.txt存在异常,请手动修复\033[0m") logs.error(e) return user_dict[info_list[1]][info_list[3]] = info_list[-1] with open('user.txt', 'w') as user_fd_rw: user_fd_rw.write(json.dumps(user_dict, indent=4)) print("\033[1;32m用户{} {}修改成功\033[0m".format( info_list[1], info_list[3])) else: print(tag)
def user(target): with open('user.txt', 'r') as fd: user_info = fd.read() if user_info == '': user_info = '{}' try: user_dict = json.loads(user_info) except Exception as e: print("\033[1;31m用户文件 user.txt 存在异常,请手动修复\033[0m") logs.error(e) return None, None, None if target.isdigit(): for i in user_dict.keys(): if user_dict[i]['id'] == int(target): return True, i, int(target) else: for i in user_dict.keys(): if i == target: return True, target, int(user_dict[i]['id']) return False, "\033[1;31m用户{}不存在\033[0m".format(target), None
def wrapper(worker, job): try: k = job.task logs.begin(saveLog=api._logsDB.saveLog, saveStat=api._statsDB.addStat, nodeName=api.node_name) logs.async_request(k) v = functions[k] data = pickle.loads(job.data) logs.info("%s: %s: %s" % (k, v, data)) v(k, data) except Exception as e: logs.error(str(e)) finally: try: logs.save() except Exception: print "Unable to save logs" import traceback traceback.print_exc() logs.warning(traceback.format_exc()) return ""
def wrapper(task_id, key, data, **kwargs): try: logs.begin(saveLog=api._logsDB.saveLog, saveStat=api._statsDB.addStat, nodeName=api.node_name) logs.async_request(key) logs.info("Request %s: %s: %s: %s" % (task_id, key, data, kwargs)) handler(task_id, key, data, **kwargs) logs.info("Finished with request %s" % (task_id,)) except Exception as e: logs.error("Failed request %s" % (task_id,)) logs.report() _warningEmail('%s - %s failed (%s)' % (api.node_name, key, datetime.utcnow().isoformat())) finally: logs.info('Saving request log for request %s' % (task_id,)) try: logs.save() except Exception: print 'Unable to save logs' import traceback traceback.print_exc() logs.warning(traceback.format_exc())
def addRequest(self, request, priority): global events try: now = time.time() expected_request_time = self._getExpectedRequestTime() expected_wait_time = self._getExpectedWaitTime(now, priority) expected_total_time = expected_request_time + expected_wait_time request.log.expected_wait_time = expected_wait_time request.log.expected_request_time = expected_request_time request.log.expected_dur = expected_total_time if priority == 0 and self.__requests.qsize() > 0 and \ (expected_wait_time + expected_request_time) > request.timeout: raise WaitTooLongException("'%s': Expected request time too long. Expected: %s Timeout: %s" % (self.__service_name, expected_total_time + expected_request_time, request.timeout)) if self._isBlackout(): raise TooManyFailedRequestsException("'%s': Too many failed requests. Wait time remaining: %s seconds" % (self.__service_name, self.blackout_wait - (now - self.blackout_start))) if self._isDayLimit(): raise DailyLimitException("'%s': Hit the daily request cap. Wait time remaining: %s minutes" % (self.__service_name, ((self.__day_start + 60*60*24) - now) / 60)) asyncresult = AsyncResult() if self.__curr_timeblock_start is None: self.__curr_timeblock_start = now if self.limit is None or self.period is None or self.__calls < self.limit: self.call() self.doRequest(request, asyncresult) else: self.__requests.put(((priority, request.created), request, asyncresult)) return asyncresult except Exception as e: logs.error('addRequest threw exception: %s' % e) traceback.print_exc() raise e
def invoke(request, *args, **kwargs): """ wrapper to invoke a stamped api function in an asynchronous context which adds logging and exception handling. """ taskId = kwargs.pop('taskId', None) try: stampedAPI = getStampedAPI() func = "%sAsync" % utils.getFuncName(1) if not request.is_eager: logs.begin( saveLog=stampedAPI._logsDB.saveLog, saveStat=stampedAPI._statsDB.addStat, nodeName=stampedAPI.node_name, ) logs.async_request(func, *args, **kwargs) logs.info("%s %s %s (is_eager=%s, hostname=%s, task_id=%s)" % (func, args, kwargs, request.is_eager, request.hostname, request.id)) getattr(stampedAPI, func)(*args, **kwargs) except Exception as e: logs.error(str(e)) raise finally: try: if taskId is not None: stampedAPI._asyncTasksDB.removeTask(taskId) if not request.is_eager: logs.save() except: pass
def _fail(self, exception): try: del localData.rateLimiter except: pass self._getHost() if self.__blackout_start is not None: return self.__fails.append(self.FailLog(exception)) now = time.time() cutoff = now - self.__fail_period count = 0 while len(self.__fails) > 0: if self.__fails[0].timestamp < cutoff: self.__fails.popleft() else: break count = len(self.__fails) if count >= self.__fail_limit: print('### RPC server fail threshold reached') self.__blackout_start = time.time() #self.__local_rlservice.loadDbLog() # update the local call log from the db logs.error('RPC server request FAIL THRESHOLD REACHED') # Email dev if a fail limit was reached if self.__is_ec2: if self.__last_email_time is not None and (time.time() - self.__last_email_time) > EMAIL_WAIT: self.sendFailLogEmail() self.__last_email_time = time.time()
def load_file(): fd = None try: logs.info('自动加载缓存文件') fd = open("homework01_user_manager.txt", 'r') data = fd.read() logs.info('成功加载数据:' + data) return json.loads(data) except FileNotFoundError: logs.error("存档文件不存在...开始进行默认初始化操作") return {} except JSONDecodeError: logs.error("存档文件格式错误...开始进行默认初始化操作") return {} except Exception as e: logs.error(str(e)) finally: if fd is not None: fd.close()
def load_user(info_list, role): load_tag = True # 检查用户权限 if role != 'admin': print("\033[1;31mpermission denied\033[0m") return # 检查用户输入参数 if len(info_list) != 2: print("\033[1;31m用户输入参数有误 eg: load filename\033[0m") return try: reader = csv.reader(open(info_list[1])) except FileNotFoundError as e: print("\033[1;31m文件不存在\033[0m") logs.error(e) return except IsADirectoryError as e: print("\033[1;31m输入有误,请输入文件\033[0m") logs.error(e) return try: for user_id, name, age, tel, email in reader: if load_tag: load_tag = False continue tag1, _, _ = check.user(name) if tag1: print("\033[1;31m {} 导入失败, id or username已存在\033[0m".format(name)) else: user_list = [user_id, name, age, tel, email] add.user(user_list, role) except ValueError as e: print("\033[1;31m文件load失败,请检查文件内容\033[0m") logs.error(e) return
def ping(self): self.update() if self.options.stack: mich = None else: mich = self._info.instance.instance_id for node in self._info.nodes: node_status = 1 # don't try to monitor myself if node.instance_id == mich: continue try: utils.logRaw("pinging node '%s.%s'..." % (node.stack, node.name), True) if "apiServer" in node.roles: self._try_ping_apiServer(node) if "webServer" in node.roles: self._try_ping_webServer(node) if "db" in node.roles: self._try_ping_db(node) utils.logRaw("success!\n") except Exception, e: node_status = -1 unexpected = False detail = None if isinstance(e, MonitorException): logs.error("monitor error: %s" % e) utils.log("monitor error: %s" % e) detail = e.detail else: logs.error("unexpected error: %s" % e) utils.log("unexpected error: %s" % e) unexpected = True # only send a notification if this node's status has changed since # the last time we checked, so we don't get duplicate notifications # related to the same incident. try: notify = -1 != self.status[node.instance_id] except KeyError: notify = False if notify and not self.options.noop: subject = "%s.%s error" % (node.stack, node.name) message = str(e) if unexpected or e.email: if detail is not None: message = "%s\n\n--- detail ---\n\n%s" % (message, detail) self.handler.email(subject, message) if unexpected or e.sms: self.handler.sms(subject, message) self.status[node.instance_id] = node_status
async def render_latex(job_id, output_format, code, density, quality): try: pdir = './temp/' + job_id + '/' mkdir(pdir) fname = pdir + 'a.tex' latex_log = '' with open(fname, 'wt') as f: f.write(code) f.flush() f.close() try: try: output = await run_command_async( COMMAND_LATEX.format(pdir = pdir, fname = fname), timeout = 20 ) finally: log_file = fname.replace('.tex', '.log') try: latex_log = open(log_file, encoding = 'utf-8').read() except FileNotFoundError: pass except subprocess.TimeoutExpired as e: return { 'status': 'error', 'description': 'Time limit exceeded during latex rendering' } except subprocess.CalledProcessError as e: # NOTE: Sometimes a pdf file can still be produced. # Maybe we should let the caller access it anyway? return { 'status': 'error', 'description': 'pdflatex exited with non-zero return code', 'log': latex_log } pdf_file = fname.replace('.tex', '.pdf') if output_format == 'pdf': try: # Binary so as to avoid encoding errors with open(pdf_file, 'rb') as f: pass except FileNotFoundError: return { 'status': 'error', 'description': 'pdflatex produced no output', 'log': latex_log } return { 'status': 'success', # 'filename': pdf_file, 'log': latex_log } elif output_format in ('png', 'jpg'): img_file = pdf_file.replace('.pdf', '.' + output_format) try: output = await run_command_async( COMMAND_IMG_CONVERT.format( density = density, quality = quality, pdf = pdf_file, dest = img_file ), timeout = 3 ) # If there are multiple pages, need to make sure we get the first one # A later version of the API will allow for accessing the rest of the # pages. This is more of a temporary bug fix than anything. multipaged = img_file.replace('.', '-0.') if os.path.isfile(multipaged): os.rename(multipaged, img_file) except subprocess.TimeoutExpired: return { 'status': 'error', 'description': 'Time limit exceeded during image conversion', } except subprocess.CalledProcessError as proc: return { 'status': 'error', 'description': 'Image conversion exited with non-zero return code' } return { 'status': 'success', # 'filename': img_file, 'log': latex_log } return { 'status': 'error', 'description': 'Output format was invalid' } except Exception as e: logs.error(e) return { 'status': 'error', 'description': 'The server broke. This is bad.' # 'details': repr(e) }
find_list.find_user(info_list) elif action == "display": logs.info(username, info_list) find_list.pagesize(info_list) elif action == "save": logs.info(username, info_list) save_load.save_user(info_list) elif action == "load": logs.info(username, info_list) save_load.load_user(info_list, role) elif action == "login_out": logs.info(username, info_list) # 切换账号重置登录失败次数 INIT_FAIL_CNT = 0 break elif action == "exit": sys.exit(0) else: print("\033[1;31minvalid action.\033[0m") except KeyboardInterrupt as e: logs.error(e) print("\033[1;31m兄弟按Ctrl + C是不对的,真想退出请用exit\033[0m") else: # 带颜色 print("\033[1;31musername or password error.\033[0m") logs.info(username, ['登录失败']) INIT_FAIL_CNT += 1 print("\033[1;31m\nInput {} failed, Terminal will exit.\033[0m".format( MAX_FAIL_CNT))
def _remove_old_items_from_library(self, unmatched_items): logs.info(u"Removing symlinks for items " "which no longer qualify ".format( library=self.recipe['new_library']['name'])) count = 0 updated_paths = [] deleted_items = [] max_date = add_years((self.recipe['new_library']['max_age'] or 0) * -1) if self.library_type == 'movie': for movie in unmatched_items: if not self.recipe['new_library']['remove_from_library']: # Only remove older than max_age if not self.recipe['new_library']['max_age'] \ or (movie.originallyAvailableAt and max_date < movie.originallyAvailableAt): continue for part in movie.iterParts(): old_path_file = part.file old_path, file_name = os.path.split(old_path_file) folder_name = os.path.relpath( old_path, self.recipe['new_library']['folder']) if folder_name == '.': new_path = os.path.join( self.recipe['new_library']['folder'], file_name) dir = False else: new_path = os.path.join( self.recipe['new_library']['folder'], folder_name) dir = True if (dir and os.path.exists(new_path)) or ( not dir and os.path.isfile(new_path)): try: if os.name == 'nt': # Python 3.2+ only if sys.version_info < (3, 2): assert os.path.islink(new_path) if dir: os.rmdir(new_path) else: os.remove(new_path) else: assert os.path.islink(new_path) os.unlink(new_path) count += 1 deleted_items.append(movie) updated_paths.append(new_path) except Exception as e: logs.error(u"Remove symlink failed for " "{path}: {e}".format(path=new_path, e=e)) else: for tv_show in unmatched_items: done = False if done: continue for episode in tv_show.episodes(): if done: break for part in episode.iterParts(): if done: break old_path_file = part.file old_path, file_name = os.path.split(old_path_file) folder_name = '' new_library_folder = \ self.recipe['new_library']['folder'] old_path = os.path.join( new_library_folder, old_path.replace(new_library_folder, '').strip( os.sep).split(os.sep)[0]) folder_name = os.path.relpath(old_path, new_library_folder) new_path = os.path.join( self.recipe['new_library']['folder'], folder_name) if os.path.exists(new_path): try: if os.name == 'nt': # Python 3.2+ only if sys.version_info < (3, 2): assert os.path.islink(new_path) os.rmdir(new_path) else: assert os.path.islink(new_path) os.unlink(new_path) count += 1 deleted_items.append(tv_show) updated_paths.append(new_path) done = True break except Exception as e: logs.error(u"Remove symlink failed for " "{path}: {e}".format(path=new_path, e=e)) else: done = True break logs.info(u"Removed symlinks for {count} items.".format(count=count)) for item in deleted_items: logs.info(u"{title} ({year})".format(title=item.title, year=item.year))
def _create_symbolic_links(self, matching_items, matching_total): logs.info(u"Creating symlinks for {count} matching items in the " u"library...".format(count=matching_total)) try: if not os.path.exists(self.recipe['new_library']['folder']): os.mkdir(self.recipe['new_library']['folder']) except: logs.error(u"Unable to create the new library folder " u"'{folder}'.".format( folder=self.recipe['new_library']['folder'])) logs.info(u"Exiting script.") return 0 count = 0 updated_paths = [] new_items = [] if self.library_type == 'movie': for movie in matching_items: for part in movie.iterParts(): old_path_file = part.file old_path, file_name = os.path.split(old_path_file) folder_name = '' for library_config in self.source_library_config: for f in self.plex.get_library_paths( library_name=library_config['name']): f = os.path.abspath(f) if old_path.lower().startswith(f.lower()): folder_name = os.path.relpath(old_path, f) break else: continue if folder_name == '.': new_path = os.path.join( self.recipe['new_library']['folder'], file_name) dir = False else: new_path = os.path.join( self.recipe['new_library']['folder'], folder_name) dir = True parent_path = os.path.dirname( os.path.abspath(new_path)) if not os.path.exists(parent_path): try: os.makedirs(parent_path) except OSError as e: if e.errno == errno.EEXIST \ and os.path.isdir(parent_path): pass else: raise # Clean up old, empty directories if os.path.exists(new_path) \ and not os.listdir(new_path): os.rmdir(new_path) if (dir and not os.path.exists(new_path)) \ or not dir and not os.path.isfile(new_path): try: if os.name == 'nt': if dir: subprocess.call([ 'mklink', '/D', new_path, old_path ], shell=True) else: subprocess.call([ 'mklink', new_path, old_path_file ], shell=True) else: if dir: os.symlink(old_path, new_path) else: os.symlink(old_path_file, new_path) count += 1 new_items.append(movie) updated_paths.append(new_path) except Exception as e: logs.error( u"Symlink failed for {path}: {e}".format( path=new_path, e=e)) else: for tv_show in matching_items: done = False if done: continue for episode in tv_show.episodes(): if done: break for part in episode.iterParts(): old_path_file = part.file old_path, file_name = os.path.split(old_path_file) folder_name = '' for library_config in self.source_library_config: for f in self.plex.get_library_paths( library_name=library_config['name']): if old_path.lower().startswith(f.lower()): old_path = os.path.join( f, old_path.replace(f, '').strip( os.sep).split(os.sep)[0]) folder_name = os.path.relpath(old_path, f) break else: continue new_path = os.path.join( self.recipe['new_library']['folder'], folder_name) if not os.path.exists(new_path): try: if os.name == 'nt': subprocess.call([ 'mklink', '/D', new_path, old_path ], shell=True) else: os.symlink(old_path, new_path) count += 1 new_items.append(tv_show) updated_paths.append(new_path) done = True break except Exception as e: logs.error( u"Symlink failed for {path}: {e}". format(path=new_path, e=e)) else: done = True break logs.info( u"Created symlinks for {count} new items:".format(count=count)) for item in new_items: logs.info(u"{title} ({year})".format(title=item.title, year=getattr( item, 'year', None)))
#return response except StampedAuthError as e: logs.warning("401 Error: %s" % (e.msg)) logs.warning(utils.getFormattedException()) response = HttpResponse(e.msg, status=401) logs.auth(e.msg) return response except StampedInputError as e: logs.warning("400 Error: %s" % (e.msg)) logs.warning(utils.getFormattedException()) response = HttpResponse("invalid_request", status=400) logs.error(response.status_code) return response except StampedIllegalActionError as e: logs.warning("403 Error: %s" % (e.msg)) logs.warning(utils.getFormattedException()) response = HttpResponse("illegal_action", status=403) logs.error(response.status_code) return response except StampedPermissionsError as e: logs.warning("403 Error: %s" % (e.msg)) logs.warning(utils.getFormattedException()) response = HttpResponse("insufficient_privileges", status=403)