~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to generate_version_info.py

  • Committer: John Arbash Meinel
  • Date: 2005-12-28 20:49:47 UTC
  • mto: (2022.1.1 version-info-55794)
  • mto: This revision was merged to the branch mainline in revision 2028.
  • Revision ID: john@arbash-meinel.com-20051228204947-2fd81543e866350a
Creating a plugin to ease generating version information from a tree.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# Copyright (C) 2005 Canonical Ltd
 
2
 
 
3
# This program is free software; you can redistribute it and/or modify
 
4
# it under the terms of the GNU General Public License as published by
 
5
# the Free Software Foundation; either version 2 of the License, or
 
6
# (at your option) any later version.
 
7
 
 
8
# This program is distributed in the hope that it will be useful,
 
9
# but WITHOUT ANY WARRANTY; without even the implied warranty of
 
10
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
11
# GNU General Public License for more details.
 
12
 
 
13
# You should have received a copy of the GNU General Public License
 
14
# along with this program; if not, write to the Free Software
 
15
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 
16
 
 
17
"""\
 
18
Routines for extracting all version information from a bzr branch.
 
19
"""
 
20
 
 
21
import sys
 
22
import time
 
23
 
 
24
from bzrlib.rio import RioReader, RioWriter, Stanza
 
25
from bzrlib.errors import NoWorkingTree
 
26
from errors import UncleanError
 
27
 
 
28
def is_clean(branch):
 
29
    """
 
30
    Raise an UncleanError if there is anything unclean about this
 
31
    branch.
 
32
 
 
33
    :param branch: The branch to check for changes
 
34
    TODO: jam 20051228 This might be better to ask for a WorkingTree
 
35
            instead of a Branch.
 
36
    """
 
37
    try:
 
38
        new_tree = branch.working_tree()
 
39
    except NoWorkingTree:
 
40
        # Trees without a working tree can't be dirty :)
 
41
        return 
 
42
 
 
43
    # Look for unknown files in the new tree
 
44
    for info in new_tree.list_files():
 
45
        path = info[0]
 
46
        file_class = info[1]
 
47
        if file_class == '?':
 
48
            raise UncleanError(branch, 'path %s is unknown' % (path,))
 
49
 
 
50
    from bzrlib.diff import compare_trees
 
51
    # See if there is anything that has been changed
 
52
    old_tree = branch.basis_tree()
 
53
    delta = compare_trees(old_tree, new_tree, want_unchanged=False)
 
54
    if len(delta.added) > 0:
 
55
        raise UncleanError(branch, 'have added files: %r' % (delta.added,))
 
56
    if len(delta.removed) > 0:
 
57
        raise UncleanError(branch, 'have removed files: %r' % (delta.removed,))
 
58
    if len(delta.modified) > 0:
 
59
        raise UncleanError(branch, 'have modified files: %r' % (delta.modified,))
 
60
    if len(delta.renamed) > 0:
 
61
        raise UncleanError(branch, 'have renamed files: %r' % (delta.renamed,))
 
62
 
 
63
 
 
64
def generate_rio_version(branch, to_file=sys.stdout,
 
65
        check_for_clean=False,
 
66
        include_revision_history=False,
 
67
        include_log_info=False,
 
68
        include_log_deltas=False):
 
69
    """Create the version file for this project.
 
70
 
 
71
    :param branch: The branch to write information about
 
72
    :param to_file: The file to write the information
 
73
    :param check_for_clean: If true, check if the branch is clean.
 
74
        This can be expensive for large trees. This is also only
 
75
        valid for branches with working trees.
 
76
    :param include_revision_history: Write out the list of revisions
 
77
    :param include_log_info: Include log information (log summary, etc),
 
78
        only valid if include_revision_history is also True
 
79
    :param include_log_deltas: Include information about what changed in
 
80
        each revision. Only valid if include_log_info is also True
 
81
    """
 
82
    info = Stanza()
 
83
    # TODO: jam 20051228 This might be better as the datestamp 
 
84
    #       of the last commit
 
85
    info.add('date', time.strftime('%Y-%m-%d %H:%M:%S (%A, %B %d, %Y, %Z)'))
 
86
    info.add('revno', branch.revno())
 
87
    info.add('revision_id', branch.last_revision())
 
88
    info.add('branch_nick', branch.nick)
 
89
    if check_for_clean:
 
90
        try:
 
91
            wt = branch.working_tree()
 
92
        except NoWorkingTree:
 
93
            pass
 
94
        else:
 
95
            pass
 
96
    info.update(_get_bzr_info(path=path, full=full))
 
97
    if info['branch_nick'] is not None:
 
98
        info['version'] = '%(branch_nick)s-%(revno)s' % info
 
99
    elif info['revno'] is not None:
 
100
        info['version'] = str(info['revno'])
 
101
    else:
 
102
        info['version'] = 'unknown'
 
103
 
 
104
    f = open(version_fn, 'wb')
 
105
    f.write(_version_template % info)
 
106
    f.close()
 
107