├── requirements.txt ├── requirements_dev.txt ├── .gitattributes ├── MANIFEST.in ├── miniast ├── util.py ├── __init__.py ├── source.py ├── tests │ └── test_miniast.py ├── _version.py └── base.py ├── setup.cfg ├── setup.py ├── .gitignore ├── .circleci └── config.yml ├── README.md ├── LICENSE └── versioneer.py /requirements.txt: -------------------------------------------------------------------------------- 1 | toolz 2 | -------------------------------------------------------------------------------- /requirements_dev.txt: -------------------------------------------------------------------------------- 1 | pytest 2 | -------------------------------------------------------------------------------- /.gitattributes: -------------------------------------------------------------------------------- 1 | miniast/_version.py export-subst 2 | -------------------------------------------------------------------------------- /MANIFEST.in: -------------------------------------------------------------------------------- 1 | include versioneer.py 2 | include miniast/_version.py 3 | -------------------------------------------------------------------------------- /miniast/util.py: -------------------------------------------------------------------------------- 1 | import textwrap 2 | 3 | 4 | def indent(text, spaces=' ' * 4): 5 | return textwrap.indent(text, spaces) 6 | -------------------------------------------------------------------------------- /miniast/__init__.py: -------------------------------------------------------------------------------- 1 | from .base import * # noqa: F401, F403 2 | from .source import sourcify # noqa: F401 3 | from toolz import identity as in_ # noqa: F401 4 | 5 | from ._version import get_versions 6 | __version__ = get_versions()['version'] 7 | del get_versions 8 | -------------------------------------------------------------------------------- /setup.cfg: -------------------------------------------------------------------------------- 1 | 2 | # See the docstring in versioneer.py for instructions. Note that you must 3 | # re-run 'versioneer.py setup' after changing this section, and commit the 4 | # resulting files. 5 | 6 | [versioneer] 7 | VCS = git 8 | style = pep440 9 | versionfile_source = miniast/_version.py 10 | versionfile_build = miniast/_version.py 11 | tag_prefix = 12 | parentdir_prefix = miniast- 13 | 14 | -------------------------------------------------------------------------------- /setup.py: -------------------------------------------------------------------------------- 1 | import versioneer 2 | 3 | from setuptools import setup, find_packages 4 | 5 | 6 | setup( 7 | name='miniast', 8 | url='https://github.com/cpcloud/miniast', 9 | packages=find_packages(), 10 | python_requires='>=3.5', 11 | version=versioneer.get_version(), 12 | cmdclass=versioneer.get_cmdclass(), 13 | description='Lightweight macros for Python', 14 | license='Apache License, Version 2.0', 15 | author='Phillip Cloud', 16 | author_email='cpcloud@gmail.com', 17 | ) 18 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # Byte-compiled / optimized / DLL files 2 | __pycache__/ 3 | *.py[cod] 4 | *$py.class 5 | 6 | # C extensions 7 | *.so 8 | *.c 9 | 10 | # Distribution / packaging 11 | .Python 12 | env/ 13 | build/ 14 | develop-eggs/ 15 | dist/ 16 | downloads/ 17 | eggs/ 18 | .eggs/ 19 | lib/ 20 | lib64/ 21 | parts/ 22 | sdist/ 23 | var/ 24 | *.egg-info/ 25 | .installed.cfg 26 | *.egg 27 | 28 | # PyInstaller 29 | # Usually these files are written by a python script from a template 30 | # before PyInstaller builds the exe, so as to inject date/other infos into it. 31 | *.manifest 32 | *.spec 33 | 34 | # Installer logs 35 | pip-log.txt 36 | pip-delete-this-directory.txt 37 | 38 | # Unit test / coverage reports 39 | htmlcov/ 40 | .tox/ 41 | .coverage 42 | .coverage.* 43 | .cache 44 | nosetests.xml 45 | coverage.xml 46 | *,cover 47 | .hypothesis/ 48 | 49 | # Translations 50 | *.mo 51 | *.pot 52 | 53 | # Django stuff: 54 | *.log 55 | local_settings.py 56 | 57 | # Flask stuff: 58 | instance/ 59 | .webassets-cache 60 | 61 | # Scrapy stuff: 62 | .scrapy 63 | 64 | # Sphinx documentation 65 | docs/_build/ 66 | 67 | # PyBuilder 68 | target/ 69 | 70 | # Jupyter Notebook 71 | .ipynb_checkpoints 72 | 73 | # pyenv 74 | .python-version 75 | 76 | # celery beat schedule file 77 | celerybeat-schedule 78 | 79 | # dotenv 80 | .env 81 | 82 | # virtualenv 83 | .venv/ 84 | venv/ 85 | ENV/ 86 | 87 | # Spyder project settings 88 | .spyderproject 89 | 90 | # Rope project settings 91 | .ropeproject 92 | .pytest_cache 93 | -------------------------------------------------------------------------------- /.circleci/config.yml: -------------------------------------------------------------------------------- 1 | test: &test 2 | steps: 3 | - checkout 4 | - run: 5 | name: Download conda 6 | command: | 7 | curl -Ls -o $HOME/miniconda.sh https://repo.continuum.io/miniconda/Miniconda3-latest-Linux-x86_64.sh 8 | - run: 9 | name: Install conda 10 | command: bash $HOME/miniconda.sh -b -p $HOME/miniconda 11 | - run: 12 | name: Create conda environment 13 | command: | 14 | $HOME/miniconda/bin/conda create -q -n miniast -y -c conda-forge python="$PYTHON" 15 | - run: 16 | name: Activate environment 17 | command: | 18 | echo 'export PATH=$HOME/miniconda/bin:$PATH' >> $BASH_ENV 19 | echo 'source activate miniast' >> $BASH_ENV 20 | - run: 21 | name: Install dependencies 22 | command: | 23 | pip install -r requirements.txt 24 | - run: 25 | name: Install dev dependencies 26 | command: | 27 | pip install -r requirements_dev.txt 28 | - run: 29 | name: Install package 30 | command: | 31 | python setup.py develop 32 | - run: 33 | name: Make test reports directory 34 | command: mkdir -p /tmp/test-reports/pytest 35 | - run: 36 | name: Execute Pytest 37 | command: | 38 | pytest -rsxX --junitxml=/tmp/test-reports/pytest/junit.xml 39 | - store_test_results: 40 | path: /tmp/test-reports 41 | - store_artifacts: 42 | path: /tmp/test-reports 43 | 44 | jobs: 45 | python35_test: 46 | docker: 47 | - image: circleci/python:3.6 48 | <<: *test 49 | environment: 50 | PYTHON: 3.5 51 | 52 | python36_test: 53 | docker: 54 | - image: circleci/python:3.6 55 | <<: *test 56 | environment: 57 | PYTHON: 3.6 58 | 59 | workflows: 60 | version: 2 61 | test: 62 | jobs: 63 | - python35_test 64 | - python36_test 65 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | `miniast`: Manipulate Python ASTs in Python 2 | --- 3 | 4 | `miniast` is a Python library that provides APIs for generating Python ASTs 5 | (abstract syntax trees). 6 | 7 | The `ast` module that ships with the standard library is wonderful until you 8 | need to generate more than a few nodes. 9 | 10 | 11 | Here's a regular Python class 12 | 13 | 14 | ```python 15 | class Average: 16 | def __init__(self): 17 | self.value = 0.0 18 | self.count = 0 19 | 20 | def step(self, value): 21 | if self.value is not None: 22 | self.value += value 23 | self.count += 1 24 | 25 | def finalize(self): 26 | if self.count: 27 | return self.value / self.count 28 | ``` 29 | 30 | Here's what it looks like if you were to define it programmatically using 31 | `miniast`: 32 | 33 | ```python 34 | from miniast import * 35 | 36 | class_.Average[ 37 | def_.__init__(arg.self)[ 38 | var.self.value.store(0.0), 39 | var.self.count.store(0), 40 | ], 41 | def_.step(arg.self, arg.value)[ 42 | if_(var.self.value.is_not(NONE))[ 43 | var.self.value.iadd(var.value), 44 | var.self.count.iadd(1), 45 | ] 46 | ], 47 | def_.finalize(arg.self)[ 48 | if_(var.self.count)[ 49 | return_(var.self.value / var.self.count) 50 | ] 51 | ] 52 | ] 53 | ``` 54 | 55 | Pretty sweet right? 56 | 57 | Here's what it would look like if you wrote it using raw `ast` nodes: 58 | 59 | ```python 60 | ClassDef( 61 | name='Average', 62 | bases=[], 63 | keywords=[], 64 | body=[ 65 | FunctionDef( 66 | name='__init__', 67 | args=arguments( 68 | args=[arg( 69 | arg='self', 70 | annotation=None)], 71 | vararg=None, 72 | kwonlyargs=[], 73 | kw_defaults=[], 74 | kwarg=None, 75 | defaults=[]), 76 | body=[ 77 | Assign( 78 | targets=[Attribute( 79 | value=Name( 80 | id='self', 81 | ctx=Load()), 82 | attr='value', 83 | ctx=Store())], 84 | value=Num(n=0.0)), 85 | Assign( 86 | targets=[Attribute( 87 | value=Name( 88 | id='self', 89 | ctx=Load()), 90 | attr='count', 91 | ctx=Store())], 92 | value=Num(n=0))], 93 | decorator_list=[], 94 | returns=None), 95 | FunctionDef( 96 | name='step', 97 | args=arguments( 98 | args=[ 99 | arg( 100 | arg='self', 101 | annotation=None), 102 | arg( 103 | arg='value', 104 | annotation=None)], 105 | vararg=None, 106 | kwonlyargs=[], 107 | kw_defaults=[], 108 | kwarg=None, 109 | defaults=[]), 110 | body=[If( 111 | test=Compare( 112 | left=Attribute( 113 | value=Name( 114 | id='self', 115 | ctx=Load()), 116 | attr='value', 117 | ctx=Load()), 118 | ops=[IsNot()], 119 | comparators=[NameConstant(value=None)]), 120 | body=[ 121 | AugAssign( 122 | target=Attribute( 123 | value=Name( 124 | id='self', 125 | ctx=Load()), 126 | attr='value', 127 | ctx=Load()), 128 | op=Add(), 129 | value=Name( 130 | id='value', 131 | ctx=Load())), 132 | AugAssign( 133 | target=Attribute( 134 | value=Name( 135 | id='self', 136 | ctx=Load()), 137 | attr='count', 138 | ctx=Load()), 139 | op=Add(), 140 | value=Num(n=1))], 141 | orelse=[])], 142 | decorator_list=[], 143 | returns=None), 144 | FunctionDef( 145 | name='finalize', 146 | args=arguments( 147 | args=[arg( 148 | arg='self', 149 | annotation=None)], 150 | vararg=None, 151 | kwonlyargs=[], 152 | kw_defaults=[], 153 | kwarg=None, 154 | defaults=[]), 155 | body=[If( 156 | test=Attribute( 157 | value=Name( 158 | id='self', 159 | ctx=Load()), 160 | attr='count', 161 | ctx=Load()), 162 | body=[Return(value=BinOp( 163 | left=Attribute( 164 | value=Name( 165 | id='self', 166 | ctx=Load()), 167 | attr='value', 168 | ctx=Load()), 169 | op=Div(), 170 | right=Attribute( 171 | value=Name( 172 | id='self', 173 | ctx=Load()), 174 | attr='count', 175 | ctx=Load())))], 176 | orelse=[])], 177 | decorator_list=[], 178 | returns=None)], 179 | decorator_list=[]) 180 | ``` 181 | 182 | That is truly horrifying. 183 | 184 | **Why should you care about this?** 185 | 186 | Since the ASTs generated are just Python expressions, you're free to manipulate 187 | them as you see fit. 188 | 189 | This library arose while writing the 190 | [`slumba`](https://github.com/cpcloud/slumba) library, to generate code that 191 | numba would be able to type infer. 192 | -------------------------------------------------------------------------------- /miniast/source.py: -------------------------------------------------------------------------------- 1 | from __future__ import print_function 2 | 3 | import ast 4 | import itertools 5 | 6 | from miniast.util import indent 7 | 8 | 9 | class SourceVisitor(ast.NodeVisitor): 10 | """An AST visitor to show what our generated function looks like. 11 | """ 12 | 13 | def visit(self, node): 14 | node_type = type(node) 15 | node_typename = node_type.__name__ 16 | method = getattr(self, 'visit_{}'.format(node_typename), None) 17 | if method is None: 18 | raise TypeError( 19 | 'Node of type {} has no visit method'.format(node_typename) 20 | ) 21 | return method(node) 22 | 23 | def visit_Lambda(self, node): 24 | return 'lambda {}: {}'.format( 25 | ', '.join(map(self.visit, node.args.args)), 26 | self.visit(node.body) 27 | ) 28 | 29 | def visit_ExceptHandler(self, node): 30 | type = node.type 31 | name = node.name 32 | body = indent('\n'.join(map(self.visit, node.body))) 33 | if type is None: 34 | assert name is None, 'type is None but got name' 35 | return 'except:\n{}'.format(body) 36 | 37 | spec = 'except {}'.format(self.visit(type)) 38 | if name is not None: 39 | spec += ' as {}'.format(name) 40 | return '{}:\n{}'.format(spec, body) 41 | 42 | def visit_TryWithFinally(self, node): 43 | # TODO: can we remove the assemble method? 44 | return self.visit(node.assemble()) 45 | 46 | visit_TryWithElse = visit_TryWithExcept = visit_TryWithFinally 47 | 48 | def visit_Try(self, node): 49 | lines = [ 50 | 'try:', 51 | indent('\n'.join(map(self.visit, node.body))) 52 | ] + list(map(self.visit, node.handlers)) 53 | 54 | orelse = node.orelse # else: clause 55 | if orelse: 56 | lines.append('else:') 57 | lines.append(indent('\n'.join(map(self.visit, orelse)))) 58 | 59 | finalbody = node.finalbody # finally: clause 60 | if finalbody: 61 | lines.append('finally:') 62 | lines.append(indent('\n'.join(map(self.visit, finalbody)))) 63 | return '\n'.join(lines) 64 | 65 | def visit_List(self, node): 66 | return '[{}]'.format(', '.join(map(self.visit, node.elts))) 67 | 68 | def visit_Tuple(self, node): 69 | elements = node.elts 70 | length = len(elements) 71 | if length == 1: 72 | return '({},)'.format(self.visit(elements[0])) 73 | return '({})'.format(', '.join(map(self.visit, elements))) 74 | 75 | def visit_Dict(self, node): 76 | return '{{{}}}'.format( 77 | ', '.join(map('{}: {}'.format, node.keys, node.values)) 78 | ) 79 | 80 | def visit_Set(self, node): 81 | elements = node.elts 82 | if not elements: 83 | return 'set()' 84 | return '{{{}}}'.format(', '.join(map(self.visit, elements))) 85 | 86 | def visit_NoneType(self, node): 87 | return '' 88 | 89 | def visit_BinOp(self, node): 90 | return '{} {} {}'.format( 91 | self.visit(node.left), self.visit(node.op), self.visit(node.right) 92 | ) 93 | 94 | def visit_Add(self, node): 95 | return '+' 96 | 97 | def visit_Sub(self, node): 98 | return '-' 99 | 100 | def visit_Mult(self, node): 101 | return '*' 102 | 103 | def visit_Div(self, node): 104 | return '/' 105 | 106 | def visit_FloorDiv(self, node): 107 | return '//' 108 | 109 | def visit_Pow(self, node): 110 | return '**' 111 | 112 | def visit_AugAssign(self, node): 113 | return '{} {}= {}'.format( 114 | self.visit(node.target), 115 | self.visit(node.op), 116 | self.visit(node.value) 117 | ) 118 | 119 | def visit_If(self, node): 120 | test = self.visit(node.test) 121 | body = indent('\n'.join(map(self.visit, node.body))) 122 | if node.orelse: 123 | orelse = indent('\n'.join(map(self.visit, node.orelse))) 124 | return 'if {test}:\n{body}\nelse:\n{orelse}'.format( 125 | test=test, 126 | body=body, 127 | orelse=orelse 128 | ) 129 | return 'if {test}:\n{body}'.format(test=test, body=body) 130 | 131 | def visit_IfExp(self, node): 132 | return '{body} if {test} else {orelse}'.format( 133 | body=self.visit(node.body), 134 | test=self.visit(node.test), 135 | orelse=self.visit(node.orelse), 136 | ) 137 | 138 | def visit_Yield(self, node): 139 | return 'yield {}'.format(self.visit(node.value)) 140 | 141 | def visit_YieldFrom(self, node): 142 | return 'yield from {}'.format(self.visit(node.value)) 143 | 144 | def visit_And(self, node): 145 | return 'and' 146 | 147 | def visit_Or(self, node): 148 | return 'or' 149 | 150 | def visit_Lt(self, node): 151 | return '<' 152 | 153 | def visit_LtE(self, node): 154 | return '<=' 155 | 156 | def visit_Gt(self, node): 157 | return '>' 158 | 159 | def visit_GtE(self, node): 160 | return '>=' 161 | 162 | def visit_In(self, node): 163 | return 'in' 164 | 165 | def visit_NotIn(self, node): 166 | return 'not in' 167 | 168 | def visit_NotEq(self, node): 169 | return '!=' 170 | 171 | def visit_Eq(self, node): 172 | return '==' 173 | 174 | def visit_Not(self, node): 175 | return 'not ' 176 | 177 | def visit_Is(self, node): 178 | return 'is' 179 | 180 | def visit_IsNot(self, node): 181 | return 'is not' 182 | 183 | def visit_UnaryOp(self, node): 184 | return '{}{}'.format(self.visit(node.op), self.visit(node.operand)) 185 | 186 | def visit_Compare(self, node): 187 | left = self.visit(node.left) 188 | return left + ' '.join( 189 | ' {} {}'.format(self.visit(op), self.visit(comparator)) 190 | for op, comparator in zip(node.ops, node.comparators) 191 | ) 192 | 193 | def visit_BoolOp(self, node): 194 | return '{} {} {}'.format( 195 | self.visit(node.left), 196 | self.visit(node.op), 197 | self.visit(node.right), 198 | ) 199 | 200 | def visit_Return(self, node): 201 | return 'return {}'.format(self.visit(node.value)) 202 | 203 | def visit_Attribute(self, node): 204 | return '{}.{}'.format(self.visit(node.value), node.attr) 205 | 206 | def visit_ImportFrom(self, node): 207 | imports = ', '.join( 208 | ' as '.join(filter(None, (alias.name, alias.asname))) 209 | for alias in node.names 210 | ) 211 | return 'from {} import {}'.format(node.module, imports) 212 | 213 | def visit_Assign(self, node): 214 | return '{} = {}'.format( 215 | ', '.join(map(self.visit, node.targets)), 216 | self.visit(node.value) 217 | ) 218 | 219 | def visit_SpecialArg(self, node): 220 | return node.arg 221 | 222 | def visit_Args(self, node): 223 | return '*{}'.format(node) 224 | 225 | def visit_Kwargs(self, node): 226 | return '**{}'.format(node.arg) 227 | 228 | def visit_FunctionDef(self, node): 229 | decorator_list = '\n'.join(map(self.visit, node.decorator_list)) 230 | decorators = '@{}\n'.format(decorator_list) if decorator_list else '' 231 | xargs = node.args 232 | allargs = itertools.chain( 233 | xargs.args, filter(None, [xargs.vararg, xargs.kwarg])) 234 | args = ', '.join(map(self.visit, allargs)) 235 | body = indent('\n'.join(map(self.visit, node.body))) 236 | return '\n{}def {}({}):\n{}'.format(decorators, node.name, args, body) 237 | 238 | def visit_Call(self, node): 239 | if isinstance(node.func, ast.Attribute): 240 | func = self.visit(node.func) 241 | args = ',\n'.join(itertools.chain( 242 | map(self.visit, node.args), 243 | ('{}={!r}'.format(kw.arg, self.visit(kw.value)) 244 | for kw in node.keywords) 245 | )) 246 | indented_args = indent(args) 247 | template = ('(\n{}\n)' if args else '({})').format(indented_args) 248 | return '{}{}'.format(func, template) 249 | else: 250 | args = ', '.join(itertools.chain( 251 | map(self.visit, node.args), 252 | ('{}={!r}'.format(kw.arg, self.visit(kw.value)) 253 | for kw in node.keywords) 254 | )) 255 | return '{}({})'.format(self.visit(node.func), args) 256 | 257 | def visit_NameConstant(self, node): 258 | return node.value 259 | 260 | def visit_Expr(self, node): 261 | return self.visit(node.value) 262 | 263 | def visit_Name(self, node): 264 | return node.id 265 | 266 | def visit_Num(self, node): 267 | return str(node.n) 268 | 269 | def visit_Str(self, node): 270 | return repr(node.s) 271 | 272 | def visit_arg(self, node): 273 | return node.arg 274 | 275 | def visit_Pass(self, node): 276 | return 'pass' 277 | 278 | def visit_Raise(self, node): 279 | raise_string = 'raise {}'.format(self.visit(node.exc)) 280 | cause = getattr(node, 'cause', None) 281 | 282 | if cause is not None: 283 | return '{} from {}'.format(raise_string, self.visit(cause)) 284 | return raise_string 285 | 286 | def visit_Subscript(self, node): 287 | value = self.visit(node.value) 288 | slice = self.visit(node.slice) 289 | return '{}[{}]'.format(value, slice) 290 | 291 | def visit_Index(self, node): 292 | return self.visit(node.value) 293 | 294 | def visit_Module(self, node): 295 | return '\n'.join(map(self.visit, node.body)) 296 | 297 | def visit_ClassDef(self, node): 298 | 299 | bases = list(map(self.visit, node.bases)) 300 | keywords = node.keywords 301 | buf = ['class {}'.format(node.name)] 302 | 303 | if bases: 304 | buf.append('({}'.format(', '.join(bases))) 305 | 306 | if keywords: 307 | kwargs = ', '.join( 308 | '{}={}'.format(k.arg, self.visit(k.value)) for k in keywords 309 | ) 310 | buf.append(', {}'.format(kwargs)) 311 | 312 | if bases or keywords: 313 | buf.append(')') 314 | 315 | body = indent('\n'.join(map(self.visit, node.body))) 316 | buf.append(':{}'.format(body)) 317 | return ''.join(buf) 318 | 319 | def visit_While(self, node): 320 | body = indent('\n'.join(map(self.visit, node.body))) 321 | return 'while {}:\n{}'.format(self.visit(node.test), body) 322 | 323 | def visit_For(self, node): 324 | target = self.visit(node.target) 325 | iter = self.visit(node.iter) 326 | body = indent('\n'.join(map(self.visit, node.body))) 327 | return 'for {} in {}:\n{}'.format(target, iter, body) 328 | 329 | def visit_Import(self, node): 330 | return 'import {}'.format(', '.join(map(self.visit, node.names))) 331 | 332 | def visit_Alias(self, node): 333 | return ' as '.join(filter(None, (node.name, node.asname))) 334 | 335 | 336 | def sourcify(mod): 337 | return SourceVisitor().visit(mod) 338 | -------------------------------------------------------------------------------- /miniast/tests/test_miniast.py: -------------------------------------------------------------------------------- 1 | from __future__ import print_function 2 | 3 | import ast 4 | 5 | import pytest 6 | 7 | from miniast import ( 8 | alias, 9 | arg, 10 | Attribute, 11 | class_, 12 | def_, 13 | FALSE, 14 | for_, 15 | if_, 16 | from_, 17 | lambda_, 18 | mod, 19 | Name, 20 | NONE, 21 | pass_, 22 | return_, 23 | TRUE, 24 | var, 25 | while_, 26 | SpecialArg, 27 | yield_, 28 | yield_from, 29 | Tuple, 30 | to_node, 31 | try_, 32 | import_, 33 | Alias, 34 | ) 35 | 36 | from miniast import sourcify 37 | 38 | 39 | def eq(a, b): 40 | """Test equality of AST nodes, because Python doesn't define __eq__ for 41 | them :( 42 | """ 43 | if isinstance(a, (ast.Load, ast.Store)): 44 | return isinstance(b, type(a)) 45 | if isinstance(a, list): 46 | return isinstance(b, list) and all(map(eq, a, b)) 47 | return a == b or ( 48 | isinstance(a, type(b)) and 49 | isinstance(b, type(a)) and 50 | hasattr(a, '_fields') and 51 | hasattr(b, '_fields') and 52 | a._fields == b._fields and 53 | all(eq(getattr(a, field), getattr(b, field)) for field in a._fields) 54 | ) 55 | 56 | 57 | def test_eq(): 58 | assert not eq(1, 0) 59 | assert eq('a', 'a') 60 | 61 | assert not eq('a', 1) 62 | 63 | assert not eq( 64 | ast.Name(id='x', ctx=ast.Load()), 65 | ast.Name(id='x', ctx=ast.Store()) 66 | ) 67 | assert eq( 68 | ast.Name(id='x', ctx=ast.Load()), 69 | ast.Name(id='x', ctx=ast.Load()) 70 | ) 71 | 72 | 73 | def test_var(): 74 | assert eq(var.foo, ast.Name(id='foo', ctx=ast.Load())) 75 | assert eq(var['bar'], ast.Name(id='bar', ctx=ast.Load())) 76 | 77 | 78 | def test_assign(): 79 | assert eq( 80 | var.foo.store(0), 81 | ast.Assign( 82 | targets=[ast.Name(id='foo', ctx=ast.Store())], 83 | value=ast.Num(n=0) 84 | ) 85 | ) 86 | 87 | 88 | def test_arg(): 89 | assert eq(arg.fizzbuzz, SpecialArg(arg='fizzbuzz', annotation=None)) 90 | 91 | 92 | def test_call(): 93 | assert eq( 94 | var.foo(1, 2), 95 | ast.Call( 96 | func=ast.Name(id='foo', ctx=ast.Load()), 97 | args=[n(1), n(2)], 98 | keywords=[], 99 | ) 100 | ) 101 | assert eq( 102 | var.func(var.a, b=var.b), 103 | ast.Call( 104 | func=ast.Name(id='func', ctx=ast.Load()), 105 | args=[ast.Name(id='a', ctx=ast.Load())], 106 | keywords=[ 107 | ast.keyword(arg='b', value=ast.Name(id='b', ctx=ast.Load())) 108 | ] 109 | ) 110 | ) 111 | 112 | 113 | def test_attr(): 114 | assert eq( 115 | var.foo.get_a_thing, 116 | Attribute( 117 | value=Name(id='foo', ctx=ast.Load()), 118 | attr='get_a_thing', 119 | ctx=ast.Load(), 120 | ) 121 | ) 122 | 123 | 124 | @pytest.mark.parametrize('i', range(5)) 125 | def test_sub(i): 126 | assert eq( 127 | var.a[i], 128 | ast.Subscript( 129 | value=ast.Name(id='a', ctx=ast.Load()), 130 | slice=ast.Index(value=ast.Num(n=i)), 131 | ctx=ast.Load() 132 | ) 133 | ) 134 | 135 | 136 | def test_alias(): 137 | assert eq( 138 | alias.foo, 139 | Alias(name='foo', asname=None) 140 | ) 141 | assert sourcify(alias.foo) == 'foo' 142 | assert eq( 143 | alias['foo'].as_('bar'), 144 | Alias(name='foo', asname='bar') 145 | ) 146 | 147 | assert sourcify(alias['foo'].as_('bar')) == 'foo as bar' 148 | 149 | 150 | def test_import_from(): 151 | assert eq( 152 | from_.bar.import_(alias.foo, baz=alias.foo), 153 | ast.ImportFrom( 154 | module='bar', 155 | names=[ 156 | Alias(name='foo', asname=None), 157 | Alias(name='foo', asname='baz') 158 | ], 159 | level=0 160 | ) 161 | ) 162 | lhs = from_.bar.import_('*') 163 | rhs = ast.ImportFrom( 164 | module='bar', 165 | names=[Alias(name='*', asname=None)], 166 | level=0 167 | ) 168 | assert eq(lhs, rhs) 169 | 170 | 171 | def test_import(): 172 | assert eq( 173 | import_(alias.bar), 174 | ast.Import(names=[Alias(name='bar', asname=None)]) 175 | ) 176 | assert eq(import_('bar'), import_(alias.bar)) 177 | assert sourcify(import_('bar')) == 'import bar' 178 | 179 | 180 | def test_constants(): 181 | assert eq(NONE, ast.NameConstant(value=None)) 182 | assert eq(TRUE, ast.NameConstant(value=True)) 183 | assert eq(FALSE, ast.NameConstant(value=False)) 184 | 185 | 186 | def test_classdef(): 187 | 188 | myklass = class_.Yuge(var.object, metaclass=var.object)[ 189 | def_.method1(arg.self, arg.a)[ 190 | if_(var.a == 1)[return_(var.a + 1)].else_[return_(1)] 191 | ] 192 | ] 193 | s = sourcify(myklass) 194 | assert s == """\ 195 | class Yuge(object, metaclass=object): 196 | def method1(self, a): 197 | if a == 1: 198 | return a + 1 199 | else: 200 | return 1""" 201 | 202 | 203 | def test_while(): 204 | loop = while_(var.x < var.y)[ 205 | pass_ 206 | ] 207 | assert sourcify(loop) == """\ 208 | while x < y: 209 | pass""" 210 | assert loop is not None 211 | 212 | 213 | def test_for(): 214 | loop = for_(var.x).in_(var.y)[ 215 | var.print(1) 216 | ] 217 | assert sourcify(loop) == """\ 218 | for x in y: 219 | print(1)""" 220 | assert loop is not None 221 | 222 | 223 | def test_complex_class(): 224 | klass = class_.Average[ 225 | def_.__init__(arg.self)[ 226 | var.self.value.store(0.0), 227 | var.self.count.store(0), 228 | ], 229 | def_.step(arg.self, arg.value)[ 230 | if_(var.value.is_not(NONE))[ 231 | var.self.value.iadd(var.value), 232 | var.self.count.iadd(1) 233 | ] 234 | ], 235 | def_.finalize(arg.self)[ 236 | if_(var.self.count)[ 237 | return_(var.self.value / var.self.count) 238 | ] 239 | ] 240 | ] 241 | assert sourcify(klass) == """\ 242 | class Average: 243 | def __init__(self): 244 | self.value = 0.0 245 | self.count = 0 246 | 247 | def step(self, value): 248 | if value is not None: 249 | self.value += value 250 | self.count += 1 251 | 252 | def finalize(self): 253 | if self.count: 254 | return self.value / self.count""" 255 | 256 | 257 | def test_exec_class(): 258 | klass = class_.Average[ 259 | def_.__init__(arg.self)[ 260 | var.self.value.store(0.0) 261 | ] 262 | ] 263 | exec(compile(mod(klass), __file__, 'exec')) 264 | assert 'Average' in locals() 265 | 266 | 267 | def test_store_compile(): 268 | expr = mod(var.self.x.store(0)) 269 | assert sourcify(expr) == 'self.x = 0' 270 | assert ast.dump(expr) == ast.dump(ast.parse('self.x = 0')) 271 | 272 | 273 | def test_lambda(): 274 | func = lambda_(arg.x, arg.y)[var.x + 1 * var.y] 275 | assert sourcify(func) == 'lambda x, y: x + 1 * y' 276 | 277 | 278 | def test_args_kwargs(): 279 | func = def_.my_func(*arg.args, **arg.kwargs)[ 280 | var.print(1) 281 | ] 282 | result = sourcify(func) 283 | assert result == """ 284 | def my_func(*args, **kwargs): 285 | print(1)""" 286 | 287 | 288 | def test_yield(): 289 | func = def_.my_func(arg.x)[ 290 | yield_(var.x) 291 | ] 292 | result = sourcify(func) 293 | assert result == """ 294 | def my_func(x): 295 | yield x""" 296 | 297 | 298 | def test_yield_from(): 299 | func = def_.my_func(arg.x)[ 300 | yield_from(var.x) 301 | ] 302 | result = sourcify(func) 303 | assert result == """ 304 | def my_func(x): 305 | yield from x""" 306 | 307 | 308 | @pytest.mark.parametrize( 309 | ('expr', 'expected'), 310 | [ 311 | ( 312 | var[var.a, var.b, var.c].store((1, 2, 3)), 313 | '(a, b, c) = (1, 2, 3)' 314 | ), 315 | ( 316 | var[var.self.a, var.x.b[0].c[var.r.a[var.i]]].store((1, 2)), 317 | '(self.a, x.b[0].c[r.a[i]]) = (1, 2)' 318 | ) 319 | ] 320 | ) 321 | def test_multiple_assignment(expr, expected): 322 | result = sourcify(expr) 323 | assert result == expected 324 | 325 | 326 | def n(value): 327 | return ast.Num(n=value) 328 | 329 | 330 | @pytest.mark.parametrize( 331 | ('value', 'expected'), 332 | [ 333 | ((), Tuple(elts=[], ctx=ast.Load())), 334 | ((1, 2, 3), Tuple(elts=[n(1), n(2), n(3)], ctx=ast.Load())), 335 | 336 | ([], ast.List(elts=[], ctx=ast.Load())), 337 | ([1, 2, 3], ast.List(elts=[n(1), n(2), n(3)], ctx=ast.Load())), 338 | 339 | ({}, ast.Dict(keys=[], values=[])), 340 | ({1: 2, 3: 4}, ast.Dict(keys=[n(1), n(3)], values=[n(2), n(4)])), 341 | 342 | (set(), ast.Set(elts=[])), 343 | ({1, 2, 3}, ast.Set(elts=[n(1), n(2), n(3)])), 344 | ] 345 | ) 346 | def test_to_node(value, expected): 347 | result = to_node(value) 348 | assert eq(expected, result) 349 | 350 | 351 | def test_try_finally(): 352 | stmt = try_( 353 | var.print(2) 354 | ).finally_( 355 | var.print(1) 356 | ) 357 | result = sourcify(stmt) 358 | expected = """\ 359 | try: 360 | print(2) 361 | finally: 362 | print(1)""" 363 | assert result == expected 364 | 365 | 366 | def test_try_except(): 367 | stmt = try_( 368 | var.print(2) 369 | ).except_(var.TypeError)[ 370 | var.print(1) 371 | ] 372 | result = sourcify(stmt) 373 | expected = """\ 374 | try: 375 | print(2) 376 | except TypeError: 377 | print(1)""" 378 | assert result == expected 379 | 380 | 381 | def test_try_except_multiple(): 382 | stmt = try_( 383 | var.print(2) 384 | ).except_(var.TypeError)[ 385 | var.print(3) 386 | ].except_(var.ValueError)[ 387 | var.print(1) 388 | ] 389 | result = sourcify(stmt) 390 | expected = """\ 391 | try: 392 | print(2) 393 | except TypeError: 394 | print(3) 395 | except ValueError: 396 | print(1)""" 397 | assert result == expected 398 | 399 | 400 | def test_try_except_finally(): 401 | stmt = try_( 402 | var.x.store(1) 403 | ).except_(var.TypeError)[ 404 | var.y.store(2) 405 | ].finally_( 406 | return_(3) 407 | ) 408 | result = sourcify(stmt) 409 | expected = """\ 410 | try: 411 | x = 1 412 | except TypeError: 413 | y = 2 414 | finally: 415 | return 3""" 416 | assert result == expected 417 | 418 | 419 | def test_try_except_else_finally(): 420 | stmt = try_( 421 | var.x.store(1) 422 | ).except_(var.TypeError)[ 423 | var.y.store(2) 424 | ].except_(var.ValueError)[ 425 | var.y.store(3) 426 | ].except_(var.OSError, var.RuntimeError, as_='foo')[ 427 | pass_, 428 | ].else_( 429 | var.x.iadd(1) 430 | ).finally_( 431 | var.print(1, 2, 3) 432 | ) 433 | expected = """\ 434 | try: 435 | x = 1 436 | except TypeError: 437 | y = 2 438 | except ValueError: 439 | y = 3 440 | except (OSError, RuntimeError) as foo: 441 | pass 442 | else: 443 | x += 1 444 | finally: 445 | print(1, 2, 3)""" 446 | result = sourcify(stmt) 447 | assert result == expected 448 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | 2 | Apache License 3 | Version 2.0, January 2004 4 | http://www.apache.org/licenses/ 5 | 6 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 7 | 8 | 1. Definitions. 9 | 10 | "License" shall mean the terms and conditions for use, reproduction, 11 | and distribution as defined by Sections 1 through 9 of this document. 12 | 13 | "Licensor" shall mean the copyright owner or entity authorized by 14 | the copyright owner that is granting the License. 15 | 16 | "Legal Entity" shall mean the union of the acting entity and all 17 | other entities that control, are controlled by, or are under common 18 | control with that entity. For the purposes of this definition, 19 | "control" means (i) the power, direct or indirect, to cause the 20 | direction or management of such entity, whether by contract or 21 | otherwise, or (ii) ownership of fifty percent (50%) or more of the 22 | outstanding shares, or (iii) beneficial ownership of such entity. 23 | 24 | "You" (or "Your") shall mean an individual or Legal Entity 25 | exercising permissions granted by this License. 26 | 27 | "Source" form shall mean the preferred form for making modifications, 28 | including but not limited to software source code, documentation 29 | source, and configuration files. 30 | 31 | "Object" form shall mean any form resulting from mechanical 32 | transformation or translation of a Source form, including but 33 | not limited to compiled object code, generated documentation, 34 | and conversions to other media types. 35 | 36 | "Work" shall mean the work of authorship, whether in Source or 37 | Object form, made available under the License, as indicated by a 38 | copyright notice that is included in or attached to the work 39 | (an example is provided in the Appendix below). 40 | 41 | "Derivative Works" shall mean any work, whether in Source or Object 42 | form, that is based on (or derived from) the Work and for which the 43 | editorial revisions, annotations, elaborations, or other modifications 44 | represent, as a whole, an original work of authorship. For the purposes 45 | of this License, Derivative Works shall not include works that remain 46 | separable from, or merely link (or bind by name) to the interfaces of, 47 | the Work and Derivative Works thereof. 48 | 49 | "Contribution" shall mean any work of authorship, including 50 | the original version of the Work and any modifications or additions 51 | to that Work or Derivative Works thereof, that is intentionally 52 | submitted to Licensor for inclusion in the Work by the copyright owner 53 | or by an individual or Legal Entity authorized to submit on behalf of 54 | the copyright owner. For the purposes of this definition, "submitted" 55 | means any form of electronic, verbal, or written communication sent 56 | to the Licensor or its representatives, including but not limited to 57 | communication on electronic mailing lists, source code control systems, 58 | and issue tracking systems that are managed by, or on behalf of, the 59 | Licensor for the purpose of discussing and improving the Work, but 60 | excluding communication that is conspicuously marked or otherwise 61 | designated in writing by the copyright owner as "Not a Contribution." 62 | 63 | "Contributor" shall mean Licensor and any individual or Legal Entity 64 | on behalf of whom a Contribution has been received by Licensor and 65 | subsequently incorporated within the Work. 66 | 67 | 2. Grant of Copyright License. Subject to the terms and conditions of 68 | this License, each Contributor hereby grants to You a perpetual, 69 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 70 | copyright license to reproduce, prepare Derivative Works of, 71 | publicly display, publicly perform, sublicense, and distribute the 72 | Work and such Derivative Works in Source or Object form. 73 | 74 | 3. Grant of Patent License. Subject to the terms and conditions of 75 | this License, each Contributor hereby grants to You a perpetual, 76 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 77 | (except as stated in this section) patent license to make, have made, 78 | use, offer to sell, sell, import, and otherwise transfer the Work, 79 | where such license applies only to those patent claims licensable 80 | by such Contributor that are necessarily infringed by their 81 | Contribution(s) alone or by combination of their Contribution(s) 82 | with the Work to which such Contribution(s) was submitted. If You 83 | institute patent litigation against any entity (including a 84 | cross-claim or counterclaim in a lawsuit) alleging that the Work 85 | or a Contribution incorporated within the Work constitutes direct 86 | or contributory patent infringement, then any patent licenses 87 | granted to You under this License for that Work shall terminate 88 | as of the date such litigation is filed. 89 | 90 | 4. Redistribution. You may reproduce and distribute copies of the 91 | Work or Derivative Works thereof in any medium, with or without 92 | modifications, and in Source or Object form, provided that You 93 | meet the following conditions: 94 | 95 | (a) You must give any other recipients of the Work or 96 | Derivative Works a copy of this License; and 97 | 98 | (b) You must cause any modified files to carry prominent notices 99 | stating that You changed the files; and 100 | 101 | (c) You must retain, in the Source form of any Derivative Works 102 | that You distribute, all copyright, patent, trademark, and 103 | attribution notices from the Source form of the Work, 104 | excluding those notices that do not pertain to any part of 105 | the Derivative Works; and 106 | 107 | (d) If the Work includes a "NOTICE" text file as part of its 108 | distribution, then any Derivative Works that You distribute must 109 | include a readable copy of the attribution notices contained 110 | within such NOTICE file, excluding those notices that do not 111 | pertain to any part of the Derivative Works, in at least one 112 | of the following places: within a NOTICE text file distributed 113 | as part of the Derivative Works; within the Source form or 114 | documentation, if provided along with the Derivative Works; or, 115 | within a display generated by the Derivative Works, if and 116 | wherever such third-party notices normally appear. The contents 117 | of the NOTICE file are for informational purposes only and 118 | do not modify the License. You may add Your own attribution 119 | notices within Derivative Works that You distribute, alongside 120 | or as an addendum to the NOTICE text from the Work, provided 121 | that such additional attribution notices cannot be construed 122 | as modifying the License. 123 | 124 | You may add Your own copyright statement to Your modifications and 125 | may provide additional or different license terms and conditions 126 | for use, reproduction, or distribution of Your modifications, or 127 | for any such Derivative Works as a whole, provided Your use, 128 | reproduction, and distribution of the Work otherwise complies with 129 | the conditions stated in this License. 130 | 131 | 5. Submission of Contributions. Unless You explicitly state otherwise, 132 | any Contribution intentionally submitted for inclusion in the Work 133 | by You to the Licensor shall be under the terms and conditions of 134 | this License, without any additional terms or conditions. 135 | Notwithstanding the above, nothing herein shall supersede or modify 136 | the terms of any separate license agreement you may have executed 137 | with Licensor regarding such Contributions. 138 | 139 | 6. Trademarks. This License does not grant permission to use the trade 140 | names, trademarks, service marks, or product names of the Licensor, 141 | except as required for reasonable and customary use in describing the 142 | origin of the Work and reproducing the content of the NOTICE file. 143 | 144 | 7. Disclaimer of Warranty. Unless required by applicable law or 145 | agreed to in writing, Licensor provides the Work (and each 146 | Contributor provides its Contributions) on an "AS IS" BASIS, 147 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 148 | implied, including, without limitation, any warranties or conditions 149 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A 150 | PARTICULAR PURPOSE. You are solely responsible for determining the 151 | appropriateness of using or redistributing the Work and assume any 152 | risks associated with Your exercise of permissions under this License. 153 | 154 | 8. Limitation of Liability. In no event and under no legal theory, 155 | whether in tort (including negligence), contract, or otherwise, 156 | unless required by applicable law (such as deliberate and grossly 157 | negligent acts) or agreed to in writing, shall any Contributor be 158 | liable to You for damages, including any direct, indirect, special, 159 | incidental, or consequential damages of any character arising as a 160 | result of this License or out of the use or inability to use the 161 | Work (including but not limited to damages for loss of goodwill, 162 | work stoppage, computer failure or malfunction, or any and all 163 | other commercial damages or losses), even if such Contributor 164 | has been advised of the possibility of such damages. 165 | 166 | 9. Accepting Warranty or Additional Liability. While redistributing 167 | the Work or Derivative Works thereof, You may choose to offer, 168 | and charge a fee for, acceptance of support, warranty, indemnity, 169 | or other liability obligations and/or rights consistent with this 170 | License. However, in accepting such obligations, You may act only 171 | on Your own behalf and on Your sole responsibility, not on behalf 172 | of any other Contributor, and only if You agree to indemnify, 173 | defend, and hold each Contributor harmless for any liability 174 | incurred by, or claims asserted against, such Contributor by reason 175 | of your accepting any such warranty or additional liability. 176 | 177 | END OF TERMS AND CONDITIONS 178 | 179 | APPENDIX: How to apply the Apache License to your work. 180 | 181 | To apply the Apache License to your work, attach the following 182 | boilerplate notice, with the fields enclosed by brackets "[]" 183 | replaced with your own identifying information. (Don't include 184 | the brackets!) The text should be enclosed in the appropriate 185 | comment syntax for the file format. We also recommend that a 186 | file or class name and description of purpose be included on the 187 | same "printed page" as the copyright notice for easier 188 | identification within third-party archives. 189 | 190 | Copyright 2018 Charles Phillip Cloud 191 | 192 | Licensed under the Apache License, Version 2.0 (the "License"); 193 | you may not use this file except in compliance with the License. 194 | You may obtain a copy of the License at 195 | 196 | http://www.apache.org/licenses/LICENSE-2.0 197 | 198 | Unless required by applicable law or agreed to in writing, software 199 | distributed under the License is distributed on an "AS IS" BASIS, 200 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 201 | See the License for the specific language governing permissions and 202 | limitations under the License. 203 | -------------------------------------------------------------------------------- /miniast/_version.py: -------------------------------------------------------------------------------- 1 | 2 | # This file helps to compute a version number in source trees obtained from 3 | # git-archive tarball (such as those provided by githubs download-from-tag 4 | # feature). Distribution tarballs (built by setup.py sdist) and build 5 | # directories (produced by setup.py build) will contain a much shorter file 6 | # that just contains the computed version number. 7 | 8 | # This file is released into the public domain. Generated by 9 | # versioneer-0.18 (https://github.com/warner/python-versioneer) 10 | 11 | """Git implementation of _version.py.""" 12 | 13 | import errno 14 | import os 15 | import re 16 | import subprocess 17 | import sys 18 | 19 | 20 | def get_keywords(): 21 | """Get the keywords needed to look up the version information.""" 22 | # these strings will be replaced by git during git-archive. 23 | # setup.py/versioneer.py will grep for the variable names, so they must 24 | # each be defined on a line of their own. _version.py will just call 25 | # get_keywords(). 26 | git_refnames = " (HEAD -> master)" 27 | git_full = "8677752f70a1bbfc9dee3c3a742ea99790b9659e" 28 | git_date = "2018-03-28 11:15:20 -0400" 29 | keywords = {"refnames": git_refnames, "full": git_full, "date": git_date} 30 | return keywords 31 | 32 | 33 | class VersioneerConfig: 34 | """Container for Versioneer configuration parameters.""" 35 | 36 | 37 | def get_config(): 38 | """Create, populate and return the VersioneerConfig() object.""" 39 | # these strings are filled in when 'setup.py versioneer' creates 40 | # _version.py 41 | cfg = VersioneerConfig() 42 | cfg.VCS = "git" 43 | cfg.style = "pep440" 44 | cfg.tag_prefix = "" 45 | cfg.parentdir_prefix = "miniast-" 46 | cfg.versionfile_source = "miniast/_version.py" 47 | cfg.verbose = False 48 | return cfg 49 | 50 | 51 | class NotThisMethod(Exception): 52 | """Exception raised if a method is not valid for the current scenario.""" 53 | 54 | 55 | LONG_VERSION_PY = {} 56 | HANDLERS = {} 57 | 58 | 59 | def register_vcs_handler(vcs, method): # decorator 60 | """Decorator to mark a method as the handler for a particular VCS.""" 61 | def decorate(f): 62 | """Store f in HANDLERS[vcs][method].""" 63 | if vcs not in HANDLERS: 64 | HANDLERS[vcs] = {} 65 | HANDLERS[vcs][method] = f 66 | return f 67 | return decorate 68 | 69 | 70 | def run_command(commands, args, cwd=None, verbose=False, hide_stderr=False, 71 | env=None): 72 | """Call the given command(s).""" 73 | assert isinstance(commands, list) 74 | p = None 75 | for c in commands: 76 | try: 77 | dispcmd = str([c] + args) 78 | # remember shell=False, so use git.cmd on windows, not just git 79 | p = subprocess.Popen([c] + args, cwd=cwd, env=env, 80 | stdout=subprocess.PIPE, 81 | stderr=(subprocess.PIPE if hide_stderr 82 | else None)) 83 | break 84 | except EnvironmentError: 85 | e = sys.exc_info()[1] 86 | if e.errno == errno.ENOENT: 87 | continue 88 | if verbose: 89 | print("unable to run %s" % dispcmd) 90 | print(e) 91 | return None, None 92 | else: 93 | if verbose: 94 | print("unable to find command, tried %s" % (commands,)) 95 | return None, None 96 | stdout = p.communicate()[0].strip() 97 | if sys.version_info[0] >= 3: 98 | stdout = stdout.decode() 99 | if p.returncode != 0: 100 | if verbose: 101 | print("unable to run %s (error)" % dispcmd) 102 | print("stdout was %s" % stdout) 103 | return None, p.returncode 104 | return stdout, p.returncode 105 | 106 | 107 | def versions_from_parentdir(parentdir_prefix, root, verbose): 108 | """Try to determine the version from the parent directory name. 109 | 110 | Source tarballs conventionally unpack into a directory that includes both 111 | the project name and a version string. We will also support searching up 112 | two directory levels for an appropriately named parent directory 113 | """ 114 | rootdirs = [] 115 | 116 | for i in range(3): 117 | dirname = os.path.basename(root) 118 | if dirname.startswith(parentdir_prefix): 119 | return {"version": dirname[len(parentdir_prefix):], 120 | "full-revisionid": None, 121 | "dirty": False, "error": None, "date": None} 122 | else: 123 | rootdirs.append(root) 124 | root = os.path.dirname(root) # up a level 125 | 126 | if verbose: 127 | print("Tried directories %s but none started with prefix %s" % 128 | (str(rootdirs), parentdir_prefix)) 129 | raise NotThisMethod("rootdir doesn't start with parentdir_prefix") 130 | 131 | 132 | @register_vcs_handler("git", "get_keywords") 133 | def git_get_keywords(versionfile_abs): 134 | """Extract version information from the given file.""" 135 | # the code embedded in _version.py can just fetch the value of these 136 | # keywords. When used from setup.py, we don't want to import _version.py, 137 | # so we do it with a regexp instead. This function is not used from 138 | # _version.py. 139 | keywords = {} 140 | try: 141 | f = open(versionfile_abs, "r") 142 | for line in f.readlines(): 143 | if line.strip().startswith("git_refnames ="): 144 | mo = re.search(r'=\s*"(.*)"', line) 145 | if mo: 146 | keywords["refnames"] = mo.group(1) 147 | if line.strip().startswith("git_full ="): 148 | mo = re.search(r'=\s*"(.*)"', line) 149 | if mo: 150 | keywords["full"] = mo.group(1) 151 | if line.strip().startswith("git_date ="): 152 | mo = re.search(r'=\s*"(.*)"', line) 153 | if mo: 154 | keywords["date"] = mo.group(1) 155 | f.close() 156 | except EnvironmentError: 157 | pass 158 | return keywords 159 | 160 | 161 | @register_vcs_handler("git", "keywords") 162 | def git_versions_from_keywords(keywords, tag_prefix, verbose): 163 | """Get version information from git keywords.""" 164 | if not keywords: 165 | raise NotThisMethod("no keywords at all, weird") 166 | date = keywords.get("date") 167 | if date is not None: 168 | # git-2.2.0 added "%cI", which expands to an ISO-8601 -compliant 169 | # datestamp. However we prefer "%ci" (which expands to an "ISO-8601 170 | # -like" string, which we must then edit to make compliant), because 171 | # it's been around since git-1.5.3, and it's too difficult to 172 | # discover which version we're using, or to work around using an 173 | # older one. 174 | date = date.strip().replace(" ", "T", 1).replace(" ", "", 1) 175 | refnames = keywords["refnames"].strip() 176 | if refnames.startswith("$Format"): 177 | if verbose: 178 | print("keywords are unexpanded, not using") 179 | raise NotThisMethod("unexpanded keywords, not a git-archive tarball") 180 | refs = set([r.strip() for r in refnames.strip("()").split(",")]) 181 | # starting in git-1.8.3, tags are listed as "tag: foo-1.0" instead of 182 | # just "foo-1.0". If we see a "tag: " prefix, prefer those. 183 | TAG = "tag: " 184 | tags = set([r[len(TAG):] for r in refs if r.startswith(TAG)]) 185 | if not tags: 186 | # Either we're using git < 1.8.3, or there really are no tags. We use 187 | # a heuristic: assume all version tags have a digit. The old git %d 188 | # expansion behaves like git log --decorate=short and strips out the 189 | # refs/heads/ and refs/tags/ prefixes that would let us distinguish 190 | # between branches and tags. By ignoring refnames without digits, we 191 | # filter out many common branch names like "release" and 192 | # "stabilization", as well as "HEAD" and "master". 193 | tags = set([r for r in refs if re.search(r'\d', r)]) 194 | if verbose: 195 | print("discarding '%s', no digits" % ",".join(refs - tags)) 196 | if verbose: 197 | print("likely tags: %s" % ",".join(sorted(tags))) 198 | for ref in sorted(tags): 199 | # sorting will prefer e.g. "2.0" over "2.0rc1" 200 | if ref.startswith(tag_prefix): 201 | r = ref[len(tag_prefix):] 202 | if verbose: 203 | print("picking %s" % r) 204 | return {"version": r, 205 | "full-revisionid": keywords["full"].strip(), 206 | "dirty": False, "error": None, 207 | "date": date} 208 | # no suitable tags, so version is "0+unknown", but full hex is still there 209 | if verbose: 210 | print("no suitable tags, using unknown + full revision id") 211 | return {"version": "0+unknown", 212 | "full-revisionid": keywords["full"].strip(), 213 | "dirty": False, "error": "no suitable tags", "date": None} 214 | 215 | 216 | @register_vcs_handler("git", "pieces_from_vcs") 217 | def git_pieces_from_vcs(tag_prefix, root, verbose, run_command=run_command): 218 | """Get version from 'git describe' in the root of the source tree. 219 | 220 | This only gets called if the git-archive 'subst' keywords were *not* 221 | expanded, and _version.py hasn't already been rewritten with a short 222 | version string, meaning we're inside a checked out source tree. 223 | """ 224 | GITS = ["git"] 225 | if sys.platform == "win32": 226 | GITS = ["git.cmd", "git.exe"] 227 | 228 | out, rc = run_command(GITS, ["rev-parse", "--git-dir"], cwd=root, 229 | hide_stderr=True) 230 | if rc != 0: 231 | if verbose: 232 | print("Directory %s not under git control" % root) 233 | raise NotThisMethod("'git rev-parse --git-dir' returned error") 234 | 235 | # if there is a tag matching tag_prefix, this yields TAG-NUM-gHEX[-dirty] 236 | # if there isn't one, this yields HEX[-dirty] (no NUM) 237 | describe_out, rc = run_command(GITS, ["describe", "--tags", "--dirty", 238 | "--always", "--long", 239 | "--match", "%s*" % tag_prefix], 240 | cwd=root) 241 | # --long was added in git-1.5.5 242 | if describe_out is None: 243 | raise NotThisMethod("'git describe' failed") 244 | describe_out = describe_out.strip() 245 | full_out, rc = run_command(GITS, ["rev-parse", "HEAD"], cwd=root) 246 | if full_out is None: 247 | raise NotThisMethod("'git rev-parse' failed") 248 | full_out = full_out.strip() 249 | 250 | pieces = {} 251 | pieces["long"] = full_out 252 | pieces["short"] = full_out[:7] # maybe improved later 253 | pieces["error"] = None 254 | 255 | # parse describe_out. It will be like TAG-NUM-gHEX[-dirty] or HEX[-dirty] 256 | # TAG might have hyphens. 257 | git_describe = describe_out 258 | 259 | # look for -dirty suffix 260 | dirty = git_describe.endswith("-dirty") 261 | pieces["dirty"] = dirty 262 | if dirty: 263 | git_describe = git_describe[:git_describe.rindex("-dirty")] 264 | 265 | # now we have TAG-NUM-gHEX or HEX 266 | 267 | if "-" in git_describe: 268 | # TAG-NUM-gHEX 269 | mo = re.search(r'^(.+)-(\d+)-g([0-9a-f]+)$', git_describe) 270 | if not mo: 271 | # unparseable. Maybe git-describe is misbehaving? 272 | pieces["error"] = ("unable to parse git-describe output: '%s'" 273 | % describe_out) 274 | return pieces 275 | 276 | # tag 277 | full_tag = mo.group(1) 278 | if not full_tag.startswith(tag_prefix): 279 | if verbose: 280 | fmt = "tag '%s' doesn't start with prefix '%s'" 281 | print(fmt % (full_tag, tag_prefix)) 282 | pieces["error"] = ("tag '%s' doesn't start with prefix '%s'" 283 | % (full_tag, tag_prefix)) 284 | return pieces 285 | pieces["closest-tag"] = full_tag[len(tag_prefix):] 286 | 287 | # distance: number of commits since tag 288 | pieces["distance"] = int(mo.group(2)) 289 | 290 | # commit: short hex revision ID 291 | pieces["short"] = mo.group(3) 292 | 293 | else: 294 | # HEX: no tags 295 | pieces["closest-tag"] = None 296 | count_out, rc = run_command(GITS, ["rev-list", "HEAD", "--count"], 297 | cwd=root) 298 | pieces["distance"] = int(count_out) # total number of commits 299 | 300 | # commit date: see ISO-8601 comment in git_versions_from_keywords() 301 | date = run_command(GITS, ["show", "-s", "--format=%ci", "HEAD"], 302 | cwd=root)[0].strip() 303 | pieces["date"] = date.strip().replace(" ", "T", 1).replace(" ", "", 1) 304 | 305 | return pieces 306 | 307 | 308 | def plus_or_dot(pieces): 309 | """Return a + if we don't already have one, else return a .""" 310 | if "+" in pieces.get("closest-tag", ""): 311 | return "." 312 | return "+" 313 | 314 | 315 | def render_pep440(pieces): 316 | """Build up version string, with post-release "local version identifier". 317 | 318 | Our goal: TAG[+DISTANCE.gHEX[.dirty]] . Note that if you 319 | get a tagged build and then dirty it, you'll get TAG+0.gHEX.dirty 320 | 321 | Exceptions: 322 | 1: no tags. git_describe was just HEX. 0+untagged.DISTANCE.gHEX[.dirty] 323 | """ 324 | if pieces["closest-tag"]: 325 | rendered = pieces["closest-tag"] 326 | if pieces["distance"] or pieces["dirty"]: 327 | rendered += plus_or_dot(pieces) 328 | rendered += "%d.g%s" % (pieces["distance"], pieces["short"]) 329 | if pieces["dirty"]: 330 | rendered += ".dirty" 331 | else: 332 | # exception #1 333 | rendered = "0+untagged.%d.g%s" % (pieces["distance"], 334 | pieces["short"]) 335 | if pieces["dirty"]: 336 | rendered += ".dirty" 337 | return rendered 338 | 339 | 340 | def render_pep440_pre(pieces): 341 | """TAG[.post.devDISTANCE] -- No -dirty. 342 | 343 | Exceptions: 344 | 1: no tags. 0.post.devDISTANCE 345 | """ 346 | if pieces["closest-tag"]: 347 | rendered = pieces["closest-tag"] 348 | if pieces["distance"]: 349 | rendered += ".post.dev%d" % pieces["distance"] 350 | else: 351 | # exception #1 352 | rendered = "0.post.dev%d" % pieces["distance"] 353 | return rendered 354 | 355 | 356 | def render_pep440_post(pieces): 357 | """TAG[.postDISTANCE[.dev0]+gHEX] . 358 | 359 | The ".dev0" means dirty. Note that .dev0 sorts backwards 360 | (a dirty tree will appear "older" than the corresponding clean one), 361 | but you shouldn't be releasing software with -dirty anyways. 362 | 363 | Exceptions: 364 | 1: no tags. 0.postDISTANCE[.dev0] 365 | """ 366 | if pieces["closest-tag"]: 367 | rendered = pieces["closest-tag"] 368 | if pieces["distance"] or pieces["dirty"]: 369 | rendered += ".post%d" % pieces["distance"] 370 | if pieces["dirty"]: 371 | rendered += ".dev0" 372 | rendered += plus_or_dot(pieces) 373 | rendered += "g%s" % pieces["short"] 374 | else: 375 | # exception #1 376 | rendered = "0.post%d" % pieces["distance"] 377 | if pieces["dirty"]: 378 | rendered += ".dev0" 379 | rendered += "+g%s" % pieces["short"] 380 | return rendered 381 | 382 | 383 | def render_pep440_old(pieces): 384 | """TAG[.postDISTANCE[.dev0]] . 385 | 386 | The ".dev0" means dirty. 387 | 388 | Eexceptions: 389 | 1: no tags. 0.postDISTANCE[.dev0] 390 | """ 391 | if pieces["closest-tag"]: 392 | rendered = pieces["closest-tag"] 393 | if pieces["distance"] or pieces["dirty"]: 394 | rendered += ".post%d" % pieces["distance"] 395 | if pieces["dirty"]: 396 | rendered += ".dev0" 397 | else: 398 | # exception #1 399 | rendered = "0.post%d" % pieces["distance"] 400 | if pieces["dirty"]: 401 | rendered += ".dev0" 402 | return rendered 403 | 404 | 405 | def render_git_describe(pieces): 406 | """TAG[-DISTANCE-gHEX][-dirty]. 407 | 408 | Like 'git describe --tags --dirty --always'. 409 | 410 | Exceptions: 411 | 1: no tags. HEX[-dirty] (note: no 'g' prefix) 412 | """ 413 | if pieces["closest-tag"]: 414 | rendered = pieces["closest-tag"] 415 | if pieces["distance"]: 416 | rendered += "-%d-g%s" % (pieces["distance"], pieces["short"]) 417 | else: 418 | # exception #1 419 | rendered = pieces["short"] 420 | if pieces["dirty"]: 421 | rendered += "-dirty" 422 | return rendered 423 | 424 | 425 | def render_git_describe_long(pieces): 426 | """TAG-DISTANCE-gHEX[-dirty]. 427 | 428 | Like 'git describe --tags --dirty --always -long'. 429 | The distance/hash is unconditional. 430 | 431 | Exceptions: 432 | 1: no tags. HEX[-dirty] (note: no 'g' prefix) 433 | """ 434 | if pieces["closest-tag"]: 435 | rendered = pieces["closest-tag"] 436 | rendered += "-%d-g%s" % (pieces["distance"], pieces["short"]) 437 | else: 438 | # exception #1 439 | rendered = pieces["short"] 440 | if pieces["dirty"]: 441 | rendered += "-dirty" 442 | return rendered 443 | 444 | 445 | def render(pieces, style): 446 | """Render the given version pieces into the requested style.""" 447 | if pieces["error"]: 448 | return {"version": "unknown", 449 | "full-revisionid": pieces.get("long"), 450 | "dirty": None, 451 | "error": pieces["error"], 452 | "date": None} 453 | 454 | if not style or style == "default": 455 | style = "pep440" # the default 456 | 457 | if style == "pep440": 458 | rendered = render_pep440(pieces) 459 | elif style == "pep440-pre": 460 | rendered = render_pep440_pre(pieces) 461 | elif style == "pep440-post": 462 | rendered = render_pep440_post(pieces) 463 | elif style == "pep440-old": 464 | rendered = render_pep440_old(pieces) 465 | elif style == "git-describe": 466 | rendered = render_git_describe(pieces) 467 | elif style == "git-describe-long": 468 | rendered = render_git_describe_long(pieces) 469 | else: 470 | raise ValueError("unknown style '%s'" % style) 471 | 472 | return {"version": rendered, "full-revisionid": pieces["long"], 473 | "dirty": pieces["dirty"], "error": None, 474 | "date": pieces.get("date")} 475 | 476 | 477 | def get_versions(): 478 | """Get version information or return default if unable to do so.""" 479 | # I am in _version.py, which lives at ROOT/VERSIONFILE_SOURCE. If we have 480 | # __file__, we can work backwards from there to the root. Some 481 | # py2exe/bbfreeze/non-CPython implementations don't do __file__, in which 482 | # case we can only use expanded keywords. 483 | 484 | cfg = get_config() 485 | verbose = cfg.verbose 486 | 487 | try: 488 | return git_versions_from_keywords(get_keywords(), cfg.tag_prefix, 489 | verbose) 490 | except NotThisMethod: 491 | pass 492 | 493 | try: 494 | root = os.path.realpath(__file__) 495 | # versionfile_source is the relative path from the top of the source 496 | # tree (where the .git directory might live) to this file. Invert 497 | # this to find the root from __file__. 498 | for i in cfg.versionfile_source.split('/'): 499 | root = os.path.dirname(root) 500 | except NameError: 501 | return {"version": "0+unknown", "full-revisionid": None, 502 | "dirty": None, 503 | "error": "unable to find root of source tree", 504 | "date": None} 505 | 506 | try: 507 | pieces = git_pieces_from_vcs(cfg.tag_prefix, root, verbose) 508 | return render(pieces, cfg.style) 509 | except NotThisMethod: 510 | pass 511 | 512 | try: 513 | if cfg.parentdir_prefix: 514 | return versions_from_parentdir(cfg.parentdir_prefix, root, verbose) 515 | except NotThisMethod: 516 | pass 517 | 518 | return {"version": "0+unknown", "full-revisionid": None, 519 | "dirty": None, 520 | "error": "unable to compute version", "date": None} 521 | -------------------------------------------------------------------------------- /miniast/base.py: -------------------------------------------------------------------------------- 1 | """Lightweight macros for Python. 2 | """ 3 | 4 | from __future__ import print_function 5 | 6 | import ast 7 | import collections 8 | import copy 9 | import functools 10 | 11 | 12 | class StatementWithBody: 13 | """Implement the square bracket syntax for Class, Def, If, While, Try, and 14 | For statements. 15 | """ 16 | __slots__ = () 17 | 18 | def __getitem__(self, *body): 19 | return self(*to_list(*body)) 20 | 21 | 22 | SWAPPED_ARGUMENT_METHODS = { 23 | '__radd__', 24 | '__rmul__', 25 | } 26 | 27 | 28 | def binary_operations(mapping, func): 29 | """Decorator providing default implementations of binary operations. 30 | 31 | Parameters 32 | ---------- 33 | mapping : collections.Mapping 34 | func : callable 35 | 36 | Returns 37 | ------- 38 | decorator : callable 39 | """ 40 | def decorator(cls): 41 | for method_name, op in mapping.items(): 42 | if method_name in SWAPPED_ARGUMENT_METHODS: 43 | f = functools.partialmethod( 44 | lambda self, other, op: func(to_node(other), self, op), 45 | op=op() 46 | ) 47 | else: 48 | f = functools.partialmethod(func, op=op()) 49 | setattr(cls, method_name, f) 50 | return cls 51 | return decorator 52 | 53 | 54 | @binary_operations( 55 | { 56 | '__eq__': ast.Eq, 57 | '__ne__': ast.NotEq, 58 | '__lt__': ast.Lt, 59 | '__le__': ast.LtE, 60 | '__gt__': ast.Gt, 61 | '__ge__': ast.GtE, 62 | 'is_': ast.Is, 63 | 'is_not': ast.IsNot, 64 | }, 65 | func=lambda self, other, op: ast.Compare( 66 | left=self, ops=[op], comparators=[to_node(other)] 67 | ) 68 | ) 69 | class Comparable: 70 | """Mixin to implement comparison operators. 71 | """ 72 | __slots__ = () 73 | 74 | def __contains__(self, other): 75 | return ast.Compare( 76 | left=to_node(other), ops=[ast.In()], comparators=[self] 77 | ) 78 | 79 | 80 | @binary_operations( 81 | { 82 | '__add__': ast.Add, 83 | '__radd__': ast.Add, 84 | '__sub__': ast.Sub, 85 | '__mul__': ast.Mult, 86 | '__rmul__': ast.Mult, 87 | '__floordiv__': ast.FloorDiv, 88 | '__truediv__': ast.Div, 89 | '__div__': ast.Div, 90 | '__pow__': ast.Pow, 91 | '__lshift__': ast.LShift, 92 | '__rshift__': ast.RShift, 93 | }, 94 | 95 | func=lambda self, other, op: ast.BinOp( 96 | left=self, op=op, right=to_node(other)) 97 | ) 98 | @binary_operations( 99 | { 100 | 'iadd': ast.Add, 101 | 'isub': ast.Sub, 102 | 'imul': ast.Mult, 103 | 'ifloordiv': ast.FloorDiv, 104 | 'idiv': ast.Div, 105 | 'ipow': ast.Pow, 106 | 'ilshift': ast.LShift, 107 | 'irshift': ast.RShift, 108 | }, 109 | func=lambda self, other, op: ast.AugAssign( 110 | target=self, op=op, value=to_node(other) 111 | ) 112 | ) 113 | class BinOp: 114 | """Mixin to implement non-comparison binary operators. 115 | """ 116 | 117 | 118 | class Callable: 119 | """Mixin to generate Call nodes. 120 | """ 121 | def __call__(self, *args, **kwargs): 122 | return ast.Call( 123 | func=self, 124 | args=list(map(to_node, args)), 125 | keywords=[ 126 | ast.keyword(arg=key, value=value) 127 | for key, value in kwargs.items() 128 | ] 129 | ) 130 | 131 | 132 | def s(value): 133 | """Convenience function to generate an ast.Str node. 134 | 135 | Parameters 136 | ---------- 137 | value : str 138 | 139 | Returns 140 | ------- 141 | node : ast.Str 142 | """ 143 | return ast.Str(s=value) 144 | 145 | 146 | class Assignable: 147 | """Mixin to generate Assign nodes. 148 | """ 149 | def store(self, value): 150 | fields = {field: getattr(self, field) for field in self._fields} 151 | fields['ctx'] = ast.Store() 152 | return ast.Assign(targets=[type(self)(**fields)], value=to_node(value)) 153 | 154 | 155 | class Indexable: 156 | """Mixin to generate Subscript nodes. 157 | """ 158 | def __getitem__(self, key): 159 | return Subscript( 160 | value=self, 161 | slice=ast.Index(value=to_node(key)), 162 | ctx=ast.Load() 163 | ) 164 | 165 | 166 | class Attributable: 167 | """Mixin to generate Attribute nodes. 168 | """ 169 | def __getattr__(self, name): 170 | return Attribute(value=self, attr=name, ctx=ast.Load()) 171 | 172 | 173 | class Name( 174 | ast.Name, 175 | Comparable, 176 | BinOp, 177 | Assignable, 178 | Indexable, 179 | Callable, 180 | Attributable 181 | ): 182 | """Represent a Python variable 183 | """ 184 | def __init__(self, id, ctx, lineno=0, col_offset=0): 185 | super().__init__(id=id, ctx=ctx, lineno=lineno, col_offset=col_offset) 186 | 187 | 188 | class Tuple(ast.Tuple, Comparable, BinOp, Assignable, Indexable): 189 | """Represent a Python tuple. 190 | """ 191 | def __init__(self, elts, ctx, lineno=0, col_offset=0): 192 | super().__init__( 193 | elts=elts, ctx=ctx, lineno=lineno, col_offset=col_offset) 194 | 195 | 196 | class Var: 197 | """Generate variable names. 198 | """ 199 | __slots__ = () 200 | 201 | def __getitem__(self, key): 202 | if isinstance(key, tuple): 203 | targets = [ 204 | var[target] if isinstance(target, str) else to_node(target) 205 | for target in key 206 | ] 207 | return Tuple(elts=targets, ctx=ast.Load()) 208 | return Name(id=key, ctx=ast.Load()) 209 | 210 | __getattr__ = __getitem__ 211 | 212 | 213 | var = Var() 214 | 215 | 216 | class Raise: 217 | __slots__ = () 218 | 219 | def __call__(self, exception, cause=None): 220 | return ast.Raise(exc=exception, cause=cause) 221 | 222 | 223 | raise_ = Raise() 224 | 225 | 226 | class SpecialArg(ast.arg): 227 | """Turns out you can spoof *args by defining ``__iter__``, and **kwargs by 228 | defining a ``keys()`` method + ``__getitem__``. 229 | 230 | One wrinkle is that the ``__iter__`` implementation needs to yield a 231 | ``str`` subclass 232 | """ 233 | def __init__(self, arg, annotation=None): 234 | super().__init__(arg=arg, annotation=annotation) 235 | 236 | def __hash__(self): 237 | return hash((type(self), self.arg, self.annotation)) 238 | 239 | def __eq__(self, other): 240 | return self.arg == other.arg and self.annotation == other.annotation 241 | 242 | def __ne__(self, other): 243 | return not (self == other) 244 | 245 | def keys(self): 246 | return collections.KeysView(self) 247 | 248 | def __iter__(self): 249 | yield Args(self.arg) 250 | 251 | def __getitem__(self, key): 252 | if isinstance(key, Args): 253 | return Kwargs(key) 254 | raise TypeError( 255 | '__getitem__ not defined for class {}'.format(type(self).__name__) 256 | ) 257 | 258 | 259 | class Args(str): 260 | pass 261 | 262 | 263 | class Kwargs(SpecialArg): 264 | pass 265 | 266 | 267 | class Arg: 268 | __slots__ = () 269 | 270 | def __getitem__(self, key): 271 | return SpecialArg(arg=key) 272 | 273 | __getattr__ = __getitem__ 274 | 275 | 276 | arg = Arg() 277 | 278 | 279 | def to_node(value): 280 | if isinstance(value, str): 281 | return ast.Str(s=value) 282 | elif isinstance(value, (int, float)): 283 | return ast.Num(n=value) 284 | elif isinstance(value, list): 285 | return ast.List(elts=list(map(to_node, value)), ctx=ast.Load()) 286 | elif isinstance(value, tuple): 287 | return ast.Tuple(elts=list(map(to_node, value)), ctx=ast.Load()) 288 | elif isinstance(value, dict): 289 | keys = list(map(to_node, value.keys())) 290 | values = list(map(to_node, value.values())) 291 | return ast.Dict(keys=keys, values=values) 292 | elif isinstance(value, set): 293 | return ast.Set(elts=list(map(to_node, value))) 294 | assert value is None or isinstance(value, ast.AST), \ 295 | 'value must be None or AST instance, got {}'.format( 296 | type(value).__name__ 297 | ) 298 | return value 299 | 300 | 301 | def to_expr(value): 302 | return value if isinstance(value, ast.stmt) else ast.Expr(value=value) 303 | 304 | 305 | class Attribute( 306 | ast.Attribute, 307 | Assignable, 308 | Indexable, 309 | Comparable, 310 | BinOp, 311 | Callable, 312 | Attributable 313 | ): 314 | 315 | def __init__(self, value, attr, ctx, lineno=0, col_offset=0): 316 | super().__init__( 317 | value=value, 318 | attr=attr, 319 | ctx=ctx, 320 | lineno=lineno, 321 | col_offset=col_offset) 322 | 323 | 324 | class Else(StatementWithBody): 325 | __slots__ = 'ifstmt', 326 | 327 | def __init__(self, ifstmt): 328 | self.ifstmt = ifstmt 329 | 330 | def __call__(self, *orelse): 331 | ifstmt = self.ifstmt 332 | return type(ifstmt)( 333 | ifstmt.test, ifstmt.body, list(map(to_expr, to_list(orelse)))) 334 | 335 | 336 | class If(ast.If): 337 | def __init__(self, test, body, orelse=None): 338 | super().__init__(test=test, body=body, orelse=orelse or []) 339 | 340 | @property 341 | def else_(self): 342 | return Else(self) 343 | 344 | 345 | class IfCond(StatementWithBody): 346 | __slots__ = 'test', 347 | 348 | def __init__(self, test): 349 | self.test = test 350 | 351 | def __call__(self, *body): 352 | return If(test=self.test, body=list(map(to_expr, to_list(body)))) 353 | 354 | 355 | class IfStatement: 356 | """ 357 | if_(cond)( 358 | ).else_( 359 | ) 360 | """ 361 | __slots__ = () 362 | 363 | def __call__(self, test): 364 | return IfCond(test) 365 | 366 | 367 | if_ = IfStatement() 368 | 369 | 370 | class For: 371 | """ 372 | for_(target).in_(iter)( 373 | ) 374 | """ 375 | __slots__ = () 376 | 377 | def __call__(self, target): 378 | return TargetedFor(target) 379 | 380 | 381 | class IteratedFor(StatementWithBody): 382 | 383 | __slots__ = 'target', 'iter' 384 | 385 | def __init__(self, target, iter): 386 | self.target = target 387 | self.iter = iter 388 | 389 | def __call__(self, *body): 390 | return ast.For(target=self.target, iter=self.iter, body=to_list(body)) 391 | 392 | 393 | class TargetedFor: 394 | __slots__ = 'target', 395 | 396 | def __init__(self, target): 397 | self.target = target 398 | 399 | def in_(self, iter): 400 | return IteratedFor(self.target, iter) 401 | 402 | 403 | for_ = For() 404 | 405 | 406 | class WhileBody(StatementWithBody): 407 | 408 | __slots__ = 'test', 409 | 410 | def __init__(self, test): 411 | self.test = test 412 | 413 | def __call__(self, *body): 414 | return ast.While(test=self.test, body=to_list(body)) 415 | 416 | 417 | class While: 418 | __slots__ = () 419 | 420 | def __call__(self, test): 421 | return WhileBody(test) 422 | 423 | 424 | while_ = While() 425 | 426 | 427 | def ifelse(test, body, orelse): 428 | return ast.IfExp(test, body, orelse) 429 | 430 | 431 | class FunctionDeclaration: 432 | 433 | def __getitem__(self, name): 434 | return FunctionDef(name=name) 435 | 436 | __getattribute__ = __getitem__ 437 | 438 | 439 | def_ = FunctionDeclaration() 440 | 441 | 442 | class FunctionSignature(StatementWithBody): 443 | __slots__ = 'name', 'arguments' 444 | 445 | def __init__(self, name, arguments): 446 | self.name = name 447 | self.arguments = arguments 448 | 449 | def __call__(self, *body): 450 | return ast.FunctionDef( 451 | name=self.name, 452 | args=self.arguments, 453 | body=to_list(body), 454 | decorator_list=[], 455 | returns=None 456 | ) 457 | 458 | 459 | class FunctionDef: 460 | __slots__ = 'name', 461 | 462 | def __init__(self, name): 463 | self.name = name 464 | 465 | def __call__(self, *arguments, **kwargs): 466 | varargs = [a for a in arguments if isinstance(a, Args)] 467 | kwargs = list(kwargs.values()) 468 | arguments = [a for a in arguments if not isinstance(a, (Args, dict))] 469 | return FunctionSignature( 470 | self.name, 471 | ast.arguments( 472 | args=list(arguments), 473 | vararg=varargs[0] if varargs else None, 474 | kwonlyargs=[], 475 | kw_defaults=[], 476 | kwarg=kwargs[0] if kwargs else None, 477 | defaults=[], 478 | ) 479 | ) 480 | 481 | 482 | def decorate(*functions): 483 | def wrapper(function_definition): 484 | func_def = copy.copy(function_definition) 485 | func_def.decorator_list = list(functions) 486 | return func_def 487 | return wrapper 488 | 489 | 490 | def mod(*lines): 491 | module = ast.Module(body=list(lines)) 492 | module = ast.fix_missing_locations(module) 493 | return module 494 | 495 | 496 | class Subscript( 497 | ast.Subscript, 498 | Comparable, 499 | BinOp, 500 | Assignable, 501 | Indexable, 502 | Callable, 503 | Attributable 504 | ): 505 | def __init__(self, value, slice, ctx, lineno=0, col_offset=0): 506 | super().__init__( 507 | value=value, 508 | slice=slice, 509 | ctx=ctx, 510 | lineno=lineno, 511 | col_offset=col_offset, 512 | ) 513 | 514 | 515 | TRUE = ast.NameConstant(value=True) 516 | FALSE = ast.NameConstant(value=False) 517 | NONE = ast.NameConstant(value=None) 518 | 519 | 520 | class Alias(ast.alias): 521 | def __init__(self, name, asname=None): 522 | super().__init__(name=name, asname=asname) 523 | 524 | def as_(self, asname): 525 | return type(self)(name=self.name, asname=asname) 526 | 527 | 528 | class AliasGenerator: 529 | """Shorter version of aliases used in `from foo import bar as baz`. 530 | 531 | API 532 | --- 533 | alias.foo == ast.alias(name=name, asname=None) 534 | """ 535 | __slots__ = () 536 | 537 | def __getattribute__(self, name): 538 | return self[name] 539 | 540 | def __getitem__(self, key): 541 | return Alias(name=key) 542 | 543 | 544 | alias = AliasGenerator() 545 | 546 | 547 | class ImportFrom: 548 | __slots__ = () 549 | 550 | def __getitem__(self, key): 551 | return DottedModule(key) 552 | 553 | __getattr__ = __getitem__ 554 | 555 | 556 | class DottedModule: 557 | __slots__ = 'name', 558 | 559 | def __init__(self, name): 560 | self.name = name 561 | 562 | def import_(self, *aliases, **kwargs): 563 | names = list(map(to_alias, aliases)) 564 | names += [ 565 | Alias(name=to_alias(a).name, asname=asname) 566 | for asname, a in kwargs.items() 567 | ] 568 | return ast.ImportFrom(module=self.name, names=names, level=0) 569 | 570 | def __getattr__(self, name): 571 | return DottedModule('{}.{}'.format(self.name, name)) 572 | 573 | 574 | def to_alias(value): 575 | """Convert `value` to an alias if it is not one already. 576 | 577 | Parameters 578 | ---------- 579 | value : Union[str, ast.Alias] 580 | 581 | Returns 582 | ------- 583 | alias : ast.Alias 584 | 585 | Examples 586 | -------- 587 | >>> x = to_alias('x') 588 | >>> x # doctest: +ELLIPSIS 589 | 590 | >>> y = to_alias(alias.y) 591 | >>> y # doctest: +ELLIPSIS 592 | 593 | """ 594 | if not isinstance(value, ast.alias): 595 | return alias[value] 596 | return value 597 | 598 | 599 | class Import: 600 | __slots__ = () 601 | 602 | def __call__(self, *args): 603 | return ast.Import(names=list(map(to_alias, args))) 604 | 605 | def __getitem__(self, key): 606 | return self(*key) if isinstance(key, tuple) else self(key) 607 | 608 | __getattr__ = __getitem__ 609 | 610 | 611 | from_ = ImportFrom() 612 | import_ = Import() 613 | 614 | 615 | class Return: 616 | __slots__ = () 617 | 618 | def __call__(self, value=None): 619 | return ast.Return(value=to_node(value)) 620 | 621 | 622 | return_ = Return() 623 | 624 | 625 | def to_list(key): 626 | if isinstance(key, collections.Iterable) and not isinstance(key, str): 627 | return list(key) 628 | return [key] 629 | 630 | 631 | class ClassConstructible: 632 | __slots__ = () 633 | 634 | def __call__(self, *body): 635 | return ast.ClassDef( 636 | name=self.name, 637 | bases=list(self.bases), 638 | keywords=[ 639 | ast.keyword(arg=arg, value=to_node(value)) 640 | for arg, value in self.keywords.items() 641 | ], 642 | body=to_list(body), 643 | decorator_list=[] 644 | ) 645 | 646 | 647 | class ClassWithArguments(ClassConstructible): 648 | __slots__ = 'name', 'bases', 'keywords' 649 | 650 | def __init__(self, name, *bases, **keywords): 651 | self.name = name 652 | self.bases = bases 653 | self.keywords = keywords 654 | 655 | def __getitem__(self, body): 656 | return super().__call__(*to_list(body)) 657 | 658 | 659 | class ClassDefinition(ClassConstructible): 660 | __slots__ = 'name', 'bases', 'keywords' 661 | 662 | def __init__(self, name): 663 | self.name = name 664 | self.bases = [] 665 | self.keywords = {} 666 | 667 | def __call__(self, *bases, **keywords): 668 | return ClassWithArguments(self.name, *bases, **keywords) 669 | 670 | def __getitem__(self, body): 671 | return super().__call__(*to_list(body)) 672 | 673 | 674 | class ClassDeclaration: 675 | __slots__ = () 676 | 677 | def __getitem__(self, name): 678 | return ClassDefinition(name) 679 | 680 | __getattr__ = __getitem__ 681 | 682 | 683 | class_ = ClassDeclaration() 684 | pass_ = ast.Pass() 685 | 686 | 687 | class LambdaWithSignature: 688 | __slots__ = 'signature', 689 | 690 | def __init__(self, *signature): 691 | self.signature = list(signature) 692 | 693 | def __getitem__(self, expr): 694 | return ast.Lambda(args=ast.arguments(args=self.signature), body=expr) 695 | 696 | 697 | class Lambda: 698 | __slots__ = () 699 | 700 | def __call__(self, *signature): 701 | return LambdaWithSignature(*signature) 702 | 703 | 704 | lambda_ = Lambda() 705 | 706 | 707 | def yield_(value): 708 | return ast.Yield(value=value) 709 | 710 | 711 | def yield_from(value): 712 | return ast.YieldFrom(value=value) 713 | 714 | 715 | class Except: 716 | def except_(self, *type, as_=None): 717 | if len(type) == 1: 718 | type, = type 719 | return TryWithExceptSetup(self, type, name=as_) 720 | 721 | 722 | class Finally: 723 | @property 724 | def finally_(self): 725 | return TryWithFinallySetup(self) 726 | 727 | 728 | class TrySuiteSetup: 729 | def assemble(self): 730 | return self.parent.assemble() 731 | 732 | 733 | class TryWithFinally(ast.stmt): 734 | def __init__(self, parent, *body): 735 | self.parent = parent 736 | self.body = list(body) 737 | 738 | def assemble(self): 739 | try_ = self.parent.assemble() 740 | try_.finalbody = self.body 741 | return try_ 742 | 743 | 744 | class TryWithFinallySetup(StatementWithBody, TrySuiteSetup): 745 | def __init__(self, parent): 746 | self.parent = parent 747 | 748 | def __call__(self, *body): 749 | return TryWithFinally(self, *body) 750 | 751 | 752 | class TryWithElse(ast.stmt, Finally): 753 | def __init__(self, parent, *body): 754 | self.parent = parent 755 | self.body = list(body) 756 | 757 | def assemble(self): 758 | try_ = self.parent.assemble() 759 | try_.orelse = self.body 760 | return try_ 761 | 762 | 763 | class TryWithElseSetup(StatementWithBody, TrySuiteSetup): 764 | def __init__(self, parent): 765 | self.parent = parent 766 | 767 | def __call__(self, *body): 768 | return TryWithElse(self, *body) 769 | 770 | 771 | class TryWithExcept(ast.stmt, Except, Finally): 772 | def __init__(self, parent, *body): 773 | self.parent = parent 774 | self.body = list(body) 775 | 776 | @property 777 | def else_(self): 778 | return TryWithElseSetup(self) 779 | 780 | def assemble(self): 781 | try_ = self.parent.assemble() 782 | handler = ast.ExceptHandler( 783 | type=self.parent.type, name=self.parent.name, body=self.body) 784 | try_.handlers.append(handler) 785 | return try_ 786 | 787 | 788 | class TryWithExceptSetup(StatementWithBody, TrySuiteSetup): 789 | def __init__(self, parent, typ, name): 790 | self.parent = parent 791 | self.type = to_node(typ) 792 | self.name = name 793 | 794 | def __call__(self, *body): 795 | return TryWithExcept(self, *body) 796 | 797 | 798 | class Try(ast.stmt, Except, Finally): 799 | def __init__(self, *body): 800 | self.body = list(body) 801 | 802 | def assemble(self): 803 | return ast.Try(body=self.body, handlers=[], orelse=[], finalbody=[]) 804 | 805 | 806 | class TrySetup(StatementWithBody): 807 | def __call__(self, *body): 808 | return Try(*body) 809 | 810 | 811 | try_ = TrySetup() 812 | -------------------------------------------------------------------------------- /versioneer.py: -------------------------------------------------------------------------------- 1 | 2 | # Version: 0.18 3 | 4 | """The Versioneer - like a rocketeer, but for versions. 5 | 6 | The Versioneer 7 | ============== 8 | 9 | * like a rocketeer, but for versions! 10 | * https://github.com/warner/python-versioneer 11 | * Brian Warner 12 | * License: Public Domain 13 | * Compatible With: python2.6, 2.7, 3.2, 3.3, 3.4, 3.5, 3.6, and pypy 14 | * [![Latest Version] 15 | (https://pypip.in/version/versioneer/badge.svg?style=flat) 16 | ](https://pypi.python.org/pypi/versioneer/) 17 | * [![Build Status] 18 | (https://travis-ci.org/warner/python-versioneer.png?branch=master) 19 | ](https://travis-ci.org/warner/python-versioneer) 20 | 21 | This is a tool for managing a recorded version number in distutils-based 22 | python projects. The goal is to remove the tedious and error-prone "update 23 | the embedded version string" step from your release process. Making a new 24 | release should be as easy as recording a new tag in your version-control 25 | system, and maybe making new tarballs. 26 | 27 | 28 | ## Quick Install 29 | 30 | * `pip install versioneer` to somewhere to your $PATH 31 | * add a `[versioneer]` section to your setup.cfg (see below) 32 | * run `versioneer install` in your source tree, commit the results 33 | 34 | ## Version Identifiers 35 | 36 | Source trees come from a variety of places: 37 | 38 | * a version-control system checkout (mostly used by developers) 39 | * a nightly tarball, produced by build automation 40 | * a snapshot tarball, produced by a web-based VCS browser, like github's 41 | "tarball from tag" feature 42 | * a release tarball, produced by "setup.py sdist", distributed through PyPI 43 | 44 | Within each source tree, the version identifier (either a string or a number, 45 | this tool is format-agnostic) can come from a variety of places: 46 | 47 | * ask the VCS tool itself, e.g. "git describe" (for checkouts), which knows 48 | about recent "tags" and an absolute revision-id 49 | * the name of the directory into which the tarball was unpacked 50 | * an expanded VCS keyword ($Id$, etc) 51 | * a `_version.py` created by some earlier build step 52 | 53 | For released software, the version identifier is closely related to a VCS 54 | tag. Some projects use tag names that include more than just the version 55 | string (e.g. "myproject-1.2" instead of just "1.2"), in which case the tool 56 | needs to strip the tag prefix to extract the version identifier. For 57 | unreleased software (between tags), the version identifier should provide 58 | enough information to help developers recreate the same tree, while also 59 | giving them an idea of roughly how old the tree is (after version 1.2, before 60 | version 1.3). Many VCS systems can report a description that captures this, 61 | for example `git describe --tags --dirty --always` reports things like 62 | "0.7-1-g574ab98-dirty" to indicate that the checkout is one revision past the 63 | 0.7 tag, has a unique revision id of "574ab98", and is "dirty" (it has 64 | uncommitted changes. 65 | 66 | The version identifier is used for multiple purposes: 67 | 68 | * to allow the module to self-identify its version: `myproject.__version__` 69 | * to choose a name and prefix for a 'setup.py sdist' tarball 70 | 71 | ## Theory of Operation 72 | 73 | Versioneer works by adding a special `_version.py` file into your source 74 | tree, where your `__init__.py` can import it. This `_version.py` knows how to 75 | dynamically ask the VCS tool for version information at import time. 76 | 77 | `_version.py` also contains `$Revision$` markers, and the installation 78 | process marks `_version.py` to have this marker rewritten with a tag name 79 | during the `git archive` command. As a result, generated tarballs will 80 | contain enough information to get the proper version. 81 | 82 | To allow `setup.py` to compute a version too, a `versioneer.py` is added to 83 | the top level of your source tree, next to `setup.py` and the `setup.cfg` 84 | that configures it. This overrides several distutils/setuptools commands to 85 | compute the version when invoked, and changes `setup.py build` and `setup.py 86 | sdist` to replace `_version.py` with a small static file that contains just 87 | the generated version data. 88 | 89 | ## Installation 90 | 91 | See [INSTALL.md](./INSTALL.md) for detailed installation instructions. 92 | 93 | ## Version-String Flavors 94 | 95 | Code which uses Versioneer can learn about its version string at runtime by 96 | importing `_version` from your main `__init__.py` file and running the 97 | `get_versions()` function. From the "outside" (e.g. in `setup.py`), you can 98 | import the top-level `versioneer.py` and run `get_versions()`. 99 | 100 | Both functions return a dictionary with different flavors of version 101 | information: 102 | 103 | * `['version']`: A condensed version string, rendered using the selected 104 | style. This is the most commonly used value for the project's version 105 | string. The default "pep440" style yields strings like `0.11`, 106 | `0.11+2.g1076c97`, or `0.11+2.g1076c97.dirty`. See the "Styles" section 107 | below for alternative styles. 108 | 109 | * `['full-revisionid']`: detailed revision identifier. For Git, this is the 110 | full SHA1 commit id, e.g. "1076c978a8d3cfc70f408fe5974aa6c092c949ac". 111 | 112 | * `['date']`: Date and time of the latest `HEAD` commit. For Git, it is the 113 | commit date in ISO 8601 format. This will be None if the date is not 114 | available. 115 | 116 | * `['dirty']`: a boolean, True if the tree has uncommitted changes. Note that 117 | this is only accurate if run in a VCS checkout, otherwise it is likely to 118 | be False or None 119 | 120 | * `['error']`: if the version string could not be computed, this will be set 121 | to a string describing the problem, otherwise it will be None. It may be 122 | useful to throw an exception in setup.py if this is set, to avoid e.g. 123 | creating tarballs with a version string of "unknown". 124 | 125 | Some variants are more useful than others. Including `full-revisionid` in a 126 | bug report should allow developers to reconstruct the exact code being tested 127 | (or indicate the presence of local changes that should be shared with the 128 | developers). `version` is suitable for display in an "about" box or a CLI 129 | `--version` output: it can be easily compared against release notes and lists 130 | of bugs fixed in various releases. 131 | 132 | The installer adds the following text to your `__init__.py` to place a basic 133 | version in `YOURPROJECT.__version__`: 134 | 135 | from ._version import get_versions 136 | __version__ = get_versions()['version'] 137 | del get_versions 138 | 139 | ## Styles 140 | 141 | The setup.cfg `style=` configuration controls how the VCS information is 142 | rendered into a version string. 143 | 144 | The default style, "pep440", produces a PEP440-compliant string, equal to the 145 | un-prefixed tag name for actual releases, and containing an additional "local 146 | version" section with more detail for in-between builds. For Git, this is 147 | TAG[+DISTANCE.gHEX[.dirty]] , using information from `git describe --tags 148 | --dirty --always`. For example "0.11+2.g1076c97.dirty" indicates that the 149 | tree is like the "1076c97" commit but has uncommitted changes (".dirty"), and 150 | that this commit is two revisions ("+2") beyond the "0.11" tag. For released 151 | software (exactly equal to a known tag), the identifier will only contain the 152 | stripped tag, e.g. "0.11". 153 | 154 | Other styles are available. See [details.md](details.md) in the Versioneer 155 | source tree for descriptions. 156 | 157 | ## Debugging 158 | 159 | Versioneer tries to avoid fatal errors: if something goes wrong, it will tend 160 | to return a version of "0+unknown". To investigate the problem, run `setup.py 161 | version`, which will run the version-lookup code in a verbose mode, and will 162 | display the full contents of `get_versions()` (including the `error` string, 163 | which may help identify what went wrong). 164 | 165 | ## Known Limitations 166 | 167 | Some situations are known to cause problems for Versioneer. This details the 168 | most significant ones. More can be found on Github 169 | [issues page](https://github.com/warner/python-versioneer/issues). 170 | 171 | ### Subprojects 172 | 173 | Versioneer has limited support for source trees in which `setup.py` is not in 174 | the root directory (e.g. `setup.py` and `.git/` are *not* siblings). The are 175 | two common reasons why `setup.py` might not be in the root: 176 | 177 | * Source trees which contain multiple subprojects, such as 178 | [Buildbot](https://github.com/buildbot/buildbot), which contains both 179 | "master" and "slave" subprojects, each with their own `setup.py`, 180 | `setup.cfg`, and `tox.ini`. Projects like these produce multiple PyPI 181 | distributions (and upload multiple independently-installable tarballs). 182 | * Source trees whose main purpose is to contain a C library, but which also 183 | provide bindings to Python (and perhaps other langauges) in subdirectories. 184 | 185 | Versioneer will look for `.git` in parent directories, and most operations 186 | should get the right version string. However `pip` and `setuptools` have bugs 187 | and implementation details which frequently cause `pip install .` from a 188 | subproject directory to fail to find a correct version string (so it usually 189 | defaults to `0+unknown`). 190 | 191 | `pip install --editable .` should work correctly. `setup.py install` might 192 | work too. 193 | 194 | Pip-8.1.1 is known to have this problem, but hopefully it will get fixed in 195 | some later version. 196 | 197 | [Bug #38](https://github.com/warner/python-versioneer/issues/38) is tracking 198 | this issue. The discussion in 199 | [PR #61](https://github.com/warner/python-versioneer/pull/61) describes the 200 | issue from the Versioneer side in more detail. 201 | [pip PR#3176](https://github.com/pypa/pip/pull/3176) and 202 | [pip PR#3615](https://github.com/pypa/pip/pull/3615) contain work to improve 203 | pip to let Versioneer work correctly. 204 | 205 | Versioneer-0.16 and earlier only looked for a `.git` directory next to the 206 | `setup.cfg`, so subprojects were completely unsupported with those releases. 207 | 208 | ### Editable installs with setuptools <= 18.5 209 | 210 | `setup.py develop` and `pip install --editable .` allow you to install a 211 | project into a virtualenv once, then continue editing the source code (and 212 | test) without re-installing after every change. 213 | 214 | "Entry-point scripts" (`setup(entry_points={"console_scripts": ..})`) are a 215 | convenient way to specify executable scripts that should be installed along 216 | with the python package. 217 | 218 | These both work as expected when using modern setuptools. When using 219 | setuptools-18.5 or earlier, however, certain operations will cause 220 | `pkg_resources.DistributionNotFound` errors when running the entrypoint 221 | script, which must be resolved by re-installing the package. This happens 222 | when the install happens with one version, then the egg_info data is 223 | regenerated while a different version is checked out. Many setup.py commands 224 | cause egg_info to be rebuilt (including `sdist`, `wheel`, and installing into 225 | a different virtualenv), so this can be surprising. 226 | 227 | [Bug #83](https://github.com/warner/python-versioneer/issues/83) describes 228 | this one, but upgrading to a newer version of setuptools should probably 229 | resolve it. 230 | 231 | ### Unicode version strings 232 | 233 | While Versioneer works (and is continually tested) with both Python 2 and 234 | Python 3, it is not entirely consistent with bytes-vs-unicode distinctions. 235 | Newer releases probably generate unicode version strings on py2. It's not 236 | clear that this is wrong, but it may be surprising for applications when then 237 | write these strings to a network connection or include them in bytes-oriented 238 | APIs like cryptographic checksums. 239 | 240 | [Bug #71](https://github.com/warner/python-versioneer/issues/71) investigates 241 | this question. 242 | 243 | 244 | ## Updating Versioneer 245 | 246 | To upgrade your project to a new release of Versioneer, do the following: 247 | 248 | * install the new Versioneer (`pip install -U versioneer` or equivalent) 249 | * edit `setup.cfg`, if necessary, to include any new configuration settings 250 | indicated by the release notes. See [UPGRADING](./UPGRADING.md) for details. 251 | * re-run `versioneer install` in your source tree, to replace 252 | `SRC/_version.py` 253 | * commit any changed files 254 | 255 | ## Future Directions 256 | 257 | This tool is designed to make it easily extended to other version-control 258 | systems: all VCS-specific components are in separate directories like 259 | src/git/ . The top-level `versioneer.py` script is assembled from these 260 | components by running make-versioneer.py . In the future, make-versioneer.py 261 | will take a VCS name as an argument, and will construct a version of 262 | `versioneer.py` that is specific to the given VCS. It might also take the 263 | configuration arguments that are currently provided manually during 264 | installation by editing setup.py . Alternatively, it might go the other 265 | direction and include code from all supported VCS systems, reducing the 266 | number of intermediate scripts. 267 | 268 | 269 | ## License 270 | 271 | To make Versioneer easier to embed, all its code is dedicated to the public 272 | domain. The `_version.py` that it creates is also in the public domain. 273 | Specifically, both are released under the Creative Commons "Public Domain 274 | Dedication" license (CC0-1.0), as described in 275 | https://creativecommons.org/publicdomain/zero/1.0/ . 276 | 277 | """ 278 | 279 | from __future__ import print_function 280 | try: 281 | import configparser 282 | except ImportError: 283 | import ConfigParser as configparser 284 | import errno 285 | import json 286 | import os 287 | import re 288 | import subprocess 289 | import sys 290 | 291 | 292 | class VersioneerConfig: 293 | """Container for Versioneer configuration parameters.""" 294 | 295 | 296 | def get_root(): 297 | """Get the project root directory. 298 | 299 | We require that all commands are run from the project root, i.e. the 300 | directory that contains setup.py, setup.cfg, and versioneer.py . 301 | """ 302 | root = os.path.realpath(os.path.abspath(os.getcwd())) 303 | setup_py = os.path.join(root, "setup.py") 304 | versioneer_py = os.path.join(root, "versioneer.py") 305 | if not (os.path.exists(setup_py) or os.path.exists(versioneer_py)): 306 | # allow 'python path/to/setup.py COMMAND' 307 | root = os.path.dirname(os.path.realpath(os.path.abspath(sys.argv[0]))) 308 | setup_py = os.path.join(root, "setup.py") 309 | versioneer_py = os.path.join(root, "versioneer.py") 310 | if not (os.path.exists(setup_py) or os.path.exists(versioneer_py)): 311 | err = ("Versioneer was unable to run the project root directory. " 312 | "Versioneer requires setup.py to be executed from " 313 | "its immediate directory (like 'python setup.py COMMAND'), " 314 | "or in a way that lets it use sys.argv[0] to find the root " 315 | "(like 'python path/to/setup.py COMMAND').") 316 | raise VersioneerBadRootError(err) 317 | try: 318 | # Certain runtime workflows (setup.py install/develop in a setuptools 319 | # tree) execute all dependencies in a single python process, so 320 | # "versioneer" may be imported multiple times, and python's shared 321 | # module-import table will cache the first one. So we can't use 322 | # os.path.dirname(__file__), as that will find whichever 323 | # versioneer.py was first imported, even in later projects. 324 | me = os.path.realpath(os.path.abspath(__file__)) 325 | me_dir = os.path.normcase(os.path.splitext(me)[0]) 326 | vsr_dir = os.path.normcase(os.path.splitext(versioneer_py)[0]) 327 | if me_dir != vsr_dir: 328 | print("Warning: build in %s is using versioneer.py from %s" 329 | % (os.path.dirname(me), versioneer_py)) 330 | except NameError: 331 | pass 332 | return root 333 | 334 | 335 | def get_config_from_root(root): 336 | """Read the project setup.cfg file to determine Versioneer config.""" 337 | # This might raise EnvironmentError (if setup.cfg is missing), or 338 | # configparser.NoSectionError (if it lacks a [versioneer] section), or 339 | # configparser.NoOptionError (if it lacks "VCS="). See the docstring at 340 | # the top of versioneer.py for instructions on writing your setup.cfg . 341 | setup_cfg = os.path.join(root, "setup.cfg") 342 | parser = configparser.SafeConfigParser() 343 | with open(setup_cfg, "r") as f: 344 | parser.readfp(f) 345 | VCS = parser.get("versioneer", "VCS") # mandatory 346 | 347 | def get(parser, name): 348 | if parser.has_option("versioneer", name): 349 | return parser.get("versioneer", name) 350 | return None 351 | cfg = VersioneerConfig() 352 | cfg.VCS = VCS 353 | cfg.style = get(parser, "style") or "" 354 | cfg.versionfile_source = get(parser, "versionfile_source") 355 | cfg.versionfile_build = get(parser, "versionfile_build") 356 | cfg.tag_prefix = get(parser, "tag_prefix") 357 | if cfg.tag_prefix in ("''", '""'): 358 | cfg.tag_prefix = "" 359 | cfg.parentdir_prefix = get(parser, "parentdir_prefix") 360 | cfg.verbose = get(parser, "verbose") 361 | return cfg 362 | 363 | 364 | class NotThisMethod(Exception): 365 | """Exception raised if a method is not valid for the current scenario.""" 366 | 367 | 368 | # these dictionaries contain VCS-specific tools 369 | LONG_VERSION_PY = {} 370 | HANDLERS = {} 371 | 372 | 373 | def register_vcs_handler(vcs, method): # decorator 374 | """Decorator to mark a method as the handler for a particular VCS.""" 375 | def decorate(f): 376 | """Store f in HANDLERS[vcs][method].""" 377 | if vcs not in HANDLERS: 378 | HANDLERS[vcs] = {} 379 | HANDLERS[vcs][method] = f 380 | return f 381 | return decorate 382 | 383 | 384 | def run_command(commands, args, cwd=None, verbose=False, hide_stderr=False, 385 | env=None): 386 | """Call the given command(s).""" 387 | assert isinstance(commands, list) 388 | p = None 389 | for c in commands: 390 | try: 391 | dispcmd = str([c] + args) 392 | # remember shell=False, so use git.cmd on windows, not just git 393 | p = subprocess.Popen([c] + args, cwd=cwd, env=env, 394 | stdout=subprocess.PIPE, 395 | stderr=(subprocess.PIPE if hide_stderr 396 | else None)) 397 | break 398 | except EnvironmentError: 399 | e = sys.exc_info()[1] 400 | if e.errno == errno.ENOENT: 401 | continue 402 | if verbose: 403 | print("unable to run %s" % dispcmd) 404 | print(e) 405 | return None, None 406 | else: 407 | if verbose: 408 | print("unable to find command, tried %s" % (commands,)) 409 | return None, None 410 | stdout = p.communicate()[0].strip() 411 | if sys.version_info[0] >= 3: 412 | stdout = stdout.decode() 413 | if p.returncode != 0: 414 | if verbose: 415 | print("unable to run %s (error)" % dispcmd) 416 | print("stdout was %s" % stdout) 417 | return None, p.returncode 418 | return stdout, p.returncode 419 | 420 | 421 | LONG_VERSION_PY['git'] = ''' 422 | # This file helps to compute a version number in source trees obtained from 423 | # git-archive tarball (such as those provided by githubs download-from-tag 424 | # feature). Distribution tarballs (built by setup.py sdist) and build 425 | # directories (produced by setup.py build) will contain a much shorter file 426 | # that just contains the computed version number. 427 | 428 | # This file is released into the public domain. Generated by 429 | # versioneer-0.18 (https://github.com/warner/python-versioneer) 430 | 431 | """Git implementation of _version.py.""" 432 | 433 | import errno 434 | import os 435 | import re 436 | import subprocess 437 | import sys 438 | 439 | 440 | def get_keywords(): 441 | """Get the keywords needed to look up the version information.""" 442 | # these strings will be replaced by git during git-archive. 443 | # setup.py/versioneer.py will grep for the variable names, so they must 444 | # each be defined on a line of their own. _version.py will just call 445 | # get_keywords(). 446 | git_refnames = "%(DOLLAR)sFormat:%%d%(DOLLAR)s" 447 | git_full = "%(DOLLAR)sFormat:%%H%(DOLLAR)s" 448 | git_date = "%(DOLLAR)sFormat:%%ci%(DOLLAR)s" 449 | keywords = {"refnames": git_refnames, "full": git_full, "date": git_date} 450 | return keywords 451 | 452 | 453 | class VersioneerConfig: 454 | """Container for Versioneer configuration parameters.""" 455 | 456 | 457 | def get_config(): 458 | """Create, populate and return the VersioneerConfig() object.""" 459 | # these strings are filled in when 'setup.py versioneer' creates 460 | # _version.py 461 | cfg = VersioneerConfig() 462 | cfg.VCS = "git" 463 | cfg.style = "%(STYLE)s" 464 | cfg.tag_prefix = "%(TAG_PREFIX)s" 465 | cfg.parentdir_prefix = "%(PARENTDIR_PREFIX)s" 466 | cfg.versionfile_source = "%(VERSIONFILE_SOURCE)s" 467 | cfg.verbose = False 468 | return cfg 469 | 470 | 471 | class NotThisMethod(Exception): 472 | """Exception raised if a method is not valid for the current scenario.""" 473 | 474 | 475 | LONG_VERSION_PY = {} 476 | HANDLERS = {} 477 | 478 | 479 | def register_vcs_handler(vcs, method): # decorator 480 | """Decorator to mark a method as the handler for a particular VCS.""" 481 | def decorate(f): 482 | """Store f in HANDLERS[vcs][method].""" 483 | if vcs not in HANDLERS: 484 | HANDLERS[vcs] = {} 485 | HANDLERS[vcs][method] = f 486 | return f 487 | return decorate 488 | 489 | 490 | def run_command(commands, args, cwd=None, verbose=False, hide_stderr=False, 491 | env=None): 492 | """Call the given command(s).""" 493 | assert isinstance(commands, list) 494 | p = None 495 | for c in commands: 496 | try: 497 | dispcmd = str([c] + args) 498 | # remember shell=False, so use git.cmd on windows, not just git 499 | p = subprocess.Popen([c] + args, cwd=cwd, env=env, 500 | stdout=subprocess.PIPE, 501 | stderr=(subprocess.PIPE if hide_stderr 502 | else None)) 503 | break 504 | except EnvironmentError: 505 | e = sys.exc_info()[1] 506 | if e.errno == errno.ENOENT: 507 | continue 508 | if verbose: 509 | print("unable to run %%s" %% dispcmd) 510 | print(e) 511 | return None, None 512 | else: 513 | if verbose: 514 | print("unable to find command, tried %%s" %% (commands,)) 515 | return None, None 516 | stdout = p.communicate()[0].strip() 517 | if sys.version_info[0] >= 3: 518 | stdout = stdout.decode() 519 | if p.returncode != 0: 520 | if verbose: 521 | print("unable to run %%s (error)" %% dispcmd) 522 | print("stdout was %%s" %% stdout) 523 | return None, p.returncode 524 | return stdout, p.returncode 525 | 526 | 527 | def versions_from_parentdir(parentdir_prefix, root, verbose): 528 | """Try to determine the version from the parent directory name. 529 | 530 | Source tarballs conventionally unpack into a directory that includes both 531 | the project name and a version string. We will also support searching up 532 | two directory levels for an appropriately named parent directory 533 | """ 534 | rootdirs = [] 535 | 536 | for i in range(3): 537 | dirname = os.path.basename(root) 538 | if dirname.startswith(parentdir_prefix): 539 | return {"version": dirname[len(parentdir_prefix):], 540 | "full-revisionid": None, 541 | "dirty": False, "error": None, "date": None} 542 | else: 543 | rootdirs.append(root) 544 | root = os.path.dirname(root) # up a level 545 | 546 | if verbose: 547 | print("Tried directories %%s but none started with prefix %%s" %% 548 | (str(rootdirs), parentdir_prefix)) 549 | raise NotThisMethod("rootdir doesn't start with parentdir_prefix") 550 | 551 | 552 | @register_vcs_handler("git", "get_keywords") 553 | def git_get_keywords(versionfile_abs): 554 | """Extract version information from the given file.""" 555 | # the code embedded in _version.py can just fetch the value of these 556 | # keywords. When used from setup.py, we don't want to import _version.py, 557 | # so we do it with a regexp instead. This function is not used from 558 | # _version.py. 559 | keywords = {} 560 | try: 561 | f = open(versionfile_abs, "r") 562 | for line in f.readlines(): 563 | if line.strip().startswith("git_refnames ="): 564 | mo = re.search(r'=\s*"(.*)"', line) 565 | if mo: 566 | keywords["refnames"] = mo.group(1) 567 | if line.strip().startswith("git_full ="): 568 | mo = re.search(r'=\s*"(.*)"', line) 569 | if mo: 570 | keywords["full"] = mo.group(1) 571 | if line.strip().startswith("git_date ="): 572 | mo = re.search(r'=\s*"(.*)"', line) 573 | if mo: 574 | keywords["date"] = mo.group(1) 575 | f.close() 576 | except EnvironmentError: 577 | pass 578 | return keywords 579 | 580 | 581 | @register_vcs_handler("git", "keywords") 582 | def git_versions_from_keywords(keywords, tag_prefix, verbose): 583 | """Get version information from git keywords.""" 584 | if not keywords: 585 | raise NotThisMethod("no keywords at all, weird") 586 | date = keywords.get("date") 587 | if date is not None: 588 | # git-2.2.0 added "%%cI", which expands to an ISO-8601 -compliant 589 | # datestamp. However we prefer "%%ci" (which expands to an "ISO-8601 590 | # -like" string, which we must then edit to make compliant), because 591 | # it's been around since git-1.5.3, and it's too difficult to 592 | # discover which version we're using, or to work around using an 593 | # older one. 594 | date = date.strip().replace(" ", "T", 1).replace(" ", "", 1) 595 | refnames = keywords["refnames"].strip() 596 | if refnames.startswith("$Format"): 597 | if verbose: 598 | print("keywords are unexpanded, not using") 599 | raise NotThisMethod("unexpanded keywords, not a git-archive tarball") 600 | refs = set([r.strip() for r in refnames.strip("()").split(",")]) 601 | # starting in git-1.8.3, tags are listed as "tag: foo-1.0" instead of 602 | # just "foo-1.0". If we see a "tag: " prefix, prefer those. 603 | TAG = "tag: " 604 | tags = set([r[len(TAG):] for r in refs if r.startswith(TAG)]) 605 | if not tags: 606 | # Either we're using git < 1.8.3, or there really are no tags. We use 607 | # a heuristic: assume all version tags have a digit. The old git %%d 608 | # expansion behaves like git log --decorate=short and strips out the 609 | # refs/heads/ and refs/tags/ prefixes that would let us distinguish 610 | # between branches and tags. By ignoring refnames without digits, we 611 | # filter out many common branch names like "release" and 612 | # "stabilization", as well as "HEAD" and "master". 613 | tags = set([r for r in refs if re.search(r'\d', r)]) 614 | if verbose: 615 | print("discarding '%%s', no digits" %% ",".join(refs - tags)) 616 | if verbose: 617 | print("likely tags: %%s" %% ",".join(sorted(tags))) 618 | for ref in sorted(tags): 619 | # sorting will prefer e.g. "2.0" over "2.0rc1" 620 | if ref.startswith(tag_prefix): 621 | r = ref[len(tag_prefix):] 622 | if verbose: 623 | print("picking %%s" %% r) 624 | return {"version": r, 625 | "full-revisionid": keywords["full"].strip(), 626 | "dirty": False, "error": None, 627 | "date": date} 628 | # no suitable tags, so version is "0+unknown", but full hex is still there 629 | if verbose: 630 | print("no suitable tags, using unknown + full revision id") 631 | return {"version": "0+unknown", 632 | "full-revisionid": keywords["full"].strip(), 633 | "dirty": False, "error": "no suitable tags", "date": None} 634 | 635 | 636 | @register_vcs_handler("git", "pieces_from_vcs") 637 | def git_pieces_from_vcs(tag_prefix, root, verbose, run_command=run_command): 638 | """Get version from 'git describe' in the root of the source tree. 639 | 640 | This only gets called if the git-archive 'subst' keywords were *not* 641 | expanded, and _version.py hasn't already been rewritten with a short 642 | version string, meaning we're inside a checked out source tree. 643 | """ 644 | GITS = ["git"] 645 | if sys.platform == "win32": 646 | GITS = ["git.cmd", "git.exe"] 647 | 648 | out, rc = run_command(GITS, ["rev-parse", "--git-dir"], cwd=root, 649 | hide_stderr=True) 650 | if rc != 0: 651 | if verbose: 652 | print("Directory %%s not under git control" %% root) 653 | raise NotThisMethod("'git rev-parse --git-dir' returned error") 654 | 655 | # if there is a tag matching tag_prefix, this yields TAG-NUM-gHEX[-dirty] 656 | # if there isn't one, this yields HEX[-dirty] (no NUM) 657 | describe_out, rc = run_command(GITS, ["describe", "--tags", "--dirty", 658 | "--always", "--long", 659 | "--match", "%%s*" %% tag_prefix], 660 | cwd=root) 661 | # --long was added in git-1.5.5 662 | if describe_out is None: 663 | raise NotThisMethod("'git describe' failed") 664 | describe_out = describe_out.strip() 665 | full_out, rc = run_command(GITS, ["rev-parse", "HEAD"], cwd=root) 666 | if full_out is None: 667 | raise NotThisMethod("'git rev-parse' failed") 668 | full_out = full_out.strip() 669 | 670 | pieces = {} 671 | pieces["long"] = full_out 672 | pieces["short"] = full_out[:7] # maybe improved later 673 | pieces["error"] = None 674 | 675 | # parse describe_out. It will be like TAG-NUM-gHEX[-dirty] or HEX[-dirty] 676 | # TAG might have hyphens. 677 | git_describe = describe_out 678 | 679 | # look for -dirty suffix 680 | dirty = git_describe.endswith("-dirty") 681 | pieces["dirty"] = dirty 682 | if dirty: 683 | git_describe = git_describe[:git_describe.rindex("-dirty")] 684 | 685 | # now we have TAG-NUM-gHEX or HEX 686 | 687 | if "-" in git_describe: 688 | # TAG-NUM-gHEX 689 | mo = re.search(r'^(.+)-(\d+)-g([0-9a-f]+)$', git_describe) 690 | if not mo: 691 | # unparseable. Maybe git-describe is misbehaving? 692 | pieces["error"] = ("unable to parse git-describe output: '%%s'" 693 | %% describe_out) 694 | return pieces 695 | 696 | # tag 697 | full_tag = mo.group(1) 698 | if not full_tag.startswith(tag_prefix): 699 | if verbose: 700 | fmt = "tag '%%s' doesn't start with prefix '%%s'" 701 | print(fmt %% (full_tag, tag_prefix)) 702 | pieces["error"] = ("tag '%%s' doesn't start with prefix '%%s'" 703 | %% (full_tag, tag_prefix)) 704 | return pieces 705 | pieces["closest-tag"] = full_tag[len(tag_prefix):] 706 | 707 | # distance: number of commits since tag 708 | pieces["distance"] = int(mo.group(2)) 709 | 710 | # commit: short hex revision ID 711 | pieces["short"] = mo.group(3) 712 | 713 | else: 714 | # HEX: no tags 715 | pieces["closest-tag"] = None 716 | count_out, rc = run_command(GITS, ["rev-list", "HEAD", "--count"], 717 | cwd=root) 718 | pieces["distance"] = int(count_out) # total number of commits 719 | 720 | # commit date: see ISO-8601 comment in git_versions_from_keywords() 721 | date = run_command(GITS, ["show", "-s", "--format=%%ci", "HEAD"], 722 | cwd=root)[0].strip() 723 | pieces["date"] = date.strip().replace(" ", "T", 1).replace(" ", "", 1) 724 | 725 | return pieces 726 | 727 | 728 | def plus_or_dot(pieces): 729 | """Return a + if we don't already have one, else return a .""" 730 | if "+" in pieces.get("closest-tag", ""): 731 | return "." 732 | return "+" 733 | 734 | 735 | def render_pep440(pieces): 736 | """Build up version string, with post-release "local version identifier". 737 | 738 | Our goal: TAG[+DISTANCE.gHEX[.dirty]] . Note that if you 739 | get a tagged build and then dirty it, you'll get TAG+0.gHEX.dirty 740 | 741 | Exceptions: 742 | 1: no tags. git_describe was just HEX. 0+untagged.DISTANCE.gHEX[.dirty] 743 | """ 744 | if pieces["closest-tag"]: 745 | rendered = pieces["closest-tag"] 746 | if pieces["distance"] or pieces["dirty"]: 747 | rendered += plus_or_dot(pieces) 748 | rendered += "%%d.g%%s" %% (pieces["distance"], pieces["short"]) 749 | if pieces["dirty"]: 750 | rendered += ".dirty" 751 | else: 752 | # exception #1 753 | rendered = "0+untagged.%%d.g%%s" %% (pieces["distance"], 754 | pieces["short"]) 755 | if pieces["dirty"]: 756 | rendered += ".dirty" 757 | return rendered 758 | 759 | 760 | def render_pep440_pre(pieces): 761 | """TAG[.post.devDISTANCE] -- No -dirty. 762 | 763 | Exceptions: 764 | 1: no tags. 0.post.devDISTANCE 765 | """ 766 | if pieces["closest-tag"]: 767 | rendered = pieces["closest-tag"] 768 | if pieces["distance"]: 769 | rendered += ".post.dev%%d" %% pieces["distance"] 770 | else: 771 | # exception #1 772 | rendered = "0.post.dev%%d" %% pieces["distance"] 773 | return rendered 774 | 775 | 776 | def render_pep440_post(pieces): 777 | """TAG[.postDISTANCE[.dev0]+gHEX] . 778 | 779 | The ".dev0" means dirty. Note that .dev0 sorts backwards 780 | (a dirty tree will appear "older" than the corresponding clean one), 781 | but you shouldn't be releasing software with -dirty anyways. 782 | 783 | Exceptions: 784 | 1: no tags. 0.postDISTANCE[.dev0] 785 | """ 786 | if pieces["closest-tag"]: 787 | rendered = pieces["closest-tag"] 788 | if pieces["distance"] or pieces["dirty"]: 789 | rendered += ".post%%d" %% pieces["distance"] 790 | if pieces["dirty"]: 791 | rendered += ".dev0" 792 | rendered += plus_or_dot(pieces) 793 | rendered += "g%%s" %% pieces["short"] 794 | else: 795 | # exception #1 796 | rendered = "0.post%%d" %% pieces["distance"] 797 | if pieces["dirty"]: 798 | rendered += ".dev0" 799 | rendered += "+g%%s" %% pieces["short"] 800 | return rendered 801 | 802 | 803 | def render_pep440_old(pieces): 804 | """TAG[.postDISTANCE[.dev0]] . 805 | 806 | The ".dev0" means dirty. 807 | 808 | Eexceptions: 809 | 1: no tags. 0.postDISTANCE[.dev0] 810 | """ 811 | if pieces["closest-tag"]: 812 | rendered = pieces["closest-tag"] 813 | if pieces["distance"] or pieces["dirty"]: 814 | rendered += ".post%%d" %% pieces["distance"] 815 | if pieces["dirty"]: 816 | rendered += ".dev0" 817 | else: 818 | # exception #1 819 | rendered = "0.post%%d" %% pieces["distance"] 820 | if pieces["dirty"]: 821 | rendered += ".dev0" 822 | return rendered 823 | 824 | 825 | def render_git_describe(pieces): 826 | """TAG[-DISTANCE-gHEX][-dirty]. 827 | 828 | Like 'git describe --tags --dirty --always'. 829 | 830 | Exceptions: 831 | 1: no tags. HEX[-dirty] (note: no 'g' prefix) 832 | """ 833 | if pieces["closest-tag"]: 834 | rendered = pieces["closest-tag"] 835 | if pieces["distance"]: 836 | rendered += "-%%d-g%%s" %% (pieces["distance"], pieces["short"]) 837 | else: 838 | # exception #1 839 | rendered = pieces["short"] 840 | if pieces["dirty"]: 841 | rendered += "-dirty" 842 | return rendered 843 | 844 | 845 | def render_git_describe_long(pieces): 846 | """TAG-DISTANCE-gHEX[-dirty]. 847 | 848 | Like 'git describe --tags --dirty --always -long'. 849 | The distance/hash is unconditional. 850 | 851 | Exceptions: 852 | 1: no tags. HEX[-dirty] (note: no 'g' prefix) 853 | """ 854 | if pieces["closest-tag"]: 855 | rendered = pieces["closest-tag"] 856 | rendered += "-%%d-g%%s" %% (pieces["distance"], pieces["short"]) 857 | else: 858 | # exception #1 859 | rendered = pieces["short"] 860 | if pieces["dirty"]: 861 | rendered += "-dirty" 862 | return rendered 863 | 864 | 865 | def render(pieces, style): 866 | """Render the given version pieces into the requested style.""" 867 | if pieces["error"]: 868 | return {"version": "unknown", 869 | "full-revisionid": pieces.get("long"), 870 | "dirty": None, 871 | "error": pieces["error"], 872 | "date": None} 873 | 874 | if not style or style == "default": 875 | style = "pep440" # the default 876 | 877 | if style == "pep440": 878 | rendered = render_pep440(pieces) 879 | elif style == "pep440-pre": 880 | rendered = render_pep440_pre(pieces) 881 | elif style == "pep440-post": 882 | rendered = render_pep440_post(pieces) 883 | elif style == "pep440-old": 884 | rendered = render_pep440_old(pieces) 885 | elif style == "git-describe": 886 | rendered = render_git_describe(pieces) 887 | elif style == "git-describe-long": 888 | rendered = render_git_describe_long(pieces) 889 | else: 890 | raise ValueError("unknown style '%%s'" %% style) 891 | 892 | return {"version": rendered, "full-revisionid": pieces["long"], 893 | "dirty": pieces["dirty"], "error": None, 894 | "date": pieces.get("date")} 895 | 896 | 897 | def get_versions(): 898 | """Get version information or return default if unable to do so.""" 899 | # I am in _version.py, which lives at ROOT/VERSIONFILE_SOURCE. If we have 900 | # __file__, we can work backwards from there to the root. Some 901 | # py2exe/bbfreeze/non-CPython implementations don't do __file__, in which 902 | # case we can only use expanded keywords. 903 | 904 | cfg = get_config() 905 | verbose = cfg.verbose 906 | 907 | try: 908 | return git_versions_from_keywords(get_keywords(), cfg.tag_prefix, 909 | verbose) 910 | except NotThisMethod: 911 | pass 912 | 913 | try: 914 | root = os.path.realpath(__file__) 915 | # versionfile_source is the relative path from the top of the source 916 | # tree (where the .git directory might live) to this file. Invert 917 | # this to find the root from __file__. 918 | for i in cfg.versionfile_source.split('/'): 919 | root = os.path.dirname(root) 920 | except NameError: 921 | return {"version": "0+unknown", "full-revisionid": None, 922 | "dirty": None, 923 | "error": "unable to find root of source tree", 924 | "date": None} 925 | 926 | try: 927 | pieces = git_pieces_from_vcs(cfg.tag_prefix, root, verbose) 928 | return render(pieces, cfg.style) 929 | except NotThisMethod: 930 | pass 931 | 932 | try: 933 | if cfg.parentdir_prefix: 934 | return versions_from_parentdir(cfg.parentdir_prefix, root, verbose) 935 | except NotThisMethod: 936 | pass 937 | 938 | return {"version": "0+unknown", "full-revisionid": None, 939 | "dirty": None, 940 | "error": "unable to compute version", "date": None} 941 | ''' 942 | 943 | 944 | @register_vcs_handler("git", "get_keywords") 945 | def git_get_keywords(versionfile_abs): 946 | """Extract version information from the given file.""" 947 | # the code embedded in _version.py can just fetch the value of these 948 | # keywords. When used from setup.py, we don't want to import _version.py, 949 | # so we do it with a regexp instead. This function is not used from 950 | # _version.py. 951 | keywords = {} 952 | try: 953 | f = open(versionfile_abs, "r") 954 | for line in f.readlines(): 955 | if line.strip().startswith("git_refnames ="): 956 | mo = re.search(r'=\s*"(.*)"', line) 957 | if mo: 958 | keywords["refnames"] = mo.group(1) 959 | if line.strip().startswith("git_full ="): 960 | mo = re.search(r'=\s*"(.*)"', line) 961 | if mo: 962 | keywords["full"] = mo.group(1) 963 | if line.strip().startswith("git_date ="): 964 | mo = re.search(r'=\s*"(.*)"', line) 965 | if mo: 966 | keywords["date"] = mo.group(1) 967 | f.close() 968 | except EnvironmentError: 969 | pass 970 | return keywords 971 | 972 | 973 | @register_vcs_handler("git", "keywords") 974 | def git_versions_from_keywords(keywords, tag_prefix, verbose): 975 | """Get version information from git keywords.""" 976 | if not keywords: 977 | raise NotThisMethod("no keywords at all, weird") 978 | date = keywords.get("date") 979 | if date is not None: 980 | # git-2.2.0 added "%cI", which expands to an ISO-8601 -compliant 981 | # datestamp. However we prefer "%ci" (which expands to an "ISO-8601 982 | # -like" string, which we must then edit to make compliant), because 983 | # it's been around since git-1.5.3, and it's too difficult to 984 | # discover which version we're using, or to work around using an 985 | # older one. 986 | date = date.strip().replace(" ", "T", 1).replace(" ", "", 1) 987 | refnames = keywords["refnames"].strip() 988 | if refnames.startswith("$Format"): 989 | if verbose: 990 | print("keywords are unexpanded, not using") 991 | raise NotThisMethod("unexpanded keywords, not a git-archive tarball") 992 | refs = set([r.strip() for r in refnames.strip("()").split(",")]) 993 | # starting in git-1.8.3, tags are listed as "tag: foo-1.0" instead of 994 | # just "foo-1.0". If we see a "tag: " prefix, prefer those. 995 | TAG = "tag: " 996 | tags = set([r[len(TAG):] for r in refs if r.startswith(TAG)]) 997 | if not tags: 998 | # Either we're using git < 1.8.3, or there really are no tags. We use 999 | # a heuristic: assume all version tags have a digit. The old git %d 1000 | # expansion behaves like git log --decorate=short and strips out the 1001 | # refs/heads/ and refs/tags/ prefixes that would let us distinguish 1002 | # between branches and tags. By ignoring refnames without digits, we 1003 | # filter out many common branch names like "release" and 1004 | # "stabilization", as well as "HEAD" and "master". 1005 | tags = set([r for r in refs if re.search(r'\d', r)]) 1006 | if verbose: 1007 | print("discarding '%s', no digits" % ",".join(refs - tags)) 1008 | if verbose: 1009 | print("likely tags: %s" % ",".join(sorted(tags))) 1010 | for ref in sorted(tags): 1011 | # sorting will prefer e.g. "2.0" over "2.0rc1" 1012 | if ref.startswith(tag_prefix): 1013 | r = ref[len(tag_prefix):] 1014 | if verbose: 1015 | print("picking %s" % r) 1016 | return {"version": r, 1017 | "full-revisionid": keywords["full"].strip(), 1018 | "dirty": False, "error": None, 1019 | "date": date} 1020 | # no suitable tags, so version is "0+unknown", but full hex is still there 1021 | if verbose: 1022 | print("no suitable tags, using unknown + full revision id") 1023 | return {"version": "0+unknown", 1024 | "full-revisionid": keywords["full"].strip(), 1025 | "dirty": False, "error": "no suitable tags", "date": None} 1026 | 1027 | 1028 | @register_vcs_handler("git", "pieces_from_vcs") 1029 | def git_pieces_from_vcs(tag_prefix, root, verbose, run_command=run_command): 1030 | """Get version from 'git describe' in the root of the source tree. 1031 | 1032 | This only gets called if the git-archive 'subst' keywords were *not* 1033 | expanded, and _version.py hasn't already been rewritten with a short 1034 | version string, meaning we're inside a checked out source tree. 1035 | """ 1036 | GITS = ["git"] 1037 | if sys.platform == "win32": 1038 | GITS = ["git.cmd", "git.exe"] 1039 | 1040 | out, rc = run_command(GITS, ["rev-parse", "--git-dir"], cwd=root, 1041 | hide_stderr=True) 1042 | if rc != 0: 1043 | if verbose: 1044 | print("Directory %s not under git control" % root) 1045 | raise NotThisMethod("'git rev-parse --git-dir' returned error") 1046 | 1047 | # if there is a tag matching tag_prefix, this yields TAG-NUM-gHEX[-dirty] 1048 | # if there isn't one, this yields HEX[-dirty] (no NUM) 1049 | describe_out, rc = run_command(GITS, ["describe", "--tags", "--dirty", 1050 | "--always", "--long", 1051 | "--match", "%s*" % tag_prefix], 1052 | cwd=root) 1053 | # --long was added in git-1.5.5 1054 | if describe_out is None: 1055 | raise NotThisMethod("'git describe' failed") 1056 | describe_out = describe_out.strip() 1057 | full_out, rc = run_command(GITS, ["rev-parse", "HEAD"], cwd=root) 1058 | if full_out is None: 1059 | raise NotThisMethod("'git rev-parse' failed") 1060 | full_out = full_out.strip() 1061 | 1062 | pieces = {} 1063 | pieces["long"] = full_out 1064 | pieces["short"] = full_out[:7] # maybe improved later 1065 | pieces["error"] = None 1066 | 1067 | # parse describe_out. It will be like TAG-NUM-gHEX[-dirty] or HEX[-dirty] 1068 | # TAG might have hyphens. 1069 | git_describe = describe_out 1070 | 1071 | # look for -dirty suffix 1072 | dirty = git_describe.endswith("-dirty") 1073 | pieces["dirty"] = dirty 1074 | if dirty: 1075 | git_describe = git_describe[:git_describe.rindex("-dirty")] 1076 | 1077 | # now we have TAG-NUM-gHEX or HEX 1078 | 1079 | if "-" in git_describe: 1080 | # TAG-NUM-gHEX 1081 | mo = re.search(r'^(.+)-(\d+)-g([0-9a-f]+)$', git_describe) 1082 | if not mo: 1083 | # unparseable. Maybe git-describe is misbehaving? 1084 | pieces["error"] = ("unable to parse git-describe output: '%s'" 1085 | % describe_out) 1086 | return pieces 1087 | 1088 | # tag 1089 | full_tag = mo.group(1) 1090 | if not full_tag.startswith(tag_prefix): 1091 | if verbose: 1092 | fmt = "tag '%s' doesn't start with prefix '%s'" 1093 | print(fmt % (full_tag, tag_prefix)) 1094 | pieces["error"] = ("tag '%s' doesn't start with prefix '%s'" 1095 | % (full_tag, tag_prefix)) 1096 | return pieces 1097 | pieces["closest-tag"] = full_tag[len(tag_prefix):] 1098 | 1099 | # distance: number of commits since tag 1100 | pieces["distance"] = int(mo.group(2)) 1101 | 1102 | # commit: short hex revision ID 1103 | pieces["short"] = mo.group(3) 1104 | 1105 | else: 1106 | # HEX: no tags 1107 | pieces["closest-tag"] = None 1108 | count_out, rc = run_command(GITS, ["rev-list", "HEAD", "--count"], 1109 | cwd=root) 1110 | pieces["distance"] = int(count_out) # total number of commits 1111 | 1112 | # commit date: see ISO-8601 comment in git_versions_from_keywords() 1113 | date = run_command(GITS, ["show", "-s", "--format=%ci", "HEAD"], 1114 | cwd=root)[0].strip() 1115 | pieces["date"] = date.strip().replace(" ", "T", 1).replace(" ", "", 1) 1116 | 1117 | return pieces 1118 | 1119 | 1120 | def do_vcs_install(manifest_in, versionfile_source, ipy): 1121 | """Git-specific installation logic for Versioneer. 1122 | 1123 | For Git, this means creating/changing .gitattributes to mark _version.py 1124 | for export-subst keyword substitution. 1125 | """ 1126 | GITS = ["git"] 1127 | if sys.platform == "win32": 1128 | GITS = ["git.cmd", "git.exe"] 1129 | files = [manifest_in, versionfile_source] 1130 | if ipy: 1131 | files.append(ipy) 1132 | try: 1133 | me = __file__ 1134 | if me.endswith(".pyc") or me.endswith(".pyo"): 1135 | me = os.path.splitext(me)[0] + ".py" 1136 | versioneer_file = os.path.relpath(me) 1137 | except NameError: 1138 | versioneer_file = "versioneer.py" 1139 | files.append(versioneer_file) 1140 | present = False 1141 | try: 1142 | f = open(".gitattributes", "r") 1143 | for line in f.readlines(): 1144 | if line.strip().startswith(versionfile_source): 1145 | if "export-subst" in line.strip().split()[1:]: 1146 | present = True 1147 | f.close() 1148 | except EnvironmentError: 1149 | pass 1150 | if not present: 1151 | f = open(".gitattributes", "a+") 1152 | f.write("%s export-subst\n" % versionfile_source) 1153 | f.close() 1154 | files.append(".gitattributes") 1155 | run_command(GITS, ["add", "--"] + files) 1156 | 1157 | 1158 | def versions_from_parentdir(parentdir_prefix, root, verbose): 1159 | """Try to determine the version from the parent directory name. 1160 | 1161 | Source tarballs conventionally unpack into a directory that includes both 1162 | the project name and a version string. We will also support searching up 1163 | two directory levels for an appropriately named parent directory 1164 | """ 1165 | rootdirs = [] 1166 | 1167 | for i in range(3): 1168 | dirname = os.path.basename(root) 1169 | if dirname.startswith(parentdir_prefix): 1170 | return {"version": dirname[len(parentdir_prefix):], 1171 | "full-revisionid": None, 1172 | "dirty": False, "error": None, "date": None} 1173 | else: 1174 | rootdirs.append(root) 1175 | root = os.path.dirname(root) # up a level 1176 | 1177 | if verbose: 1178 | print("Tried directories %s but none started with prefix %s" % 1179 | (str(rootdirs), parentdir_prefix)) 1180 | raise NotThisMethod("rootdir doesn't start with parentdir_prefix") 1181 | 1182 | 1183 | SHORT_VERSION_PY = """ 1184 | # This file was generated by 'versioneer.py' (0.18) from 1185 | # revision-control system data, or from the parent directory name of an 1186 | # unpacked source archive. Distribution tarballs contain a pre-generated copy 1187 | # of this file. 1188 | 1189 | import json 1190 | 1191 | version_json = ''' 1192 | %s 1193 | ''' # END VERSION_JSON 1194 | 1195 | 1196 | def get_versions(): 1197 | return json.loads(version_json) 1198 | """ 1199 | 1200 | 1201 | def versions_from_file(filename): 1202 | """Try to determine the version from _version.py if present.""" 1203 | try: 1204 | with open(filename) as f: 1205 | contents = f.read() 1206 | except EnvironmentError: 1207 | raise NotThisMethod("unable to read _version.py") 1208 | mo = re.search(r"version_json = '''\n(.*)''' # END VERSION_JSON", 1209 | contents, re.M | re.S) 1210 | if not mo: 1211 | mo = re.search(r"version_json = '''\r\n(.*)''' # END VERSION_JSON", 1212 | contents, re.M | re.S) 1213 | if not mo: 1214 | raise NotThisMethod("no version_json in _version.py") 1215 | return json.loads(mo.group(1)) 1216 | 1217 | 1218 | def write_to_version_file(filename, versions): 1219 | """Write the given version number to the given _version.py file.""" 1220 | os.unlink(filename) 1221 | contents = json.dumps(versions, sort_keys=True, 1222 | indent=1, separators=(",", ": ")) 1223 | with open(filename, "w") as f: 1224 | f.write(SHORT_VERSION_PY % contents) 1225 | 1226 | print("set %s to '%s'" % (filename, versions["version"])) 1227 | 1228 | 1229 | def plus_or_dot(pieces): 1230 | """Return a + if we don't already have one, else return a .""" 1231 | if "+" in pieces.get("closest-tag", ""): 1232 | return "." 1233 | return "+" 1234 | 1235 | 1236 | def render_pep440(pieces): 1237 | """Build up version string, with post-release "local version identifier". 1238 | 1239 | Our goal: TAG[+DISTANCE.gHEX[.dirty]] . Note that if you 1240 | get a tagged build and then dirty it, you'll get TAG+0.gHEX.dirty 1241 | 1242 | Exceptions: 1243 | 1: no tags. git_describe was just HEX. 0+untagged.DISTANCE.gHEX[.dirty] 1244 | """ 1245 | if pieces["closest-tag"]: 1246 | rendered = pieces["closest-tag"] 1247 | if pieces["distance"] or pieces["dirty"]: 1248 | rendered += plus_or_dot(pieces) 1249 | rendered += "%d.g%s" % (pieces["distance"], pieces["short"]) 1250 | if pieces["dirty"]: 1251 | rendered += ".dirty" 1252 | else: 1253 | # exception #1 1254 | rendered = "0+untagged.%d.g%s" % (pieces["distance"], 1255 | pieces["short"]) 1256 | if pieces["dirty"]: 1257 | rendered += ".dirty" 1258 | return rendered 1259 | 1260 | 1261 | def render_pep440_pre(pieces): 1262 | """TAG[.post.devDISTANCE] -- No -dirty. 1263 | 1264 | Exceptions: 1265 | 1: no tags. 0.post.devDISTANCE 1266 | """ 1267 | if pieces["closest-tag"]: 1268 | rendered = pieces["closest-tag"] 1269 | if pieces["distance"]: 1270 | rendered += ".post.dev%d" % pieces["distance"] 1271 | else: 1272 | # exception #1 1273 | rendered = "0.post.dev%d" % pieces["distance"] 1274 | return rendered 1275 | 1276 | 1277 | def render_pep440_post(pieces): 1278 | """TAG[.postDISTANCE[.dev0]+gHEX] . 1279 | 1280 | The ".dev0" means dirty. Note that .dev0 sorts backwards 1281 | (a dirty tree will appear "older" than the corresponding clean one), 1282 | but you shouldn't be releasing software with -dirty anyways. 1283 | 1284 | Exceptions: 1285 | 1: no tags. 0.postDISTANCE[.dev0] 1286 | """ 1287 | if pieces["closest-tag"]: 1288 | rendered = pieces["closest-tag"] 1289 | if pieces["distance"] or pieces["dirty"]: 1290 | rendered += ".post%d" % pieces["distance"] 1291 | if pieces["dirty"]: 1292 | rendered += ".dev0" 1293 | rendered += plus_or_dot(pieces) 1294 | rendered += "g%s" % pieces["short"] 1295 | else: 1296 | # exception #1 1297 | rendered = "0.post%d" % pieces["distance"] 1298 | if pieces["dirty"]: 1299 | rendered += ".dev0" 1300 | rendered += "+g%s" % pieces["short"] 1301 | return rendered 1302 | 1303 | 1304 | def render_pep440_old(pieces): 1305 | """TAG[.postDISTANCE[.dev0]] . 1306 | 1307 | The ".dev0" means dirty. 1308 | 1309 | Eexceptions: 1310 | 1: no tags. 0.postDISTANCE[.dev0] 1311 | """ 1312 | if pieces["closest-tag"]: 1313 | rendered = pieces["closest-tag"] 1314 | if pieces["distance"] or pieces["dirty"]: 1315 | rendered += ".post%d" % pieces["distance"] 1316 | if pieces["dirty"]: 1317 | rendered += ".dev0" 1318 | else: 1319 | # exception #1 1320 | rendered = "0.post%d" % pieces["distance"] 1321 | if pieces["dirty"]: 1322 | rendered += ".dev0" 1323 | return rendered 1324 | 1325 | 1326 | def render_git_describe(pieces): 1327 | """TAG[-DISTANCE-gHEX][-dirty]. 1328 | 1329 | Like 'git describe --tags --dirty --always'. 1330 | 1331 | Exceptions: 1332 | 1: no tags. HEX[-dirty] (note: no 'g' prefix) 1333 | """ 1334 | if pieces["closest-tag"]: 1335 | rendered = pieces["closest-tag"] 1336 | if pieces["distance"]: 1337 | rendered += "-%d-g%s" % (pieces["distance"], pieces["short"]) 1338 | else: 1339 | # exception #1 1340 | rendered = pieces["short"] 1341 | if pieces["dirty"]: 1342 | rendered += "-dirty" 1343 | return rendered 1344 | 1345 | 1346 | def render_git_describe_long(pieces): 1347 | """TAG-DISTANCE-gHEX[-dirty]. 1348 | 1349 | Like 'git describe --tags --dirty --always -long'. 1350 | The distance/hash is unconditional. 1351 | 1352 | Exceptions: 1353 | 1: no tags. HEX[-dirty] (note: no 'g' prefix) 1354 | """ 1355 | if pieces["closest-tag"]: 1356 | rendered = pieces["closest-tag"] 1357 | rendered += "-%d-g%s" % (pieces["distance"], pieces["short"]) 1358 | else: 1359 | # exception #1 1360 | rendered = pieces["short"] 1361 | if pieces["dirty"]: 1362 | rendered += "-dirty" 1363 | return rendered 1364 | 1365 | 1366 | def render(pieces, style): 1367 | """Render the given version pieces into the requested style.""" 1368 | if pieces["error"]: 1369 | return {"version": "unknown", 1370 | "full-revisionid": pieces.get("long"), 1371 | "dirty": None, 1372 | "error": pieces["error"], 1373 | "date": None} 1374 | 1375 | if not style or style == "default": 1376 | style = "pep440" # the default 1377 | 1378 | if style == "pep440": 1379 | rendered = render_pep440(pieces) 1380 | elif style == "pep440-pre": 1381 | rendered = render_pep440_pre(pieces) 1382 | elif style == "pep440-post": 1383 | rendered = render_pep440_post(pieces) 1384 | elif style == "pep440-old": 1385 | rendered = render_pep440_old(pieces) 1386 | elif style == "git-describe": 1387 | rendered = render_git_describe(pieces) 1388 | elif style == "git-describe-long": 1389 | rendered = render_git_describe_long(pieces) 1390 | else: 1391 | raise ValueError("unknown style '%s'" % style) 1392 | 1393 | return {"version": rendered, "full-revisionid": pieces["long"], 1394 | "dirty": pieces["dirty"], "error": None, 1395 | "date": pieces.get("date")} 1396 | 1397 | 1398 | class VersioneerBadRootError(Exception): 1399 | """The project root directory is unknown or missing key files.""" 1400 | 1401 | 1402 | def get_versions(verbose=False): 1403 | """Get the project version from whatever source is available. 1404 | 1405 | Returns dict with two keys: 'version' and 'full'. 1406 | """ 1407 | if "versioneer" in sys.modules: 1408 | # see the discussion in cmdclass.py:get_cmdclass() 1409 | del sys.modules["versioneer"] 1410 | 1411 | root = get_root() 1412 | cfg = get_config_from_root(root) 1413 | 1414 | assert cfg.VCS is not None, "please set [versioneer]VCS= in setup.cfg" 1415 | handlers = HANDLERS.get(cfg.VCS) 1416 | assert handlers, "unrecognized VCS '%s'" % cfg.VCS 1417 | verbose = verbose or cfg.verbose 1418 | assert cfg.versionfile_source is not None, \ 1419 | "please set versioneer.versionfile_source" 1420 | assert cfg.tag_prefix is not None, "please set versioneer.tag_prefix" 1421 | 1422 | versionfile_abs = os.path.join(root, cfg.versionfile_source) 1423 | 1424 | # extract version from first of: _version.py, VCS command (e.g. 'git 1425 | # describe'), parentdir. This is meant to work for developers using a 1426 | # source checkout, for users of a tarball created by 'setup.py sdist', 1427 | # and for users of a tarball/zipball created by 'git archive' or github's 1428 | # download-from-tag feature or the equivalent in other VCSes. 1429 | 1430 | get_keywords_f = handlers.get("get_keywords") 1431 | from_keywords_f = handlers.get("keywords") 1432 | if get_keywords_f and from_keywords_f: 1433 | try: 1434 | keywords = get_keywords_f(versionfile_abs) 1435 | ver = from_keywords_f(keywords, cfg.tag_prefix, verbose) 1436 | if verbose: 1437 | print("got version from expanded keyword %s" % ver) 1438 | return ver 1439 | except NotThisMethod: 1440 | pass 1441 | 1442 | try: 1443 | ver = versions_from_file(versionfile_abs) 1444 | if verbose: 1445 | print("got version from file %s %s" % (versionfile_abs, ver)) 1446 | return ver 1447 | except NotThisMethod: 1448 | pass 1449 | 1450 | from_vcs_f = handlers.get("pieces_from_vcs") 1451 | if from_vcs_f: 1452 | try: 1453 | pieces = from_vcs_f(cfg.tag_prefix, root, verbose) 1454 | ver = render(pieces, cfg.style) 1455 | if verbose: 1456 | print("got version from VCS %s" % ver) 1457 | return ver 1458 | except NotThisMethod: 1459 | pass 1460 | 1461 | try: 1462 | if cfg.parentdir_prefix: 1463 | ver = versions_from_parentdir(cfg.parentdir_prefix, root, verbose) 1464 | if verbose: 1465 | print("got version from parentdir %s" % ver) 1466 | return ver 1467 | except NotThisMethod: 1468 | pass 1469 | 1470 | if verbose: 1471 | print("unable to compute version") 1472 | 1473 | return {"version": "0+unknown", "full-revisionid": None, 1474 | "dirty": None, "error": "unable to compute version", 1475 | "date": None} 1476 | 1477 | 1478 | def get_version(): 1479 | """Get the short version string for this project.""" 1480 | return get_versions()["version"] 1481 | 1482 | 1483 | def get_cmdclass(): 1484 | """Get the custom setuptools/distutils subclasses used by Versioneer.""" 1485 | if "versioneer" in sys.modules: 1486 | del sys.modules["versioneer"] 1487 | # this fixes the "python setup.py develop" case (also 'install' and 1488 | # 'easy_install .'), in which subdependencies of the main project are 1489 | # built (using setup.py bdist_egg) in the same python process. Assume 1490 | # a main project A and a dependency B, which use different versions 1491 | # of Versioneer. A's setup.py imports A's Versioneer, leaving it in 1492 | # sys.modules by the time B's setup.py is executed, causing B to run 1493 | # with the wrong versioneer. Setuptools wraps the sub-dep builds in a 1494 | # sandbox that restores sys.modules to it's pre-build state, so the 1495 | # parent is protected against the child's "import versioneer". By 1496 | # removing ourselves from sys.modules here, before the child build 1497 | # happens, we protect the child from the parent's versioneer too. 1498 | # Also see https://github.com/warner/python-versioneer/issues/52 1499 | 1500 | cmds = {} 1501 | 1502 | # we add "version" to both distutils and setuptools 1503 | from distutils.core import Command 1504 | 1505 | class cmd_version(Command): 1506 | description = "report generated version string" 1507 | user_options = [] 1508 | boolean_options = [] 1509 | 1510 | def initialize_options(self): 1511 | pass 1512 | 1513 | def finalize_options(self): 1514 | pass 1515 | 1516 | def run(self): 1517 | vers = get_versions(verbose=True) 1518 | print("Version: %s" % vers["version"]) 1519 | print(" full-revisionid: %s" % vers.get("full-revisionid")) 1520 | print(" dirty: %s" % vers.get("dirty")) 1521 | print(" date: %s" % vers.get("date")) 1522 | if vers["error"]: 1523 | print(" error: %s" % vers["error"]) 1524 | cmds["version"] = cmd_version 1525 | 1526 | # we override "build_py" in both distutils and setuptools 1527 | # 1528 | # most invocation pathways end up running build_py: 1529 | # distutils/build -> build_py 1530 | # distutils/install -> distutils/build ->.. 1531 | # setuptools/bdist_wheel -> distutils/install ->.. 1532 | # setuptools/bdist_egg -> distutils/install_lib -> build_py 1533 | # setuptools/install -> bdist_egg ->.. 1534 | # setuptools/develop -> ? 1535 | # pip install: 1536 | # copies source tree to a tempdir before running egg_info/etc 1537 | # if .git isn't copied too, 'git describe' will fail 1538 | # then does setup.py bdist_wheel, or sometimes setup.py install 1539 | # setup.py egg_info -> ? 1540 | 1541 | # we override different "build_py" commands for both environments 1542 | if "setuptools" in sys.modules: 1543 | from setuptools.command.build_py import build_py as _build_py 1544 | else: 1545 | from distutils.command.build_py import build_py as _build_py 1546 | 1547 | class cmd_build_py(_build_py): 1548 | def run(self): 1549 | root = get_root() 1550 | cfg = get_config_from_root(root) 1551 | versions = get_versions() 1552 | _build_py.run(self) 1553 | # now locate _version.py in the new build/ directory and replace 1554 | # it with an updated value 1555 | if cfg.versionfile_build: 1556 | target_versionfile = os.path.join(self.build_lib, 1557 | cfg.versionfile_build) 1558 | print("UPDATING %s" % target_versionfile) 1559 | write_to_version_file(target_versionfile, versions) 1560 | cmds["build_py"] = cmd_build_py 1561 | 1562 | if "cx_Freeze" in sys.modules: # cx_freeze enabled? 1563 | from cx_Freeze.dist import build_exe as _build_exe 1564 | # nczeczulin reports that py2exe won't like the pep440-style string 1565 | # as FILEVERSION, but it can be used for PRODUCTVERSION, e.g. 1566 | # setup(console=[{ 1567 | # "version": versioneer.get_version().split("+", 1)[0], # FILEVERSION 1568 | # "product_version": versioneer.get_version(), 1569 | # ... 1570 | 1571 | class cmd_build_exe(_build_exe): 1572 | def run(self): 1573 | root = get_root() 1574 | cfg = get_config_from_root(root) 1575 | versions = get_versions() 1576 | target_versionfile = cfg.versionfile_source 1577 | print("UPDATING %s" % target_versionfile) 1578 | write_to_version_file(target_versionfile, versions) 1579 | 1580 | _build_exe.run(self) 1581 | os.unlink(target_versionfile) 1582 | with open(cfg.versionfile_source, "w") as f: 1583 | LONG = LONG_VERSION_PY[cfg.VCS] 1584 | f.write(LONG % 1585 | {"DOLLAR": "$", 1586 | "STYLE": cfg.style, 1587 | "TAG_PREFIX": cfg.tag_prefix, 1588 | "PARENTDIR_PREFIX": cfg.parentdir_prefix, 1589 | "VERSIONFILE_SOURCE": cfg.versionfile_source, 1590 | }) 1591 | cmds["build_exe"] = cmd_build_exe 1592 | del cmds["build_py"] 1593 | 1594 | if 'py2exe' in sys.modules: # py2exe enabled? 1595 | try: 1596 | from py2exe.distutils_buildexe import py2exe as _py2exe # py3 1597 | except ImportError: 1598 | from py2exe.build_exe import py2exe as _py2exe # py2 1599 | 1600 | class cmd_py2exe(_py2exe): 1601 | def run(self): 1602 | root = get_root() 1603 | cfg = get_config_from_root(root) 1604 | versions = get_versions() 1605 | target_versionfile = cfg.versionfile_source 1606 | print("UPDATING %s" % target_versionfile) 1607 | write_to_version_file(target_versionfile, versions) 1608 | 1609 | _py2exe.run(self) 1610 | os.unlink(target_versionfile) 1611 | with open(cfg.versionfile_source, "w") as f: 1612 | LONG = LONG_VERSION_PY[cfg.VCS] 1613 | f.write(LONG % 1614 | {"DOLLAR": "$", 1615 | "STYLE": cfg.style, 1616 | "TAG_PREFIX": cfg.tag_prefix, 1617 | "PARENTDIR_PREFIX": cfg.parentdir_prefix, 1618 | "VERSIONFILE_SOURCE": cfg.versionfile_source, 1619 | }) 1620 | cmds["py2exe"] = cmd_py2exe 1621 | 1622 | # we override different "sdist" commands for both environments 1623 | if "setuptools" in sys.modules: 1624 | from setuptools.command.sdist import sdist as _sdist 1625 | else: 1626 | from distutils.command.sdist import sdist as _sdist 1627 | 1628 | class cmd_sdist(_sdist): 1629 | def run(self): 1630 | versions = get_versions() 1631 | self._versioneer_generated_versions = versions 1632 | # unless we update this, the command will keep using the old 1633 | # version 1634 | self.distribution.metadata.version = versions["version"] 1635 | return _sdist.run(self) 1636 | 1637 | def make_release_tree(self, base_dir, files): 1638 | root = get_root() 1639 | cfg = get_config_from_root(root) 1640 | _sdist.make_release_tree(self, base_dir, files) 1641 | # now locate _version.py in the new base_dir directory 1642 | # (remembering that it may be a hardlink) and replace it with an 1643 | # updated value 1644 | target_versionfile = os.path.join(base_dir, cfg.versionfile_source) 1645 | print("UPDATING %s" % target_versionfile) 1646 | write_to_version_file(target_versionfile, 1647 | self._versioneer_generated_versions) 1648 | cmds["sdist"] = cmd_sdist 1649 | 1650 | return cmds 1651 | 1652 | 1653 | CONFIG_ERROR = """ 1654 | setup.cfg is missing the necessary Versioneer configuration. You need 1655 | a section like: 1656 | 1657 | [versioneer] 1658 | VCS = git 1659 | style = pep440 1660 | versionfile_source = src/myproject/_version.py 1661 | versionfile_build = myproject/_version.py 1662 | tag_prefix = 1663 | parentdir_prefix = myproject- 1664 | 1665 | You will also need to edit your setup.py to use the results: 1666 | 1667 | import versioneer 1668 | setup(version=versioneer.get_version(), 1669 | cmdclass=versioneer.get_cmdclass(), ...) 1670 | 1671 | Please read the docstring in ./versioneer.py for configuration instructions, 1672 | edit setup.cfg, and re-run the installer or 'python versioneer.py setup'. 1673 | """ 1674 | 1675 | SAMPLE_CONFIG = """ 1676 | # See the docstring in versioneer.py for instructions. Note that you must 1677 | # re-run 'versioneer.py setup' after changing this section, and commit the 1678 | # resulting files. 1679 | 1680 | [versioneer] 1681 | #VCS = git 1682 | #style = pep440 1683 | #versionfile_source = 1684 | #versionfile_build = 1685 | #tag_prefix = 1686 | #parentdir_prefix = 1687 | 1688 | """ 1689 | 1690 | INIT_PY_SNIPPET = """ 1691 | from ._version import get_versions 1692 | __version__ = get_versions()['version'] 1693 | del get_versions 1694 | """ 1695 | 1696 | 1697 | def do_setup(): 1698 | """Main VCS-independent setup function for installing Versioneer.""" 1699 | root = get_root() 1700 | try: 1701 | cfg = get_config_from_root(root) 1702 | except (EnvironmentError, configparser.NoSectionError, 1703 | configparser.NoOptionError) as e: 1704 | if isinstance(e, (EnvironmentError, configparser.NoSectionError)): 1705 | print("Adding sample versioneer config to setup.cfg", 1706 | file=sys.stderr) 1707 | with open(os.path.join(root, "setup.cfg"), "a") as f: 1708 | f.write(SAMPLE_CONFIG) 1709 | print(CONFIG_ERROR, file=sys.stderr) 1710 | return 1 1711 | 1712 | print(" creating %s" % cfg.versionfile_source) 1713 | with open(cfg.versionfile_source, "w") as f: 1714 | LONG = LONG_VERSION_PY[cfg.VCS] 1715 | f.write(LONG % {"DOLLAR": "$", 1716 | "STYLE": cfg.style, 1717 | "TAG_PREFIX": cfg.tag_prefix, 1718 | "PARENTDIR_PREFIX": cfg.parentdir_prefix, 1719 | "VERSIONFILE_SOURCE": cfg.versionfile_source, 1720 | }) 1721 | 1722 | ipy = os.path.join(os.path.dirname(cfg.versionfile_source), 1723 | "__init__.py") 1724 | if os.path.exists(ipy): 1725 | try: 1726 | with open(ipy, "r") as f: 1727 | old = f.read() 1728 | except EnvironmentError: 1729 | old = "" 1730 | if INIT_PY_SNIPPET not in old: 1731 | print(" appending to %s" % ipy) 1732 | with open(ipy, "a") as f: 1733 | f.write(INIT_PY_SNIPPET) 1734 | else: 1735 | print(" %s unmodified" % ipy) 1736 | else: 1737 | print(" %s doesn't exist, ok" % ipy) 1738 | ipy = None 1739 | 1740 | # Make sure both the top-level "versioneer.py" and versionfile_source 1741 | # (PKG/_version.py, used by runtime code) are in MANIFEST.in, so 1742 | # they'll be copied into source distributions. Pip won't be able to 1743 | # install the package without this. 1744 | manifest_in = os.path.join(root, "MANIFEST.in") 1745 | simple_includes = set() 1746 | try: 1747 | with open(manifest_in, "r") as f: 1748 | for line in f: 1749 | if line.startswith("include "): 1750 | for include in line.split()[1:]: 1751 | simple_includes.add(include) 1752 | except EnvironmentError: 1753 | pass 1754 | # That doesn't cover everything MANIFEST.in can do 1755 | # (http://docs.python.org/2/distutils/sourcedist.html#commands), so 1756 | # it might give some false negatives. Appending redundant 'include' 1757 | # lines is safe, though. 1758 | if "versioneer.py" not in simple_includes: 1759 | print(" appending 'versioneer.py' to MANIFEST.in") 1760 | with open(manifest_in, "a") as f: 1761 | f.write("include versioneer.py\n") 1762 | else: 1763 | print(" 'versioneer.py' already in MANIFEST.in") 1764 | if cfg.versionfile_source not in simple_includes: 1765 | print(" appending versionfile_source ('%s') to MANIFEST.in" % 1766 | cfg.versionfile_source) 1767 | with open(manifest_in, "a") as f: 1768 | f.write("include %s\n" % cfg.versionfile_source) 1769 | else: 1770 | print(" versionfile_source already in MANIFEST.in") 1771 | 1772 | # Make VCS-specific changes. For git, this means creating/changing 1773 | # .gitattributes to mark _version.py for export-subst keyword 1774 | # substitution. 1775 | do_vcs_install(manifest_in, cfg.versionfile_source, ipy) 1776 | return 0 1777 | 1778 | 1779 | def scan_setup_py(): 1780 | """Validate the contents of setup.py against Versioneer's expectations.""" 1781 | found = set() 1782 | setters = False 1783 | errors = 0 1784 | with open("setup.py", "r") as f: 1785 | for line in f.readlines(): 1786 | if "import versioneer" in line: 1787 | found.add("import") 1788 | if "versioneer.get_cmdclass()" in line: 1789 | found.add("cmdclass") 1790 | if "versioneer.get_version()" in line: 1791 | found.add("get_version") 1792 | if "versioneer.VCS" in line: 1793 | setters = True 1794 | if "versioneer.versionfile_source" in line: 1795 | setters = True 1796 | if len(found) != 3: 1797 | print("") 1798 | print("Your setup.py appears to be missing some important items") 1799 | print("(but I might be wrong). Please make sure it has something") 1800 | print("roughly like the following:") 1801 | print("") 1802 | print(" import versioneer") 1803 | print(" setup( version=versioneer.get_version(),") 1804 | print(" cmdclass=versioneer.get_cmdclass(), ...)") 1805 | print("") 1806 | errors += 1 1807 | if setters: 1808 | print("You should remove lines like 'versioneer.VCS = ' and") 1809 | print("'versioneer.versionfile_source = ' . This configuration") 1810 | print("now lives in setup.cfg, and should be removed from setup.py") 1811 | print("") 1812 | errors += 1 1813 | return errors 1814 | 1815 | 1816 | if __name__ == "__main__": 1817 | cmd = sys.argv[1] 1818 | if cmd == "setup": 1819 | errors = do_setup() 1820 | errors += scan_setup_py() 1821 | if errors: 1822 | sys.exit(1) 1823 | --------------------------------------------------------------------------------