Example #1
0
def shell_command(args):
    """
    Subcommand to execute shell commands in response to file system events.

    :param args:
        Command line argument options.
    """
    from watchdog.tricks import ShellCommandTrick

    if not args.command:
        args.command = None

    if args.debug_force_polling:
        from watchdog.observers.polling import PollingObserver as Observer
    else:
        from watchdog.observers import Observer

    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)
Example #2
0
def ablog_serve(website=None,
                port=8000,
                view=True,
                rebuild=False,
                patterns="*.rst;*.txt",
                **kwargs):

    confdir = find_confdir()
    conf = read_conf(confdir)

    # to allow restarting the server in short succession
    socketserver.TCPServer.allow_reuse_address = True

    Handler = server.SimpleHTTPRequestHandler
    httpd = socketserver.TCPServer(("", port), Handler)

    ip, port = httpd.socket.getsockname()
    print(f"Serving HTTP on {ip}:{port}.")
    print("Quit the server with Control-C.")

    website = website or os.path.join(
        confdir, getattr(conf, "ablog_website", "_website"))

    os.chdir(website)

    if rebuild:

        patterns = patterns.split(";")
        ignore_patterns = [os.path.join(website, "*")]
        handler = ShellCommandTrick(
            shell_command="ablog build -s " + confdir,
            patterns=patterns,
            ignore_patterns=ignore_patterns,
            ignore_directories=False,
            wait_for_process=True,
            drop_during_process=False,
        )

        observer = Observer(timeout=1)
        observer.schedule(handler, confdir, recursive=True)
        observer.start()
        try:
            if view:
                (webbrowser.open_new_tab(f"http://127.0.0.1:{port}")
                 and httpd.serve_forever())
            else:
                httpd.serve_forever()
        except KeyboardInterrupt:
            observer.stop()
        observer.join()

    else:
        if view:
            (webbrowser.open_new_tab(f"http://127.0.0.1:{port}")
             and httpd.serve_forever())
        else:
            httpd.serve_forever()
Example #3
0
def shell_command(args):
    from watchdog.observers import Observer
    from 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)
Example #4
0
def watch():
    """Renerate documentation when it changes."""

    # Start with a clean build
    sphinx_build['-b', 'html', '-E', 'docs', 'docs/_build/html'] & FG

    handler = ShellCommandTrick(
        shell_command='sphinx-build -b html docs docs/_build/html',
        patterns=['*.rst', '*.py'],
        ignore_patterns=['_build/*'],
        ignore_directories=['.tox'],
        drop_during_process=True)
    observer = Observer()
    observe_with(observer, handler, pathnames=['.'], recursive=True)
Example #5
0
def shell_command(command,patterns,ignore_patterns,directories):
    ignore_directories = False
    wait_for_process = False
    drop_during_process = False
    timeout = 2.0
    recursive = True
    handler = ShellCommandTrick(shell_command=command,
                                patterns=patterns,
                                ignore_patterns=ignore_patterns,
                                ignore_directories=ignore_directories,
                                wait_for_process=wait_for_process,
                                drop_during_process=drop_during_process)
    observer = Observer(timeout=timeout)
    observe_with(observer, handler, directories, recursive)
Example #6
0
def test_shell_command_subprocess_termination_nowait(tmpdir):
    from watchdog.events import FileModifiedEvent
    from watchdog.tricks import ShellCommandTrick
    import sys
    import time
    script = make_dummy_script(tmpdir, n=1)
    command = " ".join([sys.executable, script])
    trick = ShellCommandTrick(command, wait_for_process=False)
    assert not trick.is_process_running()
    trick.on_any_event(FileModifiedEvent("foo/bar.baz"))
    assert trick.is_process_running()
    time.sleep(5)
    assert not trick.is_process_running()
Example #7
0
def watch(command, directories='.', patterns='*', ignore_patterns='', ignore_directories=False, wait_for_process=False, drop_during_process=False, timeout=5, recursive=True):
    """
    Subcommand to execute shell commands in response to file system events.
    :param args:
        Command line argument options.
    """
    from watchdog.watchmedo import parse_patterns, observe_with
    from watchdog.tricks import ShellCommandTrick
    from watchdog.observers import Observer

    patterns, ignore_patterns = parse_patterns(patterns, ignore_patterns)
    handler = ShellCommandTrick(shell_command=command,
                                patterns=patterns,
                                ignore_patterns=ignore_patterns,
                                ignore_directories=ignore_directories,
                                wait_for_process=wait_for_process,
                                drop_during_process=drop_during_process)
    observer = Observer(timeout=timeout)
    observe_with(observer, handler, directories, recursive)
Example #8
0
def test_shell_command_wait_for_completion(tmpdir, capfd):
    from watchdog.events import FileModifiedEvent
    from watchdog.tricks import ShellCommandTrick
    import sys
    import time
    script = make_dummy_script(tmpdir, n=1)
    command = " ".join([sys.executable, script])
    trick = ShellCommandTrick(command, wait_for_process=True)
    assert not trick.is_process_running()
    start_time = time.monotonic()
    trick.on_any_event(FileModifiedEvent("foo/bar.baz"))
    elapsed = time.monotonic() - start_time
    print(capfd.readouterr())
    assert not trick.is_process_running()
    assert elapsed >= 1
Example #9
0
def shell_command(args):
    """
    Subcommand to execute shell commands in response to file system events.

    :param args:
        Command line argument options.
    """
    from watchdog.observers import Observer
    from 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)
    observer = Observer(timeout=args.timeout)
    observe_with(observer, handler, args.directories, args.recursive)
Example #10
0
def watch_mgipython():

    # start monitoring MGIPYTHONLIB for file changes
    # a change will trigger mgipython to rebuild
    #  this in turn forces the dev server to restart
    try:
        mgipython_dir = os.environ['MGIPYTHONLIB']
        mgipython_src_dir = os.path.join(mgipython_dir, 'mgipython')

        handler = ShellCommandTrick(
            shell_command="cd %s; ./rebuild; touch %s" %
            (mgipython_dir, app_config_file),
            #shell_command="echo 'hi'",
            patterns=["*.py"])

        mgipython_observer = Observer()
        mgipython_observer.schedule(handler, mgipython_src_dir, recursive=True)
        mgipython_observer.start()

    except KeyError as e:
        return
Example #11
0
def ablog_serve(website=None,
                port=8000,
                view=True,
                rebuild=False,
                patterns='*.rst;*.txt',
                **kwargs):

    confdir = find_confdir()
    conf = read_conf(confdir)

    try:
        import SimpleHTTPServer as server
    except ImportError:
        from http import server
        import socketserver
    else:
        import SocketServer as socketserver

    import webbrowser

    Handler = server.SimpleHTTPRequestHandler
    httpd = socketserver.TCPServer(("", port), Handler)

    ip, port = httpd.socket.getsockname()
    print("Serving HTTP on {}:{}.".format(ip, port))
    print("Quit the server with Control-C.")

    website = (website or os.path.join(
        confdir, getattr(conf, 'ablog_builddir', '_website')))

    os.chdir(website)

    if rebuild:

        #from watchdog.watchmedo import observe_with
        from watchdog.observers import Observer
        from watchdog.tricks import ShellCommandTrick
        patterns = patterns.split(';')
        ignore_patterns = [os.path.join(website, '*')]
        handler = ShellCommandTrick(shell_command='ablog build',
                                    patterns=patterns,
                                    ignore_patterns=ignore_patterns,
                                    ignore_directories=False,
                                    wait_for_process=True,
                                    drop_during_process=False)

        observer = Observer(timeout=1)
        observer.schedule(handler, confdir, recursive=True)
        observer.start()
        try:
            if view:
                (webbrowser.open_new_tab('http://127.0.0.1:{}'.format(port))
                 and httpd.serve_forever())
            else:
                httpd.serve_forever()
        except KeyboardInterrupt:
            observer.stop()
        observer.join()

    else:
        if view:
            (webbrowser.open_new_tab('http://127.0.0.1:{}'.format(port))
             and httpd.serve_forever())
        else:
            httpd.serve_forever()