def tricks_from(args): """ Subcommand to execute tricks from a tricks configuration file. :param args: Command line argument options. """ from mitmflib.watchdog.observers import Observer add_to_sys_path(path_split(args.python_path)) observers = [] for tricks_file in args.files: observer = Observer(timeout=args.timeout) if not os.path.exists(tricks_file): raise IOError("cannot find tricks file: %s" % tricks_file) config = load_config(tricks_file) try: tricks = config[CONFIG_KEY_TRICKS] except KeyError: raise KeyError("No `%s' key specified in %s." % ( CONFIG_KEY_TRICKS, tricks_file)) if CONFIG_KEY_PYTHON_PATH in config: add_to_sys_path(config[CONFIG_KEY_PYTHON_PATH]) dir_path = os.path.dirname(tricks_file) if not dir_path: dir_path = os.path.relpath(os.getcwd()) schedule_tricks(observer, tricks, dir_path, args.recursive) observer.start() observers.append(observer) try: while True: time.sleep(1) except KeyboardInterrupt: for o in observers: o.unschedule_all() o.stop() for o in observers: o.join()
def auto_restart(args): """ Subcommand to start a long-running subprocess and restart it on matched events. :param args: Command line argument options. """ from mitmflib.watchdog.observers import Observer from mitmflib.watchdog.tricks import AutoRestartTrick import signal import re if not args.directories: args.directories = ['.'] # Allow either signal name or number. if re.match('^SIG[A-Z]+$', args.signal): stop_signal = getattr(signal, args.signal) else: stop_signal = int(args.signal) # Handle SIGTERM in the same manner as SIGINT so that # this program has a chance to stop the child process. def handle_sigterm(_signum, _frame): raise KeyboardInterrupt() signal.signal(signal.SIGTERM, handle_sigterm) patterns, ignore_patterns = parse_patterns(args.patterns, args.ignore_patterns) command = [args.command] command.extend(args.command_args) handler = AutoRestartTrick(command=command, patterns=patterns, ignore_patterns=ignore_patterns, ignore_directories=args.ignore_directories, stop_signal=stop_signal, kill_after=args.kill_after) handler.start() observer = Observer(timeout=args.timeout) observe_with(observer, handler, args.directories, args.recursive) handler.stop()
def log(args): """ Subcommand to log file system events to the console. :param args: Command line argument options. """ from mitmflib.watchdog.utils import echo from mitmflib.watchdog.tricks import LoggerTrick if args.trace: echo.echo_class(LoggerTrick) patterns, ignore_patterns =\ parse_patterns(args.patterns, args.ignore_patterns) handler = LoggerTrick(patterns=patterns, ignore_patterns=ignore_patterns, ignore_directories=args.ignore_directories) if args.debug_force_polling: from mitmflib.watchdog.observers.polling import PollingObserver as Observer elif args.debug_force_kqueue: from mitmflib.watchdog.observers.kqueue import KqueueObserver as Observer elif args.debug_force_winapi_async: from mitmflib.watchdog.observers.read_directory_changes_async import\ WindowsApiAsyncObserver as Observer elif args.debug_force_winapi: from mitmflib.watchdog.observers.read_directory_changes import\ WindowsApiObserver as Observer elif args.debug_force_inotify: from mitmflib.watchdog.observers.inotify import InotifyObserver as Observer elif args.debug_force_fsevents: from mitmflib.watchdog.observers.fsevents import FSEventsObserver as Observer else: # Automatically picks the most appropriate observer for the platform # on which it is running. from mitmflib.watchdog.observers import Observer observer = Observer(timeout=args.timeout) observe_with(observer, handler, args.directories, args.recursive)
def shell_command(args): """ Subcommand to execute shell commands in response to file system events. :param args: Command line argument options. """ from mitmflib.watchdog.observers import Observer from mitmflib.watchdog.tricks import ShellCommandTrick if not args.command: args.command = None patterns, ignore_patterns = parse_patterns(args.patterns, args.ignore_patterns) handler = ShellCommandTrick(shell_command=args.command, patterns=patterns, ignore_patterns=ignore_patterns, ignore_directories=args.ignore_directories, wait_for_process=args.wait_for_process, drop_during_process=args.drop_during_process) observer = Observer(timeout=args.timeout) observe_with(observer, handler, args.directories, args.recursive)
def start_config_watch(self): observer = Observer() observer.schedule(self, path='./config', recursive=False) observer.start()
def start(self): observer = Observer() observer.schedule(self, path='./config/beef_arerules/enabled', recursive=False) observer.start()
def startConfigWatch(self): observer = Observer() observer.schedule(self, path="./config", recursive=False) observer.start()