Debian Codegraph @

setup_helpers.py

raw
1 # Copyright (C) 2009-2014 by Barry A. Warsaw
2 #
3 # This file is part of flufl.password
4 #
5 # flufl.password is free software: you can redistribute it and/or modify it
6 # under the terms of the GNU Lesser General Public License as published by the
7 # Free Software Foundation, version 3 of the License.
8 #
9 # flufl.password is distributed in the hope that it will be useful, but
10 # WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
11 # or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
12 # License for more details.
13 #
14 # You should have received a copy of the GNU Lesser General Public License
15 # along with flufl.password. If not, see <http://www.gnu.org/licenses/>.
16
17 """setup.py helper functions."""
18
19 from __future__ import absolute_import, print_function, unicode_literals
20
21
22 __metaclass__ = type
23 __all__ = [
24 'description',
25 'find_doctests',
26 'get_version',
27 'long_description',
28 'require_python',
29 ]
30
31
32 import os
33 import re
34 import sys
35
36
37 DEFAULT_VERSION_RE = re.compile(r'(?P<version>\d+\.\d+(?:\.\d+)?)')
38 EMPTYSTRING = ''
39
40 __version__ = '2.1'
41
42
43
44 def require_python(minimum):
45 """Require at least a minimum Python version.
46
47 The version number is expressed in terms of `sys.hexversion`. E.g. to
48 require a minimum of Python 2.6, use::
49
50 >>> require_python(0x206000f0)
51
52 :param minimum: Minimum Python version supported.
53 :type minimum: integer
54 """
55 if sys.hexversion < minimum:
56 hversion = hex(minimum)[2:]
57 if len(hversion) % 2 != 0:
58 hversion = '0' + hversion
59 split = list(hversion)
60 parts = []
61 while split:
62 parts.append(int(''.join((split.pop(0), split.pop(0))), 16))
63 major, minor, micro, release = parts
64 if release == 0xf0:
65 print('Python {0}.{1}.{2} or better is required'.format(
66 major, minor, micro))
67 else:
68 print('Python {0}.{1}.{2} ({3}) or better is required'.format(
69 major, minor, micro, hex(release)[2:]))
70 sys.exit(1)
71
72
73
74 def get_version(filename, pattern=None):
75 """Extract the __version__ from a file without importing it.
76
77 While you could get the __version__ by importing the module, the very act
78 of importing can cause unintended consequences. For example, Distribute's
79 automatic 2to3 support will break. Instead, this searches the file for a
80 line that starts with __version__, and extract the version number by
81 regular expression matching.
82
83 By default, two or three dot-separated digits are recognized, but by
84 passing a pattern parameter, you can recognize just about anything. Use
85 the `version` group name to specify the match group.
86
87 :param filename: The name of the file to search.
88 :type filename: string
89 :param pattern: Optional alternative regular expression pattern to use.
90 :type pattern: string
91 :return: The version that was extracted.
92 :rtype: string
93 """
94 if pattern is None:
95 cre = DEFAULT_VERSION_RE
96 else:
97 cre = re.compile(pattern)
98 with open(filename) as fp:
99 for line in fp:
100 if line.startswith('__version__'):
101 mo = cre.search(line)
102 assert mo, 'No valid __version__ string found'
103 return mo.group('version')
104 raise AssertionError('No __version__ assignment found')
105
106
107
108 def find_doctests(start='.', extension='.rst'):
109 """Find separate-file doctests in the package.
110
111 This is useful for Distribute's automatic 2to3 conversion support. The
112 `setup()` keyword argument `convert_2to3_doctests` requires file names,
113 which may be difficult to track automatically as you add new doctests.
114
115 :param start: Directory to start searching in (default is cwd)
116 :type start: string
117 :param extension: Doctest file extension (default is .txt)
118 :type extension: string
119 :return: The doctest files found.
120 :rtype: list
121 """
122 doctests = []
123 for dirpath, dirnames, filenames in os.walk(start):
124 doctests.extend(os.path.join(dirpath, filename)
125 for filename in filenames
126 if filename.endswith(extension))
127 return doctests
128
129
130
131 def long_description(*filenames):
132 """Provide a long description."""
133 res = ['']
134 for filename in filenames:
135 with open(filename) as fp:
136 for line in fp:
137 res.append(' ' + line)
138 res.append('')
139 res.append('\n')
140 return EMPTYSTRING.join(res)
141
142
143 def description(filename):
144 """Provide a short description."""
145 # This ends up in the Summary header for PKG-INFO and it should be a
146 # one-liner. It will get rendered on the package page just below the
147 # package version header but above the long_description, which ironically
148 # gets stuff into the Description header. It should not include reST, so
149 # pick out the first single line after the double header.
150 with open(filename) as fp:
151 for lineno, line in enumerate(fp):
152 if lineno < 3:
153 continue
154 line = line.strip()
155 if len(line) > 0:
156 return line