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
class PythonBinaryBuilder(object): class NotABinaryTargetException(Exception): pass def __init__(self, target, args, root_dir): self.target = target if not isinstance(target, PythonBinary): raise PythonBinaryBuilder.NotABinaryTargetException( "Target %s is not a PythonBinary!" % target) config = Config.load() self.distdir = config.getdefault('pants_distdir') distpath = tempfile.mktemp(dir=self.distdir, prefix=target.name) self.builder = PEXBuilder(distpath) # configure builder PexInfo options for repo in target._repositories: self.builder.info().add_repository(repo) for index in target._indices: self.builder.info().add_index(index) self.builder.info().allow_pypi = target._allow_pypi self.builder.info().zip_safe = target._zip_safe self.builder.info().inherit_path = target._inherit_path self.builder.info().entry_point = target._entry_point self.builder.info().ignore_errors = target._ignore_errors self.chroot = PythonChroot(target, root_dir, builder=self.builder) def run(self): print('Building PythonBinary %s:' % self.target) env = self.chroot.dump() filename = os.path.join(self.distdir, '%s.pex' % self.target.name) env.build(filename) print('Wrote %s' % filename) return 0
class PythonBinaryBuilder(object): class NotABinaryTargetException(Exception): pass def __init__(self, target, args, root_dir, conn_timeout=None): self.target = target if not isinstance(target, PythonBinary): raise PythonBinaryBuilder.NotABinaryTargetException( "Target %s is not a PythonBinary!" % target) config = Config.load() self.distdir = config.getdefault('pants_distdir') distpath = tempfile.mktemp(dir=self.distdir, prefix=target.name) self.builder = PEXBuilder(distpath) # configure builder PexInfo options for repo in target._repositories: self.builder.info().add_repository(repo) for index in target._indices: self.builder.info().add_index(index) self.builder.info().allow_pypi = target._allow_pypi self.builder.info().zip_safe = target._zip_safe self.builder.info().inherit_path = target._inherit_path self.builder.info().entry_point = target._entry_point self.builder.info().ignore_errors = target._ignore_errors self.chroot = PythonChroot(target, root_dir, builder=self.builder, conn_timeout=conn_timeout) def run(self): print('Building PythonBinary %s:' % self.target) env = self.chroot.dump() filename = os.path.join(self.distdir, '%s.pex' % self.target.name) env.build(filename) print('Wrote %s' % filename) return 0
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() 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() 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