def _run_python_test(self, target): po = None rv = PythonTestResult.exception() try: builder = PEXBuilder() builder.info().entry_point = 'pytest' builder.info().ignore_errors = target._soft_dependencies chroot = PythonChroot(target, self.root_dir, extra_targets=self.generate_test_targets(), builder=builder) builder = chroot.dump() builder.freeze() test_args = PythonTestBuilder.generate_junit_args(target) test_args.extend(self.args) sources = [os.path.join(target.target_base, source) for source in target.sources] po = PEX(builder.path()).run(args=test_args + sources, blocking=False, setsid=True) rv = PythonTestBuilder.wait_on(po, timeout=target.timeout) except Exception as e: import traceback print('Failed to run test!', file=sys.stderr) traceback.print_exc() rv = PythonTestResult.exception() finally: if po and po.returncode != 0: try: os.killpg(po.pid, signal.SIGTERM) except OSError as e: if e.errno == errno.EPERM: print("Unable to kill process group: %d" % po.pid) elif e.errno != errno.ESRCH: rv = PythonTestResult.exception() self.successes[target._create_id()] = rv return rv
def write_pex(td, exe_contents): with open(os.path.join(td, 'exe.py'), 'w') as fp: fp.write(exe_contents) pb = PEXBuilder(path=td) pb.set_executable(os.path.join(td, 'exe.py')) pb.freeze() return pb
def write_pex(td, exe_contents): with open(os.path.join(td, 'exe.py'), 'w') as fp: fp.write(exe_contents) pb = PEXBuilder(path=td) pb.set_executable(os.path.join(td, 'exe.py')) pb.freeze() return pb
def execute(self): if self.options.pex and self.options.ipython: self.error('Cannot specify both --pex and --ipython!') if self.options.entry_point and self.options.ipython: self.error('Cannot specify both --entry_point and --ipython!') if self.options.verbose: print('Build operating on target: %s %s' % (self.target, 'Extra targets: %s' % ' '.join(map(str, self.extra_targets)) if self.extra_targets else '')) builder = PEXBuilder(tempfile.mkdtemp(), interpreter=self.interpreter, pex_info=self.target.pexinfo if isinstance(self.target, PythonBinary) else None) if self.options.entry_point: builder.set_entry_point(self.options.entry_point) if self.options.ipython: if not self.config.has_section('python-ipython'): self.error('No python-ipython sections defined in your pants.ini!') builder.info.entry_point = self.config.get('python-ipython', 'entry_point') if builder.info.entry_point is None: self.error('Must specify entry_point for IPython in the python-ipython section ' 'of your pants.ini!') requirements = self.config.getlist('python-ipython', 'requirements', default=[]) with ParseContext.temp(): for requirement in requirements: self.extra_targets.append(PythonRequirement(requirement)) executor = PythonChroot( self.target, self.root_dir, builder=builder, interpreter=self.interpreter, extra_targets=self.extra_targets, conn_timeout=self.options.conn_timeout) executor.dump() if self.options.pex: pex_name = os.path.join(self.root_dir, 'dist', '%s.pex' % self.target.name) builder.build(pex_name) print('Wrote %s' % pex_name) return 0 else: builder.freeze() pex = PEX(builder.path(), interpreter=self.interpreter) po = pex.run(args=list(self.args), blocking=False) try: return po.wait() except KeyboardInterrupt: po.send_signal(signal.SIGINT) raise
def execute(self): if self.options.pex and self.options.ipython: self.error('Cannot specify both --pex and --ipython!') if self.options.entry_point and self.options.ipython: self.error('Cannot specify both --entry_point and --ipython!') if self.options.verbose: print('Build operating on target: %s %s' % (self.target, 'Extra targets: %s' % ' '.join(map(str, self.extra_targets)) if self.extra_targets else '')) builder = PEXBuilder(tempfile.mkdtemp(), interpreter=self.interpreter, pex_info=self.target.pexinfo if isinstance(self.target, PythonBinary) else None) if self.options.entry_point: builder.set_entry_point(self.options.entry_point) if self.options.ipython: if not self.config.has_section('python-ipython'): self.error('No python-ipython sections defined in your pants.ini!') builder.info.entry_point = self.config.get('python-ipython', 'entry_point') if builder.info.entry_point is None: self.error('Must specify entry_point for IPython in the python-ipython section ' 'of your pants.ini!') requirements = self.config.getlist('python-ipython', 'requirements', default=[]) for requirement in requirements: self.extra_requirements.append(PythonRequirement(requirement)) executor = PythonChroot( self.target, self.root_dir, builder=builder, interpreter=self.interpreter, extra_targets=self.extra_targets, extra_requirements=self.extra_requirements, conn_timeout=self.options.conn_timeout) executor.dump() if self.options.pex: pex_name = os.path.join(self.root_dir, 'dist', '%s.pex' % self.target.name) builder.build(pex_name) print('Wrote %s' % pex_name) return 0 else: builder.freeze() pex = PEX(builder.path(), interpreter=self.interpreter) po = pex.run(args=list(self.args), blocking=False) try: return po.wait() except KeyboardInterrupt: po.send_signal(signal.SIGINT) raise
def build_pex(args, options): pex_builder = PEXBuilder(path=safe_mkdtemp()) pex_info = pex_builder.info pex_info.zip_safe = options.zip_safe pex_info.always_write_cache = options.always_write_cache pex_info.ignore_errors = options.ignore_errors pex_info.inherit_path = options.inherit_path fetchers = [Fetcher(options.repos)] if options.pypi: fetchers.append(PyPIFetcher()) resolver = Resolver(cache=options.cache_dir, fetchers=fetchers, install_cache=options.cache_dir) resolveds = resolver.resolve(options.requirements) if resolveds: log('Resolved distributions:', v=options.verbosity) for pkg in resolveds: log(' %s' % pkg, v=options.verbosity) pex_builder.add_distribution(pkg) pex_builder.add_requirement(pkg.as_requirement()) for source_dir in options.source_dirs: dist = Installer(source_dir).distribution() if not dist: die('Failed to run installer for %s' % source_dir, CANNOT_DISTILL) egg_path = Distiller(dist).distill() if not egg_path: die('Failed to distill %s into egg' % dist, CANNOT_DISTILL) pex_builder.add_egg(egg_path) if options.entry_point is not None: log('Setting entry point to %s' % options.entry_point, v=options.verbosity) pex_builder.info.entry_point = options.entry_point else: log('Creating environment PEX.', v=options.verbosity) if options.pex_name is not None: log('Saving PEX file to %s' % options.pex_name, v=options.verbosity) tmp_name = options.pex_name + '~' safe_delete(tmp_name) pex_builder.build(tmp_name) os.rename(tmp_name, options.pex_name) else: pex_builder.freeze() log('Running PEX file at %s with args %s' % (pex_builder.path(), args), v=options.verbosity) pex = PEX(pex_builder.path()) return pex.run(args=list(args)) return 0
def write_pex(td, exe_contents, dists=None): dists = dists or [] with open(os.path.join(td, "exe.py"), "w") as fp: fp.write(exe_contents) pb = PEXBuilder(path=td) for dist in dists: pb.add_egg(dist.location) pb.set_executable(os.path.join(td, "exe.py")) pb.freeze() return pb
def write_pex(td, exe_contents, dists=None): dists = dists or [] with open(os.path.join(td, 'exe.py'), 'w') as fp: fp.write(exe_contents) pb = PEXBuilder(path=td) for dist in dists: pb.add_egg(dist.location) pb.set_executable(os.path.join(td, 'exe.py')) pb.freeze() return pb
def _run_python_test(self, target): po = None rv = PythonTestResult.exception() coverage_rc = None coverage_enabled = 'PANTS_PY_COVERAGE' in os.environ try: builder = PEXBuilder(interpreter=self.interpreter) builder.info.entry_point = target.entry_point builder.info.ignore_errors = target._soft_dependencies chroot = PythonChroot(target, self.root_dir, extra_targets=self.generate_test_targets(), builder=builder, platforms=('current', ), interpreter=self.interpreter, conn_timeout=self._conn_timeout) builder = chroot.dump() builder.freeze() test_args = PythonTestBuilder.generate_junit_args(target) test_args.extend(self.args) if coverage_enabled: coverage_rc, args = self.cov_setup(target, builder.chroot()) test_args.extend(args) sources = [ os.path.join(target.target_base, source) for source in target.sources ] po = PEX(builder.path(), interpreter=self.interpreter).run( args=test_args + sources, blocking=False, setsid=True) # TODO(wickman) If coverage is enabled, write an intermediate .html that points to # each of the coverage reports generated and webbrowser.open to that page. rv = PythonTestBuilder.wait_on(po, timeout=target.timeout) except Exception as e: import traceback print('Failed to run test!', file=sys.stderr) traceback.print_exc() rv = PythonTestResult.exception() finally: if coverage_rc: os.unlink(coverage_rc) if po and po.returncode != 0: try: os.killpg(po.pid, signal.SIGTERM) except OSError as e: if e.errno == errno.EPERM: print("Unable to kill process group: %d" % po.pid) elif e.errno != errno.ESRCH: rv = PythonTestResult.exception() self.successes[target._create_id()] = rv return rv
def _run_python_test(self, target): po = None rv = PythonTestResult.exception() coverage_rc = None coverage_enabled = "PANTS_PY_COVERAGE" in os.environ try: builder = PEXBuilder() builder.info().entry_point = "pytest" builder.info().ignore_errors = target._soft_dependencies chroot = PythonChroot( target, self.root_dir, extra_targets=self.generate_test_targets(), builder=builder, conn_timeout=self._conn_timeout, ) builder = chroot.dump() builder.freeze() test_args = PythonTestBuilder.generate_junit_args(target) test_args.extend(self.args) if coverage_enabled: coverage_rc, args = self.cov_setup(target, builder.chroot()) test_args.extend(args) sources = [os.path.join(target.target_base, source) for source in target.sources] po = PEX(builder.path()).run(args=test_args + sources, blocking=False, setsid=True) # TODO(wickman) If coverage is enabled, write an intermediate .html that points to # each of the coverage reports generated and webbrowser.open to that page. rv = PythonTestBuilder.wait_on(po, timeout=target.timeout) except KeyboardInterrupt: print("Test interrupted by user") rv = PythonTestResult.cancelled() except Exception as e: import traceback print("Failed to run test!", file=sys.stderr) traceback.print_exc() rv = PythonTestResult.exception() finally: if coverage_rc: os.unlink(coverage_rc) if po and po.returncode != 0: try: os.killpg(po.pid, signal.SIGTERM) except OSError as e: if e.errno == errno.EPERM: print("Unable to kill process group: %d" % po.pid) elif e.errno != errno.ESRCH: rv = PythonTestResult.exception() self.successes[target._create_id()] = rv return rv
def _run_python_test(self, target): po = None rv = PythonTestResult.exception() coverage_rc = None coverage_enabled = 'PANTS_PY_COVERAGE' in os.environ try: builder = PEXBuilder(interpreter=self.interpreter) builder.info.entry_point = target.entry_point builder.info.ignore_errors = target._soft_dependencies chroot = PythonChroot( target, self.root_dir, extra_requirements=self.generate_test_targets(), builder=builder, platforms=('current',), interpreter=self.interpreter, conn_timeout=self._conn_timeout) builder = chroot.dump() builder.freeze() test_args = PythonTestBuilder.generate_junit_args(target) test_args.extend(self.args) if coverage_enabled: coverage_rc, args = self.cov_setup(target, builder.chroot()) test_args.extend(args) sources = target.sources_relative_to_buildroot() po = PEX(builder.path(), interpreter=self.interpreter).run( args=test_args + sources, blocking=False, setsid=True) # TODO(wickman) If coverage is enabled, write an intermediate .html that points to # each of the coverage reports generated and webbrowser.open to that page. rv = PythonTestBuilder.wait_on(po, timeout=target.timeout) except Exception as e: import traceback print('Failed to run test!', file=sys.stderr) traceback.print_exc() rv = PythonTestResult.exception() finally: if coverage_rc: os.unlink(coverage_rc) if po and po.returncode != 0: try: os.killpg(po.pid, signal.SIGTERM) except OSError as e: if e.errno == errno.EPERM: print("Unable to kill process group: %d" % po.pid) elif e.errno != errno.ESRCH: rv = PythonTestResult.exception() self.successes[target.id] = rv return rv
def _run_python_tests(self, targets, stdout, stderr): coverage_rc = None coverage_enabled = 'PANTS_PY_COVERAGE' in os.environ try: builder = PEXBuilder(interpreter=self.interpreter) builder.info.entry_point = 'pytest' chroot = PythonChroot( targets=targets, extra_requirements=self._TESTING_TARGETS, builder=builder, platforms=('current',), interpreter=self.interpreter, conn_timeout=self._conn_timeout) builder = chroot.dump() builder.freeze() test_args = [] test_args.extend(PythonTestBuilder.generate_junit_args(targets)) test_args.extend(self.args) if coverage_enabled: coverage_rc, args = self.cov_setup(targets) test_args.extend(args) sources = list(itertools.chain(*[t.sources_relative_to_buildroot() for t in targets])) pex = PEX(builder.path(), interpreter=self.interpreter) rc = pex.run(args=test_args + sources, blocking=True, setsid=True, stdout=stdout, stderr=stderr) # TODO(wickman): If coverage is enabled, write an intermediate .html that points to # each of the coverage reports generated and webbrowser.open to that page. rv = PythonTestResult.rc(rc) except Exception: import traceback print('Failed to run test!', file=stderr) traceback.print_exc() rv = PythonTestResult.exception() finally: if coverage_rc: os.unlink(coverage_rc) return rv
def _run_python_tests(self, targets, stdout, stderr): coverage_rc = None coverage_enabled = 'PANTS_PY_COVERAGE' in os.environ try: builder = PEXBuilder(interpreter=self.interpreter) builder.info.entry_point = 'pytest' chroot = PythonChroot( targets=targets, extra_requirements=self._TESTING_TARGETS, builder=builder, platforms=('current',), interpreter=self.interpreter, conn_timeout=self._conn_timeout) builder = chroot.dump() builder.freeze() test_args = [] test_args.extend(PythonTestBuilder.generate_junit_args(targets)) test_args.extend(self.args) if coverage_enabled: coverage_rc, args = self.cov_setup(targets) test_args.extend(args) sources = list(itertools.chain(*[t.sources_relative_to_buildroot() for t in targets])) pex = PEX(builder.path(), interpreter=self.interpreter) rc = pex.run(args=test_args + sources, blocking=True, setsid=True, stdout=stdout, stderr=stderr) # TODO(wickman): If coverage is enabled, write an intermediate .html that points to # each of the coverage reports generated and webbrowser.open to that page. rv = PythonTestResult.rc(rc) except Exception: import traceback print('Failed to run test!', file=stderr) traceback.print_exc() rv = PythonTestResult.exception() finally: if coverage_rc: os.unlink(coverage_rc) return rv
def _run_python_test(self, target): po = None rv = PythonTestResult.exception() try: builder = PEXBuilder() builder.info().entry_point = 'pytest' builder.info().ignore_errors = target._soft_dependencies chroot = PythonChroot(target, self.root_dir, extra_targets=self.generate_test_targets(), builder=builder) builder = chroot.dump() builder.freeze() test_args = PythonTestBuilder.generate_junit_args(target) test_args.extend(self.args) sources = [ os.path.join(target.target_base, source) for source in target.sources ] po = PEX(builder.path()).run(args=test_args + sources, blocking=False, setsid=True) rv = PythonTestBuilder.wait_on(po, timeout=target.timeout) except Exception as e: import traceback print('Failed to run test!', file=sys.stderr) traceback.print_exc() rv = PythonTestResult.exception() finally: if po and po.returncode != 0: try: os.killpg(po.pid, signal.SIGTERM) except OSError as e: if e.errno == errno.EPERM: print("Unable to kill process group: %d" % po.pid) elif e.errno != errno.ESRCH: rv = PythonTestResult.exception() self.successes[target._create_id()] = rv return rv