<commit_before>import sys
import os

from toydist.core.utils import \
        pprint
from toydist.core.platforms import \
        get_scheme
from toydist.core import \
        PackageOptions, PackageDescription

from toydist.commands.core import \
        Command, UsageException, SCRIPT_NAME

class ConfigureState(object):
    def __init__(self):
        self.flags = {}
        self.paths = {}

    def dump(self, file='.config.bin'):
        import cPickle
        f = open(file, 'wb')
        try:
            str = cPickle.dumps(self)
            f.write(str)
        finally:
            f.close()

    @classmethod
    def from_dump(cls, file='.config.bin'):
        import cPickle
        f = open(file, 'rb')
        try:
            str = f.read()
            return cPickle.loads(str)
        finally:
            f.close()

def ensure_info_exists(opts):
    if len(opts) < 1 or opts[-1].startswith('-'):
        msg = "%s: Error: No toysetup.info found, and no toydist " \
              "configuration file given at the command line." % SCRIPT_NAME
        msg += "\nTry: %s help configure" % SCRIPT_NAME
        raise UsageException(msg)
    else:
        filename = opts[-1]
        if not os.path.exists(filename):
            msg = "%s: Error: configuration file %s not found." % \
                    (SCRIPT_NAME, filename)
            raise UsageException(msg)

    return filename

def set_scheme_options(scheme, options):
    """Set path variables given in options in scheme dictionary."""
    for k in scheme:
        if hasattr(options, k):
            val = getattr(options, k)
            if val:
                scheme[k] = val
    # XXX: define default somewhere and stick with it
    if options.prefix is not None and options.exec_prefix is None:
        scheme["eprefix"] = scheme["prefix"]

def set_flag_options(flag_opts, options):
    """Set flag variables given in options in flag dictionary."""
    # FIXME: fix this mess
    flag_vals = {}
    for k in flag_opts:
        opt_name = "with_" + k
        if hasattr(options, opt_name):
            val = getattr(options, opt_name)
            if val:
                if val == "true":
                    flag_vals[k] = True
                elif val == "false":
                    flag_vals[k] = False
                else:
                    msg = """Error: %s: option %s expects a true or false argument"""
                    raise UsageException(msg % (SCRIPT_NAME, "--with-%s" % k))

    return flag_vals

class ConfigureCommand(Command):
    long_descr = """\
Purpose: configure the project
Usage: toymaker configure [OPTIONS] [package description file]."""
    short_descr = "configure the project."
    def run(self, opts):

        # We need to obtain the package description ASAP, as we need to parse
        # it to get the options (i.e. we cannot use the option handling mechanism).
        candidate = "toysetup.info"
        if len(opts) < 1 or opts[-1].startswith('-'):
            pass
        else:
            candidate = opts[-1]

        if os.path.exists(candidate):
            filename = candidate
        else:
            filename = ensure_info_exists(opts)

        # As the configure command line handling is customized from
        # the toysetup.info (flags, paths variables), we cannot just
        # call set_options_parser.
        pkg_opts = PackageOptions.from_file(filename)
        scheme, flag_opts = self.add_configuration_options(pkg_opts)

        self.set_option_parser()
        o, a = self.parser.parse_args(opts)
        if o.help:
            self.parser.print_help()
            return

        set_scheme_options(scheme, o)
        flag_vals = set_flag_options(flag_opts, o)

        # Cache the built package description to avoid reparsing it for
        # subsequent commands
        pkg = PackageDescription.from_file(filename, flag_vals)

        s = ConfigureState()
        s.paths = scheme
        s.flags = flag_vals
        s.package_description = filename
        s.pkg = pkg
        s.dump()
        pprint('GREEN', "Writing configuration state in file %s" % '.config.bin')<commit_msg>ENH: remove useless print message in configure.<commit_after>import sys
import os

from toydist.core.utils import \
        pprint
from toydist.core.platforms import \
        get_scheme
from toydist.core import \
        PackageOptions, PackageDescription

from toydist.commands.core import \
        Command, UsageException, SCRIPT_NAME

class ConfigureState(object):
    def __init__(self):
        self.flags = {}
        self.paths = {}

    def dump(self, file='.config.bin'):
        import cPickle
        f = open(file, 'wb')
        try:
            str = cPickle.dumps(self)
            f.write(str)
        finally:
            f.close()

    @classmethod
    def from_dump(cls, file='.config.bin'):
        import cPickle
        f = open(file, 'rb')
        try:
            str = f.read()
            return cPickle.loads(str)
        finally:
            f.close()

def ensure_info_exists(opts):
    if len(opts) < 1 or opts[-1].startswith('-'):
        msg = "%s: Error: No toysetup.info found, and no toydist " \
              "configuration file given at the command line." % SCRIPT_NAME
        msg += "\nTry: %s help configure" % SCRIPT_NAME
        raise UsageException(msg)
    else:
        filename = opts[-1]
        if not os.path.exists(filename):
            msg = "%s: Error: configuration file %s not found." % \
                    (SCRIPT_NAME, filename)
            raise UsageException(msg)

    return filename

def set_scheme_options(scheme, options):
    """Set path variables given in options in scheme dictionary."""
    for k in scheme:
        if hasattr(options, k):
            val = getattr(options, k)
            if val:
                scheme[k] = val
    # XXX: define default somewhere and stick with it
    if options.prefix is not None and options.exec_prefix is None:
        scheme["eprefix"] = scheme["prefix"]

def set_flag_options(flag_opts, options):
    """Set flag variables given in options in flag dictionary."""
    # FIXME: fix this mess
    flag_vals = {}
    for k in flag_opts:
        opt_name = "with_" + k
        if hasattr(options, opt_name):
            val = getattr(options, opt_name)
            if val:
                if val == "true":
                    flag_vals[k] = True
                elif val == "false":
                    flag_vals[k] = False
                else:
                    msg = """Error: %s: option %s expects a true or false argument"""
                    raise UsageException(msg % (SCRIPT_NAME, "--with-%s" % k))

    return flag_vals

class ConfigureCommand(Command):
    long_descr = """\
Purpose: configure the project
Usage: toymaker configure [OPTIONS] [package description file]."""
    short_descr = "configure the project."
    def run(self, opts):

        # We need to obtain the package description ASAP, as we need to parse
        # it to get the options (i.e. we cannot use the option handling mechanism).
        candidate = "toysetup.info"
        if len(opts) < 1 or opts[-1].startswith('-'):
            pass
        else:
            candidate = opts[-1]

        if os.path.exists(candidate):
            filename = candidate
        else:
            filename = ensure_info_exists(opts)

        # As the configure command line handling is customized from
        # the toysetup.info (flags, paths variables), we cannot just
        # call set_options_parser.
        pkg_opts = PackageOptions.from_file(filename)
        scheme, flag_opts = self.add_configuration_options(pkg_opts)

        self.set_option_parser()
        o, a = self.parser.parse_args(opts)
        if o.help:
            self.parser.print_help()
            return

        set_scheme_options(scheme, o)
        flag_vals = set_flag_options(flag_opts, o)

        # Cache the built package description to avoid reparsing it for
        # subsequent commands
        pkg = PackageDescription.from_file(filename, flag_vals)

        s = ConfigureState()
        s.paths = scheme
        s.flags = flag_vals
        s.package_description = filename
        s.pkg = pkg
        s.dump()
{'fraction_non_alphanumeric': 0.09588206627680312, 'fraction_numerical': 0.0014619883040935672, 'mean_word_length': 1.9883509282854022, 'pattern_counts': {'":': 4, '<': 7, '<?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': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5191192', 'n_tokens_mistral': 2355, 'n_tokens_neox': 2276, 'n_words': 707}