0001-Update-versioneer-to-0.29.patch 85 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185
  1. From b283ce50c6f32387d0f474c935ed6015585b986c Mon Sep 17 00:00:00 2001
  2. From: Adam Duskett <adam.duskett@amarulasolutions.com>
  3. Date: Tue, 24 Oct 2023 09:54:40 +0200
  4. Subject: [PATCH] Update versioneer to 0.29
  5. Fixes builds against Python 3.12.0
  6. Upstream: https://github.com/magic-wormhole/magic-wormhole/pull/505
  7. Signed-off-by: Adam Duskett <adam.duskett@amarulasolutions.com>
  8. ---
  9. versioneer.py | 1348 ++++++++++++++++++++++++++++++++++---------------
  10. 1 file changed, 930 insertions(+), 418 deletions(-)
  11. diff --git a/versioneer.py b/versioneer.py
  12. index ebe628e..de97d90 100644
  13. --- a/versioneer.py
  14. +++ b/versioneer.py
  15. @@ -1,5 +1,4 @@
  16. -
  17. -# Version: 0.18
  18. +# Version: 0.29
  19. """The Versioneer - like a rocketeer, but for versions.
  20. @@ -7,18 +6,14 @@ The Versioneer
  21. ==============
  22. * like a rocketeer, but for versions!
  23. -* https://github.com/warner/python-versioneer
  24. +* https://github.com/python-versioneer/python-versioneer
  25. * Brian Warner
  26. -* License: Public Domain
  27. -* Compatible With: python2.6, 2.7, 3.2, 3.3, 3.4, 3.5, 3.6, and pypy
  28. -* [![Latest Version]
  29. -(https://pypip.in/version/versioneer/badge.svg?style=flat)
  30. -](https://pypi.python.org/pypi/versioneer/)
  31. -* [![Build Status]
  32. -(https://travis-ci.org/warner/python-versioneer.png?branch=master)
  33. -](https://travis-ci.org/warner/python-versioneer)
  34. -
  35. -This is a tool for managing a recorded version number in distutils-based
  36. +* License: Public Domain (Unlicense)
  37. +* Compatible with: Python 3.7, 3.8, 3.9, 3.10, 3.11 and pypy3
  38. +* [![Latest Version][pypi-image]][pypi-url]
  39. +* [![Build Status][travis-image]][travis-url]
  40. +
  41. +This is a tool for managing a recorded version number in setuptools-based
  42. python projects. The goal is to remove the tedious and error-prone "update
  43. the embedded version string" step from your release process. Making a new
  44. release should be as easy as recording a new tag in your version-control
  45. @@ -27,9 +22,38 @@ system, and maybe making new tarballs.
  46. ## Quick Install
  47. -* `pip install versioneer` to somewhere to your $PATH
  48. -* add a `[versioneer]` section to your setup.cfg (see below)
  49. -* run `versioneer install` in your source tree, commit the results
  50. +Versioneer provides two installation modes. The "classic" vendored mode installs
  51. +a copy of versioneer into your repository. The experimental build-time dependency mode
  52. +is intended to allow you to skip this step and simplify the process of upgrading.
  53. +
  54. +### Vendored mode
  55. +
  56. +* `pip install versioneer` to somewhere in your $PATH
  57. + * A [conda-forge recipe](https://github.com/conda-forge/versioneer-feedstock) is
  58. + available, so you can also use `conda install -c conda-forge versioneer`
  59. +* add a `[tool.versioneer]` section to your `pyproject.toml` or a
  60. + `[versioneer]` section to your `setup.cfg` (see [Install](INSTALL.md))
  61. + * Note that you will need to add `tomli; python_version < "3.11"` to your
  62. + build-time dependencies if you use `pyproject.toml`
  63. +* run `versioneer install --vendor` in your source tree, commit the results
  64. +* verify version information with `python setup.py version`
  65. +
  66. +### Build-time dependency mode
  67. +
  68. +* `pip install versioneer` to somewhere in your $PATH
  69. + * A [conda-forge recipe](https://github.com/conda-forge/versioneer-feedstock) is
  70. + available, so you can also use `conda install -c conda-forge versioneer`
  71. +* add a `[tool.versioneer]` section to your `pyproject.toml` or a
  72. + `[versioneer]` section to your `setup.cfg` (see [Install](INSTALL.md))
  73. +* add `versioneer` (with `[toml]` extra, if configuring in `pyproject.toml`)
  74. + to the `requires` key of the `build-system` table in `pyproject.toml`:
  75. + ```toml
  76. + [build-system]
  77. + requires = ["setuptools", "versioneer[toml]"]
  78. + build-backend = "setuptools.build_meta"
  79. + ```
  80. +* run `versioneer install --no-vendor` in your source tree, commit the results
  81. +* verify version information with `python setup.py version`
  82. ## Version Identifiers
  83. @@ -61,7 +85,7 @@ version 1.3). Many VCS systems can report a description that captures this,
  84. for example `git describe --tags --dirty --always` reports things like
  85. "0.7-1-g574ab98-dirty" to indicate that the checkout is one revision past the
  86. 0.7 tag, has a unique revision id of "574ab98", and is "dirty" (it has
  87. -uncommitted changes.
  88. +uncommitted changes).
  89. The version identifier is used for multiple purposes:
  90. @@ -166,7 +190,7 @@ which may help identify what went wrong).
  91. Some situations are known to cause problems for Versioneer. This details the
  92. most significant ones. More can be found on Github
  93. -[issues page](https://github.com/warner/python-versioneer/issues).
  94. +[issues page](https://github.com/python-versioneer/python-versioneer/issues).
  95. ### Subprojects
  96. @@ -194,9 +218,9 @@ work too.
  97. Pip-8.1.1 is known to have this problem, but hopefully it will get fixed in
  98. some later version.
  99. -[Bug #38](https://github.com/warner/python-versioneer/issues/38) is tracking
  100. +[Bug #38](https://github.com/python-versioneer/python-versioneer/issues/38) is tracking
  101. this issue. The discussion in
  102. -[PR #61](https://github.com/warner/python-versioneer/pull/61) describes the
  103. +[PR #61](https://github.com/python-versioneer/python-versioneer/pull/61) describes the
  104. issue from the Versioneer side in more detail.
  105. [pip PR#3176](https://github.com/pypa/pip/pull/3176) and
  106. [pip PR#3615](https://github.com/pypa/pip/pull/3615) contain work to improve
  107. @@ -224,31 +248,20 @@ regenerated while a different version is checked out. Many setup.py commands
  108. cause egg_info to be rebuilt (including `sdist`, `wheel`, and installing into
  109. a different virtualenv), so this can be surprising.
  110. -[Bug #83](https://github.com/warner/python-versioneer/issues/83) describes
  111. +[Bug #83](https://github.com/python-versioneer/python-versioneer/issues/83) describes
  112. this one, but upgrading to a newer version of setuptools should probably
  113. resolve it.
  114. -### Unicode version strings
  115. -
  116. -While Versioneer works (and is continually tested) with both Python 2 and
  117. -Python 3, it is not entirely consistent with bytes-vs-unicode distinctions.
  118. -Newer releases probably generate unicode version strings on py2. It's not
  119. -clear that this is wrong, but it may be surprising for applications when then
  120. -write these strings to a network connection or include them in bytes-oriented
  121. -APIs like cryptographic checksums.
  122. -
  123. -[Bug #71](https://github.com/warner/python-versioneer/issues/71) investigates
  124. -this question.
  125. -
  126. ## Updating Versioneer
  127. To upgrade your project to a new release of Versioneer, do the following:
  128. * install the new Versioneer (`pip install -U versioneer` or equivalent)
  129. -* edit `setup.cfg`, if necessary, to include any new configuration settings
  130. - indicated by the release notes. See [UPGRADING](./UPGRADING.md) for details.
  131. -* re-run `versioneer install` in your source tree, to replace
  132. +* edit `setup.cfg` and `pyproject.toml`, if necessary,
  133. + to include any new configuration settings indicated by the release notes.
  134. + See [UPGRADING](./UPGRADING.md) for details.
  135. +* re-run `versioneer install --[no-]vendor` in your source tree, to replace
  136. `SRC/_version.py`
  137. * commit any changed files
  138. @@ -265,35 +278,70 @@ installation by editing setup.py . Alternatively, it might go the other
  139. direction and include code from all supported VCS systems, reducing the
  140. number of intermediate scripts.
  141. +## Similar projects
  142. +
  143. +* [setuptools_scm](https://github.com/pypa/setuptools_scm/) - a non-vendored build-time
  144. + dependency
  145. +* [minver](https://github.com/jbweston/miniver) - a lightweight reimplementation of
  146. + versioneer
  147. +* [versioningit](https://github.com/jwodder/versioningit) - a PEP 518-based setuptools
  148. + plugin
  149. ## License
  150. To make Versioneer easier to embed, all its code is dedicated to the public
  151. domain. The `_version.py` that it creates is also in the public domain.
  152. -Specifically, both are released under the Creative Commons "Public Domain
  153. -Dedication" license (CC0-1.0), as described in
  154. -https://creativecommons.org/publicdomain/zero/1.0/ .
  155. +Specifically, both are released under the "Unlicense", as described in
  156. +https://unlicense.org/.
  157. +
  158. +[pypi-image]: https://img.shields.io/pypi/v/versioneer.svg
  159. +[pypi-url]: https://pypi.python.org/pypi/versioneer/
  160. +[travis-image]:
  161. +https://img.shields.io/travis/com/python-versioneer/python-versioneer.svg
  162. +[travis-url]: https://travis-ci.com/github/python-versioneer/python-versioneer
  163. """
  164. +# pylint:disable=invalid-name,import-outside-toplevel,missing-function-docstring
  165. +# pylint:disable=missing-class-docstring,too-many-branches,too-many-statements
  166. +# pylint:disable=raise-missing-from,too-many-lines,too-many-locals,import-error
  167. +# pylint:disable=too-few-public-methods,redefined-outer-name,consider-using-with
  168. +# pylint:disable=attribute-defined-outside-init,too-many-arguments
  169. -from __future__ import print_function
  170. -try:
  171. - import configparser
  172. -except ImportError:
  173. - import ConfigParser as configparser
  174. +import configparser
  175. import errno
  176. import json
  177. import os
  178. import re
  179. import subprocess
  180. import sys
  181. +from pathlib import Path
  182. +from typing import Any, Callable, cast, Dict, List, Optional, Tuple, Union
  183. +from typing import NoReturn
  184. +import functools
  185. +
  186. +have_tomllib = True
  187. +if sys.version_info >= (3, 11):
  188. + import tomllib
  189. +else:
  190. + try:
  191. + import tomli as tomllib
  192. + except ImportError:
  193. + have_tomllib = False
  194. class VersioneerConfig:
  195. """Container for Versioneer configuration parameters."""
  196. + VCS: str
  197. + style: str
  198. + tag_prefix: str
  199. + versionfile_source: str
  200. + versionfile_build: Optional[str]
  201. + parentdir_prefix: Optional[str]
  202. + verbose: Optional[bool]
  203. +
  204. -def get_root():
  205. +def get_root() -> str:
  206. """Get the project root directory.
  207. We require that all commands are run from the project root, i.e. the
  208. @@ -301,18 +349,30 @@ def get_root():
  209. """
  210. root = os.path.realpath(os.path.abspath(os.getcwd()))
  211. setup_py = os.path.join(root, "setup.py")
  212. + pyproject_toml = os.path.join(root, "pyproject.toml")
  213. versioneer_py = os.path.join(root, "versioneer.py")
  214. - if not (os.path.exists(setup_py) or os.path.exists(versioneer_py)):
  215. + if not (
  216. + os.path.exists(setup_py)
  217. + or os.path.exists(pyproject_toml)
  218. + or os.path.exists(versioneer_py)
  219. + ):
  220. # allow 'python path/to/setup.py COMMAND'
  221. root = os.path.dirname(os.path.realpath(os.path.abspath(sys.argv[0])))
  222. setup_py = os.path.join(root, "setup.py")
  223. + pyproject_toml = os.path.join(root, "pyproject.toml")
  224. versioneer_py = os.path.join(root, "versioneer.py")
  225. - if not (os.path.exists(setup_py) or os.path.exists(versioneer_py)):
  226. - err = ("Versioneer was unable to run the project root directory. "
  227. - "Versioneer requires setup.py to be executed from "
  228. - "its immediate directory (like 'python setup.py COMMAND'), "
  229. - "or in a way that lets it use sys.argv[0] to find the root "
  230. - "(like 'python path/to/setup.py COMMAND').")
  231. + if not (
  232. + os.path.exists(setup_py)
  233. + or os.path.exists(pyproject_toml)
  234. + or os.path.exists(versioneer_py)
  235. + ):
  236. + err = (
  237. + "Versioneer was unable to run the project root directory. "
  238. + "Versioneer requires setup.py to be executed from "
  239. + "its immediate directory (like 'python setup.py COMMAND'), "
  240. + "or in a way that lets it use sys.argv[0] to find the root "
  241. + "(like 'python path/to/setup.py COMMAND')."
  242. + )
  243. raise VersioneerBadRootError(err)
  244. try:
  245. # Certain runtime workflows (setup.py install/develop in a setuptools
  246. @@ -321,43 +381,64 @@ def get_root():
  247. # module-import table will cache the first one. So we can't use
  248. # os.path.dirname(__file__), as that will find whichever
  249. # versioneer.py was first imported, even in later projects.
  250. - me = os.path.realpath(os.path.abspath(__file__))
  251. - me_dir = os.path.normcase(os.path.splitext(me)[0])
  252. + my_path = os.path.realpath(os.path.abspath(__file__))
  253. + me_dir = os.path.normcase(os.path.splitext(my_path)[0])
  254. vsr_dir = os.path.normcase(os.path.splitext(versioneer_py)[0])
  255. - if me_dir != vsr_dir:
  256. - print("Warning: build in %s is using versioneer.py from %s"
  257. - % (os.path.dirname(me), versioneer_py))
  258. + if me_dir != vsr_dir and "VERSIONEER_PEP518" not in globals():
  259. + print(
  260. + "Warning: build in %s is using versioneer.py from %s"
  261. + % (os.path.dirname(my_path), versioneer_py)
  262. + )
  263. except NameError:
  264. pass
  265. return root
  266. -def get_config_from_root(root):
  267. +def get_config_from_root(root: str) -> VersioneerConfig:
  268. """Read the project setup.cfg file to determine Versioneer config."""
  269. - # This might raise EnvironmentError (if setup.cfg is missing), or
  270. + # This might raise OSError (if setup.cfg is missing), or
  271. # configparser.NoSectionError (if it lacks a [versioneer] section), or
  272. # configparser.NoOptionError (if it lacks "VCS="). See the docstring at
  273. # the top of versioneer.py for instructions on writing your setup.cfg .
  274. - setup_cfg = os.path.join(root, "setup.cfg")
  275. - parser = configparser.SafeConfigParser()
  276. - with open(setup_cfg, "r") as f:
  277. - parser.readfp(f)
  278. - VCS = parser.get("versioneer", "VCS") # mandatory
  279. -
  280. - def get(parser, name):
  281. - if parser.has_option("versioneer", name):
  282. - return parser.get("versioneer", name)
  283. - return None
  284. + root_pth = Path(root)
  285. + pyproject_toml = root_pth / "pyproject.toml"
  286. + setup_cfg = root_pth / "setup.cfg"
  287. + section: Union[Dict[str, Any], configparser.SectionProxy, None] = None
  288. + if pyproject_toml.exists() and have_tomllib:
  289. + try:
  290. + with open(pyproject_toml, "rb") as fobj:
  291. + pp = tomllib.load(fobj)
  292. + section = pp["tool"]["versioneer"]
  293. + except (tomllib.TOMLDecodeError, KeyError) as e:
  294. + print(f"Failed to load config from {pyproject_toml}: {e}")
  295. + print("Try to load it from setup.cfg")
  296. + if not section:
  297. + parser = configparser.ConfigParser()
  298. + with open(setup_cfg) as cfg_file:
  299. + parser.read_file(cfg_file)
  300. + parser.get("versioneer", "VCS") # raise error if missing
  301. +
  302. + section = parser["versioneer"]
  303. +
  304. + # `cast`` really shouldn't be used, but its simplest for the
  305. + # common VersioneerConfig users at the moment. We verify against
  306. + # `None` values elsewhere where it matters
  307. +
  308. cfg = VersioneerConfig()
  309. - cfg.VCS = VCS
  310. - cfg.style = get(parser, "style") or ""
  311. - cfg.versionfile_source = get(parser, "versionfile_source")
  312. - cfg.versionfile_build = get(parser, "versionfile_build")
  313. - cfg.tag_prefix = get(parser, "tag_prefix")
  314. - if cfg.tag_prefix in ("''", '""'):
  315. + cfg.VCS = section["VCS"]
  316. + cfg.style = section.get("style", "")
  317. + cfg.versionfile_source = cast(str, section.get("versionfile_source"))
  318. + cfg.versionfile_build = section.get("versionfile_build")
  319. + cfg.tag_prefix = cast(str, section.get("tag_prefix"))
  320. + if cfg.tag_prefix in ("''", '""', None):
  321. cfg.tag_prefix = ""
  322. - cfg.parentdir_prefix = get(parser, "parentdir_prefix")
  323. - cfg.verbose = get(parser, "verbose")
  324. + cfg.parentdir_prefix = section.get("parentdir_prefix")
  325. + if isinstance(section, configparser.SectionProxy):
  326. + # Make sure configparser translates to bool
  327. + cfg.verbose = section.getboolean("verbose")
  328. + else:
  329. + cfg.verbose = section.get("verbose")
  330. +
  331. return cfg
  332. @@ -366,37 +447,54 @@ class NotThisMethod(Exception):
  333. # these dictionaries contain VCS-specific tools
  334. -LONG_VERSION_PY = {}
  335. -HANDLERS = {}
  336. +LONG_VERSION_PY: Dict[str, str] = {}
  337. +HANDLERS: Dict[str, Dict[str, Callable]] = {}
  338. -def register_vcs_handler(vcs, method): # decorator
  339. - """Decorator to mark a method as the handler for a particular VCS."""
  340. - def decorate(f):
  341. +def register_vcs_handler(vcs: str, method: str) -> Callable: # decorator
  342. + """Create decorator to mark a method as the handler of a VCS."""
  343. +
  344. + def decorate(f: Callable) -> Callable:
  345. """Store f in HANDLERS[vcs][method]."""
  346. - if vcs not in HANDLERS:
  347. - HANDLERS[vcs] = {}
  348. - HANDLERS[vcs][method] = f
  349. + HANDLERS.setdefault(vcs, {})[method] = f
  350. return f
  351. +
  352. return decorate
  353. -def run_command(commands, args, cwd=None, verbose=False, hide_stderr=False,
  354. - env=None):
  355. +def run_command(
  356. + commands: List[str],
  357. + args: List[str],
  358. + cwd: Optional[str] = None,
  359. + verbose: bool = False,
  360. + hide_stderr: bool = False,
  361. + env: Optional[Dict[str, str]] = None,
  362. +) -> Tuple[Optional[str], Optional[int]]:
  363. """Call the given command(s)."""
  364. assert isinstance(commands, list)
  365. - p = None
  366. - for c in commands:
  367. + process = None
  368. +
  369. + popen_kwargs: Dict[str, Any] = {}
  370. + if sys.platform == "win32":
  371. + # This hides the console window if pythonw.exe is used
  372. + startupinfo = subprocess.STARTUPINFO()
  373. + startupinfo.dwFlags |= subprocess.STARTF_USESHOWWINDOW
  374. + popen_kwargs["startupinfo"] = startupinfo
  375. +
  376. + for command in commands:
  377. try:
  378. - dispcmd = str([c] + args)
  379. + dispcmd = str([command] + args)
  380. # remember shell=False, so use git.cmd on windows, not just git
  381. - p = subprocess.Popen([c] + args, cwd=cwd, env=env,
  382. - stdout=subprocess.PIPE,
  383. - stderr=(subprocess.PIPE if hide_stderr
  384. - else None))
  385. + process = subprocess.Popen(
  386. + [command] + args,
  387. + cwd=cwd,
  388. + env=env,
  389. + stdout=subprocess.PIPE,
  390. + stderr=(subprocess.PIPE if hide_stderr else None),
  391. + **popen_kwargs,
  392. + )
  393. break
  394. - except EnvironmentError:
  395. - e = sys.exc_info()[1]
  396. + except OSError as e:
  397. if e.errno == errno.ENOENT:
  398. continue
  399. if verbose:
  400. @@ -407,26 +505,27 @@ def run_command(commands, args, cwd=None, verbose=False, hide_stderr=False,
  401. if verbose:
  402. print("unable to find command, tried %s" % (commands,))
  403. return None, None
  404. - stdout = p.communicate()[0].strip()
  405. - if sys.version_info[0] >= 3:
  406. - stdout = stdout.decode()
  407. - if p.returncode != 0:
  408. + stdout = process.communicate()[0].strip().decode()
  409. + if process.returncode != 0:
  410. if verbose:
  411. print("unable to run %s (error)" % dispcmd)
  412. print("stdout was %s" % stdout)
  413. - return None, p.returncode
  414. - return stdout, p.returncode
  415. + return None, process.returncode
  416. + return stdout, process.returncode
  417. -LONG_VERSION_PY['git'] = '''
  418. +LONG_VERSION_PY[
  419. + "git"
  420. +] = r'''
  421. # This file helps to compute a version number in source trees obtained from
  422. # git-archive tarball (such as those provided by githubs download-from-tag
  423. # feature). Distribution tarballs (built by setup.py sdist) and build
  424. # directories (produced by setup.py build) will contain a much shorter file
  425. # that just contains the computed version number.
  426. -# This file is released into the public domain. Generated by
  427. -# versioneer-0.18 (https://github.com/warner/python-versioneer)
  428. +# This file is released into the public domain.
  429. +# Generated by versioneer-0.29
  430. +# https://github.com/python-versioneer/python-versioneer
  431. """Git implementation of _version.py."""
  432. @@ -435,9 +534,11 @@ import os
  433. import re
  434. import subprocess
  435. import sys
  436. +from typing import Any, Callable, Dict, List, Optional, Tuple
  437. +import functools
  438. -def get_keywords():
  439. +def get_keywords() -> Dict[str, str]:
  440. """Get the keywords needed to look up the version information."""
  441. # these strings will be replaced by git during git-archive.
  442. # setup.py/versioneer.py will grep for the variable names, so they must
  443. @@ -453,8 +554,15 @@ def get_keywords():
  444. class VersioneerConfig:
  445. """Container for Versioneer configuration parameters."""
  446. + VCS: str
  447. + style: str
  448. + tag_prefix: str
  449. + parentdir_prefix: str
  450. + versionfile_source: str
  451. + verbose: bool
  452. +
  453. -def get_config():
  454. +def get_config() -> VersioneerConfig:
  455. """Create, populate and return the VersioneerConfig() object."""
  456. # these strings are filled in when 'setup.py versioneer' creates
  457. # _version.py
  458. @@ -472,13 +580,13 @@ class NotThisMethod(Exception):
  459. """Exception raised if a method is not valid for the current scenario."""
  460. -LONG_VERSION_PY = {}
  461. -HANDLERS = {}
  462. +LONG_VERSION_PY: Dict[str, str] = {}
  463. +HANDLERS: Dict[str, Dict[str, Callable]] = {}
  464. -def register_vcs_handler(vcs, method): # decorator
  465. - """Decorator to mark a method as the handler for a particular VCS."""
  466. - def decorate(f):
  467. +def register_vcs_handler(vcs: str, method: str) -> Callable: # decorator
  468. + """Create decorator to mark a method as the handler of a VCS."""
  469. + def decorate(f: Callable) -> Callable:
  470. """Store f in HANDLERS[vcs][method]."""
  471. if vcs not in HANDLERS:
  472. HANDLERS[vcs] = {}
  473. @@ -487,22 +595,35 @@ def register_vcs_handler(vcs, method): # decorator
  474. return decorate
  475. -def run_command(commands, args, cwd=None, verbose=False, hide_stderr=False,
  476. - env=None):
  477. +def run_command(
  478. + commands: List[str],
  479. + args: List[str],
  480. + cwd: Optional[str] = None,
  481. + verbose: bool = False,
  482. + hide_stderr: bool = False,
  483. + env: Optional[Dict[str, str]] = None,
  484. +) -> Tuple[Optional[str], Optional[int]]:
  485. """Call the given command(s)."""
  486. assert isinstance(commands, list)
  487. - p = None
  488. - for c in commands:
  489. + process = None
  490. +
  491. + popen_kwargs: Dict[str, Any] = {}
  492. + if sys.platform == "win32":
  493. + # This hides the console window if pythonw.exe is used
  494. + startupinfo = subprocess.STARTUPINFO()
  495. + startupinfo.dwFlags |= subprocess.STARTF_USESHOWWINDOW
  496. + popen_kwargs["startupinfo"] = startupinfo
  497. +
  498. + for command in commands:
  499. try:
  500. - dispcmd = str([c] + args)
  501. + dispcmd = str([command] + args)
  502. # remember shell=False, so use git.cmd on windows, not just git
  503. - p = subprocess.Popen([c] + args, cwd=cwd, env=env,
  504. - stdout=subprocess.PIPE,
  505. - stderr=(subprocess.PIPE if hide_stderr
  506. - else None))
  507. + process = subprocess.Popen([command] + args, cwd=cwd, env=env,
  508. + stdout=subprocess.PIPE,
  509. + stderr=(subprocess.PIPE if hide_stderr
  510. + else None), **popen_kwargs)
  511. break
  512. - except EnvironmentError:
  513. - e = sys.exc_info()[1]
  514. + except OSError as e:
  515. if e.errno == errno.ENOENT:
  516. continue
  517. if verbose:
  518. @@ -513,18 +634,20 @@ def run_command(commands, args, cwd=None, verbose=False, hide_stderr=False,
  519. if verbose:
  520. print("unable to find command, tried %%s" %% (commands,))
  521. return None, None
  522. - stdout = p.communicate()[0].strip()
  523. - if sys.version_info[0] >= 3:
  524. - stdout = stdout.decode()
  525. - if p.returncode != 0:
  526. + stdout = process.communicate()[0].strip().decode()
  527. + if process.returncode != 0:
  528. if verbose:
  529. print("unable to run %%s (error)" %% dispcmd)
  530. print("stdout was %%s" %% stdout)
  531. - return None, p.returncode
  532. - return stdout, p.returncode
  533. + return None, process.returncode
  534. + return stdout, process.returncode
  535. -def versions_from_parentdir(parentdir_prefix, root, verbose):
  536. +def versions_from_parentdir(
  537. + parentdir_prefix: str,
  538. + root: str,
  539. + verbose: bool,
  540. +) -> Dict[str, Any]:
  541. """Try to determine the version from the parent directory name.
  542. Source tarballs conventionally unpack into a directory that includes both
  543. @@ -533,15 +656,14 @@ def versions_from_parentdir(parentdir_prefix, root, verbose):
  544. """
  545. rootdirs = []
  546. - for i in range(3):
  547. + for _ in range(3):
  548. dirname = os.path.basename(root)
  549. if dirname.startswith(parentdir_prefix):
  550. return {"version": dirname[len(parentdir_prefix):],
  551. "full-revisionid": None,
  552. "dirty": False, "error": None, "date": None}
  553. - else:
  554. - rootdirs.append(root)
  555. - root = os.path.dirname(root) # up a level
  556. + rootdirs.append(root)
  557. + root = os.path.dirname(root) # up a level
  558. if verbose:
  559. print("Tried directories %%s but none started with prefix %%s" %%
  560. @@ -550,41 +672,48 @@ def versions_from_parentdir(parentdir_prefix, root, verbose):
  561. @register_vcs_handler("git", "get_keywords")
  562. -def git_get_keywords(versionfile_abs):
  563. +def git_get_keywords(versionfile_abs: str) -> Dict[str, str]:
  564. """Extract version information from the given file."""
  565. # the code embedded in _version.py can just fetch the value of these
  566. # keywords. When used from setup.py, we don't want to import _version.py,
  567. # so we do it with a regexp instead. This function is not used from
  568. # _version.py.
  569. - keywords = {}
  570. + keywords: Dict[str, str] = {}
  571. try:
  572. - f = open(versionfile_abs, "r")
  573. - for line in f.readlines():
  574. - if line.strip().startswith("git_refnames ="):
  575. - mo = re.search(r'=\s*"(.*)"', line)
  576. - if mo:
  577. - keywords["refnames"] = mo.group(1)
  578. - if line.strip().startswith("git_full ="):
  579. - mo = re.search(r'=\s*"(.*)"', line)
  580. - if mo:
  581. - keywords["full"] = mo.group(1)
  582. - if line.strip().startswith("git_date ="):
  583. - mo = re.search(r'=\s*"(.*)"', line)
  584. - if mo:
  585. - keywords["date"] = mo.group(1)
  586. - f.close()
  587. - except EnvironmentError:
  588. + with open(versionfile_abs, "r") as fobj:
  589. + for line in fobj:
  590. + if line.strip().startswith("git_refnames ="):
  591. + mo = re.search(r'=\s*"(.*)"', line)
  592. + if mo:
  593. + keywords["refnames"] = mo.group(1)
  594. + if line.strip().startswith("git_full ="):
  595. + mo = re.search(r'=\s*"(.*)"', line)
  596. + if mo:
  597. + keywords["full"] = mo.group(1)
  598. + if line.strip().startswith("git_date ="):
  599. + mo = re.search(r'=\s*"(.*)"', line)
  600. + if mo:
  601. + keywords["date"] = mo.group(1)
  602. + except OSError:
  603. pass
  604. return keywords
  605. @register_vcs_handler("git", "keywords")
  606. -def git_versions_from_keywords(keywords, tag_prefix, verbose):
  607. +def git_versions_from_keywords(
  608. + keywords: Dict[str, str],
  609. + tag_prefix: str,
  610. + verbose: bool,
  611. +) -> Dict[str, Any]:
  612. """Get version information from git keywords."""
  613. - if not keywords:
  614. - raise NotThisMethod("no keywords at all, weird")
  615. + if "refnames" not in keywords:
  616. + raise NotThisMethod("Short version file found")
  617. date = keywords.get("date")
  618. if date is not None:
  619. + # Use only the last line. Previous lines may contain GPG signature
  620. + # information.
  621. + date = date.splitlines()[-1]
  622. +
  623. # git-2.2.0 added "%%cI", which expands to an ISO-8601 -compliant
  624. # datestamp. However we prefer "%%ci" (which expands to an "ISO-8601
  625. # -like" string, which we must then edit to make compliant), because
  626. @@ -597,11 +726,11 @@ def git_versions_from_keywords(keywords, tag_prefix, verbose):
  627. if verbose:
  628. print("keywords are unexpanded, not using")
  629. raise NotThisMethod("unexpanded keywords, not a git-archive tarball")
  630. - refs = set([r.strip() for r in refnames.strip("()").split(",")])
  631. + refs = {r.strip() for r in refnames.strip("()").split(",")}
  632. # starting in git-1.8.3, tags are listed as "tag: foo-1.0" instead of
  633. # just "foo-1.0". If we see a "tag: " prefix, prefer those.
  634. TAG = "tag: "
  635. - tags = set([r[len(TAG):] for r in refs if r.startswith(TAG)])
  636. + tags = {r[len(TAG):] for r in refs if r.startswith(TAG)}
  637. if not tags:
  638. # Either we're using git < 1.8.3, or there really are no tags. We use
  639. # a heuristic: assume all version tags have a digit. The old git %%d
  640. @@ -610,7 +739,7 @@ def git_versions_from_keywords(keywords, tag_prefix, verbose):
  641. # between branches and tags. By ignoring refnames without digits, we
  642. # filter out many common branch names like "release" and
  643. # "stabilization", as well as "HEAD" and "master".
  644. - tags = set([r for r in refs if re.search(r'\d', r)])
  645. + tags = {r for r in refs if re.search(r'\d', r)}
  646. if verbose:
  647. print("discarding '%%s', no digits" %% ",".join(refs - tags))
  648. if verbose:
  649. @@ -619,6 +748,11 @@ def git_versions_from_keywords(keywords, tag_prefix, verbose):
  650. # sorting will prefer e.g. "2.0" over "2.0rc1"
  651. if ref.startswith(tag_prefix):
  652. r = ref[len(tag_prefix):]
  653. + # Filter out refs that exactly match prefix or that don't start
  654. + # with a number once the prefix is stripped (mostly a concern
  655. + # when prefix is '')
  656. + if not re.match(r'\d', r):
  657. + continue
  658. if verbose:
  659. print("picking %%s" %% r)
  660. return {"version": r,
  661. @@ -634,7 +768,12 @@ def git_versions_from_keywords(keywords, tag_prefix, verbose):
  662. @register_vcs_handler("git", "pieces_from_vcs")
  663. -def git_pieces_from_vcs(tag_prefix, root, verbose, run_command=run_command):
  664. +def git_pieces_from_vcs(
  665. + tag_prefix: str,
  666. + root: str,
  667. + verbose: bool,
  668. + runner: Callable = run_command
  669. +) -> Dict[str, Any]:
  670. """Get version from 'git describe' in the root of the source tree.
  671. This only gets called if the git-archive 'subst' keywords were *not*
  672. @@ -645,8 +784,15 @@ def git_pieces_from_vcs(tag_prefix, root, verbose, run_command=run_command):
  673. if sys.platform == "win32":
  674. GITS = ["git.cmd", "git.exe"]
  675. - out, rc = run_command(GITS, ["rev-parse", "--git-dir"], cwd=root,
  676. - hide_stderr=True)
  677. + # GIT_DIR can interfere with correct operation of Versioneer.
  678. + # It may be intended to be passed to the Versioneer-versioned project,
  679. + # but that should not change where we get our version from.
  680. + env = os.environ.copy()
  681. + env.pop("GIT_DIR", None)
  682. + runner = functools.partial(runner, env=env)
  683. +
  684. + _, rc = runner(GITS, ["rev-parse", "--git-dir"], cwd=root,
  685. + hide_stderr=not verbose)
  686. if rc != 0:
  687. if verbose:
  688. print("Directory %%s not under git control" %% root)
  689. @@ -654,24 +800,57 @@ def git_pieces_from_vcs(tag_prefix, root, verbose, run_command=run_command):
  690. # if there is a tag matching tag_prefix, this yields TAG-NUM-gHEX[-dirty]
  691. # if there isn't one, this yields HEX[-dirty] (no NUM)
  692. - describe_out, rc = run_command(GITS, ["describe", "--tags", "--dirty",
  693. - "--always", "--long",
  694. - "--match", "%%s*" %% tag_prefix],
  695. - cwd=root)
  696. + describe_out, rc = runner(GITS, [
  697. + "describe", "--tags", "--dirty", "--always", "--long",
  698. + "--match", f"{tag_prefix}[[:digit:]]*"
  699. + ], cwd=root)
  700. # --long was added in git-1.5.5
  701. if describe_out is None:
  702. raise NotThisMethod("'git describe' failed")
  703. describe_out = describe_out.strip()
  704. - full_out, rc = run_command(GITS, ["rev-parse", "HEAD"], cwd=root)
  705. + full_out, rc = runner(GITS, ["rev-parse", "HEAD"], cwd=root)
  706. if full_out is None:
  707. raise NotThisMethod("'git rev-parse' failed")
  708. full_out = full_out.strip()
  709. - pieces = {}
  710. + pieces: Dict[str, Any] = {}
  711. pieces["long"] = full_out
  712. pieces["short"] = full_out[:7] # maybe improved later
  713. pieces["error"] = None
  714. + branch_name, rc = runner(GITS, ["rev-parse", "--abbrev-ref", "HEAD"],
  715. + cwd=root)
  716. + # --abbrev-ref was added in git-1.6.3
  717. + if rc != 0 or branch_name is None:
  718. + raise NotThisMethod("'git rev-parse --abbrev-ref' returned error")
  719. + branch_name = branch_name.strip()
  720. +
  721. + if branch_name == "HEAD":
  722. + # If we aren't exactly on a branch, pick a branch which represents
  723. + # the current commit. If all else fails, we are on a branchless
  724. + # commit.
  725. + branches, rc = runner(GITS, ["branch", "--contains"], cwd=root)
  726. + # --contains was added in git-1.5.4
  727. + if rc != 0 or branches is None:
  728. + raise NotThisMethod("'git branch --contains' returned error")
  729. + branches = branches.split("\n")
  730. +
  731. + # Remove the first line if we're running detached
  732. + if "(" in branches[0]:
  733. + branches.pop(0)
  734. +
  735. + # Strip off the leading "* " from the list of branches.
  736. + branches = [branch[2:] for branch in branches]
  737. + if "master" in branches:
  738. + branch_name = "master"
  739. + elif not branches:
  740. + branch_name = None
  741. + else:
  742. + # Pick the first branch that is returned. Good or bad.
  743. + branch_name = branches[0]
  744. +
  745. + pieces["branch"] = branch_name
  746. +
  747. # parse describe_out. It will be like TAG-NUM-gHEX[-dirty] or HEX[-dirty]
  748. # TAG might have hyphens.
  749. git_describe = describe_out
  750. @@ -688,7 +867,7 @@ def git_pieces_from_vcs(tag_prefix, root, verbose, run_command=run_command):
  751. # TAG-NUM-gHEX
  752. mo = re.search(r'^(.+)-(\d+)-g([0-9a-f]+)$', git_describe)
  753. if not mo:
  754. - # unparseable. Maybe git-describe is misbehaving?
  755. + # unparsable. Maybe git-describe is misbehaving?
  756. pieces["error"] = ("unable to parse git-describe output: '%%s'"
  757. %% describe_out)
  758. return pieces
  759. @@ -713,26 +892,27 @@ def git_pieces_from_vcs(tag_prefix, root, verbose, run_command=run_command):
  760. else:
  761. # HEX: no tags
  762. pieces["closest-tag"] = None
  763. - count_out, rc = run_command(GITS, ["rev-list", "HEAD", "--count"],
  764. - cwd=root)
  765. - pieces["distance"] = int(count_out) # total number of commits
  766. + out, rc = runner(GITS, ["rev-list", "HEAD", "--left-right"], cwd=root)
  767. + pieces["distance"] = len(out.split()) # total number of commits
  768. # commit date: see ISO-8601 comment in git_versions_from_keywords()
  769. - date = run_command(GITS, ["show", "-s", "--format=%%ci", "HEAD"],
  770. - cwd=root)[0].strip()
  771. + date = runner(GITS, ["show", "-s", "--format=%%ci", "HEAD"], cwd=root)[0].strip()
  772. + # Use only the last line. Previous lines may contain GPG signature
  773. + # information.
  774. + date = date.splitlines()[-1]
  775. pieces["date"] = date.strip().replace(" ", "T", 1).replace(" ", "", 1)
  776. return pieces
  777. -def plus_or_dot(pieces):
  778. +def plus_or_dot(pieces: Dict[str, Any]) -> str:
  779. """Return a + if we don't already have one, else return a ."""
  780. if "+" in pieces.get("closest-tag", ""):
  781. return "."
  782. return "+"
  783. -def render_pep440(pieces):
  784. +def render_pep440(pieces: Dict[str, Any]) -> str:
  785. """Build up version string, with post-release "local version identifier".
  786. Our goal: TAG[+DISTANCE.gHEX[.dirty]] . Note that if you
  787. @@ -757,23 +937,71 @@ def render_pep440(pieces):
  788. return rendered
  789. -def render_pep440_pre(pieces):
  790. - """TAG[.post.devDISTANCE] -- No -dirty.
  791. +def render_pep440_branch(pieces: Dict[str, Any]) -> str:
  792. + """TAG[[.dev0]+DISTANCE.gHEX[.dirty]] .
  793. +
  794. + The ".dev0" means not master branch. Note that .dev0 sorts backwards
  795. + (a feature branch will appear "older" than the master branch).
  796. Exceptions:
  797. - 1: no tags. 0.post.devDISTANCE
  798. + 1: no tags. 0[.dev0]+untagged.DISTANCE.gHEX[.dirty]
  799. """
  800. if pieces["closest-tag"]:
  801. rendered = pieces["closest-tag"]
  802. + if pieces["distance"] or pieces["dirty"]:
  803. + if pieces["branch"] != "master":
  804. + rendered += ".dev0"
  805. + rendered += plus_or_dot(pieces)
  806. + rendered += "%%d.g%%s" %% (pieces["distance"], pieces["short"])
  807. + if pieces["dirty"]:
  808. + rendered += ".dirty"
  809. + else:
  810. + # exception #1
  811. + rendered = "0"
  812. + if pieces["branch"] != "master":
  813. + rendered += ".dev0"
  814. + rendered += "+untagged.%%d.g%%s" %% (pieces["distance"],
  815. + pieces["short"])
  816. + if pieces["dirty"]:
  817. + rendered += ".dirty"
  818. + return rendered
  819. +
  820. +
  821. +def pep440_split_post(ver: str) -> Tuple[str, Optional[int]]:
  822. + """Split pep440 version string at the post-release segment.
  823. +
  824. + Returns the release segments before the post-release and the
  825. + post-release version number (or -1 if no post-release segment is present).
  826. + """
  827. + vc = str.split(ver, ".post")
  828. + return vc[0], int(vc[1] or 0) if len(vc) == 2 else None
  829. +
  830. +
  831. +def render_pep440_pre(pieces: Dict[str, Any]) -> str:
  832. + """TAG[.postN.devDISTANCE] -- No -dirty.
  833. +
  834. + Exceptions:
  835. + 1: no tags. 0.post0.devDISTANCE
  836. + """
  837. + if pieces["closest-tag"]:
  838. if pieces["distance"]:
  839. - rendered += ".post.dev%%d" %% pieces["distance"]
  840. + # update the post release segment
  841. + tag_version, post_version = pep440_split_post(pieces["closest-tag"])
  842. + rendered = tag_version
  843. + if post_version is not None:
  844. + rendered += ".post%%d.dev%%d" %% (post_version + 1, pieces["distance"])
  845. + else:
  846. + rendered += ".post0.dev%%d" %% (pieces["distance"])
  847. + else:
  848. + # no commits, use the tag as the version
  849. + rendered = pieces["closest-tag"]
  850. else:
  851. # exception #1
  852. - rendered = "0.post.dev%%d" %% pieces["distance"]
  853. + rendered = "0.post0.dev%%d" %% pieces["distance"]
  854. return rendered
  855. -def render_pep440_post(pieces):
  856. +def render_pep440_post(pieces: Dict[str, Any]) -> str:
  857. """TAG[.postDISTANCE[.dev0]+gHEX] .
  858. The ".dev0" means dirty. Note that .dev0 sorts backwards
  859. @@ -800,12 +1028,41 @@ def render_pep440_post(pieces):
  860. return rendered
  861. -def render_pep440_old(pieces):
  862. +def render_pep440_post_branch(pieces: Dict[str, Any]) -> str:
  863. + """TAG[.postDISTANCE[.dev0]+gHEX[.dirty]] .
  864. +
  865. + The ".dev0" means not master branch.
  866. +
  867. + Exceptions:
  868. + 1: no tags. 0.postDISTANCE[.dev0]+gHEX[.dirty]
  869. + """
  870. + if pieces["closest-tag"]:
  871. + rendered = pieces["closest-tag"]
  872. + if pieces["distance"] or pieces["dirty"]:
  873. + rendered += ".post%%d" %% pieces["distance"]
  874. + if pieces["branch"] != "master":
  875. + rendered += ".dev0"
  876. + rendered += plus_or_dot(pieces)
  877. + rendered += "g%%s" %% pieces["short"]
  878. + if pieces["dirty"]:
  879. + rendered += ".dirty"
  880. + else:
  881. + # exception #1
  882. + rendered = "0.post%%d" %% pieces["distance"]
  883. + if pieces["branch"] != "master":
  884. + rendered += ".dev0"
  885. + rendered += "+g%%s" %% pieces["short"]
  886. + if pieces["dirty"]:
  887. + rendered += ".dirty"
  888. + return rendered
  889. +
  890. +
  891. +def render_pep440_old(pieces: Dict[str, Any]) -> str:
  892. """TAG[.postDISTANCE[.dev0]] .
  893. The ".dev0" means dirty.
  894. - Eexceptions:
  895. + Exceptions:
  896. 1: no tags. 0.postDISTANCE[.dev0]
  897. """
  898. if pieces["closest-tag"]:
  899. @@ -822,7 +1079,7 @@ def render_pep440_old(pieces):
  900. return rendered
  901. -def render_git_describe(pieces):
  902. +def render_git_describe(pieces: Dict[str, Any]) -> str:
  903. """TAG[-DISTANCE-gHEX][-dirty].
  904. Like 'git describe --tags --dirty --always'.
  905. @@ -842,7 +1099,7 @@ def render_git_describe(pieces):
  906. return rendered
  907. -def render_git_describe_long(pieces):
  908. +def render_git_describe_long(pieces: Dict[str, Any]) -> str:
  909. """TAG-DISTANCE-gHEX[-dirty].
  910. Like 'git describe --tags --dirty --always -long'.
  911. @@ -862,7 +1119,7 @@ def render_git_describe_long(pieces):
  912. return rendered
  913. -def render(pieces, style):
  914. +def render(pieces: Dict[str, Any], style: str) -> Dict[str, Any]:
  915. """Render the given version pieces into the requested style."""
  916. if pieces["error"]:
  917. return {"version": "unknown",
  918. @@ -876,10 +1133,14 @@ def render(pieces, style):
  919. if style == "pep440":
  920. rendered = render_pep440(pieces)
  921. + elif style == "pep440-branch":
  922. + rendered = render_pep440_branch(pieces)
  923. elif style == "pep440-pre":
  924. rendered = render_pep440_pre(pieces)
  925. elif style == "pep440-post":
  926. rendered = render_pep440_post(pieces)
  927. + elif style == "pep440-post-branch":
  928. + rendered = render_pep440_post_branch(pieces)
  929. elif style == "pep440-old":
  930. rendered = render_pep440_old(pieces)
  931. elif style == "git-describe":
  932. @@ -894,7 +1155,7 @@ def render(pieces, style):
  933. "date": pieces.get("date")}
  934. -def get_versions():
  935. +def get_versions() -> Dict[str, Any]:
  936. """Get version information or return default if unable to do so."""
  937. # I am in _version.py, which lives at ROOT/VERSIONFILE_SOURCE. If we have
  938. # __file__, we can work backwards from there to the root. Some
  939. @@ -915,7 +1176,7 @@ def get_versions():
  940. # versionfile_source is the relative path from the top of the source
  941. # tree (where the .git directory might live) to this file. Invert
  942. # this to find the root from __file__.
  943. - for i in cfg.versionfile_source.split('/'):
  944. + for _ in cfg.versionfile_source.split('/'):
  945. root = os.path.dirname(root)
  946. except NameError:
  947. return {"version": "0+unknown", "full-revisionid": None,
  948. @@ -942,41 +1203,48 @@ def get_versions():
  949. @register_vcs_handler("git", "get_keywords")
  950. -def git_get_keywords(versionfile_abs):
  951. +def git_get_keywords(versionfile_abs: str) -> Dict[str, str]:
  952. """Extract version information from the given file."""
  953. # the code embedded in _version.py can just fetch the value of these
  954. # keywords. When used from setup.py, we don't want to import _version.py,
  955. # so we do it with a regexp instead. This function is not used from
  956. # _version.py.
  957. - keywords = {}
  958. + keywords: Dict[str, str] = {}
  959. try:
  960. - f = open(versionfile_abs, "r")
  961. - for line in f.readlines():
  962. - if line.strip().startswith("git_refnames ="):
  963. - mo = re.search(r'=\s*"(.*)"', line)
  964. - if mo:
  965. - keywords["refnames"] = mo.group(1)
  966. - if line.strip().startswith("git_full ="):
  967. - mo = re.search(r'=\s*"(.*)"', line)
  968. - if mo:
  969. - keywords["full"] = mo.group(1)
  970. - if line.strip().startswith("git_date ="):
  971. - mo = re.search(r'=\s*"(.*)"', line)
  972. - if mo:
  973. - keywords["date"] = mo.group(1)
  974. - f.close()
  975. - except EnvironmentError:
  976. + with open(versionfile_abs, "r") as fobj:
  977. + for line in fobj:
  978. + if line.strip().startswith("git_refnames ="):
  979. + mo = re.search(r'=\s*"(.*)"', line)
  980. + if mo:
  981. + keywords["refnames"] = mo.group(1)
  982. + if line.strip().startswith("git_full ="):
  983. + mo = re.search(r'=\s*"(.*)"', line)
  984. + if mo:
  985. + keywords["full"] = mo.group(1)
  986. + if line.strip().startswith("git_date ="):
  987. + mo = re.search(r'=\s*"(.*)"', line)
  988. + if mo:
  989. + keywords["date"] = mo.group(1)
  990. + except OSError:
  991. pass
  992. return keywords
  993. @register_vcs_handler("git", "keywords")
  994. -def git_versions_from_keywords(keywords, tag_prefix, verbose):
  995. +def git_versions_from_keywords(
  996. + keywords: Dict[str, str],
  997. + tag_prefix: str,
  998. + verbose: bool,
  999. +) -> Dict[str, Any]:
  1000. """Get version information from git keywords."""
  1001. - if not keywords:
  1002. - raise NotThisMethod("no keywords at all, weird")
  1003. + if "refnames" not in keywords:
  1004. + raise NotThisMethod("Short version file found")
  1005. date = keywords.get("date")
  1006. if date is not None:
  1007. + # Use only the last line. Previous lines may contain GPG signature
  1008. + # information.
  1009. + date = date.splitlines()[-1]
  1010. +
  1011. # git-2.2.0 added "%cI", which expands to an ISO-8601 -compliant
  1012. # datestamp. However we prefer "%ci" (which expands to an "ISO-8601
  1013. # -like" string, which we must then edit to make compliant), because
  1014. @@ -989,11 +1257,11 @@ def git_versions_from_keywords(keywords, tag_prefix, verbose):
  1015. if verbose:
  1016. print("keywords are unexpanded, not using")
  1017. raise NotThisMethod("unexpanded keywords, not a git-archive tarball")
  1018. - refs = set([r.strip() for r in refnames.strip("()").split(",")])
  1019. + refs = {r.strip() for r in refnames.strip("()").split(",")}
  1020. # starting in git-1.8.3, tags are listed as "tag: foo-1.0" instead of
  1021. # just "foo-1.0". If we see a "tag: " prefix, prefer those.
  1022. TAG = "tag: "
  1023. - tags = set([r[len(TAG):] for r in refs if r.startswith(TAG)])
  1024. + tags = {r[len(TAG) :] for r in refs if r.startswith(TAG)}
  1025. if not tags:
  1026. # Either we're using git < 1.8.3, or there really are no tags. We use
  1027. # a heuristic: assume all version tags have a digit. The old git %d
  1028. @@ -1002,7 +1270,7 @@ def git_versions_from_keywords(keywords, tag_prefix, verbose):
  1029. # between branches and tags. By ignoring refnames without digits, we
  1030. # filter out many common branch names like "release" and
  1031. # "stabilization", as well as "HEAD" and "master".
  1032. - tags = set([r for r in refs if re.search(r'\d', r)])
  1033. + tags = {r for r in refs if re.search(r"\d", r)}
  1034. if verbose:
  1035. print("discarding '%s', no digits" % ",".join(refs - tags))
  1036. if verbose:
  1037. @@ -1010,23 +1278,37 @@ def git_versions_from_keywords(keywords, tag_prefix, verbose):
  1038. for ref in sorted(tags):
  1039. # sorting will prefer e.g. "2.0" over "2.0rc1"
  1040. if ref.startswith(tag_prefix):
  1041. - r = ref[len(tag_prefix):]
  1042. + r = ref[len(tag_prefix) :]
  1043. + # Filter out refs that exactly match prefix or that don't start
  1044. + # with a number once the prefix is stripped (mostly a concern
  1045. + # when prefix is '')
  1046. + if not re.match(r"\d", r):
  1047. + continue
  1048. if verbose:
  1049. print("picking %s" % r)
  1050. - return {"version": r,
  1051. - "full-revisionid": keywords["full"].strip(),
  1052. - "dirty": False, "error": None,
  1053. - "date": date}
  1054. + return {
  1055. + "version": r,
  1056. + "full-revisionid": keywords["full"].strip(),
  1057. + "dirty": False,
  1058. + "error": None,
  1059. + "date": date,
  1060. + }
  1061. # no suitable tags, so version is "0+unknown", but full hex is still there
  1062. if verbose:
  1063. print("no suitable tags, using unknown + full revision id")
  1064. - return {"version": "0+unknown",
  1065. - "full-revisionid": keywords["full"].strip(),
  1066. - "dirty": False, "error": "no suitable tags", "date": None}
  1067. + return {
  1068. + "version": "0+unknown",
  1069. + "full-revisionid": keywords["full"].strip(),
  1070. + "dirty": False,
  1071. + "error": "no suitable tags",
  1072. + "date": None,
  1073. + }
  1074. @register_vcs_handler("git", "pieces_from_vcs")
  1075. -def git_pieces_from_vcs(tag_prefix, root, verbose, run_command=run_command):
  1076. +def git_pieces_from_vcs(
  1077. + tag_prefix: str, root: str, verbose: bool, runner: Callable = run_command
  1078. +) -> Dict[str, Any]:
  1079. """Get version from 'git describe' in the root of the source tree.
  1080. This only gets called if the git-archive 'subst' keywords were *not*
  1081. @@ -1037,8 +1319,14 @@ def git_pieces_from_vcs(tag_prefix, root, verbose, run_command=run_command):
  1082. if sys.platform == "win32":
  1083. GITS = ["git.cmd", "git.exe"]
  1084. - out, rc = run_command(GITS, ["rev-parse", "--git-dir"], cwd=root,
  1085. - hide_stderr=True)
  1086. + # GIT_DIR can interfere with correct operation of Versioneer.
  1087. + # It may be intended to be passed to the Versioneer-versioned project,
  1088. + # but that should not change where we get our version from.
  1089. + env = os.environ.copy()
  1090. + env.pop("GIT_DIR", None)
  1091. + runner = functools.partial(runner, env=env)
  1092. +
  1093. + _, rc = runner(GITS, ["rev-parse", "--git-dir"], cwd=root, hide_stderr=not verbose)
  1094. if rc != 0:
  1095. if verbose:
  1096. print("Directory %s not under git control" % root)
  1097. @@ -1046,24 +1334,65 @@ def git_pieces_from_vcs(tag_prefix, root, verbose, run_command=run_command):
  1098. # if there is a tag matching tag_prefix, this yields TAG-NUM-gHEX[-dirty]
  1099. # if there isn't one, this yields HEX[-dirty] (no NUM)
  1100. - describe_out, rc = run_command(GITS, ["describe", "--tags", "--dirty",
  1101. - "--always", "--long",
  1102. - "--match", "%s*" % tag_prefix],
  1103. - cwd=root)
  1104. + describe_out, rc = runner(
  1105. + GITS,
  1106. + [
  1107. + "describe",
  1108. + "--tags",
  1109. + "--dirty",
  1110. + "--always",
  1111. + "--long",
  1112. + "--match",
  1113. + f"{tag_prefix}[[:digit:]]*",
  1114. + ],
  1115. + cwd=root,
  1116. + )
  1117. # --long was added in git-1.5.5
  1118. if describe_out is None:
  1119. raise NotThisMethod("'git describe' failed")
  1120. describe_out = describe_out.strip()
  1121. - full_out, rc = run_command(GITS, ["rev-parse", "HEAD"], cwd=root)
  1122. + full_out, rc = runner(GITS, ["rev-parse", "HEAD"], cwd=root)
  1123. if full_out is None:
  1124. raise NotThisMethod("'git rev-parse' failed")
  1125. full_out = full_out.strip()
  1126. - pieces = {}
  1127. + pieces: Dict[str, Any] = {}
  1128. pieces["long"] = full_out
  1129. pieces["short"] = full_out[:7] # maybe improved later
  1130. pieces["error"] = None
  1131. + branch_name, rc = runner(GITS, ["rev-parse", "--abbrev-ref", "HEAD"], cwd=root)
  1132. + # --abbrev-ref was added in git-1.6.3
  1133. + if rc != 0 or branch_name is None:
  1134. + raise NotThisMethod("'git rev-parse --abbrev-ref' returned error")
  1135. + branch_name = branch_name.strip()
  1136. +
  1137. + if branch_name == "HEAD":
  1138. + # If we aren't exactly on a branch, pick a branch which represents
  1139. + # the current commit. If all else fails, we are on a branchless
  1140. + # commit.
  1141. + branches, rc = runner(GITS, ["branch", "--contains"], cwd=root)
  1142. + # --contains was added in git-1.5.4
  1143. + if rc != 0 or branches is None:
  1144. + raise NotThisMethod("'git branch --contains' returned error")
  1145. + branches = branches.split("\n")
  1146. +
  1147. + # Remove the first line if we're running detached
  1148. + if "(" in branches[0]:
  1149. + branches.pop(0)
  1150. +
  1151. + # Strip off the leading "* " from the list of branches.
  1152. + branches = [branch[2:] for branch in branches]
  1153. + if "master" in branches:
  1154. + branch_name = "master"
  1155. + elif not branches:
  1156. + branch_name = None
  1157. + else:
  1158. + # Pick the first branch that is returned. Good or bad.
  1159. + branch_name = branches[0]
  1160. +
  1161. + pieces["branch"] = branch_name
  1162. +
  1163. # parse describe_out. It will be like TAG-NUM-gHEX[-dirty] or HEX[-dirty]
  1164. # TAG might have hyphens.
  1165. git_describe = describe_out
  1166. @@ -1072,17 +1401,16 @@ def git_pieces_from_vcs(tag_prefix, root, verbose, run_command=run_command):
  1167. dirty = git_describe.endswith("-dirty")
  1168. pieces["dirty"] = dirty
  1169. if dirty:
  1170. - git_describe = git_describe[:git_describe.rindex("-dirty")]
  1171. + git_describe = git_describe[: git_describe.rindex("-dirty")]
  1172. # now we have TAG-NUM-gHEX or HEX
  1173. if "-" in git_describe:
  1174. # TAG-NUM-gHEX
  1175. - mo = re.search(r'^(.+)-(\d+)-g([0-9a-f]+)$', git_describe)
  1176. + mo = re.search(r"^(.+)-(\d+)-g([0-9a-f]+)$", git_describe)
  1177. if not mo:
  1178. - # unparseable. Maybe git-describe is misbehaving?
  1179. - pieces["error"] = ("unable to parse git-describe output: '%s'"
  1180. - % describe_out)
  1181. + # unparsable. Maybe git-describe is misbehaving?
  1182. + pieces["error"] = "unable to parse git-describe output: '%s'" % describe_out
  1183. return pieces
  1184. # tag
  1185. @@ -1091,10 +1419,12 @@ def git_pieces_from_vcs(tag_prefix, root, verbose, run_command=run_command):
  1186. if verbose:
  1187. fmt = "tag '%s' doesn't start with prefix '%s'"
  1188. print(fmt % (full_tag, tag_prefix))
  1189. - pieces["error"] = ("tag '%s' doesn't start with prefix '%s'"
  1190. - % (full_tag, tag_prefix))
  1191. + pieces["error"] = "tag '%s' doesn't start with prefix '%s'" % (
  1192. + full_tag,
  1193. + tag_prefix,
  1194. + )
  1195. return pieces
  1196. - pieces["closest-tag"] = full_tag[len(tag_prefix):]
  1197. + pieces["closest-tag"] = full_tag[len(tag_prefix) :]
  1198. # distance: number of commits since tag
  1199. pieces["distance"] = int(mo.group(2))
  1200. @@ -1105,19 +1435,20 @@ def git_pieces_from_vcs(tag_prefix, root, verbose, run_command=run_command):
  1201. else:
  1202. # HEX: no tags
  1203. pieces["closest-tag"] = None
  1204. - count_out, rc = run_command(GITS, ["rev-list", "HEAD", "--count"],
  1205. - cwd=root)
  1206. - pieces["distance"] = int(count_out) # total number of commits
  1207. + out, rc = runner(GITS, ["rev-list", "HEAD", "--left-right"], cwd=root)
  1208. + pieces["distance"] = len(out.split()) # total number of commits
  1209. # commit date: see ISO-8601 comment in git_versions_from_keywords()
  1210. - date = run_command(GITS, ["show", "-s", "--format=%ci", "HEAD"],
  1211. - cwd=root)[0].strip()
  1212. + date = runner(GITS, ["show", "-s", "--format=%ci", "HEAD"], cwd=root)[0].strip()
  1213. + # Use only the last line. Previous lines may contain GPG signature
  1214. + # information.
  1215. + date = date.splitlines()[-1]
  1216. pieces["date"] = date.strip().replace(" ", "T", 1).replace(" ", "", 1)
  1217. return pieces
  1218. -def do_vcs_install(manifest_in, versionfile_source, ipy):
  1219. +def do_vcs_install(versionfile_source: str, ipy: Optional[str]) -> None:
  1220. """Git-specific installation logic for Versioneer.
  1221. For Git, this means creating/changing .gitattributes to mark _version.py
  1222. @@ -1126,36 +1457,40 @@ def do_vcs_install(manifest_in, versionfile_source, ipy):
  1223. GITS = ["git"]
  1224. if sys.platform == "win32":
  1225. GITS = ["git.cmd", "git.exe"]
  1226. - files = [manifest_in, versionfile_source]
  1227. + files = [versionfile_source]
  1228. if ipy:
  1229. files.append(ipy)
  1230. - try:
  1231. - me = __file__
  1232. - if me.endswith(".pyc") or me.endswith(".pyo"):
  1233. - me = os.path.splitext(me)[0] + ".py"
  1234. - versioneer_file = os.path.relpath(me)
  1235. - except NameError:
  1236. - versioneer_file = "versioneer.py"
  1237. - files.append(versioneer_file)
  1238. + if "VERSIONEER_PEP518" not in globals():
  1239. + try:
  1240. + my_path = __file__
  1241. + if my_path.endswith((".pyc", ".pyo")):
  1242. + my_path = os.path.splitext(my_path)[0] + ".py"
  1243. + versioneer_file = os.path.relpath(my_path)
  1244. + except NameError:
  1245. + versioneer_file = "versioneer.py"
  1246. + files.append(versioneer_file)
  1247. present = False
  1248. try:
  1249. - f = open(".gitattributes", "r")
  1250. - for line in f.readlines():
  1251. - if line.strip().startswith(versionfile_source):
  1252. - if "export-subst" in line.strip().split()[1:]:
  1253. - present = True
  1254. - f.close()
  1255. - except EnvironmentError:
  1256. + with open(".gitattributes", "r") as fobj:
  1257. + for line in fobj:
  1258. + if line.strip().startswith(versionfile_source):
  1259. + if "export-subst" in line.strip().split()[1:]:
  1260. + present = True
  1261. + break
  1262. + except OSError:
  1263. pass
  1264. if not present:
  1265. - f = open(".gitattributes", "a+")
  1266. - f.write("%s export-subst\n" % versionfile_source)
  1267. - f.close()
  1268. + with open(".gitattributes", "a+") as fobj:
  1269. + fobj.write(f"{versionfile_source} export-subst\n")
  1270. files.append(".gitattributes")
  1271. run_command(GITS, ["add", "--"] + files)
  1272. -def versions_from_parentdir(parentdir_prefix, root, verbose):
  1273. +def versions_from_parentdir(
  1274. + parentdir_prefix: str,
  1275. + root: str,
  1276. + verbose: bool,
  1277. +) -> Dict[str, Any]:
  1278. """Try to determine the version from the parent directory name.
  1279. Source tarballs conventionally unpack into a directory that includes both
  1280. @@ -1164,24 +1499,29 @@ def versions_from_parentdir(parentdir_prefix, root, verbose):
  1281. """
  1282. rootdirs = []
  1283. - for i in range(3):
  1284. + for _ in range(3):
  1285. dirname = os.path.basename(root)
  1286. if dirname.startswith(parentdir_prefix):
  1287. - return {"version": dirname[len(parentdir_prefix):],
  1288. - "full-revisionid": None,
  1289. - "dirty": False, "error": None, "date": None}
  1290. - else:
  1291. - rootdirs.append(root)
  1292. - root = os.path.dirname(root) # up a level
  1293. + return {
  1294. + "version": dirname[len(parentdir_prefix) :],
  1295. + "full-revisionid": None,
  1296. + "dirty": False,
  1297. + "error": None,
  1298. + "date": None,
  1299. + }
  1300. + rootdirs.append(root)
  1301. + root = os.path.dirname(root) # up a level
  1302. if verbose:
  1303. - print("Tried directories %s but none started with prefix %s" %
  1304. - (str(rootdirs), parentdir_prefix))
  1305. + print(
  1306. + "Tried directories %s but none started with prefix %s"
  1307. + % (str(rootdirs), parentdir_prefix)
  1308. + )
  1309. raise NotThisMethod("rootdir doesn't start with parentdir_prefix")
  1310. SHORT_VERSION_PY = """
  1311. -# This file was generated by 'versioneer.py' (0.18) from
  1312. +# This file was generated by 'versioneer.py' (0.29) from
  1313. # revision-control system data, or from the parent directory name of an
  1314. # unpacked source archive. Distribution tarballs contain a pre-generated copy
  1315. # of this file.
  1316. @@ -1198,42 +1538,42 @@ def get_versions():
  1317. """
  1318. -def versions_from_file(filename):
  1319. +def versions_from_file(filename: str) -> Dict[str, Any]:
  1320. """Try to determine the version from _version.py if present."""
  1321. try:
  1322. with open(filename) as f:
  1323. contents = f.read()
  1324. - except EnvironmentError:
  1325. + except OSError:
  1326. raise NotThisMethod("unable to read _version.py")
  1327. - mo = re.search(r"version_json = '''\n(.*)''' # END VERSION_JSON",
  1328. - contents, re.M | re.S)
  1329. + mo = re.search(
  1330. + r"version_json = '''\n(.*)''' # END VERSION_JSON", contents, re.M | re.S
  1331. + )
  1332. if not mo:
  1333. - mo = re.search(r"version_json = '''\r\n(.*)''' # END VERSION_JSON",
  1334. - contents, re.M | re.S)
  1335. + mo = re.search(
  1336. + r"version_json = '''\r\n(.*)''' # END VERSION_JSON", contents, re.M | re.S
  1337. + )
  1338. if not mo:
  1339. raise NotThisMethod("no version_json in _version.py")
  1340. return json.loads(mo.group(1))
  1341. -def write_to_version_file(filename, versions):
  1342. +def write_to_version_file(filename: str, versions: Dict[str, Any]) -> None:
  1343. """Write the given version number to the given _version.py file."""
  1344. - os.unlink(filename)
  1345. - contents = json.dumps(versions, sort_keys=True,
  1346. - indent=1, separators=(",", ": "))
  1347. + contents = json.dumps(versions, sort_keys=True, indent=1, separators=(",", ": "))
  1348. with open(filename, "w") as f:
  1349. f.write(SHORT_VERSION_PY % contents)
  1350. print("set %s to '%s'" % (filename, versions["version"]))
  1351. -def plus_or_dot(pieces):
  1352. +def plus_or_dot(pieces: Dict[str, Any]) -> str:
  1353. """Return a + if we don't already have one, else return a ."""
  1354. if "+" in pieces.get("closest-tag", ""):
  1355. return "."
  1356. return "+"
  1357. -def render_pep440(pieces):
  1358. +def render_pep440(pieces: Dict[str, Any]) -> str:
  1359. """Build up version string, with post-release "local version identifier".
  1360. Our goal: TAG[+DISTANCE.gHEX[.dirty]] . Note that if you
  1361. @@ -1251,30 +1591,76 @@ def render_pep440(pieces):
  1362. rendered += ".dirty"
  1363. else:
  1364. # exception #1
  1365. - rendered = "0+untagged.%d.g%s" % (pieces["distance"],
  1366. - pieces["short"])
  1367. + rendered = "0+untagged.%d.g%s" % (pieces["distance"], pieces["short"])
  1368. if pieces["dirty"]:
  1369. rendered += ".dirty"
  1370. return rendered
  1371. -def render_pep440_pre(pieces):
  1372. - """TAG[.post.devDISTANCE] -- No -dirty.
  1373. +def render_pep440_branch(pieces: Dict[str, Any]) -> str:
  1374. + """TAG[[.dev0]+DISTANCE.gHEX[.dirty]] .
  1375. +
  1376. + The ".dev0" means not master branch. Note that .dev0 sorts backwards
  1377. + (a feature branch will appear "older" than the master branch).
  1378. Exceptions:
  1379. - 1: no tags. 0.post.devDISTANCE
  1380. + 1: no tags. 0[.dev0]+untagged.DISTANCE.gHEX[.dirty]
  1381. """
  1382. if pieces["closest-tag"]:
  1383. rendered = pieces["closest-tag"]
  1384. + if pieces["distance"] or pieces["dirty"]:
  1385. + if pieces["branch"] != "master":
  1386. + rendered += ".dev0"
  1387. + rendered += plus_or_dot(pieces)
  1388. + rendered += "%d.g%s" % (pieces["distance"], pieces["short"])
  1389. + if pieces["dirty"]:
  1390. + rendered += ".dirty"
  1391. + else:
  1392. + # exception #1
  1393. + rendered = "0"
  1394. + if pieces["branch"] != "master":
  1395. + rendered += ".dev0"
  1396. + rendered += "+untagged.%d.g%s" % (pieces["distance"], pieces["short"])
  1397. + if pieces["dirty"]:
  1398. + rendered += ".dirty"
  1399. + return rendered
  1400. +
  1401. +
  1402. +def pep440_split_post(ver: str) -> Tuple[str, Optional[int]]:
  1403. + """Split pep440 version string at the post-release segment.
  1404. +
  1405. + Returns the release segments before the post-release and the
  1406. + post-release version number (or -1 if no post-release segment is present).
  1407. + """
  1408. + vc = str.split(ver, ".post")
  1409. + return vc[0], int(vc[1] or 0) if len(vc) == 2 else None
  1410. +
  1411. +
  1412. +def render_pep440_pre(pieces: Dict[str, Any]) -> str:
  1413. + """TAG[.postN.devDISTANCE] -- No -dirty.
  1414. +
  1415. + Exceptions:
  1416. + 1: no tags. 0.post0.devDISTANCE
  1417. + """
  1418. + if pieces["closest-tag"]:
  1419. if pieces["distance"]:
  1420. - rendered += ".post.dev%d" % pieces["distance"]
  1421. + # update the post release segment
  1422. + tag_version, post_version = pep440_split_post(pieces["closest-tag"])
  1423. + rendered = tag_version
  1424. + if post_version is not None:
  1425. + rendered += ".post%d.dev%d" % (post_version + 1, pieces["distance"])
  1426. + else:
  1427. + rendered += ".post0.dev%d" % (pieces["distance"])
  1428. + else:
  1429. + # no commits, use the tag as the version
  1430. + rendered = pieces["closest-tag"]
  1431. else:
  1432. # exception #1
  1433. - rendered = "0.post.dev%d" % pieces["distance"]
  1434. + rendered = "0.post0.dev%d" % pieces["distance"]
  1435. return rendered
  1436. -def render_pep440_post(pieces):
  1437. +def render_pep440_post(pieces: Dict[str, Any]) -> str:
  1438. """TAG[.postDISTANCE[.dev0]+gHEX] .
  1439. The ".dev0" means dirty. Note that .dev0 sorts backwards
  1440. @@ -1301,12 +1687,41 @@ def render_pep440_post(pieces):
  1441. return rendered
  1442. -def render_pep440_old(pieces):
  1443. +def render_pep440_post_branch(pieces: Dict[str, Any]) -> str:
  1444. + """TAG[.postDISTANCE[.dev0]+gHEX[.dirty]] .
  1445. +
  1446. + The ".dev0" means not master branch.
  1447. +
  1448. + Exceptions:
  1449. + 1: no tags. 0.postDISTANCE[.dev0]+gHEX[.dirty]
  1450. + """
  1451. + if pieces["closest-tag"]:
  1452. + rendered = pieces["closest-tag"]
  1453. + if pieces["distance"] or pieces["dirty"]:
  1454. + rendered += ".post%d" % pieces["distance"]
  1455. + if pieces["branch"] != "master":
  1456. + rendered += ".dev0"
  1457. + rendered += plus_or_dot(pieces)
  1458. + rendered += "g%s" % pieces["short"]
  1459. + if pieces["dirty"]:
  1460. + rendered += ".dirty"
  1461. + else:
  1462. + # exception #1
  1463. + rendered = "0.post%d" % pieces["distance"]
  1464. + if pieces["branch"] != "master":
  1465. + rendered += ".dev0"
  1466. + rendered += "+g%s" % pieces["short"]
  1467. + if pieces["dirty"]:
  1468. + rendered += ".dirty"
  1469. + return rendered
  1470. +
  1471. +
  1472. +def render_pep440_old(pieces: Dict[str, Any]) -> str:
  1473. """TAG[.postDISTANCE[.dev0]] .
  1474. The ".dev0" means dirty.
  1475. - Eexceptions:
  1476. + Exceptions:
  1477. 1: no tags. 0.postDISTANCE[.dev0]
  1478. """
  1479. if pieces["closest-tag"]:
  1480. @@ -1323,7 +1738,7 @@ def render_pep440_old(pieces):
  1481. return rendered
  1482. -def render_git_describe(pieces):
  1483. +def render_git_describe(pieces: Dict[str, Any]) -> str:
  1484. """TAG[-DISTANCE-gHEX][-dirty].
  1485. Like 'git describe --tags --dirty --always'.
  1486. @@ -1343,7 +1758,7 @@ def render_git_describe(pieces):
  1487. return rendered
  1488. -def render_git_describe_long(pieces):
  1489. +def render_git_describe_long(pieces: Dict[str, Any]) -> str:
  1490. """TAG-DISTANCE-gHEX[-dirty].
  1491. Like 'git describe --tags --dirty --always -long'.
  1492. @@ -1363,24 +1778,30 @@ def render_git_describe_long(pieces):
  1493. return rendered
  1494. -def render(pieces, style):
  1495. +def render(pieces: Dict[str, Any], style: str) -> Dict[str, Any]:
  1496. """Render the given version pieces into the requested style."""
  1497. if pieces["error"]:
  1498. - return {"version": "unknown",
  1499. - "full-revisionid": pieces.get("long"),
  1500. - "dirty": None,
  1501. - "error": pieces["error"],
  1502. - "date": None}
  1503. + return {
  1504. + "version": "unknown",
  1505. + "full-revisionid": pieces.get("long"),
  1506. + "dirty": None,
  1507. + "error": pieces["error"],
  1508. + "date": None,
  1509. + }
  1510. if not style or style == "default":
  1511. style = "pep440" # the default
  1512. if style == "pep440":
  1513. rendered = render_pep440(pieces)
  1514. + elif style == "pep440-branch":
  1515. + rendered = render_pep440_branch(pieces)
  1516. elif style == "pep440-pre":
  1517. rendered = render_pep440_pre(pieces)
  1518. elif style == "pep440-post":
  1519. rendered = render_pep440_post(pieces)
  1520. + elif style == "pep440-post-branch":
  1521. + rendered = render_pep440_post_branch(pieces)
  1522. elif style == "pep440-old":
  1523. rendered = render_pep440_old(pieces)
  1524. elif style == "git-describe":
  1525. @@ -1390,16 +1811,20 @@ def render(pieces, style):
  1526. else:
  1527. raise ValueError("unknown style '%s'" % style)
  1528. - return {"version": rendered, "full-revisionid": pieces["long"],
  1529. - "dirty": pieces["dirty"], "error": None,
  1530. - "date": pieces.get("date")}
  1531. + return {
  1532. + "version": rendered,
  1533. + "full-revisionid": pieces["long"],
  1534. + "dirty": pieces["dirty"],
  1535. + "error": None,
  1536. + "date": pieces.get("date"),
  1537. + }
  1538. class VersioneerBadRootError(Exception):
  1539. """The project root directory is unknown or missing key files."""
  1540. -def get_versions(verbose=False):
  1541. +def get_versions(verbose: bool = False) -> Dict[str, Any]:
  1542. """Get the project version from whatever source is available.
  1543. Returns dict with two keys: 'version' and 'full'.
  1544. @@ -1414,9 +1839,10 @@ def get_versions(verbose=False):
  1545. assert cfg.VCS is not None, "please set [versioneer]VCS= in setup.cfg"
  1546. handlers = HANDLERS.get(cfg.VCS)
  1547. assert handlers, "unrecognized VCS '%s'" % cfg.VCS
  1548. - verbose = verbose or cfg.verbose
  1549. - assert cfg.versionfile_source is not None, \
  1550. - "please set versioneer.versionfile_source"
  1551. + verbose = verbose or bool(cfg.verbose) # `bool()` used to avoid `None`
  1552. + assert (
  1553. + cfg.versionfile_source is not None
  1554. + ), "please set versioneer.versionfile_source"
  1555. assert cfg.tag_prefix is not None, "please set versioneer.tag_prefix"
  1556. versionfile_abs = os.path.join(root, cfg.versionfile_source)
  1557. @@ -1470,18 +1896,26 @@ def get_versions(verbose=False):
  1558. if verbose:
  1559. print("unable to compute version")
  1560. - return {"version": "0+unknown", "full-revisionid": None,
  1561. - "dirty": None, "error": "unable to compute version",
  1562. - "date": None}
  1563. + return {
  1564. + "version": "0+unknown",
  1565. + "full-revisionid": None,
  1566. + "dirty": None,
  1567. + "error": "unable to compute version",
  1568. + "date": None,
  1569. + }
  1570. -def get_version():
  1571. +def get_version() -> str:
  1572. """Get the short version string for this project."""
  1573. return get_versions()["version"]
  1574. -def get_cmdclass():
  1575. - """Get the custom setuptools/distutils subclasses used by Versioneer."""
  1576. +def get_cmdclass(cmdclass: Optional[Dict[str, Any]] = None):
  1577. + """Get the custom setuptools subclasses used by Versioneer.
  1578. +
  1579. + If the package uses a different cmdclass (e.g. one from numpy), it
  1580. + should be provide as an argument.
  1581. + """
  1582. if "versioneer" in sys.modules:
  1583. del sys.modules["versioneer"]
  1584. # this fixes the "python setup.py develop" case (also 'install' and
  1585. @@ -1495,25 +1929,25 @@ def get_cmdclass():
  1586. # parent is protected against the child's "import versioneer". By
  1587. # removing ourselves from sys.modules here, before the child build
  1588. # happens, we protect the child from the parent's versioneer too.
  1589. - # Also see https://github.com/warner/python-versioneer/issues/52
  1590. + # Also see https://github.com/python-versioneer/python-versioneer/issues/52
  1591. - cmds = {}
  1592. + cmds = {} if cmdclass is None else cmdclass.copy()
  1593. - # we add "version" to both distutils and setuptools
  1594. - from distutils.core import Command
  1595. + # we add "version" to setuptools
  1596. + from setuptools import Command
  1597. class cmd_version(Command):
  1598. description = "report generated version string"
  1599. - user_options = []
  1600. - boolean_options = []
  1601. + user_options: List[Tuple[str, str, str]] = []
  1602. + boolean_options: List[str] = []
  1603. - def initialize_options(self):
  1604. + def initialize_options(self) -> None:
  1605. pass
  1606. - def finalize_options(self):
  1607. + def finalize_options(self) -> None:
  1608. pass
  1609. - def run(self):
  1610. + def run(self) -> None:
  1611. vers = get_versions(verbose=True)
  1612. print("Version: %s" % vers["version"])
  1613. print(" full-revisionid: %s" % vers.get("full-revisionid"))
  1614. @@ -1521,9 +1955,10 @@ def get_cmdclass():
  1615. print(" date: %s" % vers.get("date"))
  1616. if vers["error"]:
  1617. print(" error: %s" % vers["error"])
  1618. +
  1619. cmds["version"] = cmd_version
  1620. - # we override "build_py" in both distutils and setuptools
  1621. + # we override "build_py" in setuptools
  1622. #
  1623. # most invocation pathways end up running build_py:
  1624. # distutils/build -> build_py
  1625. @@ -1538,29 +1973,71 @@ def get_cmdclass():
  1626. # then does setup.py bdist_wheel, or sometimes setup.py install
  1627. # setup.py egg_info -> ?
  1628. + # pip install -e . and setuptool/editable_wheel will invoke build_py
  1629. + # but the build_py command is not expected to copy any files.
  1630. +
  1631. # we override different "build_py" commands for both environments
  1632. - if "setuptools" in sys.modules:
  1633. - from setuptools.command.build_py import build_py as _build_py
  1634. + if "build_py" in cmds:
  1635. + _build_py: Any = cmds["build_py"]
  1636. else:
  1637. - from distutils.command.build_py import build_py as _build_py
  1638. + from setuptools.command.build_py import build_py as _build_py
  1639. class cmd_build_py(_build_py):
  1640. - def run(self):
  1641. + def run(self) -> None:
  1642. root = get_root()
  1643. cfg = get_config_from_root(root)
  1644. versions = get_versions()
  1645. _build_py.run(self)
  1646. + if getattr(self, "editable_mode", False):
  1647. + # During editable installs `.py` and data files are
  1648. + # not copied to build_lib
  1649. + return
  1650. # now locate _version.py in the new build/ directory and replace
  1651. # it with an updated value
  1652. if cfg.versionfile_build:
  1653. - target_versionfile = os.path.join(self.build_lib,
  1654. - cfg.versionfile_build)
  1655. + target_versionfile = os.path.join(self.build_lib, cfg.versionfile_build)
  1656. print("UPDATING %s" % target_versionfile)
  1657. write_to_version_file(target_versionfile, versions)
  1658. +
  1659. cmds["build_py"] = cmd_build_py
  1660. + if "build_ext" in cmds:
  1661. + _build_ext: Any = cmds["build_ext"]
  1662. + else:
  1663. + from setuptools.command.build_ext import build_ext as _build_ext
  1664. +
  1665. + class cmd_build_ext(_build_ext):
  1666. + def run(self) -> None:
  1667. + root = get_root()
  1668. + cfg = get_config_from_root(root)
  1669. + versions = get_versions()
  1670. + _build_ext.run(self)
  1671. + if self.inplace:
  1672. + # build_ext --inplace will only build extensions in
  1673. + # build/lib<..> dir with no _version.py to write to.
  1674. + # As in place builds will already have a _version.py
  1675. + # in the module dir, we do not need to write one.
  1676. + return
  1677. + # now locate _version.py in the new build/ directory and replace
  1678. + # it with an updated value
  1679. + if not cfg.versionfile_build:
  1680. + return
  1681. + target_versionfile = os.path.join(self.build_lib, cfg.versionfile_build)
  1682. + if not os.path.exists(target_versionfile):
  1683. + print(
  1684. + f"Warning: {target_versionfile} does not exist, skipping "
  1685. + "version update. This can happen if you are running build_ext "
  1686. + "without first running build_py."
  1687. + )
  1688. + return
  1689. + print("UPDATING %s" % target_versionfile)
  1690. + write_to_version_file(target_versionfile, versions)
  1691. +
  1692. + cmds["build_ext"] = cmd_build_ext
  1693. +
  1694. if "cx_Freeze" in sys.modules: # cx_freeze enabled?
  1695. - from cx_Freeze.dist import build_exe as _build_exe
  1696. + from cx_Freeze.dist import build_exe as _build_exe # type: ignore
  1697. +
  1698. # nczeczulin reports that py2exe won't like the pep440-style string
  1699. # as FILEVERSION, but it can be used for PRODUCTVERSION, e.g.
  1700. # setup(console=[{
  1701. @@ -1569,7 +2046,7 @@ def get_cmdclass():
  1702. # ...
  1703. class cmd_build_exe(_build_exe):
  1704. - def run(self):
  1705. + def run(self) -> None:
  1706. root = get_root()
  1707. cfg = get_config_from_root(root)
  1708. versions = get_versions()
  1709. @@ -1581,24 +2058,28 @@ def get_cmdclass():
  1710. os.unlink(target_versionfile)
  1711. with open(cfg.versionfile_source, "w") as f:
  1712. LONG = LONG_VERSION_PY[cfg.VCS]
  1713. - f.write(LONG %
  1714. - {"DOLLAR": "$",
  1715. - "STYLE": cfg.style,
  1716. - "TAG_PREFIX": cfg.tag_prefix,
  1717. - "PARENTDIR_PREFIX": cfg.parentdir_prefix,
  1718. - "VERSIONFILE_SOURCE": cfg.versionfile_source,
  1719. - })
  1720. + f.write(
  1721. + LONG
  1722. + % {
  1723. + "DOLLAR": "$",
  1724. + "STYLE": cfg.style,
  1725. + "TAG_PREFIX": cfg.tag_prefix,
  1726. + "PARENTDIR_PREFIX": cfg.parentdir_prefix,
  1727. + "VERSIONFILE_SOURCE": cfg.versionfile_source,
  1728. + }
  1729. + )
  1730. +
  1731. cmds["build_exe"] = cmd_build_exe
  1732. del cmds["build_py"]
  1733. - if 'py2exe' in sys.modules: # py2exe enabled?
  1734. + if "py2exe" in sys.modules: # py2exe enabled?
  1735. try:
  1736. - from py2exe.distutils_buildexe import py2exe as _py2exe # py3
  1737. + from py2exe.setuptools_buildexe import py2exe as _py2exe # type: ignore
  1738. except ImportError:
  1739. - from py2exe.build_exe import py2exe as _py2exe # py2
  1740. + from py2exe.distutils_buildexe import py2exe as _py2exe # type: ignore
  1741. class cmd_py2exe(_py2exe):
  1742. - def run(self):
  1743. + def run(self) -> None:
  1744. root = get_root()
  1745. cfg = get_config_from_root(root)
  1746. versions = get_versions()
  1747. @@ -1610,23 +2091,67 @@ def get_cmdclass():
  1748. os.unlink(target_versionfile)
  1749. with open(cfg.versionfile_source, "w") as f:
  1750. LONG = LONG_VERSION_PY[cfg.VCS]
  1751. - f.write(LONG %
  1752. - {"DOLLAR": "$",
  1753. - "STYLE": cfg.style,
  1754. - "TAG_PREFIX": cfg.tag_prefix,
  1755. - "PARENTDIR_PREFIX": cfg.parentdir_prefix,
  1756. - "VERSIONFILE_SOURCE": cfg.versionfile_source,
  1757. - })
  1758. + f.write(
  1759. + LONG
  1760. + % {
  1761. + "DOLLAR": "$",
  1762. + "STYLE": cfg.style,
  1763. + "TAG_PREFIX": cfg.tag_prefix,
  1764. + "PARENTDIR_PREFIX": cfg.parentdir_prefix,
  1765. + "VERSIONFILE_SOURCE": cfg.versionfile_source,
  1766. + }
  1767. + )
  1768. +
  1769. cmds["py2exe"] = cmd_py2exe
  1770. + # sdist farms its file list building out to egg_info
  1771. + if "egg_info" in cmds:
  1772. + _egg_info: Any = cmds["egg_info"]
  1773. + else:
  1774. + from setuptools.command.egg_info import egg_info as _egg_info
  1775. +
  1776. + class cmd_egg_info(_egg_info):
  1777. + def find_sources(self) -> None:
  1778. + # egg_info.find_sources builds the manifest list and writes it
  1779. + # in one shot
  1780. + super().find_sources()
  1781. +
  1782. + # Modify the filelist and normalize it
  1783. + root = get_root()
  1784. + cfg = get_config_from_root(root)
  1785. + self.filelist.append("versioneer.py")
  1786. + if cfg.versionfile_source:
  1787. + # There are rare cases where versionfile_source might not be
  1788. + # included by default, so we must be explicit
  1789. + self.filelist.append(cfg.versionfile_source)
  1790. + self.filelist.sort()
  1791. + self.filelist.remove_duplicates()
  1792. +
  1793. + # The write method is hidden in the manifest_maker instance that
  1794. + # generated the filelist and was thrown away
  1795. + # We will instead replicate their final normalization (to unicode,
  1796. + # and POSIX-style paths)
  1797. + from setuptools import unicode_utils
  1798. +
  1799. + normalized = [
  1800. + unicode_utils.filesys_decode(f).replace(os.sep, "/")
  1801. + for f in self.filelist.files
  1802. + ]
  1803. +
  1804. + manifest_filename = os.path.join(self.egg_info, "SOURCES.txt")
  1805. + with open(manifest_filename, "w") as fobj:
  1806. + fobj.write("\n".join(normalized))
  1807. +
  1808. + cmds["egg_info"] = cmd_egg_info
  1809. +
  1810. # we override different "sdist" commands for both environments
  1811. - if "setuptools" in sys.modules:
  1812. - from setuptools.command.sdist import sdist as _sdist
  1813. + if "sdist" in cmds:
  1814. + _sdist: Any = cmds["sdist"]
  1815. else:
  1816. - from distutils.command.sdist import sdist as _sdist
  1817. + from setuptools.command.sdist import sdist as _sdist
  1818. class cmd_sdist(_sdist):
  1819. - def run(self):
  1820. + def run(self) -> None:
  1821. versions = get_versions()
  1822. self._versioneer_generated_versions = versions
  1823. # unless we update this, the command will keep using the old
  1824. @@ -1634,7 +2159,7 @@ def get_cmdclass():
  1825. self.distribution.metadata.version = versions["version"]
  1826. return _sdist.run(self)
  1827. - def make_release_tree(self, base_dir, files):
  1828. + def make_release_tree(self, base_dir: str, files: List[str]) -> None:
  1829. root = get_root()
  1830. cfg = get_config_from_root(root)
  1831. _sdist.make_release_tree(self, base_dir, files)
  1832. @@ -1643,8 +2168,10 @@ def get_cmdclass():
  1833. # updated value
  1834. target_versionfile = os.path.join(base_dir, cfg.versionfile_source)
  1835. print("UPDATING %s" % target_versionfile)
  1836. - write_to_version_file(target_versionfile,
  1837. - self._versioneer_generated_versions)
  1838. + write_to_version_file(
  1839. + target_versionfile, self._versioneer_generated_versions
  1840. + )
  1841. +
  1842. cmds["sdist"] = cmd_sdist
  1843. return cmds
  1844. @@ -1687,23 +2214,26 @@ SAMPLE_CONFIG = """
  1845. """
  1846. -INIT_PY_SNIPPET = """
  1847. +OLD_SNIPPET = """
  1848. from ._version import get_versions
  1849. __version__ = get_versions()['version']
  1850. del get_versions
  1851. """
  1852. +INIT_PY_SNIPPET = """
  1853. +from . import {0}
  1854. +__version__ = {0}.get_versions()['version']
  1855. +"""
  1856. +
  1857. -def do_setup():
  1858. - """Main VCS-independent setup function for installing Versioneer."""
  1859. +def do_setup() -> int:
  1860. + """Do main VCS-independent setup function for installing Versioneer."""
  1861. root = get_root()
  1862. try:
  1863. cfg = get_config_from_root(root)
  1864. - except (EnvironmentError, configparser.NoSectionError,
  1865. - configparser.NoOptionError) as e:
  1866. - if isinstance(e, (EnvironmentError, configparser.NoSectionError)):
  1867. - print("Adding sample versioneer config to setup.cfg",
  1868. - file=sys.stderr)
  1869. + except (OSError, configparser.NoSectionError, configparser.NoOptionError) as e:
  1870. + if isinstance(e, (OSError, configparser.NoSectionError)):
  1871. + print("Adding sample versioneer config to setup.cfg", file=sys.stderr)
  1872. with open(os.path.join(root, "setup.cfg"), "a") as f:
  1873. f.write(SAMPLE_CONFIG)
  1874. print(CONFIG_ERROR, file=sys.stderr)
  1875. @@ -1712,71 +2242,49 @@ def do_setup():
  1876. print(" creating %s" % cfg.versionfile_source)
  1877. with open(cfg.versionfile_source, "w") as f:
  1878. LONG = LONG_VERSION_PY[cfg.VCS]
  1879. - f.write(LONG % {"DOLLAR": "$",
  1880. - "STYLE": cfg.style,
  1881. - "TAG_PREFIX": cfg.tag_prefix,
  1882. - "PARENTDIR_PREFIX": cfg.parentdir_prefix,
  1883. - "VERSIONFILE_SOURCE": cfg.versionfile_source,
  1884. - })
  1885. -
  1886. - ipy = os.path.join(os.path.dirname(cfg.versionfile_source),
  1887. - "__init__.py")
  1888. + f.write(
  1889. + LONG
  1890. + % {
  1891. + "DOLLAR": "$",
  1892. + "STYLE": cfg.style,
  1893. + "TAG_PREFIX": cfg.tag_prefix,
  1894. + "PARENTDIR_PREFIX": cfg.parentdir_prefix,
  1895. + "VERSIONFILE_SOURCE": cfg.versionfile_source,
  1896. + }
  1897. + )
  1898. +
  1899. + ipy = os.path.join(os.path.dirname(cfg.versionfile_source), "__init__.py")
  1900. + maybe_ipy: Optional[str] = ipy
  1901. if os.path.exists(ipy):
  1902. try:
  1903. with open(ipy, "r") as f:
  1904. old = f.read()
  1905. - except EnvironmentError:
  1906. + except OSError:
  1907. old = ""
  1908. - if INIT_PY_SNIPPET not in old:
  1909. + module = os.path.splitext(os.path.basename(cfg.versionfile_source))[0]
  1910. + snippet = INIT_PY_SNIPPET.format(module)
  1911. + if OLD_SNIPPET in old:
  1912. + print(" replacing boilerplate in %s" % ipy)
  1913. + with open(ipy, "w") as f:
  1914. + f.write(old.replace(OLD_SNIPPET, snippet))
  1915. + elif snippet not in old:
  1916. print(" appending to %s" % ipy)
  1917. with open(ipy, "a") as f:
  1918. - f.write(INIT_PY_SNIPPET)
  1919. + f.write(snippet)
  1920. else:
  1921. print(" %s unmodified" % ipy)
  1922. else:
  1923. print(" %s doesn't exist, ok" % ipy)
  1924. - ipy = None
  1925. -
  1926. - # Make sure both the top-level "versioneer.py" and versionfile_source
  1927. - # (PKG/_version.py, used by runtime code) are in MANIFEST.in, so
  1928. - # they'll be copied into source distributions. Pip won't be able to
  1929. - # install the package without this.
  1930. - manifest_in = os.path.join(root, "MANIFEST.in")
  1931. - simple_includes = set()
  1932. - try:
  1933. - with open(manifest_in, "r") as f:
  1934. - for line in f:
  1935. - if line.startswith("include "):
  1936. - for include in line.split()[1:]:
  1937. - simple_includes.add(include)
  1938. - except EnvironmentError:
  1939. - pass
  1940. - # That doesn't cover everything MANIFEST.in can do
  1941. - # (http://docs.python.org/2/distutils/sourcedist.html#commands), so
  1942. - # it might give some false negatives. Appending redundant 'include'
  1943. - # lines is safe, though.
  1944. - if "versioneer.py" not in simple_includes:
  1945. - print(" appending 'versioneer.py' to MANIFEST.in")
  1946. - with open(manifest_in, "a") as f:
  1947. - f.write("include versioneer.py\n")
  1948. - else:
  1949. - print(" 'versioneer.py' already in MANIFEST.in")
  1950. - if cfg.versionfile_source not in simple_includes:
  1951. - print(" appending versionfile_source ('%s') to MANIFEST.in" %
  1952. - cfg.versionfile_source)
  1953. - with open(manifest_in, "a") as f:
  1954. - f.write("include %s\n" % cfg.versionfile_source)
  1955. - else:
  1956. - print(" versionfile_source already in MANIFEST.in")
  1957. + maybe_ipy = None
  1958. # Make VCS-specific changes. For git, this means creating/changing
  1959. # .gitattributes to mark _version.py for export-subst keyword
  1960. # substitution.
  1961. - do_vcs_install(manifest_in, cfg.versionfile_source, ipy)
  1962. + do_vcs_install(cfg.versionfile_source, maybe_ipy)
  1963. return 0
  1964. -def scan_setup_py():
  1965. +def scan_setup_py() -> int:
  1966. """Validate the contents of setup.py against Versioneer's expectations."""
  1967. found = set()
  1968. setters = False
  1969. @@ -1813,10 +2321,14 @@ def scan_setup_py():
  1970. return errors
  1971. +def setup_command() -> NoReturn:
  1972. + """Set up Versioneer and exit with appropriate error code."""
  1973. + errors = do_setup()
  1974. + errors += scan_setup_py()
  1975. + sys.exit(1 if errors else 0)
  1976. +
  1977. +
  1978. if __name__ == "__main__":
  1979. cmd = sys.argv[1]
  1980. if cmd == "setup":
  1981. - errors = do_setup()
  1982. - errors += scan_setup_py()
  1983. - if errors:
  1984. - sys.exit(1)
  1985. + setup_command()
  1986. --
  1987. 2.41.0