/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar

« back to all changes in this revision

Viewing changes to doc/developers/HACKING.txt

Update to bzr.dev.

Show diffs side-by-side

added added

removed removed

Lines of Context:
15
15
``doc/developers/HACKING.txt`` in the source tree, or at
16
16
http://doc.bazaar-vcs.org/bzr.dev/en/developer-guide/HACKING.html
17
17
 
 
18
See also:
 
19
`Bazaar Developer Documentation Catalog <../../developers/index.html>`_.
 
20
 
18
21
.. contents::
19
22
 
20
23
 
212
215
Navigating the Code Base
213
216
========================
214
217
 
215
 
TODO: List and describe in one line the purpose of each directory
216
 
inside an installation of bzr.
217
 
 
218
 
TODO: Refer to a central location holding an up to date copy of the API
219
 
documentation generated by epydoc, e.g. something like
220
 
http://starship.python.net/crew/mwh/bzrlibapi/bzrlib.html.
 
218
.. Was at <http://bazaar-vcs.org/NewDeveloperIntroduction>
 
219
 
 
220
Some of the key files in this directory are:
 
221
 
 
222
bzr
 
223
    The command you run to start Bazaar itself.  This script is pretty
 
224
    short and just does some checks then jumps into bzrlib.
 
225
 
 
226
README
 
227
    This file covers a brief introduction to Bazaar and lists some of its
 
228
    key features. 
 
229
 
 
230
NEWS
 
231
    Summary of changes in each Bazaar release that can affect users or 
 
232
    plugin developers.
 
233
 
 
234
setup.py
 
235
    Installs Bazaar system-wide or to your home directory.  To perform
 
236
    development work on Bazaar it is not required to run this file - you
 
237
    can simply run the bzr command from the top level directory of your
 
238
    development copy. Note: That if you run setup.py this will create a
 
239
    'build' directory in your development branch. There's nothing wrong
 
240
    with this but don't be confused by it. The build process puts a copy
 
241
    of the main code base into this build directory, along with some other
 
242
    files. You don't need to go in here for anything discussed in this
 
243
    guide. 
 
244
 
 
245
bzrlib
 
246
    Possibly the most exciting folder of all, bzrlib holds the main code
 
247
    base. This is where you will go to edit python files and contribute to
 
248
    Bazaar.
 
249
 
 
250
doc
 
251
    Holds documentation on a whole range of things on Bazaar from the
 
252
    origination of ideas within the project to information on Bazaar
 
253
    features and use cases.  Within this directory there is a subdirectory
 
254
    for each translation into a human language.  All the documentation 
 
255
    is in the ReStructuredText markup language.
 
256
 
 
257
doc/developers 
 
258
    Documentation specifically targetted at Bazaar and plugin developers.
 
259
    (Including this document.)
 
260
    
 
261
        
 
262
 
 
263
Automatically-generated API reference information is available at 
 
264
<http://starship.python.net/crew/mwh/bzrlibapi/>.  
 
265
(There is an experimental editable version at 
 
266
<http://starship.python.net/crew/mwh/bzrlibapi-oe/>.)
 
267
See also the `Essential Domain Classes`_
 
268
section of this guide.
221
269
 
222
270
 
223
271
Testing Bazaar
250
298
down the track do not break new features or bug fixes that you are
251
299
contributing today.
252
300
 
253
 
As of May 2007, Bazaar ships with a test suite containing over 6000 tests
 
301
As of May 2008, Bazaar ships with a test suite containing over 12000 tests
254
302
and growing. We are proud of it and want to remain so. As community
255
303
members, we all benefit from it. Would you trust version control on
256
304
your project to a product *without* a test suite like Bazaar has?
283
331
This option can be combined with other selftest options (like -x) and
284
332
filter patterns to understand their effect.
285
333
 
 
334
Once you understand how to create a list of tests, you can use the --load-list
 
335
option to run only a restricted set of tests that you kept in a file, one test
 
336
id by line. Keep in mind that this will never be sufficient to validate your
 
337
modifications, you still need to run the full test suite for that, but using it
 
338
can help in some cases (like running only the failed tests for some time)::
 
339
 
 
340
  ./bzr selftest -- load-list my_failing_tests
 
341
 
 
342
This option can also be combined with other selftest options, including
 
343
patterns. It has some drawbacks though, the list can become out of date pretty
 
344
quick when doing Test Driven Development.
 
345
 
 
346
To address this concern, there is another way to run a restricted set of tests:
 
347
the --starting-with option will run only the tests whose name starts with the
 
348
specified string. It will also avoid loading the other tests and as a
 
349
consequence starts running your tests quicker::
 
350
 
 
351
  ./bzr selftest --starting-with bzrlib.blackbox
 
352
 
 
353
This option can be combined with all the other selftest options including
 
354
--load-list. The later is rarely used but allows to run a subset of a list of
 
355
failing tests for example.
286
356
 
287
357
Test suite debug flags
288
358
----------------------
628
698
the form of URL components.
629
699
 
630
700
 
631
 
Core Topics
632
 
###########
633
 
 
634
 
Evolving Interfaces
635
 
===================
636
 
 
637
 
We have a commitment to 6 months API stability - any supported symbol in a
638
 
release of bzr MUST NOT be altered in any way that would result in
639
 
breaking existing code that uses it. That means that method names,
640
 
parameter ordering, parameter names, variable and attribute names etc must
641
 
not be changed without leaving a 'deprecated forwarder' behind. This even
642
 
applies to modules and classes.
643
 
 
644
 
If you wish to change the behaviour of a supported API in an incompatible
645
 
way, you need to change its name as well. For instance, if I add an optional keyword
646
 
parameter to branch.commit - that's fine. On the other hand, if I add a
647
 
keyword parameter to branch.commit which is a *required* transaction
648
 
object, I should rename the API - i.e. to 'branch.commit_transaction'. 
649
 
 
650
 
When renaming such supported API's, be sure to leave a deprecated_method (or
651
 
_function or ...) behind which forwards to the new API. See the
652
 
bzrlib.symbol_versioning module for decorators that take care of the
653
 
details for you - such as updating the docstring, and issuing a warning
654
 
when the old api is used.
655
 
 
656
 
For unsupported API's, it does not hurt to follow this discipline, but it's
657
 
not required. Minimally though, please try to rename things so that
658
 
callers will at least get an AttributeError rather than weird results.
659
 
 
660
 
 
661
 
Deprecation decorators
662
 
----------------------
663
 
 
664
 
``bzrlib.symbol_versioning`` provides decorators that can be attached to
665
 
methods, functions, and other interfaces to indicate that they should no
666
 
longer be used.
667
 
 
668
 
To deprecate a static method you must call ``deprecated_function``
669
 
(**not** method), after the staticmethod call::
670
 
 
671
 
    @staticmethod
672
 
    @deprecated_function(zero_ninetyone)
673
 
    def create_repository(base, shared=False, format=None):
674
 
 
675
 
When you deprecate an API, you should not just delete its tests, because
676
 
then we might introduce bugs in them.  If the API is still present at all,
677
 
it should still work.  The basic approach is to use
678
 
``TestCase.applyDeprecated`` which in one step checks that the API gives
679
 
the expected deprecation message, and also returns the real result from
680
 
the method, so that tests can keep running.
681
 
 
682
701
Coding Style Guidelines
683
 
=======================
 
702
#######################
684
703
 
685
704
hasattr and getattr
686
 
-------------------
 
705
===================
687
706
 
688
707
``hasattr`` should not be used because it swallows exceptions including
689
708
``KeyboardInterrupt``.  Instead, say something like ::
692
711
 
693
712
 
694
713
Code layout
695
 
-----------
 
714
===========
696
715
 
697
716
Please write PEP-8__ compliant code.  
698
717
 
781
800
 
782
801
 
783
802
Module Imports
784
 
--------------
 
803
==============
785
804
 
786
805
* Imports should be done at the top-level of the file, unless there is
787
806
  a strong reason to have them lazily loaded when a particular
793
812
 
794
813
 
795
814
Naming
796
 
------
 
815
======
797
816
 
798
817
Functions, methods or members that are "private" to bzrlib are given
799
818
a leading underscore prefix.  Names without a leading underscore are
816
835
 
817
836
 
818
837
Standard Names
819
 
--------------
 
838
==============
820
839
 
821
840
``revision_id`` not ``rev_id`` or ``revid``
822
841
 
825
844
 
826
845
 
827
846
Destructors
828
 
-----------
 
847
===========
829
848
 
830
849
Python destructors (``__del__``) work differently to those of other
831
850
languages.  In particular, bear in mind that destructors may be called
833
852
later time, or possibly never at all.  Therefore we have restrictions on
834
853
what can be done inside them.
835
854
 
836
 
 0. Never use a __del__ method without asking Martin/Robert first.
 
855
 0. If you think you need to use a ``__del__`` method ask another
 
856
    developer for alternatives.  If you do need to use one, explain
 
857
    why in a comment.
837
858
 
838
859
 1. Never rely on a ``__del__`` method running.  If there is code that
839
860
    must run, do it from a ``finally`` block instead.
847
868
 
848
869
 
849
870
Factories
850
 
---------
 
871
=========
851
872
 
852
873
In some places we have variables which point to callables that construct
853
874
new instances.  That is to say, they can be used a lot like class objects,
862
883
 
863
884
 
864
885
Registries
865
 
----------
 
886
==========
866
887
 
867
888
Several places in Bazaar use (or will use) a registry, which is a 
868
889
mapping from names to objects or classes.  The registry allows for 
871
892
 
872
893
 
873
894
Lazy Imports
874
 
------------
 
895
============
875
896
 
876
897
To make startup time faster, we use the ``bzrlib.lazy_import`` module to
877
898
delay importing modules until they are actually used. ``lazy_import`` uses
901
922
the variable is a module, and these object should be hidden anyway, since
902
923
they shouldn't be imported into other namespaces.
903
924
 
904
 
 
905
 
Modules versus Members
906
 
~~~~~~~~~~~~~~~~~~~~~~
907
 
 
908
925
While it is possible for ``lazy_import()`` to import members of a module
909
926
when using the ``from module import member`` syntax, it is recommended to
910
927
only use that syntax to load sub modules ``from module import submodule``.
921
938
This will incorrectly fail, because ``MyClass`` is a ``ImportReplacer``
922
939
object, rather than the real class.
923
940
 
924
 
 
925
 
Passing to Other Variables
926
 
~~~~~~~~~~~~~~~~~~~~~~~~~~
927
 
 
928
941
It also is incorrect to assign ``ImportReplacer`` objects to other variables.
929
942
Because the replacer only knows about the original name, it is unable to
930
943
replace other variables. The ``ImportReplacer`` class will raise an
934
947
 
935
948
 
936
949
The Null revision
937
 
-----------------
 
950
=================
938
951
 
939
952
The null revision is the ancestor of all revisions.  Its revno is 0, its
940
953
revision-id is ``null:``, and its tree is the empty tree.  When referring
943
956
being phased out.
944
957
 
945
958
 
 
959
Object string representations
 
960
=============================
 
961
 
 
962
Python prints objects using their ``__repr__`` method when they are
 
963
written to logs, exception tracebacks, or the debugger.  We want
 
964
objects to have useful representations to help in determining what went
 
965
wrong.
 
966
 
 
967
If you add a new class you should generally add a ``__repr__`` method
 
968
unless there is an adequate method in a parent class.  There should be a
 
969
test for the repr.  
 
970
 
 
971
Representations should typically look like Python constructor syntax, but
 
972
they don't need to include every value in the object and they don't need
 
973
to be able to actually execute.  They're to be read by humans, not
 
974
machines.  Don't hardcode the classname in the format, so that we get the
 
975
correct value if the method is inherited by a subclass.  If you're
 
976
printing attributes of the object, including strings, you should normally
 
977
use ``%r`` syntax (to call their repr in turn).
 
978
 
 
979
Try to avoid the representation becoming more than one or two lines long.
 
980
(But balance this against including useful information, and simplicity of
 
981
implementation.)
 
982
 
 
983
Because repr methods are often called when something has already gone
 
984
wrong, they should be written somewhat more defensively than most code.
 
985
The object may be half-initialized or in some other way in an illegal
 
986
state.  The repr method shouldn't raise an exception, or it may hide the
 
987
(probably more useful) underlying exception.
 
988
 
 
989
Example::
 
990
 
 
991
    def __repr__(self):
 
992
        return '%s(%r)' % (self.__class__.__name__,
 
993
                           self._transport)
 
994
 
 
995
 
 
996
Exception handling
 
997
==================
 
998
 
 
999
A bare ``except`` statement will catch all exceptions, including ones that
 
1000
really should terminate the program such as ``MemoryError`` and
 
1001
``KeyboardInterrupt``.  They should rarely be used unless the exception is
 
1002
later re-raised.  Even then, think about whether catching just
 
1003
``Exception`` (which excludes system errors in Python2.5 and later) would
 
1004
be better.
 
1005
 
 
1006
 
 
1007
 
 
1008
Core Topics
 
1009
###########
 
1010
 
 
1011
Evolving Interfaces
 
1012
===================
 
1013
 
 
1014
We have a commitment to 6 months API stability - any supported symbol in a
 
1015
release of bzr MUST NOT be altered in any way that would result in
 
1016
breaking existing code that uses it. That means that method names,
 
1017
parameter ordering, parameter names, variable and attribute names etc must
 
1018
not be changed without leaving a 'deprecated forwarder' behind. This even
 
1019
applies to modules and classes.
 
1020
 
 
1021
If you wish to change the behaviour of a supported API in an incompatible
 
1022
way, you need to change its name as well. For instance, if I add an optional keyword
 
1023
parameter to branch.commit - that's fine. On the other hand, if I add a
 
1024
keyword parameter to branch.commit which is a *required* transaction
 
1025
object, I should rename the API - i.e. to 'branch.commit_transaction'. 
 
1026
 
 
1027
When renaming such supported API's, be sure to leave a deprecated_method (or
 
1028
_function or ...) behind which forwards to the new API. See the
 
1029
bzrlib.symbol_versioning module for decorators that take care of the
 
1030
details for you - such as updating the docstring, and issuing a warning
 
1031
when the old api is used.
 
1032
 
 
1033
For unsupported API's, it does not hurt to follow this discipline, but it's
 
1034
not required. Minimally though, please try to rename things so that
 
1035
callers will at least get an AttributeError rather than weird results.
 
1036
 
 
1037
 
 
1038
Deprecation decorators
 
1039
----------------------
 
1040
 
 
1041
``bzrlib.symbol_versioning`` provides decorators that can be attached to
 
1042
methods, functions, and other interfaces to indicate that they should no
 
1043
longer be used.  For example::
 
1044
 
 
1045
   @deprecated_method(deprecated_in((0, 1, 4)))
 
1046
   def foo(self):
 
1047
        return self._new_foo()
 
1048
 
 
1049
To deprecate a static method you must call ``deprecated_function``
 
1050
(**not** method), after the staticmethod call::
 
1051
 
 
1052
    @staticmethod
 
1053
    @deprecated_function(deprecated_in((0, 1, 4)))
 
1054
    def create_repository(base, shared=False, format=None):
 
1055
 
 
1056
When you deprecate an API, you should not just delete its tests, because
 
1057
then we might introduce bugs in them.  If the API is still present at all,
 
1058
it should still work.  The basic approach is to use
 
1059
``TestCase.applyDeprecated`` which in one step checks that the API gives
 
1060
the expected deprecation message, and also returns the real result from
 
1061
the method, so that tests can keep running.
 
1062
 
 
1063
Deprecation warnings will be suppressed for final releases, but not for
 
1064
development versions or release candidates, or when running ``bzr
 
1065
selftest``. This gives developers information about whether their code is
 
1066
using deprecated functions, but avoids confusing users about things they
 
1067
can't fix.
 
1068
 
 
1069
 
946
1070
Getting Input
947
1071
=============
948
1072
 
1186
1310
 
1187
1311
 
1188
1312
Assertions
1189
 
----------
 
1313
==========
1190
1314
 
1191
1315
Do not use the Python ``assert`` statement, either in tests or elsewhere.
1192
1316
A source test checks that it is not used.  It is ok to explicitly raise
1483
1607
* reviewing changes
1484
1608
* reviewing blueprints
1485
1609
* planning releases
1486
 
* managing releases.
 
1610
* managing releases (see the `Releasing Bazaar <../../developers/releasing.html>`_)
1487
1611
 
1488
1612
.. note::
1489
1613
  Removing barriers to community participation is a key reason for adopting
1494
1618
  differences between core and non-core contributors to a minimum.
1495
1619
 
1496
1620
 
1497
 
The Development Lifecycle
1498
 
-------------------------
1499
 
 
1500
 
As a rule, Bazaar development follows a 4 week cycle:
1501
 
 
1502
 
* 2 weeks - general changes
1503
 
* 1 week - feature freeze
1504
 
* 1 week+ - Release Candidate stabilization
1505
 
 
1506
 
During the FeatureFreeze week, the trunk (bzr.dev) is open in a limited
1507
 
way: only low risk changes, critical and high priority fixes are accepted
1508
 
during this time. At the end of FeatureFreeze, a branch is created for the
1509
 
first Release Candidate and the trunk is reopened for general development
1510
 
on the *next* release. A week or so later, the final release is packaged
1511
 
assuming no serious problems were encountered with the one or more Release
1512
 
Candidates.
1513
 
 
1514
 
.. note::
1515
 
  There is a one week overlap between the start of one release and
1516
 
  the end of the previous one.
1517
 
 
1518
 
 
1519
1621
Communicating and Coordinating
1520
1622
------------------------------
1521
1623