buildout.txt 88.6 KB
Newer Older
Jim Fulton's avatar
Jim Fulton committed
1 2 3 4 5 6 7 8 9 10
Buildouts
=========

The word "buildout" refers to a description of a set of parts and the
software to create and assemble them.  It is often used informally to
refer to an installed system based on a buildout definition.  For
example, if we are creating an application named "Foo", then "the Foo
buildout" is the collection of configuration and application-specific
software that allows an instance of the application to be created.  We
may refer to such an instance of the application informally as "a Foo
11
buildout".
12 13

This document describes how to define buildouts using buildout
Jim Fulton's avatar
Jim Fulton committed
14
configuration files and recipes.  There are three ways to set up the
Jim Fulton's avatar
Jim Fulton committed
15
buildout software and create a buildout instance:
16

Jim Fulton's avatar
Jim Fulton committed
17 18
1. Install the zc.buildout egg with easy_install and use the buildout
   script installed in a Python scripts area.
19

Jim Fulton's avatar
Jim Fulton committed
20
2. Use the buildout bootstrap script to create a buildout that
21
   includes both the setuptools and zc.buildout eggs.  This allows you
Jim Fulton's avatar
Jim Fulton committed
22
   to use the buildout software without modifying a Python install.
23 24
   The buildout script is installed into your buildout local scripts
   area.
25

Christian Theune's avatar
Typo.  
Christian Theune committed
26
3. Use a buildout command from an already installed buildout to
Jim Fulton's avatar
Jim Fulton committed
27 28 29
   bootstrap a new buildout.  (See the section on bootstraping later
   in this document.)

Jim Fulton's avatar
Jim Fulton committed
30 31 32
Often, a software project will be managed in a software repository,
such as a subversion repository, that includes some software source
directories, buildout configuration files, and a copy of the buildout
Jim Fulton's avatar
Jim Fulton committed
33
bootstrap script.  To work on the project, one would check out the
Jim Fulton's avatar
Jim Fulton committed
34
project from the repository and run the bootstrap script which
35
installs setuptools and zc.buildout into the checkout as well as any
Jim Fulton's avatar
Jim Fulton committed
36 37
parts defined.

Jim Fulton's avatar
Jim Fulton committed
38 39 40 41
We have a sample buildout that we created using the bootstrap command
of an existing buildout (method 3 above).  It has the absolute minimum
information.  We have bin, develop-eggs, eggs and parts directories,
and a configuration file:
42

43 44 45
    >>> ls(sample_buildout)
    d  bin
    -  buildout.cfg
46
    d  develop-eggs
47 48 49
    d  eggs
    d  parts

Jim Fulton's avatar
Jim Fulton committed
50
The bin directory contains scripts.
51 52 53 54 55

    >>> ls(sample_buildout, 'bin')
    -  buildout

    >>> ls(sample_buildout, 'eggs')
56 57
    -  setuptools-0.7-py3.3.egg
    -  zc.buildout.egg-link
58

59
The develop-eggs and parts directories are initially empty:
60

61
    >>> ls(sample_buildout, 'develop-eggs')
62 63
    >>> ls(sample_buildout, 'parts')

64 65
The develop-eggs directory holds egg links for software being
developed in the buildout.  We separate develop-eggs and other eggs to
66
allow eggs directories to be shared across multiple buildouts.  For
67 68 69 70 71 72
example, a common developer technique is to define a common eggs
directory in their home that all non-develop eggs are stored in.  This
allows larger buildouts to be set up much more quickly and saves disk
space.

The parts directory provides an area where recipes can install
73 74
part data.  For example, if we built a custom Python, we would
install it in the part directory.  Part data is stored in a
75
sub-directory of the parts directory with the same name as the part.
76

Jim Fulton's avatar
Jim Fulton committed
77 78 79
Buildouts are defined using configuration files.  These are in the
format defined by the Python ConfigParser module, with extensions
that we'll describe later.  By default, when a buildout is run, it
80
looks for the file buildout.cfg in the directory where the buildout is
Jim Fulton's avatar
Jim Fulton committed
81
run.
82 83 84 85 86 87 88 89 90 91

The minimal configuration file has a buildout section that defines no
parts:

    >>> cat(sample_buildout, 'buildout.cfg')
    [buildout]
    parts =

A part is simply something to be created by a buildout.  It can be
almost anything, such as a Python package, a program, a directory, or
92
even a configuration file.
93

Jim Fulton's avatar
Jim Fulton committed
94 95 96
Recipes
-------

97
A part is created by a recipe.  Recipes are always installed as Python
Jim Fulton's avatar
Jim Fulton committed
98
eggs. They can be downloaded from a package server, such as the
Jim Fulton's avatar
Jim Fulton committed
99
Python Package Index, or they can be developed as part of a project
100
using a "develop" egg.
Jim Fulton's avatar
Jim Fulton committed
101 102 103 104 105 106

A develop egg is a special kind of egg that gets installed as an "egg
link" that contains the name of a source directory.  Develop eggs
don't have to be packaged for distribution to be used and can be
modified in place, which is especially useful while they are being
developed.
107

Jim Fulton's avatar
Jim Fulton committed
108 109 110
Let's create a recipe as part of the sample project.  We'll create a
recipe for creating directories.  First, we'll create a recipes source
directory for our local recipes:
111 112 113 114 115

    >>> mkdir(sample_buildout, 'recipes')

and then we'll create a source file for our mkdir recipe:

116
    >>> write(sample_buildout, 'recipes', 'mkdir.py',
117
    ... """
118
    ... import logging, os, zc.buildout
119 120 121 122
    ...
    ... class Mkdir:
    ...
    ...     def __init__(self, buildout, name, options):
123
    ...         self.name, self.options = name, options
124 125 126 127
    ...         options['path'] = os.path.join(
    ...                               buildout['buildout']['directory'],
    ...                               options['path'],
    ...                               )
128 129 130 131 132
    ...         if not os.path.isdir(os.path.dirname(options['path'])):
    ...             logging.getLogger(self.name).error(
    ...                 'Cannot create %s. %s is not a directory.',
    ...                 options['path'], os.path.dirname(options['path']))
    ...             raise zc.buildout.UserError('Invalid Path')
133
    ...
134 135
    ...
    ...     def install(self):
136
    ...         path = self.options['path']
Jim Fulton's avatar
Jim Fulton committed
137 138 139
    ...         logging.getLogger(self.name).info(
    ...             'Creating directory %s', os.path.basename(path))
    ...         os.mkdir(path)
140
    ...         return path
Jim Fulton's avatar
Jim Fulton committed
141 142 143
    ...
    ...     def update(self):
    ...         pass
144 145
    ... """)

146
Currently, recipes must define 3 methods:
147

Jim Fulton's avatar
Jim Fulton committed
148 149 150
- a constructor,

- an install method, and
151

Jim Fulton's avatar
Jim Fulton committed
152 153 154 155 156
- an update method.

The constructor is responsible for updating a parts options to reflect
data read from other sections.  The buildout system keeps track of
whether a part specification has changed.  A part specification has
157
changed if it's options, after adjusting for data read from other
Jim Fulton's avatar
Jim Fulton committed
158 159 160 161 162 163 164 165
sections, has changed, or if the recipe has changed.  Only the options
for the part are considered.  If data are read from other sections,
then that information has to be reflected in the parts options.  In
the Mkdir example, the given path is interpreted relative to the
buildout directory, and data from the buildout directory is read.  The
path option is updated to reflect this.  If the directory option was
changed in the buildout sections, we would know to update parts
created using the mkdir recipe using relative path names.
Jim Fulton's avatar
Jim Fulton committed
166 167

When buildout is run, it saves configuration data for installed parts
Jim Fulton's avatar
Jim Fulton committed
168 169
in a file named ".installed.cfg".  In subsequent runs, it compares
part-configuration data stored in the .installed.cfg file and the
Jim Fulton's avatar
Jim Fulton committed
170 171 172
part-configuration data loaded from the configuration files as
modified by recipe constructors to decide if the configuration of a
part has changed. If the configuration has changed, or if the recipe
Jim Fulton's avatar
Jim Fulton committed
173
has changed, then the part is uninstalled and reinstalled.  The
Jim Fulton's avatar
Jim Fulton committed
174 175 176
buildout only looks at the part's options, so any data used to
configure the part needs to be reflected in the part's options.  It is
the job of a recipe constructor to make sure that the options include
177
all relevant data.
Jim Fulton's avatar
Jim Fulton committed
178 179

Of course, parts are also uninstalled if they are no-longer used.
180

Jim Fulton's avatar
Jim Fulton committed
181 182 183 184 185 186 187
The recipe defines a constructor that takes a buildout object, a part
name, and an options dictionary. It saves them in instance attributes.
If the path is relative, we'll interpret it as relative to the
buildout directory.  The buildout object passed in is a mapping from
section name to a mapping of options for that section. The buildout
directory is available as the directory option of the buildout
section.  We normalize the path and save it back into the options
188
directory.
189

Jim Fulton's avatar
Jim Fulton committed
190 191 192 193 194 195 196 197
The install method is responsible for creating the part.  In this
case, we need the path of the directory to create.  We'll use a path
option from our options dictionary.  The install method logs what it's
doing using the Python logging call.  We return the path that we
installed.  If the part is uninstalled or reinstalled, then the path
returned will be removed by the buildout machinery.  A recipe install
method is expected to return a string, or an iterable of strings
containing paths to be removed if a part is uninstalled.  For most
198 199
recipes, this is all of the uninstall support needed. For more complex
uninstallation scenarios use `Uninstall recipes`_.
Jim Fulton's avatar
Jim Fulton committed
200 201 202 203 204 205

The update method is responsible for updating an already installed
part.  An empty method is often provided, as in this example, if parts
can't be updated.  An update method can return None, a string, or an
iterable of strings.  If a string or iterable of strings is returned,
then the saved list of paths to be uninstalled is updated with the new
Jim Fulton's avatar
Jim Fulton committed
206
information by adding any new files returned by the update method.
207 208

We need to provide packaging information so that our recipe can be
Jim Fulton's avatar
Jim Fulton committed
209
installed as a develop egg. The minimum information we need to specify
210
is a name.  For recipes, we also need to define the
Jim Fulton's avatar
Jim Fulton committed
211 212
names of the recipe classes as entry points.  Packaging information is
provided via a setup.py script:
213 214 215 216

    >>> write(sample_buildout, 'recipes', 'setup.py',
    ... """
    ... from setuptools import setup
217
    ...
218 219 220 221 222 223
    ... setup(
    ...     name = "recipes",
    ...     entry_points = {'zc.buildout': ['mkdir = mkdir:Mkdir']},
    ...     )
    ... """)

Jim Fulton's avatar
Jim Fulton committed
224
Our setup script defines an entry point. Entry points provide
225
a way for an egg to define the services it provides.  Here we've said
Jim Fulton's avatar
Jim Fulton committed
226
that we define a zc.buildout entry point named mkdir.  Recipe
227
classes must be exposed as entry points in the zc.buildout group.  we
Jim Fulton's avatar
Jim Fulton committed
228
give entry points names within the group.
229

Jim Fulton's avatar
Jim Fulton committed
230
We also need a README.txt for our recipes to avoid an annoying warning
231
from distutils, on which setuptools and zc.buildout are based:
232 233 234 235 236 237 238 239 240

    >>> write(sample_buildout, 'recipes', 'README.txt', " ")

Now let's update our buildout.cfg:

    >>> write(sample_buildout, 'buildout.cfg',
    ... """
    ... [buildout]
    ... develop = recipes
241
    ... parts = data-dir
242
    ...
243
    ... [data-dir]
244 245 246 247 248 249 250 251 252 253
    ... recipe = recipes:mkdir
    ... path = mystuff
    ... """)

Let's go through the changes one by one::

    develop = recipes

This tells the buildout to install a development egg for our recipes.
Any number of paths can be listed.  The paths can be relative or
Fred Drake's avatar
Fred Drake committed
254
absolute.  If relative, they are treated as relative to the buildout
255 256
directory.  They can be directory or file paths.  If a file path is
given, it should point to a Python setup script.  If a directory path
Jim Fulton's avatar
Jim Fulton committed
257
is given, it should point to a directory containing a setup.py file.
258 259 260 261 262
Development eggs are installed before building any parts, as they may
provide locally-defined recipes needed by the parts.

::

263
    parts = data-dir
264 265 266 267 268 269 270

Here we've named a part to be "built".  We can use any name we want
except that different part names must be unique and recipes will often
use the part name to decide what to do.

::

271
    [data-dir]
272
    recipe = recipes:mkdir
273
    path = mystuff
274

275 276 277

When we name a part, we also create a section of the same
name that contains part data.  In this section, we'll define
278 279 280 281 282 283 284 285
the recipe to be used to install the part.  In this case, we also
specify the path to be created.

Let's run the buildout.  We do so by running the build script in the
buildout:

    >>> import os
    >>> os.chdir(sample_buildout)
Jim Fulton's avatar
Jim Fulton committed
286
    >>> buildout = os.path.join(sample_buildout, 'bin', 'buildout')
287
    >>> print_(system(buildout), end='')
288 289
    Develop: '/sample-buildout/recipes'
    Installing data-dir.
290
    data-dir: Creating directory mystuff
291 292 293 294 295 296 297

We see that the recipe created the directory, as expected:

    >>> ls(sample_buildout)
    -  .installed.cfg
    d  bin
    -  buildout.cfg
298
    d  develop-eggs
299 300 301 302 303
    d  eggs
    d  mystuff
    d  parts
    d  recipes

Jim Fulton's avatar
Jim Fulton committed
304 305
In addition, .installed.cfg has been created containing information
about the part we installed:
306 307 308

    >>> cat(sample_buildout, '.installed.cfg')
    [buildout]
309
    installed_develop_eggs = /sample-buildout/develop-eggs/recipes.egg-link
310
    parts = data-dir
311
    <BLANKLINE>
312
    [data-dir]
313
    __buildout_installed__ = /sample-buildout/mystuff
314
    __buildout_signature__ = recipes-c7vHV6ekIDUPy/7fjAaYjg==
315
    path = /sample-buildout/mystuff
316 317 318
    recipe = recipes:mkdir

Note that the directory we installed is included in .installed.cfg.
319
In addition, the path option includes the actual destination
320
directory.
321 322 323 324 325 326 327 328

If we change the name of the directory in the configuration file,
we'll see that the directory gets removed and recreated:

    >>> write(sample_buildout, 'buildout.cfg',
    ... """
    ... [buildout]
    ... develop = recipes
329
    ... parts = data-dir
330
    ...
331
    ... [data-dir]
332 333 334 335
    ... recipe = recipes:mkdir
    ... path = mydata
    ... """)

336
    >>> print_(system(buildout), end='')
337 338 339
    Develop: '/sample-buildout/recipes'
    Uninstalling data-dir.
    Installing data-dir.
340
    data-dir: Creating directory mydata
341 342 343 344 345

    >>> ls(sample_buildout)
    -  .installed.cfg
    d  bin
    -  buildout.cfg
346
    d  develop-eggs
347 348 349 350 351
    d  eggs
    d  mydata
    d  parts
    d  recipes

Jim Fulton's avatar
Jim Fulton committed
352 353 354 355
If any of the files or directories created by a recipe are removed,
the part will be reinstalled:

    >>> rmdir(sample_buildout, 'mydata')
356
    >>> print_(system(buildout), end='')
357 358 359
    Develop: '/sample-buildout/recipes'
    Uninstalling data-dir.
    Installing data-dir.
Jim Fulton's avatar
Jim Fulton committed
360
    data-dir: Creating directory mydata
361 362 363 364 365 366 367

Error reporting
---------------

If a user makes an error, an error needs to be printed and work needs
to stop.  This is accomplished by logging a detailed error message and
then raising a (or an instance of a subclass of a)
368 369 370
zc.buildout.UserError exception.  Raising an error other than a
UserError still displays the error, but labels it as a bug in the
buildout software or recipe. In the sample above, of someone gives a
371
non-existent directory to create the directory in:
372 373 374 375 376 377 378 379 380 381 382 383 384 385 386


    >>> write(sample_buildout, 'buildout.cfg',
    ... """
    ... [buildout]
    ... develop = recipes
    ... parts = data-dir
    ...
    ... [data-dir]
    ... recipe = recipes:mkdir
    ... path = /xxx/mydata
    ... """)

We'll get a user error, not a traceback.

387
    >>> print_(system(buildout), end='')
388
    Develop: '/sample-buildout/recipes'
389
    data-dir: Cannot create /xxx/mydata. /xxx is not a directory.
390
    While:
391 392
      Installing.
      Getting section data-dir.
Jim Fulton's avatar
Jim Fulton committed
393
      Initializing section data-dir.
394 395 396
    Error: Invalid Path


397 398 399 400 401 402 403
Recipe Error Handling
---------------------

If an error occurs during installation, it is up to the recipe to
clean up any system side effects, such as files created.  Let's update
the mkdir recipe to support multiple paths:

404
    >>> write(sample_buildout, 'recipes', 'mkdir.py',
405 406 407 408 409 410 411 412 413 414 415
    ... """
    ... import logging, os, zc.buildout
    ...
    ... class Mkdir:
    ...
    ...     def __init__(self, buildout, name, options):
    ...         self.name, self.options = name, options
    ...
    ...         # Normalize paths and check that their parent
    ...         # directories exist:
    ...         paths = []
416
    ...         for path in options['path'].split():
417 418 419 420 421 422 423 424 425 426 427
    ...             path = os.path.join(buildout['buildout']['directory'], path)
    ...             if not os.path.isdir(os.path.dirname(path)):
    ...                 logging.getLogger(self.name).error(
    ...                     'Cannot create %s. %s is not a directory.',
    ...                     options['path'], os.path.dirname(options['path']))
    ...                 raise zc.buildout.UserError('Invalid Path')
    ...             paths.append(path)
    ...         options['path'] = ' '.join(paths)
    ...
    ...     def install(self):
    ...         paths = self.options['path'].split()
428
    ...         for path in paths:
429 430 431 432 433 434 435 436 437
    ...             logging.getLogger(self.name).info(
    ...                 'Creating directory %s', os.path.basename(path))
    ...             os.mkdir(path)
    ...         return paths
    ...
    ...     def update(self):
    ...         pass
    ... """)

438 439 440 441
..

    >>> clean_up_pyc(sample_buildout, 'recipes', 'mkdir.py')

442 443 444 445 446 447 448 449 450 451 452 453 454 455
If there is an error creating a path, the install method will exit and
leave previously created paths in place:

    >>> write(sample_buildout, 'buildout.cfg',
    ... """
    ... [buildout]
    ... develop = recipes
    ... parts = data-dir
    ...
    ... [data-dir]
    ... recipe = recipes:mkdir
    ... path = foo bin
    ... """)

456
    >>> print_(system(buildout)) # doctest: +ELLIPSIS
457 458 459
    Develop: '/sample-buildout/recipes'
    Uninstalling data-dir.
    Installing data-dir.
460 461 462
    data-dir: Creating directory foo
    data-dir: Creating directory bin
    While:
463
      Installing data-dir.
464
    <BLANKLINE>
pombredanne's avatar
pombredanne committed
465
    An internal error occurred due to a bug in either zc.buildout or in a
466
    recipe being used:
467
    Traceback (most recent call last):
468
    ... exists...
469

470
We meant to create a directory bins, but typed bin.  Now foo was
471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488
left behind.

    >>> os.path.exists('foo')
    True

If we fix the typo:

    >>> write(sample_buildout, 'buildout.cfg',
    ... """
    ... [buildout]
    ... develop = recipes
    ... parts = data-dir
    ...
    ... [data-dir]
    ... recipe = recipes:mkdir
    ... path = foo bins
    ... """)

489
    >>> print_(system(buildout)) # doctest: +ELLIPSIS
490 491
    Develop: '/sample-buildout/recipes'
    Installing data-dir.
492 493
    data-dir: Creating directory foo
    While:
494
      Installing data-dir.
495
    <BLANKLINE>
pombredanne's avatar
pombredanne committed
496
    An internal error occurred due to a bug in either zc.buildout or in a
497
    recipe being used:
498
    Traceback (most recent call last):
499
    ... exists...
500 501 502 503 504 505 506

Now they fail because foo exists, because it was left behind.

    >>> remove('foo')

Let's fix the recipe:

507
    >>> write(sample_buildout, 'recipes', 'mkdir.py',
508
    ... """
509
    ... import logging, os, zc.buildout, sys
510 511 512 513 514 515 516 517 518
    ...
    ... class Mkdir:
    ...
    ...     def __init__(self, buildout, name, options):
    ...         self.name, self.options = name, options
    ...
    ...         # Normalize paths and check that their parent
    ...         # directories exist:
    ...         paths = []
519
    ...         for path in options['path'].split():
520 521 522 523 524 525 526 527 528 529 530 531 532
    ...             path = os.path.join(buildout['buildout']['directory'], path)
    ...             if not os.path.isdir(os.path.dirname(path)):
    ...                 logging.getLogger(self.name).error(
    ...                     'Cannot create %s. %s is not a directory.',
    ...                     options['path'], os.path.dirname(options['path']))
    ...                 raise zc.buildout.UserError('Invalid Path')
    ...             paths.append(path)
    ...         options['path'] = ' '.join(paths)
    ...
    ...     def install(self):
    ...         paths = self.options['path'].split()
    ...         created = []
    ...         try:
533
    ...             for path in paths:
534 535 536 537
    ...                 logging.getLogger(self.name).info(
    ...                     'Creating directory %s', os.path.basename(path))
    ...                 os.mkdir(path)
    ...                 created.append(path)
538
    ...         except Exception:
539 540
    ...             for d in created:
    ...                 os.rmdir(d)
541
    ...                 assert not os.path.exists(d)
542 543 544
    ...                 logging.getLogger(self.name).info(
    ...                     'Removed %s due to error',
    ...                      os.path.basename(d))
545 546
    ...             sys.stderr.flush()
    ...             sys.stdout.flush()
547 548 549 550 551 552 553 554
    ...             raise
    ...
    ...         return paths
    ...
    ...     def update(self):
    ...         pass
    ... """)

555 556 557 558
..

    >>> clean_up_pyc(sample_buildout, 'recipes', 'mkdir.py')

559 560 561 562 563 564 565 566 567 568 569 570 571 572 573
And put back the typo:

    >>> write(sample_buildout, 'buildout.cfg',
    ... """
    ... [buildout]
    ... develop = recipes
    ... parts = data-dir
    ...
    ... [data-dir]
    ... recipe = recipes:mkdir
    ... path = foo bin
    ... """)

When we rerun the buildout:

574
    >>> print_(system(buildout)) # doctest: +ELLIPSIS
575 576
    Develop: '/sample-buildout/recipes'
    Installing data-dir.
577 578
    data-dir: Creating directory foo
    data-dir: Creating directory bin
579
    data-dir: Removed foo due to error
580
    While:
581
      Installing data-dir.
582
    <BLANKLINE>
pombredanne's avatar
pombredanne committed
583
    An internal error occurred due to a bug in either zc.buildout or in a
584
    recipe being used:
585
    Traceback (most recent call last):
586
    ... exists...
587 588 589 590 591 592 593 594 595 596

we get the same error, but we don't get the directory left behind:

    >>> os.path.exists('foo')
    False

It's critical that recipes clean up partial effects when errors
occur.  Because recipes most commonly create files and directories,
buildout provides a helper API for removing created files when an
error occurs.  Option objects have a created method that can be called
597
to record files as they are created.  If the install or update method
598 599 600 601 602
returns with an error, then any registered paths are removed
automatically.  The method returns the files registered and can be
used to return the files created.  Let's use this API to simplify the
recipe:

603
    >>> write(sample_buildout, 'recipes', 'mkdir.py',
604 605 606 607 608 609 610 611 612 613 614
    ... """
    ... import logging, os, zc.buildout
    ...
    ... class Mkdir:
    ...
    ...     def __init__(self, buildout, name, options):
    ...         self.name, self.options = name, options
    ...
    ...         # Normalize paths and check that their parent
    ...         # directories exist:
    ...         paths = []
615
    ...         for path in options['path'].split():
616 617 618 619 620 621 622 623 624 625 626
    ...             path = os.path.join(buildout['buildout']['directory'], path)
    ...             if not os.path.isdir(os.path.dirname(path)):
    ...                 logging.getLogger(self.name).error(
    ...                     'Cannot create %s. %s is not a directory.',
    ...                     options['path'], os.path.dirname(options['path']))
    ...                 raise zc.buildout.UserError('Invalid Path')
    ...             paths.append(path)
    ...         options['path'] = ' '.join(paths)
    ...
    ...     def install(self):
    ...         paths = self.options['path'].split()
627
    ...         for path in paths:
628 629 630 631 632 633 634 635 636 637 638 639 640
    ...             logging.getLogger(self.name).info(
    ...                 'Creating directory %s', os.path.basename(path))
    ...             os.mkdir(path)
    ...             self.options.created(path)
    ...
    ...         return self.options.created()
    ...
    ...     def update(self):
    ...         pass
    ... """)

..

641
    >>> clean_up_pyc(sample_buildout, 'recipes', 'mkdir.py')
642 643 644 645 646 647

We returned by calling created, taking advantage of the fact that it
returns the registered paths.  We did this for illustrative purposes.
It would be simpler just to return the paths as before.

If we rerun the buildout, again, we'll get the error and no
648
directories will be created:
649

650
    >>> print_(system(buildout)) # doctest: +ELLIPSIS
651 652
    Develop: '/sample-buildout/recipes'
    Installing data-dir.
653 654 655
    data-dir: Creating directory foo
    data-dir: Creating directory bin
    While:
656
      Installing data-dir.
657
    <BLANKLINE>
pombredanne's avatar
pombredanne committed
658
    An internal error occurred due to a bug in either zc.buildout or in a
659
    recipe being used:
660
    Traceback (most recent call last):
661
    ... exists...
662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678

    >>> os.path.exists('foo')
    False

Now, we'll fix the typo again and we'll get the directories we expect:

    >>> write(sample_buildout, 'buildout.cfg',
    ... """
    ... [buildout]
    ... develop = recipes
    ... parts = data-dir
    ...
    ... [data-dir]
    ... recipe = recipes:mkdir
    ... path = foo bins
    ... """)

679
    >>> print_(system(buildout), end='')
680 681
    Develop: '/sample-buildout/recipes'
    Installing data-dir.
682 683 684 685 686 687 688 689
    data-dir: Creating directory foo
    data-dir: Creating directory bins

    >>> os.path.exists('foo')
    True
    >>> os.path.exists('bins')
    True

690 691 692
Configuration file syntax
-------------------------

693 694 695 696 697 698
A buildout configuration file consists of a sequence of sections.  A
section has a section header followed by 0 or more section options.
(Buildout configuration files may be viewed as a variation on INI
files.)

A section header consists of a section name enclosed in square braces.
699 700 701 702 703 704
A section name consists of one or more non-whitespace characters other
than square braces ('[', ']'), curly braces ('{', '}'), colons (':')
or equal signs ('='). Whitespace surrounding section names is ignored.

A section header can optionally have a condition expression separated
by a colon.  See `Conditional sections`_.
705 706 707 708 709 710 711 712 713 714 715 716 717 718 719

Options consist of option names, followed by optional space or tab
characters, an optional plus or minus sign and an equal signs and
values.  An option value may be spread over multiple lines as long as
the lines after the first start with a whitespace character.  An
option name consists of one or more non-whitespace characters other
than equal signs, square braces ("[", "]"), curly braces ("{", "}"),
plus signs or colons (":"). The option name '<' is reserved.  An
option's data consists of the characters following the equal sign on
the start line, plus the continuation lines.

Option values have extra whitespace stripped.  How this is done
depends on whether the value has non-whitespace characterts on the
first line.  If an option value has non-whitespace characters on the
first line, then each line is stripped and blank lines are removed.
pombredanne's avatar
pombredanne committed
720
For example, in::
721 722 723 724 725 726 727 728 729 730

  [foo]
  bar = 1
  baz = a
        b

        c

.. -> text

731 732 733
    >>> try: import StringIO
    ... except ImportError: import io as StringIO
    >>> import pprint, zc.buildout.configparser
734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770
    >>> pprint.pprint(zc.buildout.configparser.parse(StringIO.StringIO(
    ...     text), 'test'))
    {'foo': {'bar': '1', 'baz': 'a\nb\nc'}}

The value of of ``bar`` is ``'1'`` and the value of ``baz`` is
``'a\nb\nc'``.

If the first line of an option doesn't contain whitespace, then the
value is dedented (with ``textwrap.dedent``), trailing spaces in lines
are removed, and leading and trailing blank lines are removed.  For
example, in::


  [foo]
  bar =
  baz =

    a
      b

    c

.. -> text

    >>> pprint.pprint(zc.buildout.configparser.parse(StringIO.StringIO(
    ...     text), 'test'))
    {'foo': {'bar': '', 'baz': 'a\n  b\n\nc'}}

The value of bar is ``''``, and the value of baz is ``'a\n  b\n\nc'``.

Lines starting with '#' or ';' characters are comments.  Comments can
also be placed after the closing square bracket (']') in a section header.

Buildout configuration data are Python strings, which are bytes in
Python 2 and unicode in Python 3.

Sections and options within sections may be repeated.  Multiple
pombredanne's avatar
pombredanne committed
771
occurrences of of a section are treated as if they were concatenated.
772 773 774 775
The last option value for a given name in a section overrides previous
values.

In addition top the syntactic details above:
776 777 778

- option names are case sensitive

779
- option values can use a substitution syntax, described below, to
780 781
  refer to option values in specific sections.

782 783 784
- option values can be appended or removed using the - and +
  operators.

785 786 787
Annotated sections
------------------

788
When used with the `annotate` command, buildout displays annotated sections.
789
All sections are displayed, sorted alphabetically. For each section,
790 791
all key-value pairs are displayed, sorted alphabetically, along with
the origin of the value (file name or COMPUTED_VALUE, DEFAULT_VALUE,
792 793
COMMAND_LINE_VALUE).

794
    >>> print_(system(buildout+ ' annotate'), end='')
795
    ... # doctest: +ELLIPSIS +NORMALIZE_WHITESPACE
796 797 798 799 800
    <BLANKLINE>
    Annotated sections
    ==================
    <BLANKLINE>
    [buildout]
801 802 803 804
    allow-hosts= *
        DEFAULT_VALUE
    allow-picked-versions= true
        DEFAULT_VALUE
805
    bin-directory= bin
806
        DEFAULT_VALUE
807 808 809
    develop= recipes
        /sample-buildout/buildout.cfg
    develop-eggs-directory= develop-eggs
810
        DEFAULT_VALUE
811
    directory= /sample-buildout
812
        COMPUTED_VALUE
813
    eggs-directory= /sample-buildout/eggs
814
        DEFAULT_VALUE
815 816 817 818 819 820
    executable= ...
        DEFAULT_VALUE
    find-links=
        DEFAULT_VALUE
    install-from-cache= false
        DEFAULT_VALUE
821
    installed= .installed.cfg
822
        DEFAULT_VALUE
823
    log-format=
824
        DEFAULT_VALUE
825
    log-level= INFO
826
        DEFAULT_VALUE
827 828 829 830
    newest= true
        DEFAULT_VALUE
    offline= false
        DEFAULT_VALUE
831 832 833
    parts= data-dir
        /sample-buildout/buildout.cfg
    parts-directory= parts
834
        DEFAULT_VALUE
Jim Fulton's avatar
Jim Fulton committed
835
    prefer-final= true
836 837 838
        DEFAULT_VALUE
    python= buildout
        DEFAULT_VALUE
839 840
    show-picked-versions= false
        DEFAULT_VALUE
841 842
    socket-timeout=
        DEFAULT_VALUE
843 844
    update-versions-file=
        DEFAULT_VALUE
845 846
    use-dependency-links= true
        DEFAULT_VALUE
847 848
    versions= versions
        DEFAULT_VALUE
849 850
    <BLANKLINE>
    [data-dir]
851 852 853 854
    path= foo bins
        /sample-buildout/buildout.cfg
    recipe= recipes:mkdir
        /sample-buildout/buildout.cfg
855
    <BLANKLINE>
856 857 858 859 860 861
    [versions]
    zc.buildout= >=1.99
        DEFAULT_VALUE
    zc.recipe.egg= >=1.99
        DEFAULT_VALUE
    <BLANKLINE>
862

863 864 865
Variable substitutions
----------------------

866 867
Buildout configuration files support variable substitution.
To illustrate this, we'll create an debug recipe to
868 869
allow us to see interactions with the buildout:

870
    >>> write(sample_buildout, 'recipes', 'debug.py',
871
    ... """
872
    ... import sys
873 874 875 876 877 878 879 880
    ... class Debug:
    ...
    ...     def __init__(self, buildout, name, options):
    ...         self.buildout = buildout
    ...         self.name = name
    ...         self.options = options
    ...
    ...     def install(self):
881 882
    ...         for option, value in sorted(self.options.items()):
    ...             sys.stdout.write('%s %s\\n' % (option, value))
Jim Fulton's avatar
Jim Fulton committed
883 884 885
    ...         return ()
    ...
    ...     update = install
886 887
    ... """)

888 889 890
This recipe doesn't actually create anything. The install method
doesn't return anything, because it didn't create any files or
directories.
891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906

We also have to update our setup script:

    >>> write(sample_buildout, 'recipes', 'setup.py',
    ... """
    ... from setuptools import setup
    ... entry_points = (
    ... '''
    ... [zc.buildout]
    ... mkdir = mkdir:Mkdir
    ... debug = debug:Debug
    ... ''')
    ... setup(name="recipes", entry_points=entry_points)
    ... """)

We've rearranged the script a bit to make the entry points easier to
Jim Fulton's avatar
Jim Fulton committed
907 908
edit.  In particular, entry points are now defined as a configuration
string, rather than a dictionary.
909 910 911 912 913 914 915 916

Let's update our configuration to provide variable substitution
examples:

    >>> write(sample_buildout, 'buildout.cfg',
    ... """
    ... [buildout]
    ... develop = recipes
917
    ... parts = data-dir debug
Jim Fulton's avatar
Jim Fulton committed
918
    ... log-level = INFO
919 920 921
    ...
    ... [debug]
    ... recipe = recipes:debug
922 923
    ... File-1 = ${data-dir:path}/file
    ... File-2 = ${debug:File-1}/log
924
    ...
925
    ... [data-dir]
926 927 928 929
    ... recipe = recipes:mkdir
    ... path = mydata
    ... """)

930
We used a string-template substitution for File-1 and File-2.  This
931 932 933
type of substitution uses the string.Template syntax.  Names
substituted are qualified option names, consisting of a section name
and option name joined by a colon.
934 935

Now, if we run the buildout, we'll see the options with the values
936
substituted.
937

938
    >>> print_(system(buildout), end='')
939 940 941
    Develop: '/sample-buildout/recipes'
    Uninstalling data-dir.
    Installing data-dir.
942
    data-dir: Creating directory mydata
943
    Installing debug.
944 945
    File-1 /sample-buildout/mydata/file
    File-2 /sample-buildout/mydata/file/log
946 947
    recipe recipes:debug

948 949 950
Note that the substitution of the data-dir path option reflects the
update to the option performed by the mkdir recipe.

951 952 953 954 955 956
It might seem surprising that mydata was created again.  This is
because we changed our recipes package by adding the debug module.
The buildout system didn't know if this module could effect the mkdir
recipe, so it assumed it could and reinstalled mydata.  If we rerun
the buildout:

957
    >>> print_(system(buildout), end='')
958 959 960
    Develop: '/sample-buildout/recipes'
    Updating data-dir.
    Updating debug.
961 962
    File-1 /sample-buildout/mydata/file
    File-2 /sample-buildout/mydata/file/log
963 964 965
    recipe recipes:debug

We can see that mydata was not recreated.
966

967
Note that, in this case, we didn't specify a log level, so
Jim Fulton's avatar
Jim Fulton committed
968 969
we didn't get output about what the buildout was doing.

970 971 972 973
Section and option names in variable substitutions are only allowed to
contain alphanumeric characters, hyphens, periods and spaces. This
restriction might be relaxed in future releases.

pombredanne's avatar
pombredanne committed
974
We can omit the section name in a variable substitution to refer to
975 976 977 978 979 980 981 982 983 984 985 986
the current section.  We can also use the special option,
_buildout_section_name_ to get the current section name.

    >>> write(sample_buildout, 'buildout.cfg',
    ... """
    ... [buildout]
    ... develop = recipes
    ... parts = data-dir debug
    ... log-level = INFO
    ...
    ... [debug]
    ... recipe = recipes:debug
987 988
    ... File-1 = ${data-dir:path}/file
    ... File-2 = ${:File-1}/log
989 990 991 992 993 994 995
    ... my_name = ${:_buildout_section_name_}
    ...
    ... [data-dir]
    ... recipe = recipes:mkdir
    ... path = mydata
    ... """)

996
    >>> print_(system(buildout), end='')
997 998 999 1000
    Develop: '/sample-buildout/recipes'
    Uninstalling debug.
    Updating data-dir.
    Installing debug.
1001 1002
    File-1 /sample-buildout/mydata/file
    File-2 /sample-buildout/mydata/file/log
1003 1004
    my_name debug
    recipe recipes:debug
1005

1006 1007 1008
Automatic part selection and ordering
-------------------------------------

1009
When a section with a recipe is referred to, either through variable
1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022
substitution or by an initializing recipe, the section is treated as a
part and added to the part list before the referencing part.  For
example, we can leave data-dir out of the parts list:

    >>> write(sample_buildout, 'buildout.cfg',
    ... """
    ... [buildout]
    ... develop = recipes
    ... parts = debug
    ... log-level = INFO
    ...
    ... [debug]
    ... recipe = recipes:debug
1023 1024
    ... File-1 = ${data-dir:path}/file
    ... File-2 = ${debug:File-1}/log
1025 1026 1027 1028 1029 1030 1031 1032 1033
    ...
    ... [data-dir]
    ... recipe = recipes:mkdir
    ... path = mydata
    ... """)


It will still be treated as a part:

1034
    >>> print_(system(buildout), end='')
1035
    Develop: '/sample-buildout/recipes'
1036
    Uninstalling debug.
1037
    Updating data-dir.
1038
    Installing debug.
1039 1040
    File-1 /sample-buildout/mydata/file
    File-2 /sample-buildout/mydata/file/log
1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061
    recipe recipes:debug

    >>> cat('.installed.cfg') # doctest: +ELLIPSIS
    [buildout]
    installed_develop_eggs = /sample-buildout/develop-eggs/recipes.egg-link
    parts = data-dir debug
    ...

Note that the data-dir part is included *before* the debug part,
because the debug part refers to the data-dir part.  Even if we list
the data-dir part after the debug part, it will be included before:

    >>> write(sample_buildout, 'buildout.cfg',
    ... """
    ... [buildout]
    ... develop = recipes
    ... parts = debug data-dir
    ... log-level = INFO
    ...
    ... [debug]
    ... recipe = recipes:debug
1062 1063
    ... File-1 = ${data-dir:path}/file
    ... File-2 = ${debug:File-1}/log
1064 1065 1066 1067 1068 1069
    ...
    ... [data-dir]
    ... recipe = recipes:mkdir
    ... path = mydata
    ... """)

1070

1071
It will still be treated as a part:
1072

1073
    >>> print_(system(buildout), end='')
1074 1075 1076
    Develop: '/sample-buildout/recipes'
    Updating data-dir.
    Updating debug.
1077 1078
    File-1 /sample-buildout/mydata/file
    File-2 /sample-buildout/mydata/file/log
1079
    recipe recipes:debug
1080

1081 1082 1083 1084 1085 1086
    >>> cat('.installed.cfg') # doctest: +ELLIPSIS
    [buildout]
    installed_develop_eggs = /sample-buildout/develop-eggs/recipes.egg-link
    parts = data-dir debug
    ...

Jim Fulton's avatar
Jim Fulton committed
1087 1088 1089 1090 1091
Extending sections (macros)
---------------------------

A section (other than the buildout section) can extend one or more
other sections using the ``<`` option.  Options from the referenced
Patrick Strawderman's avatar
Patrick Strawderman committed
1092
sections are copied to the referring section *before* variable
Jim Fulton's avatar
Jim Fulton committed
1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121
substitution.  This, together with the ability to refer to variables
of the current section allows sections to be used as macros.

    >>> write(sample_buildout, 'buildout.cfg',
    ... """
    ... [buildout]
    ... develop = recipes
    ... parts = myfiles
    ... log-level = INFO
    ...
    ... [debug]
    ... recipe = recipes:debug
    ...
    ... [with_file1]
    ... <= debug
    ... file1 = ${:path}/file1
    ... color = red
    ...
    ... [with_file2]
    ... <= debug
    ... file2 = ${:path}/file2
    ... color = blue
    ...
    ... [myfiles]
    ... <= with_file1
    ...    with_file2
    ... path = mydata
    ... """)

1122
    >>> print_(system(buildout), end='')
Jim Fulton's avatar
Jim Fulton committed
1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135
    Develop: '/sample-buildout/recipes'
    Uninstalling debug.
    Uninstalling data-dir.
    Installing myfiles.
    color blue
    file1 mydata/file1
    file2 mydata/file2
    path mydata
    recipe recipes:debug

In this example, the debug, with_file1 and with_file2 sections act as
macros. In particular, the variable substitutions are performed
relative to the myfiles section.
1136

1137 1138 1139 1140 1141
Conditional sections
--------------------

Sometimes, you need different configuration in different environments
(different operating systems, or different versions of Python).  To
Jim Fulton's avatar
Jim Fulton committed
1142
make this easier, you can define environment-specific options by
1143 1144
providing conditional sections::

Jim Fulton's avatar
Jim Fulton committed
1145 1146 1147
    [ctl]
    suffix =

1148 1149 1150
    [ctl:windows]
    suffix = .bat

Jim Fulton's avatar
Jim Fulton committed
1151 1152 1153 1154 1155 1156 1157 1158 1159
.. -> conf

    >>> import zc.buildout.configparser
    >>> zc.buildout.configparser.parse(
    ...     StringIO.StringIO(conf), 'test', lambda : dict(windows=True))
    {'ctl': {'suffix': '.bat'}}
    >>> zc.buildout.configparser.parse(
    ...     StringIO.StringIO(conf), 'test', lambda : dict(windows=False))
    {'ctl': {'suffix': ''}}
1160 1161 1162 1163 1164 1165 1166

In this tiny example, we've defined a ``ctl:suffix`` option that's
``.bat`` on Windows and an empty string elsewhere.

A conditional section has a colon and then a Python expression after
the name.  If the Python expression result is true, the section
options from the section are included.  If the value is false, the
Jim Fulton's avatar
Jim Fulton committed
1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181
section is ignored.

Some things to note:

- If there is no exception, then options from the section are
  included.

- Sections and options can be repeated.  If an option is repeated, the
  last value is used. In the example above, on Windows, the second
  ``suffix`` option overrides the first.  If the order of the sections
  was reversed, the conditional section would have no effect.

In addition to the normal built-ins, the expression has access to
global variable that make common cases short and description as shown
above:
1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242

sys
  the ``sys`` module

os
  the ``os`` module

platform
  the ``platform`` module

re
  The ``re`` module

python2
  We're running Python 2

python3
  We're running Python 3

python26
  We're running Python 2.6

python27
  We're running Python 2.7

python32
  We're running Python 3.2

python33
  We're running Python 3.3

sys_version
  ``sys.version.lower()``

pypy
  We're running PyPy

jython
  We're running Jython

iron
  We're running Iron Python

cpython
  We're not running PyPy, Jython, or Iron Python

sys_platform
  ``str(sys.platform).lower()``

linux
  We're running on linux

windows
  We're running on Windows

cygwin
  We're running on cygwin

solaris
  We're running on solaris

1243 1244
macosx
  We're running on Mac OS X
1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263

posix
  We're running on a POSIX-compatible system

bits32
  We're running on a 32-bit system.

bits64
  We're running on a 64-bit system.

little_endian
  We're running on a little-endian system

big_endian
  We're running on a little-endian system

Expressions must not contain either the ``#`` or the ``;`` character.


1264 1265 1266 1267 1268 1269 1270 1271
Adding and removing options
---------------------------

We can append and remove values to an option by using the + and -
operators.

This is illustrated below; first we define a base configuration.

1272
    >>> write(sample_buildout, 'base.cfg',
1273 1274 1275 1276 1277
    ... """
    ... [buildout]
    ... parts = part1 part2 part3
    ...
    ... [part1]
1278
    ... recipe =
1279 1280 1281
    ... option = a1 a2
    ...
    ... [part2]
1282
    ... recipe =
1283
    ... option = b1 b2 b3 b4
1284
    ...
1285
    ... [part3]
1286
    ... recipe =
1287
    ... option = c1 c2
1288
    ...
1289 1290 1291
    ... [part4]
    ... recipe =
    ... option = d2
1292 1293
    ...     d3
    ...     d5
1294
    ...
1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316
    ... """)

Extending this configuration, we can "adjust" the values set in the
base configuration file.

    >>> write(sample_buildout, 'extension1.cfg',
    ... """
    ... [buildout]
    ... extends = base.cfg
    ...
    ... # appending values
    ... [part1]
    ... option += a3 a4
    ...
    ... # removing values
    ... [part2]
    ... option -= b1 b2
    ...
    ... # alt. spelling
    ... [part3]
    ... option+=c3 c4 c5
    ...
1317
    ... # combining both adding and removing
1318
    ... [part4]
1319 1320 1321 1322 1323 1324
    ... option += d1
    ...      d4
    ... option -= d5
    ...
    ... # normal assignment
    ... [part5]
1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344
    ... option = h1 h2
    ...
    ... """)

An additional extension.

    >>> write(sample_buildout, 'extension2.cfg',
    ... """
    ... [buildout]
    ... extends = extension1.cfg
    ...
    ... # appending values
    ... [part1]
    ... option += a5
    ...
    ... # removing values
    ... [part2]
    ... option -= b1 b2 b3
    ...
    ... """)
1345

1346 1347
To verify that the options are adjusted correctly, we'll set up an
extension that prints out the options.
1348

1349 1350 1351
    >>> mkdir(sample_buildout, 'demo')
    >>> write(sample_buildout, 'demo', 'demo.py',
    ... """
1352
    ... import sys
1353
    ... def ext(buildout):
1354
    ...     sys.stdout.write(str(
1355
    ...         [part['option'] for name, part in sorted(buildout.items())
1356
    ...          if name.startswith('part')])+'\\n')
1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374
    ... """)

    >>> write(sample_buildout, 'demo', 'setup.py',
    ... """
    ... from setuptools import setup
    ...
    ... setup(
    ...     name="demo",
    ...     entry_points={'zc.buildout.extension': ['ext = demo:ext']},
    ...     )
    ... """)

Set up a buildout configuration for this extension.

    >>> write(sample_buildout, 'buildout.cfg',
    ... """
    ... [buildout]
    ... develop = demo
1375
    ... parts =
1376 1377 1378
    ... """)

    >>> os.chdir(sample_buildout)
1379 1380 1381
    >>> print_(system(os.path.join(sample_buildout, 'bin', 'buildout')), end='')
    ... # doctest: +ELLIPSIS
    Develop: '/sample-buildout/demo'...
1382 1383 1384 1385 1386 1387 1388 1389 1390 1391

Verify option values.

    >>> write(sample_buildout, 'buildout.cfg',
    ... """
    ... [buildout]
    ... develop = demo
    ... extensions = demo
    ... extends = extension2.cfg
    ... """)
1392

1393
    >>> print_(system(os.path.join('bin', 'buildout')), end='')
1394
    ['a1 a2/na3 a4/na5', 'b1 b2 b3 b4', 'c1 c2/nc3 c4 c5', 'd2/nd3/nd1/nd4', 'h1 h2']
1395 1396
    Develop: '/sample-buildout/demo'

1397 1398 1399
Annotated sections output shows which files are responsible for which
operations.

1400
    >>> print_(system(os.path.join('bin', 'buildout') + ' annotate'), end='')
1401
    ... # doctest: +ELLIPSIS +NORMALIZE_WHITESPACE
1402 1403 1404 1405 1406 1407
    <BLANKLINE>
    Annotated sections
    ==================
    ...
    <BLANKLINE>
    [part1]
1408
    option= a1 a2
1409 1410
    a3 a4
    a5
1411 1412 1413
        /sample-buildout/base.cfg
    +=  /sample-buildout/extension1.cfg
    +=  /sample-buildout/extension2.cfg
1414
    recipe=
1415
        /sample-buildout/base.cfg
1416 1417
    <BLANKLINE>
    [part2]
1418 1419 1420 1421
    option= b1 b2 b3 b4
        /sample-buildout/base.cfg
    -=  /sample-buildout/extension1.cfg
    -=  /sample-buildout/extension2.cfg
1422
    recipe=
1423
        /sample-buildout/base.cfg
1424 1425
    <BLANKLINE>
    [part3]
1426
    option= c1 c2
1427
    c3 c4 c5
1428 1429
        /sample-buildout/base.cfg
    +=  /sample-buildout/extension1.cfg
1430
    recipe=
1431
        /sample-buildout/base.cfg
1432 1433
    <BLANKLINE>
    [part4]
1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444
    option= d2
    d3
    d1
    d4
        /sample-buildout/base.cfg
    +=  /sample-buildout/extension1.cfg
    -=  /sample-buildout/extension1.cfg
    recipe=
        /sample-buildout/base.cfg
    <BLANKLINE>
    [part5]
1445 1446
    option= h1 h2
        /sample-buildout/extension1.cfg
1447 1448 1449 1450 1451 1452
    [versions]
    zc.buildout= >=1.99
        DEFAULT_VALUE
    zc.recipe.egg= >=1.99
        DEFAULT_VALUE
    <BLANKLINE>
1453

1454 1455 1456 1457 1458
Cleanup.

    >>> os.remove(os.path.join(sample_buildout, 'base.cfg'))
    >>> os.remove(os.path.join(sample_buildout, 'extension1.cfg'))
    >>> os.remove(os.path.join(sample_buildout, 'extension2.cfg'))
1459

1460 1461 1462 1463 1464 1465 1466 1467 1468
Multiple configuration files
----------------------------

A configuration file can "extend" another configuration file.
Options are read from the other configuration file if they aren't
already defined by your configuration file.

The configuration files your file extends can extend
other configuration files.  The same file may be
1469 1470 1471 1472 1473 1474 1475 1476 1477 1478
used more than once although, of course, cycles aren't allowed.

To see how this works, we use an example:

    >>> write(sample_buildout, 'buildout.cfg',
    ... """
    ... [buildout]
    ... extends = base.cfg
    ...
    ... [debug]
1479
    ... op = buildout
1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492
    ... """)

    >>> write(sample_buildout, 'base.cfg',
    ... """
    ... [buildout]
    ... develop = recipes
    ... parts = debug
    ...
    ... [debug]
    ... recipe = recipes:debug
    ... op = base
    ... """)

1493
    >>> print_(system(buildout), end='')
1494 1495
    Develop: '/sample-buildout/recipes'
    Installing debug.
1496
    op buildout
1497 1498 1499 1500 1501
    recipe recipes:debug

The example is pretty trivial, but the pattern it illustrates is
pretty common.  In a more practical example, the base buildout might
represent a product and the extending buildout might be a
1502
customization.
1503

1504
Here is a more elaborate example.
1505

1506
    >>> other = tmpdir('other')
1507 1508 1509 1510

    >>> write(sample_buildout, 'buildout.cfg',
    ... """
    ... [buildout]
1511
    ... extends = b1.cfg b2.cfg %(b3)s
1512 1513
    ...
    ... [debug]
1514 1515
    ... op = buildout
    ... """ % dict(b3=os.path.join(other, 'b3.cfg')))
1516 1517 1518 1519 1520 1521 1522

    >>> write(sample_buildout, 'b1.cfg',
    ... """
    ... [buildout]
    ... extends = base.cfg
    ...
    ... [debug]
1523 1524
    ... op1 = b1 1
    ... op2 = b1 2
1525 1526 1527 1528 1529 1530 1531 1532
    ... """)

    >>> write(sample_buildout, 'b2.cfg',
    ... """
    ... [buildout]
    ... extends = base.cfg
    ...
    ... [debug]
1533 1534
    ... op2 = b2 2
    ... op3 = b2 3
1535 1536
    ... """)

1537
    >>> write(other, 'b3.cfg',
1538 1539
    ... """
    ... [buildout]
1540
    ... extends = b3base.cfg
1541 1542
    ...
    ... [debug]
1543
    ... op4 = b3 4
1544 1545
    ... """)

1546
    >>> write(other, 'b3base.cfg',
1547 1548
    ... """
    ... [debug]
1549
    ... op5 = b3base 5
1550 1551
    ... """)

1552
    >>> write(sample_buildout, 'base.cfg',
1553 1554
    ... """
    ... [buildout]
1555 1556
    ... develop = recipes
    ... parts = debug
1557 1558
    ...
    ... [debug]
1559 1560
    ... recipe = recipes:debug
    ... name = base
1561 1562
    ... """)

1563
    >>> print_(system(buildout), end='')
1564 1565 1566
    Develop: '/sample-buildout/recipes'
    Uninstalling debug.
    Installing debug.
1567
    name base
1568
    op buildout
1569 1570
    op1 b1 1
    op2 b2 2
1571
    op3 b2 3
1572 1573
    op4 b3 4
    op5 b3base 5
1574 1575 1576 1577
    recipe recipes:debug

There are several things to note about this example:

1578
- We can name multiple files in an extends option.
1579 1580 1581

- We can reference files recursively.

1582 1583
- Relative file names in extended options are interpreted relative to
  the directory containing the referencing configuration file.
1584

1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603
Loading Configuration from URLs
-------------------------------

Configuration files can be loaded from URLs.  To see how this works,
we'll set up a web server with some configuration files.

    >>> server_data = tmpdir('server_data')

    >>> write(server_data, "r1.cfg",
    ... """
    ... [debug]
    ... op1 = r1 1
    ... op2 = r1 2
    ... """)

    >>> write(server_data, "r2.cfg",
    ... """
    ... [buildout]
    ... extends = r1.cfg
1604
    ...
1605 1606 1607 1608 1609 1610 1611
    ... [debug]
    ... op2 = r2 2
    ... op3 = r2 3
    ... """)

    >>> server_url = start_server(server_data)

1612
    >>> write('client.cfg',
1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624
    ... """
    ... [buildout]
    ... develop = recipes
    ... parts = debug
    ... extends = %(url)s/r2.cfg
    ...
    ... [debug]
    ... recipe = recipes:debug
    ... name = base
    ... """ % dict(url=server_url))


1625
    >>> print_(system(buildout+ ' -c client.cfg'), end='')
1626 1627 1628
    Develop: '/sample-buildout/recipes'
    Uninstalling debug.
    Installing debug.
1629 1630 1631 1632 1633 1634 1635
    name base
    op1 r1 1
    op2 r2 2
    op3 r2 3
    recipe recipes:debug

Here we specified a URL for the file we extended.  The file we
1636
downloaded, itself referred to a file on the server using a relative
1637 1638 1639 1640
URL reference.  Relative references are interpreted relative to the
base URL when they appear in configuration files loaded via URL.

We can also specify a URL as the configuration file to be used by a
1641
buildout.
1642 1643

    >>> os.remove('client.cfg')
1644
    >>> write(server_data, 'remote.cfg',
1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655
    ... """
    ... [buildout]
    ... develop = recipes
    ... parts = debug
    ... extends = r2.cfg
    ...
    ... [debug]
    ... recipe = recipes:debug
    ... name = remote
    ... """)

1656
    >>> print_(system(buildout + ' -c ' + server_url + '/remote.cfg'), end='')
1657
    While:
1658
      Initializing.
1659 1660 1661 1662 1663 1664 1665
    Error: Missing option: buildout:directory

Normally, the buildout directory defaults to directory
containing a configuration file.  This won't work for configuration
files loaded from URLs.  In this case, the buildout directory would
normally be defined on the command line:

1666
    >>> print_(system(buildout
1667 1668
    ...              + ' -c ' + server_url + '/remote.cfg'
    ...              + ' buildout:directory=' + sample_buildout
1669
    ...              ), end='')
1670 1671 1672
    Develop: '/sample-buildout/recipes'
    Uninstalling debug.
    Installing debug.
1673 1674 1675 1676 1677 1678
    name remote
    op1 r1 1
    op2 r2 2
    op3 r2 3
    recipe recipes:debug

1679 1680 1681
User defaults
-------------

Fred Drake's avatar
Fred Drake committed
1682
If the file $HOME/.buildout/default.cfg, exists, it is read before
1683
reading the configuration file.  ($HOME is the value of the HOME
1684
environment variable. The '/' is replaced by the operating system file
1685 1686
delimiter.)

1687
    >>> old_home = os.environ['HOME']
1688
    >>> home = tmpdir('home')
1689 1690 1691 1692 1693 1694 1695 1696 1697
    >>> mkdir(home, '.buildout')
    >>> write(home, '.buildout', 'default.cfg',
    ... """
    ... [debug]
    ... op1 = 1
    ... op7 = 7
    ... """)

    >>> os.environ['HOME'] = home
1698
    >>> print_(system(buildout), end='')
1699 1700 1701
    Develop: '/sample-buildout/recipes'
    Uninstalling debug.
    Installing debug.
1702
    name base
1703
    op buildout
1704 1705
    op1 b1 1
    op2 b2 2
1706
    op3 b2 3
1707 1708
    op4 b3 4
    op5 b3base 5
1709 1710 1711
    op7 7
    recipe recipes:debug

Jim Fulton's avatar
Jim Fulton committed
1712 1713 1714
A buildout command-line argument, -U, can be used to suppress reading
user defaults:

1715
    >>> print_(system(buildout + ' -U'), end='')
1716 1717 1718
    Develop: '/sample-buildout/recipes'
    Uninstalling debug.
    Installing debug.
Jim Fulton's avatar
Jim Fulton committed
1719 1720 1721 1722 1723 1724 1725 1726 1727
    name base
    op buildout
    op1 b1 1
    op2 b2 2
    op3 b2 3
    op4 b3 4
    op5 b3base 5
    recipe recipes:debug

1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773
If the environment variable BUILDOUT_HOME is non-empty, that is used to
locate default.cfg instead of looking in ~/.buildout/.  Let's set up a
configuration file in an alternate directory and verify that we get the
appropriate set of defaults:

    >>> alterhome = tmpdir('alterhome')
    >>> write(alterhome, 'default.cfg',
    ... """
    ... [debug]
    ... op1 = 1'
    ... op7 = 7'
    ... op8 = eight!
    ... """)

    >>> os.environ['BUILDOUT_HOME'] = alterhome
    >>> print_(system(buildout), end='')
    Develop: '/sample-buildout/recipes'
    Uninstalling debug.
    Installing debug.
    name base
    op buildout
    op1 b1 1
    op2 b2 2
    op3 b2 3
    op4 b3 4
    op5 b3base 5
    op7 7'
    op8 eight!
    recipe recipes:debug

The -U argument still suppresses reading of the default.cfg file from
BUILDOUT_HOME:

    >>> print_(system(buildout + ' -U'), end='')
    Develop: '/sample-buildout/recipes'
    Uninstalling debug.
    Installing debug.
    name base
    op buildout
    op1 b1 1
    op2 b2 2
    op3 b2 3
    op4 b3 4
    op5 b3base 5
    recipe recipes:debug

1774
    >>> os.environ['HOME'] = old_home
1775
    >>> del os.environ['BUILDOUT_HOME']
1776

1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790
Log level
---------

We can control the level of logging by specifying a log level in out
configuration file.  For example, so suppress info messages, we can
set the logging level to WARNING

    >>> write(sample_buildout, 'buildout.cfg',
    ... """
    ... [buildout]
    ... log-level = WARNING
    ... extends = b1.cfg b2.cfg
    ... """)

1791
    >>> print_(system(buildout), end='')
1792 1793 1794
    name base
    op1 b1 1
    op2 b2 2
1795 1796 1797
    op3 b2 3
    recipe recipes:debug

1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815
Socket timeout
--------------

The timeout of the connections to egg and configuration servers can be
configured in the buildout section. Its value is configured in seconds.

    >>> write(sample_buildout, 'buildout.cfg',
    ... """
    ... [buildout]
    ... socket-timeout = 5
    ... develop = recipes
    ... parts = debug
    ...
    ... [debug]
    ... recipe = recipes:debug
    ... op = timeout
    ... """)

1816
    >>> print_(system(buildout), end='')
1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838
    Setting socket time out to 5 seconds.
    Develop: '/sample-buildout/recipes'
    Uninstalling debug.
    Installing debug.
    op timeout
    recipe recipes:debug

If the socket-timeout is not numeric, a warning is issued and the default
timeout of the Python socket module is used.

    >>> write(sample_buildout, 'buildout.cfg',
    ... """
    ... [buildout]
    ... socket-timeout = 5s
    ... develop = recipes
    ... parts = debug
    ...
    ... [debug]
    ... recipe = recipes:debug
    ... op = timeout
    ... """)

1839
    >>> print_(system(buildout), end='')
1840 1841 1842 1843 1844 1845 1846 1847
    Default socket timeout is used !
    Value in configuration is not numeric: [5s].
    <BLANKLINE>
    Develop: '/sample-buildout/recipes'
    Updating debug.
    op timeout
    recipe recipes:debug

1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873
Uninstall recipes
-----------------

As we've seen, when parts are installed, buildout keeps track of files
and directories that they create. When the parts are uninstalled these
files and directories are deleted.

Sometimes more clean up is needed. For example, a recipe might add a
system service by calling chkconfig --add during installation. Later
during uninstallation, chkconfig --del will need to be called to
remove the system service.

In order to deal with these uninstallation issues, you can register
uninstall recipes. Uninstall recipes are registered using the
'zc.buildout.uninstall' entry point. Parts specify uninstall recipes
using the 'uninstall' option.

In comparison to regular recipes, uninstall recipes are much
simpler. They are simply callable objects that accept the name of the
part to be uninstalled and the part's options dictionary. Uninstall
recipes don't have access to the part itself since it maybe not be
able to be instantiated at uninstallation time.

Here's a recipe that simulates installation of a system service, along
with an uninstall recipe that simulates removing the service.

1874
    >>> write(sample_buildout, 'recipes', 'service.py',
1875
    ... """
1876
    ... import sys
1877 1878 1879 1880 1881 1882 1883 1884
    ... class Service:
    ...
    ...     def __init__(self, buildout, name, options):
    ...         self.buildout = buildout
    ...         self.name = name
    ...         self.options = options
    ...
    ...     def install(self):
1885 1886
    ...         sys.stdout.write("chkconfig --add %s\\n"
    ...                          % self.options['script'])
1887 1888 1889 1890 1891 1892 1893
    ...         return ()
    ...
    ...     def update(self):
    ...         pass
    ...
    ...
    ... def uninstall_service(name, options):
1894
    ...     sys.stdout.write("chkconfig --del %s\\n" % options['script'])
1895 1896
    ... """)

1897 1898 1899 1900
To use these recipes we must register them using entry points. Make
sure to use the same name for the recipe and uninstall recipe. This is
required to let buildout know which uninstall recipe goes with which
recipe.
1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912

    >>> write(sample_buildout, 'recipes', 'setup.py',
    ... """
    ... from setuptools import setup
    ... entry_points = (
    ... '''
    ... [zc.buildout]
    ... mkdir = mkdir:Mkdir
    ... debug = debug:Debug
    ... service = service:Service
    ...
    ... [zc.buildout.uninstall]
1913
    ... service = service:uninstall_service
1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932
    ... ''')
    ... setup(name="recipes", entry_points=entry_points)
    ... """)

Here's how these recipes could be used in a buildout:

    >>> write(sample_buildout, 'buildout.cfg',
    ... """
    ... [buildout]
    ... develop = recipes
    ... parts = service
    ...
    ... [service]
    ... recipe = recipes:service
    ... script = /path/to/script
    ... """)

When the buildout is run the service will be installed

1933
    >>> print_(system(buildout), end='')
1934 1935 1936
    Develop: '/sample-buildout/recipes'
    Uninstalling debug.
    Installing service.
1937 1938
    chkconfig --add /path/to/script

1939
The service has been installed. If the buildout is run again with no
1940
changes, the service shouldn't be changed.
1941

1942
    >>> print_(system(buildout), end='')
1943 1944
    Develop: '/sample-buildout/recipes'
    Updating service.
1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959

Now we change the service part to trigger uninstallation and
re-installation.

    >>> write(sample_buildout, 'buildout.cfg',
    ... """
    ... [buildout]
    ... develop = recipes
    ... parts = service
    ...
    ... [service]
    ... recipe = recipes:service
    ... script = /path/to/a/different/script
    ... """)

1960
    >>> print_(system(buildout), end='')
1961 1962 1963
    Develop: '/sample-buildout/recipes'
    Uninstalling service.
    Running uninstall recipe.
1964
    chkconfig --del /path/to/script
1965
    Installing service.
1966 1967 1968 1969 1970 1971 1972 1973 1974
    chkconfig --add /path/to/a/different/script

Now we remove the service part, and add another part.

    >>> write(sample_buildout, 'buildout.cfg',
    ... """
    ... [buildout]
    ... develop = recipes
    ... parts = debug
1975
    ...
1976 1977 1978 1979
    ... [debug]
    ... recipe = recipes:debug
    ... """)

1980
    >>> print_(system(buildout), end='')
1981 1982 1983
    Develop: '/sample-buildout/recipes'
    Uninstalling service.
    Running uninstall recipe.
1984
    chkconfig --del /path/to/a/different/script
1985
    Installing debug.
1986 1987 1988 1989 1990 1991 1992 1993 1994
    recipe recipes:debug

Uninstall recipes don't have to take care of removing all the files
and directories created by the part. This is still done automatically,
following the execution of the uninstall recipe. An upshot is that an
uninstallation recipe can access files and directories created by a
recipe before they are deleted.

For example, here's an uninstallation recipe that simulates backing up
1995 1996
a directory before it is deleted. It is designed to work with the
mkdir recipe introduced earlier.
1997 1998

    >>> write(sample_buildout, 'recipes', 'backup.py',
1999
    ... """
2000
    ... import os, sys
2001 2002
    ... def backup_directory(name, options):
    ...     path = options['path']
2003
    ...     size = len(os.listdir(path))
2004 2005
    ...     sys.stdout.write("backing up directory %s of size %s\\n"
    ...                      % (path, size))
2006 2007
    ... """)

2008 2009 2010
It must be registered with the zc.buildout.uninstall entry
point. Notice how it is given the name 'mkdir' to associate it with
the mkdir recipe.
2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023

    >>> write(sample_buildout, 'recipes', 'setup.py',
    ... """
    ... from setuptools import setup
    ... entry_points = (
    ... '''
    ... [zc.buildout]
    ... mkdir = mkdir:Mkdir
    ... debug = debug:Debug
    ... service = service:Service
    ...
    ... [zc.buildout.uninstall]
    ... uninstall_service = service:uninstall_service
2024
    ... mkdir = backup:backup_directory
2025 2026 2027 2028
    ... ''')
    ... setup(name="recipes", entry_points=entry_points)
    ... """)

2029
Now we can use it with a mkdir part.
2030 2031 2032 2033 2034 2035

    >>> write(sample_buildout, 'buildout.cfg',
    ... """
    ... [buildout]
    ... develop = recipes
    ... parts = dir debug
2036
    ...
2037 2038 2039 2040 2041 2042 2043 2044 2045 2046
    ... [dir]
    ... recipe = recipes:mkdir
    ... path = my_directory
    ...
    ... [debug]
    ... recipe = recipes:debug
    ... """)

Run the buildout to install the part.

2047
    >>> print_(system(buildout), end='')
2048 2049 2050
    Develop: '/sample-buildout/recipes'
    Uninstalling debug.
    Installing dir.
2051
    dir: Creating directory my_directory
2052
    Installing debug.
2053 2054 2055 2056 2057 2058 2059 2060 2061
    recipe recipes:debug

Now we remove the part from the configuration file.

    >>> write(sample_buildout, 'buildout.cfg',
    ... """
    ... [buildout]
    ... develop = recipes
    ... parts = debug
2062
    ...
2063 2064 2065 2066 2067 2068 2069
    ... [debug]
    ... recipe = recipes:debug
    ... """)

When the buildout is run the part is removed, and the uninstall recipe
is run before the directory is deleted.

2070
    >>> print_(system(buildout), end='')
2071 2072 2073
    Develop: '/sample-buildout/recipes'
    Uninstalling dir.
    Running uninstall recipe.
2074
    backing up directory /sample-buildout/my_directory of size 0
2075
    Updating debug.
2076 2077
    recipe recipes:debug

2078
Now we will return the registration to normal for the benefit of the
2079 2080 2081 2082 2083 2084 2085 2086 2087 2088 2089 2090 2091 2092
rest of the examples.

    >>> write(sample_buildout, 'recipes', 'setup.py',
    ... """
    ... from setuptools import setup
    ... entry_points = (
    ... '''
    ... [zc.buildout]
    ... mkdir = mkdir:Mkdir
    ... debug = debug:Debug
    ... ''')
    ... setup(name="recipes", entry_points=entry_points)
    ... """)

2093

2094 2095 2096 2097 2098 2099
Command-line usage
------------------

A number of arguments can be given on the buildout command line.  The
command usage is::

Jim Fulton's avatar
Jim Fulton committed
2100
  buildout [options and assignments] [command [command arguments]]
2101

Jim Fulton's avatar
Jim Fulton committed
2102
The following options are supported:
2103

Jim Fulton's avatar
Jim Fulton committed
2104 2105 2106
-h (or --help)
    Print basic usage information.  If this option is used, then all
    other options are ignored.
Jim Fulton's avatar
Jim Fulton committed
2107

Jim Fulton's avatar
Jim Fulton committed
2108 2109
-c filename
    The -c option can be used to specify a configuration file, rather than
2110
    buildout.cfg in the current directory.
Jim Fulton's avatar
Jim Fulton committed
2111

2112 2113 2114 2115 2116

-t socket_timeout

   Specify the socket timeout in seconds.

Jim Fulton's avatar
Jim Fulton committed
2117 2118 2119 2120 2121 2122 2123 2124 2125 2126 2127 2128
-v
    Increment the verbosity by 10.  The verbosity is used to adjust
    the logging level.  The verbosity is subtracted from the numeric
    value of the log-level option specified in the configuration file.

-q
    Decrement the verbosity by 10.

-U
    Don't read user-default configuration.

-o
2129
    Run in off-line mode.  This is equivalent to the assignment
Jim Fulton's avatar
Jim Fulton committed
2130
    buildout:offline=true.
Jim Fulton's avatar
Jim Fulton committed
2131

Jim Fulton's avatar
Jim Fulton committed
2132
-O
2133
    Run in non-off-line mode.  This is equivalent to the assignment
Jim Fulton's avatar
Jim Fulton committed
2134 2135 2136 2137 2138 2139 2140 2141 2142 2143 2144
    buildout:offline=false.  This is the default buildout mode.  The
    -O option would normally be used to override a true offline
    setting in a configuration file.

-n
    Run in newest mode.  This is equivalent to the assignment
    buildout:newest=true.  With this setting, which is the default,
    buildout will try to find the newest versions of distributions
    available that satisfy its requirements.

-N
2145
    Run in non-newest mode.  This is equivalent to the assignment
Jim Fulton's avatar
Jim Fulton committed
2146 2147
    buildout:newest=false.  With this setting, buildout will not seek
    new distributions if installed distributions satisfy it's
2148
    requirements.
Jim Fulton's avatar
Jim Fulton committed
2149

Jim Fulton's avatar
Jim Fulton committed
2150
Assignments are of the form::
2151 2152 2153

  section_name:option_name=value

2154 2155 2156 2157 2158 2159 2160 2161
Or::

  option_name=value

which is equivalent to::

  buildout:option_name=value

Jim Fulton's avatar
Jim Fulton committed
2162 2163 2164
Options and assignments can be given in any order.

Here's an example:
Jim Fulton's avatar
Jim Fulton committed
2165 2166 2167 2168 2169 2170 2171

    >>> write(sample_buildout, 'other.cfg',
    ... """
    ... [buildout]
    ... develop = recipes
    ... parts = debug
    ... installed = .other.cfg
2172
    ... log-level = WARNING
Jim Fulton's avatar
Jim Fulton committed
2173 2174 2175 2176 2177
    ...
    ... [debug]
    ... name = other
    ... recipe = recipes:debug
    ... """)
2178

Jim Fulton's avatar
Jim Fulton committed
2179 2180
Note that we used the installed buildout option to specify an
alternate file to store information about installed parts.
2181

2182
    >>> print_(system(buildout+' -c other.cfg debug:op1=foo -v'), end='')
2183 2184
    Develop: '/sample-buildout/recipes'
    Installing debug.
Jim Fulton's avatar
Jim Fulton committed
2185
    name other
2186 2187 2188
    op1 foo
    recipe recipes:debug

2189
Here we used the -c option to specify an alternate configuration file,
Jim Fulton's avatar
Jim Fulton committed
2190 2191 2192 2193
and the -v option to increase the level of logging from the default,
WARNING.

Options can also be combined in the usual Unix way, as in:
2194

2195
    >>> print_(system(buildout+' -vcother.cfg debug:op1=foo'), end='')
2196 2197
    Develop: '/sample-buildout/recipes'
    Updating debug.
Jim Fulton's avatar
Jim Fulton committed
2198 2199 2200 2201 2202 2203 2204 2205
    name other
    op1 foo
    recipe recipes:debug

Here we combined the -v and -c options with the configuration file
name.  Note that the -c option has to be last, because it takes an
argument.

Jim Fulton's avatar
Jim Fulton committed
2206 2207 2208
    >>> os.remove(os.path.join(sample_buildout, 'other.cfg'))
    >>> os.remove(os.path.join(sample_buildout, '.other.cfg'))

2209 2210
The most commonly used command is 'install' and it takes a list of
parts to install. if any parts are specified, only those parts are
Jim Fulton's avatar
Jim Fulton committed
2211 2212
installed.  To illustrate this, we'll update our configuration and run
the buildout in the usual way:
2213 2214 2215 2216 2217 2218 2219 2220 2221 2222 2223 2224 2225 2226 2227 2228 2229 2230 2231 2232 2233 2234 2235

    >>> write(sample_buildout, 'buildout.cfg',
    ... """
    ... [buildout]
    ... develop = recipes
    ... parts = debug d1 d2 d3
    ...
    ... [d1]
    ... recipe = recipes:mkdir
    ... path = d1
    ...
    ... [d2]
    ... recipe = recipes:mkdir
    ... path = d2
    ...
    ... [d3]
    ... recipe = recipes:mkdir
    ... path = d3
    ...
    ... [debug]
    ... recipe = recipes:debug
    ... """)

2236
    >>> print_(system(buildout), end='')
2237 2238 2239
    Develop: '/sample-buildout/recipes'
    Uninstalling debug.
    Installing debug.
2240
    recipe recipes:debug
2241
    Installing d1.
Jim Fulton's avatar
Jim Fulton committed
2242
    d1: Creating directory d1
2243
    Installing d2.
Jim Fulton's avatar
Jim Fulton committed
2244
    d2: Creating directory d2
2245
    Installing d3.
Jim Fulton's avatar
Jim Fulton committed
2246
    d3: Creating directory d3
2247

2248 2249 2250 2251 2252 2253 2254 2255 2256 2257
    >>> ls(sample_buildout)
    -  .installed.cfg
    -  b1.cfg
    -  b2.cfg
    -  base.cfg
    d  bin
    -  buildout.cfg
    d  d1
    d  d2
    d  d3
2258
    d  demo
2259
    d  develop-eggs
2260 2261 2262 2263 2264
    d  eggs
    d  parts
    d  recipes

    >>> cat(sample_buildout, '.installed.cfg')
2265
    ... # doctest: +NORMALIZE_WHITESPACE
2266
    [buildout]
2267
    installed_develop_eggs = /sample-buildout/develop-eggs/recipes.egg-link
2268 2269 2270
    parts = debug d1 d2 d3
    <BLANKLINE>
    [debug]
2271
    __buildout_installed__ =
2272
    __buildout_signature__ = recipes-PiIFiO8ny5yNZ1S3JfT0xg==
2273 2274 2275
    recipe = recipes:debug
    <BLANKLINE>
    [d1]
2276
    __buildout_installed__ = /sample-buildout/d1
2277
    __buildout_signature__ = recipes-PiIFiO8ny5yNZ1S3JfT0xg==
2278
    path = /sample-buildout/d1
2279 2280 2281
    recipe = recipes:mkdir
    <BLANKLINE>
    [d2]
2282
    __buildout_installed__ = /sample-buildout/d2
2283
    __buildout_signature__ = recipes-PiIFiO8ny5yNZ1S3JfT0xg==
2284
    path = /sample-buildout/d2
2285 2286 2287
    recipe = recipes:mkdir
    <BLANKLINE>
    [d3]
2288
    __buildout_installed__ = /sample-buildout/d3
2289
    __buildout_signature__ = recipes-PiIFiO8ny5yNZ1S3JfT0xg==
2290
    path = /sample-buildout/d3
2291 2292 2293 2294 2295 2296 2297 2298 2299 2300 2301 2302 2303 2304 2305 2306 2307 2308 2309 2310
    recipe = recipes:mkdir

Now we'll update our configuration file:

    >>> write(sample_buildout, 'buildout.cfg',
    ... """
    ... [buildout]
    ... develop = recipes
    ... parts = debug d2 d3 d4
    ...
    ... [d2]
    ... recipe = recipes:mkdir
    ... path = data2
    ...
    ... [d3]
    ... recipe = recipes:mkdir
    ... path = data3
    ...
    ... [d4]
    ... recipe = recipes:mkdir
2311
    ... path = ${d2:path}-extra
2312 2313 2314 2315 2316 2317
    ...
    ... [debug]
    ... recipe = recipes:debug
    ... x = 1
    ... """)

2318
and run the buildout specifying just d3 and d4:
2319

2320
    >>> print_(system(buildout+' install d3 d4'), end='')
2321 2322 2323
    Develop: '/sample-buildout/recipes'
    Uninstalling d3.
    Installing d3.
Jim Fulton's avatar
Jim Fulton committed
2324
    d3: Creating directory data3
2325
    Installing d4.
2326
    d4: Creating directory data2-extra
2327

2328 2329 2330 2331 2332 2333 2334 2335 2336
    >>> ls(sample_buildout)
    -  .installed.cfg
    -  b1.cfg
    -  b2.cfg
    -  base.cfg
    d  bin
    -  buildout.cfg
    d  d1
    d  d2
2337
    d  data2-extra
2338
    d  data3
2339
    d  demo
2340
    d  develop-eggs
2341 2342 2343
    d  eggs
    d  parts
    d  recipes
2344

2345
Only the d3 and d4 recipes ran.  d3 was removed and data3 and data2-extra
2346 2347 2348 2349 2350
were created.

The .installed.cfg is only updated for the recipes that ran:

    >>> cat(sample_buildout, '.installed.cfg')
2351
    ... # doctest: +NORMALIZE_WHITESPACE
2352
    [buildout]
2353
    installed_develop_eggs = /sample-buildout/develop-eggs/recipes.egg-link
2354
    parts = debug d1 d2 d3 d4
2355 2356
    <BLANKLINE>
    [debug]
2357
    __buildout_installed__ =
2358
    __buildout_signature__ = recipes-PiIFiO8ny5yNZ1S3JfT0xg==
2359 2360
    recipe = recipes:debug
    <BLANKLINE>
2361 2362 2363 2364 2365 2366
    [d1]
    __buildout_installed__ = /sample-buildout/d1
    __buildout_signature__ = recipes-PiIFiO8ny5yNZ1S3JfT0xg==
    path = /sample-buildout/d1
    recipe = recipes:mkdir
    <BLANKLINE>
2367
    [d2]
2368
    __buildout_installed__ = /sample-buildout/d2
2369
    __buildout_signature__ = recipes-PiIFiO8ny5yNZ1S3JfT0xg==
2370
    path = /sample-buildout/d2
2371 2372 2373
    recipe = recipes:mkdir
    <BLANKLINE>
    [d3]
2374
    __buildout_installed__ = /sample-buildout/data3
2375
    __buildout_signature__ = recipes-PiIFiO8ny5yNZ1S3JfT0xg==
2376
    path = /sample-buildout/data3
2377 2378 2379
    recipe = recipes:mkdir
    <BLANKLINE>
    [d4]
2380
    __buildout_installed__ = /sample-buildout/data2-extra
2381
    __buildout_signature__ = recipes-PiIFiO8ny5yNZ1S3JfT0xg==
2382
    path = /sample-buildout/data2-extra
2383 2384 2385 2386 2387 2388
    recipe = recipes:mkdir

Note that the installed data for debug, d1, and d2 haven't changed,
because we didn't install those parts and that the d1 and d2
directories are still there.

Jim Fulton's avatar
Jim Fulton committed
2389
Now, if we run the buildout without the install command:
2390

2391
    >>> print_(system(buildout), end='')
2392 2393 2394 2395 2396
    Develop: '/sample-buildout/recipes'
    Uninstalling d2.
    Uninstalling d1.
    Uninstalling debug.
    Installing debug.
2397 2398
    recipe recipes:debug
    x 1
2399
    Installing d2.
Jim Fulton's avatar
Jim Fulton committed
2400
    d2: Creating directory data2
2401 2402
    Updating d3.
    Updating d4.
2403 2404 2405 2406 2407 2408 2409 2410 2411 2412 2413 2414

We see the output of the debug recipe and that data2 was created.  We
also see that d1 and d2 have gone away:

    >>> ls(sample_buildout)
    -  .installed.cfg
    -  b1.cfg
    -  b2.cfg
    -  base.cfg
    d  bin
    -  buildout.cfg
    d  data2
2415
    d  data2-extra
2416
    d  data3
2417
    d  demo
2418
    d  develop-eggs
2419 2420 2421 2422
    d  eggs
    d  parts
    d  recipes

Jim Fulton's avatar
Jim Fulton committed
2423 2424
Alternate directory and file locations
--------------------------------------
2425 2426

The buildout normally puts the bin, eggs, and parts directories in the
2427 2428
directory in the directory containing the configuration file. You can
provide alternate locations, and even names for these directories.
2429

2430
    >>> alt = tmpdir('sample-alt')
2431 2432 2433 2434 2435

    >>> write(sample_buildout, 'buildout.cfg',
    ... """
    ... [buildout]
    ... develop = recipes
2436
    ... parts =
2437
    ... develop-eggs-directory = %(developbasket)s
2438 2439 2440 2441
    ... eggs-directory = %(basket)s
    ... bin-directory = %(scripts)s
    ... parts-directory = %(work)s
    ... """ % dict(
2442
    ...    developbasket = os.path.join(alt, 'developbasket'),
2443 2444 2445 2446
    ...    basket = os.path.join(alt, 'basket'),
    ...    scripts = os.path.join(alt, 'scripts'),
    ...    work = os.path.join(alt, 'work'),
    ... ))
2447

2448
    >>> print_(system(buildout), end='')
2449 2450 2451 2452 2453 2454 2455 2456 2457
    Creating directory '/sample-alt/scripts'.
    Creating directory '/sample-alt/work'.
    Creating directory '/sample-alt/basket'.
    Creating directory '/sample-alt/developbasket'.
    Develop: '/sample-buildout/recipes'
    Uninstalling d4.
    Uninstalling d3.
    Uninstalling d2.
    Uninstalling debug.
2458 2459 2460

    >>> ls(alt)
    d  basket
2461
    d  developbasket
2462 2463 2464
    d  scripts
    d  work

2465
    >>> ls(alt, 'developbasket')
2466 2467
    -  recipes.egg-link

2468 2469
You can also specify an alternate buildout directory:

2470 2471
    >>> rmdir(alt)
    >>> alt = tmpdir('sample-alt')
2472 2473 2474 2475 2476 2477

    >>> write(sample_buildout, 'buildout.cfg',
    ... """
    ... [buildout]
    ... directory = %(alt)s
    ... develop = %(recipes)s
2478
    ... parts =
2479 2480 2481 2482
    ... """ % dict(
    ...    alt=alt,
    ...    recipes=os.path.join(sample_buildout, 'recipes'),
    ...    ))
2483

2484
    >>> print_(system(buildout), end='')
2485 2486 2487 2488 2489
    Creating directory '/sample-alt/bin'.
    Creating directory '/sample-alt/parts'.
    Creating directory '/sample-alt/eggs'.
    Creating directory '/sample-alt/develop-eggs'.
    Develop: '/sample-buildout/recipes'
2490 2491 2492 2493

    >>> ls(alt)
    -  .installed.cfg
    d  bin
2494
    d  develop-eggs
2495 2496 2497
    d  eggs
    d  parts

2498
    >>> ls(alt, 'develop-eggs')
2499 2500
    -  recipes.egg-link

Jim Fulton's avatar
Jim Fulton committed
2501 2502 2503 2504 2505
Logging control
---------------

Three buildout options are used to control logging:

2506
log-level
Jim Fulton's avatar
Jim Fulton committed
2507 2508
   specifies the log level

2509
verbosity
Jim Fulton's avatar
Jim Fulton committed
2510 2511 2512 2513 2514 2515 2516 2517 2518 2519 2520 2521 2522 2523 2524
   adjusts the log level

log-format
   allows an alternate logging for mat to be specified

We've already seen the log level and verbosity.  Let's look at an example
of changing the format:

    >>> write(sample_buildout, 'buildout.cfg',
    ... """
    ... [buildout]
    ... develop = recipes
    ... parts =
    ... log-level = 25
    ... verbosity = 5
2525
    ... log-format = %(levelname)s %(message)s
Jim Fulton's avatar
Jim Fulton committed
2526
    ... """)
2527

Jim Fulton's avatar
Jim Fulton committed
2528
Here, we've changed the format to include the log-level name, rather
2529
than the logger name.
Jim Fulton's avatar
Jim Fulton committed
2530 2531 2532

We've also illustrated, with a contrived example, that the log level
can be a numeric value and that the verbosity can be specified in the
2533
configuration file.  Because the verbosity is subtracted from the log
Jim Fulton's avatar
Jim Fulton committed
2534 2535
level, we get a final log level of 20, which is the INFO level.

2536
    >>> print_(system(buildout), end='')
2537
    INFO Develop: '/sample-buildout/recipes'
2538

2539 2540 2541
Predefined buildout options
---------------------------

2542
Buildouts have a number of predefined options that recipes can use
2543 2544 2545 2546
and that users can override in their configuration files.  To see
these, we'll run a minimal buildout configuration with a debug logging
level.  One of the features of debug logging is that the configuration
database is shown.
2547

2548 2549 2550 2551 2552 2553
    >>> write(sample_buildout, 'buildout.cfg',
    ... """
    ... [buildout]
    ... parts =
    ... """)

2554
    >>> print_(system(buildout+' -vv'), end='') # doctest: +NORMALIZE_WHITESPACE
2555
    Installing 'zc.buildout', 'setuptools'.
2556
    We have a develop egg: zc.buildout 1.0.0.
2557 2558
    We have the best distribution that satisfies 'setuptools'.
    Picked: setuptools = 0.7
2559
    <BLANKLINE>
2560 2561
    Configuration data:
    [buildout]
2562 2563
    allow-hosts = *
    allow-picked-versions = true
2564 2565 2566 2567
    bin-directory = /sample-buildout/bin
    develop-eggs-directory = /sample-buildout/develop-eggs
    directory = /sample-buildout
    eggs-directory = /sample-buildout/eggs
2568 2569 2570
    executable = python
    find-links =
    install-from-cache = false
2571
    installed = /sample-buildout/.installed.cfg
2572
    log-format =
2573
    log-level = INFO
Jim Fulton's avatar
Jim Fulton committed
2574
    newest = true
2575
    offline = false
2576
    parts =
2577
    parts-directory = /sample-buildout/parts
Jim Fulton's avatar
Jim Fulton committed
2578
    prefer-final = true
2579
    python = buildout
2580
    show-picked-versions = false
2581
    socket-timeout =
2582
    update-versions-file =
2583
    use-dependency-links = true
2584
    verbosity = 20
2585 2586 2587 2588
    versions = versions
    [versions]
    zc.buildout = >=1.99
    zc.recipe.egg = >=1.99
2589
    <BLANKLINE>
2590

2591 2592 2593 2594
All of these options can be overridden by configuration files or by
command-line assignments.  We've discussed most of these options
already, but let's review them and touch on some we haven't discussed:

2595 2596 2597 2598 2599 2600 2601 2602 2603 2604 2605 2606 2607 2608 2609 2610 2611 2612 2613 2614 2615 2616 2617 2618 2619 2620 2621 2622 2623 2624 2625
allow-hosts
    On some environments the links visited by `zc.buildout` can be forbidden by
    paranoid firewalls. These URLs might be in the chain of links visited by
    `zc.buildout` as defined by buildout's `find-links` option, or as defined
    by various eggs in their `url`, `download_url`, `dependency_links` metadata.

    The fact that package_index works like a spider and might visit links and
    go to other locations makes this even harder.

    The `allow-hosts` option provides a way to prevent this, and
    works exactly like the one provided in `easy_install`.

    You can provide a list of allowed host, together with wildcards::

        [buildout]
        ...

        allow-hosts =
            *.python.org
            example.com

    All URLs that does not match these hosts will not be visited.

allow-picked-versions
    By default, the buildout will choose the best match for a given requirement
    if the requirement is not specified precisely (for instance, using the
    "versions" option.  This behavior corresponds to the
    "allow-picked-versions" being set to its default value, "true".  If
    "allow-picked-versions" is "false," instead of picking the best match,
    buildout will raise an error.  This helps enforce repeatability.

2626 2627 2628 2629 2630 2631 2632
bin-directory
   The directory path where scripts are written.  This can be a
   relative path, which is interpreted relative to the directory
   option.

develop-eggs-directory
   The directory path where development egg links are created for software
2633
   being created in the local project.  This can be a relative path,
2634 2635 2636 2637 2638 2639 2640 2641
   which is interpreted relative to the directory option.

directory
   The buildout directory.  This is the base for other buildout file
   and directory locations, when relative locations are used.

eggs-directory
   The directory path where downloaded eggs are put.  It is common to share
2642
   this directory across buildouts. Eggs in this directory should
2643 2644 2645
   *never* be modified.  This can be a relative path, which is
   interpreted relative to the directory option.

2646 2647 2648 2649 2650 2651 2652 2653 2654 2655 2656 2657 2658 2659 2660 2661 2662 2663 2664 2665 2666 2667 2668 2669 2670 2671 2672 2673 2674 2675 2676 2677 2678 2679 2680 2681 2682 2683 2684 2685 2686
find-links
    You can specify more locations to search for distributions using the
    `find-links` option. All locations specified will be searched for
    distributions along with the package index as described before.

    Locations can be urls::

      [buildout]
      ...
      find-links = http://download.zope.org/distribution/

    They can also be directories on disk::

      [buildout]
      ...
      find-links = /some/path

    Finally, they can also be direct paths to distributions::

      [buildout]
      ...
      find-links = /some/path/someegg-1.0.0-py2.3.egg

    Any number of locations can be specified in the `find-links` option::

      [buildout]
      ...
      find-links =
          http://download.zope.org/distribution/
          /some/otherpath
          /some/path/someegg-1.0.0-py2.3.egg

install-from-cache
    A download cache can be used as the basis of application source releases.
    In an application source release, we want to distribute an application that
    can be built without making any network accesses.  In this case, we
    distribute a buildout with download cache and tell the buildout to install
    from the download cache only, without making network accesses.  The
    buildout install-from-cache option can be used to signal that packages
    should be installed only from the download cache.

2687 2688 2689 2690 2691 2692 2693 2694 2695 2696 2697 2698 2699
installed
   The file path where information about the results of the previous
   buildout run is written.  This can be a relative path, which is
   interpreted relative to the directory option.  This file provides
   an inventory of installed parts with information needed to decide
   which if any parts need to be uninstalled.

log-format
   The format used for logging messages.

log-level
   The log level before verbosity adjustment

2700 2701 2702 2703 2704 2705 2706 2707 2708 2709 2710 2711 2712 2713 2714 2715 2716 2717
newest
    By default buildout and recipes will try to find the newest versions of
    distributions needed to satisfy requirements.  This can be very time
    consuming, especially when incrementally working on setting up a buildout
    or working on a recipe.  The buildout "newest" option can be used to to
    suppress this.  If the "newest" option is set to false, then new
    distributions won't be sought if an installed distribution meets
    requirements.  The "newest" option can also be set to false using the -N
    command-line option.  See also the "offline" option.

offline
    The "offline" option goes a bit further than the "newest" option.  If the
    buildout "offline" option is given a value of "true", the buildout and
    recipes that are aware of the option will avoid doing network access.  This
    is handy when running the buildout when not connected to the internet.  It
    also makes buildouts run much faster. This option is typically set using
    the buildout -o option.

2718
parts
2719
   A white space separated list of parts to be installed.
2720 2721 2722 2723

parts-directory
   A working directory that parts can used to store data.

2724 2725 2726 2727 2728 2729 2730 2731 2732 2733 2734 2735 2736 2737 2738 2739 2740 2741 2742 2743 2744 2745 2746 2747 2748 2749 2750 2751 2752 2753 2754 2755 2756
prefer-final
    Currently, when searching for new releases, the newest available
    release is used.  This isn't usually ideal, as you may get a
    development release or alpha releases not ready to be widely used.
    You can request that final releases be preferred using the prefer
    final option in the buildout section::

      [buildout]
      ...
      prefer-final = true

    When the prefer-final option is set to true, then when searching for
    new releases, final releases are preferred.  If there are final
    releases that satisfy distribution requirements, then those releases
    are used even if newer non-final releases are available.  The buildout
    prefer-final option can be used to override this behavior.

    In buildout version 2, final releases will be preferred by default.
    You will then need to use a false value for prefer-final to get the
    newest releases.

use-dependency-links
    By default buildout will obey the setuptools dependency_links metadata
    when it looks for dependencies. This behavior can be controlled with
    the use-dependency-links buildout option::

      [buildout]
      ...
      use-dependency-links = false

    The option defaults to true. If you set it to false, then dependency
    links are only looked for in the locations specified by find-links.

2757 2758 2759 2760 2761
verbosity
   A log-level adjustment.  Typically, this is set via the -q and -v
   command-line options.


2762 2763
Creating new buildouts and bootstrapping
----------------------------------------
Jim Fulton's avatar
Jim Fulton committed
2764 2765

If zc.buildout is installed, you can use it to create a new buildout
2766
with it's own local copies of zc.buildout and setuptools and with
2767
local buildout scripts.
Jim Fulton's avatar
Jim Fulton committed
2768

2769
    >>> sample_bootstrapped = tmpdir('sample-bootstrapped')
2770

2771
    >>> print_(system(buildout
2772
    ...              +' -c'+os.path.join(sample_bootstrapped, 'setup.cfg')
2773
    ...              +' init'), end='')
2774 2775 2776 2777 2778 2779
    Creating '/sample-bootstrapped/setup.cfg'.
    Creating directory '/sample-bootstrapped/bin'.
    Creating directory '/sample-bootstrapped/parts'.
    Creating directory '/sample-bootstrapped/eggs'.
    Creating directory '/sample-bootstrapped/develop-eggs'.
    Generated script '/sample-bootstrapped/bin/buildout'.
2780

2781 2782 2783 2784 2785 2786 2787 2788 2789
Note that a basic setup.cfg was created for us.  This is because we
provided an 'init' argument.  By default, the generated
``setup.cfg`` is as minimal as it could be:

    >>> cat(sample_bootstrapped, 'setup.cfg')
    [buildout]
    parts =

We also get other buildout artifacts:
Jim Fulton's avatar
Jim Fulton committed
2790 2791 2792 2793 2794 2795 2796 2797 2798 2799 2800

    >>> ls(sample_bootstrapped)
    d  bin
    d  develop-eggs
    d  eggs
    d  parts
    -  setup.cfg

    >>> ls(sample_bootstrapped, 'bin')
    -  buildout

2801 2802
    >>> _ = (ls(sample_bootstrapped, 'eggs'),
    ...      ls(sample_bootstrapped, 'develop-eggs'))
2803
    -  setuptools-0.7-py2.3.egg
Jim Fulton's avatar
Jim Fulton committed
2804
    -  zc.buildout-1.0-py2.3.egg
2805

2806
(We list both the eggs and develop-eggs directories because the
2807
buildout or setuptools egg could be installed in the develop-eggs
2808
directory if the original buildout had develop eggs for either
2809
buildout or setuptools.)
2810

Jim Fulton's avatar
Jim Fulton committed
2811
Note that the buildout script was installed but not run.  To run
2812
the buildout, we'd have to run the installed buildout script.
2813

2814 2815 2816 2817
If we have an existing buildout that already has a buildout.cfg, we'll
normally use the bootstrap command instead of init.  It will complain
if there isn't a configuration file:

2818
    >>> sample_bootstrapped2 = tmpdir('sample-bootstrapped2')
2819

2820
    >>> print_(system(buildout
2821
    ...              +' -c'+os.path.join(sample_bootstrapped2, 'setup.cfg')
2822
    ...              +' bootstrap'), end='')
2823 2824 2825
    While:
      Initializing.
    Error: Couldn't open /sample-bootstrapped2/setup.cfg
2826

2827 2828 2829 2830 2831
    >>> write(sample_bootstrapped2, 'setup.cfg',
    ... """
    ... [buildout]
    ... parts =
    ... """)
2832

2833
    >>> print_(system(buildout
2834
    ...              +' -c'+os.path.join(sample_bootstrapped2, 'setup.cfg')
2835
    ...              +' bootstrap'), end='')
2836 2837 2838 2839 2840
    Creating directory '/sample-bootstrapped2/bin'.
    Creating directory '/sample-bootstrapped2/parts'.
    Creating directory '/sample-bootstrapped2/eggs'.
    Creating directory '/sample-bootstrapped2/develop-eggs'.
    Generated script '/sample-bootstrapped2/bin/buildout'.
2841

2842 2843 2844 2845
Similarly, if there is a configuration file and we use the init
command, we'll get an error that the configuration file already
exists:

2846
    >>> print_(system(buildout
2847
    ...              +' -c'+os.path.join(sample_bootstrapped, 'setup.cfg')
2848
    ...              +' init'), end='')
2849 2850 2851 2852 2853 2854 2855 2856 2857 2858 2859 2860
    While:
      Initializing.
    Error: '/sample-bootstrapped/setup.cfg' already exists.

Initial eggs
------------

When using the ``init`` command, you can specify distribution requirements
or paths to use:

    >>> cd(sample_bootstrapped)
    >>> remove('setup.cfg')
2861
    >>> print_(system(buildout + ' -csetup.cfg init demo other ./src'), end='')
2862
    Creating '/sample-bootstrapped/setup.cfg'.
2863 2864
    Getting distribution for 'zc.recipe.egg>=2.0.0a3'.
    Got zc.recipe.egg
2865 2866
    Installing py.
    Getting distribution for 'demo'.
Jim Fulton's avatar
Jim Fulton committed
2867
    Got demo 0.3.
2868 2869 2870
    Getting distribution for 'other'.
    Got other 1.0.
    Getting distribution for 'demoneeded'.
Jim Fulton's avatar
Jim Fulton committed
2871
    Got demoneeded 1.1.
2872 2873 2874 2875 2876 2877 2878 2879 2880 2881 2882 2883 2884 2885 2886 2887 2888 2889 2890 2891
    Generated script '/sample-bootstrapped/bin/demo'.
    Generated script '/sample-bootstrapped/bin/distutilsscript'.
    Generated interpreter '/sample-bootstrapped/bin/py'.

This causes a ``py`` part to be included that sets up a custom python
interpreter with the given requirements or paths:

    >>> cat('setup.cfg')
    [buildout]
    parts = py
    <BLANKLINE>
    [py]
    recipe = zc.recipe.egg
    interpreter = py
    eggs =
      demo
      other
    extra-paths =
      ./src

pombredanne's avatar
pombredanne committed
2892
Passing requirements or paths causes the the buildout to be run as part
2893 2894 2895 2896
of initialization.  In the example above, we got a number of
distributions installed and 2 scripts generated.  The first, ``demo``,
was defined by the ``demo`` project. The second, ``py`` was defined by
the generated configuration.  It's a "custom interpreter" that behaves
pombredanne's avatar
pombredanne committed
2897
like a standard Python interpreter, except that includes the specified
2898 2899 2900 2901 2902 2903 2904 2905 2906 2907 2908 2909 2910 2911 2912 2913 2914 2915 2916 2917 2918
eggs and extra paths in it's Python path.

We specified a source directory that didn't exist. Buildout created it
for us:

    >>> ls('.')
    -  .installed.cfg
    d  bin
    d  develop-eggs
    d  eggs
    d  parts
    -  setup.cfg
    d  src

    >>> uncd()

.. Make sure it works if the dir is already there:

    >>> cd(sample_bootstrapped)
    >>> _ = system(buildout + ' -csetup.cfg buildout:parts=')
    >>> remove('setup.cfg')
2919
    >>> print_(system(buildout + ' -csetup.cfg init demo other ./src'), end='')
2920 2921 2922 2923 2924 2925 2926 2927 2928 2929
    Creating '/sample-bootstrapped/setup.cfg'.
    Installing py.
    Generated script '/sample-bootstrapped/bin/demo'.
    Generated script '/sample-bootstrapped/bin/distutilsscript'.
    Generated interpreter '/sample-bootstrapped/bin/py'.

.. cleanup

    >>> _ = system(buildout + ' -csetup.cfg buildout:parts=')
    >>> uncd()
2930

2931 2932 2933 2934
Finding distributions
---------------------

By default, buildout searches the Python Package Index when looking
2935 2936
for distributions. You can, instead, specify your own index to search
using the `index` option::
2937

2938 2939 2940 2941 2942 2943 2944 2945 2946 2947 2948
  [buildout]
  ...
  index = http://index.example.com/

This index, or the default of http://pypi.python.org/simple/ if no
index is specified, will always be searched for distributions unless
running buildout with options that prevent searching for
distributions. The latest version of the distribution that meets the
requirements of the buildout will always be used.

You can also specify more locations to search for distributions using
2949
the `find-links` option. See its description above.
2950

Jim Fulton's avatar
Jim Fulton committed
2951 2952 2953
Controlling the installation database
-------------------------------------

2954
The buildout installed option is used to specify the file used to save
Jim Fulton's avatar
Jim Fulton committed
2955
information on installed parts.  This option is initialized to
2956
".installed.cfg", but it can be overridden in the configuration file
Jim Fulton's avatar
Jim Fulton committed
2957 2958
or on the command line:

2959
    >>> write('buildout.cfg',
2960 2961 2962 2963 2964 2965 2966 2967 2968
    ... """
    ... [buildout]
    ... develop = recipes
    ... parts = debug
    ...
    ... [debug]
    ... recipe = recipes:debug
    ... """)

2969
    >>> print_(system(buildout+' buildout:installed=inst.cfg'), end='')
2970 2971
    Develop: '/sample-buildout/recipes'
    Installing debug.
2972
    recipe recipes:debug
Jim Fulton's avatar
Jim Fulton committed
2973 2974 2975 2976 2977 2978 2979

    >>> ls(sample_buildout)
    -  b1.cfg
    -  b2.cfg
    -  base.cfg
    d  bin
    -  buildout.cfg
2980
    d  demo
Jim Fulton's avatar
Jim Fulton committed
2981 2982 2983 2984 2985 2986 2987
    d  develop-eggs
    d  eggs
    -  inst.cfg
    d  parts
    d  recipes

The installation database can be disabled by supplying an empty
2988
buildout installed option:
Jim Fulton's avatar
Jim Fulton committed
2989 2990

    >>> os.remove('inst.cfg')
2991
    >>> print_(system(buildout+' buildout:installed='), end='')
2992 2993
    Develop: '/sample-buildout/recipes'
    Installing debug.
2994
    recipe recipes:debug
Jim Fulton's avatar
Jim Fulton committed
2995 2996 2997 2998 2999 3000 3001

    >>> ls(sample_buildout)
    -  b1.cfg
    -  b2.cfg
    -  base.cfg
    d  bin
    -  buildout.cfg
3002
    d  demo
Jim Fulton's avatar
Jim Fulton committed
3003 3004 3005 3006 3007 3008
    d  develop-eggs
    d  eggs
    d  parts
    d  recipes


3009
Note that there will be no installation database if there are no parts:
3010

3011
    >>> write('buildout.cfg',
3012 3013 3014 3015 3016
    ... """
    ... [buildout]
    ... parts =
    ... """)

3017
    >>> print_(system(buildout+' buildout:installed=inst.cfg'), end='')
3018 3019 3020 3021 3022 3023 3024

    >>> ls(sample_buildout)
    -  b1.cfg
    -  b2.cfg
    -  base.cfg
    d  bin
    -  buildout.cfg
3025
    d  demo
3026 3027 3028 3029 3030
    d  develop-eggs
    d  eggs
    d  parts
    d  recipes

3031 3032 3033
Extensions
----------

3034
A feature allows code to be loaded and run after
Jim Fulton's avatar
Jim Fulton committed
3035
configuration files have been read but before the buildout has begun
3036 3037 3038 3039 3040
any processing.  The intent is to allow special plugins such as
urllib2 request handlers to be loaded.

To load an extension, we use the extensions option and list one or
more distribution requirements, on separate lines.  The distributions
3041 3042 3043 3044
named will be loaded and any ``zc.buildout.extension`` entry points found
will be called with the buildout as an argument.  When buildout
finishes processing, any ``zc.buildout.unloadextension`` entry points
found will be called with the buildout as an argument.
3045

Jim Fulton's avatar
Jim Fulton committed
3046
Let's create a sample extension in our sample buildout created in the
3047 3048 3049 3050
previous section:

    >>> mkdir(sample_bootstrapped, 'demo')

3051
    >>> write(sample_bootstrapped, 'demo', 'demo.py',
3052
    ... """
3053
    ... import sys
3054
    ... def ext(buildout):
3055
    ...     sys.stdout.write('%s %s\\n' % ('ext', sorted(buildout)))
3056
    ... def unload(buildout):
3057
    ...     sys.stdout.write('%s %s\\n' % ('unload', sorted(buildout)))
3058 3059 3060 3061 3062
    ... """)

    >>> write(sample_bootstrapped, 'demo', 'setup.py',
    ... """
    ... from setuptools import setup
3063
    ...
3064 3065
    ... setup(
    ...     name = "demo",
3066 3067 3068 3069
    ...     entry_points = {
    ...        'zc.buildout.extension': ['ext = demo:ext'],
    ...        'zc.buildout.unloadextension': ['ext = demo:unload'],
    ...        },
3070 3071 3072 3073 3074 3075 3076 3077 3078 3079 3080 3081 3082 3083 3084 3085 3086
    ...     )
    ... """)

Our extension just prints out the word 'demo', and lists the sections
found in the buildout passed to it.

We'll update our buildout.cfg to list the demo directory as a develop
egg to be built:

    >>> write(sample_bootstrapped, 'buildout.cfg',
    ... """
    ... [buildout]
    ... develop = demo
    ... parts =
    ... """)

    >>> os.chdir(sample_bootstrapped)
3087 3088
    >>> print_(system(os.path.join(sample_bootstrapped, 'bin', 'buildout')),
    ...        end='')
3089
    Develop: '/sample-bootstrapped/demo'
3090

3091
Now we can add the extensions option.  We were a bit tricky and ran
3092 3093 3094 3095 3096 3097 3098 3099 3100 3101 3102 3103 3104
the buildout once with the demo develop egg defined but without the
extension option.  This is because extensions are loaded before the
buildout creates develop eggs. We needed to use a separate buildout
run to create the develop egg.  Normally, when eggs are loaded from
the network, we wouldn't need to do anything special.

    >>> write(sample_bootstrapped, 'buildout.cfg',
    ... """
    ... [buildout]
    ... develop = demo
    ... extensions = demo
    ... parts =
    ... """)
3105

Jim Fulton's avatar
Jim Fulton committed
3106
We see that our extension is loaded and executed:
3107

3108 3109
    >>> print_(system(os.path.join(sample_bootstrapped, 'bin', 'buildout')),
    ...        end='')
3110
    ext ['buildout', 'versions']
3111
    Develop: '/sample-bootstrapped/demo'
3112
    unload ['buildout', 'versions']