├── .gitattributes ├── .gitchangelog.rc ├── .gitignore ├── CHANGELOG ├── LICENSE ├── MANIFEST.in ├── README.rst ├── azure-pipelines.yml ├── azure └── job_template.yml ├── mypy.ini ├── pyproject.toml ├── requirements-dev.txt ├── requirements.txt ├── setup.cfg ├── setup.py ├── src └── webdrivermanager │ ├── __init__.py │ ├── __main__.py │ ├── _version.py │ ├── base.py │ ├── chrome.py │ ├── edge.py │ ├── edgechromium.py │ ├── gecko.py │ ├── ie.py │ └── misc.py ├── tasks.py ├── test ├── __init__.py ├── acceptance │ ├── __init__.py │ ├── test_base.py │ ├── test_chrome.py │ ├── test_edge.py │ ├── test_edgechromium.py │ ├── test_gecko.py │ ├── test_ie.py │ └── tools.py └── unit │ └── __init__.py ├── tox.ini └── versioneer.py /.gitattributes: -------------------------------------------------------------------------------- 1 | webdrivermanager/_version.py export-subst 2 | -------------------------------------------------------------------------------- /.gitchangelog.rc: -------------------------------------------------------------------------------- 1 | ## 2 | ## Format 3 | ## 4 | ## ACTION: [AUDIENCE:] COMMIT_MSG [@TAG ...] 5 | ## 6 | ## Description 7 | ## 8 | ## ACTION is one of 'chg', 'fix', 'new' 9 | ## 10 | ## Is WHAT the change is about. 11 | ## 12 | ## 'chg' is for refactor, small improvement, cosmetic changes... 13 | ## 'fix' is for bug fixes 14 | ## 'new' is for new features, big improvement 15 | ## 16 | ## SUBJECT is optional and one of 'dev', 'usr', 'pkg', 'test', 'doc' 17 | ## 18 | ## Is WHO is concerned by the change. 19 | ## 20 | ## 'dev' is for developpers (API changes, refactors...) 21 | ## 'usr' is for final users (UI changes) 22 | ## 'pkg' is for packagers (packaging changes) 23 | ## 'test' is for testers (test only related changes) 24 | ## 'doc' is for doc guys (doc only changes) 25 | ## 26 | ## COMMIT_MSG is ... well ... the commit message itself. 27 | ## 28 | ## TAGs are additionnal adjective as 'refactor' 'minor' 'cosmetic' 29 | ## 30 | ## 'refactor' is obviously for refactoring code only 31 | ## 'minor' is for a very meaningless change (a typo, adding a comment) 32 | ## 'cosmetic' is for cosmetic driven change (re-indentation, 80-col...) 33 | ## 'wip' is for partial functionality but complete subfunctionality. 34 | ## 35 | ## Example: 36 | ## 37 | ## new: usr: support of bazaar implemented 38 | ## chg: re-indentend some lines @cosmetic 39 | ## new: dev: updated code to be compatible with last version of killer lib. 40 | ## fix: pkg: updated year of licence coverage. 41 | ## new: test: added a bunch of test around user usability of feature X. 42 | ## fix: typo in spelling my name in comment. @minor 43 | ## 44 | ## Please note that multi-line commit message are supported, and only the 45 | ## first line will be considered as the "summary" of the commit message. So 46 | ## tags, and other rules only applies to the summary. The body of the commit 47 | ## message will be displayed in the changelog with minor reformating. 48 | 49 | 50 | ## 51 | ## ``ignore_regexps`` is a line of regexps 52 | ## 53 | ## Any commit having its full commit message matching any regexp listed here 54 | ## will be ignored and won't be reported in the changelog. 55 | ## 56 | ignore_regexps = [ 57 | r'@minor', r'!minor', 58 | r'@cosmetic', r'!cosmetic', 59 | r'@refactor', r'!refactor', 60 | r'bump', r'dump', 61 | r'@wip', r'!wip', 62 | r'^([cC]hg|[fF]ix|[nN]ew)\s*:\s*[p|P]kg:', 63 | r'^([cC]hg|[fF]ix|[nN]ew)\s*:\s*[d|D]ev:', 64 | r'^(.{3,3}\s*:)?\s*[fF]irst commit.?\s*$', 65 | ] 66 | 67 | 68 | ## 69 | ## ``replace_regexps`` is a dict associating a regexp pattern and its replacement 70 | ## 71 | ## It will be applied to get the summary line from the full commit message. 72 | ## 73 | ## Note that you can provide multiple replacement patterns, they will be all 74 | ## tried. If None matches, the summary line will be the full commit message. 75 | ## 76 | replace_regexps = { 77 | ## current format (ie: 'chg: dev: my commit msg @tag1 @tag2') 78 | 79 | r'^([cC]hg|[fF]ix|[nN]ew)\s*:\s*((dev|use?r|pkg|test|doc)\s*:\s*)?([^\n@]*)(@[a-z]+\s+)*$': 80 | r'\4', 81 | } 82 | 83 | 84 | ## ``section_regexps`` is a list of 2-tuples associating a string label and a 85 | ## list of regexp 86 | ## 87 | ## Commit messages will be classified in sections thanks to this. Section 88 | ## titles are the label, and a commit is classified under this section if any 89 | ## of the regexps associated is matching. 90 | ## 91 | section_regexps = [ 92 | ('New', [ 93 | r'^[nN]ew\s*:\s*((dev|use?r|pkg|test|doc)\s*:\s*)?([^\n]*)$', 94 | ]), 95 | ('Changes', [ 96 | r'^[cC]hg\s*:\s*((dev|use?r|pkg|test|doc)\s*:\s*)?([^\n]*)$', 97 | ]), 98 | ('Fix', [ 99 | r'^[fF]ix\s*:\s*((dev|use?r|pkg|test|doc)\s*:\s*)?([^\n]*)$', 100 | ]), 101 | 102 | ('Other', None ## Match all lines 103 | ), 104 | 105 | ] 106 | 107 | 108 | ## ``body_split_regexp`` is a regexp 109 | ## 110 | ## Commit message body (not the summary) if existing will be split 111 | ## (new line) on this regexp 112 | ## 113 | body_split_regexp = r'\n(?=\w+\s*:)' 114 | 115 | 116 | ## ``tag_filter_regexp`` is a regexp 117 | ## 118 | ## Tags that will be used for the changelog must match this regexp. 119 | ## 120 | tag_filter_regexp = r'^[0-9]+\.[0-9]+(\.[0-9]+)?$' 121 | 122 | 123 | ## ``unreleased_version_label`` is a string 124 | ## 125 | ## This label will be used as the changelog Title of the last set of changes 126 | ## between last valid tag and HEAD if any. 127 | unreleased_version_label = "(unreleased)" 128 | 129 | 130 | ## ``output_engine`` is a callable 131 | ## 132 | ## This will change the output format of the generated changelog file 133 | ## 134 | ## Available choices are: 135 | ## 136 | ## - rest_py 137 | ## 138 | ## Legacy pure python engine, outputs ReSTructured text. 139 | ## This is the default. 140 | ## 141 | ## - mustache() 142 | ## 143 | ## Template name could be any of the available templates in 144 | ## ``templates/mustache/*.tpl``. 145 | ## Requires python package ``pystache``. 146 | ## Examples: 147 | ## - mustache("markdown") 148 | ## - mustache("restructuredtext") 149 | ## 150 | ## - makotemplate() 151 | ## 152 | ## Template name could be any of the available templates in 153 | ## ``templates/mako/*.tpl``. 154 | ## Requires python package ``mako``. 155 | ## Examples: 156 | ## - makotemplate("restructuredtext") 157 | ## 158 | output_engine = rest_py 159 | #output_engine = mustache("restructuredtext") 160 | #output_engine = mustache("markdown") 161 | #output_engine = makotemplate("restructuredtext") 162 | 163 | 164 | ## ``include_merges`` is a boolean 165 | ## 166 | ## This option tells git-log whether to include merge commits in the log. 167 | ## The default is to include them. 168 | include_merges = True 169 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # Byte-compiled / optimized / DLL files 2 | __pycache__/ 3 | *.py[cod] 4 | *$py.class 5 | 6 | # C extensions 7 | *.so 8 | 9 | # Distribution / packaging 10 | .Python 11 | env/ 12 | build/ 13 | develop-eggs/ 14 | dist/ 15 | downloads/ 16 | eggs/ 17 | .eggs/ 18 | lib/ 19 | lib64/ 20 | parts/ 21 | sdist/ 22 | var/ 23 | wheels/ 24 | *.egg-info/ 25 | .installed.cfg 26 | *.egg 27 | 28 | # PyInstaller 29 | # Usually these files are written by a python script from a template 30 | # before PyInstaller builds the exe, so as to inject date/other infos into it. 31 | *.manifest 32 | *.spec 33 | 34 | # Installer logs 35 | pip-log.txt 36 | pip-delete-this-directory.txt 37 | 38 | # Unit test / coverage reports 39 | htmlcov/ 40 | .tox/ 41 | .coverage 42 | .coverage.* 43 | .cache 44 | nosetests.xml 45 | coverage.xml 46 | *.cover 47 | .hypothesis/ 48 | 49 | # Translations 50 | *.mo 51 | *.pot 52 | 53 | # Django stuff: 54 | *.log 55 | local_settings.py 56 | 57 | # Flask stuff: 58 | instance/ 59 | .webassets-cache 60 | 61 | # Scrapy stuff: 62 | .scrapy 63 | 64 | # Sphinx documentation 65 | docs/_build/ 66 | 67 | # PyBuilder 68 | target/ 69 | 70 | # Jupyter Notebook 71 | .ipynb_checkpoints 72 | 73 | # pyenv 74 | .python-version 75 | 76 | # celery beat schedule file 77 | celerybeat-schedule 78 | 79 | # SageMath parsed files 80 | *.sage.py 81 | 82 | # dotenv 83 | .env 84 | 85 | # virtualenv 86 | .venv 87 | venv/ 88 | ENV/ 89 | 90 | # Spyder project settings 91 | .spyderproject 92 | .spyproject 93 | 94 | # Rope project settings 95 | .ropeproject 96 | 97 | # mkdocs documentation 98 | /site 99 | 100 | # mypy 101 | .mypy_cache/ 102 | 103 | # doit 104 | .doit.db 105 | 106 | # tests 107 | acceptance_tests.xml 108 | -------------------------------------------------------------------------------- /CHANGELOG: -------------------------------------------------------------------------------- 1 | * Mon Feb 22 2021 Jani Mikkonen - 0.10.0 2 | - Add baseclass methods for parsing latest and compatible versions (rev.9d37e559) 3 | - Code restructure and refactorings (rev.ded7c464) 4 | - Add option for 'compatible' chromedriver version (rev.537edf64) 5 | - Version detection/download on EdgeChromium fixed. (rev.72c9955f) 6 | - Remove 2.7 support from CI (rev.c612eefe) 7 | 8 | * Sun Sep 06 2020 Jani Mikkonen - 0.9.0 9 | - fix linux geckodriver download (rev.778b1d1c) 10 | - Fix support for EdgeChromium (rev.77485c05) 11 | - - Fix support for IE --bitness 32 (rev.288d96da) 12 | 13 | * Sat Mar 14 2020 Jani Mikkonen - 0.8.0 14 | - Release 0.8.0 (rev.758af05f) 15 | - Support for "EdgeChromium" webdrivers. (rev.5f8f8e5d) 16 | - Fix dev dependencies to support 2.7 python still. (rev.14b37f45) 17 | - dev deps updated (rev.079a3004) 18 | - Added support for tox (rev.613cdf37) 19 | - Use fixed gitchangelog from own fork (rev.a6a174ee) 20 | - Few lintian fixes (rev.5ac893f2) 21 | - Bugfix: AVAILABLE_DRIVERS exported incorrectly. (rev.17bf7453) 22 | - Basic code for Internet Explorer (rev.d069e239) 23 | - Fixes new edge driver download scheme .. (rev.6670b251) 24 | - include licese file in package (rev.961060ad) 25 | - expose get_version (rev.80e43249) 26 | 27 | * Thu Mar 14 2019 Jani Mikkonen - 0.7.4 28 | - Version bump to 0.7.4 (rev.181143ff) 29 | - Fixed imports (rev.50f96502) 30 | - Use flaky package go retry tests failed (rev.8d38107b) 31 | - Use better reporters for pylint&flake8 (rev.e4bfedc0) 32 | 33 | * Wed Mar 13 2019 Jani Mikkonen - 0.7.3 34 | - Version bump to 0.7.3 (rev.c8a2926a) 35 | - Show human readable message on connection error (rev.dc856570) 36 | - Changelog into a separate file (rev.76b3fbf6) 37 | - Add version information (rev.c9074259) 38 | - lxml removed from pylintrc (rev.6553c556) 39 | - Fixed readme (rev.d46c8453) 40 | 41 | * Wed Feb 20 2019 Jani Mikkonen - 0.7.2 42 | - Version bump to 0.7.2 (rev.c738289e) 43 | - Bugfix: unhandled exception if windows binary did not exists (rev.c23720f3) 44 | 45 | * Tue Feb 19 2019 Jani Mikkonen - 0.7.1 46 | - version bump (rev.852a3651) 47 | - Cygwin bug fixes (rev.a3b3297b) 48 | - Add option to skip link creation (rev.54c4b309) 49 | - Enable choosing binary bitness (rev.0ac1a7e4) 50 | - moar lintian fixes (rev.aa40df9c) 51 | - Bugfix: handle chromedriver bitness across platforms (rev.07940b7d) 52 | - Fix for python2/3 compatibility (rev.9a4c020e) 53 | - Fix for requirements (rev.884727ce) 54 | - Rewrite for get_download_url (rev.9952ec63) 55 | - Updated requirements-dev (rev.0d70fdae) 56 | - Updated readme to include edge (rev.f005d076) 57 | - Pylint fixes (rev.1eed6bce) 58 | - BeautifulSoup instead of lxml (rev.b0fcdc24) 59 | - Run flake8 & pylint on ci and generate junits (rev.213136ae) 60 | - Cygwin support (rev.367d9782) 61 | - Extended tests and lots of flake8 fixes (rev.9fa5faeb) 62 | - updated readme (rev.5b7e3b49) 63 | - Overwrite also binary if it exists (rev.528f703d) 64 | - option to autodiscover bin location (rev.8ea9817f) 65 | 66 | * Tue Jan 29 2019 Jani Mikkonen - 0.6.0 67 | - Refactor cli portion (rev.ee43c164) 68 | - Refactor cli portion (rev.dbf95149) 69 | 70 | * Fri Jan 04 2019 Jani Mikkonen - 0.5.0 71 | - Export test results to azure (rev.f5fbfc45) 72 | - Version bump to 0.5.0 (rev.00f66be2) 73 | - Dont crash with unknown browser string (rev.8118dab2) 74 | - Basic support for edge (rev.e9af3942) 75 | - Add possibility to download any os binaries (rev.4a705c0a) 76 | - Removed dead code (rev.dbd166d6) 77 | 78 | * Fri Nov 02 2018 Jani Mikkonen - 0.4.2 79 | - Release: 0.4.2 (rev.d01a3c35) 80 | - Corner case for Mac OS X (rev.e4210de7) 81 | - Dont publish artifacts (rev.c3233343) 82 | - Fixed fallback github method (rev.f712f36d) 83 | 84 | * Wed Oct 31 2018 Jani Mikkonen - 0.4.1 85 | - version bump (rev.7a2c4f0c) 86 | - Fix & release (rev.d9cdaf2e) 87 | - code deduplication (rev.aa5417aa) 88 | - doit tasks and few dependencies for dev side (rev.37a250b7) 89 | - publish artifacts task (rev.79d324ae) 90 | - python 2.7 fix (rev.fc7f4e9e) 91 | - Removed cruft (rev.d60897eb) 92 | - Basic pipeline (rev.eba22e39) 93 | - Fix download_and_install show_progress_bar handling (rev.931a2457) 94 | - acceptance tests to just prevent things getting broken (rev.e3907313) 95 | - Bug fixes: (rev.7f560bb8) 96 | - All Downloaders into Managers (rev.b9f3c0f0) 97 | 98 | * Sun Oct 28 2018 Jani Mikkonen - 0.4.0 99 | - Version bump to 0.4.0 (rev.1db2cb4d) 100 | - Fixed chrome/opera download in mac (rev.cdd21b92) 101 | - setup.py pulls requirements.txt (rev.70be9c34) 102 | - Added lxml dependency (rev.78cee05d) 103 | - Added a fallback download method for opera chromium driver (rev.03766304) 104 | - Added a fallback download method for geckodriver (rev.f4a364ea) 105 | 106 | * Sat Oct 27 2018 Jani Mikkonen - 0.3.0 107 | - Version bump (rev.e368c12f) 108 | - Updated readme (rev.ee237b21) 109 | - cache get_download_path calls (rev.3749cc01) 110 | - Updated readme (rev.09c3569f) 111 | 112 | * Fri Oct 26 2018 Jani Mikkonen - 0.2.0 113 | - utf8 for the win (rev.26d907aa) 114 | - support passing dl and linkpath from cli (rev.2a89467c) 115 | - Misc changes (rev.fea92ae7) 116 | - Setup changes (rev.3dc12714) 117 | - dev requirements (rev.31015de3) 118 | - Fix setup.py (rev.a3bdf592) 119 | - If venv is activate, choose install location accordingly. (rev.66d91386) 120 | - Rename the poroject (rev.86cd02b5) 121 | - Fix up dependencies, add a few more classifiers to setup. (rev.88761b17) 122 | - Updated README.rst to include info about command line tool, downloaders for Chrome and Chromium-based Opera browsers. (rev.d9779a68) 123 | - Finish adding support for downloading a specific WebDriver version to the command-line tool. (rev.b1a2a6ae) 124 | - Fix display bug if link_path is not in the PATH environment variable. (rev.fdd785e0) 125 | - Add command-line support. (rev.b2a9d735) 126 | - Initial work on OperaChromiumDriverDownloader class. Change download behavior to include webdriver name and version in the download path (needed since the version number is not embedded in the filename for the Opera driver). Fix bug where the executable bit was not getting set on the extracted driver binary. (rev.8f6c34ff) 127 | - Initial work on ChromeDriverDownloader class. (rev.ab1c088e) 128 | - Change Windows behavior to simply copy the driver binary since symlinking requires special privileges and creating a wrapper .cmd script does not work (webdriver expects the .exe to be in the system path). (rev.c410d41e) 129 | - Fix bug with .zip file handling; update License section in README to note that the licenses of the WebDriver downloads should be checked prior to download / use. (rev.c1ba19aa) 130 | - Add requirements.txt. (rev.f5c35d1f) 131 | - Initial commit. (rev.9478d458) 132 | 133 | 134 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2018 Leonides T. Saguisag, Jr. 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /MANIFEST.in: -------------------------------------------------------------------------------- 1 | include requirements.txt CHANGELOG README.rst LICENSE 2 | exclude tasks.py .gitignore azure-pipelines.yml azure/* test/* 3 | include versioneer.py 4 | include src/webdrivermanager/_version.py 5 | -------------------------------------------------------------------------------- /README.rst: -------------------------------------------------------------------------------- 1 | 2 | 3 | ``webdrivermanager`` 4 | ======================= 5 | 6 | Python module to facilitate downloading and deploying `WebDriver `_ binaries. The classes in this module can be used to automatically search for and download the latest version (or a specific version) of a WebDriver binary and then extract it and place it by copying or symlinking it to the location where Selenium or other tools should be able to find it then. 7 | 8 | 9 | Installation 10 | ------------ 11 | 12 | This module is available on the Python Package Index (PyPI) and can be installed as follows: 13 | 14 | ``pip install webdrivermanager`` 15 | 16 | 17 | Dependencies 18 | ------------ 19 | 20 | This module is dependent on the following additional packages: 21 | 22 | - `requests `_ 23 | - `tqdm `_ 24 | - `BeautifulSoup4 `_ 25 | - `appdirs `_ 26 | 27 | 28 | Classes 29 | ------- 30 | 31 | The following classes are available: 32 | 33 | - ``ChromeDriverManager`` for downloading and installing `chromedriver `_ (for Google Chrome). 34 | - ``GeckoDriverManager`` for downloading and installing `geckodriver `_ (for Mozilla Firefox). 35 | - ``EdgeDriverManager`` for downloading and installing `edgedriver `_ (for Microsoft Edge). 36 | - ``EdgeChromiumDriverManager`` for downloading and installing Edge Chromium based webdrivers 37 | - ``IeDriverManager`` for downloading and installing Internet Explorer based webdrivers 38 | 39 | 40 | Status 41 | ------ 42 | 43 | Currently being developed/tested using Python 2.7.15 and 3.7 on macOS, Windows & Linux 44 | 45 | 46 | Example module usage 47 | -------------------- 48 | 49 | Example:: 50 | 51 | >>> from webdrivermanager import GeckoDriverManager 52 | >>> gdd = GeckoDriverManager() 53 | >>> gdd.download_and_install() 54 | 1524kb [00:00, 1631.24kb/s] 55 | ('/Users/rasjani/webdriver/geckodriver-v0.20.1-macos/geckodriver', '/Users/rasjani/bin/geckodriver') 56 | >>> gdd.download_and_install("v0.20.0") 57 | 1501kb [00:02, 678.92kb/s] 58 | Symlink /Users/rasjani/bin/geckodriver already exists and will be overwritten. 59 | ('/Users/rasjani/webdriver/geckodriver-v0.20.0-macos/geckodriver', '/Users/rasjani/bin/geckodriver') 60 | >>> gdd.download_and_install() 61 | Symlink /Users/rasjani/bin/geckodriver already exists and will be overwritten. 62 | ('/Users/rasjani/webdriver/geckodriver-v0.20.1-macos/geckodriver', '/Users/rasjani/bin/geckodriver') 63 | >>> 64 | 65 | 66 | Command line tool 67 | ----------------- 68 | 69 | There is a command-line tool that is also available. After installing the package, it can be used as follows (Windows example):: 70 | 71 | > webdrivermanager chrome:2.38 firefox 72 | Downloading WebDriver for browser: 'chrome' 73 | 3300kb [00:00, 11216.38kb/s] 74 | Driver binary downloaded to: C:\Users\rasjani\webdriver\chrome\2.38\2.38%2Fchromedriver_win32\chromedriver.exe 75 | Driver copied to: C:\Users\rasjani\bin\chromedriver.exe 76 | 77 | Downloading WebDriver for browser: 'firefox' 78 | 3031kb [00:01, 2253.64kb/s] 79 | Driver binary downloaded to: C:\Users\rasjani\webdriver\gecko\v0.20.1\geckodriver-v0.20.1-win64\geckodriver.exe 80 | Driver copied to: C:\Users\rasjani\bin\geckodriver.exe 81 | 82 | WARNING: Path 'C:\Users\rasjani\bin' is not in the PATH environment variable. 83 | 84 | In the above example, a version was specified for Chrome while no version was specified for Firefox so the latest version of ``geckodriver`` was implicitly downloaded. 85 | 86 | Command line options 87 | -------------------- 88 | 89 | usage: webdrivermanager [-h] [--downloadpath F] [--linkpath F] [--os OSNAME] 90 | browser [browser ...] 91 | 92 | Tool for downloading and installing WebDriver binaries. 93 | 94 | positional arguments: 95 | browser Browser to download the corresponding WebDriver 96 | binary. Valid values are: chrome, firefox, gecko, 97 | mozilla, edge. Optionally specify a version 98 | number of the WebDriver binary as follows: 99 | 'browser:version' e.g. 'chrome:2.39'. If no version 100 | number is specified, the latest available version of 101 | the WebDriver binary will be downloaded. 102 | 103 | optional arguments: 104 | -h, --help show this help message and exit 105 | --downloadpath F, -d F 106 | Where to download the webdriver binaries 107 | --linkpath F, -l F Where to link the webdriver binary to. Set to "AUTO" 108 | if you need some intelligence to decice where to place 109 | the final webdriver binary 110 | --linkpath F, -l F Where to link the webdriver binary to. Set to "AUTO" 111 | if you need some intelligense to decide where to place 112 | the final webdriver binary. If set to "SKIP", no 113 | link/copy done 114 | --os OSNAME, -o OSNAME 115 | Overrides os detection with given os name 116 | 117 | 118 | Do note that `--downloadpath`/`-d` flag location is used for storing the whole downloaded and then `--linkpath`/`-l` path location is where the final binary is either symlinled or copied to. Linkpath should be the directory you either already have in PATH or you should place there since tools using these webdrivers usually locate the appropriate webdriver binary from PATH environment variable. 119 | 120 | If linkpath flag is set to *AUTO*, tool will iterate over your current PATH environment variable and tries to find the first writeable directory within it and place the copy or symlink into it. If linkpath is set to *SKIP*, only download is done, linking/copying is skipped. 121 | 122 | License 123 | ------- 124 | 125 | This is released under an MIT license. See the ``LICENSE`` file in this repository for more information. 126 | 127 | Consult the license terms of the providers of the WebDriver downloads prior to downloading / using the WebDrivers. 128 | -------------------------------------------------------------------------------- /azure-pipelines.yml: -------------------------------------------------------------------------------- 1 | jobs: 2 | - template: azure/job_template.yml 3 | parameters: 4 | name: 'OSX_37' 5 | vmImage: 'macOS-10.14' 6 | python_version: '3.7' 7 | - template: azure/job_template.yml 8 | parameters: 9 | name: 'Windows_39' 10 | vmImage: 'VS2017-Win2016' 11 | python_version: '3.9' 12 | - template: azure/job_template.yml 13 | parameters: 14 | name: 'Linux_38' 15 | vmImage: 'ubuntu-16.04' 16 | python_version: '3.8' 17 | -------------------------------------------------------------------------------- /azure/job_template.yml: -------------------------------------------------------------------------------- 1 | parameters: 2 | name: 'webdrivermanager' 3 | vmImage: '' 4 | 5 | jobs: 6 | - job: ${{ parameters.name }} 7 | timeoutInMinutes: 60 8 | cancelTimeoutInMinutes: 25 9 | pool: 10 | vmImage: ${{ parameters.vmImage }} 11 | strategy: 12 | matrix: 13 | Python: 14 | os_name: ${{ parameters.name }} 15 | python_version: ${{ parameters.python_version}} 16 | image: ${{ parameters.vmImage }} 17 | maxParallel: 2 18 | 19 | steps: 20 | - task: UsePythonVersion@0 21 | displayName: 'Python for test execution' 22 | inputs: 23 | versionSpec: $(python_version) 24 | addToPath: true 25 | architecture: 'x64' 26 | 27 | - script: | 28 | python -m pip install --upgrade pip wheel 29 | python -m pip install -r requirements-dev.txt 30 | displayName: 'Install requirements' 31 | 32 | - script: pytest test/acceptance --junitxml acceptance_tests.xml 33 | displayName: 'Running Acceptance Tests' 34 | continueOnError: true 35 | 36 | - task: PublishTestResults@2 37 | inputs: 38 | testRunner: JUnit 39 | testResultsFiles: acceptance_tests.xml 40 | testResultsTitle: Acceptance Results for $(os_name) with Python $(python_version) 41 | testRunTitle: Python-$(python_version)-$(os_name)-acceptance 42 | publishRunAttachments: true 43 | 44 | - script: | 45 | flake8 --exit-zero --output flake8.xml --format=junit-xml 46 | displayName: 'Running Flake8 Static Analysis' 47 | continueOnError: true 48 | condition: startsWith(variables['image'], 'macOS') 49 | 50 | - task: PublishTestResults@2 51 | inputs: 52 | testRunner: JUnit 53 | testResultsFiles: flake8.xml 54 | testResultsTitle: Flake8 Results for $(os_name) with Python $(python_version) 55 | testRunTitle: Python-$(python_version)-$(os_name)-flake8 56 | condition: startsWith(variables['image'], 'macOS') 57 | 58 | - script: python setup.py sdist 59 | displayName: 'Generate sdist files' 60 | -------------------------------------------------------------------------------- /mypy.ini: -------------------------------------------------------------------------------- 1 | # Global options: 2 | 3 | [mypy] 4 | python_version = 3.7 5 | warn_return_any = False 6 | warn_unused_configs = True 7 | python_executable = python 8 | ignore_missing_imports = True 9 | files=src/webdrivermanager/*.py 10 | # Per-module options: 11 | -------------------------------------------------------------------------------- /pyproject.toml: -------------------------------------------------------------------------------- 1 | [tool.black] 2 | line-length = 130 3 | target-version = ['py37'] 4 | exclude = ''' 5 | ( 6 | /( 7 | | \.git 8 | | \venv 9 | | \.venv 10 | )/ | versioneer.py | _version.py 11 | ) 12 | ''' 13 | -------------------------------------------------------------------------------- /requirements-dev.txt: -------------------------------------------------------------------------------- 1 | twine 2 | flake8 3 | flake8-formatter-junit-xml 4 | flake8-self 5 | flake8-eradicate 6 | flake8-alfred 7 | flake8-builtins 8 | flake8-commas 9 | python-language-server[all] 10 | coverage 11 | unittest-xml-reporting 12 | invoke 13 | flaky 14 | gcg 15 | tox 16 | tox-pyenv 17 | tox-venv 18 | pytest 19 | mypy 20 | black 21 | -r requirements.txt 22 | -------------------------------------------------------------------------------- /requirements.txt: -------------------------------------------------------------------------------- 1 | requests 2 | tqdm 3 | BeautifulSoup4 4 | appdirs 5 | lxml 6 | -------------------------------------------------------------------------------- /setup.cfg: -------------------------------------------------------------------------------- 1 | [sdist] 2 | formats=gztar,zip 3 | 4 | [wheel] 5 | universal=1 6 | 7 | [bdist_wheel] 8 | universal=1 9 | 10 | [flake8] 11 | ignore = E501 12 | max-line-length = 130 13 | exclude = .git,__pycache__,dist,venv,setup.py,versioneer.py,_version.py,build 14 | 15 | [coverage:run] 16 | branch = True 17 | 18 | [coverage:report] 19 | exclude_lines = 20 | pragma: no cover 21 | raise AssertionError 22 | raise NotImplementedError 23 | if __name__ == .__main__.: 24 | 25 | ignore_errors = True 26 | omit = 27 | venv/* 28 | test/* 29 | 30 | [coverage:paths] 31 | source = src/webdrivermanager 32 | [coverage:xml] 33 | output = coverage.xml 34 | [coverage:html] 35 | directory = report 36 | 37 | [versioneer] 38 | VCS = git 39 | style = pep440 40 | versionfile_source = src/webdrivermanager/_version.py 41 | versionfile_build = webdrivermanager/_version.py 42 | tag_prefix = 43 | -------------------------------------------------------------------------------- /setup.py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | 3 | from codecs import open 4 | from os.path import join, abspath, dirname 5 | 6 | from setuptools import setup 7 | import versioneer 8 | 9 | CWD = abspath(dirname(__file__)) 10 | PACKAGE_NAME = "webdrivermanager" 11 | 12 | # Get the long description from the README file 13 | with open(join(CWD, "README.rst"), encoding="utf-8") as f: 14 | long_description = f.read() 15 | 16 | # Get version 17 | CWD = abspath(dirname(__file__)) 18 | 19 | with open(join(CWD, "requirements.txt"), encoding="utf-8") as f: 20 | REQUIREMENTS = f.read().splitlines() 21 | 22 | CLASSIFIERS = """ 23 | Development Status :: 4 - Beta 24 | Environment :: Console 25 | Intended Audience :: Developers 26 | Intended Audience :: End Users/Desktop 27 | Intended Audience :: Information Technology 28 | Intended Audience :: System Administrators 29 | License :: OSI Approved :: MIT License 30 | Programming Language :: Python 31 | Programming Language :: Python :: 3 32 | Programming Language :: Python :: 3.7 33 | Programming Language :: Python :: 3.8 34 | Programming Language :: Python :: 3.9 35 | Topic :: Software Development :: Libraries 36 | Topic :: Software Development :: Quality Assurance 37 | Topic :: Software Development :: Testing 38 | Topic :: Utilities 39 | Operating System :: MacOS 40 | Operating System :: Microsoft :: Windows 41 | Operating System :: POSIX :: Linux 42 | Operating System :: POSIX :: Other 43 | """.strip().splitlines() 44 | 45 | setup( 46 | name=PACKAGE_NAME, 47 | version=versioneer.get_version(), 48 | cmdclass=versioneer.get_cmdclass(), 49 | description="Module for facilitating download and deploy of WebDriver binaries.", 50 | long_description=long_description, 51 | classifiers=CLASSIFIERS, 52 | url="https://github.com/rasjani/webdrivermanager", 53 | author="Jani Mikkonen", 54 | author_email="jani.mikkonen@gmail.com", 55 | license="MIT", 56 | packages=[PACKAGE_NAME], 57 | package_dir={"": "src"}, 58 | install_requires=REQUIREMENTS, 59 | include_package_data=True, 60 | platforms="any", 61 | keywords="webdriver chromedriver geckodriver edgechromiumdriver selenium", 62 | zip_safe=False, 63 | entry_points={ 64 | "console_scripts": [ 65 | "webdrivermanager = webdrivermanager.__main__:main", 66 | ], 67 | }, 68 | ) 69 | -------------------------------------------------------------------------------- /src/webdrivermanager/__init__.py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | 3 | from .base import WebDriverManagerBase 4 | from .chrome import ChromeDriverManager 5 | from .gecko import GeckoDriverManager 6 | from .edge import EdgeDriverManager 7 | from .ie import IEDriverManager 8 | from .edgechromium import EdgeChromiumDriverManager 9 | 10 | from ._version import get_versions 11 | 12 | AVAILABLE_DRIVERS = { 13 | "chrome": ChromeDriverManager, 14 | "firefox": GeckoDriverManager, 15 | "gecko": GeckoDriverManager, 16 | "mozilla": GeckoDriverManager, 17 | "edge": EdgeDriverManager, 18 | "edgechromium": EdgeChromiumDriverManager, 19 | "ie": IEDriverManager, 20 | } 21 | 22 | __all__ = [ 23 | "WebDriverManagerBase", 24 | "ChromeDriverManager", 25 | "GeckoDriverManager", 26 | "EdgeDriverManager", 27 | "IEDriverManager", 28 | "EdgeChromiumDriverManager", 29 | "get_version", 30 | "AVAILABLE_DRIVERS", 31 | ] 32 | 33 | __version__ = get_versions()["version"] 34 | 35 | 36 | def get_version(): 37 | return get_versions()["version"] 38 | -------------------------------------------------------------------------------- /src/webdrivermanager/__main__.py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | 3 | import os 4 | import sys 5 | import argparse 6 | from requests import ConnectionError 7 | 8 | from ._version import get_versions 9 | from webdrivermanager import AVAILABLE_DRIVERS as DOWNLOADERS 10 | from .misc import LOGGER, LOG_LEVELS 11 | 12 | 13 | OS_NAMES = ["mac", "win", "linux"] 14 | BITNESS = ["32", "64"] 15 | 16 | 17 | def parse_command_line(): 18 | parser = argparse.ArgumentParser( 19 | description=f"Tool for downloading and installing WebDriver binaries. Version: {get_versions()['version']}", 20 | ) 21 | parser.add_argument( 22 | "browser", 23 | help=f"Browser to download the corresponding WebDriver binary. Valid values are: {' '.join(DOWNLOADERS.keys())}. Optionally specify a version number of the WebDriver binary as follows: 'browser:version' e.g. 'chrome:2.39'. If no version number is specified, the latest available version of the WebDriver binary will be downloaded.", 24 | nargs="+", 25 | ) 26 | parser.add_argument( 27 | "--downloadpath", 28 | "-d", 29 | action="store", 30 | dest="downloadpath", 31 | metavar="F", 32 | default=None, 33 | help="Where to download the webdriver binaries", 34 | ) 35 | parser.add_argument( 36 | "--linkpath", 37 | "-l", 38 | action="store", 39 | dest="linkpath", 40 | metavar="F", 41 | default=None, 42 | help='Where to link the webdriver binary to. Set to "AUTO" if you need some intelligense to decide where to place the final webdriver binary. If set to "SKIP", no link/copy done.', 43 | ) 44 | parser.add_argument( 45 | "--os", 46 | "-o", 47 | action="store", 48 | dest="os_name", 49 | choices=OS_NAMES, 50 | metavar="OSNAME", 51 | default=None, 52 | help=f"Overrides os detection with given os name. Values: {' '.join(OS_NAMES)}", 53 | ) 54 | parser.add_argument( 55 | "--bitness", 56 | "-b", 57 | action="store", 58 | dest="bitness", 59 | choices=BITNESS, 60 | metavar="BITS", 61 | default=None, 62 | help=f"Overrides bitness detection with given value. Values: {' '.join(BITNESS)}", 63 | ) 64 | parser.add_argument("--version", action="version", version=f"%(prog)s {get_versions()['version']}") 65 | parser.add_argument("--loglevel", default="info", dest="loglevel", choices=list(LOG_LEVELS.keys())[1:], help="Log Level") 66 | return parser.parse_args() 67 | 68 | 69 | def main(): 70 | args = parse_command_line() 71 | LOGGER.setLevel(LOG_LEVELS[args.loglevel]) 72 | for browser in args.browser: 73 | 74 | if ":" in browser: 75 | browser, version = browser.split(":") 76 | else: 77 | version = "compatible" 78 | 79 | if browser.lower() in DOWNLOADERS.keys(): 80 | print(f'Downloading WebDriver for browser: "{browser}"') 81 | downloader = DOWNLOADERS[browser](args.downloadpath, args.linkpath, args.os_name, args.bitness) 82 | 83 | try: 84 | extracted_binary, link = downloader.download_and_install(version) 85 | except ConnectionError: 86 | print("Unable to download webdriver's at this time due to network connectivity error") 87 | sys.exit(1) 88 | 89 | print(f'Driver binary downloaded to: "{extracted_binary}"') 90 | if link: 91 | if link.is_symlink(): 92 | print(f"Symlink created: {link}") 93 | else: 94 | print(f"Driver copied to: {link}") 95 | link_path = link.parent 96 | if str(link_path) not in os.environ["PATH"].split(os.pathsep): 97 | print(f'WARNING: Path "{link_path}" is not in the PATH environment variable.') 98 | else: 99 | print("Linking webdriver skipped") 100 | else: 101 | print('Unrecognized browser: "{browser}". Ignoring...') 102 | print("") 103 | 104 | 105 | if __name__ == "__main__": 106 | main() 107 | -------------------------------------------------------------------------------- /src/webdrivermanager/_version.py: -------------------------------------------------------------------------------- 1 | 2 | # This file helps to compute a version number in source trees obtained from 3 | # git-archive tarball (such as those provided by githubs download-from-tag 4 | # feature). Distribution tarballs (built by setup.py sdist) and build 5 | # directories (produced by setup.py build) will contain a much shorter file 6 | # that just contains the computed version number. 7 | 8 | # This file is released into the public domain. Generated by 9 | # versioneer-0.18 (https://github.com/warner/python-versioneer) 10 | 11 | """Git implementation of _version.py.""" 12 | 13 | import errno 14 | import os 15 | import re 16 | import subprocess 17 | import sys 18 | 19 | 20 | def get_keywords(): 21 | """Get the keywords needed to look up the version information.""" 22 | # these strings will be replaced by git during git-archive. 23 | # setup.py/versioneer.py will grep for the variable names, so they must 24 | # each be defined on a line of their own. _version.py will just call 25 | # get_keywords(). 26 | git_refnames = "$Format:%d$" 27 | git_full = "$Format:%H$" 28 | git_date = "$Format:%ci$" 29 | keywords = {"refnames": git_refnames, "full": git_full, "date": git_date} 30 | return keywords 31 | 32 | 33 | class VersioneerConfig: 34 | """Container for Versioneer configuration parameters.""" 35 | 36 | 37 | def get_config(): 38 | """Create, populate and return the VersioneerConfig() object.""" 39 | # these strings are filled in when 'setup.py versioneer' creates 40 | # _version.py 41 | cfg = VersioneerConfig() 42 | cfg.VCS = "git" 43 | cfg.style = "pep440" 44 | cfg.tag_prefix = "" 45 | cfg.parentdir_prefix = "None" 46 | cfg.versionfile_source = "webdrivermanager/_version.py" 47 | cfg.verbose = False 48 | return cfg 49 | 50 | 51 | class NotThisMethod(Exception): 52 | """Exception raised if a method is not valid for the current scenario.""" 53 | 54 | 55 | LONG_VERSION_PY = {} 56 | HANDLERS = {} 57 | 58 | 59 | def register_vcs_handler(vcs, method): # decorator 60 | """Decorator to mark a method as the handler for a particular VCS.""" 61 | def decorate(f): 62 | """Store f in HANDLERS[vcs][method].""" 63 | if vcs not in HANDLERS: 64 | HANDLERS[vcs] = {} 65 | HANDLERS[vcs][method] = f 66 | return f 67 | return decorate 68 | 69 | 70 | def run_command(commands, args, cwd=None, verbose=False, hide_stderr=False, 71 | env=None): 72 | """Call the given command(s).""" 73 | assert isinstance(commands, list) 74 | p = None 75 | for c in commands: 76 | try: 77 | dispcmd = str([c] + args) 78 | # remember shell=False, so use git.cmd on windows, not just git 79 | p = subprocess.Popen([c] + args, cwd=cwd, env=env, 80 | stdout=subprocess.PIPE, 81 | stderr=(subprocess.PIPE if hide_stderr 82 | else None)) 83 | break 84 | except EnvironmentError: 85 | e = sys.exc_info()[1] 86 | if e.errno == errno.ENOENT: 87 | continue 88 | if verbose: 89 | print("unable to run %s" % dispcmd) 90 | print(e) 91 | return None, None 92 | else: 93 | if verbose: 94 | print("unable to find command, tried %s" % (commands,)) 95 | return None, None 96 | stdout = p.communicate()[0].strip() 97 | if sys.version_info[0] >= 3: 98 | stdout = stdout.decode() 99 | if p.returncode != 0: 100 | if verbose: 101 | print("unable to run %s (error)" % dispcmd) 102 | print("stdout was %s" % stdout) 103 | return None, p.returncode 104 | return stdout, p.returncode 105 | 106 | 107 | def versions_from_parentdir(parentdir_prefix, root, verbose): 108 | """Try to determine the version from the parent directory name. 109 | 110 | Source tarballs conventionally unpack into a directory that includes both 111 | the project name and a version string. We will also support searching up 112 | two directory levels for an appropriately named parent directory 113 | """ 114 | rootdirs = [] 115 | 116 | for i in range(3): 117 | dirname = os.path.basename(root) 118 | if dirname.startswith(parentdir_prefix): 119 | return {"version": dirname[len(parentdir_prefix):], 120 | "full-revisionid": None, 121 | "dirty": False, "error": None, "date": None} 122 | else: 123 | rootdirs.append(root) 124 | root = os.path.dirname(root) # up a level 125 | 126 | if verbose: 127 | print("Tried directories %s but none started with prefix %s" % 128 | (str(rootdirs), parentdir_prefix)) 129 | raise NotThisMethod("rootdir doesn't start with parentdir_prefix") 130 | 131 | 132 | @register_vcs_handler("git", "get_keywords") 133 | def git_get_keywords(versionfile_abs): 134 | """Extract version information from the given file.""" 135 | # the code embedded in _version.py can just fetch the value of these 136 | # keywords. When used from setup.py, we don't want to import _version.py, 137 | # so we do it with a regexp instead. This function is not used from 138 | # _version.py. 139 | keywords = {} 140 | try: 141 | f = open(versionfile_abs, "r") 142 | for line in f.readlines(): 143 | if line.strip().startswith("git_refnames ="): 144 | mo = re.search(r'=\s*"(.*)"', line) 145 | if mo: 146 | keywords["refnames"] = mo.group(1) 147 | if line.strip().startswith("git_full ="): 148 | mo = re.search(r'=\s*"(.*)"', line) 149 | if mo: 150 | keywords["full"] = mo.group(1) 151 | if line.strip().startswith("git_date ="): 152 | mo = re.search(r'=\s*"(.*)"', line) 153 | if mo: 154 | keywords["date"] = mo.group(1) 155 | f.close() 156 | except EnvironmentError: 157 | pass 158 | return keywords 159 | 160 | 161 | @register_vcs_handler("git", "keywords") 162 | def git_versions_from_keywords(keywords, tag_prefix, verbose): 163 | """Get version information from git keywords.""" 164 | if not keywords: 165 | raise NotThisMethod("no keywords at all, weird") 166 | date = keywords.get("date") 167 | if date is not None: 168 | # git-2.2.0 added "%cI", which expands to an ISO-8601 -compliant 169 | # datestamp. However we prefer "%ci" (which expands to an "ISO-8601 170 | # -like" string, which we must then edit to make compliant), because 171 | # it's been around since git-1.5.3, and it's too difficult to 172 | # discover which version we're using, or to work around using an 173 | # older one. 174 | date = date.strip().replace(" ", "T", 1).replace(" ", "", 1) 175 | refnames = keywords["refnames"].strip() 176 | if refnames.startswith("$Format"): 177 | if verbose: 178 | print("keywords are unexpanded, not using") 179 | raise NotThisMethod("unexpanded keywords, not a git-archive tarball") 180 | refs = set([r.strip() for r in refnames.strip("()").split(",")]) 181 | # starting in git-1.8.3, tags are listed as "tag: foo-1.0" instead of 182 | # just "foo-1.0". If we see a "tag: " prefix, prefer those. 183 | TAG = "tag: " 184 | tags = set([r[len(TAG):] for r in refs if r.startswith(TAG)]) 185 | if not tags: 186 | # Either we're using git < 1.8.3, or there really are no tags. We use 187 | # a heuristic: assume all version tags have a digit. The old git %d 188 | # expansion behaves like git log --decorate=short and strips out the 189 | # refs/heads/ and refs/tags/ prefixes that would let us distinguish 190 | # between branches and tags. By ignoring refnames without digits, we 191 | # filter out many common branch names like "release" and 192 | # "stabilization", as well as "HEAD" and "master". 193 | tags = set([r for r in refs if re.search(r'\d', r)]) 194 | if verbose: 195 | print("discarding '%s', no digits" % ",".join(refs - tags)) 196 | if verbose: 197 | print("likely tags: %s" % ",".join(sorted(tags))) 198 | for ref in sorted(tags): 199 | # sorting will prefer e.g. "2.0" over "2.0rc1" 200 | if ref.startswith(tag_prefix): 201 | r = ref[len(tag_prefix):] 202 | if verbose: 203 | print("picking %s" % r) 204 | return {"version": r, 205 | "full-revisionid": keywords["full"].strip(), 206 | "dirty": False, "error": None, 207 | "date": date} 208 | # no suitable tags, so version is "0+unknown", but full hex is still there 209 | if verbose: 210 | print("no suitable tags, using unknown + full revision id") 211 | return {"version": "0+unknown", 212 | "full-revisionid": keywords["full"].strip(), 213 | "dirty": False, "error": "no suitable tags", "date": None} 214 | 215 | 216 | @register_vcs_handler("git", "pieces_from_vcs") 217 | def git_pieces_from_vcs(tag_prefix, root, verbose, run_command=run_command): 218 | """Get version from 'git describe' in the root of the source tree. 219 | 220 | This only gets called if the git-archive 'subst' keywords were *not* 221 | expanded, and _version.py hasn't already been rewritten with a short 222 | version string, meaning we're inside a checked out source tree. 223 | """ 224 | GITS = ["git"] 225 | if sys.platform == "win32": 226 | GITS = ["git.cmd", "git.exe"] 227 | 228 | out, rc = run_command(GITS, ["rev-parse", "--git-dir"], cwd=root, 229 | hide_stderr=True) 230 | if rc != 0: 231 | if verbose: 232 | print("Directory %s not under git control" % root) 233 | raise NotThisMethod("'git rev-parse --git-dir' returned error") 234 | 235 | # if there is a tag matching tag_prefix, this yields TAG-NUM-gHEX[-dirty] 236 | # if there isn't one, this yields HEX[-dirty] (no NUM) 237 | describe_out, rc = run_command(GITS, ["describe", "--tags", "--dirty", 238 | "--always", "--long", 239 | "--match", "%s*" % tag_prefix], 240 | cwd=root) 241 | # --long was added in git-1.5.5 242 | if describe_out is None: 243 | raise NotThisMethod("'git describe' failed") 244 | describe_out = describe_out.strip() 245 | full_out, rc = run_command(GITS, ["rev-parse", "HEAD"], cwd=root) 246 | if full_out is None: 247 | raise NotThisMethod("'git rev-parse' failed") 248 | full_out = full_out.strip() 249 | 250 | pieces = {} 251 | pieces["long"] = full_out 252 | pieces["short"] = full_out[:7] # maybe improved later 253 | pieces["error"] = None 254 | 255 | # parse describe_out. It will be like TAG-NUM-gHEX[-dirty] or HEX[-dirty] 256 | # TAG might have hyphens. 257 | git_describe = describe_out 258 | 259 | # look for -dirty suffix 260 | dirty = git_describe.endswith("-dirty") 261 | pieces["dirty"] = dirty 262 | if dirty: 263 | git_describe = git_describe[:git_describe.rindex("-dirty")] 264 | 265 | # now we have TAG-NUM-gHEX or HEX 266 | 267 | if "-" in git_describe: 268 | # TAG-NUM-gHEX 269 | mo = re.search(r'^(.+)-(\d+)-g([0-9a-f]+)$', git_describe) 270 | if not mo: 271 | # unparseable. Maybe git-describe is misbehaving? 272 | pieces["error"] = ("unable to parse git-describe output: '%s'" 273 | % describe_out) 274 | return pieces 275 | 276 | # tag 277 | full_tag = mo.group(1) 278 | if not full_tag.startswith(tag_prefix): 279 | if verbose: 280 | fmt = "tag '%s' doesn't start with prefix '%s'" 281 | print(fmt % (full_tag, tag_prefix)) 282 | pieces["error"] = ("tag '%s' doesn't start with prefix '%s'" 283 | % (full_tag, tag_prefix)) 284 | return pieces 285 | pieces["closest-tag"] = full_tag[len(tag_prefix):] 286 | 287 | # distance: number of commits since tag 288 | pieces["distance"] = int(mo.group(2)) 289 | 290 | # commit: short hex revision ID 291 | pieces["short"] = mo.group(3) 292 | 293 | else: 294 | # HEX: no tags 295 | pieces["closest-tag"] = None 296 | count_out, rc = run_command(GITS, ["rev-list", "HEAD", "--count"], 297 | cwd=root) 298 | pieces["distance"] = int(count_out) # total number of commits 299 | 300 | # commit date: see ISO-8601 comment in git_versions_from_keywords() 301 | date = run_command(GITS, ["show", "-s", "--format=%ci", "HEAD"], 302 | cwd=root)[0].strip() 303 | pieces["date"] = date.strip().replace(" ", "T", 1).replace(" ", "", 1) 304 | 305 | return pieces 306 | 307 | 308 | def plus_or_dot(pieces): 309 | """Return a + if we don't already have one, else return a .""" 310 | if "+" in pieces.get("closest-tag", ""): 311 | return "." 312 | return "+" 313 | 314 | 315 | def render_pep440(pieces): 316 | """Build up version string, with post-release "local version identifier". 317 | 318 | Our goal: TAG[+DISTANCE.gHEX[.dirty]] . Note that if you 319 | get a tagged build and then dirty it, you'll get TAG+0.gHEX.dirty 320 | 321 | Exceptions: 322 | 1: no tags. git_describe was just HEX. 0+untagged.DISTANCE.gHEX[.dirty] 323 | """ 324 | if pieces["closest-tag"]: 325 | rendered = pieces["closest-tag"] 326 | if pieces["distance"] or pieces["dirty"]: 327 | rendered += plus_or_dot(pieces) 328 | rendered += "%d.g%s" % (pieces["distance"], pieces["short"]) 329 | if pieces["dirty"]: 330 | rendered += ".dirty" 331 | else: 332 | # exception #1 333 | rendered = "0+untagged.%d.g%s" % (pieces["distance"], 334 | pieces["short"]) 335 | if pieces["dirty"]: 336 | rendered += ".dirty" 337 | return rendered 338 | 339 | 340 | def render_pep440_pre(pieces): 341 | """TAG[.post.devDISTANCE] -- No -dirty. 342 | 343 | Exceptions: 344 | 1: no tags. 0.post.devDISTANCE 345 | """ 346 | if pieces["closest-tag"]: 347 | rendered = pieces["closest-tag"] 348 | if pieces["distance"]: 349 | rendered += ".post.dev%d" % pieces["distance"] 350 | else: 351 | # exception #1 352 | rendered = "0.post.dev%d" % pieces["distance"] 353 | return rendered 354 | 355 | 356 | def render_pep440_post(pieces): 357 | """TAG[.postDISTANCE[.dev0]+gHEX] . 358 | 359 | The ".dev0" means dirty. Note that .dev0 sorts backwards 360 | (a dirty tree will appear "older" than the corresponding clean one), 361 | but you shouldn't be releasing software with -dirty anyways. 362 | 363 | Exceptions: 364 | 1: no tags. 0.postDISTANCE[.dev0] 365 | """ 366 | if pieces["closest-tag"]: 367 | rendered = pieces["closest-tag"] 368 | if pieces["distance"] or pieces["dirty"]: 369 | rendered += ".post%d" % pieces["distance"] 370 | if pieces["dirty"]: 371 | rendered += ".dev0" 372 | rendered += plus_or_dot(pieces) 373 | rendered += "g%s" % pieces["short"] 374 | else: 375 | # exception #1 376 | rendered = "0.post%d" % pieces["distance"] 377 | if pieces["dirty"]: 378 | rendered += ".dev0" 379 | rendered += "+g%s" % pieces["short"] 380 | return rendered 381 | 382 | 383 | def render_pep440_old(pieces): 384 | """TAG[.postDISTANCE[.dev0]] . 385 | 386 | The ".dev0" means dirty. 387 | 388 | Eexceptions: 389 | 1: no tags. 0.postDISTANCE[.dev0] 390 | """ 391 | if pieces["closest-tag"]: 392 | rendered = pieces["closest-tag"] 393 | if pieces["distance"] or pieces["dirty"]: 394 | rendered += ".post%d" % pieces["distance"] 395 | if pieces["dirty"]: 396 | rendered += ".dev0" 397 | else: 398 | # exception #1 399 | rendered = "0.post%d" % pieces["distance"] 400 | if pieces["dirty"]: 401 | rendered += ".dev0" 402 | return rendered 403 | 404 | 405 | def render_git_describe(pieces): 406 | """TAG[-DISTANCE-gHEX][-dirty]. 407 | 408 | Like 'git describe --tags --dirty --always'. 409 | 410 | Exceptions: 411 | 1: no tags. HEX[-dirty] (note: no 'g' prefix) 412 | """ 413 | if pieces["closest-tag"]: 414 | rendered = pieces["closest-tag"] 415 | if pieces["distance"]: 416 | rendered += "-%d-g%s" % (pieces["distance"], pieces["short"]) 417 | else: 418 | # exception #1 419 | rendered = pieces["short"] 420 | if pieces["dirty"]: 421 | rendered += "-dirty" 422 | return rendered 423 | 424 | 425 | def render_git_describe_long(pieces): 426 | """TAG-DISTANCE-gHEX[-dirty]. 427 | 428 | Like 'git describe --tags --dirty --always -long'. 429 | The distance/hash is unconditional. 430 | 431 | Exceptions: 432 | 1: no tags. HEX[-dirty] (note: no 'g' prefix) 433 | """ 434 | if pieces["closest-tag"]: 435 | rendered = pieces["closest-tag"] 436 | rendered += "-%d-g%s" % (pieces["distance"], pieces["short"]) 437 | else: 438 | # exception #1 439 | rendered = pieces["short"] 440 | if pieces["dirty"]: 441 | rendered += "-dirty" 442 | return rendered 443 | 444 | 445 | def render(pieces, style): 446 | """Render the given version pieces into the requested style.""" 447 | if pieces["error"]: 448 | return {"version": "unknown", 449 | "full-revisionid": pieces.get("long"), 450 | "dirty": None, 451 | "error": pieces["error"], 452 | "date": None} 453 | 454 | if not style or style == "default": 455 | style = "pep440" # the default 456 | 457 | if style == "pep440": 458 | rendered = render_pep440(pieces) 459 | elif style == "pep440-pre": 460 | rendered = render_pep440_pre(pieces) 461 | elif style == "pep440-post": 462 | rendered = render_pep440_post(pieces) 463 | elif style == "pep440-old": 464 | rendered = render_pep440_old(pieces) 465 | elif style == "git-describe": 466 | rendered = render_git_describe(pieces) 467 | elif style == "git-describe-long": 468 | rendered = render_git_describe_long(pieces) 469 | else: 470 | raise ValueError("unknown style '%s'" % style) 471 | 472 | return {"version": rendered, "full-revisionid": pieces["long"], 473 | "dirty": pieces["dirty"], "error": None, 474 | "date": pieces.get("date")} 475 | 476 | 477 | def get_versions(): 478 | """Get version information or return default if unable to do so.""" 479 | # I am in _version.py, which lives at ROOT/VERSIONFILE_SOURCE. If we have 480 | # __file__, we can work backwards from there to the root. Some 481 | # py2exe/bbfreeze/non-CPython implementations don't do __file__, in which 482 | # case we can only use expanded keywords. 483 | 484 | cfg = get_config() 485 | verbose = cfg.verbose 486 | 487 | try: 488 | return git_versions_from_keywords(get_keywords(), cfg.tag_prefix, 489 | verbose) 490 | except NotThisMethod: 491 | pass 492 | 493 | try: 494 | root = os.path.realpath(__file__) 495 | # versionfile_source is the relative path from the top of the source 496 | # tree (where the .git directory might live) to this file. Invert 497 | # this to find the root from __file__. 498 | for i in cfg.versionfile_source.split('/'): 499 | root = os.path.dirname(root) 500 | except NameError: 501 | return {"version": "0+unknown", "full-revisionid": None, 502 | "dirty": None, 503 | "error": "unable to find root of source tree", 504 | "date": None} 505 | 506 | try: 507 | pieces = git_pieces_from_vcs(cfg.tag_prefix, root, verbose) 508 | return render(pieces, cfg.style) 509 | except NotThisMethod: 510 | pass 511 | 512 | try: 513 | if cfg.parentdir_prefix: 514 | return versions_from_parentdir(cfg.parentdir_prefix, root, verbose) 515 | except NotThisMethod: 516 | pass 517 | 518 | return {"version": "0+unknown", "full-revisionid": None, 519 | "dirty": None, 520 | "error": "unable to compute version", "date": None} 521 | -------------------------------------------------------------------------------- /src/webdrivermanager/base.py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | 3 | import re 4 | import abc 5 | import sys 6 | import stat 7 | import shutil 8 | import tarfile 9 | import gzip 10 | import zipfile 11 | import platform 12 | import tqdm 13 | import requests 14 | import os 15 | from pathlib import Path 16 | from bs4 import BeautifulSoup 17 | from appdirs import AppDirs 18 | 19 | from .misc import LOGGER, _inside_virtualenv, raise_runtime_error 20 | 21 | 22 | class WebDriverManagerBase: 23 | """Abstract Base Class for the different web driver downloaders""" 24 | 25 | __metaclass__ = abc.ABCMeta 26 | fallback_url = None 27 | driver_filenames = None 28 | 29 | def _get_basepath(self): 30 | if self.os_name in ["mac", "linux"] and os.geteuid() == 0: 31 | return Path(self.dirs.site_data_dir) 32 | if _inside_virtualenv(): 33 | return Path(sys.prefix) / "WebDriverManager" 34 | return Path(self.dirs.user_data_dir) 35 | 36 | def __init__(self, download_root=None, link_path=None, os_name=None, bitness=None): 37 | """ 38 | Initializer for the class. Accepts two optional parameters. 39 | 40 | :param download_root: Path where the web driver binaries will be downloaded. If running as root in macOS or 41 | Linux, the default will be '/usr/local/webdriver', otherwise python appdirs module will 42 | be used to determine appropriate location if no value given. 43 | :param link_path: Path where the link to the web driver binaries will be created. If running as root in macOS 44 | or Linux, the default will be 'usr/local/bin', otherwise appdirs python module will be used 45 | to determine appropriate location if no value give. If set "AUTO", link will be created into 46 | first writeable directory in PATH. If set "SKIP", no link will be created. 47 | """ 48 | 49 | if not bitness: 50 | self.bitness = "64" if sys.maxsize > 2**32 else "32" # noqa: KEK100 51 | else: 52 | self.bitness = bitness 53 | 54 | self.os_name = os_name or self.get_os_name() 55 | self.dirs = AppDirs("WebDriverManager", "rasjani") 56 | base_path = self._get_basepath() 57 | self.download_root = Path(download_root or base_path) 58 | 59 | if link_path in [None, "AUTO"]: 60 | bin_location = "bin" 61 | if _inside_virtualenv(): 62 | if self.os_name == "win" and "CYGWIN" not in platform.system(): 63 | bin_location = "Scripts" 64 | self.link_path = Path(sys.prefix) / bin_location 65 | else: 66 | if self.os_name in ["mac", "linux"] and os.geteuid() == 0: 67 | self.link_path = Path("/usr/local/bin") 68 | else: 69 | dir_in_path = None 70 | if link_path == "AUTO": 71 | dir_in_path = self._find_bin() 72 | self.link_path = dir_in_path or base_path / bin_location 73 | elif link_path == "SKIP": 74 | self.link_path = None 75 | else: 76 | self.link_path = Path(link_path) 77 | 78 | try: 79 | self.download_root.mkdir(parents=True, exist_ok=True) 80 | LOGGER.info("Created download root directory: %s", self.download_root) 81 | except OSError: 82 | pass 83 | 84 | if self.link_path: 85 | try: 86 | self.link_path.mkdir(parents=True, exist_ok=True) 87 | LOGGER.info("Created symlink directory: %s", self.link_path) 88 | except OSError: 89 | pass 90 | 91 | def _find_bin(self): 92 | dirs = os.environ["PATH"].split(os.pathsep) 93 | for directory in dirs: 94 | if os.access(directory, os.W_OK): 95 | return Path(directory) 96 | return None 97 | 98 | def get_os_name(self): 99 | platform_name = platform.system() 100 | namelist = {"Darwin": "mac", "Windows": "win", "Linux": "linux"} 101 | if "CYGWIN" in platform_name: 102 | return "win" 103 | 104 | return namelist[platform_name] 105 | 106 | @abc.abstractmethod 107 | def get_download_path(self, version="latest"): 108 | """ 109 | Method for getting the download path for a web driver binary. 110 | 111 | :param version: String representing the version of the web driver binary to download. For example, "2.38". 112 | Default if no version is specified is "latest". The version string should match the version 113 | as specified on the download page of the webdriver binary. 114 | 115 | :returns: The download path of the web driver binary. 116 | """ 117 | raise NotImplementedError 118 | 119 | @abc.abstractmethod 120 | def get_download_url(self, version="latest"): 121 | """ 122 | Method for getting the download URL for a web driver binary. 123 | 124 | :param version: String representing the version of the web driver binary to download. For example, "2.38". 125 | Default if no version is specified is "latest". The version string should match the version 126 | as specified on the download page of the webdriver binary. 127 | :returns: The download URL for the web driver binary. 128 | """ 129 | raise NotImplementedError 130 | 131 | @abc.abstractmethod 132 | def get_latest_version(self): 133 | raise NotImplementedError 134 | 135 | @abc.abstractmethod 136 | def get_compatible_version(self): 137 | raise NotImplementedError 138 | 139 | def get_driver_filename(self): 140 | return self.driver_filenames[self.os_name] 141 | 142 | def get_mac_cpu_type(self): 143 | # Identify mac CPU type, refer to https://stackoverflow.com/questions/65970469/what-does-platform-system-and-platform-architecture-return-on-apple-m1-silic 144 | return "m1" if platform.processor() == "arm" else "intel" if self.os_name == "mac" else "" 145 | 146 | def _parse_version(self, version): 147 | method = version.strip().lower() 148 | 149 | # Attempt to match webdriver to current browser version, if supported 150 | if method == "compatible": 151 | try: 152 | return self.get_compatible_version() 153 | except NotImplementedError: 154 | pass 155 | except Exception as exc: 156 | LOGGER.info("Failed to parse compatible version: %s", exc) 157 | method = "latest" 158 | 159 | if method == "latest": 160 | return self.get_latest_version() 161 | else: 162 | return version 163 | 164 | def _get_latest_version_with_github_page_fallback(self, url, fallback_url, required_version): 165 | version = None 166 | info = requests.get(f"{url}{required_version}") 167 | 168 | if info.ok: 169 | version = info.json()["tag_name"] 170 | elif info.status_code == 403: 171 | response = requests.get(fallback_url) 172 | tree = BeautifulSoup(response.text, "html.parser") 173 | latest_release = tree.find("div", {"class", "release-header"}).findAll("a")[0] 174 | version = latest_release.text 175 | else: 176 | raise_runtime_error(f"Error attempting to get version info, got status code: {info.status_code}") 177 | 178 | return version # noqa: R504 179 | 180 | def _parse_github_api_response(self, version, response): 181 | filenames = [asset["name"] for asset in response.json()["assets"]] 182 | filename = [name for name in filenames if self.os_name in name] 183 | mac_cpu_type = self.get_mac_cpu_type() 184 | 185 | if not filename: 186 | raise_runtime_error(f"Error, unable to find a download for os: {self.os_name}") 187 | 188 | if len(filename) > 1: 189 | if self.os_name == "mac": 190 | filename = ( 191 | [name for name in filenames if "aarch64" in name] 192 | if mac_cpu_type == "arm" 193 | else [name for name in filenames if "aarch64" not in name] 194 | ) 195 | else: 196 | filename = [name for name in filenames if self.os_name + self.bitness in name and not name.endswith(".asc")] 197 | if len(filename) != 1: 198 | raise_runtime_error(f"Error, unable to determine correct filename for {self.bitness}bit {self.os_name}") 199 | 200 | filename = filename[0] 201 | 202 | url = response.json()["assets"][filenames.index(filename)]["browser_download_url"] 203 | LOGGER.info("Download URL: %s", url) 204 | return url 205 | 206 | def _parse_github_page(self, version): 207 | if version == "latest": 208 | release_url = f"{self.fallback_url}latest" 209 | matcher = r".*\/releases\/download\/.*{}".format(self.os_name) 210 | else: 211 | release_url = f"{self.fallback_url}tag/{version}" 212 | matcher = r".*\/releases\/download\/{}\/.*{}".format(version, self.os_name) 213 | 214 | response = requests.get(release_url) 215 | if response.status_code != 200: 216 | return None 217 | 218 | tree = BeautifulSoup(response.text, "html.parser") 219 | links = tree.find_all("a", href=re.compile(matcher)) 220 | if len(links) == 2: 221 | matcher = f"{matcher}.*{self.bitness}" 222 | links = tree.find_all("a", href=re.compile(matcher)) 223 | 224 | if links: 225 | return f"https://github.com{links[0]['href']}" 226 | 227 | return None 228 | 229 | def download(self, version="latest", show_progress_bar=True, force=False): 230 | """ 231 | Method for downloading a web driver binary. 232 | 233 | :param version: String representing the version of the web driver binary to download. For example, "2.38". 234 | Default if no version is specified is "latest". The version string should match the version 235 | as specified on the download page of the webdriver binary. Prior to downloading, the method 236 | will check the local filesystem to see if the driver has been downloaded already and will 237 | skip downloading if the file is already present locally. 238 | :param show_progress_bar: Boolean (default=install_requires) indicating if a progress bar should be shown in the console. 239 | :returns: The path + filename to the downloaded web driver binary. 240 | """ 241 | (download_url, filename) = self.get_download_url(version) 242 | 243 | dl_path = Path(self.get_download_path(version)) 244 | filename_with_path = dl_path / filename 245 | dl_path.mkdir(parents=True, exist_ok=True) 246 | if filename_with_path.exists(): 247 | if force: 248 | filename_with_path.unlink() 249 | else: 250 | LOGGER.info("Skipping download. File %s already on filesystem.", filename_with_path) 251 | return filename_with_path 252 | 253 | data = requests.get(download_url, stream=True) 254 | if data.status_code == 200: 255 | LOGGER.debug("Starting download of %s to %s", download_url, filename_with_path) 256 | with open(filename_with_path, mode="wb") as fileobj: 257 | chunk_size = 1024 258 | if show_progress_bar: 259 | expected_size = int(data.headers["Content-Length"]) 260 | for chunk in tqdm.tqdm(data.iter_content(chunk_size), total=int(expected_size / chunk_size), unit="kb"): 261 | fileobj.write(chunk) 262 | else: 263 | for chunk in data.iter_content(chunk_size): 264 | fileobj.write(chunk) 265 | LOGGER.debug("Finished downloading %s to %s", download_url, filename_with_path) 266 | return filename_with_path 267 | 268 | raise_runtime_error(f"Error downloading file {filename}, got status code: {data.status_code}") 269 | return None 270 | 271 | @staticmethod 272 | def _generate_archive_details(dl_path, filename): 273 | if filename.lower().endswith(".tar.gz"): 274 | return (dl_path / filename[:-7], 1) 275 | elif filename.lower().endswith(".zip"): 276 | return (dl_path / filename[:-4], 2) 277 | elif filename.lower().endswith(".exe"): 278 | return (dl_path / filename[:-4], 3) 279 | else: 280 | raise_runtime_error(f"Unknown archive format: {filename}") 281 | 282 | def download_and_install(self, version="latest", show_progress_bar=True): 283 | """ 284 | Method for downloading a web driver binary, extracting it into the download directory and creating a symlink 285 | to the binary in the link directory. 286 | 287 | :param version: String representing the version of the web driver binary to download. For example, "2.38". 288 | Default if no version is specified is "latest". The version string should match the version 289 | as specified on the download page of the webdriver binary. 290 | :param show_progress_bar: Boolean (default=install_requires) indicating if a progress bar should be shown in 291 | the console. 292 | :returns: Tuple containing the path + filename to [0] the extracted binary, and [1] the symlink to the 293 | extracted binary. 294 | """ 295 | archive_type = 0 296 | actual_driver_filename = None 297 | 298 | driver_filename = self.get_driver_filename() 299 | if not driver_filename: 300 | raise_runtime_error(f"Error, unable to find appropriate drivername for {self.os_name}.") 301 | 302 | force = False 303 | for _ in range(0, 2): 304 | filename_with_path = self.download(version, show_progress_bar=show_progress_bar, force=force) 305 | filename = filename_with_path.name 306 | dl_path = Path(self.get_download_path(version)) 307 | 308 | (extract_dir, archive_type) = self._generate_archive_details(dl_path, filename) 309 | 310 | if not extract_dir.exists(): 311 | extract_dir.mkdir(parents=True, exist_ok=True) 312 | LOGGER.debug("Created directory: %s", extract_dir) 313 | 314 | try: 315 | archive_file = dl_path / filename 316 | if archive_type == 1: 317 | with tarfile.open(archive_file, mode="r:*") as tar: 318 | tar.extractall(extract_dir) 319 | LOGGER.debug("Extracted files: %s", ", ".join(tar.getnames())) 320 | elif archive_type == 2: 321 | with zipfile.ZipFile(archive_file, mode="r") as driver_zipfile: 322 | driver_zipfile.extractall(extract_dir) 323 | # TODO: Get filenames and log debug 324 | elif archive_type == 3: 325 | shutil.copy2(archive_file, extract_dir / filename) 326 | except (gzip.BadGzipFile, tarfile.TarError, zipfile.BadZipFile): 327 | force = True 328 | LOGGER.debug(f"Downloaded archive {archive_file} seems to be corrupted - redownloading") 329 | continue 330 | except Exception as e: 331 | raise_runtime_error( 332 | f"Unrecoverable error extracting {archive_file}. Try to remove the file and re-download.\n{e}", 333 | ) 334 | break 335 | 336 | # TODO: Clean up 337 | for root, _, files in os.walk(extract_dir): 338 | for curr_file in files: 339 | if curr_file in driver_filename: 340 | actual_driver_filename = Path(root) / curr_file 341 | break 342 | 343 | if not actual_driver_filename: 344 | LOGGER.warning("Cannot locate binary %s from the archive", driver_filename) 345 | return None 346 | 347 | if not self.link_path: 348 | return (actual_driver_filename, None) 349 | 350 | if self.os_name in ["mac", "linux"]: 351 | symlink_src = actual_driver_filename 352 | symlink_target = self.link_path / driver_filename 353 | if symlink_target.is_symlink() or symlink_target.exists(): 354 | if symlink_src.samefile(symlink_target): 355 | LOGGER.info("Symlink already exists: %s -> %s", symlink_target, symlink_src) 356 | return (symlink_src, symlink_target) 357 | 358 | LOGGER.warning("Symlink target %s already exists and will be overwritten.", symlink_target) 359 | os.unlink(symlink_target) 360 | 361 | symlink_target.symlink_to(symlink_src) 362 | LOGGER.info("Created symlink: %s -> %s", symlink_target, symlink_src) 363 | try: 364 | symlink_stat = os.stat(symlink_src) 365 | os.chmod(symlink_src, symlink_stat.st_mode | stat.S_IXUSR | stat.S_IXGRP | stat.S_IXOTH) 366 | except Exception as e: 367 | LOGGER.warning(f"Unable to change permissions of {symlink_src}.\n{e}") 368 | return (symlink_src, symlink_target) 369 | 370 | # self.os_name == 'win': 371 | src_file = actual_driver_filename 372 | dest_file = self.link_path / actual_driver_filename.name 373 | 374 | try: 375 | if dest_file.is_file(): 376 | LOGGER.info("File %s already exists and will be overwritten.", dest_file) 377 | except OSError: 378 | pass 379 | shutil.copy2(src_file, dest_file) 380 | try: 381 | dest_stat = os.stat(dest_file) 382 | os.chmod(dest_file, dest_stat.st_mode | stat.S_IXUSR | stat.S_IXGRP | stat.S_IXOTH) 383 | except Exception as e: 384 | LOGGER.warning(f"Unable to change permissions of {dest_file}.\n{e}") 385 | return (src_file, dest_file) 386 | -------------------------------------------------------------------------------- /src/webdrivermanager/chrome.py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | import requests 3 | import re 4 | from pathlib import Path 5 | from .base import WebDriverManagerBase 6 | from .misc import LOGGER, raise_runtime_error, get_output 7 | 8 | 9 | class ChromeDriverManager(WebDriverManagerBase): 10 | """Class for downloading the Google Chrome WebDriver.""" 11 | 12 | chrome_driver_base_url = "https://www.googleapis.com/storage/v1/b/chromedriver" 13 | 14 | driver_filenames = { 15 | "win": "chromedriver.exe", 16 | "mac": "chromedriver", 17 | "linux": "chromedriver", 18 | } 19 | 20 | chrome_version_pattern = r"(\d+\.\d+.\d+)(\.\d+)" 21 | chrome_version_commands = { 22 | "win": [ 23 | ["reg", "query", r"HKEY_CURRENT_USER\Software\Google\Chrome\BLBeacon", "/v", "version"], 24 | ["reg", "query", r"HKEY_CURRENT_USER\Software\Chromium\BLBeacon", "/v", "version"], 25 | ], 26 | "linux": [ 27 | ["chromium", "--version"], 28 | ["chromium-browser", "--version"], 29 | ["google-chrome", "--version"], 30 | ], 31 | "mac": [ 32 | ["/Applications/Google Chrome.app/Contents/MacOS/Google Chrome", "--version"], 33 | ["/Applications/Chromium.app/Contents/MacOS/Chromium", "--version"], 34 | ], 35 | } 36 | 37 | def get_download_path(self, version="latest"): 38 | version = self._parse_version(version) 39 | return self.download_root / "chrome" / version 40 | 41 | def get_download_url(self, version="latest"): 42 | """ 43 | Method for getting the download URL for the Google Chome driver binary. 44 | 45 | :param version: String representing the version of the web driver binary to download. For example, "2.39". 46 | Default if no version is specified is "latest". The version string should match the version 47 | as specified on the download page of the webdriver binary. 48 | :returns: The download URL for the Google Chrome driver binary. 49 | """ 50 | version = self._parse_version(version) 51 | LOGGER.debug("Detected OS: %sbit %s", self.bitness, self.os_name) 52 | 53 | chrome_driver_objects = requests.get(self.chrome_driver_base_url + "/o").json() 54 | # chromedriver only has 64 bit versions of mac and 32bit versions of windows. For now. 55 | if self.os_name == "win": 56 | local_bitness = "32" 57 | elif self.os_name == "mac": 58 | local_bitness = "64" 59 | else: 60 | local_bitness = self.bitness 61 | 62 | matcher = r"{0}/.*{1}{2}.*".format(version, self.os_name, local_bitness) 63 | 64 | entry = [obj for obj in chrome_driver_objects["items"] if re.match(matcher, obj["name"])] 65 | if not entry: 66 | raise_runtime_error(f"Error, unable to find appropriate download for {self.os_name}{self.bitness}.") 67 | 68 | url = entry[0]["mediaLink"] 69 | filename = Path(entry[0]["name"]).name 70 | return (url, filename) 71 | 72 | def get_latest_version(self): 73 | resp = requests.get(self.chrome_driver_base_url + "/o/LATEST_RELEASE") 74 | if resp.status_code != 200: 75 | raise_runtime_error(f"Error, unable to get version number for latest release, got code: {resp.status_code}") 76 | 77 | latest_release = requests.get(resp.json()["mediaLink"]) 78 | return latest_release.text 79 | 80 | def get_compatible_version(self): 81 | browser_version = self._get_browser_version() 82 | resp = requests.get(self.chrome_driver_base_url + "/o/LATEST_RELEASE_" + browser_version) 83 | 84 | if resp.status_code != 200: 85 | raise_runtime_error( 86 | f"Error, unable to get version number for release {browser_version}, got code: {resp.status_code}" # NOQA: C812 87 | ) 88 | 89 | latest_release = requests.get(resp.json()["mediaLink"]) 90 | return latest_release.text 91 | 92 | def _get_browser_version(self): 93 | commands = self.chrome_version_commands.get(self.os_name) 94 | if not commands: 95 | raise NotImplementedError("Unsupported system: %s", self.os_name) 96 | 97 | for cmd in commands: 98 | output = get_output(cmd) 99 | if not output: 100 | continue 101 | 102 | version = re.search(self.chrome_version_pattern, output) 103 | if not version: 104 | continue 105 | 106 | return version.group(1) 107 | 108 | raise RuntimeError("Unable to read current browser version") 109 | -------------------------------------------------------------------------------- /src/webdrivermanager/edge.py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | import requests 3 | import re 4 | import os 5 | from urllib.parse import urlparse 6 | from bs4 import BeautifulSoup 7 | from .base import WebDriverManagerBase 8 | from .misc import LOGGER, raise_runtime_error 9 | 10 | 11 | class EdgeDriverManager(WebDriverManagerBase): 12 | """Class for downloading the Edge WebDriver.""" 13 | 14 | driver_filenames = { 15 | "win": ["MicrosoftWebDriver.exe", "msedgedriver.exe"], 16 | "mac": None, 17 | "linux": None, 18 | } 19 | 20 | edge_driver_base_url = "https://developer.microsoft.com/en-us/microsoft-edge/tools/webdriver/" 21 | 22 | def get_download_path(self, version="latest"): 23 | version = self._parse_version(version) 24 | return self.download_root / "edge" / version 25 | 26 | def get_download_url(self, version="latest"): 27 | """ 28 | Method for getting the download URL for the MSEdge WebDriver binary. 29 | 30 | :param version: String representing the version of the web driver binary to download. For example, "2.39". 31 | Default if no version is specified is "latest". The version string should match the version 32 | as specified on the download page of the webdriver binary. 33 | :returns: The download URL for the Google Chrome driver binary. 34 | """ 35 | version = self._parse_version(version) 36 | LOGGER.debug("Detected OS: %sbit %s", self.bitness, self.os_name) 37 | 38 | # TODO: handle error 500 by sleep & retry here 39 | resp = requests.get(self.edge_driver_base_url) 40 | if resp.status_code != 200: 41 | raise_runtime_error(f"Error, unable to get version number for latest release, got code: {resp.status_code}") 42 | 43 | url = self._get_download_url(resp, version) 44 | return (url, os.path.split(urlparse(url).path)[1]) 45 | 46 | def get_latest_version(self): 47 | # TODO: handle error 500 by sleep & retry here 48 | resp = requests.get(self.edge_driver_base_url) 49 | if resp.status_code != 200: 50 | raise_runtime_error(f"Error, unable to get version number for latest release, got code: {resp.status_code}") 51 | 52 | return self._get_version_number(resp) 53 | 54 | def get_compatible_version(self): 55 | raise NotImplementedError 56 | 57 | def _get_download_url(self, body, version): 58 | try: 59 | tree = BeautifulSoup(body.text, "html.parser") 60 | mstr = f"Release {version}" 61 | link_texts = tree.find_all("a", string=re.compile(mstr)) 62 | if "index.html" in link_texts[0]["href"]: 63 | local_bitness = self.bitness 64 | if local_bitness == "32": 65 | local_bitness = "86" 66 | mstr = f"WebDriver for release number {version} x{local_bitness}" 67 | link_texts = tree.find_all("a", {"aria-label": re.compile(mstr)}) 68 | return link_texts[0]["href"] 69 | except Exception: 70 | return None 71 | 72 | def _get_version_number(self, body): 73 | try: 74 | tree = BeautifulSoup(body.text, "html.parser") 75 | link_texts = tree.find_all("a", string=re.compile("Release ")) 76 | results = re.findall(r"\"WebDriver for release number ([\d\.]+)\"", str(link_texts[0])) 77 | if bool(results and results[0]): 78 | return results[0] 79 | 80 | return None 81 | except Exception: 82 | return None 83 | -------------------------------------------------------------------------------- /src/webdrivermanager/edgechromium.py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | import requests 3 | import re 4 | from bs4 import BeautifulSoup 5 | from pathlib import Path 6 | from .base import WebDriverManagerBase 7 | from .misc import LOGGER, raise_runtime_error, versiontuple 8 | 9 | 10 | class EdgeChromiumDriverManager(WebDriverManagerBase): 11 | """Class for downloading Edge Chromium WebDriver.""" 12 | 13 | edgechromium_driver_base_url = ( 14 | "https://msedgewebdriverstorage.blob.core.windows.net/edgewebdriver?maxresults=1000&comp=list&timeout=60000" 15 | ) 16 | _drivers = None 17 | _versions = None 18 | driver_filenames = { 19 | "win": "msedgedriver.exe", 20 | "mac": "msedgedriver", 21 | "linux": "msedgedriver", 22 | } 23 | 24 | def get_download_path(self, version="latest"): 25 | version = self._parse_version(version) 26 | return self.download_root / "edgechromium" / version 27 | 28 | def get_download_url(self, version="latest"): 29 | """ 30 | Method for getting the download URL for the Google Chome driver binary. 31 | 32 | :param version: String representing the version of the web driver binary to download. For example, "2.39". 33 | Default if no version is specified is "latest". The version string should match the version 34 | as specified on the download page of the webdriver binary. 35 | :returns: The download URL for the Internet Explorer driver binary. 36 | """ 37 | version = self._parse_version(version) 38 | 39 | if not self._drivers: 40 | self._populate_cache(self.edgechromium_driver_base_url) 41 | 42 | LOGGER.debug("Detected OS: %sbit %s", self.bitness, self.os_name) 43 | local_osname = self.os_name 44 | matcher = r".*/{0}/edgedriver_{1}{2}".format(version, local_osname, self.bitness) 45 | entry = [entry for entry in self._drivers if re.match(matcher, entry)] 46 | if not entry: 47 | raise_runtime_error(f"Error, unable to find appropriate download for {self.os_name}{self.bitness}.") 48 | 49 | url = entry[0] 50 | filename = Path(entry[0]).name 51 | return (url, filename) 52 | 53 | def get_latest_version(self): 54 | if self._drivers is None or self._versions is None: 55 | self._populate_cache(self.edgechromium_driver_base_url) 56 | return ".".join(map(str, max(self._versions))) 57 | 58 | def get_compatible_version(self): 59 | raise NotImplementedError 60 | 61 | def _extract_ver(self, s): 62 | matcher = r".*\/edgewebdriver\/([\d.]+)\/edgedriver_.*\.zip" 63 | ret = re.match(matcher, s) 64 | return ret.group(1) 65 | 66 | def _populate_cache(self, url): 67 | urls = [] 68 | at_the_end = False 69 | pagination = "" 70 | while not at_the_end: 71 | local_url = f"{url}{pagination}" 72 | print(f"URL {local_url}") 73 | resp = requests.get(local_url) 74 | if resp.status_code != 200: 75 | raise_runtime_error(f"Error, unable to get version number for latest release, got code: {resp.status_code}") 76 | 77 | soup = BeautifulSoup(resp.text, "lxml") 78 | urls.extend(soup.find_all("url")) 79 | 80 | next_marker = soup.find("nextmarker").text 81 | if next_marker: 82 | pagination = f"&marker={next_marker}" 83 | else: 84 | at_the_end = True 85 | 86 | arch_matcher = f"{self.os_name}{self.bitness}" 87 | drivers = filter(lambda entry: f"edgedriver_{arch_matcher}" in entry.contents[0], urls) 88 | self._drivers = list(map(lambda entry: entry.contents[0], drivers)) 89 | self._versions = set(map(lambda entry: versiontuple(self._extract_ver(entry)), self._drivers)) 90 | -------------------------------------------------------------------------------- /src/webdrivermanager/gecko.py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | import requests 3 | import os 4 | import re 5 | from urllib.parse import urlparse 6 | from .base import WebDriverManagerBase 7 | from .misc import LOGGER, raise_runtime_error, get_output 8 | 9 | 10 | class GeckoDriverManager(WebDriverManagerBase): 11 | """Class for downloading the Gecko (Mozilla Firefox) WebDriver.""" 12 | 13 | gecko_driver_releases_url = "https://api.github.com/repos/mozilla/geckodriver/releases/" 14 | fallback_url = "https://github.com/mozilla/geckodriver/releases/" 15 | 16 | driver_filenames = { 17 | "win": "geckodriver.exe", 18 | "mac": "geckodriver", 19 | "linux": "geckodriver", 20 | } 21 | 22 | firefox_version_pattern = r"(\d+)(\.\d+)" 23 | firefox_version_commands = { 24 | "win": [ 25 | ["reg", "query", r"HKEY_LOCAL_MACHINE\Software\Mozilla\Mozilla Firefox", "/v", "CurrentVersion"], 26 | ["reg", "query", r"HKEY_CURRENT_USER\Software\Mozilla\Mozilla Firefox", "/v", "CurrentVersion"], 27 | ], 28 | "linux": [ 29 | ["firefox", "--version", "--headless"], 30 | ], 31 | "mac": [ 32 | ["/Applications/Firefox.app/Contents/MacOS/firefox-bin", "--version", "--headless"], 33 | ], 34 | } 35 | 36 | def get_download_path(self, version="latest"): 37 | version = self._parse_version(version) 38 | return self.download_root / "gecko" / version 39 | 40 | def get_download_url(self, version="latest"): 41 | """ 42 | Method for getting the download URL for the Gecko (Mozilla Firefox) driver binary. 43 | 44 | :param version: String representing the version of the web driver binary to download. For example, "v0.20.1". 45 | Default if no version is specified is "latest". The version string should match the version 46 | as specified on the download page of the webdriver binary. 47 | :returns: The download URL for the Gecko (Mozilla Firefox) driver binary. 48 | """ 49 | version = self._parse_version(version) 50 | releases_url = f"{self.gecko_driver_releases_url}tags/{version}" 51 | 52 | LOGGER.debug("Attempting to access URL: %s", releases_url) 53 | response = requests.get(releases_url) 54 | if response.ok: 55 | url = self._parse_github_api_response(version, response) 56 | elif response.status_code == 403: 57 | url = self._parse_github_page(version) 58 | else: 59 | raise_runtime_error( 60 | f"Error, unable to get info for gecko driver {version} release. Status code: {response.status_code}. Error message: {response.text}" # NOQA: C812 61 | ) 62 | 63 | return (url, os.path.split(urlparse(url).path)[1]) 64 | 65 | def get_latest_version(self): 66 | return self._get_latest_version_with_github_page_fallback(self.gecko_driver_releases_url, self.fallback_url, "latest") 67 | 68 | def get_compatible_version(self): 69 | # Map browser version to webdriver version 70 | # https://firefox-source-docs.mozilla.org/testing/geckodriver/Support.html 71 | browser_version = self._get_browser_version() 72 | version_map = [(60, "v0.29.0"), (57, "v0.25.0"), (55, "v0.20.1"), (53, "v0.18.0"), (52, "v0.17.0")] 73 | 74 | for browser_minimum, driver_version in version_map: 75 | if browser_version >= browser_minimum: 76 | return driver_version 77 | 78 | raise_runtime_error(f"Unsupported Firefox version: {browser_version}") 79 | 80 | def _get_browser_version(self): 81 | commands = self.firefox_version_commands.get(self.os_name) 82 | if not commands: 83 | raise NotImplementedError("Unsupported system: %s", self.os_name) 84 | 85 | for cmd in commands: 86 | output = get_output(cmd) 87 | if not output: 88 | continue 89 | 90 | version = re.search(self.firefox_version_pattern, output) 91 | if not version: 92 | continue 93 | 94 | return int(version.group(1)) 95 | 96 | raise_runtime_error("Error, browser version does not match known pattern") 97 | -------------------------------------------------------------------------------- /src/webdrivermanager/ie.py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | import requests 3 | import re 4 | from pathlib import Path 5 | from bs4 import BeautifulSoup 6 | from .base import WebDriverManagerBase 7 | from .misc import LOGGER, raise_runtime_error, versiontuple 8 | 9 | 10 | class IEDriverManager(WebDriverManagerBase): 11 | """Class for downloading Internet Explorer WebDriver.""" 12 | 13 | ie_driver_base_url = "https://selenium-release.storage.googleapis.com" 14 | _drivers = None 15 | _versions = None 16 | 17 | driver_filenames = { 18 | "win": "IEDriverServer.exe", 19 | "mac": None, 20 | "linux": None, 21 | } 22 | 23 | def get_download_path(self, version="latest"): 24 | version = self._parse_version(version) 25 | return self.download_root / "ie" / version 26 | 27 | def get_download_url(self, version="latest"): 28 | """ 29 | Method for getting the download URL for the Google Chome driver binary. 30 | 31 | :param version: String representing the version of the web driver binary to download. For example, "2.39". 32 | Default if no version is specified is "latest". The version string should match the version 33 | as specified on the download page of the webdriver binary. 34 | :returns: The download URL for the Internet Explorer driver binary. 35 | """ 36 | version = self._parse_version(version) 37 | 38 | if not self._drivers: 39 | self._populate_cache(self.ie_driver_base_url) 40 | 41 | LOGGER.debug("Detected OS: %sbit %s", self.bitness, self.os_name) 42 | local_osname = self.os_name 43 | if self.bitness == "64": 44 | local_osname = "x" 45 | elif self.bitness == "32": 46 | local_osname = "Win" 47 | matcher = r".*/.*_{0}{1}_{2}".format(local_osname, self.bitness, version) 48 | entry = [entry for entry in self._drivers if re.match(matcher, entry)] 49 | 50 | if not entry: 51 | raise_runtime_error(f"Error, unable to find appropriate download for {self.os_name}{self.bitness}.") 52 | 53 | url = f"{self.ie_driver_base_url}/{entry[0]}" 54 | filename = Path(entry[0]).name 55 | return (url, filename) 56 | 57 | def get_latest_version(self): 58 | if self._drivers is None or self._versions is None: 59 | self._populate_cache(self.ie_driver_base_url) 60 | return ".".join(map(str, max(self._versions))) 61 | 62 | def get_compatible_version(self): 63 | raise NotImplementedError 64 | 65 | def _extract_ver(self, s): 66 | matcher = r".*\/IEDriverServer_(x64|Win32)_(\d+\.\d+\.\d+)\.zip" 67 | ret = re.match(matcher, s) 68 | return ret.group(2) 69 | 70 | def _populate_cache(self, url): 71 | resp = requests.get(url) 72 | if resp.status_code != 200: 73 | raise_runtime_error(f"Error, unable to get version number for latest release, got code: {resp.status_code}") 74 | 75 | soup = BeautifulSoup(resp.text, "lxml") 76 | drivers = filter(lambda entry: "IEDriverServer_" in entry.contents[0], soup.find_all("key")) 77 | self._drivers = list(map(lambda entry: entry.contents[0], drivers)) 78 | self._versions = set(map(lambda entry: versiontuple(self._extract_ver(entry)), self._drivers)) 79 | -------------------------------------------------------------------------------- /src/webdrivermanager/misc.py: -------------------------------------------------------------------------------- 1 | import logging 2 | import subprocess 3 | import sys 4 | 5 | LOGGER = logging.getLogger(__name__) 6 | LOG_LEVELS = { 7 | "notset": logging.NOTSET, 8 | "debug": logging.DEBUG, 9 | "info": logging.INFO, 10 | "warning": logging.WARNING, 11 | "error": logging.ERROR, 12 | "critical": logging.CRITICAL, 13 | } 14 | 15 | 16 | def _inside_virtualenv(): 17 | return hasattr(sys, "real_prefix") or hasattr(sys, "base_prefix") and sys.base_prefix != sys.prefix 18 | 19 | 20 | def raise_runtime_error(msg): 21 | LOGGER.error(msg) 22 | raise RuntimeError(msg) 23 | 24 | 25 | def versiontuple(v): 26 | return tuple(map(int, (v.split(".")))) 27 | 28 | 29 | def get_output(cmd, **kwargs): 30 | try: 31 | output = subprocess.check_output(cmd, **kwargs, stderr=subprocess.STDOUT) 32 | return output.decode().strip() 33 | except subprocess.CalledProcessError as err: 34 | error = err.output.decode().strip() 35 | LOGGER.debug("Command failed:\n%s", error) 36 | return None 37 | except FileNotFoundError as err: 38 | LOGGER.debug("Command not found: %s", err) 39 | return None 40 | -------------------------------------------------------------------------------- /tasks.py: -------------------------------------------------------------------------------- 1 | # flake8: noqa 2 | from pathlib import Path 3 | from invoke import task 4 | from pathlib import Path 5 | import os 6 | import shutil 7 | 8 | 9 | QUOTE = '"' if os.name == "nt" else "'" 10 | 11 | CHANGELOG = "CHANGELOG" 12 | filters = ["poc", "new release", "wip", "cleanup", "!nocl"] 13 | 14 | 15 | def filter_entries(filename): 16 | buffer = [] 17 | with open(filename) as old_file: 18 | buffer = old_file.read().split("\n") 19 | 20 | with open(filename, "w") as new_file: 21 | for line in buffer: 22 | if not any(bad_word in line.lower() for bad_word in filters): 23 | new_file.write(line + "\n") 24 | 25 | 26 | assert Path.cwd() == Path(__file__).parent 27 | 28 | 29 | @task() 30 | def build(ctx): 31 | """Generates dist tar ball""" 32 | ctx.run("python setup.py sdist") 33 | 34 | 35 | @task 36 | def flake(ctx): 37 | """Runs flake8 against whole project""" 38 | ctx.run("flake8") 39 | 40 | 41 | @task 42 | def mypy(ctx): 43 | """Runs mypy against the codebase""" 44 | ctx.run("mypy --config mypy.ini") 45 | 46 | 47 | @task 48 | def black(ctx): 49 | """Reformat code with black""" 50 | ctx.run("black -l130 -tpy37 src") 51 | 52 | 53 | @task 54 | def clean(ctx): 55 | to_be_removed = [ 56 | "report", 57 | "dist/", 58 | ".coverage*", 59 | "output*", 60 | ] 61 | 62 | for item in to_be_removed: 63 | fs_entry = Path(item) 64 | if fs_entry.is_dir: 65 | shutil.rmtree(item) 66 | elif fs_entry.is_file(): 67 | fs_entry.unlink() 68 | else: 69 | for fs_entry in Path().glob(item): 70 | fs_entry.unlink() 71 | 72 | 73 | @task 74 | def changelog(ctx, version=None): 75 | if version is not None: 76 | version = f"-c {version}" 77 | else: 78 | version = "" 79 | ctx.run(f"gcg -x -o {CHANGELOG} -O rpm {version}") 80 | filter_entries(CHANGELOG) 81 | 82 | 83 | @task 84 | def release(ctx, version=None): 85 | assert version != None 86 | changelog(ctx, version) 87 | ctx.run(f"git add {CHANGELOG}") 88 | ctx.run(f"git commit -m {QUOTE}New Release {version}{QUOTE}") 89 | ctx.run(f"git tag {version}") 90 | build(ctx) 91 | -------------------------------------------------------------------------------- /test/__init__.py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MarketSquare/webdrivermanager/0eade159ddffadd1d4221d3537263deabb9b55c0/test/__init__.py -------------------------------------------------------------------------------- /test/acceptance/__init__.py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MarketSquare/webdrivermanager/0eade159ddffadd1d4221d3537263deabb9b55c0/test/acceptance/__init__.py -------------------------------------------------------------------------------- /test/acceptance/test_base.py: -------------------------------------------------------------------------------- 1 | # pylint: disable=attribute-defined-outside-init 2 | import sys 3 | 4 | from .tools import SRC_ROOT, ExplicitBaseTest 5 | 6 | sys.path.append(SRC_ROOT) 7 | import webdrivermanager # noqa: E402 I001 8 | 9 | 10 | class GeneralWebDriverManagerTests(ExplicitBaseTest): 11 | DRIVER_MANAGER = webdrivermanager.WebDriverManagerBase 12 | 13 | def test_available_drivers(self): 14 | self.assertIsInstance(webdrivermanager.AVAILABLE_DRIVERS, dict, "available_drivers doesnt seem to be exported correctly") 15 | self.assertIsNotNone(webdrivermanager.AVAILABLE_DRIVERS, "No exported drivers found") 16 | self.assertGreater(len(webdrivermanager.AVAILABLE_DRIVERS), 1, "Not enough drivers found") 17 | -------------------------------------------------------------------------------- /test/acceptance/test_chrome.py: -------------------------------------------------------------------------------- 1 | import sys 2 | from flaky import flaky 3 | 4 | from .tools import SRC_ROOT, AutomaticBaseTest, ExplicitBaseTest, NO_FILE, NO_LINK_FILE 5 | 6 | sys.path.append(SRC_ROOT) 7 | import webdrivermanager # noqa: E402 I001 8 | 9 | 10 | class ChromeDriverManagerTestsWithAutomaticLocations(AutomaticBaseTest): 11 | DRIVER_MANAGER = webdrivermanager.ChromeDriverManager 12 | 13 | @flaky 14 | def test_download(self): 15 | self.instance = self.DRIVER_MANAGER() 16 | filename = self.instance.download(show_progress_bar=False) 17 | self.assertTrue(filename.is_file(), NO_FILE) 18 | 19 | @flaky 20 | def test_download_and_install(self): 21 | self.instance = self.DRIVER_MANAGER() 22 | driver_link_target, driver_binary = self.instance.download_and_install(show_progress_bar=False) 23 | self.assertTrue(driver_binary.is_file(), NO_FILE) 24 | self.assertTrue(driver_link_target.is_file(), NO_LINK_FILE) 25 | 26 | 27 | class ChromeDriverManagerTestsWithExplicitLocations(ExplicitBaseTest): 28 | DRIVER_MANAGER = webdrivermanager.ChromeDriverManager 29 | 30 | @flaky 31 | def test_download(self): 32 | self.instance = self.DRIVER_MANAGER(download_root=self.temp_dir.name) 33 | filename = self.instance.download(show_progress_bar=False) 34 | self.assertTrue(filename.is_file(), NO_FILE) 35 | 36 | @flaky 37 | def test_download_and_install(self): 38 | link_path = self.make_link_dir() 39 | self.instance = self.DRIVER_MANAGER(download_root=self.temp_dir.name, link_path=link_path) 40 | driver_link_target, driver_binary = self.instance.download_and_install(show_progress_bar=False) 41 | self.assertTrue(driver_binary.is_file(), NO_FILE) 42 | self.assertTrue(driver_link_target.is_file(), NO_LINK_FILE) 43 | -------------------------------------------------------------------------------- /test/acceptance/test_edge.py: -------------------------------------------------------------------------------- 1 | import sys 2 | from flaky import flaky 3 | 4 | from .tools import SRC_ROOT, AutomaticBaseTest, ExplicitBaseTest, NO_FILE, NO_LINK_FILE 5 | 6 | sys.path.append(SRC_ROOT) 7 | import webdrivermanager # noqa: E402 I001 8 | 9 | 10 | class EdgeDriverManagerTestsWithAutomaticLocations(AutomaticBaseTest): 11 | DRIVER_MANAGER = webdrivermanager.EdgeDriverManager 12 | 13 | @flaky 14 | def test_download(self): 15 | self.instance = self.DRIVER_MANAGER(os_name="win") 16 | filename = self.instance.download(show_progress_bar=False) 17 | self.assertTrue(filename.is_file(), NO_FILE) 18 | 19 | @flaky 20 | def test_download_and_install(self): 21 | self.instance = self.DRIVER_MANAGER(os_name="win") 22 | driver_link_target, driver_binary = self.instance.download_and_install(show_progress_bar=False) 23 | self.assertTrue(driver_binary.is_file(), NO_FILE) 24 | self.assertTrue(driver_link_target.is_file(), NO_LINK_FILE) 25 | 26 | 27 | class EdgeDriverManagerTestsWithExplicitLocations(ExplicitBaseTest): 28 | DRIVER_MANAGER = webdrivermanager.EdgeDriverManager 29 | 30 | @flaky 31 | def test_download(self): 32 | self.instance = self.DRIVER_MANAGER(download_root=self.temp_dir.name, os_name="win") 33 | filename = self.instance.download(show_progress_bar=False) 34 | self.assertTrue(filename.is_file(), NO_FILE) 35 | 36 | @flaky 37 | def test_download_and_install(self): 38 | link_path = self.make_link_dir() 39 | self.instance = self.DRIVER_MANAGER(download_root=self.temp_dir.name, link_path=link_path, os_name="win") 40 | driver_link_target, driver_binary = self.instance.download_and_install(show_progress_bar=False) 41 | self.assertTrue(driver_binary.is_file(), NO_FILE) 42 | self.assertTrue(driver_link_target.is_file(), NO_LINK_FILE) 43 | -------------------------------------------------------------------------------- /test/acceptance/test_edgechromium.py: -------------------------------------------------------------------------------- 1 | import sys 2 | from flaky import flaky 3 | from .tools import SRC_ROOT, AutomaticBaseTest, ExplicitBaseTest, NO_FILE, NO_LINK_FILE 4 | 5 | sys.path.append(SRC_ROOT) 6 | import webdrivermanager # noqa: E402 I001 7 | 8 | 9 | class EdgeChromiumDriverManagerTestsWithAutomaticLocations(AutomaticBaseTest): 10 | DRIVER_MANAGER = webdrivermanager.EdgeChromiumDriverManager 11 | 12 | @flaky 13 | def test_download(self): 14 | self.instance = self.DRIVER_MANAGER() 15 | filename = self.instance.download(show_progress_bar=False) 16 | self.assertTrue(filename.is_file(), NO_FILE) 17 | 18 | @flaky 19 | def test_download_and_install(self): 20 | self.instance = self.DRIVER_MANAGER() 21 | driver_link_target, driver_binary = self.instance.download_and_install(show_progress_bar=False) 22 | self.assertTrue(driver_binary.is_file(), NO_FILE) 23 | self.assertTrue(driver_link_target.is_file(), NO_LINK_FILE) 24 | 25 | 26 | class EdgeChromiumDriverManagerTestsWithExplicitLocations(ExplicitBaseTest): 27 | DRIVER_MANAGER = webdrivermanager.EdgeChromiumDriverManager 28 | 29 | @flaky 30 | def test_download(self): 31 | self.instance = self.DRIVER_MANAGER(download_root=self.temp_dir.name) 32 | filename = self.instance.download(show_progress_bar=False) 33 | self.assertTrue(filename.is_file(), NO_FILE) 34 | 35 | @flaky 36 | def test_download_and_install(self): 37 | link_path = self.make_link_dir() 38 | self.instance = self.DRIVER_MANAGER(download_root=self.temp_dir.name, link_path=link_path) 39 | driver_link_target, driver_binary = self.instance.download_and_install(show_progress_bar=False) 40 | self.assertTrue(driver_binary.is_file(), NO_FILE) 41 | self.assertTrue(driver_link_target.is_file(), NO_LINK_FILE) 42 | -------------------------------------------------------------------------------- /test/acceptance/test_gecko.py: -------------------------------------------------------------------------------- 1 | import sys 2 | from flaky import flaky 3 | 4 | from .tools import SRC_ROOT, AutomaticBaseTest, ExplicitBaseTest, NO_FILE, NO_LINK_FILE 5 | 6 | sys.path.append(SRC_ROOT) 7 | import webdrivermanager # noqa: E402 I001 8 | 9 | 10 | class GeckoDriverManagerTestsWithAutomaticLocations(AutomaticBaseTest): 11 | DRIVER_MANAGER = webdrivermanager.GeckoDriverManager 12 | 13 | @flaky 14 | def test_download(self): 15 | self.instance = self.DRIVER_MANAGER() 16 | filename = self.instance.download(show_progress_bar=False) 17 | self.assertTrue(filename.is_file(), NO_FILE) 18 | 19 | @flaky 20 | def test_download_and_install(self): 21 | self.instance = self.DRIVER_MANAGER() 22 | driver_link_target, driver_binary = self.instance.download_and_install(show_progress_bar=False) 23 | self.assertTrue(driver_binary.is_file(), NO_FILE) 24 | self.assertTrue(driver_link_target.is_file(), NO_LINK_FILE) 25 | 26 | 27 | class GeckoDriverManagerTestsWithExplicitLocations(ExplicitBaseTest): 28 | DRIVER_MANAGER = webdrivermanager.GeckoDriverManager 29 | 30 | @flaky 31 | def test_download(self): 32 | self.instance = self.DRIVER_MANAGER(download_root=self.temp_dir.name) 33 | filename = self.instance.download(show_progress_bar=False) 34 | self.assertTrue(filename.is_file(), NO_FILE) 35 | 36 | @flaky 37 | def test_download_and_install(self): 38 | link_path = self.make_link_dir() 39 | self.instance = self.DRIVER_MANAGER(download_root=self.temp_dir.name, link_path=link_path) 40 | driver_link_target, driver_binary = self.instance.download_and_install(show_progress_bar=False) 41 | self.assertTrue(driver_binary.is_file(), NO_FILE) 42 | self.assertTrue(driver_link_target.is_file(), NO_LINK_FILE) 43 | -------------------------------------------------------------------------------- /test/acceptance/test_ie.py: -------------------------------------------------------------------------------- 1 | import sys 2 | from flaky import flaky 3 | 4 | from .tools import SRC_ROOT, AutomaticBaseTest, ExplicitBaseTest, NO_FILE, NO_LINK_FILE 5 | 6 | sys.path.append(SRC_ROOT) 7 | import webdrivermanager # noqa: E402 I001 8 | 9 | 10 | class IEDriverManagerTestsWithAutomaticLocations(AutomaticBaseTest): 11 | DRIVER_MANAGER = webdrivermanager.IEDriverManager 12 | 13 | @flaky 14 | def test_download(self): 15 | self.instance = self.DRIVER_MANAGER(os_name="win", bitness="64") 16 | filename = self.instance.download(show_progress_bar=False) 17 | self.assertTrue(filename.is_file(), NO_FILE) 18 | 19 | @flaky 20 | def test_download_and_install(self): 21 | self.instance = self.DRIVER_MANAGER(os_name="win", bitness="64") 22 | driver_link_target, driver_binary = self.instance.download_and_install(show_progress_bar=False) 23 | self.assertTrue(driver_binary.is_file(), NO_FILE) 24 | self.assertTrue(driver_link_target.is_file(), NO_LINK_FILE) 25 | 26 | 27 | class IEDriverManagerTestsWithExplicitLocations(ExplicitBaseTest): 28 | DRIVER_MANAGER = webdrivermanager.IEDriverManager 29 | 30 | @flaky 31 | def test_download(self): 32 | self.instance = self.DRIVER_MANAGER(download_root=self.temp_dir.name, os_name="win", bitness="64") 33 | filename = self.instance.download(show_progress_bar=False) 34 | self.assertTrue(filename.is_file(), NO_FILE) 35 | 36 | @flaky 37 | def test_download_and_install(self): 38 | link_path = self.make_link_dir() 39 | self.instance = self.DRIVER_MANAGER(download_root=self.temp_dir.name, link_path=link_path, os_name="win", bitness="64") 40 | driver_link_target, driver_binary = self.instance.download_and_install(show_progress_bar=False) 41 | self.assertTrue(driver_binary.is_file(), NO_FILE) 42 | self.assertTrue(driver_link_target.is_file(), NO_LINK_FILE) 43 | -------------------------------------------------------------------------------- /test/acceptance/tools.py: -------------------------------------------------------------------------------- 1 | from unittest import TestCase 2 | from pathlib import Path 3 | from tempfile import TemporaryDirectory 4 | 5 | NO_FILE = "Actual webdriver artifact is not available" 6 | NO_LINK_FILE = "Link to webdriver artifact is not available" 7 | SRC_ROOT = str(Path(__file__).absolute().parent.parent.parent / "src") 8 | 9 | 10 | class AutomaticBaseTest(TestCase): 11 | DRIVER_MANAGER = None 12 | 13 | def setUp(self): 14 | self.assertIsNot(self.DRIVER_MANAGER, None, "DRIVER_MANAGER should not be none") 15 | 16 | 17 | class ExplicitBaseTest(TestCase): 18 | DRIVER_MANAGER = None 19 | 20 | def setUp(self): 21 | self.assertIsNot(self.DRIVER_MANAGER, None, "DRIVER_MANAGER should not be none") 22 | self.temp_dir = TemporaryDirectory() 23 | 24 | def tearDown(self): 25 | self.temp_dir.cleanup() 26 | 27 | def make_link_dir(self): 28 | link_path = Path(self.temp_dir.name) / "bin" 29 | link_path.mkdir(parents=True, exist_ok=True) 30 | return link_path 31 | -------------------------------------------------------------------------------- /test/unit/__init__.py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MarketSquare/webdrivermanager/0eade159ddffadd1d4221d3537263deabb9b55c0/test/unit/__init__.py -------------------------------------------------------------------------------- /tox.ini: -------------------------------------------------------------------------------- 1 | [tox] 2 | envlist=py37 3 | install_command = pip install {opts} {packages} 4 | [testenv] 5 | commands = python -V 6 | pytest test/ 7 | 8 | deps = -rrequirements-dev.txt 9 | -------------------------------------------------------------------------------- /versioneer.py: -------------------------------------------------------------------------------- 1 | 2 | # Version: 0.18 3 | 4 | """The Versioneer - like a rocketeer, but for versions. 5 | 6 | The Versioneer 7 | ============== 8 | 9 | * like a rocketeer, but for versions! 10 | * https://github.com/warner/python-versioneer 11 | * Brian Warner 12 | * License: Public Domain 13 | * Compatible With: python2.6, 2.7, 3.2, 3.3, 3.4, 3.5, 3.6, and pypy 14 | * [![Latest Version] 15 | (https://pypip.in/version/versioneer/badge.svg?style=flat) 16 | ](https://pypi.python.org/pypi/versioneer/) 17 | * [![Build Status] 18 | (https://travis-ci.org/warner/python-versioneer.png?branch=master) 19 | ](https://travis-ci.org/warner/python-versioneer) 20 | 21 | This is a tool for managing a recorded version number in distutils-based 22 | python projects. The goal is to remove the tedious and error-prone "update 23 | the embedded version string" step from your release process. Making a new 24 | release should be as easy as recording a new tag in your version-control 25 | system, and maybe making new tarballs. 26 | 27 | 28 | ## Quick Install 29 | 30 | * `pip install versioneer` to somewhere to your $PATH 31 | * add a `[versioneer]` section to your setup.cfg (see below) 32 | * run `versioneer install` in your source tree, commit the results 33 | 34 | ## Version Identifiers 35 | 36 | Source trees come from a variety of places: 37 | 38 | * a version-control system checkout (mostly used by developers) 39 | * a nightly tarball, produced by build automation 40 | * a snapshot tarball, produced by a web-based VCS browser, like github's 41 | "tarball from tag" feature 42 | * a release tarball, produced by "setup.py sdist", distributed through PyPI 43 | 44 | Within each source tree, the version identifier (either a string or a number, 45 | this tool is format-agnostic) can come from a variety of places: 46 | 47 | * ask the VCS tool itself, e.g. "git describe" (for checkouts), which knows 48 | about recent "tags" and an absolute revision-id 49 | * the name of the directory into which the tarball was unpacked 50 | * an expanded VCS keyword ($Id$, etc) 51 | * a `_version.py` created by some earlier build step 52 | 53 | For released software, the version identifier is closely related to a VCS 54 | tag. Some projects use tag names that include more than just the version 55 | string (e.g. "myproject-1.2" instead of just "1.2"), in which case the tool 56 | needs to strip the tag prefix to extract the version identifier. For 57 | unreleased software (between tags), the version identifier should provide 58 | enough information to help developers recreate the same tree, while also 59 | giving them an idea of roughly how old the tree is (after version 1.2, before 60 | version 1.3). Many VCS systems can report a description that captures this, 61 | for example `git describe --tags --dirty --always` reports things like 62 | "0.7-1-g574ab98-dirty" to indicate that the checkout is one revision past the 63 | 0.7 tag, has a unique revision id of "574ab98", and is "dirty" (it has 64 | uncommitted changes. 65 | 66 | The version identifier is used for multiple purposes: 67 | 68 | * to allow the module to self-identify its version: `myproject.__version__` 69 | * to choose a name and prefix for a 'setup.py sdist' tarball 70 | 71 | ## Theory of Operation 72 | 73 | Versioneer works by adding a special `_version.py` file into your source 74 | tree, where your `__init__.py` can import it. This `_version.py` knows how to 75 | dynamically ask the VCS tool for version information at import time. 76 | 77 | `_version.py` also contains `$Revision$` markers, and the installation 78 | process marks `_version.py` to have this marker rewritten with a tag name 79 | during the `git archive` command. As a result, generated tarballs will 80 | contain enough information to get the proper version. 81 | 82 | To allow `setup.py` to compute a version too, a `versioneer.py` is added to 83 | the top level of your source tree, next to `setup.py` and the `setup.cfg` 84 | that configures it. This overrides several distutils/setuptools commands to 85 | compute the version when invoked, and changes `setup.py build` and `setup.py 86 | sdist` to replace `_version.py` with a small static file that contains just 87 | the generated version data. 88 | 89 | ## Installation 90 | 91 | See [INSTALL.md](./INSTALL.md) for detailed installation instructions. 92 | 93 | ## Version-String Flavors 94 | 95 | Code which uses Versioneer can learn about its version string at runtime by 96 | importing `_version` from your main `__init__.py` file and running the 97 | `get_versions()` function. From the "outside" (e.g. in `setup.py`), you can 98 | import the top-level `versioneer.py` and run `get_versions()`. 99 | 100 | Both functions return a dictionary with different flavors of version 101 | information: 102 | 103 | * `['version']`: A condensed version string, rendered using the selected 104 | style. This is the most commonly used value for the project's version 105 | string. The default "pep440" style yields strings like `0.11`, 106 | `0.11+2.g1076c97`, or `0.11+2.g1076c97.dirty`. See the "Styles" section 107 | below for alternative styles. 108 | 109 | * `['full-revisionid']`: detailed revision identifier. For Git, this is the 110 | full SHA1 commit id, e.g. "1076c978a8d3cfc70f408fe5974aa6c092c949ac". 111 | 112 | * `['date']`: Date and time of the latest `HEAD` commit. For Git, it is the 113 | commit date in ISO 8601 format. This will be None if the date is not 114 | available. 115 | 116 | * `['dirty']`: a boolean, True if the tree has uncommitted changes. Note that 117 | this is only accurate if run in a VCS checkout, otherwise it is likely to 118 | be False or None 119 | 120 | * `['error']`: if the version string could not be computed, this will be set 121 | to a string describing the problem, otherwise it will be None. It may be 122 | useful to throw an exception in setup.py if this is set, to avoid e.g. 123 | creating tarballs with a version string of "unknown". 124 | 125 | Some variants are more useful than others. Including `full-revisionid` in a 126 | bug report should allow developers to reconstruct the exact code being tested 127 | (or indicate the presence of local changes that should be shared with the 128 | developers). `version` is suitable for display in an "about" box or a CLI 129 | `--version` output: it can be easily compared against release notes and lists 130 | of bugs fixed in various releases. 131 | 132 | The installer adds the following text to your `__init__.py` to place a basic 133 | version in `YOURPROJECT.__version__`: 134 | 135 | from ._version import get_versions 136 | __version__ = get_versions()['version'] 137 | del get_versions 138 | 139 | ## Styles 140 | 141 | The setup.cfg `style=` configuration controls how the VCS information is 142 | rendered into a version string. 143 | 144 | The default style, "pep440", produces a PEP440-compliant string, equal to the 145 | un-prefixed tag name for actual releases, and containing an additional "local 146 | version" section with more detail for in-between builds. For Git, this is 147 | TAG[+DISTANCE.gHEX[.dirty]] , using information from `git describe --tags 148 | --dirty --always`. For example "0.11+2.g1076c97.dirty" indicates that the 149 | tree is like the "1076c97" commit but has uncommitted changes (".dirty"), and 150 | that this commit is two revisions ("+2") beyond the "0.11" tag. For released 151 | software (exactly equal to a known tag), the identifier will only contain the 152 | stripped tag, e.g. "0.11". 153 | 154 | Other styles are available. See [details.md](details.md) in the Versioneer 155 | source tree for descriptions. 156 | 157 | ## Debugging 158 | 159 | Versioneer tries to avoid fatal errors: if something goes wrong, it will tend 160 | to return a version of "0+unknown". To investigate the problem, run `setup.py 161 | version`, which will run the version-lookup code in a verbose mode, and will 162 | display the full contents of `get_versions()` (including the `error` string, 163 | which may help identify what went wrong). 164 | 165 | ## Known Limitations 166 | 167 | Some situations are known to cause problems for Versioneer. This details the 168 | most significant ones. More can be found on Github 169 | [issues page](https://github.com/warner/python-versioneer/issues). 170 | 171 | ### Subprojects 172 | 173 | Versioneer has limited support for source trees in which `setup.py` is not in 174 | the root directory (e.g. `setup.py` and `.git/` are *not* siblings). The are 175 | two common reasons why `setup.py` might not be in the root: 176 | 177 | * Source trees which contain multiple subprojects, such as 178 | [Buildbot](https://github.com/buildbot/buildbot), which contains both 179 | "master" and "slave" subprojects, each with their own `setup.py`, 180 | `setup.cfg`, and `tox.ini`. Projects like these produce multiple PyPI 181 | distributions (and upload multiple independently-installable tarballs). 182 | * Source trees whose main purpose is to contain a C library, but which also 183 | provide bindings to Python (and perhaps other langauges) in subdirectories. 184 | 185 | Versioneer will look for `.git` in parent directories, and most operations 186 | should get the right version string. However `pip` and `setuptools` have bugs 187 | and implementation details which frequently cause `pip install .` from a 188 | subproject directory to fail to find a correct version string (so it usually 189 | defaults to `0+unknown`). 190 | 191 | `pip install --editable .` should work correctly. `setup.py install` might 192 | work too. 193 | 194 | Pip-8.1.1 is known to have this problem, but hopefully it will get fixed in 195 | some later version. 196 | 197 | [Bug #38](https://github.com/warner/python-versioneer/issues/38) is tracking 198 | this issue. The discussion in 199 | [PR #61](https://github.com/warner/python-versioneer/pull/61) describes the 200 | issue from the Versioneer side in more detail. 201 | [pip PR#3176](https://github.com/pypa/pip/pull/3176) and 202 | [pip PR#3615](https://github.com/pypa/pip/pull/3615) contain work to improve 203 | pip to let Versioneer work correctly. 204 | 205 | Versioneer-0.16 and earlier only looked for a `.git` directory next to the 206 | `setup.cfg`, so subprojects were completely unsupported with those releases. 207 | 208 | ### Editable installs with setuptools <= 18.5 209 | 210 | `setup.py develop` and `pip install --editable .` allow you to install a 211 | project into a virtualenv once, then continue editing the source code (and 212 | test) without re-installing after every change. 213 | 214 | "Entry-point scripts" (`setup(entry_points={"console_scripts": ..})`) are a 215 | convenient way to specify executable scripts that should be installed along 216 | with the python package. 217 | 218 | These both work as expected when using modern setuptools. When using 219 | setuptools-18.5 or earlier, however, certain operations will cause 220 | `pkg_resources.DistributionNotFound` errors when running the entrypoint 221 | script, which must be resolved by re-installing the package. This happens 222 | when the install happens with one version, then the egg_info data is 223 | regenerated while a different version is checked out. Many setup.py commands 224 | cause egg_info to be rebuilt (including `sdist`, `wheel`, and installing into 225 | a different virtualenv), so this can be surprising. 226 | 227 | [Bug #83](https://github.com/warner/python-versioneer/issues/83) describes 228 | this one, but upgrading to a newer version of setuptools should probably 229 | resolve it. 230 | 231 | ### Unicode version strings 232 | 233 | While Versioneer works (and is continually tested) with both Python 2 and 234 | Python 3, it is not entirely consistent with bytes-vs-unicode distinctions. 235 | Newer releases probably generate unicode version strings on py2. It's not 236 | clear that this is wrong, but it may be surprising for applications when then 237 | write these strings to a network connection or include them in bytes-oriented 238 | APIs like cryptographic checksums. 239 | 240 | [Bug #71](https://github.com/warner/python-versioneer/issues/71) investigates 241 | this question. 242 | 243 | 244 | ## Updating Versioneer 245 | 246 | To upgrade your project to a new release of Versioneer, do the following: 247 | 248 | * install the new Versioneer (`pip install -U versioneer` or equivalent) 249 | * edit `setup.cfg`, if necessary, to include any new configuration settings 250 | indicated by the release notes. See [UPGRADING](./UPGRADING.md) for details. 251 | * re-run `versioneer install` in your source tree, to replace 252 | `SRC/_version.py` 253 | * commit any changed files 254 | 255 | ## Future Directions 256 | 257 | This tool is designed to make it easily extended to other version-control 258 | systems: all VCS-specific components are in separate directories like 259 | src/git/ . The top-level `versioneer.py` script is assembled from these 260 | components by running make-versioneer.py . In the future, make-versioneer.py 261 | will take a VCS name as an argument, and will construct a version of 262 | `versioneer.py` that is specific to the given VCS. It might also take the 263 | configuration arguments that are currently provided manually during 264 | installation by editing setup.py . Alternatively, it might go the other 265 | direction and include code from all supported VCS systems, reducing the 266 | number of intermediate scripts. 267 | 268 | 269 | ## License 270 | 271 | To make Versioneer easier to embed, all its code is dedicated to the public 272 | domain. The `_version.py` that it creates is also in the public domain. 273 | Specifically, both are released under the Creative Commons "Public Domain 274 | Dedication" license (CC0-1.0), as described in 275 | https://creativecommons.org/publicdomain/zero/1.0/ . 276 | 277 | """ 278 | 279 | from __future__ import print_function 280 | try: 281 | import configparser 282 | except ImportError: 283 | import ConfigParser as configparser 284 | import errno 285 | import json 286 | import os 287 | import re 288 | import subprocess 289 | import sys 290 | 291 | 292 | class VersioneerConfig: 293 | """Container for Versioneer configuration parameters.""" 294 | 295 | 296 | def get_root(): 297 | """Get the project root directory. 298 | 299 | We require that all commands are run from the project root, i.e. the 300 | directory that contains setup.py, setup.cfg, and versioneer.py . 301 | """ 302 | root = os.path.realpath(os.path.abspath(os.getcwd())) 303 | setup_py = os.path.join(root, "setup.py") 304 | versioneer_py = os.path.join(root, "versioneer.py") 305 | if not (os.path.exists(setup_py) or os.path.exists(versioneer_py)): 306 | # allow 'python path/to/setup.py COMMAND' 307 | root = os.path.dirname(os.path.realpath(os.path.abspath(sys.argv[0]))) 308 | setup_py = os.path.join(root, "setup.py") 309 | versioneer_py = os.path.join(root, "versioneer.py") 310 | if not (os.path.exists(setup_py) or os.path.exists(versioneer_py)): 311 | err = ("Versioneer was unable to run the project root directory. " 312 | "Versioneer requires setup.py to be executed from " 313 | "its immediate directory (like 'python setup.py COMMAND'), " 314 | "or in a way that lets it use sys.argv[0] to find the root " 315 | "(like 'python path/to/setup.py COMMAND').") 316 | raise VersioneerBadRootError(err) 317 | try: 318 | # Certain runtime workflows (setup.py install/develop in a setuptools 319 | # tree) execute all dependencies in a single python process, so 320 | # "versioneer" may be imported multiple times, and python's shared 321 | # module-import table will cache the first one. So we can't use 322 | # os.path.dirname(__file__), as that will find whichever 323 | # versioneer.py was first imported, even in later projects. 324 | me = os.path.realpath(os.path.abspath(__file__)) 325 | me_dir = os.path.normcase(os.path.splitext(me)[0]) 326 | vsr_dir = os.path.normcase(os.path.splitext(versioneer_py)[0]) 327 | if me_dir != vsr_dir: 328 | print("Warning: build in %s is using versioneer.py from %s" 329 | % (os.path.dirname(me), versioneer_py)) 330 | except NameError: 331 | pass 332 | return root 333 | 334 | 335 | def get_config_from_root(root): 336 | """Read the project setup.cfg file to determine Versioneer config.""" 337 | # This might raise EnvironmentError (if setup.cfg is missing), or 338 | # configparser.NoSectionError (if it lacks a [versioneer] section), or 339 | # configparser.NoOptionError (if it lacks "VCS="). See the docstring at 340 | # the top of versioneer.py for instructions on writing your setup.cfg . 341 | setup_cfg = os.path.join(root, "setup.cfg") 342 | parser = configparser.SafeConfigParser() 343 | with open(setup_cfg, "r") as f: 344 | parser.readfp(f) 345 | VCS = parser.get("versioneer", "VCS") # mandatory 346 | 347 | def get(parser, name): 348 | if parser.has_option("versioneer", name): 349 | return parser.get("versioneer", name) 350 | return None 351 | cfg = VersioneerConfig() 352 | cfg.VCS = VCS 353 | cfg.style = get(parser, "style") or "" 354 | cfg.versionfile_source = get(parser, "versionfile_source") 355 | cfg.versionfile_build = get(parser, "versionfile_build") 356 | cfg.tag_prefix = get(parser, "tag_prefix") 357 | if cfg.tag_prefix in ("''", '""'): 358 | cfg.tag_prefix = "" 359 | cfg.parentdir_prefix = get(parser, "parentdir_prefix") 360 | cfg.verbose = get(parser, "verbose") 361 | return cfg 362 | 363 | 364 | class NotThisMethod(Exception): 365 | """Exception raised if a method is not valid for the current scenario.""" 366 | 367 | 368 | # these dictionaries contain VCS-specific tools 369 | LONG_VERSION_PY = {} 370 | HANDLERS = {} 371 | 372 | 373 | def register_vcs_handler(vcs, method): # decorator 374 | """Decorator to mark a method as the handler for a particular VCS.""" 375 | def decorate(f): 376 | """Store f in HANDLERS[vcs][method].""" 377 | if vcs not in HANDLERS: 378 | HANDLERS[vcs] = {} 379 | HANDLERS[vcs][method] = f 380 | return f 381 | return decorate 382 | 383 | 384 | def run_command(commands, args, cwd=None, verbose=False, hide_stderr=False, 385 | env=None): 386 | """Call the given command(s).""" 387 | assert isinstance(commands, list) 388 | p = None 389 | for c in commands: 390 | try: 391 | dispcmd = str([c] + args) 392 | # remember shell=False, so use git.cmd on windows, not just git 393 | p = subprocess.Popen([c] + args, cwd=cwd, env=env, 394 | stdout=subprocess.PIPE, 395 | stderr=(subprocess.PIPE if hide_stderr 396 | else None)) 397 | break 398 | except EnvironmentError: 399 | e = sys.exc_info()[1] 400 | if e.errno == errno.ENOENT: 401 | continue 402 | if verbose: 403 | print("unable to run %s" % dispcmd) 404 | print(e) 405 | return None, None 406 | else: 407 | if verbose: 408 | print("unable to find command, tried %s" % (commands,)) 409 | return None, None 410 | stdout = p.communicate()[0].strip() 411 | if sys.version_info[0] >= 3: 412 | stdout = stdout.decode() 413 | if p.returncode != 0: 414 | if verbose: 415 | print("unable to run %s (error)" % dispcmd) 416 | print("stdout was %s" % stdout) 417 | return None, p.returncode 418 | return stdout, p.returncode 419 | 420 | 421 | LONG_VERSION_PY['git'] = ''' 422 | # This file helps to compute a version number in source trees obtained from 423 | # git-archive tarball (such as those provided by githubs download-from-tag 424 | # feature). Distribution tarballs (built by setup.py sdist) and build 425 | # directories (produced by setup.py build) will contain a much shorter file 426 | # that just contains the computed version number. 427 | 428 | # This file is released into the public domain. Generated by 429 | # versioneer-0.18 (https://github.com/warner/python-versioneer) 430 | 431 | """Git implementation of _version.py.""" 432 | 433 | import errno 434 | import os 435 | import re 436 | import subprocess 437 | import sys 438 | 439 | 440 | def get_keywords(): 441 | """Get the keywords needed to look up the version information.""" 442 | # these strings will be replaced by git during git-archive. 443 | # setup.py/versioneer.py will grep for the variable names, so they must 444 | # each be defined on a line of their own. _version.py will just call 445 | # get_keywords(). 446 | git_refnames = "%(DOLLAR)sFormat:%%d%(DOLLAR)s" 447 | git_full = "%(DOLLAR)sFormat:%%H%(DOLLAR)s" 448 | git_date = "%(DOLLAR)sFormat:%%ci%(DOLLAR)s" 449 | keywords = {"refnames": git_refnames, "full": git_full, "date": git_date} 450 | return keywords 451 | 452 | 453 | class VersioneerConfig: 454 | """Container for Versioneer configuration parameters.""" 455 | 456 | 457 | def get_config(): 458 | """Create, populate and return the VersioneerConfig() object.""" 459 | # these strings are filled in when 'setup.py versioneer' creates 460 | # _version.py 461 | cfg = VersioneerConfig() 462 | cfg.VCS = "git" 463 | cfg.style = "%(STYLE)s" 464 | cfg.tag_prefix = "%(TAG_PREFIX)s" 465 | cfg.parentdir_prefix = "%(PARENTDIR_PREFIX)s" 466 | cfg.versionfile_source = "%(VERSIONFILE_SOURCE)s" 467 | cfg.verbose = False 468 | return cfg 469 | 470 | 471 | class NotThisMethod(Exception): 472 | """Exception raised if a method is not valid for the current scenario.""" 473 | 474 | 475 | LONG_VERSION_PY = {} 476 | HANDLERS = {} 477 | 478 | 479 | def register_vcs_handler(vcs, method): # decorator 480 | """Decorator to mark a method as the handler for a particular VCS.""" 481 | def decorate(f): 482 | """Store f in HANDLERS[vcs][method].""" 483 | if vcs not in HANDLERS: 484 | HANDLERS[vcs] = {} 485 | HANDLERS[vcs][method] = f 486 | return f 487 | return decorate 488 | 489 | 490 | def run_command(commands, args, cwd=None, verbose=False, hide_stderr=False, 491 | env=None): 492 | """Call the given command(s).""" 493 | assert isinstance(commands, list) 494 | p = None 495 | for c in commands: 496 | try: 497 | dispcmd = str([c] + args) 498 | # remember shell=False, so use git.cmd on windows, not just git 499 | p = subprocess.Popen([c] + args, cwd=cwd, env=env, 500 | stdout=subprocess.PIPE, 501 | stderr=(subprocess.PIPE if hide_stderr 502 | else None)) 503 | break 504 | except EnvironmentError: 505 | e = sys.exc_info()[1] 506 | if e.errno == errno.ENOENT: 507 | continue 508 | if verbose: 509 | print("unable to run %%s" %% dispcmd) 510 | print(e) 511 | return None, None 512 | else: 513 | if verbose: 514 | print("unable to find command, tried %%s" %% (commands,)) 515 | return None, None 516 | stdout = p.communicate()[0].strip() 517 | if sys.version_info[0] >= 3: 518 | stdout = stdout.decode() 519 | if p.returncode != 0: 520 | if verbose: 521 | print("unable to run %%s (error)" %% dispcmd) 522 | print("stdout was %%s" %% stdout) 523 | return None, p.returncode 524 | return stdout, p.returncode 525 | 526 | 527 | def versions_from_parentdir(parentdir_prefix, root, verbose): 528 | """Try to determine the version from the parent directory name. 529 | 530 | Source tarballs conventionally unpack into a directory that includes both 531 | the project name and a version string. We will also support searching up 532 | two directory levels for an appropriately named parent directory 533 | """ 534 | rootdirs = [] 535 | 536 | for i in range(3): 537 | dirname = os.path.basename(root) 538 | if dirname.startswith(parentdir_prefix): 539 | return {"version": dirname[len(parentdir_prefix):], 540 | "full-revisionid": None, 541 | "dirty": False, "error": None, "date": None} 542 | else: 543 | rootdirs.append(root) 544 | root = os.path.dirname(root) # up a level 545 | 546 | if verbose: 547 | print("Tried directories %%s but none started with prefix %%s" %% 548 | (str(rootdirs), parentdir_prefix)) 549 | raise NotThisMethod("rootdir doesn't start with parentdir_prefix") 550 | 551 | 552 | @register_vcs_handler("git", "get_keywords") 553 | def git_get_keywords(versionfile_abs): 554 | """Extract version information from the given file.""" 555 | # the code embedded in _version.py can just fetch the value of these 556 | # keywords. When used from setup.py, we don't want to import _version.py, 557 | # so we do it with a regexp instead. This function is not used from 558 | # _version.py. 559 | keywords = {} 560 | try: 561 | f = open(versionfile_abs, "r") 562 | for line in f.readlines(): 563 | if line.strip().startswith("git_refnames ="): 564 | mo = re.search(r'=\s*"(.*)"', line) 565 | if mo: 566 | keywords["refnames"] = mo.group(1) 567 | if line.strip().startswith("git_full ="): 568 | mo = re.search(r'=\s*"(.*)"', line) 569 | if mo: 570 | keywords["full"] = mo.group(1) 571 | if line.strip().startswith("git_date ="): 572 | mo = re.search(r'=\s*"(.*)"', line) 573 | if mo: 574 | keywords["date"] = mo.group(1) 575 | f.close() 576 | except EnvironmentError: 577 | pass 578 | return keywords 579 | 580 | 581 | @register_vcs_handler("git", "keywords") 582 | def git_versions_from_keywords(keywords, tag_prefix, verbose): 583 | """Get version information from git keywords.""" 584 | if not keywords: 585 | raise NotThisMethod("no keywords at all, weird") 586 | date = keywords.get("date") 587 | if date is not None: 588 | # git-2.2.0 added "%%cI", which expands to an ISO-8601 -compliant 589 | # datestamp. However we prefer "%%ci" (which expands to an "ISO-8601 590 | # -like" string, which we must then edit to make compliant), because 591 | # it's been around since git-1.5.3, and it's too difficult to 592 | # discover which version we're using, or to work around using an 593 | # older one. 594 | date = date.strip().replace(" ", "T", 1).replace(" ", "", 1) 595 | refnames = keywords["refnames"].strip() 596 | if refnames.startswith("$Format"): 597 | if verbose: 598 | print("keywords are unexpanded, not using") 599 | raise NotThisMethod("unexpanded keywords, not a git-archive tarball") 600 | refs = set([r.strip() for r in refnames.strip("()").split(",")]) 601 | # starting in git-1.8.3, tags are listed as "tag: foo-1.0" instead of 602 | # just "foo-1.0". If we see a "tag: " prefix, prefer those. 603 | TAG = "tag: " 604 | tags = set([r[len(TAG):] for r in refs if r.startswith(TAG)]) 605 | if not tags: 606 | # Either we're using git < 1.8.3, or there really are no tags. We use 607 | # a heuristic: assume all version tags have a digit. The old git %%d 608 | # expansion behaves like git log --decorate=short and strips out the 609 | # refs/heads/ and refs/tags/ prefixes that would let us distinguish 610 | # between branches and tags. By ignoring refnames without digits, we 611 | # filter out many common branch names like "release" and 612 | # "stabilization", as well as "HEAD" and "master". 613 | tags = set([r for r in refs if re.search(r'\d', r)]) 614 | if verbose: 615 | print("discarding '%%s', no digits" %% ",".join(refs - tags)) 616 | if verbose: 617 | print("likely tags: %%s" %% ",".join(sorted(tags))) 618 | for ref in sorted(tags): 619 | # sorting will prefer e.g. "2.0" over "2.0rc1" 620 | if ref.startswith(tag_prefix): 621 | r = ref[len(tag_prefix):] 622 | if verbose: 623 | print("picking %%s" %% r) 624 | return {"version": r, 625 | "full-revisionid": keywords["full"].strip(), 626 | "dirty": False, "error": None, 627 | "date": date} 628 | # no suitable tags, so version is "0+unknown", but full hex is still there 629 | if verbose: 630 | print("no suitable tags, using unknown + full revision id") 631 | return {"version": "0+unknown", 632 | "full-revisionid": keywords["full"].strip(), 633 | "dirty": False, "error": "no suitable tags", "date": None} 634 | 635 | 636 | @register_vcs_handler("git", "pieces_from_vcs") 637 | def git_pieces_from_vcs(tag_prefix, root, verbose, run_command=run_command): 638 | """Get version from 'git describe' in the root of the source tree. 639 | 640 | This only gets called if the git-archive 'subst' keywords were *not* 641 | expanded, and _version.py hasn't already been rewritten with a short 642 | version string, meaning we're inside a checked out source tree. 643 | """ 644 | GITS = ["git"] 645 | if sys.platform == "win32": 646 | GITS = ["git.cmd", "git.exe"] 647 | 648 | out, rc = run_command(GITS, ["rev-parse", "--git-dir"], cwd=root, 649 | hide_stderr=True) 650 | if rc != 0: 651 | if verbose: 652 | print("Directory %%s not under git control" %% root) 653 | raise NotThisMethod("'git rev-parse --git-dir' returned error") 654 | 655 | # if there is a tag matching tag_prefix, this yields TAG-NUM-gHEX[-dirty] 656 | # if there isn't one, this yields HEX[-dirty] (no NUM) 657 | describe_out, rc = run_command(GITS, ["describe", "--tags", "--dirty", 658 | "--always", "--long", 659 | "--match", "%%s*" %% tag_prefix], 660 | cwd=root) 661 | # --long was added in git-1.5.5 662 | if describe_out is None: 663 | raise NotThisMethod("'git describe' failed") 664 | describe_out = describe_out.strip() 665 | full_out, rc = run_command(GITS, ["rev-parse", "HEAD"], cwd=root) 666 | if full_out is None: 667 | raise NotThisMethod("'git rev-parse' failed") 668 | full_out = full_out.strip() 669 | 670 | pieces = {} 671 | pieces["long"] = full_out 672 | pieces["short"] = full_out[:7] # maybe improved later 673 | pieces["error"] = None 674 | 675 | # parse describe_out. It will be like TAG-NUM-gHEX[-dirty] or HEX[-dirty] 676 | # TAG might have hyphens. 677 | git_describe = describe_out 678 | 679 | # look for -dirty suffix 680 | dirty = git_describe.endswith("-dirty") 681 | pieces["dirty"] = dirty 682 | if dirty: 683 | git_describe = git_describe[:git_describe.rindex("-dirty")] 684 | 685 | # now we have TAG-NUM-gHEX or HEX 686 | 687 | if "-" in git_describe: 688 | # TAG-NUM-gHEX 689 | mo = re.search(r'^(.+)-(\d+)-g([0-9a-f]+)$', git_describe) 690 | if not mo: 691 | # unparseable. Maybe git-describe is misbehaving? 692 | pieces["error"] = ("unable to parse git-describe output: '%%s'" 693 | %% describe_out) 694 | return pieces 695 | 696 | # tag 697 | full_tag = mo.group(1) 698 | if not full_tag.startswith(tag_prefix): 699 | if verbose: 700 | fmt = "tag '%%s' doesn't start with prefix '%%s'" 701 | print(fmt %% (full_tag, tag_prefix)) 702 | pieces["error"] = ("tag '%%s' doesn't start with prefix '%%s'" 703 | %% (full_tag, tag_prefix)) 704 | return pieces 705 | pieces["closest-tag"] = full_tag[len(tag_prefix):] 706 | 707 | # distance: number of commits since tag 708 | pieces["distance"] = int(mo.group(2)) 709 | 710 | # commit: short hex revision ID 711 | pieces["short"] = mo.group(3) 712 | 713 | else: 714 | # HEX: no tags 715 | pieces["closest-tag"] = None 716 | count_out, rc = run_command(GITS, ["rev-list", "HEAD", "--count"], 717 | cwd=root) 718 | pieces["distance"] = int(count_out) # total number of commits 719 | 720 | # commit date: see ISO-8601 comment in git_versions_from_keywords() 721 | date = run_command(GITS, ["show", "-s", "--format=%%ci", "HEAD"], 722 | cwd=root)[0].strip() 723 | pieces["date"] = date.strip().replace(" ", "T", 1).replace(" ", "", 1) 724 | 725 | return pieces 726 | 727 | 728 | def plus_or_dot(pieces): 729 | """Return a + if we don't already have one, else return a .""" 730 | if "+" in pieces.get("closest-tag", ""): 731 | return "." 732 | return "+" 733 | 734 | 735 | def render_pep440(pieces): 736 | """Build up version string, with post-release "local version identifier". 737 | 738 | Our goal: TAG[+DISTANCE.gHEX[.dirty]] . Note that if you 739 | get a tagged build and then dirty it, you'll get TAG+0.gHEX.dirty 740 | 741 | Exceptions: 742 | 1: no tags. git_describe was just HEX. 0+untagged.DISTANCE.gHEX[.dirty] 743 | """ 744 | if pieces["closest-tag"]: 745 | rendered = pieces["closest-tag"] 746 | if pieces["distance"] or pieces["dirty"]: 747 | rendered += plus_or_dot(pieces) 748 | rendered += "%%d.g%%s" %% (pieces["distance"], pieces["short"]) 749 | if pieces["dirty"]: 750 | rendered += ".dirty" 751 | else: 752 | # exception #1 753 | rendered = "0+untagged.%%d.g%%s" %% (pieces["distance"], 754 | pieces["short"]) 755 | if pieces["dirty"]: 756 | rendered += ".dirty" 757 | return rendered 758 | 759 | 760 | def render_pep440_pre(pieces): 761 | """TAG[.post.devDISTANCE] -- No -dirty. 762 | 763 | Exceptions: 764 | 1: no tags. 0.post.devDISTANCE 765 | """ 766 | if pieces["closest-tag"]: 767 | rendered = pieces["closest-tag"] 768 | if pieces["distance"]: 769 | rendered += ".post.dev%%d" %% pieces["distance"] 770 | else: 771 | # exception #1 772 | rendered = "0.post.dev%%d" %% pieces["distance"] 773 | return rendered 774 | 775 | 776 | def render_pep440_post(pieces): 777 | """TAG[.postDISTANCE[.dev0]+gHEX] . 778 | 779 | The ".dev0" means dirty. Note that .dev0 sorts backwards 780 | (a dirty tree will appear "older" than the corresponding clean one), 781 | but you shouldn't be releasing software with -dirty anyways. 782 | 783 | Exceptions: 784 | 1: no tags. 0.postDISTANCE[.dev0] 785 | """ 786 | if pieces["closest-tag"]: 787 | rendered = pieces["closest-tag"] 788 | if pieces["distance"] or pieces["dirty"]: 789 | rendered += ".post%%d" %% pieces["distance"] 790 | if pieces["dirty"]: 791 | rendered += ".dev0" 792 | rendered += plus_or_dot(pieces) 793 | rendered += "g%%s" %% pieces["short"] 794 | else: 795 | # exception #1 796 | rendered = "0.post%%d" %% pieces["distance"] 797 | if pieces["dirty"]: 798 | rendered += ".dev0" 799 | rendered += "+g%%s" %% pieces["short"] 800 | return rendered 801 | 802 | 803 | def render_pep440_old(pieces): 804 | """TAG[.postDISTANCE[.dev0]] . 805 | 806 | The ".dev0" means dirty. 807 | 808 | Eexceptions: 809 | 1: no tags. 0.postDISTANCE[.dev0] 810 | """ 811 | if pieces["closest-tag"]: 812 | rendered = pieces["closest-tag"] 813 | if pieces["distance"] or pieces["dirty"]: 814 | rendered += ".post%%d" %% pieces["distance"] 815 | if pieces["dirty"]: 816 | rendered += ".dev0" 817 | else: 818 | # exception #1 819 | rendered = "0.post%%d" %% pieces["distance"] 820 | if pieces["dirty"]: 821 | rendered += ".dev0" 822 | return rendered 823 | 824 | 825 | def render_git_describe(pieces): 826 | """TAG[-DISTANCE-gHEX][-dirty]. 827 | 828 | Like 'git describe --tags --dirty --always'. 829 | 830 | Exceptions: 831 | 1: no tags. HEX[-dirty] (note: no 'g' prefix) 832 | """ 833 | if pieces["closest-tag"]: 834 | rendered = pieces["closest-tag"] 835 | if pieces["distance"]: 836 | rendered += "-%%d-g%%s" %% (pieces["distance"], pieces["short"]) 837 | else: 838 | # exception #1 839 | rendered = pieces["short"] 840 | if pieces["dirty"]: 841 | rendered += "-dirty" 842 | return rendered 843 | 844 | 845 | def render_git_describe_long(pieces): 846 | """TAG-DISTANCE-gHEX[-dirty]. 847 | 848 | Like 'git describe --tags --dirty --always -long'. 849 | The distance/hash is unconditional. 850 | 851 | Exceptions: 852 | 1: no tags. HEX[-dirty] (note: no 'g' prefix) 853 | """ 854 | if pieces["closest-tag"]: 855 | rendered = pieces["closest-tag"] 856 | rendered += "-%%d-g%%s" %% (pieces["distance"], pieces["short"]) 857 | else: 858 | # exception #1 859 | rendered = pieces["short"] 860 | if pieces["dirty"]: 861 | rendered += "-dirty" 862 | return rendered 863 | 864 | 865 | def render(pieces, style): 866 | """Render the given version pieces into the requested style.""" 867 | if pieces["error"]: 868 | return {"version": "unknown", 869 | "full-revisionid": pieces.get("long"), 870 | "dirty": None, 871 | "error": pieces["error"], 872 | "date": None} 873 | 874 | if not style or style == "default": 875 | style = "pep440" # the default 876 | 877 | if style == "pep440": 878 | rendered = render_pep440(pieces) 879 | elif style == "pep440-pre": 880 | rendered = render_pep440_pre(pieces) 881 | elif style == "pep440-post": 882 | rendered = render_pep440_post(pieces) 883 | elif style == "pep440-old": 884 | rendered = render_pep440_old(pieces) 885 | elif style == "git-describe": 886 | rendered = render_git_describe(pieces) 887 | elif style == "git-describe-long": 888 | rendered = render_git_describe_long(pieces) 889 | else: 890 | raise ValueError("unknown style '%%s'" %% style) 891 | 892 | return {"version": rendered, "full-revisionid": pieces["long"], 893 | "dirty": pieces["dirty"], "error": None, 894 | "date": pieces.get("date")} 895 | 896 | 897 | def get_versions(): 898 | """Get version information or return default if unable to do so.""" 899 | # I am in _version.py, which lives at ROOT/VERSIONFILE_SOURCE. If we have 900 | # __file__, we can work backwards from there to the root. Some 901 | # py2exe/bbfreeze/non-CPython implementations don't do __file__, in which 902 | # case we can only use expanded keywords. 903 | 904 | cfg = get_config() 905 | verbose = cfg.verbose 906 | 907 | try: 908 | return git_versions_from_keywords(get_keywords(), cfg.tag_prefix, 909 | verbose) 910 | except NotThisMethod: 911 | pass 912 | 913 | try: 914 | root = os.path.realpath(__file__) 915 | # versionfile_source is the relative path from the top of the source 916 | # tree (where the .git directory might live) to this file. Invert 917 | # this to find the root from __file__. 918 | for i in cfg.versionfile_source.split('/'): 919 | root = os.path.dirname(root) 920 | except NameError: 921 | return {"version": "0+unknown", "full-revisionid": None, 922 | "dirty": None, 923 | "error": "unable to find root of source tree", 924 | "date": None} 925 | 926 | try: 927 | pieces = git_pieces_from_vcs(cfg.tag_prefix, root, verbose) 928 | return render(pieces, cfg.style) 929 | except NotThisMethod: 930 | pass 931 | 932 | try: 933 | if cfg.parentdir_prefix: 934 | return versions_from_parentdir(cfg.parentdir_prefix, root, verbose) 935 | except NotThisMethod: 936 | pass 937 | 938 | return {"version": "0+unknown", "full-revisionid": None, 939 | "dirty": None, 940 | "error": "unable to compute version", "date": None} 941 | ''' 942 | 943 | 944 | @register_vcs_handler("git", "get_keywords") 945 | def git_get_keywords(versionfile_abs): 946 | """Extract version information from the given file.""" 947 | # the code embedded in _version.py can just fetch the value of these 948 | # keywords. When used from setup.py, we don't want to import _version.py, 949 | # so we do it with a regexp instead. This function is not used from 950 | # _version.py. 951 | keywords = {} 952 | try: 953 | f = open(versionfile_abs, "r") 954 | for line in f.readlines(): 955 | if line.strip().startswith("git_refnames ="): 956 | mo = re.search(r'=\s*"(.*)"', line) 957 | if mo: 958 | keywords["refnames"] = mo.group(1) 959 | if line.strip().startswith("git_full ="): 960 | mo = re.search(r'=\s*"(.*)"', line) 961 | if mo: 962 | keywords["full"] = mo.group(1) 963 | if line.strip().startswith("git_date ="): 964 | mo = re.search(r'=\s*"(.*)"', line) 965 | if mo: 966 | keywords["date"] = mo.group(1) 967 | f.close() 968 | except EnvironmentError: 969 | pass 970 | return keywords 971 | 972 | 973 | @register_vcs_handler("git", "keywords") 974 | def git_versions_from_keywords(keywords, tag_prefix, verbose): 975 | """Get version information from git keywords.""" 976 | if not keywords: 977 | raise NotThisMethod("no keywords at all, weird") 978 | date = keywords.get("date") 979 | if date is not None: 980 | # git-2.2.0 added "%cI", which expands to an ISO-8601 -compliant 981 | # datestamp. However we prefer "%ci" (which expands to an "ISO-8601 982 | # -like" string, which we must then edit to make compliant), because 983 | # it's been around since git-1.5.3, and it's too difficult to 984 | # discover which version we're using, or to work around using an 985 | # older one. 986 | date = date.strip().replace(" ", "T", 1).replace(" ", "", 1) 987 | refnames = keywords["refnames"].strip() 988 | if refnames.startswith("$Format"): 989 | if verbose: 990 | print("keywords are unexpanded, not using") 991 | raise NotThisMethod("unexpanded keywords, not a git-archive tarball") 992 | refs = set([r.strip() for r in refnames.strip("()").split(",")]) 993 | # starting in git-1.8.3, tags are listed as "tag: foo-1.0" instead of 994 | # just "foo-1.0". If we see a "tag: " prefix, prefer those. 995 | TAG = "tag: " 996 | tags = set([r[len(TAG):] for r in refs if r.startswith(TAG)]) 997 | if not tags: 998 | # Either we're using git < 1.8.3, or there really are no tags. We use 999 | # a heuristic: assume all version tags have a digit. The old git %d 1000 | # expansion behaves like git log --decorate=short and strips out the 1001 | # refs/heads/ and refs/tags/ prefixes that would let us distinguish 1002 | # between branches and tags. By ignoring refnames without digits, we 1003 | # filter out many common branch names like "release" and 1004 | # "stabilization", as well as "HEAD" and "master". 1005 | tags = set([r for r in refs if re.search(r'\d', r)]) 1006 | if verbose: 1007 | print("discarding '%s', no digits" % ",".join(refs - tags)) 1008 | if verbose: 1009 | print("likely tags: %s" % ",".join(sorted(tags))) 1010 | for ref in sorted(tags): 1011 | # sorting will prefer e.g. "2.0" over "2.0rc1" 1012 | if ref.startswith(tag_prefix): 1013 | r = ref[len(tag_prefix):] 1014 | if verbose: 1015 | print("picking %s" % r) 1016 | return {"version": r, 1017 | "full-revisionid": keywords["full"].strip(), 1018 | "dirty": False, "error": None, 1019 | "date": date} 1020 | # no suitable tags, so version is "0+unknown", but full hex is still there 1021 | if verbose: 1022 | print("no suitable tags, using unknown + full revision id") 1023 | return {"version": "0+unknown", 1024 | "full-revisionid": keywords["full"].strip(), 1025 | "dirty": False, "error": "no suitable tags", "date": None} 1026 | 1027 | 1028 | @register_vcs_handler("git", "pieces_from_vcs") 1029 | def git_pieces_from_vcs(tag_prefix, root, verbose, run_command=run_command): 1030 | """Get version from 'git describe' in the root of the source tree. 1031 | 1032 | This only gets called if the git-archive 'subst' keywords were *not* 1033 | expanded, and _version.py hasn't already been rewritten with a short 1034 | version string, meaning we're inside a checked out source tree. 1035 | """ 1036 | GITS = ["git"] 1037 | if sys.platform == "win32": 1038 | GITS = ["git.cmd", "git.exe"] 1039 | 1040 | out, rc = run_command(GITS, ["rev-parse", "--git-dir"], cwd=root, 1041 | hide_stderr=True) 1042 | if rc != 0: 1043 | if verbose: 1044 | print("Directory %s not under git control" % root) 1045 | raise NotThisMethod("'git rev-parse --git-dir' returned error") 1046 | 1047 | # if there is a tag matching tag_prefix, this yields TAG-NUM-gHEX[-dirty] 1048 | # if there isn't one, this yields HEX[-dirty] (no NUM) 1049 | describe_out, rc = run_command(GITS, ["describe", "--tags", "--dirty", 1050 | "--always", "--long", 1051 | "--match", "%s*" % tag_prefix], 1052 | cwd=root) 1053 | # --long was added in git-1.5.5 1054 | if describe_out is None: 1055 | raise NotThisMethod("'git describe' failed") 1056 | describe_out = describe_out.strip() 1057 | full_out, rc = run_command(GITS, ["rev-parse", "HEAD"], cwd=root) 1058 | if full_out is None: 1059 | raise NotThisMethod("'git rev-parse' failed") 1060 | full_out = full_out.strip() 1061 | 1062 | pieces = {} 1063 | pieces["long"] = full_out 1064 | pieces["short"] = full_out[:7] # maybe improved later 1065 | pieces["error"] = None 1066 | 1067 | # parse describe_out. It will be like TAG-NUM-gHEX[-dirty] or HEX[-dirty] 1068 | # TAG might have hyphens. 1069 | git_describe = describe_out 1070 | 1071 | # look for -dirty suffix 1072 | dirty = git_describe.endswith("-dirty") 1073 | pieces["dirty"] = dirty 1074 | if dirty: 1075 | git_describe = git_describe[:git_describe.rindex("-dirty")] 1076 | 1077 | # now we have TAG-NUM-gHEX or HEX 1078 | 1079 | if "-" in git_describe: 1080 | # TAG-NUM-gHEX 1081 | mo = re.search(r'^(.+)-(\d+)-g([0-9a-f]+)$', git_describe) 1082 | if not mo: 1083 | # unparseable. Maybe git-describe is misbehaving? 1084 | pieces["error"] = ("unable to parse git-describe output: '%s'" 1085 | % describe_out) 1086 | return pieces 1087 | 1088 | # tag 1089 | full_tag = mo.group(1) 1090 | if not full_tag.startswith(tag_prefix): 1091 | if verbose: 1092 | fmt = "tag '%s' doesn't start with prefix '%s'" 1093 | print(fmt % (full_tag, tag_prefix)) 1094 | pieces["error"] = ("tag '%s' doesn't start with prefix '%s'" 1095 | % (full_tag, tag_prefix)) 1096 | return pieces 1097 | pieces["closest-tag"] = full_tag[len(tag_prefix):] 1098 | 1099 | # distance: number of commits since tag 1100 | pieces["distance"] = int(mo.group(2)) 1101 | 1102 | # commit: short hex revision ID 1103 | pieces["short"] = mo.group(3) 1104 | 1105 | else: 1106 | # HEX: no tags 1107 | pieces["closest-tag"] = None 1108 | count_out, rc = run_command(GITS, ["rev-list", "HEAD", "--count"], 1109 | cwd=root) 1110 | pieces["distance"] = int(count_out) # total number of commits 1111 | 1112 | # commit date: see ISO-8601 comment in git_versions_from_keywords() 1113 | date = run_command(GITS, ["show", "-s", "--format=%ci", "HEAD"], 1114 | cwd=root)[0].strip() 1115 | pieces["date"] = date.strip().replace(" ", "T", 1).replace(" ", "", 1) 1116 | 1117 | return pieces 1118 | 1119 | 1120 | def do_vcs_install(manifest_in, versionfile_source, ipy): 1121 | """Git-specific installation logic for Versioneer. 1122 | 1123 | For Git, this means creating/changing .gitattributes to mark _version.py 1124 | for export-subst keyword substitution. 1125 | """ 1126 | GITS = ["git"] 1127 | if sys.platform == "win32": 1128 | GITS = ["git.cmd", "git.exe"] 1129 | files = [manifest_in, versionfile_source] 1130 | if ipy: 1131 | files.append(ipy) 1132 | try: 1133 | me = __file__ 1134 | if me.endswith(".pyc") or me.endswith(".pyo"): 1135 | me = os.path.splitext(me)[0] + ".py" 1136 | versioneer_file = os.path.relpath(me) 1137 | except NameError: 1138 | versioneer_file = "versioneer.py" 1139 | files.append(versioneer_file) 1140 | present = False 1141 | try: 1142 | f = open(".gitattributes", "r") 1143 | for line in f.readlines(): 1144 | if line.strip().startswith(versionfile_source): 1145 | if "export-subst" in line.strip().split()[1:]: 1146 | present = True 1147 | f.close() 1148 | except EnvironmentError: 1149 | pass 1150 | if not present: 1151 | f = open(".gitattributes", "a+") 1152 | f.write("%s export-subst\n" % versionfile_source) 1153 | f.close() 1154 | files.append(".gitattributes") 1155 | run_command(GITS, ["add", "--"] + files) 1156 | 1157 | 1158 | def versions_from_parentdir(parentdir_prefix, root, verbose): 1159 | """Try to determine the version from the parent directory name. 1160 | 1161 | Source tarballs conventionally unpack into a directory that includes both 1162 | the project name and a version string. We will also support searching up 1163 | two directory levels for an appropriately named parent directory 1164 | """ 1165 | rootdirs = [] 1166 | 1167 | for i in range(3): 1168 | dirname = os.path.basename(root) 1169 | if dirname.startswith(parentdir_prefix): 1170 | return {"version": dirname[len(parentdir_prefix):], 1171 | "full-revisionid": None, 1172 | "dirty": False, "error": None, "date": None} 1173 | else: 1174 | rootdirs.append(root) 1175 | root = os.path.dirname(root) # up a level 1176 | 1177 | if verbose: 1178 | print("Tried directories %s but none started with prefix %s" % 1179 | (str(rootdirs), parentdir_prefix)) 1180 | raise NotThisMethod("rootdir doesn't start with parentdir_prefix") 1181 | 1182 | 1183 | SHORT_VERSION_PY = """ 1184 | # This file was generated by 'versioneer.py' (0.18) from 1185 | # revision-control system data, or from the parent directory name of an 1186 | # unpacked source archive. Distribution tarballs contain a pre-generated copy 1187 | # of this file. 1188 | 1189 | import json 1190 | 1191 | version_json = ''' 1192 | %s 1193 | ''' # END VERSION_JSON 1194 | 1195 | 1196 | def get_versions(): 1197 | return json.loads(version_json) 1198 | """ 1199 | 1200 | 1201 | def versions_from_file(filename): 1202 | """Try to determine the version from _version.py if present.""" 1203 | try: 1204 | with open(filename) as f: 1205 | contents = f.read() 1206 | except EnvironmentError: 1207 | raise NotThisMethod("unable to read _version.py") 1208 | mo = re.search(r"version_json = '''\n(.*)''' # END VERSION_JSON", 1209 | contents, re.M | re.S) 1210 | if not mo: 1211 | mo = re.search(r"version_json = '''\r\n(.*)''' # END VERSION_JSON", 1212 | contents, re.M | re.S) 1213 | if not mo: 1214 | raise NotThisMethod("no version_json in _version.py") 1215 | return json.loads(mo.group(1)) 1216 | 1217 | 1218 | def write_to_version_file(filename, versions): 1219 | """Write the given version number to the given _version.py file.""" 1220 | os.unlink(filename) 1221 | contents = json.dumps(versions, sort_keys=True, 1222 | indent=1, separators=(",", ": ")) 1223 | with open(filename, "w") as f: 1224 | f.write(SHORT_VERSION_PY % contents) 1225 | 1226 | print("set %s to '%s'" % (filename, versions["version"])) 1227 | 1228 | 1229 | def plus_or_dot(pieces): 1230 | """Return a + if we don't already have one, else return a .""" 1231 | if "+" in pieces.get("closest-tag", ""): 1232 | return "." 1233 | return "+" 1234 | 1235 | 1236 | def render_pep440(pieces): 1237 | """Build up version string, with post-release "local version identifier". 1238 | 1239 | Our goal: TAG[+DISTANCE.gHEX[.dirty]] . Note that if you 1240 | get a tagged build and then dirty it, you'll get TAG+0.gHEX.dirty 1241 | 1242 | Exceptions: 1243 | 1: no tags. git_describe was just HEX. 0+untagged.DISTANCE.gHEX[.dirty] 1244 | """ 1245 | if pieces["closest-tag"]: 1246 | rendered = pieces["closest-tag"] 1247 | if pieces["distance"] or pieces["dirty"]: 1248 | rendered += plus_or_dot(pieces) 1249 | rendered += "%d.g%s" % (pieces["distance"], pieces["short"]) 1250 | if pieces["dirty"]: 1251 | rendered += ".dirty" 1252 | else: 1253 | # exception #1 1254 | rendered = "0+untagged.%d.g%s" % (pieces["distance"], 1255 | pieces["short"]) 1256 | if pieces["dirty"]: 1257 | rendered += ".dirty" 1258 | return rendered 1259 | 1260 | 1261 | def render_pep440_pre(pieces): 1262 | """TAG[.post.devDISTANCE] -- No -dirty. 1263 | 1264 | Exceptions: 1265 | 1: no tags. 0.post.devDISTANCE 1266 | """ 1267 | if pieces["closest-tag"]: 1268 | rendered = pieces["closest-tag"] 1269 | if pieces["distance"]: 1270 | rendered += ".post.dev%d" % pieces["distance"] 1271 | else: 1272 | # exception #1 1273 | rendered = "0.post.dev%d" % pieces["distance"] 1274 | return rendered 1275 | 1276 | 1277 | def render_pep440_post(pieces): 1278 | """TAG[.postDISTANCE[.dev0]+gHEX] . 1279 | 1280 | The ".dev0" means dirty. Note that .dev0 sorts backwards 1281 | (a dirty tree will appear "older" than the corresponding clean one), 1282 | but you shouldn't be releasing software with -dirty anyways. 1283 | 1284 | Exceptions: 1285 | 1: no tags. 0.postDISTANCE[.dev0] 1286 | """ 1287 | if pieces["closest-tag"]: 1288 | rendered = pieces["closest-tag"] 1289 | if pieces["distance"] or pieces["dirty"]: 1290 | rendered += ".post%d" % pieces["distance"] 1291 | if pieces["dirty"]: 1292 | rendered += ".dev0" 1293 | rendered += plus_or_dot(pieces) 1294 | rendered += "g%s" % pieces["short"] 1295 | else: 1296 | # exception #1 1297 | rendered = "0.post%d" % pieces["distance"] 1298 | if pieces["dirty"]: 1299 | rendered += ".dev0" 1300 | rendered += "+g%s" % pieces["short"] 1301 | return rendered 1302 | 1303 | 1304 | def render_pep440_old(pieces): 1305 | """TAG[.postDISTANCE[.dev0]] . 1306 | 1307 | The ".dev0" means dirty. 1308 | 1309 | Eexceptions: 1310 | 1: no tags. 0.postDISTANCE[.dev0] 1311 | """ 1312 | if pieces["closest-tag"]: 1313 | rendered = pieces["closest-tag"] 1314 | if pieces["distance"] or pieces["dirty"]: 1315 | rendered += ".post%d" % pieces["distance"] 1316 | if pieces["dirty"]: 1317 | rendered += ".dev0" 1318 | else: 1319 | # exception #1 1320 | rendered = "0.post%d" % pieces["distance"] 1321 | if pieces["dirty"]: 1322 | rendered += ".dev0" 1323 | return rendered 1324 | 1325 | 1326 | def render_git_describe(pieces): 1327 | """TAG[-DISTANCE-gHEX][-dirty]. 1328 | 1329 | Like 'git describe --tags --dirty --always'. 1330 | 1331 | Exceptions: 1332 | 1: no tags. HEX[-dirty] (note: no 'g' prefix) 1333 | """ 1334 | if pieces["closest-tag"]: 1335 | rendered = pieces["closest-tag"] 1336 | if pieces["distance"]: 1337 | rendered += "-%d-g%s" % (pieces["distance"], pieces["short"]) 1338 | else: 1339 | # exception #1 1340 | rendered = pieces["short"] 1341 | if pieces["dirty"]: 1342 | rendered += "-dirty" 1343 | return rendered 1344 | 1345 | 1346 | def render_git_describe_long(pieces): 1347 | """TAG-DISTANCE-gHEX[-dirty]. 1348 | 1349 | Like 'git describe --tags --dirty --always -long'. 1350 | The distance/hash is unconditional. 1351 | 1352 | Exceptions: 1353 | 1: no tags. HEX[-dirty] (note: no 'g' prefix) 1354 | """ 1355 | if pieces["closest-tag"]: 1356 | rendered = pieces["closest-tag"] 1357 | rendered += "-%d-g%s" % (pieces["distance"], pieces["short"]) 1358 | else: 1359 | # exception #1 1360 | rendered = pieces["short"] 1361 | if pieces["dirty"]: 1362 | rendered += "-dirty" 1363 | return rendered 1364 | 1365 | 1366 | def render(pieces, style): 1367 | """Render the given version pieces into the requested style.""" 1368 | if pieces["error"]: 1369 | return {"version": "unknown", 1370 | "full-revisionid": pieces.get("long"), 1371 | "dirty": None, 1372 | "error": pieces["error"], 1373 | "date": None} 1374 | 1375 | if not style or style == "default": 1376 | style = "pep440" # the default 1377 | 1378 | if style == "pep440": 1379 | rendered = render_pep440(pieces) 1380 | elif style == "pep440-pre": 1381 | rendered = render_pep440_pre(pieces) 1382 | elif style == "pep440-post": 1383 | rendered = render_pep440_post(pieces) 1384 | elif style == "pep440-old": 1385 | rendered = render_pep440_old(pieces) 1386 | elif style == "git-describe": 1387 | rendered = render_git_describe(pieces) 1388 | elif style == "git-describe-long": 1389 | rendered = render_git_describe_long(pieces) 1390 | else: 1391 | raise ValueError("unknown style '%s'" % style) 1392 | 1393 | return {"version": rendered, "full-revisionid": pieces["long"], 1394 | "dirty": pieces["dirty"], "error": None, 1395 | "date": pieces.get("date")} 1396 | 1397 | 1398 | class VersioneerBadRootError(Exception): 1399 | """The project root directory is unknown or missing key files.""" 1400 | 1401 | 1402 | def get_versions(verbose=False): 1403 | """Get the project version from whatever source is available. 1404 | 1405 | Returns dict with two keys: 'version' and 'full'. 1406 | """ 1407 | if "versioneer" in sys.modules: 1408 | # see the discussion in cmdclass.py:get_cmdclass() 1409 | del sys.modules["versioneer"] 1410 | 1411 | root = get_root() 1412 | cfg = get_config_from_root(root) 1413 | 1414 | assert cfg.VCS is not None, "please set [versioneer]VCS= in setup.cfg" 1415 | handlers = HANDLERS.get(cfg.VCS) 1416 | assert handlers, "unrecognized VCS '%s'" % cfg.VCS 1417 | verbose = verbose or cfg.verbose 1418 | assert cfg.versionfile_source is not None, \ 1419 | "please set versioneer.versionfile_source" 1420 | assert cfg.tag_prefix is not None, "please set versioneer.tag_prefix" 1421 | 1422 | versionfile_abs = os.path.join(root, cfg.versionfile_source) 1423 | 1424 | # extract version from first of: _version.py, VCS command (e.g. 'git 1425 | # describe'), parentdir. This is meant to work for developers using a 1426 | # source checkout, for users of a tarball created by 'setup.py sdist', 1427 | # and for users of a tarball/zipball created by 'git archive' or github's 1428 | # download-from-tag feature or the equivalent in other VCSes. 1429 | 1430 | get_keywords_f = handlers.get("get_keywords") 1431 | from_keywords_f = handlers.get("keywords") 1432 | if get_keywords_f and from_keywords_f: 1433 | try: 1434 | keywords = get_keywords_f(versionfile_abs) 1435 | ver = from_keywords_f(keywords, cfg.tag_prefix, verbose) 1436 | if verbose: 1437 | print("got version from expanded keyword %s" % ver) 1438 | return ver 1439 | except NotThisMethod: 1440 | pass 1441 | 1442 | try: 1443 | ver = versions_from_file(versionfile_abs) 1444 | if verbose: 1445 | print("got version from file %s %s" % (versionfile_abs, ver)) 1446 | return ver 1447 | except NotThisMethod: 1448 | pass 1449 | 1450 | from_vcs_f = handlers.get("pieces_from_vcs") 1451 | if from_vcs_f: 1452 | try: 1453 | pieces = from_vcs_f(cfg.tag_prefix, root, verbose) 1454 | ver = render(pieces, cfg.style) 1455 | if verbose: 1456 | print("got version from VCS %s" % ver) 1457 | return ver 1458 | except NotThisMethod: 1459 | pass 1460 | 1461 | try: 1462 | if cfg.parentdir_prefix: 1463 | ver = versions_from_parentdir(cfg.parentdir_prefix, root, verbose) 1464 | if verbose: 1465 | print("got version from parentdir %s" % ver) 1466 | return ver 1467 | except NotThisMethod: 1468 | pass 1469 | 1470 | if verbose: 1471 | print("unable to compute version") 1472 | 1473 | return {"version": "0+unknown", "full-revisionid": None, 1474 | "dirty": None, "error": "unable to compute version", 1475 | "date": None} 1476 | 1477 | 1478 | def get_version(): 1479 | """Get the short version string for this project.""" 1480 | return get_versions()["version"] 1481 | 1482 | 1483 | def get_cmdclass(): 1484 | """Get the custom setuptools/distutils subclasses used by Versioneer.""" 1485 | if "versioneer" in sys.modules: 1486 | del sys.modules["versioneer"] 1487 | # this fixes the "python setup.py develop" case (also 'install' and 1488 | # 'easy_install .'), in which subdependencies of the main project are 1489 | # built (using setup.py bdist_egg) in the same python process. Assume 1490 | # a main project A and a dependency B, which use different versions 1491 | # of Versioneer. A's setup.py imports A's Versioneer, leaving it in 1492 | # sys.modules by the time B's setup.py is executed, causing B to run 1493 | # with the wrong versioneer. Setuptools wraps the sub-dep builds in a 1494 | # sandbox that restores sys.modules to it's pre-build state, so the 1495 | # parent is protected against the child's "import versioneer". By 1496 | # removing ourselves from sys.modules here, before the child build 1497 | # happens, we protect the child from the parent's versioneer too. 1498 | # Also see https://github.com/warner/python-versioneer/issues/52 1499 | 1500 | cmds = {} 1501 | 1502 | # we add "version" to both distutils and setuptools 1503 | from distutils.core import Command 1504 | 1505 | class cmd_version(Command): 1506 | description = "report generated version string" 1507 | user_options = [] 1508 | boolean_options = [] 1509 | 1510 | def initialize_options(self): 1511 | pass 1512 | 1513 | def finalize_options(self): 1514 | pass 1515 | 1516 | def run(self): 1517 | vers = get_versions(verbose=True) 1518 | print("Version: %s" % vers["version"]) 1519 | print(" full-revisionid: %s" % vers.get("full-revisionid")) 1520 | print(" dirty: %s" % vers.get("dirty")) 1521 | print(" date: %s" % vers.get("date")) 1522 | if vers["error"]: 1523 | print(" error: %s" % vers["error"]) 1524 | cmds["version"] = cmd_version 1525 | 1526 | # we override "build_py" in both distutils and setuptools 1527 | # 1528 | # most invocation pathways end up running build_py: 1529 | # distutils/build -> build_py 1530 | # distutils/install -> distutils/build ->.. 1531 | # setuptools/bdist_wheel -> distutils/install ->.. 1532 | # setuptools/bdist_egg -> distutils/install_lib -> build_py 1533 | # setuptools/install -> bdist_egg ->.. 1534 | # setuptools/develop -> ? 1535 | # pip install: 1536 | # copies source tree to a tempdir before running egg_info/etc 1537 | # if .git isn't copied too, 'git describe' will fail 1538 | # then does setup.py bdist_wheel, or sometimes setup.py install 1539 | # setup.py egg_info -> ? 1540 | 1541 | # we override different "build_py" commands for both environments 1542 | if "setuptools" in sys.modules: 1543 | from setuptools.command.build_py import build_py as _build_py 1544 | else: 1545 | from distutils.command.build_py import build_py as _build_py 1546 | 1547 | class cmd_build_py(_build_py): 1548 | def run(self): 1549 | root = get_root() 1550 | cfg = get_config_from_root(root) 1551 | versions = get_versions() 1552 | _build_py.run(self) 1553 | # now locate _version.py in the new build/ directory and replace 1554 | # it with an updated value 1555 | if cfg.versionfile_build: 1556 | target_versionfile = os.path.join(self.build_lib, 1557 | cfg.versionfile_build) 1558 | print("UPDATING %s" % target_versionfile) 1559 | write_to_version_file(target_versionfile, versions) 1560 | cmds["build_py"] = cmd_build_py 1561 | 1562 | if "cx_Freeze" in sys.modules: # cx_freeze enabled? 1563 | from cx_Freeze.dist import build_exe as _build_exe 1564 | # nczeczulin reports that py2exe won't like the pep440-style string 1565 | # as FILEVERSION, but it can be used for PRODUCTVERSION, e.g. 1566 | # setup(console=[{ 1567 | # "version": versioneer.get_version().split("+", 1)[0], # FILEVERSION 1568 | # "product_version": versioneer.get_version(), 1569 | # ... 1570 | 1571 | class cmd_build_exe(_build_exe): 1572 | def run(self): 1573 | root = get_root() 1574 | cfg = get_config_from_root(root) 1575 | versions = get_versions() 1576 | target_versionfile = cfg.versionfile_source 1577 | print("UPDATING %s" % target_versionfile) 1578 | write_to_version_file(target_versionfile, versions) 1579 | 1580 | _build_exe.run(self) 1581 | os.unlink(target_versionfile) 1582 | with open(cfg.versionfile_source, "w") as f: 1583 | LONG = LONG_VERSION_PY[cfg.VCS] 1584 | f.write(LONG % 1585 | {"DOLLAR": "$", 1586 | "STYLE": cfg.style, 1587 | "TAG_PREFIX": cfg.tag_prefix, 1588 | "PARENTDIR_PREFIX": cfg.parentdir_prefix, 1589 | "VERSIONFILE_SOURCE": cfg.versionfile_source, 1590 | }) 1591 | cmds["build_exe"] = cmd_build_exe 1592 | del cmds["build_py"] 1593 | 1594 | if 'py2exe' in sys.modules: # py2exe enabled? 1595 | try: 1596 | from py2exe.distutils_buildexe import py2exe as _py2exe # py3 1597 | except ImportError: 1598 | from py2exe.build_exe import py2exe as _py2exe # py2 1599 | 1600 | class cmd_py2exe(_py2exe): 1601 | def run(self): 1602 | root = get_root() 1603 | cfg = get_config_from_root(root) 1604 | versions = get_versions() 1605 | target_versionfile = cfg.versionfile_source 1606 | print("UPDATING %s" % target_versionfile) 1607 | write_to_version_file(target_versionfile, versions) 1608 | 1609 | _py2exe.run(self) 1610 | os.unlink(target_versionfile) 1611 | with open(cfg.versionfile_source, "w") as f: 1612 | LONG = LONG_VERSION_PY[cfg.VCS] 1613 | f.write(LONG % 1614 | {"DOLLAR": "$", 1615 | "STYLE": cfg.style, 1616 | "TAG_PREFIX": cfg.tag_prefix, 1617 | "PARENTDIR_PREFIX": cfg.parentdir_prefix, 1618 | "VERSIONFILE_SOURCE": cfg.versionfile_source, 1619 | }) 1620 | cmds["py2exe"] = cmd_py2exe 1621 | 1622 | # we override different "sdist" commands for both environments 1623 | if "setuptools" in sys.modules: 1624 | from setuptools.command.sdist import sdist as _sdist 1625 | else: 1626 | from distutils.command.sdist import sdist as _sdist 1627 | 1628 | class cmd_sdist(_sdist): 1629 | def run(self): 1630 | versions = get_versions() 1631 | self._versioneer_generated_versions = versions 1632 | # unless we update this, the command will keep using the old 1633 | # version 1634 | self.distribution.metadata.version = versions["version"] 1635 | return _sdist.run(self) 1636 | 1637 | def make_release_tree(self, base_dir, files): 1638 | root = get_root() 1639 | cfg = get_config_from_root(root) 1640 | _sdist.make_release_tree(self, base_dir, files) 1641 | # now locate _version.py in the new base_dir directory 1642 | # (remembering that it may be a hardlink) and replace it with an 1643 | # updated value 1644 | target_versionfile = os.path.join(base_dir, cfg.versionfile_source) 1645 | print("UPDATING %s" % target_versionfile) 1646 | write_to_version_file(target_versionfile, 1647 | self._versioneer_generated_versions) 1648 | cmds["sdist"] = cmd_sdist 1649 | 1650 | return cmds 1651 | 1652 | 1653 | CONFIG_ERROR = """ 1654 | setup.cfg is missing the necessary Versioneer configuration. You need 1655 | a section like: 1656 | 1657 | [versioneer] 1658 | VCS = git 1659 | style = pep440 1660 | versionfile_source = src/myproject/_version.py 1661 | versionfile_build = myproject/_version.py 1662 | tag_prefix = 1663 | parentdir_prefix = myproject- 1664 | 1665 | You will also need to edit your setup.py to use the results: 1666 | 1667 | import versioneer 1668 | setup(version=versioneer.get_version(), 1669 | cmdclass=versioneer.get_cmdclass(), ...) 1670 | 1671 | Please read the docstring in ./versioneer.py for configuration instructions, 1672 | edit setup.cfg, and re-run the installer or 'python versioneer.py setup'. 1673 | """ 1674 | 1675 | SAMPLE_CONFIG = """ 1676 | # See the docstring in versioneer.py for instructions. Note that you must 1677 | # re-run 'versioneer.py setup' after changing this section, and commit the 1678 | # resulting files. 1679 | 1680 | [versioneer] 1681 | #VCS = git 1682 | #style = pep440 1683 | #versionfile_source = 1684 | #versionfile_build = 1685 | #tag_prefix = 1686 | #parentdir_prefix = 1687 | 1688 | """ 1689 | 1690 | INIT_PY_SNIPPET = """ 1691 | from ._version import get_versions 1692 | __version__ = get_versions()['version'] 1693 | del get_versions 1694 | """ 1695 | 1696 | 1697 | def do_setup(): 1698 | """Main VCS-independent setup function for installing Versioneer.""" 1699 | root = get_root() 1700 | try: 1701 | cfg = get_config_from_root(root) 1702 | except (EnvironmentError, configparser.NoSectionError, 1703 | configparser.NoOptionError) as e: 1704 | if isinstance(e, (EnvironmentError, configparser.NoSectionError)): 1705 | print("Adding sample versioneer config to setup.cfg", 1706 | file=sys.stderr) 1707 | with open(os.path.join(root, "setup.cfg"), "a") as f: 1708 | f.write(SAMPLE_CONFIG) 1709 | print(CONFIG_ERROR, file=sys.stderr) 1710 | return 1 1711 | 1712 | print(" creating %s" % cfg.versionfile_source) 1713 | with open(cfg.versionfile_source, "w") as f: 1714 | LONG = LONG_VERSION_PY[cfg.VCS] 1715 | f.write(LONG % {"DOLLAR": "$", 1716 | "STYLE": cfg.style, 1717 | "TAG_PREFIX": cfg.tag_prefix, 1718 | "PARENTDIR_PREFIX": cfg.parentdir_prefix, 1719 | "VERSIONFILE_SOURCE": cfg.versionfile_source, 1720 | }) 1721 | 1722 | ipy = os.path.join(os.path.dirname(cfg.versionfile_source), 1723 | "__init__.py") 1724 | if os.path.exists(ipy): 1725 | try: 1726 | with open(ipy, "r") as f: 1727 | old = f.read() 1728 | except EnvironmentError: 1729 | old = "" 1730 | if INIT_PY_SNIPPET not in old: 1731 | print(" appending to %s" % ipy) 1732 | with open(ipy, "a") as f: 1733 | f.write(INIT_PY_SNIPPET) 1734 | else: 1735 | print(" %s unmodified" % ipy) 1736 | else: 1737 | print(" %s doesn't exist, ok" % ipy) 1738 | ipy = None 1739 | 1740 | # Make sure both the top-level "versioneer.py" and versionfile_source 1741 | # (PKG/_version.py, used by runtime code) are in MANIFEST.in, so 1742 | # they'll be copied into source distributions. Pip won't be able to 1743 | # install the package without this. 1744 | manifest_in = os.path.join(root, "MANIFEST.in") 1745 | simple_includes = set() 1746 | try: 1747 | with open(manifest_in, "r") as f: 1748 | for line in f: 1749 | if line.startswith("include "): 1750 | for include in line.split()[1:]: 1751 | simple_includes.add(include) 1752 | except EnvironmentError: 1753 | pass 1754 | # That doesn't cover everything MANIFEST.in can do 1755 | # (http://docs.python.org/2/distutils/sourcedist.html#commands), so 1756 | # it might give some false negatives. Appending redundant 'include' 1757 | # lines is safe, though. 1758 | if "versioneer.py" not in simple_includes: 1759 | print(" appending 'versioneer.py' to MANIFEST.in") 1760 | with open(manifest_in, "a") as f: 1761 | f.write("include versioneer.py\n") 1762 | else: 1763 | print(" 'versioneer.py' already in MANIFEST.in") 1764 | if cfg.versionfile_source not in simple_includes: 1765 | print(" appending versionfile_source ('%s') to MANIFEST.in" % 1766 | cfg.versionfile_source) 1767 | with open(manifest_in, "a") as f: 1768 | f.write("include %s\n" % cfg.versionfile_source) 1769 | else: 1770 | print(" versionfile_source already in MANIFEST.in") 1771 | 1772 | # Make VCS-specific changes. For git, this means creating/changing 1773 | # .gitattributes to mark _version.py for export-subst keyword 1774 | # substitution. 1775 | do_vcs_install(manifest_in, cfg.versionfile_source, ipy) 1776 | return 0 1777 | 1778 | 1779 | def scan_setup_py(): 1780 | """Validate the contents of setup.py against Versioneer's expectations.""" 1781 | found = set() 1782 | setters = False 1783 | errors = 0 1784 | with open("setup.py", "r") as f: 1785 | for line in f.readlines(): 1786 | if "import versioneer" in line: 1787 | found.add("import") 1788 | if "versioneer.get_cmdclass()" in line: 1789 | found.add("cmdclass") 1790 | if "versioneer.get_version()" in line: 1791 | found.add("get_version") 1792 | if "versioneer.VCS" in line: 1793 | setters = True 1794 | if "versioneer.versionfile_source" in line: 1795 | setters = True 1796 | if len(found) != 3: 1797 | print("") 1798 | print("Your setup.py appears to be missing some important items") 1799 | print("(but I might be wrong). Please make sure it has something") 1800 | print("roughly like the following:") 1801 | print("") 1802 | print(" import versioneer") 1803 | print(" setup( version=versioneer.get_version(),") 1804 | print(" cmdclass=versioneer.get_cmdclass(), ...)") 1805 | print("") 1806 | errors += 1 1807 | if setters: 1808 | print("You should remove lines like 'versioneer.VCS = ' and") 1809 | print("'versioneer.versionfile_source = ' . This configuration") 1810 | print("now lives in setup.cfg, and should be removed from setup.py") 1811 | print("") 1812 | errors += 1 1813 | return errors 1814 | 1815 | 1816 | if __name__ == "__main__": 1817 | cmd = sys.argv[1] 1818 | if cmd == "setup": 1819 | errors = do_setup() 1820 | errors += scan_setup_py() 1821 | if errors: 1822 | sys.exit(1) 1823 | --------------------------------------------------------------------------------