Given an *options* object (from `optparse.OptionParser` or similar),
    performs minification and/or obfuscation on the given *files* (any iterable
    containing file paths) based on said *options*.

    All accepted options can　be listed by running ``python __main__.py -h`` or
    examining the :py:func:`__init__.main` function.

def pyminify(options, files):
    """
    Given an *options* object (from `optparse.OptionParser` or similar),
    performs minification and/or obfuscation on the given *files* (any iterable
    containing file paths) based on said *options*.

    All accepted options can　be listed by running ``python __main__.py -h`` or
    examining the :py:func:`__init__.main` function.
    """
    global name_generator
    if not is_iterable(files):
        print(
            "Error: The 'files' argument must be a list, tuple, etc of files.  "
            "Strings and bytes won't work.")
        sys.exit(1)
    if options.pyz:
        # Check to make sure we were only passed one script (only one at a time)
        if len(files) > 1:
            print("ERROR: The --pyz option only works with one python file at "
                  "a time.")
            print("(Dependencies will be automagically included in the "
                  "resulting .pyz)")
            sys.exit(1)
        # Make our .pyz:
        compression.zip_pack(files, options)
        return None # Make sure we don't do anything else
    # Read in our prepend text (if any)
    prepend = None
    if options.prepend:
        try:
            prepend = open(options.prepend).read()
        except Exception as err:
            print("Error reading %s:" % options.prepend)
            print(err)

    obfuscations = (options.obfuscate, options.obf_classes,
                    options.obf_functions, options.obf_variables,
                    options.obf_builtins, options.obf_import_methods)

    # Automatically enable obfuscation if --nonlatin (implied if no explicit
    # obfuscation is stated)
    if options.use_nonlatin and not any(obfuscations):
        options.obfuscate = True
    if len(files) > 1: # We're dealing with more than one file
        name_generator = None # So we can tell if we need to obfuscate
        if any(obfuscations):
            # Put together that will be used for all obfuscation functions:
            identifier_length = int(options.replacement_length)
            if options.use_nonlatin:
                if sys.version_info[0] == 3:
                    name_generator = obfuscate.obfuscation_machine(
                        use_unicode=True, identifier_length=identifier_length
                    )
                else:
                    print(
                        "ERROR: You can't use nonlatin characters without Python 3")
                    sys.exit(2)
            else:
                name_generator = obfuscate.obfuscation_machine(
                    identifier_length=identifier_length)
            table =[{}]
        cumulative_size = 0 # For size reduction stats
        cumulative_new = 0 # Ditto
        for sourcefile in files:
            # Record how big the file is so we can compare afterwards
            filesize = os.path.getsize(sourcefile)
            cumulative_size += filesize
            # Get the module name from the path
            module = os.path.split(sourcefile)[1]
            module = ".".join(module.split('.')[:-1])
            source = open(sourcefile).read()
            tokens = token_utils.listified_tokenizer(source)
            if not options.nominify: # Perform minification
                source = minification.minify(tokens, options)
            # Have to re-tokenize for obfucation (it is quick):
            tokens = token_utils.listified_tokenizer(source)
            # Perform obfuscation if any of the related options were set
            if name_generator:
                obfuscate.obfuscate(
                    module,
                    tokens,
                    options,
                    name_generator=name_generator,
                    table=table
                )
            # Convert back to text
            result = ''
            if prepend:
                result += prepend
            result += token_utils.untokenize(tokens)
            # Compress it if we were asked to do so
            if options.bzip2:
                result = compression.bz2_pack(result)
            elif options.gzip:
                result = compression.gz_pack(result)
            elif lzma and options.lzma:
                result = compression.lzma_pack(result)
            result += (
                "# Created by pyminifier "
                "(https://github.com/liftoff/pyminifier)\n")
            # Either save the result to the output file or print it to stdout
            if not os.path.exists(options.destdir):
                os.mkdir(options.destdir)
            # Need the path where the script lives for the next steps:
            filepath = os.path.split(sourcefile)[1]
            path = options.destdir + '/' + filepath # Put everything in destdir
            f = open(path, 'w')
            f.write(result)
            f.close()
            new_filesize = os.path.getsize(path)
            cumulative_new += new_filesize
            percent_saved = round((float(new_filesize) / float(filesize)) * 100, 2) if float(filesize)!=0 else 0
            print((
                "{sourcefile} ({filesize}) reduced to {new_filesize} bytes "
                "({percent_saved}% of original size)").format(**locals()))
        p_saved = round(
            (float(cumulative_new) / float(cumulative_size) * 100), 2)
        print("Overall size reduction: {0}% of original size".format(p_saved))
    else:
        # Get the module name from the path
        _file = files[0]
        module = os.path.split(_file)[1]
        module = ".".join(module.split('.')[:-1])
        filesize = os.path.getsize(_file)
        source = open(_file).read()
        # Convert the tokens from a tuple of tuples to a list of lists so we can
        # update in-place.
        tokens = token_utils.listified_tokenizer(source)
        if not options.nominify: # Perform minification
            source = minification.minify(tokens, options)
            # Convert back to tokens in case we're obfuscating
            tokens = token_utils.listified_tokenizer(source)
        # Perform obfuscation if any of the related options were set
        if options.obfuscate or options.obf_classes or options.obf_functions \
                or options.obf_variables or options.obf_builtins \
                or options.obf_import_methods:
            identifier_length = int(options.replacement_length)
            name_generator = obfuscate.obfuscation_machine(
                identifier_length=identifier_length)
            obfuscate.obfuscate(module, tokens, options)
        # Convert back to text
        result = ''
        if prepend:
            result += prepend
        result += token_utils.untokenize(tokens)
        # Compress it if we were asked to do so
        if options.bzip2:
            result = compression.bz2_pack(result)
        elif options.gzip:
            result = compression.gz_pack(result)
        elif lzma and options.lzma:
            result = compression.lzma_pack(result)
        result += (
            "# Created by pyminifier "
            "(https://github.com/liftoff/pyminifier)\n")
        # Either save the result to the output file or print it to stdout
        if options.outfile:
            f = io.open(options.outfile, 'w', encoding='utf-8')
            f.write(result)
            f.close()
            new_filesize = os.path.getsize(options.outfile)
            percent_saved = round(float(new_filesize)/float(filesize) * 100, 2)
            print((
                "{_file} ({filesize}) reduced to {new_filesize} bytes "
                "({percent_saved}% of original size)".format(**locals())))
        else:
            print(result)