def write_logs( logs, app_name, filepath=None, root=None, ): # filepath: logs for which file, root: under which root if isinstance(logs, string_types): logs = [logs] if not logs: return # ignore if not root: return # ignore now = datetime.datetime.now() now_str = now.strftime('%Y-%m-%d %H:%M:%S') log_path = make_sure_sync_log_path(root, app_name) with open(log_path, 'a') as f: for log in logs: log = raw_log = smart_str(log) if filepath: filepath = smart_str(filepath) log = '%s: %s %s\n' % (now_str, filepath, log) else: log = '%s %s\n' % (now_str, smart_str(log)) write_file(f, log)
def store_nginx_server_cert(ssl_key, ssl_cert): if not ssl_key or not ssl_cert: return old_ssl_key = read_file(app_nginx_server_ssl_key_filepath) old_ssl_cert = read_file(app_nginx_server_ssl_cert_filepath) if old_ssl_key != ssl_key or old_ssl_cert != ssl_cert: make_sure_path(app_nginx_server_ssl_cert_filepath, is_file=True) write_file(app_nginx_server_ssl_key_filepath, ssl_key) write_file(app_nginx_server_ssl_cert_filepath, ssl_cert) # reload nginx c_f = os.popen("/usr/nginx/sbin/nginx -s reload") try: c_f.read() except: pass
def get_server_status_bucket_configs(): global server_status_bucket_configs if not server_status_bucket_configs: config_filepath = os.path.join(config_folder, 'server_status.json') if not os.path.isfile(config_filepath): private_key, public_key = create_private_public_keys() bucket = get_bucket_by_public_key(public_key) configs = dict(bucket=bucket, private_key=private_key, public_key=public_key, created_at=time.time()) write_file(config_filepath, json_dumps(configs, indent=4)) else: configs = load_json_file(config_filepath) server_status_bucket_configs = configs return server_status_bucket_configs
def accept_upload_file_from_client(self, bucket, record_data, get_raw_content_func=None): # return ok, failed, existed if not self.should_upload_file_by_client(bucket, record_data): return "failed" filepath = self.get_filepath_from_record(bucket=bucket, record_data=record_data) if not filepath: return "failed" if not get_raw_content_func: return "failed" if not os.path.isfile(filepath): if hasattr(get_raw_content_func, "__call__"): raw_content = get_raw_content_func() else: raw_content = get_raw_content_func if not raw_content or not isinstance(raw_content, string_types): return "failed" if len(raw_content) > MAX_FILE_SIZE: return "failed" if DEBUG: relative_path = record_data.get("path") or "" print("store %s to local" % relative_path) write_file(filepath, raw_content) file_size = len(raw_content) image_info = None if guess_type(filepath).startswith("image/"): image_info = get_image_info_from_raw_content(raw_content) self.update_record_when_file_stored(bucket, record_data, file_size=file_size, image_info=image_info) # update the record return "ok" else: try: file_size = os.path.getsize(filepath) or 0 except: file_size = 0 self.update_record_when_file_stored(bucket, record_data, file_size=file_size) # try to update the record return "existed"
def store_files_info(self): files_info_content = json_dumps(self.files_info, indent=4) write_file(self.files_info_filepath, files_info_content)
if sentry_dsn: sentry_client = Client(sentry_dsn) else: sentry_client = None server_secret_key = get_env('SERVER_SECRET_KEY') if not server_secret_key: server_secret_key_filepath = "/tmp/farbox_server_secret_key" try: server_secret_key = read_file(server_secret_key_filepath) except: server_secret_key = "" if not server_secret_key: server_secret_key = uuid.uuid1().hex try: write_file(server_secret_key_filepath, server_secret_key) except: pass signer = ServerSerializer(server_secret_key) # aws ses mail sender SES_ID = get_env('SES_ID') SES_KEY = get_env('SES_KEY') SES_SENDER = get_env('SES_SENDER') or 'No-Reply<*****@*****.**>' CAN_SEND_SYSTEM_EMAIL = bool(SES_ID and SES_KEY) # todo 这里应该允许二次定制的时候,进行自定义的逻辑 BASIC_FORM_FORMATS = {}
def build_api_templates(): info = get_templates_info() info_string = '# coding: utf8\napi_templates = %s' % json.dumps(info, indent=4, ensure_ascii=False) info_filepath = templates_folder = os.path.join(os.path.dirname(os.path.abspath(__file__)), 'info.py') write_file(info_filepath, info_string)