Python 3 implementation of execfile.
166140
def _execfile(filename, globals, locals=None):
    
    mode = 'rb'
    # Python 2.6 compile requires LF for newlines, so use deprecated
    #  Universal newlines support.
    if sys.version_info < (2, 7):
        mode += 'U'
    with open(filename, mode) as stream:
        script = stream.read()
    if locals is None:
        locals = globals
    code = compile(script, filename, 'exec')
    exec(code, globals, locals)
184251
def main(argv=sys.argv[1:]):
    """"""

    global ext_lang

    parser = argparse.ArgumentParser(description=description,
                                     epilog=epilog)
    parser.add_argument('files', metavar='FILES', nargs='*',
                        help='the files to process')
    parser.add_argument('--version', action='version', version=version)
    parser.add_argument('-i', '--in-place', action='store_true',
                        help='modify files inplace')
    parser.add_argument('--print-langs', action='store_true',
                        help='print out all the supported languages')
    parser.add_argument('--lang', action='store', default='auto',
                        help='use the language with given extention')
    args = parser.parse_args(argv)

    # Print the current set of working languages.
    if args.print_langs:
        parser.print_usage()
        sys.stdout.write('\n')
        sys.stdout.write(textwrap.fill(textwrap.dedent("""\
        The following languages are supported:
        """)) + '\n')
        for cls in sorted({getattr(langs, nm) for nm in langs.__all__}):
            l = cls.__name__
            out = cls.__doc__.split('\n\n')[0]
            out = out + ' (' + ', '.join(cls.ext) + ')'

            prefix2 = ' '.join([''] * 20)
            prefix1 = prefix2[:2] + l + prefix2[2 + len(l):]

            wrapped = textwrap.fill(out, initial_indent=prefix1,
                                    subsequent_indent=prefix2)

            sys.stdout.write(wrapped + '\n')

        parser.exit(0)          # Exit once we're done

    if not args.files:
        parser.print_usage()

    for fname in args.files:
        # load the file
        with open(fname) as f:
            text = f.read()

        # generate the new output according to the language
        if args.lang == 'auto':
            l = language(fname)
        else:
            l = language(args.lang, True)
        text = l.gen(text)

        # emit the output
        if args.in_place:
            with open(fname, 'w') as f:
                f.write(text)
        else:
            sys.stdout.write(text)
469434
def _process_execs(self, execmatch, parent, module):
        """"""
        #Get the matches that must be present for every executable.
        name = execmatch.group("name").strip()
        modifiers = execmatch.group("modifiers")
        if modifiers is None:
            modifiers = []
        else:
            modifiers = re.split(",[ \t]*", modifiers)
        codetype = execmatch.group("codetype")
        params = re.split("[\s,]+", execmatch.group("parameters"))
        contents = execmatch.group("contents")

        #If the exec is a function, we also may have a type and kind specified.
        if codetype.lower() == "function":
            dtype = execmatch.group("type")
            kind = execmatch.group("kind")
            result = Function(name, modifiers, dtype, kind, parent)
        else:
            result = Subroutine(name, modifiers, parent)

        #Set the regex start and end char indices
        result.start, result.end = module.absolute_charindex(execmatch.string, execmatch.start(),
                                                             execmatch.end())
        result.contents = contents

        #Now we can handle the rest which is common to both types of executable
        #Extract a list of local variables
        self._parse_members(contents, result, params)
        if isinstance(result, Function):
            #We need to handle the syntax for defining a function with result(variable)
            #to specify the return type of the function.
            if execmatch.group("result") is not None:
                resvar = execmatch.group("result").lower().split("result(")[1].replace(")", "")
            else:
                resvar = None
            result.update_dtype(resvar)
        
        #Fortran allows lines to be continued using &. The easiest way
        #to deal with this is to remove all of those before processing
        #any of the regular expressions
        decommented = "\n".join([ self._depend_exec_clean(l) for l in contents.split("\n") ])
        cleaned = re.sub("&\s*", "", decommented)

        #Finally, process the dependencies. These are calls to functions and
        #subroutines from within the executable body
        self._process_dependencies(result, cleaned)
        self._process_assignments(result, cleaned)
        
        return result
1297771
def main():
    """"""
    parser = get_parser()
    args = parser.parse_args()

    # set up variables
    ARCHIVE = args.archive_path
    archive = (not args.no_archive)
    os.environ['F2FORMAT_VERSION'] = args.python
    os.environ['F2FORMAT_ENCODING'] = args.encoding

    def find(root):
        """Recursively find all files under root."""
        flst = list()
        temp = os.listdir(root)
        for file in temp:
            path = os.path.join(root, file)
            if os.path.isdir(path):
                flst.extend(find(path))
            elif os.path.isfile(path):
                flst.append(path)
            elif os.path.islink(path):  # exclude symbolic links
                continue
        yield from flst

    def rename(path):
        stem, ext = os.path.splitext(path)
        name = '%s-%s%s' % (stem, uuid.uuid4(), ext)
        return os.path.join(ARCHIVE, name)

    # make archive directory
    if archive:
        os.makedirs(ARCHIVE, exist_ok=True)

    # fetch file list
    filelist = list()
    for path in sys.argv[1:]:
        if os.path.isfile(path):
            if archive:
                dest = rename(path)
                os.makedirs(os.path.dirname(dest), exist_ok=True)
                shutil.copy(path, dest)
            filelist.append(path)
        if os.path.isdir(path):
            if archive:
                shutil.copytree(path, rename(path))
            filelist.extend(find(path))

    # check if file is Python source code
    def ispy(file): return (os.path.isfile(file) and (os.path.splitext(file)[1] in ('.py', '.pyw')))
    filelist = sorted(filter(ispy, filelist))

    # if no file supplied
    if len(filelist) == 0:
        parser.error('argument PATH: no valid source file found')

    # process files
    if mp is None  or CPU_CNT <= 1:
        [f2format(filename) for filename in filelist]
    else:
        mp.Pool(processes=CPU_CNT).map(f2format, filelist)
454820
def _write_executable_py(self, execname, lines):
        
        executable = self.module.executables[execname]
            
        #The 5 in the formatting indent comes from 1 space, 3 from the 'def'
        #and 1 from '('.
        inparams = []
        for p in executable.ordered_parameters:
            if "in" in p.direction:
                inparams.append(p.lname)
        pyparams = present_params(inparams, len(execname) + 5, linecont=", ")
        lines.append("def {}({}):".format(execname.lower(), pyparams))
        tab = "    "

        #Next, we want to append the docstring information from the XML tags
        #that were parsed by fortpy.
        docs = ['"""']
        summary = self._format_docstring(executable.summary)
        if len(summary) > 0:
            docs[0] += summary[0]
            docs.extend(summary[1::])
            docs.append("")
        else:
            docs[0] += "No XML documentation summary."
        for p in executable.ordered_parameters:
            doc = ":arg {}: {}; {}".format(p.name, p.strtype, p.summary)
            docs.extend(self._format_docstring(doc))

        lines.extend([tab + l for l in docs])
        lines.append(tab + '"""')

        pathstr = '{}libpath = path.join(path.dirname(__file__), "{}")'
        lines.append(pathstr.format(tab, self.libname))
        methparams = '"{}_c", "{}_c", libpath, True'.format(self.module.name, execname)
        lines.append("{}method = static_symbol({})".format(tab, methparams))
        argtypes = _py_ex_argtype(executable)
        spacing = len(tab) + 6 + 8 + 3
        lines.append("{}method.argtypes = [{}]".format(tab, present_params(argtypes, spacing,
                                                                           linecont=", ")))
        lines.append("")
        lparams = []
        _py_code_variables(lines, executable, lparams, tab)
        #This is the line that calls the shared library executable.
        methparams = present_params(lparams, len(tab) + 5, linecont=", ")
        lines.append("{}method({})\n".format(tab, methparams))

        #Now we clean up the output variables to turn them into numpy arrays etc.
        constructor = '{}result = FtypesResult("{}", "{}", "{}")'
        lines.append(constructor.format(tab, self.module.name, execname, type(executable).__name__))
        count = _py_code_clean(lines, tab, executable)
        
        if count > 0:
            lines.append("")
            lines.append("{}return result\n".format(tab))
        else:
            lines.append("{}return None\n".format(tab))
446508
def run_ext(args):
    """"""
    try:
        subprocess.check_call(args)
    except subprocess.CalledProcessError as exc:
        print(exc)
        print(exc.cmd)
        print(exc.returncode)
        print(exc.output)
        raise
894553
def create_module(name, path):
    
    module = imp.new_module(name)
    module.__file__ = path
    execfile(path, module.__dict__)
    return module
1334093
def run():
    
    __builtins__
    script_name = sys.argv[1]
    namespace = dict(
        __file__=script_name,
        __name__='__main__',
        __doc__=None,
    )
    sys.argv[:] = sys.argv[1:]

    open_ = getattr(tokenize, 'open', open)
    script = open_(script_name).read()
    norm_script = script.replace('\\r\\n', '\\n')
    code = compile(norm_script, script_name, 'exec')
    exec(code, namespace)
504862
def run(files, temp_folder, arg=''):
    ""
    parser = get_parser()
    args = parser.parse_args(arg.split())

    py_files = filter_python_files(files)
    if args.ignore:
        orig_file_list = original_files(py_files, temp_folder)
        py_files = set(orig_file_list) - set(args.ignore)
        py_files = [temp_folder + f for f in py_files]

    return check_files(py_files).value()
294018
def _exec(self, is_module, entry_point_fn, module_name, file, globals, locals):
        '''
        
        '''
        if not is_module:
            pydev_imports.execfile(file, globals, locals)  # execute the script
        else:
            # treat ':' as a separator between module and entry point function
            # if there is no entry point we run we same as with -m switch. Otherwise we perform
            # an import and execute the entry point
            if entry_point_fn:
                mod = __import__(module_name, level=0, fromlist=[entry_point_fn], globals=globals, locals=locals)
                func = getattr(mod, entry_point_fn)
                func()
            else:
                # Run with the -m switch
                import runpy
                if hasattr(runpy, '_run_module_as_main'):
                    # Newer versions of Python actually use this when the -m switch is used.
                    if sys.version_info[:2] <= (2, 6):
                        runpy._run_module_as_main(module_name, set_argv0=False)
                    else:
                        runpy._run_module_as_main(module_name, alter_argv=False)
                else:
                    runpy.run_module(module_name)
        return globals
763175
def main():
    
    usage = '%prog [options] "<input file>"'
    if '__main__.py' in sys.argv[0]: # python -m pyminifier
        usage = 'pyminifier [options] "<input file>"'
    parser = OptionParser(usage=usage, version=__version__)
    parser.disable_interspersed_args()
    parser.add_option(
        "-o", "--outfile",
        dest="outfile",
        default=None,
        help="Save output to the given file.",
        metavar="<file path>"
    )
    parser.add_option(
        "-d", "--destdir",
        dest="destdir",
        default="./minified",
        help=("Save output to the given directory. "
              "This option is required when handling multiple files. "
              "Defaults to './minified' and will be created if not present. "),
        metavar="<file path>"
    )
    parser.add_option(
        "--nominify",
        action="store_true",
        dest="nominify",
        default=False,
        help="Don't bother minifying (only used with --pyz).",
    )
    parser.add_option(
        "--use-tabs",
        action="store_true",
        dest="tabs",
        default=False,
        help="Use tabs for indentation instead of spaces.",
    )
    parser.add_option(
        "--bzip2",
        action="store_true",
        dest="bzip2",
        default=False,
        help=("bzip2-compress the result into a self-executing python script.  "
              "Only works on stand-alone scripts without implicit imports.")
    )
    parser.add_option(
        "--gzip",
        action="store_true",
        dest="gzip",
        default=False,
        help=("gzip-compress the result into a self-executing python script.  "
              "Only works on stand-alone scripts without implicit imports.")
    )
    if lzma:
        parser.add_option(
            "--lzma",
            action="store_true",
            dest="lzma",
            default=False,
            help=("lzma-compress the result into a self-executing python script.  "
                  "Only works on stand-alone scripts without implicit imports.")
        )
    parser.add_option(
        "--pyz",
        dest="pyz",
        default=None,
        help=("zip-compress the result into a self-executing python script. "
              "This will create a new file that includes any necessary implicit"
              " (local to the script) modules.  Will include/process all files "
              "given as arguments to pyminifier.py on the command line."),
        metavar="<name of archive>.pyz"
    )
    parser.add_option(
        "-O", "--obfuscate",
        action="store_true",
        dest="obfuscate",
        default=False,
        help=(
            "Obfuscate all function/method names, variables, and classes.  "
            "Default is to NOT obfuscate."
        )
    )
    parser.add_option(
        "--obfuscate-classes",
        action="store_true",
        dest="obf_classes",
        default=False,
        help="Obfuscate class names."
    )
    parser.add_option(
        "--obfuscate-functions",
        action="store_true",
        dest="obf_functions",
        default=False,
        help="Obfuscate function and method names."
    )
    parser.add_option(
        "--obfuscate-variables",
        action="store_true",
        dest="obf_variables",
        default=False,
        help="Obfuscate variable names."
    )
    parser.add_option(
        "--obfuscate-import-methods",
        action="store_true",
        dest="obf_import_methods",
        default=False,
        help="Obfuscate globally-imported mouled methods (e.g. 'Ag=re.compile')."
    )
    parser.add_option(
        "--obfuscate-builtins",
        action="store_true",
        dest="obf_builtins",
        default=False,
        help="Obfuscate built-ins (i.e. True, False, object, Exception, etc)."
    )
    parser.add_option(
        "--replacement-length",
        dest="replacement_length",
        default=1,
        help=(
            "The length of the random names that will be used when obfuscating "
            "identifiers."
        ),
        metavar="1"
    )
    parser.add_option(
        "--nonlatin",
        action="store_true",
        dest="use_nonlatin",
        default=False,
        help=(
            "Use non-latin (unicode) characters in obfuscation (Python 3 only)."
            "  WARNING: This results in some SERIOUSLY hard-to-read code."
        )
    )
    parser.add_option(
        "--prepend",
        dest="prepend",
        default=None,
        help=(
            "Prepend the text in this file to the top of our output.  "
            "e.g. A copyright notice."
        ),
        metavar="<file path>"
    )
    options, files = parser.parse_args()
    if not files:
        parser.print_help()
        sys.exit(2)
    pyminify(options, files)
643385
def main():
    """"""
    if len(sys.argv) < 3:
        print('Usage: <project-name> <filetype> <list-of-path to traverse>')
        print('\tfiletype can be python/cpp/all')
        exit(-1)
    _HELPER.project_name = sys.argv[1]
    file_type = sys.argv[2]
    allow_type = []
    if file_type == 'python' or file_type == 'all':
        allow_type += [x for x in PYTHON_SUFFIX]
    if file_type == 'cpp' or file_type == 'all':
        allow_type += [x for x in CXX_SUFFIX]
    allow_type = set(allow_type)
    if os.name != 'nt':
        sys.stderr = codecs.StreamReaderWriter(sys.stderr,
                                               codecs.getreader('utf8'),
                                               codecs.getwriter('utf8'),
                                               'replace')
    for path in sys.argv[3:]:
        if os.path.isfile(path):
            process(path, allow_type)
        else:
            for root, dirs, files in os.walk(path):
                for name in files:
                    process(os.path.join(root, name), allow_type)

    nerr = _HELPER.print_summary(sys.stderr)
    sys.exit(nerr > 0)
800502
async def transform(self, file):
		""""""
		path = get_path(self.dest, file)
		await self.loop.run_in_executor(self.exe, write_file, path, file.contents)
		return file
1244018
def _compiled(self):
        """"""
        # Dev Notes:
        # Because of fast locals in functions in both CPython and PyPy, getting a
        # function to execute makes the code a few times faster than
        # just executing it in the global exec scope.
        prog = [self._prog_start]

        simple_func = {  # OPS
            'w': lambda x: x,
            'r': lambda x: x,
            '~': lambda x: '(~' + x + ')',
            '&': lambda l, r: '(' + l + '&' + r + ')',
            '|': lambda l, r: '(' + l + '|' + r + ')',
            '^': lambda l, r: '(' + l + '^' + r + ')',
            'n': lambda l, r: '(~(' + l + '&' + r + '))',
            '+': lambda l, r: '(' + l + '+' + r + ')',
            '-': lambda l, r: '(' + l + '-' + r + ')',
            '*': lambda l, r: '(' + l + '*' + r + ')',
            '<': lambda l, r: 'int(' + l + '<' + r + ')',
            '>': lambda l, r: 'int(' + l + '>' + r + ')',
            '=': lambda l, r: 'int(' + l + '==' + r + ')',
            'x': lambda sel, f, t: '({}) if ({}==0) else ({})'.format(f, sel, t),
        }

        def shift(value, direction, shift_amt):
            if shift_amt == 0:
                return value
            else:
                return '(%s %s %d)' % (value, direction, shift_amt)

        def make_split():
            if split_start_bit == 0:
                bit = '(%d & %s)' % ((1 << split_length) - 1, source)
            elif len(net.args[0]) - split_start_bit == split_length:
                bit = '(%s >> %d)' % (source, split_start_bit)
            else:
                bit = '(%d & (%s >> %d))' % ((1 << split_length) - 1, source, split_start_bit)
            return shift(bit, '<<', split_res_start_bit)

        for net in self.block:
            if net.op in simple_func:
                argvals = (self._arg_varname(arg) for arg in net.args)
                expr = simple_func[net.op](*argvals)
            elif net.op == 'c':
                expr = ''
                for i in range(len(net.args)):
                    if expr is not '':
                        expr += ' | '
                    shiftby = sum(len(j) for j in net.args[i+1:])
                    expr += shift(self._arg_varname(net.args[i]), '<<', shiftby)
            elif net.op == 's':
                source = self._arg_varname(net.args[0])
                expr = ''
                split_length = 0
                split_start_bit = -2
                split_res_start_bit = -1

                for i, b in enumerate(net.op_param):
                    if b != split_start_bit + split_length:
                        if split_start_bit >= 0:
                            # create a wire
                            expr += make_split() + '|'
                        split_length = 1
                        split_start_bit = b
                        split_res_start_bit = i
                    else:
                        split_length += 1
                expr += make_split()
            elif net.op == 'm':
                read_addr = self._arg_varname(net.args[0])
                mem = net.op_param[1]
                if isinstance(net.op_param[1], RomBlock):
                    expr = 'd["%s"]._get_read_data(%s)' % (self._mem_varname(mem), read_addr)
                else:  # memories act async for reads
                    expr = 'd["%s"].get(%s, %s)' % (self._mem_varname(mem),
                                                    read_addr, self.default_value)
            elif net.op == '@':
                mem = self._mem_varname(net.op_param[1])
                write_addr, write_val, write_enable = (self._arg_varname(a) for a in net.args)
                prog.append('    if {}:'.format(write_enable))
                prog.append('        mem_ws.append(("{}", {}, {}))'
                            .format(mem, write_addr, write_val))
                continue  # memwrites are special
            else:
                raise PyrtlError('FastSimulation cannot handle primitive "%s"' % net.op)

            # prog.append('    #  ' + str(net))
            result = self._dest_varname(net.dests[0])
            if len(net.dests[0]) == self._no_mask_bitwidth[net.op](net):
                prog.append("    %s = %s" % (result, expr))
            else:
                mask = str(net.dests[0].bitmask)
                prog.append('    %s = %s & %s' % (result, mask, expr))

        # add traced wires to dict
        if self.tracer is not None:
            for wire_name in self.tracer.trace:
                wire = self.block.wirevector_by_name[wire_name]
                if not isinstance(wire, (Input, Const, Register, Output)):
                    v_wire_name = self._varname(wire)
                    prog.append('    outs["%s"] = %s' % (wire_name, v_wire_name))

        prog.append("    return regs, outs, mem_ws")
        return '\n'.join(prog)
446392
def main():
    
    parser = optparse.OptionParser(usage="""usage: %prog [options] <package path> [exclude paths, ...]

Note: By default this script will not overwrite already created files.""")
    parser.add_option("-n", "--doc-header", action="store", dest="header", help="Documentation Header (default=Project)", default="Project")
    parser.add_option("-d", "--dest-dir", action="store", dest="destdir", help="Output destination directory", default="")
    parser.add_option("-s", "--suffix", action="store", dest="suffix", help="module suffix (default=txt)", default="txt")
    parser.add_option("-m", "--maxdepth", action="store", dest="maxdepth", help="Maximum depth of submodules to show in the TOC (default=4)", type="int", default=4)
    parser.add_option("-r", "--dry-run", action="store_true", dest="dryrun", help="Run the script without creating the files")
    parser.add_option("-f", "--force", action="store_true", dest="force", help="Overwrite all the files")
    parser.add_option("-t", "--no-toc", action="store_true", dest="notoc", help="Don't create the table of content file")
    (opts, args) = parser.parse_args()
    if not args:
        parser.error("package path is required.")
    else:
        rootpath, excludes = args[0], args[1:]
        if os.path.isdir(rootpath):
            # check if the output destination is a valid directory
            if opts.destdir and os.path.isdir(opts.destdir):
                excludes = normalize_excludes(rootpath, excludes)
                recurse_tree(rootpath, excludes, opts)
            else:
                print '%s is not a valid output destination directory.' % opts.destdir
        else:
            print '%s is not a valid directory.' % rootpath
529373
def exec_module(self, module):
        """"""
        path = [os.path.dirname(module.__file__)]  # file should have been resolved before (module creation)
        file = None
        try:
            file, pathname, description = imp.find_module(module.__name__.rpartition('.')[-1], path)
            module = imp.load_module(module.__name__, file, pathname, description)
        finally:
            if file:
                file.close()
909664
def conf_from_file(filepath):
    '''
    
    '''

    abspath = os.path.abspath(os.path.expanduser(filepath))
    conf_dict = {}
    if not os.path.isfile(abspath):
        raise RuntimeError('`%s` is not a file.' % abspath)

    # First, make sure the code will actually compile (and has no SyntaxErrors)
    with open(abspath, 'rb') as f:
        compiled = compile(f.read(), abspath, 'exec')

    # Next, attempt to actually import the file as a module.
    # This provides more verbose import-related error reporting than exec()
    absname, _ = os.path.splitext(abspath)
    basepath, module_name = absname.rsplit(os.sep, 1)
    if six.PY3:
        SourceFileLoader(module_name, abspath).load_module(module_name)
    else:
        imp.load_module(
            module_name,
            *imp.find_module(module_name, [basepath])
        )

    # If we were able to import as a module, actually exec the compiled code
    exec(compiled, globals(), conf_dict)
    conf_dict['__file__'] = abspath

    return conf_from_dict(conf_dict)