Commit f06e61a6 authored by Jérome Perrin's avatar Jérome Perrin

userinfo: expose values as string

To be compatible with buildout (and not only `slapos.recipe.template:jinja2`)

See [#20180416-1138FFA](https://nexedi.erp5.net/bug_module/20180416-1138FFA/)

---

Running this buildout:

```
[buildout]
parts=x

[userinfo]
recipe = slapos.cookbook:userinfo

[x]
recipe = plone.recipe.command
command = echo "Hello user id ${userinfo:pw-uid}"
```


In a slapos patched buildout it outputs: `"Hello user id !py!991"` (which means it's not usable for references directly in other buildout parts)

In a "non patched" buildout fail with error:

```
While:
  Installing.
  Getting section x.
  Initializing section x.
  Getting option x:command.
  Getting section userinfo.
  Initializing section userinfo.

An internal error occurred due to a bug in either zc.buildout or in a
recipe being used:
Traceback (most recent call last):
  File "/tmp/x/env/lib/python2.7/site-packages/zc/buildout/buildout.py", line 2127, in main
    getattr(buildout, command)(args)
  File "/tmp/x/env/lib/python2.7/site-packages/zc/buildout/buildout.py", line 681, in install
    [self[part]['recipe'] for part in install_parts]
  File "/tmp/x/env/lib/python2.7/site-packages/zc/buildout/buildout.py", line 1278, in __getitem__
    options._initialize()
  File "/tmp/x/env/lib/python2.7/site-packages/zc/buildout/buildout.py", line 1376, in _initialize
    self._dosub(k, v)
  File "/tmp/x/env/lib/python2.7/site-packages/zc/buildout/buildout.py", line 1431, in _dosub
    v = '$$'.join([self._sub(s, seen) for s in v.split('$$')])
  File "/tmp/x/env/lib/python2.7/site-packages/zc/buildout/buildout.py", line 1495, in _sub
    v = self.buildout[section].get(option, None, seen)
  File "/tmp/x/env/lib/python2.7/site-packages/zc/buildout/buildout.py", line 1278, in __getitem__
    options._initialize()
  File "/tmp/x/env/lib/python2.7/site-packages/zc/buildout/buildout.py", line 1386, in _initialize
    self.initialize()
  File "/tmp/x/env/lib/python2.7/site-packages/zc/buildout/buildout.py", line 1395, in initialize
    self.recipe = recipe_class(buildout, name, self)
  File "/tmp/x/eggs/slapos.cookbook-1.0.62-py2.7.egg/slapos/recipe/userinfo.py", line 13, in __init__
    options['pw-uid'] = pinfo.pw_uid
  File "/tmp/x/env/lib/python2.7/site-packages/zc/buildout/buildout.py", line 1520, in __setitem__
    raise TypeError('Option values must be strings', value)
TypeError: ('Option values must be strings', 991)
```

/cc @Nicolas @alain.takoudjou  @vpelletier 

/reviewed-on nexedi/slapos!321
parent ebb0421a
......@@ -10,13 +10,13 @@ class Recipe(object):
def __init__(self, buildout, name, options):
pinfo = pwd.getpwuid(os.getuid())
options['pw-name'] = pinfo.pw_name
options['pw-uid'] = pinfo.pw_uid
options['pw-gid'] = pinfo.pw_gid
options['pw-uid'] = str(pinfo.pw_uid)
options['pw-gid'] = str(pinfo.pw_gid)
options['pw-dir'] = pinfo.pw_dir
options['pw-shell'] = pinfo.pw_shell
ginfo = grp.getgrgid(os.getgid())
options['gr-name'] = ginfo.gr_name
options['gr-gid'] = ginfo.gr_gid
options['gr-gid'] = str(ginfo.gr_gid)
install = update = lambda self: []
import unittest
import zc.buildout.testing
class UserInfoTest(unittest.TestCase):
def setUp(self):
self.buildout = buildout = zc.buildout.testing.Buildout()
buildout['userinfo'] = {}
from slapos.recipe import userinfo
self.recipe = userinfo.Recipe(
buildout,
'userinfo',
buildout['userinfo'])
def test_options(self):
buildout = self.buildout
self.assertTrue(buildout['userinfo']['pw-name'])
self.assertTrue(buildout['userinfo']['pw-uid'])
self.assertTrue(buildout['userinfo']['pw-gid'])
self.assertTrue(buildout['userinfo']['pw-dir'])
self.assertTrue(buildout['userinfo']['pw-shell'])
self.assertTrue(buildout['userinfo']['gr-name'])
self.assertTrue(buildout['userinfo']['gr-gid'])
def test_install(self):
self.assertEqual(self.recipe.install(), [])
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment