Ejemplo n.º 1
0
 def parse_flags(self, program_text: str, extra: List[str]) -> Options:
     flags = re.search('# flags: (.*)$', program_text, flags=re.MULTILINE)
     if flags:
         flag_list = flags.group(1).split()
     else:
         flag_list = []
     return parse_options(flag_list + extra)
Ejemplo n.º 2
0
def parse_flags(program_text: str) -> Options:
    flags = re.search('# flags: (.*)$', program_text, flags=re.MULTILINE)
    if flags:
        flag_list = flags.group(1).split()
    else:
        flag_list = []
    return parse_options(flag_list + ['dummy.py'])
 def test_packages_found(self) -> None:
     current = os.getcwd()
     with tempfile.TemporaryDirectory() as tmp:
         try:
             os.chdir(tmp)
             os.mkdir('pack')
             self.make_file('pack',
                            '__init__.py',
                            content='from . import a, b')
             self.make_file('pack', 'a.py')
             self.make_file('pack', 'b.py')
             opts = parse_options(['-p', 'pack'])
             py_mods, c_mods = collect_build_targets(
                 opts, mypy_options(opts))
             assert_equal(c_mods, [])
             files = {
                 os.path.relpath(mod.path or 'FAIL')
                 for mod in py_mods
             }
             assert_equal(
                 files, {
                     os.path.join('pack', '__init__.py'),
                     os.path.join('pack', 'a.py'),
                     os.path.join('pack', 'b.py')
                 })
         finally:
             os.chdir(current)
Ejemplo n.º 4
0
 def parse_flags(self, program_text: str, extra: List[str]) -> Options:
     flags = re.search('# flags: (.*)$', program_text, flags=re.MULTILINE)
     if flags:
         flag_list = flags.group(1).split()
     else:
         flag_list = []
     return parse_options(flag_list + extra)
Ejemplo n.º 5
0
def call_stubgen(command_line_args: List[str]) -> None:
    """
    Call stubgen like the command line tool.

    :param command_line_args: list of command line args
    """

    generate_stubs(parse_options(command_line_args))
Ejemplo n.º 6
0
def stubgen_main(opts):
    mypy_util.check_python_version('stubgen')
    # Make sure that the current directory is in sys.path so that
    # stubgen can be run on packages in the current directory.
    if not ('' in sys.path or '.' in sys.path):
        sys.path.insert(0, '')
    options = stubgen.parse_options(opts)
    stubgen.generate_stubs(options)
Ejemplo n.º 7
0
def generate_stubs_from_sample(sample, python2=True, *args):
    full_sample_path = os.path.join('.', 'samples', sample)
    options = parse_options((['--py2'] if python2 else []) + list(args) +
                            [full_sample_path])
    generate_stubs(options)
    try:
        yield __import__(f'out.{sample.split(".py")[0]}', fromlist=['a'])
    finally:
        shutil.rmtree('out')
 def parse_flags(self, program_text: str, extra: List[str]) -> Options:
     flags = re.search('# flags: (.*)$', program_text, flags=re.MULTILINE)
     if flags:
         flag_list = flags.group(1).split()
     else:
         flag_list = []
     options = parse_options(flag_list + extra)
     if '--verbose' not in flag_list:
         options.quiet = True
     else:
         options.verbose = True
     return options
Ejemplo n.º 9
0
 def test_module_not_found(self) -> None:
     current = os.getcwd()
     captured_output = io.StringIO()
     sys.stdout = captured_output
     with tempfile.TemporaryDirectory() as tmp:
         try:
             os.chdir(tmp)
             self.make_file(tmp, 'mymodule.py', content='import a')
             opts = parse_options(['-m', 'mymodule'])
             py_mods, c_mods = collect_build_targets(opts, mypy_options(opts))
             assert captured_output.getvalue() == ''
         finally:
             sys.stdout = sys.__stdout__
             os.chdir(current)
Ejemplo n.º 10
0
 def test_module_not_found(self) -> None:
     current = os.getcwd()
     captured_output = io.StringIO()
     sys.stdout = captured_output
     with tempfile.TemporaryDirectory() as tmp:
         try:
             os.chdir(tmp)
             self.make_file(tmp, 'mymodule.py', content='import a')
             opts = parse_options(['-m', 'mymodule'])
             py_mods, c_mods = collect_build_targets(opts, mypy_options(opts))
             self.assertRegex(captured_output.getvalue(), "No module named 'a'")
         finally:
             sys.stdout = sys.__stdout__
             os.chdir(current)
Ejemplo n.º 11
0
 def test_packages_found(self) -> None:
     current = os.getcwd()
     with tempfile.TemporaryDirectory() as tmp:
         try:
             os.chdir(tmp)
             os.mkdir('pack')
             self.make_file('pack', '__init__.py', content='from . import a, b')
             self.make_file('pack', 'a.py')
             self.make_file('pack', 'b.py')
             opts = parse_options(['-p', 'pack'])
             py_mods, c_mods = collect_build_targets(opts, mypy_options(opts))
             assert_equal(c_mods, [])
             files = {os.path.relpath(mod.path or 'FAIL') for mod in py_mods}
             assert_equal(files, {os.path.join('pack', '__init__.py'),
                                  os.path.join('pack', 'a.py'),
                                  os.path.join('pack', 'b.py')})
         finally:
             os.chdir(current)
Ejemplo n.º 12
0
 def test_files_found(self) -> None:
     current = os.getcwd()
     with tempfile.TemporaryDirectory() as tmp:
         try:
             os.chdir(tmp)
             os.mkdir('subdir')
             self.make_file('subdir', 'a.py')
             self.make_file('subdir', 'b.py')
             os.mkdir(os.path.join('subdir', 'pack'))
             self.make_file('subdir', 'pack', '__init__.py')
             opts = parse_options(['subdir'])
             py_mods, c_mods = collect_build_targets(opts, mypy_options(opts))
             assert_equal(c_mods, [])
             files = {mod.path for mod in py_mods}
             assert_equal(files, {os.path.join('subdir', 'pack', '__init__.py'),
                                  os.path.join('subdir', 'a.py'),
                                  os.path.join('subdir', 'b.py')})
         finally:
             os.chdir(current)
Ejemplo n.º 13
0
 def test_files_found(self) -> None:
     current = os.getcwd()
     with tempfile.TemporaryDirectory() as tmp:
         try:
             os.chdir(tmp)
             os.mkdir('subdir')
             self.make_file('subdir', 'a.py')
             self.make_file('subdir', 'b.py')
             os.mkdir(os.path.join('subdir', 'pack'))
             self.make_file('subdir', 'pack', '__init__.py')
             opts = parse_options(['subdir'])
             py_mods, c_mods = collect_build_targets(opts, mypy_options(opts))
             assert_equal(c_mods, [])
             files = {mod.path for mod in py_mods}
             assert_equal(files, {os.path.join('subdir', 'pack', '__init__.py'),
                                  os.path.join('subdir', 'a.py'),
                                  os.path.join('subdir', 'b.py')})
         finally:
             os.chdir(current)
Ejemplo n.º 14
0
def clone_and_generate(clone_dir: Path, package_dir: Path, dst_dir: Path,
                       branch: str):
    """
    Clone source repo and generate stubs for package

    :param Path clone_dir: Where to clone source repo
    :param Path package_dir: Path to module that needs stubs generated
    :param Path dst_dir: Destination path for stubs
    :param str branch: Which branch to clone
    """

    if clone_dir.exists():
        shutil.rmtree(clone_dir)
    clone_dir.mkdir(exist_ok=True, parents=False)

    repo = Repo.clone_from(
        "https://github.com/kubernetes-client/python.git",
        clone_dir,
        progress=RemoteProgress(),
        branch=branch,
        depth=1,
    )

    for submodule in repo.submodules:
        submodule.update(init=True)

    stubgen_options = parse_options([
        f"{package_dir}",
        f"-o={dst_dir}",
    ])
    generate_stubs(stubgen_options)

    for dirpath, _, filenames in os.walk(dst_dir):
        for fn in filenames:
            with open(os.path.join(dirpath, fn), "r") as original:
                data = original.read()
            with open(os.path.join(dirpath, fn), "w") as modified:
                modified.write(
                    "# Code generated by `stubgen`. DO NOT EDIT.\n" + data)
Ejemplo n.º 15
0
from argparse import ArgumentParser

from mypy.stubgen import generate_stubs, parse_options

from scripts.git_helpers import checkout_django_branch
from scripts.paths import DJANGO_SOURCE_DIRECTORY

if __name__ == "__main__":
    parser = ArgumentParser()
    parser.add_argument("--django_version", required=True)
    parser.add_argument("--commit_sha", required=False)
    args = parser.parse_args()
    checkout_django_branch(args.django_version, args.commit_sha)
    stubgen_options = parse_options([f"{DJANGO_SOURCE_DIRECTORY}", "-o=stubgen"])
    generate_stubs(stubgen_options)