Exemple #1
0
    def replace_values(self, values):
        """Replace values in the snippet text.

        Placeholders in the text are replaced with actual values, and
        the result is returned.
"""
        return replace_values(self.text, values)
Exemple #2
0
    def test_errors_02(self):
        """Not enough placeholders 2"""
        s = """hello <code>someone</code> \
bye <code>someone else</code>
"""
        vals = ['john', 'paul', 'mary']
        t = """hello john \
bye paul
"""
        self.assertEqual(t, replace_values(s, vals))
Exemple #3
0
    def test_replace_04(self):
        """Continuation lines"""

        s = """abc \
def"""
        vals = []
        t = """abc \
def"""
        self.assertEqual(t, replace_values(s, vals))
        
        s = """abc <code>hello</code> \
def"""
        vals = ['bye']
        t = """abc bye \
def"""
        self.assertEqual(t, replace_values(s, vals))

        s = """abc \
def <code>bonjour</code>"""
        vals = ['bonsoir']
        t = """abc \
def bonsoir"""
        self.assertEqual(t, replace_values(s, vals))
        
        # Deux continuations, trois lignes
        s = """abc \
def \
ghi"""
        vals = []
        t = """abc \
def \
ghi"""
        self.assertEqual(t, replace_values(s, vals))

        # \u can be interpreted as a unicode escape sequence, use a raw string
        # to avoid that
        s = r'abc \u xyz \
<code>hello</code> abc'
        vals = ['goodbye']
        t = r'abc \u xyz \
goodbye abc'
        self.assertEqual(t, replace_values(s, vals))
Exemple #4
0
    def test_replace_02(self):
        """Double replacements"""
        s = '<code></code><code></code>'
        vals = ['a', 'b']
        t = 'ab'
        self.assertEqual(t, replace_values(s, vals))

        s = '<code>x</code><code>y</code>'
        vals = ['a', 'b']
        t = 'ab'
        self.assertEqual(t, replace_values(s, vals))

        s = 'abc<code>x</code>abc<code>x</code>abc'
        vals = ['1', '2']
        t = 'abc1abc2abc'
        self.assertEqual(t, replace_values(s, vals))

        s = '  \t<code> x</code>   \t<code> x</code>\t   '
        vals = ['8', '9']
        t = '  \t8   \t9\t   '
        self.assertEqual(t, replace_values(s, vals))
Exemple #5
0
    def test_replace_01(self):
        """Simple replacements"""
        s = 'hello'
        vals = []
        t = 'hello'
        self.assertEqual(t, replace_values(s, vals))

        s = '<code></code>'
        vals = ['x']
        t = 'x'
        self.assertEqual(t, replace_values(s, vals))

        s = '<code>x</code>'
        vals = ['y']
        t = 'y'
        self.assertEqual(t, replace_values(s, vals))

        s = 'abc<code>x</code>abc'
        vals = ['y']
        t = 'abcyabc'
        self.assertEqual(t, replace_values(s, vals))

        s = '  \t<code> x</code>   \t'
        vals = ['y']
        t = '  \ty   \t'
        self.assertEqual(t, replace_values(s, vals))

        # \u can be interpreted as a unicode escape sequence, use a raw string
        # to avoid that
        s = r'abc \u xyz <code>hello</code> abc'
        vals = ['goodbye']
        t = r'abc \u xyz goodbye abc'
        self.assertEqual(t, replace_values(s, vals))
Exemple #6
0
    def test_replace_03(self):
        """Multiline replacements"""
        s = """<code>x</code>
<code>y</code>
<code>y</code>"""
        vals = ['x', 'y', 'z']
        t = """x
y
z"""
        self.assertEqual(t, replace_values(s, vals))

        s = """
zertzertabc<code>x</code>oiuoyi
rgsfg<code>y</code>dfgsdfgs
cbxcvbx<code>y</code>sdfgsd
sgsdfgsdf"""
        vals = ['x', 'y', 'z']
        t = """
zertzertabcxoiuoyi
rgsfgydfgsdfgs
cbxcvbxzsdfgsd
sgsdfgsdf"""
        self.assertEqual(t, replace_values(s, vals))
Exemple #7
0
 def test_errors_05(self):
     """Not enough values 2"""
     s = 'hello <code>someone</code><code>bye</code>foo'
     vals = ['first']
     with self.assertRaises(RuntimeError):
         replace_values(s, vals)
Exemple #8
0
 def test_errors_04(self):
     """Not enough values 1"""
     s = 'hello <code>someone</code>'
     vals = []
     with self.assertRaises(RuntimeError):
         replace_values(s, vals)
Exemple #9
0
 def test_errors_03(self):
     """No values, no placeholders"""
     s = 'hello'
     vals = []
     t = 'hello'
     self.assertEqual(t, replace_values(s, vals))
Exemple #10
0
 def test_errors_01(self):
     """Not enough placeholders 1"""
     s = 'hello <code>someone</code>'
     vals = ['john', 'paul']
     t = 'hello john'
     self.assertEqual(t, replace_values(s, vals))
Exemple #11
0
 def test_errors_00(self):
     """No  placeholder"""
     s = 'hello'
     vals = ['bye']
     t = 'hello'
     self.assertEqual(t, replace_values(s, vals))
Exemple #12
0
 def test_errors_01(self):
     """No placeholder"""
     s = 'hello'
     vals = ['bye']
     with self.assertRaises(RuntimeError):
         replace_values(s, vals)
Exemple #13
0
    def test_replace_05(self):
        """LFS directives examples"""
        s = """echo "export LFS='<code>/mnt/lfs</code>'" >> $HOME/.bashrc
export LFS=<code>/mnt/lfs</code>
echo \$LFS=$LFS"""
        vals = ['/mnt/lfs', '/mnt/lfs']
        t = """echo "export LFS='/mnt/lfs'" >> $HOME/.bashrc
export LFS=/mnt/lfs
echo \$LFS=$LFS"""
        self.assertEqual(t, replace_values(s, vals))

        s = """cp <code>pkg_repository</code>/*.* $LFS/sources"""
        vals = ['/home/joao/lfs/9.0-systemd/pkgs']
        t = """cp /home/joao/lfs/9.0-systemd/pkgs/*.* $LFS/sources"""
        self.assertEqual(t, replace_values(s, vals))

        # Example from 6.04, with no replacement
        s = r"""chroot "$LFS" /tools/bin/env -i \
HOME=/root \
TERM="$TERM" \
PS1='(lfs chroot) \u:\w\$ ' \
PATH=/bin:/usr/bin:/sbin:/usr/sbin:/tools/bin \
/tools/bin/bash --login +h /var/tmp/lfs/script_03.sh"""
        vals = []
        t = r"""chroot "$LFS" /tools/bin/env -i \
HOME=/root \
TERM="$TERM" \
PS1='(lfs chroot) \u:\w\$ ' \
PATH=/bin:/usr/bin:/sbin:/usr/sbin:/tools/bin \
/tools/bin/bash --login +h /var/tmp/lfs/script_03.sh"""
        self.assertEqual(t, replace_values(s, vals))

        # Example from 6.04, with an added replacement
        s = r"""chroot "$LFS" <code>env</code> -i \
HOME=/root \
TERM="$TERM" \
PS1='(lfs chroot) \u:\w\$ ' \
PATH=/bin:/usr/bin:/sbin:/usr/sbin:/tools/bin \
/tools/bin/bash --login +h /var/tmp/lfs/script_03.sh"""
        vals = ['/tools/bin/env']
        t = r"""chroot "$LFS" /tools/bin/env -i \
HOME=/root \
TERM="$TERM" \
PS1='(lfs chroot) \u:\w\$ ' \
PATH=/bin:/usr/bin:/sbin:/usr/sbin:/tools/bin \
/tools/bin/bash --login +h /var/tmp/lfs/script_03.sh"""
        self.assertEqual(t, replace_values(s, vals))

        # Example from 6.04, adding the LFS variable
        s = r"""chroot "$LFS" /tools/bin/env -i \
LFS=<code></code> \
HOME=/root \
TERM="$TERM" \
PS1='(lfs chroot) \u:\w\$ ' \
PATH=/bin:/usr/bin:/sbin:/usr/sbin:/tools/bin \
/tools/bin/bash --login +h /var/tmp/lfs/script_03.sh"""
        vals = ['/mnt/lfs']
        t = r"""chroot "$LFS" /tools/bin/env -i \
LFS=/mnt/lfs \
HOME=/root \
TERM="$TERM" \
PS1='(lfs chroot) \u:\w\$ ' \
PATH=/bin:/usr/bin:/sbin:/usr/sbin:/tools/bin \
/tools/bin/bash --login +h /var/tmp/lfs/script_03.sh"""
        self.assertEqual(t, replace_values(s, vals))
Exemple #14
0
    def apply_directive(self,
                        directive,
                        bk,
                        code=None,
                        s=None,
                        repl_records=None):
        """Apply directives.

        The value in directive[2] depends on the command:
        'replace': the argument is the string to use as a replacement
        'push': the argument is the filepath to save into
"""
        snip_id = directive[0]
        cmd = directive[1]

        if cmd == 'push':
            # Perform replacements on the directive argument if necessary
            arg = directive[2]
            if repl_records is not None:
                for rec in repl_records:
                    # rec[0] = snippet id, rec[1] = array of replacement values
                    if rec[0] == snip_id:
                        arg = replace_values(arg, rec[1])
                        break
            # Save the current (code, filepath) on the stack
            bk.code_stack.append((code + s, arg))
            code = s = ''

        elif cmd == 'pop':
            # Finalize code being generated
            code += s
            # Pop up one stack level and resume that level's code generation
            prev_code, filepath = bk.code_stack.pop()
            print(f'{self.id}: apply_directive: writing "{filepath}"')

            # Write out to file
            chrooted = self.id.startswith('6.') or self.id.startswith('9.')
            with open(filepath, 'w') as f:
                f.write(
                    file_header(filepath, bk, chrooted=chrooted) + code +
                    file_footer(filepath))

            # Reset code generation for this stack level
            code = prev_code
            s = ''

        elif cmd == 'add':
            arg = directive[2]
            t = Snippet('userinput', arg)

            # Replace placeholders with actual values
            if repl_records is not None:
                for rec in repl_records:
                    # rec[0] = snippet id, rec[1] = array of replacement values
                    if rec[0] == snip_id:
                        t.text = t.replace_values(rec[1])
                        break
            s += t.generate(f'{self.id}_{snip_id} [add]')
        else:
            print(f'Sect: {self.id}, snippet directive: {snip_id}' +
                  f', unknown command "{cmd}"')

        return code, s