~abentley/bzrtools/bzrtools.dev

147.1.3 by Robert Collins
test and deliver basic pending-merges into bzr so that merging is recorded
1
# Copyright (C) 2004 Aaron Bentley
2
# <aaron.bentley@utoronto.ca>
3
#
4
#    This program is free software; you can redistribute it and/or modify
5
#    it under the terms of the GNU General Public License as published by
6
#    the Free Software Foundation; either version 2 of the License, or
7
#    (at your option) any later version.
8
#
9
#    This program is distributed in the hope that it will be useful,
10
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
11
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12
#    GNU General Public License for more details.
13
#
14
#    You should have received a copy of the GNU General Public License
15
#    along with this program; if not, write to the Free Software
16
#    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
17
18
import pybaz as arch
19
__docformat__ = "restructuredtext"
20
__doc__ = "Tools to generate and interpret Arch pathnames"
21
22
def determine_path(thing, thingname=None, archivelocation=None):
23
    """
24
    Converts a name to its location.
25
26
    :param thing: an archive, version, revision or other name
27
    :type thing: anything a NameParser can parse
28
    :param thingname: Optional NameParser version of the name
29
    :type thingname: `arch.NameParser`
30
    :param archivelocation: Optional location of the archive.  Required if \
31
    thing has no archive name. 
32
    :rtype: str
33
    """
34
    if thingname is None:
35
	thingname=arch.NameParser(thing)
36
    if archivelocation is None:
37
	archivelocation=arch.Archive(thingname.get_archive()).location
38
    path=archivelocation
39
    if not thingname.has_category():
40
	return path
41
    path+="/"+thingname.get_category()
42
    if not thingname.has_package():
43
	return path
44
    path+="/"+thingname.get_package()
45
    if not thingname.has_version():
46
	return path
47
    path+="/"+thingname.get_package_version()
48
    if not thingname.has_patchlevel():
49
	return path
50
    path+="/"+thingname.get_patchlevel()
51
    return path
52
53
class NotARevision(Exception):
54
    """Raise if a name is not a revision name, though it should be."""
55
    pass
56
57
def determine_import_path(thing):
58
    return determine_file_path(thing, ".src.tar.gz")
59
60
def determine_cacherev_path(thing):
61
    return determine_file_path(thing, ".tar.gz")
62
63
def determine_file_path(thing, extension):
64
    thingname=arch.NameParser(thing)
65
    if not thingname.has_patchlevel():
66
	raise NotARevision
67
    return determine_path(thing, thingname)+"/"+thingname.get_nonarch()+extension
68
69
def determine_patch_path(thing):
70
    return determine_file_path(thing, ".patches.tar.gz")
71
72
def determine_log_path(thing):
73
    return determine_path(thing)+"/log"
74
75
def determine_continuation_path(thing):
76
    return determine_path(thing)+"/CONTINUATION"
77
78
def decode_path(path):
79
    pathcomponents = path.rstrip("/").split("/")
80
    if arch.NameParser.is_patchlevel(pathcomponents[-1]):
81
        is_revision = True
82
    else:
83
        is_revision = False
84
    if is_revision:
85
        if not is_version_path(pathcomponents[:-1]):
86
            raise CantDecode(path)
87
        nonarch = pathcomponents[-2]+"--"+pathcomponents[-1]
88
        arch_loc = "/".join(pathcomponents[:-4])
89
    else:
90
        if not is_version_path(pathcomponents):
91
            raise CantDecode(path)
92
        nonarch = pathcomponents[-1]
93
        arch_loc = "/".join(pathcomponents[:-3])
94
    return arch_loc, nonarch 
95
96
def is_version_path(pathcomponents):
97
    """
98
    Determines whether a file path is a plausible version path.
99
100
    :param pathcomponents: The components of the file path
101
    :type pathcomponents: List of str
102
    :rtype: bool
103
    """
104
    if not pathcomponents[-1].startswith(pathcomponents[-2]+"--"):
105
        return False
106
    elif not pathcomponents[-2].startswith(pathcomponents[-3]):
107
        return False
108
    else:
109
        return True
110
111
def arch_name_from_path(path):
112
    for myarch in arch.iter_archives():
113
        if myarch.location == path:
114
            return myarch.official_name
115
    
116
    tmparch = arch.register_archive('tmparch@example.com', path)
117
    name = tmparch.official_name
118
    tmparch.unregister()
119
    return name
120
121
class CantDecode(Exception):
122
    """
123
    Raise to indicate that a path could not be decoded.
124
    """
125
    def __init__(self, path):
126
        self.path=path
127
        message = "Path \"%s\" could not be decoded." % self.path
128
        Exception.__init__(self, message)
129
130
def full_path_decode(path):
131
    arch_loc, nonarch = decode_path(path)
132
    archive = arch_name_from_path(arch_loc)
133
    fq_name = arch.NameParser("%s/%s" % (archive, nonarch))
134
    if fq_name.is_version():
135
        return arch.Version(fq_name), arch_loc
136
    elif fq_name.has_patchlevel():
137
        return arch.Revision(fq_name), arch_loc
138
    else:
139
        raise CantDecode(path)
140
141
def is_url_path(spec):
142
    """
143
    Determines whether the provided spec is a path or archive URL.
144
145
    :param spec: the string that may be a path or archive URL
146
    :type spec: str
147
    """
148
    if spec.startswith("/") or spec.startswith("http://"):
149
        return True
150
    elif spec.startswith("sftp://") or spec.startswith ("ftp://"):
151
        return True
152
    elif spec.startswith("ws_ftp://"):
153
        return True
154
    else:
155
        return False
156
157
def tree_log(dir, revision):
158
    """Return the full path to a patchlog file.
159
    :param dir: The tree-root directory
160
    :type dir: str
161
    :param revision: The revision of the log to get
162
    :type revision: `arch.Revision`
163
    """
164
    return "%s/{arch}/%s/%s/%s/%s/patch-log/%s" % (dir, 
165
                                                   revision.category.nonarch, 
166
                                                   revision.branch.nonarch, 
167
                                                   revision.version.nonarch, 
168
                                                   revision.archive, 
169
                                                   revision.patchlevel)
170
171
172
# arch-tag: 5f0fccc4-9681-4652-bb79-10560f2da13e