Пример #1
0
def gen_command(
        plugins='', # extra python packages to load so plugins will register with dexy
        d=None,  # The directory to place generated files in, must not exist.
        t=False, # Shorter alternative to --template.
        template=DEFAULT_TEMPLATE, # The alias of the template to use.
        **kwargs # Additional kwargs passed to template's run() method.
        ):
    """
    Generate a new dexy project in the specified directory, using the template.
    """
    wrapper = init_wrapper(locals())

    if t and (template == DEFAULT_TEMPLATE):
        template = t
    elif t and template != DEFAULT_TEMPLATE:
        raise dexy.exceptions.UserFeedback("Only specify one of --t or --template, not both.")

    if not template in dexy.template.Template.plugins:
        print "Can't find a template named '%s'. Run 'dexy templates' for a list of templates." % template
        sys.exit(1)

    template_instance = dexy.template.Template.create_instance(template)
    template_instance.generate(d, **kwargs)

    # We run dexy setup. This will respect any dexy.conf file in the template
    # but passing command line options for 'setup' to 'gen' currently not supported.
    os.chdir(d)
    wrapper.create_dexy_dirs()
    print "Success! Your new dexy project has been created in directory '%s'" % d
    if file_exists("README"):
        print "\n--------------------------------------------------"
        with open("README", "r") as f:
            print f.read()
        print "\n--------------------------------------------------"
        print "\nThis information is in the 'README' file for future reference."
Пример #2
0
def test_config_file():
    with tempdir():
        with open("dexy.conf", "w") as f:
            f.write("""{ "logfile" : "a.log" }""")

        wrapper = init_wrapper({'conf' : 'dexy.conf'})
        assert wrapper.log_file == "a.log"
Пример #3
0
def targets_command(
        full=False, # Whether to just print likely pretty target names, or all names.
        **kwargs):
    """
    Prints a list of available targets, which can be run via "dexy -target name".
    """
    wrapper = init_wrapper(locals())
    wrapper.assert_dexy_dirs_exist()
    wrapper.to_valid()
    wrapper.to_walked()

    print "Targets you can pass to -target option:"
    for doc in sorted(wrapper.bundle_docs(), key=attrgetter('key')):
        print "  ", doc.key

    if full:
        print
        print "These targets are also available, with lower priority:"
        for doc in sorted(wrapper.non_bundle_docs(), key=attrgetter('key')):
            print "  ", doc.key
        print
        print """Target names can be matched exactly or with the first few characters,
in which case all matching targets will be run."""
    else:
        print
        print "Run this command with --full option for additional available target names."
Пример #4
0
def targets_command(
    full=False,  # Whether to just print likely pretty target names, or all names.
    **kwargs):
    """
    Prints a list of available targets, which can be run via "dexy -target name".
    """
    wrapper = init_wrapper(locals())
    wrapper.assert_dexy_dirs_exist()
    wrapper.to_valid()
    wrapper.to_walked()

    print "Targets you can pass to -target option:"
    for doc in sorted(wrapper.bundle_docs(), key=attrgetter('key')):
        print "  ", doc.key

    if full:
        print
        print "These targets are also available, with lower priority:"
        for doc in sorted(wrapper.non_bundle_docs(), key=attrgetter('key')):
            print "  ", doc.key
        print
        print """Target names can be matched exactly or with the first few characters,
in which case all matching targets will be run."""
    else:
        print
        print "Run this command with --full option for additional available target names."
Пример #5
0
def test_config_file():
    with tempdir():
        with open("dexy.conf", "w") as f:
            f.write("""{ "logfile" : "a.log" }""")

        wrapper = init_wrapper({'conf': 'dexy.conf'})
        assert wrapper.log_file == "a.log"
Пример #6
0
def gen_command(
        plugins='', # extra python packages to load so plugins will register with dexy
        d=None,  # The directory to place generated files in, must not exist.
        t=False, # Shorter alternative to --template.
        template=DEFAULT_TEMPLATE, # The alias of the template to use.
        **kwargs # Additional kwargs passed to template's run() method.
        ):
    """
    Generate a new dexy project in the specified directory, using the template.
    """
    wrapper = init_wrapper(locals())

    if t and (template == DEFAULT_TEMPLATE):
        template = t
    elif t and template != DEFAULT_TEMPLATE:
        raise dexy.exceptions.UserFeedback("Only specify one of --t or --template, not both.")

    if not template in dexy.template.Template.plugins:
        print("Can't find a template named '%s'. Run 'dexy templates' for a list of templates." % template)
        sys.exit(1)

    template_instance = dexy.template.Template.create_instance(template)
    template_instance.generate(d, **kwargs)

    # We run dexy setup. This will respect any dexy.conf file in the template
    # but passing command line options for 'setup' to 'gen' currently not supported.
    os.chdir(d)
    wrapper.create_dexy_dirs()
    print("Success! Your new dexy project has been created in directory '%s'" % d)
    if file_exists("README"):
        print("\n--------------------------------------------------")
        with open("README", "r") as f:
            print(f.read())
        print("\n--------------------------------------------------")
        print("\nThis information is in the 'README' file for future reference.")
Пример #7
0
def grep_command(
        __cli_options=False, # nodoc
        contents=False, # print out the contents of each matched file
        expr="", # An expression partially matching document name.
        key="", # An exact document key
        keyexpr="", # Only search for keys matching this expression
        keylimit=10, # Maximum number of matching keys to print
        keys=False, # List keys in documents
        limit=10, # Maximum number of matching documents to print
        lines=False, # maximum number of lines of content to print
        **kwargs
        ):
    """
    Search for documents and sections within documents.

    Dexy must have already run successfully.

    You can search for documents based on exact key or inexpect expression. The
    number of documents returned is controlled by --limit.

    You can print all keys in found documents by requesting --keys, number of
    results is controlled by --keylimit.

    You can search the section names/keys in found documents by passing a
    --keyexpr

    You can print contents of documents by requesting --contents, number of
    lines of content can be controlled by --lines.

    This does not search contents of documents, just document names and
    internal section names.
    """

    artifactsdir = kwargs.get('artifactsdir', defaults['artifacts_dir'])
    wrapper = init_wrapper(locals())
    batch = Batch.load_most_recent(wrapper)
   
    if not batch:
        print "you need to run dexy first"
        sys.exit(1)
    else:
        if expr:
            matches = sorted([data for data in batch if expr in data.key],
                    key=attrgetter('key'))
        elif key:
            matches = sorted([data for data in batch if key == data.key],
                    key=attrgetter('key'))
        else:
            raise dexy.exceptions.UserFeedback("Must specify either expr or key")

        n = len(matches)
        if n > limit:
            print "only printing first %s of %s total matches" % (limit, n)
            matches = matches[0:limit]

        for match in matches:
            print_match(match, keys, keyexpr, contents, keylimit, lines)
Пример #8
0
def setup_command(
        __cli_options=False,
        artifactsdir=defaults['artifacts_dir'], # Where dexy should store working files.
        **kwargs):
    """
    Create the directories dexy needs to run.
    """
    wrapper = init_wrapper(locals())
    wrapper.create_dexy_dirs()
Пример #9
0
def grep_command(
        __cli_options=False,  # nodoc
        contents=False,  # print out the contents of each matched file
        expr="",  # An expression partially matching document name.
        key="",  # An exact document key
        keyexpr="",  # Only search for keys matching this expression
        keylimit=10,  # Maximum number of matching keys to print
        keys=False,  # List keys in documents
        limit=10,  # Maximum number of matching documents to print
        lines=False,  # maximum number of lines of content to print
        **kwargs):
    """
    Search for documents and sections within documents.

    Dexy must have already run successfully.

    You can search for documents based on exact key or inexpect expression. The
    number of documents returned is controlled by --limit.

    You can print all keys in found documents by requesting --keys, number of
    results is controlled by --keylimit.

    You can search the section names/keys in found documents by passing a
    --keyexpr

    You can print contents of documents by requesting --contents, number of
    lines of content can be controlled by --lines.

    This does not search contents of documents, just document names and
    internal section names.
    """

    artifactsdir = kwargs.get('artifactsdir', defaults['artifacts_dir'])
    wrapper = init_wrapper(locals())
    batch = Batch.load_most_recent(wrapper)

    if not batch:
        print "you need to run dexy first"
        sys.exit(1)
    else:
        if expr:
            matches = sorted([data for data in batch if expr in data.key],
                             key=attrgetter('key'))
        elif key:
            matches = sorted([data for data in batch if key == data.key],
                             key=attrgetter('key'))
        else:
            raise dexy.exceptions.UserFeedback(
                "Must specify either expr or key")

        n = len(matches)
        if n > limit:
            print "only printing first %s of %s total matches" % (limit, n)
            matches = matches[0:limit]

        for match in matches:
            print_match(match, keys, keyexpr, contents, keylimit, lines)
Пример #10
0
def test_kwargs_override_config_file():
    with tempdir():
        with open("dexy.conf", "w") as f:
            f.write("""{ "logfile" : "a.log" }""")

        wrapper = init_wrapper({
            '__cli_options' : { 'logfile' : 'b.log' },
            'logfile' : "b.log",
            'conf' : 'dexy.conf'
            })
        assert wrapper.log_file == "b.log"
Пример #11
0
def cleanup_command(
        __cli_options=False,
        artifactsdir=defaults['artifacts_dir'], # location of directory in which to store artifacts
        logdir=defaults['log_dir'], # DEPRECATED location of directory in which to store logs
        reports=True # Also remove report generated dirs
        ):
    """
    Remove the artifacts and logs directories.
    """
    wrapper = init_wrapper(locals())
    wrapper.remove_dexy_dirs()
    wrapper.remove_reports_dirs(reports)
Пример #12
0
def reset_command(
        __cli_options=False,
        artifactsdir=defaults['artifacts_dir'], # location of directory in which to store artifacts
        logdir=defaults['log_dir']# DEPRECATED location of directory in which to store logs
        ):
    """
    Empty the artifacts and logs directories.
    """
    wrapper = init_wrapper(locals())
    wrapper.remove_dexy_dirs()
    wrapper.remove_reports_dirs(keep_empty_dir=True)
    wrapper.create_dexy_dirs()
Пример #13
0
def reset_command(
        __cli_options=False,
        artifactsdir=defaults['artifacts_dir'], # Where dexy should store working files.
        logdir=defaults['log_dir'] # DEPRECATED
        ):
    """
    Clean out the contents of dexy's cache and reports directories.
    """
    wrapper = init_wrapper(locals())
    wrapper.remove_dexy_dirs()
    wrapper.remove_reports_dirs(keep_empty_dir=True)
    wrapper.create_dexy_dirs()
Пример #14
0
def templates_command(
        plugins='', # extra python packages to load so plugins will register with dexy
        simple=False, # Only print template names, without docstring or headers.
        validate=False, # Intended for developer use only, validate templates (runs and checks each template).
        key=False # Only print information which matches this search key.
        ):
    """
    List templates that can be used to generate new projects.
    """
    init_wrapper(locals())

    if not simple:
        FMT = "%-40s %s"
        print FMT % ("Alias", "Info")

    for i, template in enumerate(dexy.template.Template):
        if key:
            if not key in template.alias:
                continue

        if template.setting('nodoc'):
            continue

        if simple:
            print template.alias
        else:
            first_line_help = template.setting('help').splitlines()[0].strip()
            print FMT % (template.alias, first_line_help),
            if validate:
                print " validating...",
                print template.validate() and "OK" or "ERROR"
            else:
                print ''
    
    if i < 5:
        print "Run '[sudo] pip install dexy-templates' to install some more templates."

    if not simple:
        print "Run 'dexy help -on gen' for help on generating projects from templates."
Пример #15
0
def templates_command(
    plugins='',  # extra python packages to load so plugins will register with dexy
    simple=False,  # Only print template names, without docstring or headers.
    validate=False,  # Intended for developer use only, validate templates (runs and checks each template).
    key=False  # Only print information which matches this search key.
):
    """
    List templates that can be used to generate new projects.
    """
    init_wrapper(locals())

    if not simple:
        FMT = "%-40s %s"
        print FMT % ("Alias", "Info")

    for i, template in enumerate(dexy.template.Template):
        if key:
            if not key in template.alias:
                continue

        if template.setting('nodoc'):
            continue

        if simple:
            print template.alias
        else:
            first_line_help = template.setting('help').splitlines()[0].strip()
            print FMT % (template.alias, first_line_help),
            if validate:
                print " validating...",
                print template.validate() and "OK" or "ERROR"
            else:
                print ''

    if i < 5:
        print "Run '[sudo] pip install dexy-templates' to install some more templates."

    if not simple:
        print "Run 'dexy help -on gen' for help on generating projects from templates."
Пример #16
0
def test_kwargs_override_config_file():
    with tempdir():
        with open("dexy.conf", "w") as f:
            f.write("""{ "logfile" : "a.log" }""")

        wrapper = init_wrapper({
            '__cli_options': {
                'logfile': 'b.log'
            },
            'logfile': "b.log",
            'conf': 'dexy.conf'
        })
        assert wrapper.log_file == "b.log"
Пример #17
0
def cleanup_command(
        __cli_options=False,
        artifactsdir=defaults['artifacts_dir'], # Where dexy should store working files.
        logdir=defaults['log_dir'], # DEPRECATED
        reports=True # Whether directories generated by reports should also be removed.
        ):
    """
    Remove the directories which dexy created, including working directories
    and reports.
    """
    wrapper = init_wrapper(locals())
    wrapper.remove_dexy_dirs()
    wrapper.remove_reports_dirs(reports)
Пример #18
0
def reset_command(
    __cli_options=False,
    artifactsdir=defaults[
        'artifacts_dir'],  # location of directory in which to store artifacts
    logdir=defaults[
        'log_dir']  # DEPRECATED location of directory in which to store logs
):
    """
    Empty the artifacts and logs directories.
    """
    wrapper = init_wrapper(locals())
    wrapper.remove_dexy_dirs()
    wrapper.remove_reports_dirs(keep_empty_dir=True)
    wrapper.create_dexy_dirs()
Пример #19
0
def cleanup_command(
    __cli_options=False,
    artifactsdir=defaults[
        'artifacts_dir'],  # location of directory in which to store artifacts
    logdir=defaults[
        'log_dir'],  # DEPRECATED location of directory in which to store logs
    reports=True  # Also remove report generated dirs
):
    """
    Remove the artifacts and logs directories.
    """
    wrapper = init_wrapper(locals())
    wrapper.remove_dexy_dirs()
    wrapper.remove_reports_dirs(reports)
Пример #20
0
def links_command(
        **kwargs
        ):
    """
    Print list of links and sections found in dexy documents.
    """
    artifactsdir = kwargs.get('artifactsdir', defaults['artifacts_dir'])
    wrapper = init_wrapper(locals())
    batch = Batch.load_most_recent(wrapper)

    if not batch:
        print "you need to run dexy first"
        sys.exit(1)

    wrapper.setup_log()
    wrapper.batch = batch

    wrapper.add_lookups()

    if wrapper.lookup_nodes:
        print_indented("Nodes:")
    for label in sorted(wrapper.lookup_nodes):
        nodes = wrapper.lookup_nodes[label]
        if len(nodes) > 1:
            print ''
            print_indented("'%s'" % label, 2)
            print_indented("Multiple nodes match %s:" % label, 4)
            for node in nodes:
                print_indented(">> %r" % node, 6)
        elif len(nodes) == 0:
            print_indented("'%s'" % label, 2)
            print_indented("NO nodes match %s" % label, 4)
        else:
            node = nodes[0]
            print_indented("'%s'" % label, 2)
            print_indented("%r" % node, 4)
        print ''

    print ''

    if wrapper.lookup_sections:
        print_indented("Sections:")
    for label in sorted(wrapper.lookup_sections):
        node = wrapper.lookup_sections[label][0]
        print_indented("'%s'" % label, 2)
        print_indented("%r" % node, 4)
        print ''
Пример #21
0
def info_command(
    __cli_options=False,
    expr="",  # The doc key to query. Use dexy grep to search doc keys.
    key="",  # The doc key to match exactly. Use dexy grep to search doc keys.
    artifactsdir=defaults[
        'artifacts_dir'],  # location of directory in which to store artifacts
    logdir=defaults['log_dir']  # location of directory in which to store logs
):
    wrapper = init_wrapper(locals())
    batch = Batch.load_most_recent(wrapper)

    if expr:
        matches = sorted([data for data in batch if expr in data.key],
                         key=attrgetter('key'))
        print "search expr:", expr
    elif key:
        matches = sorted([data for data in batch if key == data.key],
                         key=attrgetter('key'))
    else:
        raise dexy.exceptions.UserFeedback("Must specify either expr or key")

    for match in matches:
        print
        print "  doc key:", match.key

        print "    data attributes:"
        for fname in sorted(INFO_ATTRS):
            print "      %s: %s" % (fname, getattr(match, fname))
        print

        print "    data methods:"
        for fname in sorted(INFO_METHODS):
            print "      %s(): %s" % (fname, getattr(match, fname)())
        print

        print "    storage methods:"
        for fname in sorted(STORAGE_METHODS):
            print "      %s(): %s" % (fname, getattr(match.storage, fname)())
        print
Пример #22
0
def grep_command(
    __cli_options=False,  # nodoc
    expr="",  # The expression to search for
    key="",  # The exact document key to search for
    keyexpr="",  # Only search for keys matching this expression, implies keys=True
    keys=False,  # if True, try to list the keys in any found files
    keylimit=10,  # maximum number of matching keys to print
    limit=10,  # maximum number of matching records to print
    contents=False,  # print out the contents of each matched file
    lines=False,  # maximum number of lines of content to print
    artifactsdir=defaults[
        'artifacts_dir'],  # location of directory in which to store artifacts
    logdir=defaults['log_dir']  # location of directory in which to store logs
):
    """
    Search for a Dexy document in the previously run batch. Prints out document
    keys which include the expression.
    """
    wrapper = init_wrapper(locals())
    batch = Batch.load_most_recent(wrapper)

    def print_keys(pkeys):
        n = len(pkeys)
        if n > keylimit:
            pkeys = pkeys[0:keylimit]

        for key in pkeys:
            print '  ', key

        if n > keylimit:
            print "  only printed first %s of %s total keys" % (keylimit, n)

    def print_contents(text):
        text_lines = text.splitlines()
        for i, line in enumerate(text_lines):
            if lines and i > lines - 1:
                continue
            print "  ", line

        if lines and lines < len(text_lines):
            print "   only printed first %s of %s total lines" % (
                lines, len(text_lines))

    def print_match(match):
        print match.key, "\tcache key:", match.storage_key

        if hasattr(match, 'keys'):
            if keyexpr:
                print_keys([key for key in match.keys() if keyexpr in key])
            elif keys:
                print_keys(match.keys())

        if contents:
            if isinstance(match, Sectioned):
                for section_name, section_contents in match.data().iteritems():
                    print "  section: %s" % section_name
                    print
                    print_contents(section_contents)
                    print
            elif isinstance(match, KeyValue):
                pass
            elif isinstance(match, Generic):
                try:
                    json.dumps(unicode(match))
                    print_contents(unicode(match))
                except UnicodeDecodeError:
                    print "  not printable"

    def print_matches(matches):
        for match in matches:
            print_match(match)

    if not batch:
        print "you need to run dexy first"
    else:
        if expr:
            matches = sorted([data for data in batch if expr in data.key],
                             key=attrgetter('key'))
        elif key:
            matches = sorted([data for data in batch if key == data.key],
                             key=attrgetter('key'))
        else:
            raise dexy.exceptions.UserFeedback(
                "Must specify either expr or key")

        n = len(matches)
        if n > limit:
            print_matches(matches[0:limit])
            print "only printed first %s of %s total matches" % (limit, n)
        else:
            print_matches(matches)
Пример #23
0
def dexy_command(
    __cli_options=False,
    artifactsdir=defaults[
        'artifacts_dir'],  # location of directory in which to store artifacts
    conf=defaults['config_file'],  # name to use for configuration file
    configs=defaults['configs'],  # list of doc config files to parse
    debug=defaults['debug'],  # Prints stack traces, other debug stuff.
    directory=defaults['directory'],  # Allow processing just a subdirectory.
    dryrun=defaults[
        'dry_run'],  # if True, just parse config and print batch info, don't run dexyT
    encoding=defaults[
        'encoding'],  # Default encoding. Set to 'chardet' to use chardet auto detection.
    exclude=defaults[
        'exclude'],  # comma-separated list of directory names to exclude from dexy processing
    excludealso=defaults[
        'exclude_also'],  # comma-separated list of directory names to exclude from dexy processing
    full=defaults[
        'full'],  # Whether to do a full run including tasks marked default: False
    globals=defaults[
        'globals'],  # global values to make available within dexy documents, should be KEY=VALUE pairs separated by spaces
    help=False,  #nodoc
    h=False,  #nodoc
    hashfunction=defaults[
        'hashfunction'],  # What hash function to use, set to crc32 or adler32 for more speed but less reliability
    include=defaults[
        'include'],  # Locations to include which would normally be excluded.
    logdir=defaults['log_dir'],  # DEPRECATED
    logfile=defaults['log_file'],  # name of log file
    logformat=defaults['log_format'],  # format of log entries
    loglevel=defaults[
        'log_level'],  # log level, valid options are DEBUG, INFO, WARN
    nocache=defaults[
        'dont_use_cache'],  # whether to force dexy not to use files from the cache
    noreports=False,  # if true, don't run any reports
    outputroot=defaults[
        'output_root'],  # Subdirectory to use as root for output
    pickle=defaults[
        'pickle'],  # library to use for persisting info to disk, may be 'c', 'py', 'json'
    plugins=defaults[
        'plugins'],  # additional python packages containing dexy plugins
    profile=defaults[
        'profile'],  # whether to run with cProfile. Arg can be a boolean, in which case profile saved to 'dexy.prof', or a filename to save to.
    r=False,  # whether to clear cache before running dexy
    recurse=defaults[
        'recurse'],  # whether to include doc config files in subdirectories
    reports=defaults[
        'reports'],  # reports to be run after dexy runs, enclose in quotes and separate with spaces
    reset=False,  # whether to clear cache before running dexy
    silent=defaults[
        'silent'],  # Whether to not print any output when running dexy
    strace=defaults['strace'],  # Run dexy using strace (VERY slow)
    uselocals=defaults[
        'uselocals'],  # use cached local copies of remote URLs, faster but might not be up to date, 304 from server will override this setting
    target=defaults[
        'target'],  # Which target to run. By default all targets are run, this allows you to run only 1 bundle (and its dependencies).
    version=False,  # For people who type -version out of habit
    writeanywhere=defaults[
        'writeanywhere']  # Whether dexy can write files outside of the dexy project root.
):
    """
    Runs Dexy.
    """
    if h or help:
        return dexy.commands.help_command()

    if version:
        return dexy.commands.version_command()

    if r or reset:
        dexy.commands.dirs.reset_command(artifactsdir=artifactsdir,
                                         logdir=logdir)

    if silent:
        print "sorry, -silent option not implemented yet https://github.com/ananelson/dexy/issues/33"

    wrapper = init_wrapper(locals())
    wrapper.assert_dexy_dirs_exist()
    run_reports = (not noreports)

    try:
        if profile:
            run_dexy_in_profiler(wrapper, profile)

        elif strace:
            run_dexy_in_strace(wrapper, strace)
            run_reports = False

        else:
            start = time.time()
            wrapper.run_from_new()
            elapsed = time.time() - start
            print "dexy run finished in %0.3f%s" % (elapsed,
                                                    wrapper.state_message())

    except dexy.exceptions.UserFeedback as e:
        handle_user_feedback_exception(wrapper, e)

    except KeyboardInterrupt:
        handle_keyboard_interrupt()

    except Exception as e:
        log_and_print_exception(wrapper, e)
        raise

    if run_reports and hasattr(wrapper, 'batch'):
        start_time = time.time()
        wrapper.report()
        print "dexy reports finished in %0.3f" % (time.time() - start_time)
Пример #24
0
def setup_command(__cli_options=False, **kwargs):
    """
    Create the directories dexy needs to run. This helps make sure you mean to run dexy in this directory.
    """
    wrapper = init_wrapper(locals())
    wrapper.create_dexy_dirs()
Пример #25
0
def grep_command(
        __cli_options=False, # nodoc
        expr="", # The expression to search for
        key="", # The exact document key to search for
        keyexpr="", # Only search for keys matching this expression, implies keys=True
        keys=False, # if True, try to list the keys in any found files
        keylimit=10, # maximum number of matching keys to print
        limit=10, # maximum number of matching records to print
        contents=False, # print out the contents of each matched file
        lines=False, # maximum number of lines of content to print
        artifactsdir=defaults['artifacts_dir'], # location of directory in which to store artifacts
        logdir=defaults['log_dir'] # location of directory in which to store logs
        ):
    """
    Search for a Dexy document in the previously run batch. Prints out document
    keys which include the expression.
    """
    wrapper = init_wrapper(locals())
    batch = Batch.load_most_recent(wrapper)

    def print_keys(pkeys):
        n = len(pkeys)
        if n > keylimit:
            pkeys = pkeys[0:keylimit]
        
        for key in pkeys:
            print '  ', key

        if n > keylimit:
            print "  only printed first %s of %s total keys" % (keylimit, n)

    def print_contents(text):
        text_lines = text.splitlines()
        for i, line in enumerate(text_lines):
            if lines and i > lines-1:
                continue
            print "  ", line

        if lines and lines < len(text_lines):
            print "   only printed first %s of %s total lines" % (lines, len(text_lines))

    def print_match(match):
        print match.key, "\tcache key:", match.storage_key

        if hasattr(match, 'keys'):
            if keyexpr:
                print_keys([key for key in match.keys() if keyexpr in key])
            elif keys:
                print_keys(match.keys())

        if contents:
            if isinstance(match, Sectioned):
                for section_name, section_contents in match.data().iteritems():
                    print "  section: %s" % section_name
                    print
                    print_contents(section_contents)
                    print
            elif isinstance(match, KeyValue):
                pass
            elif isinstance(match, Generic):
                try:
                    json.dumps(unicode(match))
                    print_contents(unicode(match))
                except UnicodeDecodeError:
                    print "  not printable"

    def print_matches(matches):
        for match in matches:
            print_match(match)
   
    if not batch:
        print "you need to run dexy first"
    else:
        if expr:
            matches = sorted([data for data in batch if expr in data.key], key=attrgetter('key'))
        elif key:
            matches = sorted([data for data in batch if key == data.key], key=attrgetter('key'))
        else:
            raise dexy.exceptions.UserFeedback("Must specify either expr or key")

        n = len(matches)
        if n > limit:
            print_matches(matches[0:limit])
            print "only printed first %s of %s total matches" % (limit, n)
        else:
            print_matches(matches)
Пример #26
0
def info_command(
        __cli_options=False,
        expr="", # An expression partially matching document name.
        key="", # The exact document key.
        ws=False, # Whether to print website reporter keys and values.
        **kwargs
        ):
    """
    Prints metadata about a dexy document.

    Dexy must have already run successfully.

    You can specify an exact document key or an expression which matches part
    of a document name/key. The `dexy grep` command is available to help you
    search for documents and print document contents.
    """
    artifactsdir = kwargs.get('artifactsdir', defaults['artifacts_dir'])
    wrapper = init_wrapper(locals())
    wrapper.setup_log()
    batch = Batch.load_most_recent(wrapper)
    wrapper.batch = batch

    if expr:
        print "search expr:", expr
        matches = sorted([data for data in batch if expr in data.key],
                key=attrgetter('key'))
    elif key:
        matches = sorted([data for data in batch if key == data.key],
                key=attrgetter('key'))
    else:
        raise dexy.exceptions.UserFeedback("Must specify either expr or key")

    for match in matches:
        print ""
        print "  Info for Document '%s'" % match.key
        print ""
        print "  document output data type:", match.alias
        print ""

        print_indented("settings:", 2)
        for k in sorted(match._instance_settings):
            if not k in ('aliases', 'help'):
                print_indented("%s: %s" % (k, match.setting(k)), 4)

        print ""
        print_indented("attributes:", 2)
        for fname in sorted(info_attrs):
            print_indented("%s: %s" % (fname, getattr(match, fname)), 4)
        print ""
    
        print_indented("methods:", 2)
        for fname in sorted(info_methods):
            print_indented("%s(): %s" % (fname, getattr(match, fname)()), 4)
        print ""

        if storage_methods:
            print_indented("storage methods:", 2)
            for fname in sorted(storage_methods):
                print_indented("%s(): %s" % (fname, getattr(match.storage, fname)), 4)
            print ''

        if ws:
            print_indented("website reporter methods:", 2)
            print ''
            reporter = dexy.reporter.Reporter.create_instance('ws')
            reporter.wrapper = wrapper
            reporter.setup_navobj()
            reporter.help(match)
            print ''
            print_indented("active template plugins are:", 2)
            print_indented(", ".join(reporter.setting('plugins')), 4)
            print ''


        else:
            print_indented("For website reporter tags, run this command with -ws option", 4)
            print ''


        print_rewrapped("""For more information about methods available on this
        data type run `dexy datas -alias %s`""" % match.alias)
Пример #27
0
def setup_command(__cli_options=False, **kwargs):
    """
    Create the directories dexy needs to run. This helps make sure you mean to run dexy in this directory.
    """
    wrapper = init_wrapper(locals())
    wrapper.create_dexy_dirs()
Пример #28
0
def dexy_command(
        __cli_options=False,
        artifactsdir=defaults['artifacts_dir'], # location of directory in which to store artifacts
        conf=defaults['config_file'], # name to use for configuration file
        configs=defaults['configs'], # list of doc config files to parse
        debug=defaults['debug'], # Prints stack traces, other debug stuff.
        dryrun=defaults['dry_run'], # if True, just parse config and print batch info, don't run dexy
        encoding=defaults['encoding'], # Default encoding. Set to 'chardet' to use chardet auto detection.
        exclude=defaults['exclude'], # comma-separated list of directory names to exclude from dexy processing
        excludealso=defaults['exclude_also'], # comma-separated list of directory names to exclude from dexy processing
        full=defaults['full'], # Whether to do a full run including tasks marked default: False
        globals=defaults['globals'], # global values to make available within dexy documents, should be KEY=VALUE pairs separated by spaces
        help=False, #nodoc
        h=False, #nodoc
        hashfunction=defaults['hashfunction'], # What hash function to use, set to crc32 or adler32 for more speed but less reliability
        include=defaults['include'], # Locations to include which would normally be excluded.
        logdir=defaults['log_dir'], # location of directory in which to store logs
        logfile=defaults['log_file'], # name of log file
        logformat=defaults['log_format'], # format of log entries
        loglevel=defaults['log_level'], # log level, valid options are DEBUG, INFO, WARN
        nocache=defaults['dont_use_cache'], # whether to force dexy not to use files from the cache
        noreports=False, # if true, don't run any reports
        outputroot=defaults['output_root'], # Subdirectory to use as root for output
        pickle=defaults['pickle'], # library to use for persisting info to disk, may be 'c', 'py', 'json'
        plugins=defaults['plugins'], # additional python packages containing dexy plugins
        profile=defaults['profile'], # whether to run with cProfile. Arg can be a boolean, in which case profile saved to 'dexy.prof', or a filename to save to.
        r=False, # whether to clear cache before running dexy
        recurse=defaults['recurse'], # whether to include doc config files in subdirectories
        reports=defaults['reports'], # reports to be run after dexy runs, enclose in quotes and separate with spaces
        reset=False, # whether to clear cache before running dexy
        silent=defaults['silent'], # Whether to not print any output when running dexy
        strace=defaults['strace'], # Run dexy using strace (VERY slow)
        uselocals=defaults['uselocals'], # use cached local copies of remote URLs, faster but might not be up to date, 304 from server will override this setting
        target=defaults['target'], # Which target to run. By default all targets are run, this allows you to run only 1 bundle (and its dependencies).
        version=False # For people who type -version out of habit
    ):
    """
    Runs Dexy.
    """
    if h or help:
        return dexy.commands.help_command()

    if version:
        return dexy.commands.version_command()

    if r or reset:
        dexy.commands.dirs.reset_command(artifactsdir=artifactsdir, logdir=logdir)

    if silent:
        print "sorry, -silent option not implemented yet https://github.com/ananelson/dexy/issues/33"

    # Don't trap errors yet because error handling uses wrapper instance.
    wrapper = init_wrapper(locals())

    # TODO make this error nicer..
    wrapper.assert_dexy_dirs_exist()

    run_reports = (not noreports)

    try:
        if profile:
            run_dexy_in_profiler(wrapper, profile)
        elif strace:
            run_dexy_in_strace(wrapper, strace)
            run_reports = False
        else:
            start = time.time()
            wrapper.run_from_new()
            elapsed = time.time() - start
            print "dexy run finished in %0.3f%s" % (elapsed, wrapper.state_message())

    except dexy.exceptions.UserFeedback as e:
        handle_user_feedback_exception(wrapper, e)
    except KeyboardInterrupt:
        handle_keyboard_interrupt()
    except Exception as e:
        log_and_print_exception(wrapper, e)
        raise

    if run_reports and hasattr(wrapper, 'batch'):
        start_time = time.time()
        wrapper.report()
        print "dexy reports finished in %0.3f" % (time.time() - start_time)
Пример #29
0
from dexy.commands.utils import init_wrapper
from operator import attrgetter
from dexy.batch import Batch

urls = (
        '/favicon.ico', 'favicon',
        '/doc/(.*)', 'document',
        '/raw/(.*)', 'raw',
        '/snip/(.*)/(.*)', 'snippet',
        '/(.*)/(.*)', 'grep',
        '/(.*)', 'grep'
        )

render = web.template.render(os.path.join(os.path.abspath(os.path.dirname(__file__)), 'templates'))

wrapper = init_wrapper({})

def wrap_content(content, ext):
    # Add other extensions here with special handling needs. Default is wrapped in <pre> tags.
    if ext == ".html":
        return content
    else:
        return "<pre>\n%s\n</pre>" % content

class favicon:
    def GET(self):
        return ''

class grep:
    def GET(self, expr, keyexpr=None):
        batch = Batch.load_most_recent(wrapper)
Пример #30
0
def serve_command(
        port=-1,
        reporters=['ws', 'output'
                   ],  # Reporters whose output to try to serve (in order).
        username='',  # http auth username to use (if provided)
        password='',  # http auth password to use (if provided)
        realm='Dexy',  # http auth realm to use (if username and password are provided)
        directory=False,  # Custom directory to be served.
        **kwargs):
    """
    Runs a simple web server on dexy-generated files.
    
    Will look first to see if the Website Reporter has run, if so this content
    is served. If not the standard output/ directory contents are served. You
    can also specify another directory to be served. The port defaults to 8085,
    this can also be customized. If a username and password are provided, uses
    HTTP auth to access pages.
    """

    if not directory:
        wrapper = init_wrapper(locals(), True)

        for alias in reporters:
            report_dir = dexy.reporter.Reporter.create_instance(alias).setting(
                "dir")
            print "report dir", report_dir
            if report_dir and file_exists(report_dir):
                directory = report_dir
                break

    if not directory:
        print NO_OUTPUT_MSG
        sys.exit(1)

    os.chdir(directory)

    if port < 0:
        ports = range(8085, 8100)
    else:
        ports = [port]

    p = None
    for p in ports:
        try:
            if username and password:
                import base64
                authcode = base64.b64encode("%s:%s" % (username, password))
                Handler = SimpleHTTPAuthRequestHandler
                Handler.authcode = authcode
                Handler.realm = realm
            else:
                Handler = SimpleHTTPServer.SimpleHTTPRequestHandler
            httpd = SocketServer.TCPServer(("", p), Handler)
        except socket.error:
            print "port %s already in use" % p
            p = None
        else:
            break

    if p:
        print "serving contents of %s on http://localhost:%s" % (directory, p)
        if username and password and Handler.authcode:
            print "username '%s' and password '%s' are required to access contents" % (
                username, password)
        print "type ctrl+c to stop"
        try:
            httpd.serve_forever()
        except KeyboardInterrupt:
            sys.exit(1)
    else:
        print "could not find a free port to serve on, tried", ports
        sys.exit(1)
Пример #31
0
def serve_command(
        port=-1,
        reporters=['ws', 'output'], # Reporters whose output to try to serve (in order).
        username='', # http auth username to use (if provided)
        password='', # http auth password to use (if provided)
        realm='Dexy', # http auth realm to use (if username and password are provided)
        directory=False, # Custom directory to be served.
        **kwargs
        ):
    """
    Runs a simple web server on dexy-generated files.
    
    Will look first to see if the Website Reporter has run, if so this content
    is served. If not the standard output/ directory contents are served. You
    can also specify another directory to be served. The port defaults to 8085,
    this can also be customized. If a username and password are provided, uses
    HTTP auth to access pages.
    """

    if not directory:
        wrapper = init_wrapper(locals(), True)

        for alias in reporters:
            report_dir = dexy.reporter.Reporter.create_instance(alias).setting("dir")
            print "report dir", report_dir
            if report_dir and file_exists(report_dir):
                directory = report_dir
                break

    if not directory:
        print NO_OUTPUT_MSG
        sys.exit(1)

    os.chdir(directory)

    if port < 0:
        ports = range(8085, 8100)
    else:
        ports = [port]

    p = None
    for p in ports:
        try:
            if username and password:
                import base64
                authcode = base64.b64encode("%s:%s" % (username, password))
                Handler = SimpleHTTPAuthRequestHandler
                Handler.authcode = authcode
                Handler.realm = realm
            else:
                Handler = SimpleHTTPServer.SimpleHTTPRequestHandler
            httpd = SocketServer.TCPServer(("", p), Handler)
        except socket.error:
            print "port %s already in use" % p
            p = None
        else:
            break

    if p:
        print "serving contents of %s on http://localhost:%s" % (directory, p)
        if username and password and Handler.authcode:
            print "username '%s' and password '%s' are required to access contents" % (username, password)
        print "type ctrl+c to stop"
        try:
            httpd.serve_forever()
        except KeyboardInterrupt:
            sys.exit(1)
    else:
        print "could not find a free port to serve on, tried", ports
        sys.exit(1)