def run_pytype(self, filename, report_errors=True): """Run pytype over a single file.""" path, target, module_name = self.infer_module_name(filename) out = target + 'i' err = target + '.errors' outfile = os.path.join(self.pyi_dir, out) errfile = os.path.join(self.pyi_dir, err) target_dir = os.path.join(self.pyi_dir, os.path.dirname(target)) in_projects = any(path.startswith(d) for d in self.projects) in_deps = any(path.startswith(d) for d in self.deps) # Do not try to analyse files importlab has resolved via the system path. # Also, if importlab has returned a non-.py dependency, ignore it. if (not in_projects and not in_deps) or not target.endswith('.py'): print(' skipping file:', target) return # Report errors for files in projects (those we are analysing directly) if in_deps and not in_projects: report_errors = False if not report_errors: print(' %s*' % out) else: print(' %s' % out) # Create the output subdirectory for this file. try: utils.makedirs(target_dir) except OSError: self.logger.error( 'Could not create directory for output file: %s' % out) return run_cmd = [ 'pytype', '-P', self.pyi_dir, '-V', self.python_version, '-o', outfile, '--quick', '--module-name', module_name ] if not report_errors: run_cmd += ['--no-report-errors'] run_cmd += [filename] self.logger.info('Running: ' + ' '.join(run_cmd)) run = runner.BinaryRun(run_cmd, env=self.system_env) try: returncode, _, stderr = run.communicate() except OSError: self.logger.error('Could not run pytype.') return if returncode: print(' errors written to:', errfile) error = stderr.decode('utf-8') with open(errfile, 'w') as f: f.write(error) if not self.quiet: print(error) # Log as WARNING since this is a pytype error, not our error. self.logger.warning(error)
def check_python_version(exe: List[str], required): """Check if exe is a python executable with the required version.""" try: # python --version outputs to stderr for earlier versions _, out, err = runner.BinaryRun(exe + ["--version"]).communicate() # pylint: disable=unpacking-non-sequence version = out or err version = version.decode("utf-8") if version.startswith("Python %s" % required): return True, None else: return False, version.rstrip() except OSError: return False, None
def run_pytype(self, filename, report_errors=True): """Run pytype over a single file.""" path, module_name = self.infer_module_name(filename) target = os.path.relpath(filename, path) out = os.path.join(self.pyi_dir, target + 'i') err = os.path.join(self.pyi_dir, target + '.errors') in_projects = any(path.startswith(d) for d in self.projects) in_deps = any(path.startswith(d) for d in self.deps) if in_deps and not in_projects: report_errors = False if not report_errors: print(' %s*' % out) else: print(' %s' % out) try: os.makedirs(self.pyi_dir) except OSError: self.logger.error( 'Could not create directory for output file: %s' % out) return run_cmd = [ 'pytype', '-P', self.pyi_dir, '-V', self.env.python_version_string, '-o', out, '--quick', '--module-name', module_name ] if not report_errors: run_cmd += ['--no-report-errors'] run_cmd += [filename] self.logger.info('Running: ' + ' '.join(run_cmd)) run = runner.BinaryRun(run_cmd, env=self.system_env) try: returncode, _, stderr = run.communicate() except OSError: self.logger.error('Could not run pytype.') return if returncode: print(' errors written to:', err) error = stderr.decode('utf-8') with open(err, 'w') as f: f.write(error) if not self.env.args.quiet: print(error) # Log as WARNING since this is a pytype error, not our error. self.logger.warning(error)