def main(): config = args.parse() say_hello() print('cli arguments as understood:\n') pprint(config, indent=2) print('\n\n') # dry run if config['dry'] is True: dry_run(config['dry_opts']) # real run elif len(config['watchers']): YOUTUBE_FILENAME_PATTERN = "(.*\\/)?([a-zA-Z0-9\\_\\-]{10,})\\.(mp3|m4a)$" watcher = Watcher() for watcher_config in config['watchers']: rc_json = get_configuration_from_file(watcher_config['rc_filename']) on_event = ConditionalFileEventHandler( both(is_moved_event, partial(filename_matches, YOUTUBE_FILENAME_PATTERN)), partial(on_youtube_music_file, rc_json=rc_json, watcher_config=watcher_config) ) watcher.schedule(on_event, watcher_config['watch'], recursive=config['recursive']) watcher.start() try: while True: time.sleep(1) except KeyboardInterrupt: if watcher is not None: watcher.stop() # inconsistent state else: raise ValueError('ValueError: Program should be run with either the --dry flag or with some --watch and --out values')
def main(): parser = argparse.ArgumentParser() parser.add_argument('--address', default=krpc.DEFAULT_ADDRESS) parser.add_argument('--rpc-port', type=int, default=krpc.DEFAULT_RPC_PORT) parser.add_argument('--stream-port', type=int, default=krpc.DEFAULT_STREAM_PORT) parser.add_argument('--client-name', default='Program') parser.add_argument('--debug', action='store_true') parser.add_argument('--quicksave', action='store_true') parser.add_argument('--quickload', action='store_true') parser.add_argument('--no-autodecouple', action='store_true') parser.add_argument('program') args = parser.parse_args() if args.debug: log_level = logging.DEBUG else: log_level = logging.INFO logging.basicConfig(level=log_level) conn = krpc.connect(name=args.client_name, address=args.address, rpc_port=args.rpc_port, stream_port=args.stream_port) log.info('Connected to %s', args.address) if args.quickload: log.info('Loading quicksave as requested') conn.space_center.quickload() vessel = conn.space_center.active_vessel watcher_thread = Watcher(conn, vessel) watcher_thread.start() program = eval('[{}]'.format(args.program), {}, ProgramLocals(conn, vessel, watcher_thread)) program = [init_stage(stage) for stage in program] log.info('Program: %s', program) for stage in program: log.info('Executing stage %s', stage) watcher_thread.autodecouple = stage.autodecouple and not args.no_autodecouple stage() if args.quicksave: log.info('Quicksaving as requested') conn.space_center.quicksave() log.info('Program complete')
class Server(SmartServer): watcher = roproperty("_watcher") web_server = roproperty("_web_server") def prepare(self): self._watcher = Watcher() self._watcher.start() self._web_server = WebServer() self._web_server.start() def work(self): profiler.autosave()
def main(): prs = argparse.ArgumentParser(description='Generate the website anew.') prs.add_argument('-c', '--conf', type=str, default="example_configuration.yml", dest='conf_path', help='Path to the configuration file.') args = prs.parse_args() conf = yaml.load(open(args.conf_path, 'r').read()) task_manager = pst.Tasker(conf) watcher = Watcher(task_manager) watcher.start() try: while True: time.sleep(1) except KeyboardInterrupt: watcher.stop() watcher.join()
def main(): # Setup Logging logger = logging.getLogger('twitterbox') hdlr = logging.StreamHandler(sys.stdout) formatter = logging.Formatter('%(asctime)s %(module)s %(message)s') hdlr.setFormatter(formatter) logger.addHandler(hdlr) logger.setLevel(logging.DEBUG) queue = Queue.PriorityQueue() watcher = None loops = 0 while True: try: loops = loops + 1 logger.debug("Main Loop " + str(loops)) # Make sure our twitter thread is alive and happy if not watcher or not watcher.is_alive(): logger.info("Starting watcher thread") watcher = Watcher(queue, logger) watcher.setDaemon(True) watcher.start() # Dump the queue for m in range(queue.qsize()): msg = queue.get() priority = msg[0] line1 = msg[1] line2 = msg[2] alert = msg[3] logger.info(line1 + " " + line2) queue.task_done() except Exception as e: logger.error("Exception in main thread: " + str(e)) traceback.print_tb(sys.exc_info()[2]) time.sleep(15) logger.warn("Exiting main thread")
class Scanner: def __init__(self, path): self.path = path self.queue = Queue.PriorityQueue() self.watcher = Watcher(path, self.queue) self.walker = Walker(path, self.queue, Settings.is_rescan_forced()) self.reader = Reader(self.queue) self.validator = Validator(self.queue) def start(self): self.validator.start() self.watcher.start() self.walker.start() self.reader.start() def stop(self): self.watcher.stop() self.reader.stop() self.validator.join() self.walker.join() self.watcher.join() self.reader.join()
class ClientDaemon: def __init__(self): self.init_home_dir() self.api = "https://andrefreitas.pt/budibox/api/" self.login = LoginBox() self.watcher = Watcher(self.budibox_home) def init_home_dir(self): # Searchs for user home folder and creates budibox folder self.budibox_home = expanduser("~") + "/budibox" self.home = expanduser("~") self.budibox_home = self.budibox_home.decode(system_enconding) # Creates budibox folder if (not os.path.exists(self.budibox_home)): os.makedirs(self.budibox_home) def client_start(self): self.login.start() self.computerId = self.get_computer_id() watcher_thread = Thread(target=self.listen_watcher, args=()) requests_thread = Thread(target=self.listen_requests, args=()) keep_alive = Thread(target=self.keep_alive, args=()) sync = Thread(target=self.sync, args=()) # Start threads watcher_thread.start() requests_thread.start() keep_alive.start() sync.start() # Join threads watcher_thread.join() requests_thread.join() keep_alive.join() sync.join() def get_computer_id(self): url = self.api + 'computers/getComputerId.php' values = { 'apikey': '12', 'user': login_box.client.get_email(), 'computer': get_computer_name() } response = json_request(url, values) print_message("Getting the computer ID") if (response['result'] == 'ok'): return response['computerId'] def sync(self): while True: url = self.api + 'files/getUserFiles.php' values = {'apikey': '12', 'user': login_box.client.get_email()} response = json_request(url, values) if (response['result'] != 'ok'): print_message("Error trying to get user files of " + login_box.client.get_email()) else: print_message("Total files: " + str(len(response['files']))) self.restore_file(response['files']) time.sleep(sync_files_interval) def restore_file(self, files): list_dir = os.listdir(self.budibox_home) global restore_requests for file in files: file_path = file['path'] if os.path.isfile(self.budibox_home + file_path): if (file['status'] == 'deleted'): os.remove(self.budibox_home + file_path) break timestamp = int(file['date_modified']['sec']) datetime_request = datetime.fromtimestamp(timestamp) datetime_local_file = datetime.fromtimestamp( os.path.getmtime(self.budibox_home + file_path)) difference_times = time.mktime( datetime_request.timetuple()) - time.mktime( datetime_local_file.timetuple()) - 3600 if (difference_times > 0): if not (file_path in restore_requests): print_message("More recent " + file_path) url = self.api + 'files/restoreFile.php' values = { 'apikey': '12', 'computerId': self.computerId, 'modification': file['modification'] } response = json_request(url, values) if (response['result'] == 'ok'): print_message("Sent request of restore file of " + file_path) restore_requests[file_path] = False else: print_message( "Error sending request of restore file of " + file_path) else: print "older or equal" else: if not (file_path in restore_requests) and file['status'] != 'deleted': url = self.api + 'files/restoreFile.php' values = { 'apikey': '12', 'computerId': self.computerId, 'modification': file['modification'] } response = json_request(url, values) restore_requests[file_path] = False if (response['result'] == 'ok'): print_message("Sent request of restore file of " + file_path) else: print_message( "Error sending request of restore file of " + file_path) def keep_alive(self): while True: url = self.api + 'computers/keepAlive.php' values = {'apikey': '12', 'computerId': self.computerId} response = json_request(url, values) if (response['result'] != 'ok'): print_message( "Error sending message of keepAlive of computerId " + self.computerId) time.sleep(10) def listen_watcher(self): self.watcher.start(login_box.client, self.computerId) def listen_requests(self): while (True): url = self.api + 'requests/getComputerRequests.php' values = {'apikey': '12', 'computerId': self.computerId} response = json_request(url, values) if (response['result'] == 'ok'): total_requests = len(response['requests']) print_message("Requests: " + str(total_requests)) if (total_requests > 0): self.handle_request(response['requests']) time.sleep(listen_requests_interval) def handle_request(self, requests): for request in requests: if (request['action'] == "storeChunk"): self.store_chunk(request['chunkNumber'], request['modification'], request['fileId']) if (request['action'] == "deleteFile"): self.delete_chunks(request['modification']) if (request['action'] == "giveChunk"): if (os.path.exists(self.budibox_home + "/chunks/" + request['modification'] + "_" + str(request['chunkNumber']) + ".chunk")): self.send_chunk_to_restore(request['modification'], request['chunkNumber'], request['owner']) if (request['action'] == "recoverChunk"): self.store_temp_chunk(request['modification'], request['number'], request['path']) def store_temp_chunk(self, modification, number, path): temp_dir = self.home + "/chunks_restore/temp/" if (not os.path.exists(temp_dir)): os.makedirs(temp_dir) # Creates chunk temp_dir = temp_dir.decode(system_enconding) chunk = open(temp_dir + modification + "_" + str(number) + ".chunk", "w") # Gets chunk body url = self.api + 'chunks/getRecover.php' values = { 'apikey': '12', 'owner': self.computerId, 'number': str(number), 'modification': modification } response = json_request(url, values) if (response['result'] == 'ok'): chunk_body = response['chunk'] chunk.write(chunk_body) chunk.close() print_message("Chunk of " + modification + " and number " + str(number) + " writed!") else: chunk.close() print_message("Error getting chunk of " + modification + " and number " + str(number) + " writed!") # Delete chunk recover url = self.api + 'chunks/deleteChunkRecover.php' values = { 'apikey': '12', 'owner': self.computerId, 'modification': modification, 'number': str(number) } response = json_request(url, values) if (response['result'] == 'ok'): print_message("Deleted chunk recover !") else: print_message("Error deleting chunk recover!") return # Confirm chunk recover url = self.api + 'requests/confirmRecoverChunk.php' values = { 'apikey': '12', 'computerId': self.computerId, 'modification': modification, 'chunkNumber': str(number) } response = json_request(url, values) if (response['result'] == 'ok'): print_message("Confirmed chunk recover !") else: print_message("Error confirming chunk recover!") return # Checks if received all chunks url = self.api + 'files/restoreFileIsDone.php' values = { 'apikey': '12', 'computerId': self.computerId, 'modification': modification } response = json_request(url, values) if (response['result'] == 'ok'): if (response['isDone'] == True): print_message("Restore file " + path + " is done!") f = File(self.budibox_home + path, login_box.client, modification) f.restore_file(temp_dir, self.budibox_home + path) global restore_requests del restore_requests[path] print "Feito" def send_chunk_to_restore(self, modification, number, owner): path = self.budibox_home + "/chunks/" + modification + "_" + str( number) + ".chunk" chunk = open(path, "rb") chunk_body = chunk.read() url = self.api + 'chunks/giveForRestore.php' values = { 'apikey': '12', 'modification': modification, 'number': str(number), 'body': chunk_body, 'owner': owner['$id'] } response = json_post_request(url, values) if (response['result'] == 'ok'): print_message("Sent chunk for restore of " + path) else: print_message("Error sending chunk for restore of " + path) def delete_chunks(self, modification): list_dir = os.listdir(self.budibox_home + "/chunks/") for file in list_dir: if (file.startswith(modification)): f = open(self.budibox_home + "/chunks/" + file, "rb") file_size = len(f.read()) f.close() os.remove(self.budibox_home + "/chunks/" + file) url = self.api + 'users/incOfferUsage.php' values = { 'apikey': '12', 'user': login_box.client.get_email(), 'value': str(-file_size) } response = json_request(url, values) if (response['result'] == 'ok'): print_message("Decremented offer_usage in " + str(file_size)) else: print_message("Error decrementing offer_usage in " + str(file_size)) url = self.api + 'requests/confirmFileDelete.php' values = { 'apikey': '12', 'computerId': self.computerId, 'modification': modification } response = json_request(url, values) if (response['result'] == 'ok'): print_message("Sent confirm delete message of modification: " + modification) else: print_message("Error confirm delete message of modification: " + modification) def store_chunk(self, chunkNumber, modification, fileId): # Gets Information about chunk to Store url = self.api + 'chunks/get.php' values = { 'apikey': '12', 'fileId': fileId['$id'], 'modification': modification, 'number': str(chunkNumber) } response = json_request(url, values) if (response['result'] == 'ok'): chunk_body = response['chunk'] else: print_message("Error trying to get chunk body of fileID " + fileId['$id'] + "and chunkNumber " + str(chunkNumber)) return False print_message("Processing request, getting chunk body of " + fileId['$id'] + " and chunkNumber " + str(chunkNumber)) if (not os.path.exists(self.budibox_home + "/chunks/")): os.makedirs(self.budibox_home + "/chunks/") chunk_file = open( self.budibox_home + "/chunks/" + modification + "_" + str(chunkNumber) + ".chunk", "wb") chunk_file.write(chunk_body) chunk_file.close() # Sends confirmStorage message url = self.api + 'chunks/confirmStorage.php' values = { 'apikey': '12', 'fileId': fileId['$id'], 'computerId': self.computerId, 'number': str(chunkNumber), 'modification': modification } response = json_request(url, values) if (response['result'] == 'ok'): print_message("Sent confirmation message: fileId " + fileId['$id'] + " and chunkNumber " + str(chunkNumber) + " and computerId " + self.computerId) # Adds space of the offer_used url = self.api + 'users/incOfferUsage.php' values = { 'apikey': '12', 'user': login_box.client.get_email(), 'value': str(len(chunk_body)) } response_space = json_request(url, values) if (response_space['result'] == 'ok'): print_message("Increment offer usage successfully with " + str(len(chunk_body))) return True else: print_message("Error in incrementing offer usage with " + str(len(chunk_body))) return False else: print_message("Error trying to send confirm message: fileId " + fileId['$id'] + " and chunkNumber " + str(chunkNumber) + " and computerId " + self.computerId) return False
def main(): # Setup Logging logger = logging.getLogger('twitterbox') hdlr = logging.FileHandler(settings.LOG) formatter = logging.Formatter('%(asctime)s %(levelname)s %(message)s') hdlr.setFormatter(formatter) logger.addHandler(hdlr) hdlr = logging.StreamHandler(sys.stdout) formatter = logging.Formatter('%(asctime)s %(levelname)s %(message)s') hdlr.setFormatter(formatter) logger.addHandler(hdlr) if settings.DEBUG: logger.setLevel(logging.DEBUG) logger.info("DEBUG level logging") else: logger.setLevel(logging.INFO) logger.info("INFO level logging") logger.info("Starting up...") if PI: # Not interested #GPIO.setwarnings(False) # Setup the LCD display GPIO.setmode(GPIO.BCM) # Use BCM GPIO numbers # Setup the alert light if settings.LIGHT_PIN_1: GPIO.setup(settings.LIGHT_PIN_1, GPIO.OUT) GPIO.output(settings.LIGHT_PIN_1, GPIO.LOW) if settings.LIGHT_PIN_2: GPIO.setup(settings.LIGHT_PIN_2, GPIO.OUT) GPIO.output(settings.LIGHT_PIN_2, GPIO.LOW) # The queue is where messages go to be displayed queue = Queue.PriorityQueue() watcher = None printer = None server = None loops = 0 while True: try: loops = loops + 1 logger.debug("Main Loop " + str(loops)) # Make sure our twitter thread is alive and happy if not watcher or not watcher.is_alive(): logger.info("Starting watcher thread") watcher = Watcher(queue, logger) watcher.setDaemon(True) watcher.start() # Make sure our printing thread is alive and happy if not printer or not printer.is_alive(): logger.info("Starting printer thread") printer = Printer(queue, logger, PI) printer.setDaemon(True) printer.start() if settings.HTTP_SERVER: if not server or not server.is_alive(): logger.info("Starting HTTP server") server = Server(queue, logger) server.setDaemon(True) server.start() # Throw some info in the queue if it's getting low if queue.qsize() < 1: messages = open(settings.MSG_FILE, 'r') for msg in messages: queue.put((settings.PRIORITY_LOW, msg, "", False)) messages.close() if settings.SLIDE_DIR: for filename in os.listdir(settings.SLIDE_DIR): queue.put((settings.PRIORITY_LOW, os.path.join(settings.SLIDE_DIR, filename), SLIDE, False)) except Exception as e: logger.exception("Exception in main thread: " + str(e)) time.sleep(15) logger.warn("Exiting main thread")
parser.add_argument("--debug") parser.add_argument( "--rpccookiefile", help="Cookie file for Bitcoin Core RPC creds", default="~/.bitcoin/.cookie", ) args = parser.parse_args() procs = [] # Handler for SIGINT that stops all of the processes def sigint_handler(signal, frame): global procs for p in procs: p.close() p.terminate() # Start all watcher processes signal.signal(signal.SIGINT, signal.SIG_IGN) for pool in POOLS: proc = Watcher(pool[0], pool[1], args.rpccookiefile, name=f"Watcher {pool[0]}") proc.start() procs.append(proc) signal.signal(signal.SIGINT, sigint_handler) # Interrupt and wait for all of the processes to end for p in procs: p.join()
def main(): # Setup Logging logger = logging.getLogger('twitterbox') hdlr = logging.FileHandler(settings.LOG) formatter = logging.Formatter('%(asctime)s %(levelname)s %(message)s') hdlr.setFormatter(formatter) logger.addHandler(hdlr) hdlr = logging.StreamHandler(sys.stdout) formatter = logging.Formatter('%(asctime)s %(levelname)s %(message)s') hdlr.setFormatter(formatter) logger.addHandler(hdlr) if settings.DEBUG: logger.setLevel(logging.DEBUG) logger.info("DEBUG level logging") else: logger.setLevel(logging.INFO) logger.info("INFO level logging") logger.info("Starting up...") if PI: # Not interested GPIO.setwarnings(False) # Setup the LCD display GPIO.setmode(GPIO.BCM) # Use BCM GPIO numbers # Setup the alert light GPIO.setup(settings.LIGHT_PIN, GPIO.OUT) GPIO.output(settings.LIGHT_PIN, GPIO.LOW) # The queue is where messages go to be displayed queue = Queue.PriorityQueue() watcher = None printer = None loops = 0 while True: try: loops = loops + 1 logger.debug("Main Loop " + str(loops)) # Make sure our twitter thread is alive and happy if not watcher or not watcher.is_alive(): logger.info("Starting watcher thread") watcher = Watcher(queue, logger) watcher.setDaemon(True) watcher.start() # Make sure our printing thread is alive and happy if not printer or not printer.is_alive(): logger.info("Starting printer thread") printer = Printer(queue, logger, PI) printer.setDaemon(True) printer.start() # Throw some info in the queue if it's getting low if queue.qsize() == 0: messages = open(settings.MSG_FILE, 'r') for msg in messages: queue.put((settings.PRIORITY_LOW, msg, "", False)) messages.close() # Switching to using the message file instead of this generated msg #for w in settings.TRACK: # queue.put((settings.PRIORITY_LOW, "Watching for:", w, False)) # We are not tracking the number of followers for this event # This frees up an extra twitter connection to avoid rate limiting --JLS # user_data = watcher.getUserData() # if user_data != None: # for k,v in user_data.iteritems(): # queue.put((PRIORITY_LOW, k, v, False)) except Exception as e: logger.error("Exception in main thread: " + str(e)) traceback.print_tb(sys.exc_info()[2]) time.sleep(15) logger.warn("Exiting main thread")
def start(self): self.gui = WatcherGUI(self) Watcher.start(self)
class ClientDaemon: def __init__(self): self.init_home_dir() self.api = "https://andrefreitas.pt/budibox/api/" self.login = LoginBox() self.watcher = Watcher(self.budibox_home) def init_home_dir(self): # Searchs for user home folder and creates budibox folder self.budibox_home = expanduser("~") + "/budibox" self.home = expanduser("~") self.budibox_home = self.budibox_home.decode(system_enconding) # Creates budibox folder if not os.path.exists(self.budibox_home): os.makedirs(self.budibox_home) def client_start(self): self.login.start() self.computerId = self.get_computer_id() watcher_thread = Thread(target=self.listen_watcher, args=()) requests_thread = Thread(target=self.listen_requests, args=()) keep_alive = Thread(target=self.keep_alive, args=()) sync = Thread(target=self.sync, args=()) # Start threads watcher_thread.start() requests_thread.start() keep_alive.start() sync.start() # Join threads watcher_thread.join() requests_thread.join() keep_alive.join() sync.join() def get_computer_id(self): url = self.api + "computers/getComputerId.php" values = {"apikey": "12", "user": login_box.client.get_email(), "computer": get_computer_name()} response = json_request(url, values) print_message("Getting the computer ID") if response["result"] == "ok": return response["computerId"] def sync(self): while True: url = self.api + "files/getUserFiles.php" values = {"apikey": "12", "user": login_box.client.get_email()} response = json_request(url, values) if response["result"] != "ok": print_message("Error trying to get user files of " + login_box.client.get_email()) else: print_message("Total files: " + str(len(response["files"]))) self.restore_file(response["files"]) time.sleep(sync_files_interval) def restore_file(self, files): list_dir = os.listdir(self.budibox_home) global restore_requests for file in files: file_path = file["path"] if os.path.isfile(self.budibox_home + file_path): if file["status"] == "deleted": os.remove(self.budibox_home + file_path) break timestamp = int(file["date_modified"]["sec"]) datetime_request = datetime.fromtimestamp(timestamp) datetime_local_file = datetime.fromtimestamp(os.path.getmtime(self.budibox_home + file_path)) difference_times = ( time.mktime(datetime_request.timetuple()) - time.mktime(datetime_local_file.timetuple()) - 3600 ) if difference_times > 0: if not (file_path in restore_requests): print_message("More recent " + file_path) url = self.api + "files/restoreFile.php" values = {"apikey": "12", "computerId": self.computerId, "modification": file["modification"]} response = json_request(url, values) if response["result"] == "ok": print_message("Sent request of restore file of " + file_path) restore_requests[file_path] = False else: print_message("Error sending request of restore file of " + file_path) else: print "older or equal" else: if not (file_path in restore_requests) and file["status"] != "deleted": url = self.api + "files/restoreFile.php" values = {"apikey": "12", "computerId": self.computerId, "modification": file["modification"]} response = json_request(url, values) restore_requests[file_path] = False if response["result"] == "ok": print_message("Sent request of restore file of " + file_path) else: print_message("Error sending request of restore file of " + file_path) def keep_alive(self): while True: url = self.api + "computers/keepAlive.php" values = {"apikey": "12", "computerId": self.computerId} response = json_request(url, values) if response["result"] != "ok": print_message("Error sending message of keepAlive of computerId " + self.computerId) time.sleep(10) def listen_watcher(self): self.watcher.start(login_box.client, self.computerId) def listen_requests(self): while True: url = self.api + "requests/getComputerRequests.php" values = {"apikey": "12", "computerId": self.computerId} response = json_request(url, values) if response["result"] == "ok": total_requests = len(response["requests"]) print_message("Requests: " + str(total_requests)) if total_requests > 0: self.handle_request(response["requests"]) time.sleep(listen_requests_interval) def handle_request(self, requests): for request in requests: if request["action"] == "storeChunk": self.store_chunk(request["chunkNumber"], request["modification"], request["fileId"]) if request["action"] == "deleteFile": self.delete_chunks(request["modification"]) if request["action"] == "giveChunk": if os.path.exists( self.budibox_home + "/chunks/" + request["modification"] + "_" + str(request["chunkNumber"]) + ".chunk" ): self.send_chunk_to_restore(request["modification"], request["chunkNumber"], request["owner"]) if request["action"] == "recoverChunk": self.store_temp_chunk(request["modification"], request["number"], request["path"]) def store_temp_chunk(self, modification, number, path): temp_dir = self.home + "/chunks_restore/temp/" if not os.path.exists(temp_dir): os.makedirs(temp_dir) # Creates chunk temp_dir = temp_dir.decode(system_enconding) chunk = open(temp_dir + modification + "_" + str(number) + ".chunk", "w") # Gets chunk body url = self.api + "chunks/getRecover.php" values = {"apikey": "12", "owner": self.computerId, "number": str(number), "modification": modification} response = json_request(url, values) if response["result"] == "ok": chunk_body = response["chunk"] chunk.write(chunk_body) chunk.close() print_message("Chunk of " + modification + " and number " + str(number) + " writed!") else: chunk.close() print_message("Error getting chunk of " + modification + " and number " + str(number) + " writed!") # Delete chunk recover url = self.api + "chunks/deleteChunkRecover.php" values = {"apikey": "12", "owner": self.computerId, "modification": modification, "number": str(number)} response = json_request(url, values) if response["result"] == "ok": print_message("Deleted chunk recover !") else: print_message("Error deleting chunk recover!") return # Confirm chunk recover url = self.api + "requests/confirmRecoverChunk.php" values = { "apikey": "12", "computerId": self.computerId, "modification": modification, "chunkNumber": str(number), } response = json_request(url, values) if response["result"] == "ok": print_message("Confirmed chunk recover !") else: print_message("Error confirming chunk recover!") return # Checks if received all chunks url = self.api + "files/restoreFileIsDone.php" values = {"apikey": "12", "computerId": self.computerId, "modification": modification} response = json_request(url, values) if response["result"] == "ok": if response["isDone"] == True: print_message("Restore file " + path + " is done!") f = File(self.budibox_home + path, login_box.client, modification) f.restore_file(temp_dir, self.budibox_home + path) global restore_requests del restore_requests[path] print "Feito" def send_chunk_to_restore(self, modification, number, owner): path = self.budibox_home + "/chunks/" + modification + "_" + str(number) + ".chunk" chunk = open(path, "rb") chunk_body = chunk.read() url = self.api + "chunks/giveForRestore.php" values = { "apikey": "12", "modification": modification, "number": str(number), "body": chunk_body, "owner": owner["$id"], } response = json_post_request(url, values) if response["result"] == "ok": print_message("Sent chunk for restore of " + path) else: print_message("Error sending chunk for restore of " + path) def delete_chunks(self, modification): list_dir = os.listdir(self.budibox_home + "/chunks/") for file in list_dir: if file.startswith(modification): f = open(self.budibox_home + "/chunks/" + file, "rb") file_size = len(f.read()) f.close() os.remove(self.budibox_home + "/chunks/" + file) url = self.api + "users/incOfferUsage.php" values = {"apikey": "12", "user": login_box.client.get_email(), "value": str(-file_size)} response = json_request(url, values) if response["result"] == "ok": print_message("Decremented offer_usage in " + str(file_size)) else: print_message("Error decrementing offer_usage in " + str(file_size)) url = self.api + "requests/confirmFileDelete.php" values = {"apikey": "12", "computerId": self.computerId, "modification": modification} response = json_request(url, values) if response["result"] == "ok": print_message("Sent confirm delete message of modification: " + modification) else: print_message("Error confirm delete message of modification: " + modification) def store_chunk(self, chunkNumber, modification, fileId): # Gets Information about chunk to Store url = self.api + "chunks/get.php" values = {"apikey": "12", "fileId": fileId["$id"], "modification": modification, "number": str(chunkNumber)} response = json_request(url, values) if response["result"] == "ok": chunk_body = response["chunk"] else: print_message( "Error trying to get chunk body of fileID " + fileId["$id"] + "and chunkNumber " + str(chunkNumber) ) return False print_message( "Processing request, getting chunk body of " + fileId["$id"] + " and chunkNumber " + str(chunkNumber) ) if not os.path.exists(self.budibox_home + "/chunks/"): os.makedirs(self.budibox_home + "/chunks/") chunk_file = open(self.budibox_home + "/chunks/" + modification + "_" + str(chunkNumber) + ".chunk", "wb") chunk_file.write(chunk_body) chunk_file.close() # Sends confirmStorage message url = self.api + "chunks/confirmStorage.php" values = { "apikey": "12", "fileId": fileId["$id"], "computerId": self.computerId, "number": str(chunkNumber), "modification": modification, } response = json_request(url, values) if response["result"] == "ok": print_message( "Sent confirmation message: fileId " + fileId["$id"] + " and chunkNumber " + str(chunkNumber) + " and computerId " + self.computerId ) # Adds space of the offer_used url = self.api + "users/incOfferUsage.php" values = {"apikey": "12", "user": login_box.client.get_email(), "value": str(len(chunk_body))} response_space = json_request(url, values) if response_space["result"] == "ok": print_message("Increment offer usage successfully with " + str(len(chunk_body))) return True else: print_message("Error in incrementing offer usage with " + str(len(chunk_body))) return False else: print_message( "Error trying to send confirm message: fileId " + fileId["$id"] + " and chunkNumber " + str(chunkNumber) + " and computerId " + self.computerId ) return False
import os import time import sys from watcher import Watcher if __name__ == "__main__": # path = "D:\\Bharat - Cisco DCN\\Bharat\\Assign\\Asign" path = input("Enter path to monitor for txt files. \n(Leave blank to monitor current dir) \n").strip() todecode_path = os.path.join(os.path.abspath(os.path.dirname(__file__)), "todecode") txt_watcher = Watcher(action="store_to_zip", path=path, file_type=["txt"]) todecode_watcher = Watcher(action="filter_zip", path=todecode_path, file_type=["zip"]) txt_watcher.daemon = True todecode_watcher.daemon = True try: txt_watcher.start() time.sleep(1) todecode_watcher.start() while 1: time.sleep(1) except KeyboardInterrupt: print("Stopped Manually..")