1
# Copyright (C) 2004 Aaron Bentley
2
# <aaron.bentley@utoronto.ca>
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.
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.
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
19
__docformat__ = "restructuredtext"
20
__doc__ = "Tools to generate and interpret Arch pathnames"
22
def determine_path(thing, thingname=None, archivelocation=None):
24
Converts a name to its location.
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.
35
thingname=arch.NameParser(thing)
36
if archivelocation is None:
37
archivelocation=arch.Archive(thingname.get_archive()).location
39
if not thingname.has_category():
41
path+="/"+thingname.get_category()
42
if not thingname.has_package():
44
path+="/"+thingname.get_package()
45
if not thingname.has_version():
47
path+="/"+thingname.get_package_version()
48
if not thingname.has_patchlevel():
50
path+="/"+thingname.get_patchlevel()
53
class NotARevision(Exception):
54
"""Raise if a name is not a revision name, though it should be."""
57
def determine_import_path(thing):
58
return determine_file_path(thing, ".src.tar.gz")
60
def determine_cacherev_path(thing):
61
return determine_file_path(thing, ".tar.gz")
63
def determine_file_path(thing, extension):
64
thingname=arch.NameParser(thing)
65
if not thingname.has_patchlevel():
67
return determine_path(thing, thingname)+"/"+thingname.get_nonarch()+extension
69
def determine_patch_path(thing):
70
return determine_file_path(thing, ".patches.tar.gz")
72
def determine_log_path(thing):
73
return determine_path(thing)+"/log"
75
def determine_continuation_path(thing):
76
return determine_path(thing)+"/CONTINUATION"
78
def decode_path(path):
79
pathcomponents = path.rstrip("/").split("/")
80
if arch.NameParser.is_patchlevel(pathcomponents[-1]):
85
if not is_version_path(pathcomponents[:-1]):
86
raise CantDecode(path)
87
nonarch = pathcomponents[-2]+"--"+pathcomponents[-1]
88
arch_loc = "/".join(pathcomponents[:-4])
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
96
def is_version_path(pathcomponents):
98
Determines whether a file path is a plausible version path.
100
:param pathcomponents: The components of the file path
101
:type pathcomponents: List of str
104
if not pathcomponents[-1].startswith(pathcomponents[-2]+"--"):
106
elif not pathcomponents[-2].startswith(pathcomponents[-3]):
111
def arch_name_from_path(path):
112
for myarch in arch.iter_archives():
113
if myarch.location == path:
114
return myarch.official_name
116
tmparch = arch.register_archive('tmparch@example.com', path)
117
name = tmparch.official_name
121
class CantDecode(Exception):
123
Raise to indicate that a path could not be decoded.
125
def __init__(self, path):
127
message = "Path \"%s\" could not be decoded." % self.path
128
Exception.__init__(self, message)
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
139
raise CantDecode(path)
141
def is_url_path(spec):
143
Determines whether the provided spec is a path or archive URL.
145
:param spec: the string that may be a path or archive URL
148
if spec.startswith("/") or spec.startswith("http://"):
150
elif spec.startswith("sftp://") or spec.startswith ("ftp://"):
152
elif spec.startswith("ws_ftp://"):
157
def tree_log(dir, revision):
158
"""Return the full path to a patchlog file.
159
:param dir: The tree-root directory
161
:param revision: The revision of the log to get
162
:type revision: `arch.Revision`
164
return "%s/{arch}/%s/%s/%s/%s/patch-log/%s" % (dir,
165
revision.category.nonarch,
166
revision.branch.nonarch,
167
revision.version.nonarch,
172
# arch-tag: 5f0fccc4-9681-4652-bb79-10560f2da13e