def save_json(path, data): """ given a path, and structure, save as JSON string """ jsonstr = "" try: jsonstr = json.dumps(data) except TypeError: L.error("Error: data supplied can't be converted to JSON") return False dirname = os.path.dirname(path) if not dirname: name = os.path.basename(path) thisdir = os.path.dirname(os.path.realpath(__file__)) path = os.path.join(thisdir, name + '.json') filep = None try: filep = open(path, "w") except IOError: L.error("Error: can't open " + path + " for writing") return False filep.write(jsonstr) filep.write("\n") filep.close() return True
def process_message(self, user, msg): """ process a request from a client-side webtail component """ reply_packet = {"status": ""} packet = {} try: packet = json.loads(msg) except Exception as ex: self.log_exeption(ex, "loading client request " + msg) reply_packet["status"] = "error" reply_packet["msg"] = "invalid request" if reply_packet["status"] == "error": return json.dumps(reply_packet) if "request" not in packet: reply_packet["status"] = "error" reply_packet["msg"] = "invalid request" L.error("webtail: no request in packet") return json.dumps(reply_packet) req = packet["request"] if req not in self.webtail_funcs: L.error("webtail: no handler for request") reply_packet["status"] = "error" reply_packet["msg"] = "invalid request" return json.dumps(reply_packet) reply_packet = self.webtail_funcs[req](user, packet) if "ctx" in packet: reply_packet["ctx"] = packet["ctx"] reply_packet["reply-to"] = req return json.dumps(reply_packet)
def load_json(path): """ given a path, return a structure with the contained JSON """ dirname = os.path.dirname(path) if not dirname: name = os.path.basename(path) thisdir = os.path.dirname(os.path.realpath(__file__)) path = os.path.join(thisdir, name + '.json') filep = None try: filep = open(path) except IOError: L.warning("Can't open " + path + " for reading") return False data = {} filestr = filep.read() try: data = json.loads(filestr) except ValueError: L.error("Error: packet not valid json:" + filestr) filep.close() return False filep.close() return data
def update_files_to_monitor(self, user, newclient): """ read user file monitor prefs and update monitoring paths if required """ if user not in self.config: self.config[user] = {} user_config = self.config[user] if "monitored_files" not in user_config: user_config["monitored_files"] = {} if "regexes" not in user_config: user_config["regexes"] = [] user_watched_files = user_config["monitored_files"] # L.info("==============") # L.info(user_watched_files) resave_config = False for filepath in user_watched_files: if filepath in self.path_filter and newclient: L.info(filepath + ": file already being monitored by another client") self.path_filter[filepath] += 1 # increase reference count continue if not self.get_fp(filepath): L.error("Bad file path for:" + filepath) user_watched_files[filepath]['follow'] = 0 resave_config = True continue # First subscription to monitor this file self.path_filter[filepath] = 1 if resave_config: # Some bad file paths encountered and were marked. Resave users configurations # with the marked bad paths so that they can be displayed on the client side self.save_config()
def post(self): spacket = self.get_argument("packet") packet = {} try: packet = json.loads(spacket) except ValueError: L.error("Error: packet not valid json:" + spacket) if "request" not in packet: L.error("Error: Received packet without request:" + spacket) return user = self.get_current_user() reply = RH.ajax_request(user, packet) # convert the reply to JSON and then to bytes response = str.encode(json.dumps(reply)) self.set_header("Content-type", 'text/plain') self.set_header("Content-Encoding", 'gzip') t_1 = time.time() gzip_compress = zlib.compressobj(9, zlib.DEFLATED, zlib.MAX_WBITS | 16) content = gzip_compress.compress(response) + gzip_compress.flush() L.info("compression time:%f" % (time.time() - t_1)) compressed_content_length = len(content) self.set_header("Content-Length", compressed_content_length) self.write(content)
def websock_new_connection(self, user, client, service): """ Notify handler of new client """ if not service in self.websock_handlers: L.error("Error: service:" + service + " not found for new connection") return False return self.websock_handlers[service]['new_client'](user, client)
def register_ajax_handler(self, request, function): """ component registers its ajax requests handlers via this method""" if request in self.ajax_handlers: L.error("Error: request:" + request + " is already registered") return False self.ajax_handlers[request] = function L.info("registered:" + request) return True
def websock_close_connection(self, client): """ Notify handler of client closing a connection """ service = client.service if not service in self.websock_handlers: L.error("Error: service:" + service + " not found for close connection") return self.websock_handlers[service]['close_client'](client)
def new_message(self, user, client, message): """ invoked whenever a client sends a new message to service webtail""" L.info("received new message:" + message) if client not in self.listeners: L.error("received message from unregiatered client") return reply_str = self.process_message(user, message) client.write_message(reply_str)
def start(self, reqhandler, logger, timer): if self.started: L.error("WebTail plugin already started") return False webtail = WebTail() self.tail_timer = timer(webtail.follow, 1000) self.tail_timer.start() return True
def unfollow_file(self, fullpath): """ remove file from list of tracked files """ if fullpath not in self.path_filter: return if fullpath not in self.tailed_file_ptrs: L.error("can't unfollow " + fullpath + " as it is not followed.") return self.tailed_file_ptrs[fullpath].close() del self.tailed_file_ptrs[fullpath]
def ajax_request(self, user, packet): """ invoke the appropriate method of the designated request handler """ request = packet["request"] if not request in self.ajax_handlers: L.error("Error: request " + request + " does not have a handler") return {"status": "error", "msg": "no handler for " + request} reply = self.ajax_handlers[request](user, packet) reply['reply-to'] = request if 'ctx' in packet: # if context received with request, return it unchanged reply['ctx'] = packet['ctx'] return reply
def register_websock_handlers(self, service, new_client, new_message, close_client): """ component registers its websocket requests handlers via this method The new_client method should return False if there is a problem servicing a new client, otherwise True """ if service in self.websock_handlers: L.error("Error: service:" + service + " is already registered") return False handlers = { "new_client": new_client, "new_message": new_message, "close_client": close_client } self.websock_handlers[service] = handlers return True
def gen_hash(args): mode = "" if len(args) > 0: parser = Parser(prog=utils.abs_path('./android_build.py')) parser.add_argument("-m", "--mode", default=mode, help="gen_hash() failed! |help: -m release|debug") arguments = parser.parse_args(args) mode = arguments.mode if mode == "debug": return _gen_hash_debug() if mode == "release": return _gen_hash_release() L.error("gen_hash() failed! |help: -m release|debug")
def run(port=8000, address='', path="./"): utils.register_exit() L.error("p %s a %s p %s", port, address, path) if num_version == 2: L.debug(utils.abs_path(path)) os.chdir(path) handler = SimpleHTTPServer.SimpleHTTPRequestHandler httpd = SocketServer.TCPServer((address, port), handler) httpd.serve_forever() pass else: L.debug(utils.abs_path(path)) os.chdir(path) http.server.test(HandlerClass=SimpleHTTPRequestHandler, port=port, bind=address)
def _gen_hash_release(): store_file = gv.build_android_store_file() if os.path.isfile(store_file): alias_name = gv.build_android_alias_name() alias_pass = gv.build_android_alias_pass() store_pass = gv.build_android_store_pass() cmd = "keytool -exportcert " \ "-alias {0} " \ "-keystore {1} " \ "-keypass {2} " \ "-storepass {3} " \ "| openssl sha1 -binary | openssl base64" cmd = cmd.format(alias_name, store_file, alias_pass, store_pass) print(cmd) os.system(cmd) else: L.error("miss file keystore:" + store_file)
def register_dashboard(self, function): """ add a dashboard function for a monitoring component """ plugin_file = None frame = inspect.stack()[1] if hasattr(frame, 'filename'): # Changed from Python 3.5 plugin_file = os.path.basename(os.path.normpath(frame.filename)) else: plugin_file = os.path.basename(os.path.normpath(frame[1])) plugin_name = plugin_file.split(".")[0] if plugin_name in self.dashboard_handlers: L.error("Error:" + plugin_name + " is already in dashboard") return False self.dashboard_handlers[plugin_name] = function return True
def main(args): gen = "" if len(args) > 1: parser = Parser(prog=utils.abs_path('./android_build.py')) parser.add_argument("-g", "--gen", default=gen, help="gen() failed! |help: -g keystore|hash") arguments = [args.pop(0), args.pop(0)] arguments = parser.parse_args(arguments) gen = arguments.gen if gen == "keystore": gen_key(args) elif gen == "hash": gen_hash(args) else: L.error("failed! |help: android-gen -g keystore|hash")
def gen_manifest(num_version, client_path, package_url, dest_path, dest_project_manifest, folder_will_gen): """ :param client_path: :param folder_will_gen: :param num_version: :param package_url: :param dest_path: :param dest_project_manifest: :return: """ json_manifest = { packageUrl: package_url, remoteManifestUrl: join_path(package_url, project_manifest_name), remoteVersionUrl: join_path(package_url, version_manifest_name), version: num_version } L.error(utils.abs_path(dest_path)) # json_assets = gen_folders({}, client_path, '') json_assets = {} for folder in folder_will_gen: json_assets = gen_folders(json_assets, join_path(client_path, folder), folder) copy_tree(join_path(client_path, folder), join_path(dest_path, folder)) #project.json file_name = "project.json" json_assets = gen_files(json_assets, '', file_name, join_path(client_path, file_name)) copy_file(join_path(client_path, file_name), join_path(dest_path, file_name)) #main.js file_name = "main.js" json_assets = gen_files(json_assets, '', file_name, join_path(client_path, file_name)) copy_file(join_path(client_path, file_name), join_path(dest_path, file_name)) save(json_manifest, json_assets, dest_path, dest_project_manifest) # L.debug(json.dumps(json_assets, indent=4)) pass
def copytree_process(src, dst, process=None, symlinks=False): names = os.listdir(src) if os.path.isdir(dst): shutil.rmtree(dst) os.makedirs(dst) errors = [] for name in names: srcname = os.path.join(src, name) dstname = os.path.join(dst, name) try: if symlinks and os.path.islink(srcname): linkto = os.readlink(srcname) os.symlink(linkto, dstname) elif os.path.isdir(srcname): copytree_process(srcname, dstname, process, symlinks) else: shutil.copy2(srcname, dstname) if process is not None: process(dstname) except (IOError, os.error) as why: errors.append((srcname, dstname, str(why))) # catch the Error from the recursive copytree so that we can # continue with other files except Error as err: errors.extend(err.args[0]) try: shutil.copystat(src, dst) except WindowsError: # can't copy file access times on Windows pass except OSError as why: errors.extend((src, dst, str(why))) for err in errors: L.error(err) return len(errors) == 0
def on_msg_websock(self, client, user, message): """ new message from existing websock """ L.info("websock message:" + message) if not hasattr(client, "service"): # first time we hear from this connection (i.e service request) packet = {} try: packet = json.loads(message) except ValueError: errstr = "websock initial packet not valid json" L.error(errstr) reply = {"status": "error", "msg": errstr} client.write_message(json.dumps(reply)) return if 'service' not in packet: errstr = "no service specification in request" reply = {"status": "error", "msg": errstr} client.write_message(json.dumps(reply)) return service = packet['service'] if not RH.websock_new_connection(user, client, service): msg = "service " + service + " unavailable" L.error(msg) reply = {"status": "error", "msg": msg} client.write_message(json.dumps(reply)) return reply = { "status": "ok", "msg": "Service accepted", "service": service } client.write_message(json.dumps(reply)) client.service = service return RH.websock_message(user, client, message)
def log_exeption(self, ex, msg): """ generic exception logging """ L.error(type(ex).__name__) L.error(msg)