Exemple #1
0
def test_compilation_writing_to_file():
    with temporary.temp_file('to_compile') as in_file:
        with temporary.temp_file() as out_file:
            __main__.compile_file(in_file=in_file,
                                  out_file=out_file,
                                  compile_fn=lambda x: 'Compiled(' + x + ')')
            with open(str(out_file)) as f:
                assert f.read() == 'Compiled(to_compile)'
Exemple #2
0
def test_compilation_writing_to_file():
    with temporary.temp_file('to_compile') as in_file:
        with temporary.temp_file() as out_file:
            __main__.compile_file(
                in_file=in_file,
                out_file=out_file,
                compile_fn=lambda x: 'Compiled(' + x + ')')
            with open(str(out_file)) as f:
                assert f.read() == 'Compiled(to_compile)'
Exemple #3
0
 def run (self):
   if not self.conf.force:
     self.error ("  Must force writes (--force).")
     clip.exit (err = True)
   rc = False
   content = None
   in_file = Path (self.kwargs.in_file)
   if not in_file.isfile ():
     self.error ("Template file does not exist.")
     clip.exit (err = True)
   self.info ("  Generate...")
   if self.kwargs.out_file == "=":
     f = Path (self.kwargs.in_file)
     out_file = f.parent / f.namebase
     rc = self.process_one_file (in_file, out_file, self.make_file)
     content = out_file.bytes ()
   elif self.kwargs.out_file:
     out_file = Path (self.kwargs.out_file)
     rc = self.process_one_file (in_file, out_file, self.make_file)
     content = out_file.bytes ()
   else:
     with temporary.temp_file () as fname:
       out_file = Path (fname)
       rc = self.process_one_file (in_file, out_file, self.make_file)
       content = out_file.bytes ()
   if not self.kwargs.out_file:
     self.info ("Produced file:")
     self.report (content)
   clip.exit (err = (True if rc else False))
Exemple #4
0
def test_compilation_writing_to_stdout():
    with temporary.temp_file('to_compile') as in_file:
        with test.captured_stdout() as stdout:
            __main__.compile_file(in_file=in_file,
                                  out_file='-',
                                  compile_fn=lambda x: 'Compiled(' + x + ')')
        assert stdout.getvalue().strip() == 'Compiled(to_compile)'
Exemple #5
0
def test_compilation_writing_to_stdout():
    with temporary.temp_file('to_compile') as in_file:
        with test.captured_stdout() as stdout:
            __main__.compile_file(
                in_file=in_file,
                out_file='-',
                compile_fn=lambda x: 'Compiled(' + x + ')')
        assert stdout.getvalue().strip() == 'Compiled(to_compile)'
Exemple #6
0
def test_run_file():
    @contextlib.contextmanager
    def temp_file_ctx(run_str):
        yield 'Temp(' + run_str + ')'

    with temporary.temp_file('to_compile') as to_run:
        actual = __main__.run_file(to_run=to_run,
                                   args=['one', 'two'],
                                   compile_fn=lambda x: 'Compiled(' + x + ')',
                                   run_fn=lambda x: 'Ran(' + str(x) + ')',
                                   temp_file_ctx=temp_file_ctx)

    assert actual == "Ran(('bash', 'Temp(Compiled(to_compile))', 'one', 'two'))"
Exemple #7
0
def test_run_file():
    @contextlib.contextmanager
    def temp_file_ctx(run_str):
        yield 'Temp(' + run_str + ')'

    with temporary.temp_file('to_compile') as to_run:
        actual = __main__.run_file(
            to_run=to_run,
            args=['one', 'two'],
            compile_fn=lambda x: 'Compiled(' + x + ')',
            run_fn=lambda x: 'Ran(' + str(x) + ')',
            temp_file_ctx=temp_file_ctx)

    assert actual == "Ran(('bash', 'Temp(Compiled(to_compile))', 'one', 'two'))"
Exemple #8
0
def test_temp_file_passes_through_mkstemp_args(master_mock):
    master_mock.mkstemp.side_effect = (DummyException(), )
    try:
        ctx = temporary.temp_file(suffix='suffix',
                                  prefix='prefix',
                                  parent_dir='parent_dir')
        with ctx:
            pass  # pragma: no cover
    except DummyException:
        master_mock.mkstemp.assert_called_once_with('suffix',
                                                    'prefix',
                                                    'parent_dir',
                                                    text=False)
        raise
Exemple #9
0
def test_direct_run():  # pragma: no cover
    if not __is_bash_in_path():
        pytest.skip('bash executable not found')

    if not __is_bashup_in_path():
        pytest.skip('bashup executable not found')

    with temporary.temp_file(__BASHUP_STR) as in_file:
        p = subprocess.Popen(args=('bashup', '--run', str(in_file)),
                             stdout=subprocess.PIPE,
                             stderr=subprocess.PIPE)

        stdout, _ = [o.decode('utf-8').strip() for o in p.communicate()]

        test.assert_eq(stdout, __EXPECTED_OUTPUT)
    assert p.returncode == 55
Exemple #10
0
def test_direct_run():  # pragma: no cover
    if not __is_bash_in_path():
        pytest.skip('bash executable not found')

    if not __is_bashup_in_path():
        pytest.skip('bashup executable not found')

    with temporary.temp_file(__BASHUP_STR) as in_file:
        p = subprocess.Popen(
            args=('bashup', '--run', str(in_file)),
            stdout=subprocess.PIPE,
            stderr=subprocess.PIPE)

        stdout, _ = [o.decode('utf-8').strip() for o in p.communicate()]

        test.assert_eq(stdout, __EXPECTED_OUTPUT)
    assert p.returncode == 55
Exemple #11
0
def __assert_compiled_bash(bash_binary,
                           bashup_str,
                           expected_output,
                           expected_return_code,
                           script_args=()):  # pragma: no cover

    with temporary.temp_file(bashup_str) as in_file:
        subprocess.check_call(args=('bashup', '--in', str(in_file), '--out',
                                    'out.sh'))

    p = subprocess.Popen(args=(bash_binary, 'out.sh') + tuple(script_args),
                         stdout=subprocess.PIPE,
                         stderr=subprocess.PIPE)

    stdout, _ = [o.decode('UTF-8').strip() for o in p.communicate()]

    test.assert_eq(stdout, expected_output)
    assert p.returncode == expected_return_code
Exemple #12
0
def __assert_compiled_bash(
        bash_binary,
        bashup_str,
        expected_output,
        expected_return_code,
        script_args=()):                                # pragma: no cover

    with temporary.temp_file(bashup_str) as in_file:
        subprocess.check_call(args=(
            'bashup',
            '--in', str(in_file),
            '--out', 'out.sh'))

    p = subprocess.Popen(
        args=(bash_binary, 'out.sh') + tuple(script_args),
        stdout=subprocess.PIPE,
        stderr=subprocess.PIPE)

    stdout, _ = [o.decode('UTF-8').strip() for o in p.communicate()]

    test.assert_eq(stdout, expected_output)
    assert p.returncode == expected_return_code
Exemple #13
0
def test_temp_file_with_bytearray_content():
    with temporary.temp_file(bytearray(b'hello!')) as temp_file:
        with temp_file.open('rb') as f:
            assert f.read() == b'hello!'
Exemple #14
0
 def blow_up(master_mock):
     master_mock.Path.return_value.unlink.side_effect = (OSError(
         -1, 'Fake'), )
     with temporary.temp_file(parent_dir=parent_dir):
         pass
Exemple #15
0
def test_temp_file_manually_deleted_is_allowed():
    with temporary.temp_file() as temp_file:
        temp_file.unlink()
Exemple #16
0
def test_temp_file_with_string_content():
    with temporary.temp_file('hello!') as temp_file:
        with temp_file.open() as f:
            assert f.read() == 'hello!'
Exemple #17
0
def test_temp_file_in_custom_parent_dir():
    with temporary.temp_dir() as parent_dir:
        with temporary.temp_file(parent_dir=parent_dir) as temp_file:
            assert temp_file.parent == parent_dir
Exemple #18
0
def test_temp_file_creates_and_deletes_file():
    with temporary.temp_file() as temp_file:
        assert temp_file.is_file()
    assert not temp_file.exists()
Exemple #19
0
def run_commands(commands,
                 processes=None,
                 timeout=None,
                 meta=None,
                 observer=None):
    pool = Pool(processes=processes)
    manager, queue, m = None, None, None
    manager = Manager()
    m = manager.Queue()
    if observer:
        queue = manager.Queue()

    if meta:
        commands = [(i, meta, command, timeout, queue, m)
                    for i, (command, meta) in enumerate(zip(commands, meta))]
    else:
        commands = [(i, meta, command, timeout, queue, m)
                    for i, command in enumerate(commands)]

    with temp_file() as f:
        filename = str(f)

    m_process = Process(target=monitor, args=(filename, m))
    m_process.daemon = True
    m_process.start()

    def clean_exit():
        status("Keyboard interrupt intercepted, shutting down")
        try:
            m_process.terminate()
            m_process.join()
        except Exception:
            status("Monitor process could not be shut down")
            print_exc()

        try:
            pool.terminate()
            pool.join()
        except Exception:
            status("Pool could not be shut down")
            print_exc()

        status("Shutting down potential orphan processes")
        active = set()
        with open(filename) as ref:
            for line in ref:
                parts = line.split(" ")
                if parts[0] == "ADD":
                    active.add(int(parts[1]))
                elif parts[0] == "REM":
                    active.remove(int(parts[1]))

        for pid in active:
            try:
                print("Killing", pid)
                os.killpg(pid,
                          signal.SIGTERM)  # send signal to the process group
            except OSError as e:
                if e.errno != errno.ESRCH:
                    if e.errno == errno.EPERM:
                        os.waitpid(-pid, 0)
                else:
                    raise e
            except Exception:
                print_exc()
                pass

        os.unlink(filename)

        status("Completely shut down")

    r = pool.map_async(worker, commands)
    atexit.register(clean_exit)

    if observer:
        observe(observer, queue, len(commands))

    r.wait()
    status("### DONE ##")
    m.put(Update.SENTINEL)
    m_process.join()