Debian Codegraph @

setup_helpers.py

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