def update_buildout_data(cls, buildout): import gitpy repository = cls.get_repository() branch = repository.getCurrentBranch() try: remote = branch.getRemoteBranch() if branch is not None else None except gitpy.exceptions.NonexistentRefException: remote = None head = repository.getHead() from zc.buildout.buildout import Options data = {} data['version'] = cls.extract_version_tag().lstrip('v') data['author'] = head.getAuthorName() data['author_email'] = head.getAuthorEmail() data['git_local_branch'] = repr(branch.name if branch is not None else '(Not currently on any branch)') data['git_remote_tracking_branch'] = repr(remote.getNormalizedName() if remote is not None else '(No remote tracking)') data['git_remote_url'] = repr(remote.remote.url if remote is not None else '(Not remote tracking)') data['head_subject'] = repr(cls.strip_mako_characters(head.getSubject())) data['head_message'] = repr(cls.strip_mako_characters(head.getMessageBody())) data['head_hash'] = repr(head.hash) data['git_commit_date'] = repr(datetime.datetime.fromtimestamp(head.getDate()).isoformat(' ')) diff = execute_async("git diff --patch --no-color", shell=True) diff.wait() data['dirty_diff'] = repr(cls.strip_mako_characters(diff.get_stdout().decode("utf-8"))) data['homepage'] = repr(cls.get_homepage()) if buildout.get("project").get("homepage"): data['homepage'] = repr(buildout.get("project").get("homepage")) buildout._data.update({SECTION_NAME: Options(buildout, SECTION_NAME, data)})
def test__async_execute_with_little_output(self): part_a = 'legen' part_b = 'wait for it' part_c = 'dary' command = ["-c", "from sys import stdout; from time import sleep; stdout.write('%s');stdout.flush(); sleep(2); stdout.write('%s');stdout.flush(); sleep(2); stdout.write('%s'); stdout.flush(); sleep(2)" % \ (part_a, part_b, part_c)] command.insert(0,sys.executable) num_secs = 6 with self.assertTakesAlmost(num_secs, 1): with self.assertImmediate(): result = execute_async(command) with self.assertTakesAlmost(1): self.assertRaises(CommandTimeout, result.wait, **{'timeout':1}) self.assertIn(part_a, result.get_stdout()) #self.assertNotIn(part_b, result.get_stdout()) with self.assertTakesAlmost(2): self.assertRaises(CommandTimeout, result.wait, **{'timeout':2}) self.assertIn(part_a, result.get_stdout()) self.assertIn(part_b, result.get_stdout()) self.assertNotIn(part_c, result.get_stdout()) with self.assertTakesAlmost(2): self.assertRaises(CommandTimeout, result.wait, **{'timeout':2}) self.assertIn(part_a, result.get_stdout()) self.assertIn(part_b, result.get_stdout()) self.assertIn(part_c, result.get_stdout()) result.wait()
def log_execute(args, timeout_in_seconds=None): from infi.execute import execute_async, CommandTimeout pid = execute_async(args) try: pid.wait(timeout_in_seconds) except CommandTimeout: pid.kill(9) return pid.get_pid()
def test__async_execute(self): num_secs = 3 with self.assertTakesAlmost(num_secs, 1): with self.assertImmediate(): result = execute_async("sleep {}".format(num_secs), shell=True) self.assertIsNone(result.get_returncode()) self.assertFalse(result.is_finished()) self.assertIsNone(result.poll()) self.assertEquals(result.wait(), True) self.assertEquals(0, result.poll())
def _execute(self): from infi.execute import execute_async, CommandTimeout from os import path executable = self.executable if path.exists(self.executable) else find_executable(self.executable) logger.info("Going to run {} {}".format(executable, self.commandline_arguments)) cmd = execute_async([executable] + self.commandline_arguments) try: cmd.wait(self.wait_time_in_seconds) except OSError, error: logger.exception("Command did not run")
def test_async_with_another_greenlet_running(self): import gevent event = gevent.event.Event() def func(): event.set() result = execute_async("sleep 1", shell=True) greenlet = gevent.spawn(func) self.assertFalse(event.is_set()) result.wait() self.assertTrue(event.is_set()) greenlet.join()
def test__async_execute(self): num_secs = 3 with self.assertTakesAlmost(num_secs, 1): with self.assertImmediate(): result = execute_async("sleep {0}".format(num_secs), shell=True) self.assertIsNone(result.get_returncode()) self.assertFalse(result.is_finished()) self.assertIsNone(result.poll()) self.assertEquals(result.wait(), True) self.assertEquals(0, result.poll())
def with_webserver_running(self, configfile=None): tempfile_context = self.with_new_devlopment_config_file() if configfile is None: configfile = tempfile_context.__enter__() self.addCleanup(tempfile_context.__exit__, None, None, None) pid = execute_async(["bin/app_repo", "-f", configfile, "webserver", "start"]) sleep(2) try: # wait(lambda: 'Bus STARTED' in pid.get_stderr(), timeout_seconds=3) yield finally: pid.kill(9)
def test__async_wait_periods(self): num_secs = 3 with self.assertTakesAlmost(num_secs): with self.assertImmediate(): result = execute_async("sleep {}".format(num_secs), shell=True) self.assertFalse(result.is_finished()) with self.assertTakesAlmost(1): self.assertRaises(CommandTimeout, result.wait, **{'timeout':1}) self.assertFalse(result.is_finished()) with self.assertTakesAlmost(1): self.assertRaises(CommandTimeout, result.wait, **{'timeout':1}) self.assertFalse(result.is_finished()) result.wait() self.assertTrue(result.is_finished())
def _execute(self): from infi.execute import execute_async, CommandTimeout from os import path executable = self.executable if path.exists( self.executable) else find_executable(self.executable) logger.info("Going to run {} {}".format(executable, self.commandline_arguments)) try: cmd = execute_async([executable] + self.commandline_arguments, env=self.env) except OSError: logger.error("executable {} not found".format(executable)) return FakeResult try: cmd.wait(self.wait_time_in_seconds) except OSError, error: logger.exception("Command did not run")
def test__async_wait_periods(self): num_secs = 3 with self.assertTakesAlmost(num_secs): with self.assertImmediate(): result = execute_async("sleep {0}".format(num_secs), shell=True) self.assertFalse(result.is_finished()) with self.assertTakesAlmost(1): self.assertRaises(CommandTimeout, result.wait, **{'timeout': 1}) self.assertFalse(result.is_finished()) with self.assertTakesAlmost(1): self.assertRaises(CommandTimeout, result.wait, **{'timeout': 1}) self.assertFalse(result.is_finished()) result.wait() self.assertTrue(result.is_finished())
def start(self): from os import path from sys import executable, path as sys_path from infi.execute import execute_async script = path.join(self.tempdir, 'script.py') with open(script, 'w') as fd: kwargs = dict(path=sys_path, server_port=self.server.get_port(), gevent_friendly=self.gevent_friendly, worker_id=self._id, tempdir=self.tempdir, logging_port=self.logging_port) fd.write(SCRIPT.format(**kwargs)) logger.debug("starting worker {}: {} {}".format( self._id, executable, script)) self._result = execute_async([executable, script])
def test__async_execute_with_little_output(self): try: import gevent.subprocess raise test_utils.SkipTest() except ImportError: pass part_a = b'legen' part_b = b'wait for it' part_c = b'dary' if PY3: command = ["-c", "from sys import stdout; from time import sleep; stdout.buffer.write(b'%s');stdout.flush(); sleep(2); stdout.buffer.write(b'%s');stdout.flush(); sleep(2); stdout.buffer.write(b'%s'); stdout.flush(); sleep(2)" % \ (part_a.decode("ASCII"), part_b.decode("ASCII"), part_c.decode("ASCII"))] else: command = ["-c", "from sys import stdout; from time import sleep; stdout.write(b'%s');stdout.flush(); sleep(2); stdout.write(b'%s');stdout.flush(); sleep(2); stdout.write(b'%s'); stdout.flush(); sleep(2)" % \ (part_a, part_b, part_c)] command.insert(0, sys.executable) num_secs = 6 with self.assertTakesAlmost(num_secs, 1): with self.assertImmediate(): result = execute_async(command) with self.assertTakesAlmost(1): self.assertRaises(CommandTimeout, result.wait, **{'timeout': 1}) self.assertIn(part_a, result.get_stdout()) #self.assertNotIn(part_b, result.get_stdout()) with self.assertTakesAlmost(2): self.assertRaises(CommandTimeout, result.wait, **{'timeout': 2}) self.assertIn(part_a, result.get_stdout()) self.assertIn(part_b, result.get_stdout()) self.assertNotIn(part_c, result.get_stdout()) with self.assertTakesAlmost(2): self.assertRaises(CommandTimeout, result.wait, **{'timeout': 2}) self.assertIn(part_a, result.get_stdout()) self.assertIn(part_b, result.get_stdout()) self.assertIn(part_c, result.get_stdout()) result.wait()
def _execute(self): from infi.execute import execute_async, CommandTimeout from os import path executable = self.executable if path.exists(self.executable) else find_executable(self.executable) logger.info("Going to run {} {}".format(executable, self.commandline_arguments)) try: cmd = execute_async([executable] + self.commandline_arguments, env=self.env) except OSError: logger.error("executable {} not found".format(executable)) return FakeResult try: cmd.wait(self.wait_time_in_seconds) except OSError as error: logger.exception("Command did not run") except CommandTimeout as error: logger.exception("Command did not finish in {} seconds, killing it".format(self.wait_time_in_seconds)) cmd.kill() if not cmd.is_finished(): cmd.kill(9) if not cmd.is_finished(): logger.info("{!r} is stuck".format(cmd)) else: logger.info("{!r} was killed".format(cmd)) return cmd
def update_buildout_data(cls, buildout): import gitpy repository = cls.get_repository() branch = repository.getCurrentBranch() try: remote = branch.getRemoteBranch() if branch is not None else None except gitpy.exceptions.NonexistentRefException: remote = None head = repository.getHead() from zc.buildout.buildout import Options data = {} data['version'] = cls.extract_version_tag().lstrip('v') data['author'] = head.getAuthorName() data['author_email'] = head.getAuthorEmail() data['git_local_branch'] = repr(branch.name if branch is not None else '(Not currently on any branch)') data['git_remote_tracking_branch'] = repr(remote.getNormalizedName( ) if remote is not None else '(No remote tracking)') data['git_remote_url'] = repr(remote.remote.url if remote is not None else '(Not remote tracking)') data['head_subject'] = repr( cls.strip_mako_characters(head.getSubject())) data['head_message'] = repr( cls.strip_mako_characters(head.getMessageBody())) data['head_hash'] = repr(head.hash) data['git_commit_date'] = repr( datetime.datetime.fromtimestamp(head.getDate()).isoformat(' ')) diff = execute_async("git diff --patch --no-color", shell=True) diff.wait() data['dirty_diff'] = repr( cls.strip_mako_characters(diff.get_stdout().decode("utf-8"))) data['homepage'] = repr(cls.get_homepage()) if buildout.get("project").get("homepage"): data['homepage'] = repr(buildout.get("project").get("homepage")) buildout._data.update( {SECTION_NAME: Options(buildout, SECTION_NAME, data)})
def test__async_execute_with_little_output(self): try: import gevent.subprocess raise test_utils.SkipTest() except ImportError: pass part_a = b'legen' part_b = b'wait for it' part_c = b'dary' if PY3: command = ["-c", "from sys import stdout; from time import sleep; stdout.buffer.write(b'%s');stdout.flush(); sleep(2); stdout.buffer.write(b'%s');stdout.flush(); sleep(2); stdout.buffer.write(b'%s'); stdout.flush(); sleep(2)" % \ (part_a.decode("ASCII"), part_b.decode("ASCII"), part_c.decode("ASCII"))] else: command = ["-c", "from sys import stdout; from time import sleep; stdout.write(b'%s');stdout.flush(); sleep(2); stdout.write(b'%s');stdout.flush(); sleep(2); stdout.write(b'%s'); stdout.flush(); sleep(2)" % \ (part_a, part_b, part_c)] command.insert(0,sys.executable) num_secs = 6 with self.assertTakesAlmost(num_secs, 1): with self.assertImmediate(): result = execute_async(command) with self.assertTakesAlmost(1): self.assertRaises(CommandTimeout, result.wait, **{'timeout':1}) self.assertIn(part_a, result.get_stdout()) #self.assertNotIn(part_b, result.get_stdout()) with self.assertTakesAlmost(2): self.assertRaises(CommandTimeout, result.wait, **{'timeout':2}) self.assertIn(part_a, result.get_stdout()) self.assertIn(part_b, result.get_stdout()) self.assertNotIn(part_c, result.get_stdout()) with self.assertTakesAlmost(2): self.assertRaises(CommandTimeout, result.wait, **{'timeout':2}) self.assertIn(part_a, result.get_stdout()) self.assertIn(part_b, result.get_stdout()) self.assertIn(part_c, result.get_stdout()) result.wait()
def start_python(self): basename = 'python' + EXTENSION python = os.path.abspath(os.path.join(os.path.curdir, 'bin', basename)) pid = execute_async([python, '-c', 'import time; time.sleep(60)']) return pid
def test__timeout_async(self): result = execute_async("sleep 100", shell=True, timeout=1) with self.assertRaises(CommandTimeout) as caught: result.wait() self.assertFalse(caught.exception.result.is_finished()) caught.exception.result.kill()