Exemplo n.º 1
0
def render(template, saltenv='base', sls='', tmplpath=None, rendered_sls=None, **kws):
    sls = to_str(sls)
    mod = types.ModuleType(sls)
    # Note: mod object is transient. It's existence only lasts as long as
    #       the lowstate data structure that the highstate in the sls file
    #       is compiled to.

    mod.__name__ = sls

    # to workaround state.py's use of copy.deepcopy(chunk)
    mod.__deepcopy__ = lambda x: mod

    dsl_sls = pydsl.Sls(sls, saltenv, rendered_sls)
    mod.__dict__.update(
        __pydsl__=dsl_sls,
        include=_wrap_sls(dsl_sls.include),
        extend=_wrap_sls(dsl_sls.extend),
        state=_wrap_sls(dsl_sls.state),
        __salt__=__salt__,
        __grains__=__grains__,
        __opts__=__opts__,
        __pillar__=__pillar__,
        __env__=saltenv,
        __sls__=sls,
        __file__=tmplpath,
        **kws)

    dsl_sls.get_render_stack().append(dsl_sls)
    exec_(template.read(), mod.__dict__)
    highstate = dsl_sls.to_highstate(mod)
    dsl_sls.get_render_stack().pop()
    return highstate
Exemplo n.º 2
0
 def test_to_str(self):
     for x in (123, (1, 2, 3), [1, 2, 3], {1: 23}, None):
         self.assertRaises(TypeError, utils.to_str, x)
     if six.PY3:
         self.assertEqual(utils.to_str('plugh'), 'plugh')
         self.assertEqual(utils.to_str('áéíóúý', 'utf-8'), 'áéíóúý')
         un = '\u4e2d\u56fd\u8a9e (\u7e41\u4f53)'  # pylint: disable=anomalous-unicode-escape-in-string
         ut = bytes((0xe4, 0xb8, 0xad, 0xe5, 0x9b, 0xbd, 0xe8, 0xaa, 0x9e, 0x20, 0x28, 0xe7, 0xb9, 0x81, 0xe4, 0xbd, 0x93, 0x29))
         self.assertEqual(utils.to_str(ut, 'utf-8'), un)
         self.assertEqual(utils.to_str(bytearray(ut), 'utf-8'), un)
         # Test situation when a minion returns incorrect utf-8 string because of... million reasons
         ut2 = b'\x9c'
         self.assertEqual(utils.to_str(ut2, 'utf-8'), u'\ufffd')
         self.assertEqual(utils.to_str(bytearray(ut2), 'utf-8'), u'\ufffd')
     else:
         self.assertEqual(utils.to_str('plugh'), 'plugh')
         self.assertEqual(utils.to_str(u'áéíóúý', 'utf-8'), 'áéíóúý')
         un = u'\u4e2d\u56fd\u8a9e (\u7e41\u4f53)'
         ut = '\xe4\xb8\xad\xe5\x9b\xbd\xe8\xaa\x9e (\xe7\xb9\x81\xe4\xbd\x93)'
         self.assertEqual(utils.to_str(un, 'utf-8'), ut)
         self.assertEqual(utils.to_str(bytearray(ut), 'utf-8'), ut)
Exemplo n.º 3
0
 def test_to_str(self):
     for x in (123, (1, 2, 3), [1, 2, 3], {1: 23}, None):
         self.assertRaises(TypeError, utils.to_str, x)
     if six.PY3:
         self.assertEqual(utils.to_str('plugh'), 'plugh')
         self.assertEqual(utils.to_str('áéíóúý', 'utf-8'), 'áéíóúý')
         un = '\u4e2d\u56fd\u8a9e (\u7e41\u4f53)'  # pylint: disable=anomalous-unicode-escape-in-string
         ut = bytes((0xe4, 0xb8, 0xad, 0xe5, 0x9b, 0xbd, 0xe8, 0xaa, 0x9e, 0x20, 0x28, 0xe7, 0xb9, 0x81, 0xe4, 0xbd, 0x93, 0x29))
         self.assertEqual(utils.to_str(ut, 'utf-8'), un)
         self.assertEqual(utils.to_str(bytearray(ut), 'utf-8'), un)
     else:
         self.assertEqual(utils.to_str('plugh'), 'plugh')
         self.assertEqual(utils.to_str(u'áéíóúý', 'utf-8'), 'áéíóúý')
         un = u'\u4e2d\u56fd\u8a9e (\u7e41\u4f53)'
         ut = '\xe4\xb8\xad\xe5\x9b\xbd\xe8\xaa\x9e (\xe7\xb9\x81\xe4\xbd\x93)'
         self.assertEqual(utils.to_str(un, 'utf-8'), ut)
         self.assertEqual(utils.to_str(bytearray(ut), 'utf-8'), ut)
Exemplo n.º 4
0
 def test_to_str(self):
     for x in (123, (1, 2, 3), [1, 2, 3], {1: 23}, None):
         self.assertRaises(TypeError, utils.to_str, x)
     if six.PY3:
         self.assertEqual(utils.to_str('plugh'), 'plugh')
         self.assertEqual(utils.to_str('áéíóúý', 'utf-8'), 'áéíóúý')
         un = '\u4e2d\u56fd\u8a9e (\u7e41\u4f53)'  # pylint: disable=anomalous-unicode-escape-in-string
         ut = bytes((0xe4, 0xb8, 0xad, 0xe5, 0x9b, 0xbd, 0xe8, 0xaa, 0x9e,
                     0x20, 0x28, 0xe7, 0xb9, 0x81, 0xe4, 0xbd, 0x93, 0x29))
         self.assertEqual(utils.to_str(ut, 'utf-8'), un)
         self.assertEqual(utils.to_str(bytearray(ut), 'utf-8'), un)
     else:
         self.assertEqual(utils.to_str('plugh'), 'plugh')
         self.assertEqual(utils.to_str(u'áéíóúý', 'utf-8'), 'áéíóúý')
         un = u'\u4e2d\u56fd\u8a9e (\u7e41\u4f53)'
         ut = '\xe4\xb8\xad\xe5\x9b\xbd\xe8\xaa\x9e (\xe7\xb9\x81\xe4\xbd\x93)'
         self.assertEqual(utils.to_str(un, 'utf-8'), ut)
         self.assertEqual(utils.to_str(bytearray(ut), 'utf-8'), ut)
Exemplo n.º 5
0
def _subprocess(cmd):
    '''
    Function to standardize the subprocess call
    '''

    try:
        proc = subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE)
        ret = utils.to_str(proc.communicate()[0]).strip()
        retcode = proc.wait()

        if ret:
            return ret
        elif retcode != 1:
            return True
        else:
            return False
    except OSError as err:
        log.error(err)
        return False
Exemplo n.º 6
0
def _subprocess(cmd):
    '''
    Function to standardize the subprocess call
    '''

    try:
        proc = subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE)
        ret = utils.to_str(proc.communicate()[0]).strip()
        retcode = proc.wait()

        if ret:
            return ret
        elif retcode != 1:
            return True
        else:
            return False
    except OSError as err:
        log.error(err)
        return False