chiark / gitweb /
Escape: Add missing r in regexp literals ('...' => r'...') [6]
[git-buildpackage.git] / gbp / deb / policy.py
1 # vim: set fileencoding=utf-8 :
2 #
3 # (C) 2006,2007,2011 Guido Günther <agx@sigxcpu.org>
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, please see
16 #    <http://www.gnu.org/licenses/>
17 """
18 Debian Packaging policies
19
20 like allowed characters in version numbers, etc.
21 """
22
23 import os
24 import re
25
26 from gbp.errors import GbpError
27 from gbp.pkg.pkgpolicy import PkgPolicy
28 from gbp.pkg.compressor import Compressor
29
30
31 class DebianPkgPolicy(PkgPolicy):
32     """
33     Packaging policy for Debian Source Packages
34
35     >>> DebianPkgPolicy.is_valid_upstreamversion('1:9.8.4.dfsg.P1-6')
36     True
37     >>> DebianPkgPolicy.is_valid_upstreamversion('-1')
38     False
39     """
40
41     # Valid package names according to Debian Policy Manual 5.6.1:
42     # "Package names (both source and binary, see Package, Section 5.6.7)
43     # must consist only of lower case letters (a-z), digits (0-9), plus (+)
44     # and minus (-) signs, and periods (.). They must be at least two
45     # characters long and must start with an alphanumeric character."
46     packagename_re = re.compile(r"^[a-zA-Z0-9][a-zA-Z0-9\.\+\-~]+$")
47     packagename_msg = """Package names must be at least two characters long, start with an
48     alphanumeric and can only containg letters (a-z,A-Z), digits
49     (0-9), plus signs (+), minus signs (-), periods (.) and hyphens (~)"""
50
51     # Valid upstream versions according to Debian Policy Manual 5.6.12:
52     # "The upstream_version may contain only alphanumerics[32] and the
53     # characters . + - : ~ (full stop, plus, hyphen, colon, tilde) and
54     # should start with a digit. If there is no debian_revision then hyphens
55     # are not allowed; if there is no epoch then colons are not allowed."
56     # Since we don't know about any epochs and debian revisions yet, the
57     # last two conditions are not checked.
58     upstreamversion_re = re.compile(r"^[0-9][a-zA-Z0-9\.\+\-\:\~]*$")
59     upstreamversion_msg = """Upstream version numbers must start with a digit and can only containg lower case
60     letters (a-z), digits (0-9), full stops (.), plus signs (+), minus signs
61     (-), colons (:) and tildes (~)"""
62
63     # Valid characters in a debian version
64     debianversion_chars = 'a-zA-Z\\d.~+-'
65
66     @staticmethod
67     def build_tarball_name(name, version, compression, dir=None, component=None):
68         """
69         Given a source package's I{name}, I{version} and I{compression}
70         return the name of the corresponding upstream tarball.
71
72         >>> DebianPkgPolicy.build_tarball_name('foo', '1.0', 'bzip2')
73         'foo_1.0.orig.tar.bz2'
74         >>> DebianPkgPolicy.build_tarball_name('bar', '0.0~git1234', 'xz')
75         'bar_0.0~git1234.orig.tar.xz'
76         >>> DebianPkgPolicy.build_tarball_name('bar', '0.0~git1234', 'xz', component="foo")
77         'bar_0.0~git1234.orig-foo.tar.xz'
78
79         @param name: the source package's name
80         @type name: C{str}
81         @param version: the upstream version
82         @type version: C{str}
83         @param compression: the desired compression
84         @type compression: C{str}
85         @param dir: a directory to prepend
86         @type dir: C{str}
87         @return: the tarballs name corresponding to the input parameters
88         @rtype: C{str}
89         """
90         try:
91             ext = Compressor.Exts[compression]
92         except KeyError:
93             raise GbpError("Unknown compression type '%s'" % compression)
94         sub = '-{0}'.format(component) if component else ''
95         tarball = "%s_%s.orig%s.tar.%s" % (name, version, sub, ext)
96         if dir:
97             tarball = os.path.join(dir, tarball)
98         return tarball