Commit 94706ac7 authored by Julien Muchembled's avatar Julien Muchembled

Add support for Python 3

parent 40270cb5
...@@ -46,7 +46,7 @@ And according Jinja2 template (kept simple, control structures are possible):: ...@@ -46,7 +46,7 @@ And according Jinja2 template (kept simple, control structures are possible)::
We run buildout:: We run buildout::
>>> print system(join('bin', 'buildout')), >>> run_buildout()
Installing template. Installing template.
And the template has been rendered:: And the template has been rendered::
...@@ -177,7 +177,7 @@ Use jinja2 extensions ...@@ -177,7 +177,7 @@ Use jinja2 extensions
... extensions = jinja2.ext.do jinja2.ext.loopcontrols ... extensions = jinja2.ext.do jinja2.ext.loopcontrols
... jinja2.ext.with_ ... jinja2.ext.with_
... ''') ... ''')
>>> print system(join('bin', 'buildout')), >>> run_buildout()
Uninstalling template. Uninstalling template.
Installing template. Installing template.
...@@ -190,8 +190,9 @@ Check file integrity ...@@ -190,8 +190,9 @@ Check file integrity
Compute template's MD5 sum:: Compute template's MD5 sum::
>>> write('foo.in', '{{bar}}') >>> write('foo.in', '{{bar}}')
>>> import md5 >>> from hashlib import md5
>>> md5sum = md5.new(open('foo.in', 'r').read()).hexdigest() >>> with open('foo.in', 'rb') as f:
... md5sum = md5(f.read()).hexdigest()
>>> write('buildout.cfg', >>> write('buildout.cfg',
... ''' ... '''
... [buildout] ... [buildout]
...@@ -204,7 +205,7 @@ Compute template's MD5 sum:: ...@@ -204,7 +205,7 @@ Compute template's MD5 sum::
... context = key bar buildout:parts ... context = key bar buildout:parts
... md5sum = ''' + md5sum + ''' ... md5sum = ''' + md5sum + '''
... ''') ... ''')
>>> print system(join('bin', 'buildout')), >>> run_buildout()
Uninstalling template. Uninstalling template.
Installing template. Installing template.
...@@ -225,7 +226,7 @@ If the md5sum doesn't match, the buildout fail:: ...@@ -225,7 +226,7 @@ If the md5sum doesn't match, the buildout fail::
... context = key bar buildout:parts ... context = key bar buildout:parts
... md5sum = 0123456789abcdef0123456789abcdef ... md5sum = 0123456789abcdef0123456789abcdef
... ''') ... ''')
>>> print system(join('bin', 'buildout')), >>> run_buildout()
Uninstalling template. Uninstalling template.
Installing template. Installing template.
While: While:
...@@ -251,14 +252,13 @@ You can specify the mode for rendered file:: ...@@ -251,14 +252,13 @@ You can specify the mode for rendered file::
... context = key bar buildout:parts ... context = key bar buildout:parts
... mode = 205 ... mode = 205
... ''') ... ''')
>>> print system(join('bin', 'buildout')), >>> run_buildout()
Installing template. Installing template.
And the generated file with have the right permissions:: And the generated file with have the right permissions::
>>> import stat >>> import os, stat
>>> import os >>> print("0%o" % stat.S_IMODE(os.stat('foo').st_mode))
>>> print oct(stat.S_IMODE(os.stat('foo').st_mode))
0205 0205
Note that Buildout will not allow you to have write permission for others Note that Buildout will not allow you to have write permission for others
...@@ -288,7 +288,7 @@ imported:: ...@@ -288,7 +288,7 @@ imported::
... rendered = bar ... rendered = bar
... import-list = rawfile library library.in ... import-list = rawfile library library.in
... ''') ... ''')
>>> print system(join('bin', 'buildout')), >>> run_buildout()
Uninstalling template. Uninstalling template.
Installing template. Installing template.
>>> cat('bar') >>> cat('bar')
...@@ -309,7 +309,7 @@ Just like context definition, it also works with indirect values:: ...@@ -309,7 +309,7 @@ Just like context definition, it also works with indirect values::
... rendered = bar ... rendered = bar
... import-list = file library template-library:path ... import-list = file library template-library:path
... ''') ... ''')
>>> print system(join('bin', 'buildout')), >>> run_buildout()
Uninstalling template. Uninstalling template.
Installing template. Installing template.
>>> cat('bar') >>> cat('bar')
...@@ -353,7 +353,7 @@ All templates can be accessed inside both folders:: ...@@ -353,7 +353,7 @@ All templates can be accessed inside both folders::
... rawfolder dir_a a ... rawfolder dir_a a
... rawfolder dir_b b ... rawfolder dir_b b
... ''') ... ''')
>>> print system(join('bin', 'buildout')), >>> run_buildout()
Uninstalling template. Uninstalling template.
Installing template. Installing template.
>>> cat('bar') >>> cat('bar')
...@@ -391,7 +391,7 @@ path):: ...@@ -391,7 +391,7 @@ path)::
... rawfolder dir_a a ... rawfolder dir_a a
... rawfolder dir_b b ... rawfolder dir_b b
... ''') ... ''')
>>> print system(join('bin', 'buildout')), >>> run_buildout()
Uninstalling template. Uninstalling template.
Installing template. Installing template.
>>> cat('bar') >>> cat('bar')
...@@ -421,7 +421,7 @@ will be installed as dependency:: ...@@ -421,7 +421,7 @@ will be installed as dependency::
... recipe = zc.buildout:debug ... recipe = zc.buildout:debug
... ''') ... ''')
>>> print system(join('bin', 'buildout')), >>> run_buildout()
Uninstalling template. Uninstalling template.
Installing dependency. Installing dependency.
foobar='dependency content' foobar='dependency content'
...@@ -474,7 +474,7 @@ Let's just use ``buildout.cfg`` using this egg:: ...@@ -474,7 +474,7 @@ Let's just use ``buildout.cfg`` using this egg::
... [sample] ... [sample]
... recipe = samplerecipe ... recipe = samplerecipe
... ''') ... ''')
>>> print system(join('bin', 'buildout')), >>> run_buildout()
Develop: '/sample-buildout/.' Develop: '/sample-buildout/.'
Uninstalling template. Uninstalling template.
Uninstalling dependency. Uninstalling dependency.
...@@ -504,7 +504,7 @@ rendering. ...@@ -504,7 +504,7 @@ rendering.
... rendered = foo_once ... rendered = foo_once
... once = foo_flag ... once = foo_flag
... ''') ... ''')
>>> print system(join('bin', 'buildout')), # doctest: +ELLIPSIS >>> run_buildout() # doctest: +ELLIPSIS
Uninstalling template. Uninstalling template.
Uninstalling sample. Uninstalling sample.
Getting distribution for 'samplerecipe'. Getting distribution for 'samplerecipe'.
...@@ -529,8 +529,9 @@ Remove rendered file and re-render:: ...@@ -529,8 +529,9 @@ Remove rendered file and re-render::
>>> import os >>> import os
>>> os.unlink('foo_once') >>> os.unlink('foo_once')
>>> open('buildout.cfg', 'a').writelines(['extra = useless']) >>> with open('buildout.cfg', 'a') as f:
>>> print system(join('bin', 'buildout')), ... f.writelines(['extra = useless'])
>>> run_buildout()
Uninstalling template. Uninstalling template.
Installing template. Installing template.
The template install returned None. A path or iterable os paths should be returned. The template install returned None. A path or iterable os paths should be returned.
...@@ -544,8 +545,9 @@ Template was not rendered:: ...@@ -544,8 +545,9 @@ Template was not rendered::
Removing the canary allows template to be re-rendered:: Removing the canary allows template to be re-rendered::
>>> os.unlink('foo_flag') >>> os.unlink('foo_flag')
>>> open('buildout.cfg', 'a').writelines(['moreextra = still useless']) >>> with open('buildout.cfg', 'a') as f:
>>> print system(join('bin', 'buildout')), ... f.writelines(['moreextra = still useless'])
>>> run_buildout()
Uninstalling template. Uninstalling template.
Installing template. Installing template.
The template install returned None. A path or iterable os paths should be returned. The template install returned None. A path or iterable os paths should be returned.
......
...@@ -26,7 +26,7 @@ And a simple template:: ...@@ -26,7 +26,7 @@ And a simple template::
We run buildout:: We run buildout::
>>> print system(join('bin', 'buildout')), >>> run_buildout()
Installing template. Installing template.
And the output file has been parsed by buildout itself:: And the output file has been parsed by buildout itself::
...@@ -54,8 +54,8 @@ Let's write a file template:: ...@@ -54,8 +54,8 @@ Let's write a file template::
Compute its MD5 sum:: Compute its MD5 sum::
>>> import md5 >>> from hashlib import md5
>>> md5sum = md5.new(open('template.in', 'r').read()).hexdigest() >>> md5sum = md5(open('template.in', 'rb').read()).hexdigest()
Write the ``buildout.cfg`` using slapos.recipe.template:: Write the ``buildout.cfg`` using slapos.recipe.template::
...@@ -73,7 +73,7 @@ Write the ``buildout.cfg`` using slapos.recipe.template:: ...@@ -73,7 +73,7 @@ Write the ``buildout.cfg`` using slapos.recipe.template::
And run buildout, and see the result:: And run buildout, and see the result::
>>> print system(join('bin', 'buildout')), >>> run_buildout()
Uninstalling template. Uninstalling template.
Installing template. Installing template.
...@@ -93,7 +93,7 @@ If the md5sum doesn't match, the buildout fail:: ...@@ -93,7 +93,7 @@ If the md5sum doesn't match, the buildout fail::
... output = template.out ... output = template.out
... md5sum = 0123456789abcdef0123456789abcdef ... md5sum = 0123456789abcdef0123456789abcdef
... ''') ... ''')
>>> print system(join('bin', 'buildout')), >>> run_buildout()
While: While:
Installing. Installing.
Getting section template. Getting section template.
...@@ -120,15 +120,14 @@ You can specify the mode of the written file:: ...@@ -120,15 +120,14 @@ You can specify the mode of the written file::
... mode = 0627 ... mode = 0627
... ''') ... ''')
>>> print system(join('bin', 'buildout')), >>> run_buildout()
Uninstalling template. Uninstalling template.
Installing template. Installing template.
And the generated file with have the right permissions:: And the generated file with have the right permissions::
>>> import stat >>> import os, stat
>>> import os >>> print("0%o" % stat.S_IMODE(os.stat('template.out').st_mode))
>>> print oct(stat.S_IMODE(os.stat('template.out').st_mode))
0627 0627
Section dependency Section dependency
...@@ -152,7 +151,7 @@ will be installed as dependency:: ...@@ -152,7 +151,7 @@ will be installed as dependency::
... recipe = zc.buildout:debug ... recipe = zc.buildout:debug
... ''') ... ''')
>>> print system(join('bin', 'buildout')), >>> run_buildout()
Uninstalling template. Uninstalling template.
Installing dependency. Installing dependency.
foobar='dependency content' foobar='dependency content'
...@@ -204,7 +203,7 @@ Let's just use ``buildout.cfg`` using this egg:: ...@@ -204,7 +203,7 @@ Let's just use ``buildout.cfg`` using this egg::
... [sample] ... [sample]
... recipe = samplerecipe ... recipe = samplerecipe
... ''') ... ''')
>>> print system(join('bin', 'buildout')), >>> run_buildout()
Develop: '/sample-buildout/.' Develop: '/sample-buildout/.'
Uninstalling template. Uninstalling template.
Uninstalling dependency. Uninstalling dependency.
......
...@@ -92,7 +92,7 @@ class RecipeBaseLoader(BaseLoader): ...@@ -92,7 +92,7 @@ class RecipeBaseLoader(BaseLoader):
if path is None or not os.path.exists(path): if path is None or not os.path.exists(path):
raise TemplateNotFound(template) raise TemplateNotFound(template)
mtime = os.path.getmtime(path) mtime = os.path.getmtime(path)
with file(path) as f: with open(path, 'rb') as f:
source = f.read().decode(self.encoding) source = f.read().decode(self.encoding)
return source, path, lambda: mtime == os.path.getmtime(path) return source, path, lambda: mtime == os.path.getmtime(path)
...@@ -211,7 +211,7 @@ class Recipe(object): ...@@ -211,7 +211,7 @@ class Recipe(object):
os.makedirs(outdir) os.makedirs(outdir)
mode = self.mode mode = self.mode
fd = os.open(self.rendered, os.O_CREAT | os.O_EXCL | os.O_WRONLY, fd = os.open(self.rendered, os.O_CREAT | os.O_EXCL | os.O_WRONLY,
(0777 if rendered.startswith('#!') else 0666) (0o777 if rendered.startswith(b'#!') else 0o666)
if mode is None else 0) if mode is None else 0)
try: try:
os.write(fd, rendered) os.write(fd, rendered)
...@@ -220,7 +220,7 @@ class Recipe(object): ...@@ -220,7 +220,7 @@ class Recipe(object):
finally: finally:
os.close(fd) os.close(fd)
if self.once: if self.once:
open(self.once, 'w').close() open(self.once, 'wb').close()
return return
return self.rendered return self.rendered
......
...@@ -24,6 +24,7 @@ ...@@ -24,6 +24,7 @@
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. # Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
# #
############################################################################## ##############################################################################
from __future__ import print_function
import doctest import doctest
import unittest import unittest
from zc.buildout import testing from zc.buildout import testing
...@@ -32,6 +33,9 @@ from zope.testing import renormalizing ...@@ -32,6 +33,9 @@ from zope.testing import renormalizing
def setUp(test): def setUp(test):
testing.buildoutSetUp(test) testing.buildoutSetUp(test)
testing.install_develop('slapos.recipe.template', test) testing.install_develop('slapos.recipe.template', test)
(lambda system, buildout, **kw: test.globs.update(
run_buildout = lambda: print(system(buildout), end='')
))(**test.globs)
def test_suite(): def test_suite():
return unittest.TestSuite([ return unittest.TestSuite([
......
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