<commit_before>#!!REZ_PYTHON_BINARY!

import os
import sys
import inspect
import argparse
import pkgutil
import rez.commands

def subpackages(packagemod):
    """
    Given a module object, returns an iterator which yields a tuple (modulename, moduleobject, ispkg)
    for the given module and all it's submodules/subpackages.
    """
    modpkgs = []
    modpkgs_names = set()
    if hasattr(packagemod, '__path__'):
        for importer, modname, ispkg in pkgutil.walk_packages(packagemod.__path__,
                                                              packagemod.__name__+'.'):
            if modname not in sys.modules:
                try:
                    mod = importer.find_module(modname).load_module(modname)
                except Exception, e:
                    print "error importing %s: %s" %  ( modname, e)
            else:
                mod = sys.modules[modname]
            yield modname, mod, ispkg
    else:
        yield packagemod.__name__, packagemod, False

def main():
    parser = argparse.ArgumentParser("rez")
    subparsers = parser.add_subparsers(title='subcommands')
    for name, mod, ispkg in subpackages(rez.commands):
        assert mod.__doc__, "command module %s must have a module-level docstring (used as the command help)" % name
        assert hasattr(mod, 'command'), "command module %s must provide a command() function" % name
        assert hasattr(mod, 'setup_parser'), "command module %s  must provide a setup_parser() function" % name

        brief = mod.__doc__.strip('\n').split('\n')[0]
        cmdname = name.split('.')[-1].replace('_', '-')
        subparser = subparsers.add_parser(cmdname,
                                          description=mod.__doc__,
                                          help=brief)
        mod.setup_parser(subparser)
        subparser.set_defaults(func=mod.command)<commit_msg>Support sub-commands of arbitrary depth<commit_after>#!!REZ_PYTHON_BINARY!

import os
import sys
import inspect
import argparse
import pkgutil
import rez.commands

def subpackages(packagemod):
    """
    Given a module object, returns an iterator which yields a tuple (modulename, moduleobject, ispkg)
    for the given module and all it's submodules/subpackages.
    """
    modpkgs = []
    modpkgs_names = set()
    if hasattr(packagemod, '__path__'):
        yield packagemod.__name__, packagemod, True
        for importer, modname, ispkg in pkgutil.walk_packages(packagemod.__path__,
                                                              packagemod.__name__+'.'):
            if modname not in sys.modules:
                try:
                    mod = importer.find_module(modname).load_module(modname)
                except Exception, e:
                    print "error importing %s: %s" %  ( modname, e)
            else:
                mod = sys.modules[modname]
            yield modname, mod, ispkg
    else:
        yield packagemod.__name__, packagemod, False

def main():
    parser = argparse.ArgumentParser("rez")
    subparsers = []
    parents = []
    for name, mod, ispkg in subpackages(rez.commands):
        cmdname = name.split('.')[-1].replace('_', '-')
        if ispkg:
            if cmdname == 'commands':
                title = 'commands'
            else:
                title = (cmdname + ' subcommands')
            subparsers.append(parser.add_subparsers(title=title))
            parents.append(name)
            continue
        elif not name.startswith(parents[-1]):
            parents.pop()
            subparsers.pop()
        assert mod.__doc__, "command module %s must have a module-level docstring (used as the command help)" % name
        assert hasattr(mod, 'command'), "command module %s must provide a command() function" % name
        assert hasattr(mod, 'setup_parser'), "command module %s  must provide a setup_parser() function" % name

        brief = mod.__doc__.strip('\n').split('\n')[0]
        subparser = subparsers[-1].add_parser(cmdname,
                                              description=mod.__doc__,
                                              help=brief)
        mod.setup_parser(subparser)
        subparser.set_defaults(func=mod.command)
{'fraction_non_alphanumeric': 0.10054670786783931, 'fraction_numerical': 0.0014261944378416924, 'mean_word_length': 2.0514865844815082, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 12, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1616061', 'n_tokens_mistral': 1142, 'n_tokens_neox': 1125, 'n_words': 310}