import cacheManager import configManager import fileMover import helper # from watchdog.observers.polling import PollingObserver as Observer import scanner # from getpass import getuser if __name__ == '__main__': patterns = '*' ignore_patterns = '' ignore_directories = False case_sensitive = True my_event_handler = PatternMatchingEventHandler(patterns, ignore_patterns, ignore_directories, case_sensitive) def on_created(file): ''' Prints out a statement when a new file is created in the specified directory :param file: The specified file :return: None ''' # print(get_filename(file)) print(f"{file.src_path} has been created!") def on_deleted(file):
def convertHexPubKey(hex_pub_key, output_format="ed25519"): raw_pub_key = binascii.unhexlify(hex_pub_key) bech32_pub_key = bech32.bech32_encode("ed25519_pk", bech32.convertbits(raw_pub_key, 8, 5)) if output_format == "ed25519": return bech32_pub_key elif output_format == "jcliaddr": return "jcliaddr_" + hex_pub_key else: print(f"output format {output_format} not supported!") os.chdir(os.getcwd() + "/" + rewards_path) # setup an observer to parse new files parse_event_handler = PatternMatchingEventHandler(patterns=[csvFilePath]) def parseFileEvent(event): start_time = time.time() filename = event.src_path csvfile = os.path.splitext(filename)[0] _, _, epoch, _ = filename.split('-') # rewards are distributed for the previous epoch epoch = str(int(epoch) - 1) aggregate=True try: get_tip() except: aggregate=False rewards[epoch] = parseEpochRewards(csvfile, epoch, aggregate=aggregate) end_time = time.time()
from time import sleep import os.path from os import path import threading from datetime import datetime, timedelta from watchdog.observers import Observer from watchdog.events import PatternMatchingEventHandler instance = input("instance (earth, delay, mars, rover):") network_map_path = 'ion-open-source-3.7.1/dtn/network.map' ip_map_path = 'ion-open-source-3.7.1/dtn/ip.map' msg_queue_path = 'ion-open-source-3.7.1/dtn/msg_queue.dat' host_rc_path = 'ion-open-source-3.7.1/dtn/host.rc' cfdp_rc_path = 'ion-open-source-3.7.1/dtn/host.cfdprc' incoming_message_directory_path = 'ion-open-source-3.7.1/dtn/incoming' my_event_handler = PatternMatchingEventHandler("*", "", False, True) FWD_QUEUE = 'ion-open-source-3.7.1/dtn/msg_queue.dat' DELAY_QUEUE = 'delay_queue.txt' USERS_DIR = 'Users/' UPLOAD_DIR = 'Uploads/' MSG_FILE_NAME = 'msg_log.txt' if instance == 'earth': THIS_USER = '******' if instance == 'rover': THIS_USER = '******' if instance == 'delay': THIS_USER = '******' if instance == 'mars': THIS_USER = '******' FWD_DIR = 'ion-open-source-3.7.1/dtn/incoming/'
def __init__(self): self.event_handler = PatternMatchingEventHandler(["*.eu4"], "", True, False) self.event_handler.on_modified = self.on_modified
def test_patterns(self): handler1 = PatternMatchingEventHandler(g_allowed_patterns, g_ignore_patterns, True) self.assertEqual(handler1.patterns, g_allowed_patterns)
# Check for valid paths if (not path.exists(args.src_path)): print("src_path needs to exist!\nExiting.") exit() if (not path.exists(args.dest_path)): print("dest_path needs to exist!\nExiting.") exit() if args.verbose: print("Verbose: " + str(args.verbose)) print("src_path:\n" + args.src_path) print("dest_path:\n" + args.dest_path) # set up the event handler patterns = "*" my_event_handler = PatternMatchingEventHandler(patterns) # the other events were not needed but could be useful for other tasks # in the future my_event_handler.on_created = on_created # my_event_handler.on_deleted = on_deleted # my_event_handler.on_modified = on_modified # my_event_handler.on_moved = on_moved # Set up the observer my_observer = Observer() my_observer.schedule(my_event_handler, args.src_path, recursive=True) my_observer.start() if args.verbose: print("Observer initialization done.\nNow looking at: " +
import time from watchdog.observers import Observer from watchdog.events import PatternMatchingEventHandler if __name__ == '__main__': patterns = '*' ignore_patterns = '' ignore_directories = '' my_event_handler = PatternMatchingEventHandler( patterns=patterns, # watched patterns ignore_patterns=ignore_patterns, # ignored patterns ignore_directories=ignore_directories, # ignored directories case_sensitive=True) def on_created(event): print(f"File created: {event.src_path}") def on_deleted(event): print(f"File deleted: {event.src_path}") def on_modified(event): print(f"File modified: {event.src_path}") def on_moved(event): print(f"File moved: {event.src_path}") my_event_handler.on_created = on_created my_event_handler.on_deleted = on_deleted my_event_handler.on_modified = on_modified my_event_handler.on_moved = on_moved
def main(): parser = ArgumentParser(description='') parser.add_argument('path', default='') parser.add_argument('--config', help='Config file') parser.add_argument('--generate-config', help='Generate config file', action='store_true') parser.add_argument('--create-project', help='Create a default project folder structure') parser.add_argument('--server', help='Run a development server', action='store_true') parser.add_argument('--rebuild', help='Rebuilds all files', action='store_true') parser.add_argument('--minify', help='Minifies HTML, CSS and JS files', action='store_true') parser.add_argument('--watch', help='Watch directory and automatically rebuild', action='store_true') parser.add_argument('--verbose', help='Show verbose output', action='store_true') parser.add_argument('--version', action='version', version=f'%(prog)s {__version__}') global WORKING_PATH if parser.parse_args().path: working_path = parser.parse_args().path if parser.parse_args().path is '.': working_path = os.getcwd() + '/' print(f'[MAIN] Using path: {working_path}') WORKING_PATH = working_path if parser.parse_args().config: print(parser.parse_args().config) set_config_file(parser.parse_args().config) elif check_config_exists(location=working_path): if check_config_exists(location=working_path) == 'OTHER_DIR': set_config_file(working_path + 'mango.toml') else: set_config_file() else: set_config_file(default_config_options, use_default=True) if parser.parse_args().rebuild: rebuild(True if parser.parse_args().minify is True else False, folder=working_path, verbose=parser.parse_args().verbose) elif parser.parse_args().create_project: project_name = parser.parse_args().create_project if os.path.exists(project_name): print('Folder already exists, please use a non existing folder.') else: os.makedirs(project_name) os.makedirs(project_name + '/content') os.makedirs(project_name + '/static') os.makedirs(project_name + '/templates') generate_config(location=project_name + '/') create_default_files(project_name) elif parser.parse_args().generate_config: generate_config('') if parser.parse_args().server and parser.parse_args().watch: try: server = WebServer(str(get_config_setting('server', 'host')), str(get_config_setting('server', 'port')), parser.parse_args().path) event_handler = PatternMatchingEventHandler( patterns='*', ignore_patterns=['output/*'], ignore_directories=True) event_handler.on_modified = watch_on_modified observer = Observer() observer.schedule(event_handler, working_path, recursive=True) observer.start() print('[WATCHER] Started watcher') server.start_server() except KeyboardInterrupt: server.stop_server() observer.stop() observer.join() if parser.parse_args().server: try: server = WebServer(str(get_config_setting('server', 'host')), str(get_config_setting('server', 'port')), parser.parse_args().path) server.start_server() except KeyboardInterrupt: server.stop_server() if parser.parse_args().watch: try: event_handler = PatternMatchingEventHandler( patterns='*', ignore_patterns=['output/*'], ignore_directories=True) event_handler.on_modified = watch_on_modified observer = Observer() observer.schedule(event_handler, working_path, recursive=True) observer.start() print('[WATCHER] Started watcher') except KeyboardInterrupt: observer.stop() observer.join()
def watchdog(ctx, jobs="auto", sleep_time=5): """ Start watchdog service to watch F90 files and execute `make` when changes are detected. """ cprint( "Starting watchdog service to watch F90 files and execute `make` when changes are detected", color="green") cprint("Enter <CTRL + C> in the terminal to kill the service.", color="green") cprint(f"Start watching F90 files with sleep_time {sleep_time} s ....", color="green") top = find_top_build_tree(".", with_abinit=True) jobs = max(1, number_of_cpus() // 2) if jobs == "auto" else int(jobs) # http://thepythoncorner.com/dev/how-to-create-a-watchdog-in-python-to-look-for-filesystem-changes/ # https://stackoverflow.com/questions/19991033/generating-multiple-observers-with-python-watchdog import time from watchdog.observers import Observer from watchdog.events import PatternMatchingEventHandler event_handler = PatternMatchingEventHandler(patterns="*.F90", ignore_patterns="", ignore_directories=False, case_sensitive=True) def on_created(event): print(f"hey, {event.src_path} has been created!") def on_deleted(event): print(f"what the f**k! Someone deleted {event.src_path}!") def on_modified(event): print(f"hey buddy, {event.src_path} has been modified") cmd = "make -j%d > >(tee -a make.log) 2> >(tee -a make.stderr >&2)" % jobs cprint("Executing: %s" % cmd, color="yellow") with cd(top): try: result = ctx.run(cmd, pty=True) if result.ok: cprint("Make completed successfully", color="green") cprint("Watching for changes ...", color="green") except Exception: cprint(f"Make returned non-zero exit status", color="red") cprint( f"Keep on watching for changes hoping you get it right ...", color="red") def on_moved(event): print(f"ok ok ok, someone moved {event.src_path} to {event.dest_path}") event_handler.on_created = on_created event_handler.on_deleted = on_deleted event_handler.on_modified = on_modified event_handler.on_moved = on_moved observer = Observer() path = ABINIT_SRCDIR observer.schedule(event_handler, path, recursive=True) observer.start() try: while True: time.sleep(sleep_time) except KeyboardInterrupt: observer.stop() observer.join()