def main(): argument_parser = make_parser() opts = argument_parser.parse_args() python_version = utils.version_from_string(opts.python_version) try: utils.validate_version(python_version) except utils.UsageError as e: sys.stderr.write("Usage error: %s\n" % utils.message(e)) sys.exit(1) with open(opts.input) as fi: sourcecode = fi.read() try: parsed = parser.parse_string(sourcecode, filename=opts.input, python_version=python_version) except parser.ParseError as e: sys.stderr.write(str(e)) sys.exit(1) if opts.optimize: parsed = optimize.Optimize(parsed, builtins.GetBuiltinsPyTD(python_version), lossy=opts.lossy, use_abcs=opts.use_abcs, max_union=opts.max_union, remove_mutable=opts.remove_mutable, can_do_lookup=False) if opts.output is not None: out_text = pytd_utils.Print(parsed, opts.multiline_args) if opts.output == "-": sys.stdout.write(out_text) else: with open(opts.output, "w") as out: out.write(out_text)
def _store_python_version(self, python_version): """Configure the python version.""" if python_version: if isinstance(python_version, str): version = utils.version_from_string(python_version) else: version = python_version else: version = sys.version_info[:2] if len(version) != 2: self.error("--python_version must be <major>.<minor>: %r" % python_version) # Check that we have a version supported by pytype. utils.validate_version(version) self.output_options.python_version = version if utils.can_compile_bytecode_natively( self.output_options.python_version): # pytype does not need an exe for bytecode compilation. Abort early to # avoid extracting a large unused exe into /tmp. self.output_options.python_exe = None return for exe in utils.get_python_exes(self.output_options.python_version): exe_version = utils.get_python_exe_version(exe) if exe_version == self.output_options.python_version: self.output_options.python_exe = exe break else: self.error("Need a valid python%d.%d executable in $PATH" % self.output_options.python_version)
def create_importlab_environment(conf, typeshed): """Create an importlab environment from the python version and path.""" python_version = utils.version_from_string(conf.python_version) path = fs.Path() for p in conf.pythonpath: path.add_path(p, 'os') for p in typeshed.get_pytd_paths(python_version): path.add_fs(PytdFileSystem(fs.OSFileSystem(p))) for p in typeshed.get_typeshed_paths(python_version): path.add_path(p, 'pyi') return environment.Environment(path, python_version)
def _store_python_version(self, python_version): """Configure the python version.""" if python_version: if isinstance(python_version, str): self.output_options.python_version = utils.version_from_string( python_version) elif isinstance(python_version, int): self.output_options.python_version = utils.full_version_from_major( python_version) else: self.output_options.python_version = python_version else: self.output_options.python_version = sys.version_info[:2] if len(self.output_options.python_version) != 2: self.error("--python_version must be <major>.<minor>: %r" % python_version) # Check that we have a version supported by pytype. utils.validate_version(self.output_options.python_version)
def test_version_from_string_tuple(self): self.assertEqual(utils.version_from_string("2.7"), (2, 7))
def test_version_from_string_int(self): self.assertEqual(utils.version_from_string("2"), (2, 7))
def testVersionFromStringTuple(self): self.assertEqual(utils.version_from_string("2.7"), (2, 7))
def testVersionFromStringInt(self): self.assertEqual(utils.version_from_string("2"), (2, 7))
def test_version_from_string(self): self.assertEqual(utils.version_from_string("3.7"), (3, 7))