buildout.txt 66.3 KB
Newer Older
Jim Fulton's avatar
Jim Fulton committed
1 2 3 4 5 6 7 8 9 10 11
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
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 21 22
2. Use the buildout bootstrap script to create a buildout that
   includes both the setuptools and zc.buildout eggs.  This allows you
   to use the buildout software without modifying a Python install.
23 24
   The buildout script is installed into your buildout local scripts
   area.
25

Jim Fulton's avatar
Jim Fulton committed
26 27 28 29
3. Use a buildoput command from an already installed buildout to 
   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 35 36 37
project from the repository and run the bootstrap script which
installs setuptools and zc.buildout into the checkout as well as any
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')
Jim Fulton's avatar
Jim Fulton committed
56 57
    -  setuptools-0.6-py2.4.egg
    -  zc.buildout-1.0-py2.4.egg
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 100 101 102 103 104 105 106
Python Package Index, or they can be developed as part of a project
using a "develop" egg.  

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 116 117

    >>> mkdir(sample_buildout, 'recipes')

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

    >>> write(sample_buildout, 'recipes', 'mkdir.py', 
    ... """
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 133
    ...         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')
    ...             
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
    ... """)

Jim Fulton's avatar
Jim Fulton committed
146
Currently, recipes must define 3 methods [#future_recipe_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 188
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
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 210 211 212
installed as a develop egg. The minimum information we need to specify
[#packaging_info]_ is a name.  For recipes, we also need to define the
names of the recipe classes as entry points.  Packaging information is
provided via a setup.py script:
213 214 215 216 217 218 219 220 221 222 223

    >>> write(sample_buildout, 'recipes', 'setup.py',
    ... """
    ... from setuptools import setup
    ... 
    ... 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 231
We also need a README.txt for our recipes to avoid an annoying warning
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 273 274
    recipe = recipes:mkdir
    path = mystuff    

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 287
    >>> buildout = os.path.join(sample_buildout, 'bin', 'buildout')
    >>> print system(buildout),
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 320
In addition, the path option includes the actual destination
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
    ... """)

Jim Fulton's avatar
Jim Fulton committed
336
    >>> print system(buildout),
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 356
If any of the files or directories created by a recipe are removed,
the part will be reinstalled:

    >>> rmdir(sample_buildout, 'mydata')
    >>> print system(buildout),
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 387


    >>> 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.

    >>> print system(buildout),
388
    Develop: '/sample-buildout/recipes'
389
    data-dir: Cannot create /xxx/mydata. /xxx is not a directory.
390
    While:
391 392 393
      Installing.
      Getting section data-dir.
      Initializing part data-dir.
394 395 396
    Error: Invalid Path


397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452
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:

    >>> write(sample_buildout, 'recipes', 'mkdir.py', 
    ... """
    ... 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 = []
    ...         for path in options['path'].split(): 
    ...             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()
    ...         for path in paths: 
    ...             logging.getLogger(self.name).info(
    ...                 'Creating directory %s', os.path.basename(path))
    ...             os.mkdir(path)
    ...         return paths
    ...
    ...     def update(self):
    ...         pass
    ... """)

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
    ... """)

    >>> print system(buildout),
453 454 455
    Develop: '/sample-buildout/recipes'
    Uninstalling data-dir.
    Installing data-dir.
456 457 458
    data-dir: Creating directory foo
    data-dir: Creating directory bin
    While:
459
      Installing data-dir.
460 461 462 463 464 465 466
    <BLANKLINE>
    An internal error occured due to a bug in either zc.buildout or in a
    recipe being used:
    <BLANKLINE>
    OSError:
    [Errno 17] File exists: '/sample-buildout/bin'

467
We meant to create a directory bins, but typed bin.  Now foo was
468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486
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
    ... """)

    >>> print system(buildout),
487 488
    Develop: '/sample-buildout/recipes'
    Installing data-dir.
489 490
    data-dir: Creating directory foo
    While:
491
      Installing data-dir.
492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562
    <BLANKLINE>
    An internal error occured due to a bug in either zc.buildout or in a
    recipe being used:
    <BLANKLINE>
    OSError:
    [Errno 17] File exists: '/sample-buildout/foo'

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

    >>> remove('foo')

Let's fix the recipe:

    >>> write(sample_buildout, 'recipes', 'mkdir.py', 
    ... """
    ... 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 = []
    ...         for path in options['path'].split(): 
    ...             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:
    ...             for path in paths: 
    ...                 logging.getLogger(self.name).info(
    ...                     'Creating directory %s', os.path.basename(path))
    ...                 os.mkdir(path)
    ...                 created.append(path)
    ...         except:
    ...             for d in created:
    ...                 os.rmdir(d)
    ...             raise
    ...
    ...         return paths
    ...
    ...     def update(self):
    ...         pass
    ... """)

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:

    >>> print system(buildout),
563 564
    Develop: '/sample-buildout/recipes'
    Installing data-dir.
565 566 567
    data-dir: Creating directory foo
    data-dir: Creating directory bin
    While:
568
      Installing data-dir.
569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584
    <BLANKLINE>
    An internal error occured due to a bug in either zc.buildout or in a
    recipe being used:
    <BLANKLINE>
    OSError:
    [Errno 17] File exists: '/sample-buildout/bin'

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
585
to record files as they are created.  If the install or update method
586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635
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:

    >>> write(sample_buildout, 'recipes', 'mkdir.py', 
    ... """
    ... 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 = []
    ...         for path in options['path'].split(): 
    ...             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()
    ...         for path in paths: 
    ...             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
    ... """)

..

    >>> remove(sample_buildout, 'recipes', 'mkdir.pyc')

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
636
directories will be created:
637 638

    >>> print system(buildout),
639 640
    Develop: '/sample-buildout/recipes'
    Installing data-dir.
641 642 643
    data-dir: Creating directory foo
    data-dir: Creating directory bin
    While:
644
      Installing data-dir.
645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668
    <BLANKLINE>
    An internal error occured due to a bug in either zc.buildout or in a
    recipe being used:
    <BLANKLINE>
    OSError:
    [Errno 17] File exists: '/sample-buildout/bin'

    >>> 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
    ... """)

    >>> print system(buildout),
669 670
    Develop: '/sample-buildout/recipes'
    Installing data-dir.
671 672 673 674 675 676 677 678
    data-dir: Creating directory foo
    data-dir: Creating directory bins

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

679 680 681 682 683 684 685 686 687
Configuration file syntax
-------------------------

As mentioned earlier, buildout configuration files use the format
defined by the Python ConfigParser module with extensions.  The
extensions are:

- option names are case sensitive

688
- option values can use a substitution syntax, described below, to
689 690 691 692 693 694 695 696 697 698 699 700 701 702
  refer to option values in specific sections.

The ConfigParser syntax is very flexible.  Section names can contain
any characters other than newlines and right square braces ("]").
Option names can contain any characters other than newlines, colons,
and equal signs, can not start with a space, and don't include
trailing spaces.

It is likely that, in the future, some characters will be given
special buildout-defined meanings.  This is already true of the
characters ":", "$", "%", "(", and ")".  For now, it is a good idea to
keep section and option names simple, sticking to alphanumeric
characters, hyphens, and periods.

703 704 705
Variable substitutions
----------------------

706 707
Buildout configuration files support variable substitution.
To illustrate this, we'll create an debug recipe to
708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723
allow us to see interactions with the buildout:

    >>> write(sample_buildout, 'recipes', 'debug.py', 
    ... """
    ... class Debug:
    ...
    ...     def __init__(self, buildout, name, options):
    ...         self.buildout = buildout
    ...         self.name = name
    ...         self.options = options
    ...
    ...     def install(self):
    ...         items = self.options.items()
    ...         items.sort()
    ...         for option, value in items:
    ...             print option, value
Jim Fulton's avatar
Jim Fulton committed
724 725 726
    ...         return ()
    ...
    ...     update = install
727 728
    ... """)

729 730 731
This recipe doesn't actually create anything. The install method
doesn't return anything, because it didn't create any files or
directories.
732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747

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
748 749
edit.  In particular, entry points are now defined as a configuration
string, rather than a dictionary.
750 751 752 753 754 755 756 757

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

    >>> write(sample_buildout, 'buildout.cfg',
    ... """
    ... [buildout]
    ... develop = recipes
758
    ... parts = data-dir debug
Jim Fulton's avatar
Jim Fulton committed
759
    ... log-level = INFO
760 761 762
    ...
    ... [debug]
    ... recipe = recipes:debug
763
    ... File 1 = ${data-dir:path}/file
764
    ... File 2 = ${debug:File 1}/log
765
    ...
766
    ... [data-dir]
767 768 769 770 771 772
    ... recipe = recipes:mkdir
    ... path = mydata
    ... """)

In this example, we've used ConfigParser substitutions for file2 and
file3.  This type of substitution uses Python string format syntax.
Jim Fulton's avatar
Jim Fulton committed
773 774 775 776
Valid names are options in the same section and options defined in the
DEFAULT section.  

We used a string-template substitution for file1.  This type of
777
substitution uses the string.Template syntax.  Names substituted are
Jim Fulton's avatar
Jim Fulton committed
778 779
qualified option names, consisting of a section name and option name
joined by a colon.
780 781 782 783

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

Jim Fulton's avatar
Jim Fulton committed
784
    >>> print system(buildout),
785 786 787
    Develop: '/sample-buildout/recipes'
    Uninstalling data-dir.
    Installing data-dir.
788
    data-dir: Creating directory mydata
789
    Installing debug.
790 791
    File 1 /sample-buildout/mydata/file
    File 2 /sample-buildout/mydata/file/log
792 793
    recipe recipes:debug

794 795 796
Note that the substitution of the data-dir path option reflects the
update to the option performed by the mkdir recipe.

797 798 799 800 801 802
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:

Jim Fulton's avatar
Jim Fulton committed
803
    >>> print system(buildout),
804 805 806
    Develop: '/sample-buildout/recipes'
    Updating data-dir.
    Updating debug.
807 808
    File 1 /sample-buildout/mydata/file
    File 2 /sample-buildout/mydata/file/log
809 810 811
    recipe recipes:debug

We can see that mydata was not recreated.
812

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

816 817 818 819
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.

820

821 822 823
Automatic part selection and ordering
-------------------------------------

824
When a section with a recipe is referred to, either through variable
825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849
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
    ... File 1 = ${data-dir:path}/file
    ... File 2 = ${debug:File 1}/log
    ...
    ... [data-dir]
    ... recipe = recipes:mkdir
    ... path = mydata
    ... """)


It will still be treated as a part:

    >>> print system(buildout),
850 851 852
    Develop: '/sample-buildout/recipes'
    Updating data-dir.
    Updating debug.
853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883
    File 1 /sample-buildout/mydata/file
    File 2 /sample-buildout/mydata/file/log
    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
    ... File 1 = ${data-dir:path}/file
    ... File 2 = ${debug:File 1}/log
    ...
    ... [data-dir]
    ... recipe = recipes:mkdir
    ... path = mydata
    ... """)

884

885
It will still be treated as a part:
886

887
    >>> print system(buildout),
888 889 890
    Develop: '/sample-buildout/recipes'
    Updating data-dir.
    Updating debug.
891 892 893
    File 1 /sample-buildout/mydata/file
    File 2 /sample-buildout/mydata/file/log
    recipe recipes:debug
894

895 896 897 898 899 900 901 902 903 904 905 906 907 908 909
    >>> cat('.installed.cfg') # doctest: +ELLIPSIS
    [buildout]
    installed_develop_eggs = /sample-buildout/develop-eggs/recipes.egg-link
    parts = data-dir debug
    ...

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
910 911 912 913 914 915 916 917 918 919
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]
920
    ... op = buildout
921 922 923 924 925 926 927 928 929 930 931 932 933
    ... """)

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

Jim Fulton's avatar
Jim Fulton committed
934
    >>> print system(buildout),
935 936 937 938
    Develop: '/sample-buildout/recipes'
    Uninstalling debug.
    Uninstalling data-dir.
    Installing debug.
939
    op buildout
940 941 942 943 944 945 946
    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
customization. 

Jim Fulton's avatar
Jim Fulton committed
947
Here is a more elaborate example. 
948

949
    >>> other = tmpdir('other')
950 951 952 953

    >>> write(sample_buildout, 'buildout.cfg',
    ... """
    ... [buildout]
954
    ... extends = b1.cfg b2.cfg %(b3)s
955 956
    ...
    ... [debug]
957 958
    ... op = buildout
    ... """ % dict(b3=os.path.join(other, 'b3.cfg')))
959 960 961 962 963 964 965

    >>> write(sample_buildout, 'b1.cfg',
    ... """
    ... [buildout]
    ... extends = base.cfg
    ...
    ... [debug]
966 967
    ... op1 = b1 1
    ... op2 = b1 2
968 969 970 971 972 973 974 975
    ... """)

    >>> write(sample_buildout, 'b2.cfg',
    ... """
    ... [buildout]
    ... extends = base.cfg
    ...
    ... [debug]
976 977
    ... op2 = b2 2
    ... op3 = b2 3
978 979
    ... """)

980
    >>> write(other, 'b3.cfg',
981 982
    ... """
    ... [buildout]
983
    ... extends = b3base.cfg
984 985
    ...
    ... [debug]
986
    ... op4 = b3 4
987 988
    ... """)

989
    >>> write(other, 'b3base.cfg',
990 991
    ... """
    ... [debug]
992
    ... op5 = b3base 5
993 994
    ... """)

995
    >>> write(sample_buildout, 'base.cfg',
996 997
    ... """
    ... [buildout]
998 999
    ... develop = recipes
    ... parts = debug
1000 1001
    ...
    ... [debug]
1002 1003
    ... recipe = recipes:debug
    ... name = base
1004 1005
    ... """)

Jim Fulton's avatar
Jim Fulton committed
1006
    >>> print system(buildout),
1007 1008 1009
    Develop: '/sample-buildout/recipes'
    Uninstalling debug.
    Installing debug.
1010
    name base
1011
    op buildout
1012 1013
    op1 b1 1
    op2 b2 2
1014
    op3 b2 3
1015 1016
    op4 b3 4
    op5 b3base 5
1017 1018 1019 1020
    recipe recipes:debug

There are several things to note about this example:

1021
- We can name multiple files in an extends option.
1022 1023 1024

- We can reference files recursively.

1025 1026
- Relative file names in extended options are interpreted relative to
  the directory containing the referencing configuration file.
1027

1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068
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
    ... 
    ... [debug]
    ... op2 = r2 2
    ... op3 = r2 3
    ... """)

    >>> server_url = start_server(server_data)

    >>> write('client.cfg', 
    ... """
    ... [buildout]
    ... develop = recipes
    ... parts = debug
    ... extends = %(url)s/r2.cfg
    ...
    ... [debug]
    ... recipe = recipes:debug
    ... name = base
    ... """ % dict(url=server_url))


    >>> print system(buildout+ ' -c client.cfg'),
1069 1070 1071
    Develop: '/sample-buildout/recipes'
    Uninstalling debug.
    Installing debug.
1072 1073 1074 1075 1076 1077 1078
    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
1079
downloaded, itself referred to a file on the server using a relative
1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099
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
buildout.  

    >>> os.remove('client.cfg')
    >>> write(server_data, 'remote.cfg', 
    ... """
    ... [buildout]
    ... develop = recipes
    ... parts = debug
    ... extends = r2.cfg
    ...
    ... [debug]
    ... recipe = recipes:debug
    ... name = remote
    ... """)

    >>> print system(buildout + ' -c ' + server_url + '/remote.cfg'),
1100
    While:
1101
      Initializing.
1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112
    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:

    >>> print system(buildout
    ...              + ' -c ' + server_url + '/remote.cfg'
    ...              + ' buildout:directory=' + sample_buildout
    ...              ),
1113 1114 1115
    Develop: '/sample-buildout/recipes'
    Uninstalling debug.
    Installing debug.
1116 1117 1118 1119 1120 1121
    name remote
    op1 r1 1
    op2 r2 2
    op3 r2 3
    recipe recipes:debug

1122 1123 1124
User defaults
-------------

Fred Drake's avatar
Fred Drake committed
1125
If the file $HOME/.buildout/default.cfg, exists, it is read before
1126
reading the configuration file.  ($HOME is the value of the HOME
1127
environment variable. The '/' is replaced by the operating system file
1128 1129
delimiter.)

1130
    >>> old_home = os.environ['HOME']
1131
    >>> home = tmpdir('home')
1132 1133 1134 1135 1136 1137 1138 1139 1140
    >>> mkdir(home, '.buildout')
    >>> write(home, '.buildout', 'default.cfg',
    ... """
    ... [debug]
    ... op1 = 1
    ... op7 = 7
    ... """)

    >>> os.environ['HOME'] = home
Jim Fulton's avatar
Jim Fulton committed
1141
    >>> print system(buildout),
1142 1143 1144
    Develop: '/sample-buildout/recipes'
    Uninstalling debug.
    Installing debug.
1145
    name base
1146
    op buildout
1147 1148
    op1 b1 1
    op2 b2 2
1149
    op3 b2 3
1150 1151
    op4 b3 4
    op5 b3base 5
1152 1153 1154
    op7 7
    recipe recipes:debug

Jim Fulton's avatar
Jim Fulton committed
1155 1156 1157 1158
A buildout command-line argument, -U, can be used to suppress reading
user defaults:

    >>> print system(buildout + ' -U'),
1159 1160 1161
    Develop: '/sample-buildout/recipes'
    Uninstalling debug.
    Installing debug.
Jim Fulton's avatar
Jim Fulton committed
1162 1163 1164 1165 1166 1167 1168 1169 1170
    name base
    op buildout
    op1 b1 1
    op2 b2 2
    op3 b2 3
    op4 b3 4
    op5 b3base 5
    recipe recipes:debug

1171
    >>> os.environ['HOME'] = old_home
1172

1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187
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
    ... """)

    >>> print system(buildout),
1188 1189 1190
    name base
    op1 b1 1
    op2 b2 2
1191 1192 1193
    op3 b2 3
    recipe recipes:debug

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
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.

    >>> write(sample_buildout, 'recipes', 'service.py', 
    ... """
    ... class Service:
    ...
    ...     def __init__(self, buildout, name, options):
    ...         self.buildout = buildout
    ...         self.name = name
    ...         self.options = options
    ...
    ...     def install(self):
    ...         print "chkconfig --add %s" % self.options['script']         
    ...         return ()
    ...
    ...     def update(self):
    ...         pass
    ...
    ...
    ... def uninstall_service(name, options):
    ...     print "chkconfig --del %s" % options['script']
    ... """)

1241 1242 1243 1244
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.
1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256

    >>> 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]
1257
    ... service = service:uninstall_service
1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277
    ... ''')
    ... 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

    >>> print system(buildout)
1278 1279 1280
    Develop: '/sample-buildout/recipes'
    Uninstalling debug.
    Installing service.
1281 1282 1283
    chkconfig --add /path/to/script
    <BLANKLINE>

1284
The service has been installed. If the buildout is run again with no
1285
changes, the service shouldn't be changed.
1286 1287

    >>> print system(buildout)
1288 1289
    Develop: '/sample-buildout/recipes'
    Updating service.
1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306
    <BLANKLINE>

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
    ... """)

    >>> print system(buildout)
1307 1308 1309
    Develop: '/sample-buildout/recipes'
    Uninstalling service.
    Running uninstall recipe.
1310
    chkconfig --del /path/to/script
1311
    Installing service.
1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327
    chkconfig --add /path/to/a/different/script
    <BLANKLINE>

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

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

    >>> print system(buildout)
1328 1329 1330
    Develop: '/sample-buildout/recipes'
    Uninstalling service.
    Running uninstall recipe.
1331
    chkconfig --del /path/to/a/different/script
1332
    Installing debug.
1333 1334 1335 1336 1337 1338 1339 1340 1341 1342
    recipe recipes:debug
    <BLANKLINE>

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
1343 1344
a directory before it is deleted. It is designed to work with the
mkdir recipe introduced earlier.
1345 1346 1347 1348 1349 1350
 
    >>> write(sample_buildout, 'recipes', 'backup.py', 
    ... """
    ... import os
    ... def backup_directory(name, options):
    ...     path = options['path']
1351
    ...     size = len(os.listdir(path))
1352 1353 1354
    ...     print "backing up directory %s of size %s" % (path, size) 
    ... """)

1355 1356 1357
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.
1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370

    >>> 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
1371
    ... mkdir = backup:backup_directory
1372 1373 1374 1375
    ... ''')
    ... setup(name="recipes", entry_points=entry_points)
    ... """)

1376
Now we can use it with a mkdir part.
1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394

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

Run the buildout to install the part.

    >>> print system(buildout)
1395 1396 1397
    Develop: '/sample-buildout/recipes'
    Uninstalling debug.
    Installing dir.
1398
    dir: Creating directory my_directory
1399
    Installing debug.
1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418
    recipe recipes:debug
    <BLANKLINE>

Now we remove the part from the configuration file.

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

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

    >>> print system(buildout)
1419 1420 1421
    Develop: '/sample-buildout/recipes'
    Uninstalling dir.
    Running uninstall recipe.
1422
    backing up directory /sample-buildout/my_directory of size 0
1423
    Updating debug.
1424 1425 1426
    recipe recipes:debug
    <BLANKLINE>

1427
Now we will return the registration to normal for the benefit of the
1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441
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)
    ... """)

1442

1443 1444 1445 1446 1447 1448
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
1449
  buildout [options and assignments] [command [command arguments]]
1450

Jim Fulton's avatar
Jim Fulton committed
1451
The following options are supported:
1452

Jim Fulton's avatar
Jim Fulton committed
1453 1454 1455
-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
1456

Jim Fulton's avatar
Jim Fulton committed
1457 1458 1459
-c filename
    The -c option can be used to specify a configuration file, rather than
    buildout.cfg in the current directory.  
Jim Fulton's avatar
Jim Fulton committed
1460

Jim Fulton's avatar
Jim Fulton committed
1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474
-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
    Run in off-line mode.  This is equivalent to the assignment 
    buildout:offline=true.
Jim Fulton's avatar
Jim Fulton committed
1475

Jim Fulton's avatar
Jim Fulton committed
1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493
-O
    Run in non-off-line mode.  This is equivalent to the assignment 
    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
    Run in non-newest mode.  This is equivalent to the assignment 
    buildout:newest=false.  With this setting, buildout will not seek
    new distributions if installed distributions satisfy it's
    requirements. 

Jim Fulton's avatar
Jim Fulton committed
1494
Assignments are of the form::
1495 1496 1497

  section_name:option_name=value

Jim Fulton's avatar
Jim Fulton committed
1498 1499 1500
Options and assignments can be given in any order.

Here's an example:
Jim Fulton's avatar
Jim Fulton committed
1501 1502 1503 1504 1505 1506 1507

    >>> write(sample_buildout, 'other.cfg',
    ... """
    ... [buildout]
    ... develop = recipes
    ... parts = debug
    ... installed = .other.cfg
1508
    ... log-level = WARNING
Jim Fulton's avatar
Jim Fulton committed
1509 1510 1511 1512 1513
    ...
    ... [debug]
    ... name = other
    ... recipe = recipes:debug
    ... """)
1514

Jim Fulton's avatar
Jim Fulton committed
1515 1516 1517
Note that we used the installed buildout option to specify an
alternate file to store information about installed parts.
    
Jim Fulton's avatar
Jim Fulton committed
1518
    >>> print system(buildout+' -c other.cfg debug:op1=foo -v'),
1519 1520
    Develop: '/sample-buildout/recipes'
    Installing debug.
Jim Fulton's avatar
Jim Fulton committed
1521
    name other
1522 1523 1524
    op1 foo
    recipe recipes:debug

Jim Fulton's avatar
Jim Fulton committed
1525 1526 1527 1528 1529 1530
Here we used the -c option to specify an alternate configuration file, 
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:
    
Jim Fulton's avatar
Jim Fulton committed
1531
    >>> print system(buildout+' -vcother.cfg debug:op1=foo'),
1532 1533
    Develop: '/sample-buildout/recipes'
    Updating debug.
Jim Fulton's avatar
Jim Fulton committed
1534 1535 1536 1537 1538 1539 1540 1541
    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
1542 1543 1544
    >>> os.remove(os.path.join(sample_buildout, 'other.cfg'))
    >>> os.remove(os.path.join(sample_buildout, '.other.cfg'))

1545 1546
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
1547 1548
installed.  To illustrate this, we'll update our configuration and run
the buildout in the usual way:
1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571

    >>> 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
    ... """)

1572
    >>> print system(buildout),
1573 1574 1575
    Develop: '/sample-buildout/recipes'
    Uninstalling debug.
    Installing debug.
1576
    recipe recipes:debug
1577
    Installing d1.
Jim Fulton's avatar
Jim Fulton committed
1578
    d1: Creating directory d1
1579
    Installing d2.
Jim Fulton's avatar
Jim Fulton committed
1580
    d2: Creating directory d2
1581
    Installing d3.
Jim Fulton's avatar
Jim Fulton committed
1582
    d3: Creating directory d3
1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593
    
    >>> ls(sample_buildout)
    -  .installed.cfg
    -  b1.cfg
    -  b2.cfg
    -  base.cfg
    d  bin
    -  buildout.cfg
    d  d1
    d  d2
    d  d3
1594
    d  develop-eggs
1595 1596 1597 1598 1599 1600
    d  eggs
    d  parts
    d  recipes

    >>> cat(sample_buildout, '.installed.cfg')
    [buildout]
1601
    installed_develop_eggs = /sample-buildout/develop-eggs/recipes.egg-link
1602 1603 1604 1605
    parts = debug d1 d2 d3
    <BLANKLINE>
    [debug]
    __buildout_installed__ = 
1606
    __buildout_signature__ = recipes-PiIFiO8ny5yNZ1S3JfT0xg==
1607 1608 1609
    recipe = recipes:debug
    <BLANKLINE>
    [d1]
1610
    __buildout_installed__ = /sample-buildout/d1
1611
    __buildout_signature__ = recipes-PiIFiO8ny5yNZ1S3JfT0xg==
1612
    path = /sample-buildout/d1
1613 1614 1615
    recipe = recipes:mkdir
    <BLANKLINE>
    [d2]
1616
    __buildout_installed__ = /sample-buildout/d2
1617
    __buildout_signature__ = recipes-PiIFiO8ny5yNZ1S3JfT0xg==
1618
    path = /sample-buildout/d2
1619 1620 1621
    recipe = recipes:mkdir
    <BLANKLINE>
    [d3]
1622
    __buildout_installed__ = /sample-buildout/d3
1623
    __buildout_signature__ = recipes-PiIFiO8ny5yNZ1S3JfT0xg==
1624
    path = /sample-buildout/d3
1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644
    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
1645
    ... path = ${d2:path}-extra
1646 1647 1648 1649 1650 1651
    ...
    ... [debug]
    ... recipe = recipes:debug
    ... x = 1
    ... """)

1652
and run the buildout specifying just d3 and d4:
1653

1654
    >>> print system(buildout+' install d3 d4'),
1655 1656 1657
    Develop: '/sample-buildout/recipes'
    Uninstalling d3.
    Installing d3.
Jim Fulton's avatar
Jim Fulton committed
1658
    d3: Creating directory data3
1659
    Installing d4.
1660
    d4: Creating directory data2-extra
1661 1662 1663 1664 1665 1666 1667 1668 1669 1670
    
    >>> ls(sample_buildout)
    -  .installed.cfg
    -  b1.cfg
    -  b2.cfg
    -  base.cfg
    d  bin
    -  buildout.cfg
    d  d1
    d  d2
1671
    d  data2-extra
1672
    d  data3
1673
    d  develop-eggs
1674 1675 1676 1677
    d  eggs
    d  parts
    d  recipes
    
1678
Only the d3 and d4 recipes ran.  d3 was removed and data3 and data2-extra
1679 1680 1681 1682 1683 1684
were created.

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

    >>> cat(sample_buildout, '.installed.cfg')
    [buildout]
1685
    installed_develop_eggs = /sample-buildout/develop-eggs/recipes.egg-link
1686
    parts = debug d1 d2 d3 d4
1687 1688 1689
    <BLANKLINE>
    [debug]
    __buildout_installed__ = 
1690
    __buildout_signature__ = recipes-PiIFiO8ny5yNZ1S3JfT0xg==
1691 1692
    recipe = recipes:debug
    <BLANKLINE>
1693 1694 1695 1696 1697 1698
    [d1]
    __buildout_installed__ = /sample-buildout/d1
    __buildout_signature__ = recipes-PiIFiO8ny5yNZ1S3JfT0xg==
    path = /sample-buildout/d1
    recipe = recipes:mkdir
    <BLANKLINE>
1699
    [d2]
1700
    __buildout_installed__ = /sample-buildout/d2
1701
    __buildout_signature__ = recipes-PiIFiO8ny5yNZ1S3JfT0xg==
1702
    path = /sample-buildout/d2
1703 1704 1705
    recipe = recipes:mkdir
    <BLANKLINE>
    [d3]
1706
    __buildout_installed__ = /sample-buildout/data3
1707
    __buildout_signature__ = recipes-PiIFiO8ny5yNZ1S3JfT0xg==
1708
    path = /sample-buildout/data3
1709 1710 1711
    recipe = recipes:mkdir
    <BLANKLINE>
    [d4]
1712
    __buildout_installed__ = /sample-buildout/data2-extra
1713
    __buildout_signature__ = recipes-PiIFiO8ny5yNZ1S3JfT0xg==
1714
    path = /sample-buildout/data2-extra
1715 1716 1717 1718 1719 1720
    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
1721
Now, if we run the buildout without the install command:
1722

1723
    >>> print system(buildout),
1724 1725 1726 1727 1728
    Develop: '/sample-buildout/recipes'
    Uninstalling d2.
    Uninstalling d1.
    Uninstalling debug.
    Installing debug.
1729 1730
    recipe recipes:debug
    x 1
1731
    Installing d2.
Jim Fulton's avatar
Jim Fulton committed
1732
    d2: Creating directory data2
1733 1734
    Updating d3.
    Updating d4.
1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746

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
1747
    d  data2-extra
1748
    d  data3
1749
    d  develop-eggs
1750 1751 1752 1753
    d  eggs
    d  parts
    d  recipes

Jim Fulton's avatar
Jim Fulton committed
1754 1755
Alternate directory and file locations
--------------------------------------
1756 1757

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

1761
    >>> alt = tmpdir('sample-alt')
1762 1763 1764 1765 1766 1767

    >>> write(sample_buildout, 'buildout.cfg',
    ... """
    ... [buildout]
    ... develop = recipes
    ... parts = 
1768
    ... develop-eggs-directory = %(developbasket)s
1769 1770 1771 1772
    ... eggs-directory = %(basket)s
    ... bin-directory = %(scripts)s
    ... parts-directory = %(work)s
    ... """ % dict(
1773
    ...    developbasket = os.path.join(alt, 'developbasket'),
1774 1775 1776 1777
    ...    basket = os.path.join(alt, 'basket'),
    ...    scripts = os.path.join(alt, 'scripts'),
    ...    work = os.path.join(alt, 'work'),
    ... ))
1778

1779
    >>> print system(buildout),
1780 1781 1782 1783 1784 1785 1786 1787 1788
    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.
1789 1790 1791

    >>> ls(alt)
    d  basket
1792
    d  developbasket
1793 1794 1795
    d  scripts
    d  work

1796
    >>> ls(alt, 'developbasket')    
1797 1798
    -  recipes.egg-link

1799 1800
You can also specify an alternate buildout directory:

1801 1802
    >>> rmdir(alt)
    >>> alt = tmpdir('sample-alt')
1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814

    >>> write(sample_buildout, 'buildout.cfg',
    ... """
    ... [buildout]
    ... directory = %(alt)s
    ... develop = %(recipes)s
    ... parts = 
    ... """ % dict(
    ...    alt=alt,
    ...    recipes=os.path.join(sample_buildout, 'recipes'),
    ...    ))
 
1815
    >>> print system(buildout),
1816 1817 1818 1819 1820
    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'
1821 1822 1823 1824

    >>> ls(alt)
    -  .installed.cfg
    d  bin
1825
    d  develop-eggs
1826 1827 1828
    d  eggs
    d  parts

1829
    >>> ls(alt, 'develop-eggs')    
1830 1831
    -  recipes.egg-link

Jim Fulton's avatar
Jim Fulton committed
1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855
Logging control
---------------

Three buildout options are used to control logging:

log-level 
   specifies the log level

verbosity 
   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
1856
    ... log-format = %(levelname)s %(message)s
Jim Fulton's avatar
Jim Fulton committed
1857 1858 1859
    ... """)
 
Here, we've changed the format to include the log-level name, rather
1860
than the logger name.
Jim Fulton's avatar
Jim Fulton committed
1861 1862 1863

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
1864
configuration file.  Because the verbosity is subtracted from the log
Jim Fulton's avatar
Jim Fulton committed
1865 1866
level, we get a final log level of 20, which is the INFO level.

Jim Fulton's avatar
Jim Fulton committed
1867
    >>> print system(buildout),
1868
    INFO Develop: '/sample-buildout/recipes'
1869

1870 1871 1872
Predefined buildout options
---------------------------

1873
Buildouts have a number of predefined options that recipes can use
1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884
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.
         
    >>> write(sample_buildout, 'buildout.cfg',
    ... """
    ... [buildout]
    ... parts =
    ... """)

1885 1886 1887 1888 1889
    >>> print system(buildout+' -vv'),
    Installing 'zc.buildout', 'setuptools'.
    We have a develop egg: zc.buildout 1.0.0.
    We have the best distribution that satisfies 'setuptools'.
    Picked: setuptools = 0.6
1890
    <BLANKLINE>
1891 1892
    Configuration data:
    [buildout]
1893 1894 1895 1896
    bin-directory = /sample-buildout/bin
    develop-eggs-directory = /sample-buildout/develop-eggs
    directory = /sample-buildout
    eggs-directory = /sample-buildout/eggs
1897
    executable = /usr/local/bin/python2.3
1898
    installed = /sample-buildout/.installed.cfg
1899
    log-format = 
1900
    log-level = INFO
Jim Fulton's avatar
Jim Fulton committed
1901
    newest = true
1902
    offline = false
1903
    parts = 
1904
    parts-directory = /sample-buildout/parts
1905
    python = buildout
1906
    verbosity = 20
1907
    <BLANKLINE>
1908
 
1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919
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:

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
1920
   being created in the local project.  This can be a relative path,
1921 1922 1923 1924 1925 1926 1927 1928
   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
1929
   this directory across buildouts. Eggs in this directory should
1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950
   *never* be modified.  This can be a relative path, which is
   interpreted relative to the directory option.

executable
   The Python executable used to run the buildout.  See the python
   option below.

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

parts
1951
   A white space separated list of parts to be installed.
1952 1953 1954 1955 1956 1957

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

python
   The name of a section containing information about the default
1958
   Python interpreter.  Recipes that need a installation
1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970
   typically have options to tell them which Python installation to
   use.  By convention, if a section-specific option isn't used, the
   option is looked for in the buildout section.  The option must
   point to a section with an executable option giving the path to a
   Python executable.  By default, the buildout section defines the
   default Python as the Python used to run the buildout.

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


1971 1972
Creating new buildouts and bootstrapping
----------------------------------------
Jim Fulton's avatar
Jim Fulton committed
1973 1974 1975

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

1978
    >>> sample_bootstrapped = tmpdir('sample-bootstrapped')
1979

1980 1981 1982 1983 1984 1985 1986 1987 1988
    >>> print system(buildout
    ...              +' -c'+os.path.join(sample_bootstrapped, 'setup.cfg')
    ...              +' init'),
    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'.
1989 1990

Note that a basic setup.cfg was created for us.
Jim Fulton's avatar
Jim Fulton committed
1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001

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

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

2002 2003
    >>> _ = (ls(sample_bootstrapped, 'eggs'),
    ...      ls(sample_bootstrapped, 'develop-eggs'))
Jim Fulton's avatar
Jim Fulton committed
2004 2005
    -  setuptools-0.6-py2.3.egg
    -  zc.buildout-1.0-py2.3.egg
2006

2007
(We list both the eggs and develop-eggs directories because the
2008 2009 2010 2011
buildout or setuptools egg could be installed in the develop-eggs
directory if the original buildout had develop eggs for either
buildout or setuptools.)

Jim Fulton's avatar
Jim Fulton committed
2012
Note that the buildout script was installed but not run.  To run
2013
the buildout, we'd have to run the installed buildout script.
2014

2015 2016 2017 2018
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:

2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032
     >>> sample_bootstrapped2 = tmpdir('sample-bootstrapped2')

     >>> print system(buildout
     ...              +' -c'+os.path.join(sample_bootstrapped2, 'setup.cfg')
     ...              +' bootstrap'),
     While:
       Initializing.
     Error: Couldn't open /sample-bootstrapped2/setup.cfg

     >>> write(sample_bootstrapped2, 'setup.cfg',
     ... """
     ... [buildout]
     ... parts =
     ... """)
2033 2034 2035 2036

    >>> print system(buildout
    ...              +' -c'+os.path.join(sample_bootstrapped2, 'setup.cfg')
    ...              +' bootstrap'),
2037 2038 2039 2040 2041
    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'.
2042 2043


Jim Fulton's avatar
Jim Fulton committed
2044 2045 2046 2047 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061
Newest and Offline Modes
------------------------

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 be set to false using the -N
command-line option.

The offline option goes a bit further.  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.
2062

2063 2064
Preferring Final Releases
-------------------------
2065 2066

Currently, when searching for new releases, the newest available
2067 2068 2069 2070
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::
2071 2072 2073 2074 2075 2076

  [buildout]
  ...
  prefer-final = true

When the prefer-final option is set to true, then when searching for
2077
new releases, final releases are preferred.  If there are final
2078 2079 2080 2081
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.

2082
In buildout version 2, final releases will be preferred by default.
2083
You will then need to use a false value for prefer-final to get the
2084 2085 2086 2087 2088 2089 2090 2091 2092 2093 2094 2095 2096 2097 2098
newest releases.

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.
2099

Jim Fulton's avatar
Jim Fulton committed
2100 2101 2102
Controlling the installation database
-------------------------------------

2103
The buildout installed option is used to specify the file used to save
Jim Fulton's avatar
Jim Fulton committed
2104
information on installed parts.  This option is initialized to
2105
".installed.cfg", but it can be overridden in the configuration file
Jim Fulton's avatar
Jim Fulton committed
2106 2107
or on the command line:

2108 2109 2110 2111 2112 2113 2114 2115 2116 2117
    >>> write('buildout.cfg', 
    ... """
    ... [buildout]
    ... develop = recipes
    ... parts = debug
    ...
    ... [debug]
    ... recipe = recipes:debug
    ... """)

Jim Fulton's avatar
Jim Fulton committed
2118
    >>> print system(buildout+' buildout:installed=inst.cfg'),
2119 2120
    Develop: '/sample-buildout/recipes'
    Installing debug.
2121
    recipe recipes:debug
Jim Fulton's avatar
Jim Fulton committed
2122 2123 2124 2125 2126 2127 2128 2129 2130 2131 2132 2133 2134 2135

    >>> ls(sample_buildout)
    -  b1.cfg
    -  b2.cfg
    -  base.cfg
    d  bin
    -  buildout.cfg
    d  develop-eggs
    d  eggs
    -  inst.cfg
    d  parts
    d  recipes

The installation database can be disabled by supplying an empty
2136
buildout installed option:
Jim Fulton's avatar
Jim Fulton committed
2137 2138 2139

    >>> os.remove('inst.cfg')
    >>> print system(buildout+' buildout:installed='),
2140 2141
    Develop: '/sample-buildout/recipes'
    Installing debug.
2142
    recipe recipes:debug
Jim Fulton's avatar
Jim Fulton committed
2143 2144 2145 2146 2147 2148 2149 2150 2151 2152 2153 2154 2155

    >>> ls(sample_buildout)
    -  b1.cfg
    -  b2.cfg
    -  base.cfg
    d  bin
    -  buildout.cfg
    d  develop-eggs
    d  eggs
    d  parts
    d  recipes


2156 2157 2158 2159 2160 2161 2162 2163 2164 2165 2166 2167 2168 2169 2170 2171 2172 2173 2174 2175 2176 2177
Note that there will be no installation database if there are no
parts:

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

    >>> print system(buildout+' buildout:installed=inst.cfg'),

    >>> ls(sample_buildout)
    -  b1.cfg
    -  b2.cfg
    -  base.cfg
    d  bin
    -  buildout.cfg
    d  develop-eggs
    d  eggs
    d  parts
    d  recipes

2178 2179 2180 2181
Extensions
----------

An **experimental** feature allows code to be loaded and run after
Jim Fulton's avatar
Jim Fulton committed
2182
configuration files have been read but before the buildout has begun
2183 2184 2185 2186 2187 2188 2189 2190
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
named will be loaded and any zc.buildout.extensions entry points found
will be called with the buildout as an argument.

Jim Fulton's avatar
Jim Fulton committed
2191
Let's create a sample extension in our sample buildout created in the
2192 2193 2194 2195 2196 2197 2198 2199 2200 2201 2202 2203 2204 2205 2206 2207 2208 2209 2210 2211 2212 2213 2214 2215 2216 2217 2218 2219 2220 2221 2222 2223 2224 2225 2226
previous section:

    >>> mkdir(sample_bootstrapped, 'demo')

    >>> write(sample_bootstrapped, 'demo', 'demo.py', 
    ... """
    ... def ext(buildout):
    ...     print 'ext', list(buildout)
    ... """)

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

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)
    >>> print system(os.path.join(sample_bootstrapped, 'bin', 'buildout')),
2227
    Develop: '/sample-bootstrapped/demo'
2228

2229
Now we can add the extensions option.  We were a bit tricky and ran
2230 2231 2232 2233 2234 2235 2236 2237 2238 2239 2240 2241 2242 2243
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 =
    ... """)
   
Jim Fulton's avatar
Jim Fulton committed
2244
We see that our extension is loaded and executed:
2245 2246 2247

    >>> print system(os.path.join(sample_bootstrapped, 'bin', 'buildout')),
    ext ['buildout']
2248
    Develop: '/sample-bootstrapped/demo'
2249

Jim Fulton's avatar
Jim Fulton committed
2250 2251


2252
.. [#future_recipe_methods] In the future, additional methods may be
Jim Fulton's avatar
Jim Fulton committed
2253 2254 2255 2256 2257 2258 2259
       added. Older recipes with fewer methods will still be
       supported.

.. [#packaging_info] If we wanted to create a distribution from this
       package, we would need specify much more information.  See the
       `setuptools documentation
       <http://peak.telecommunity.com/DevCenter/setuptools>`_.