diff --git a/.bandit b/.bandit new file mode 100644 index 00000000..c9a5f28b --- /dev/null +++ b/.bandit @@ -0,0 +1,3 @@ +[bandit] +skips: B506 +exclude: pyorbital/tests diff --git a/.github/ISSUE_TEMPLATE.md b/.github/ISSUE_TEMPLATE.md index 18c258ad..7831c592 100644 --- a/.github/ISSUE_TEMPLATE.md +++ b/.github/ISSUE_TEMPLATE.md @@ -6,7 +6,7 @@ ``` #### Problem description -[this should also explain **why** the current behaviour is a problem and why the +[this should also explain **why** the current behaviour is a problem and why the expected output is a better solution.] #### Expected Output diff --git a/.github/workflows/deploy-sdist.yaml b/.github/workflows/deploy-sdist.yaml index 23fa0ff8..410bb00c 100644 --- a/.github/workflows/deploy-sdist.yaml +++ b/.github/workflows/deploy-sdist.yaml @@ -22,4 +22,4 @@ jobs: uses: pypa/gh-action-pypi-publish@v1.12.2 with: user: __token__ - password: ${{ secrets.pypi_password }} \ No newline at end of file + password: ${{ secrets.pypi_password }} diff --git a/.gitignore b/.gitignore index 7a2d2e6d..f9221bba 100644 --- a/.gitignore +++ b/.gitignore @@ -39,3 +39,5 @@ nosetests.xml # rope .ropeproject + +pyorbital/version.py diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index 68fd7028..ae519516 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -1,8 +1,42 @@ exclude: '^$' fail_fast: false repos: -- repo: https://github.com/pre-commit/pre-commit-hooks - rev: v2.2.3 + - repo: https://github.com/astral-sh/ruff-pre-commit + # Ruff version. + rev: 'v0.7.2' hooks: - - id: flake8 - additional_dependencies: [flake8-docstrings, flake8-debugger, flake8-bugbear] + - id: ruff + - repo: https://github.com/pre-commit/pre-commit-hooks + rev: v5.0.0 + hooks: + - id: trailing-whitespace + exclude: pyorbital/tests/SGP4-VER.TLE + - id: end-of-file-fixer + - id: check-yaml + args: [--unsafe] + - repo: https://github.com/PyCQA/bandit + rev: '1.7.10' # Update me! + hooks: + - id: bandit + args: [--ini, .bandit] + - repo: https://github.com/pre-commit/mirrors-mypy + rev: 'v1.13.0' # Use the sha / tag you want to point at + hooks: + - id: mypy + additional_dependencies: + - types-docutils + - types-setuptools + - types-PyYAML + - types-requests + - types-pytz + args: ["--python-version", "3.10", "--ignore-missing-imports"] + - repo: https://github.com/pycqa/isort + rev: 5.13.2 + hooks: + - id: isort + language_version: python3 +ci: + # To trigger manually, comment on a pull request with "pre-commit.ci autofix" + autofix_prs: false + autoupdate_schedule: "monthly" + skip: [bandit] diff --git a/.stickler.yml b/.stickler.yml deleted file mode 100644 index 1c51f637..00000000 --- a/.stickler.yml +++ /dev/null @@ -1,4 +0,0 @@ -linters: - flake8: - python: 3 - config: setup.cfg diff --git a/CHANGELOG.md b/CHANGELOG.md index 7f013ddb..67f97225 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -163,7 +163,7 @@ In this release 5 pull requests were closed. ### Issues Closed * [Issue 63](https://github.com/pytroll/pyorbital/issues/63) - Runtime error in get_next_passes ([PR 64](https://github.com/pytroll/pyorbital/pull/64)) -* [Issue 62](https://github.com/pytroll/pyorbital/issues/62) - can this tool run +* [Issue 62](https://github.com/pytroll/pyorbital/issues/62) - can this tool run * [Issue 22](https://github.com/pytroll/pyorbital/issues/22) - get_next_passes returns max-elevation-time time not between rise & fall time ([PR 76](https://github.com/pytroll/pyorbital/pull/76)) In this release 3 issues were closed. diff --git a/README.md b/README.md index cc3102a6..87fe885e 100644 --- a/README.md +++ b/README.md @@ -12,4 +12,3 @@ This is the Pyorbital, a Python package for computing orbital parameters from TL files, and making various astronomical computations. It is part of the Pytroll project: http://pytroll.org - diff --git a/changelog.rst b/changelog.rst index 1c588c41..9824089e 100644 --- a/changelog.rst +++ b/changelog.rst @@ -531,6 +531,3 @@ v0.1.0 (2011-10-03) - Cleanup of astronomy file. [Martin Raspaud] - Added a readme file. [Martin Raspaud] - Added astronomy.py file. [Martin Raspaud] - - - diff --git a/continuous_integration/environment.yaml b/continuous_integration/environment.yaml index 8ddf082c..ab1473c9 100644 --- a/continuous_integration/environment.yaml +++ b/continuous_integration/environment.yaml @@ -24,6 +24,7 @@ dependencies: - pytest - pytest-cov - fsspec + - defusedxml - pip - pip: - trollsift diff --git a/doc/source/conf.py b/doc/source/conf.py index ab8067a5..f3bba78e 100644 --- a/doc/source/conf.py +++ b/doc/source/conf.py @@ -12,16 +12,17 @@ # serve to show the default. """Configurations for sphinx based documentation.""" -import sys +import datetime as dt import os +import sys # If extensions (or modules to document with autodoc) are in another directory, # add these directories to sys.path here. If the directory is relative to the # documentation root, use os.path.abspath to make it absolute, like shown here. -sys.path.insert(0, os.path.abspath('../../')) -sys.path.insert(0, os.path.abspath('../../pyorbital')) -from pyorbital import __version__ # noqa +sys.path.insert(0, os.path.abspath("../../")) +sys.path.insert(0, os.path.abspath("../../pyorbital")) +from pyorbital.version import __version__ # noqa # -- General configuration ----------------------------------------------------- @@ -30,30 +31,32 @@ # Add any Sphinx extension module names here, as strings. They can be extensions # coming with Sphinx (named 'sphinx.ext.*') or your custom ones. -extensions = ['sphinx.ext.autodoc', 'sphinx.ext.doctest', 'sphinx.ext.coverage', 'sphinx.ext.napoleon'] +extensions = ["sphinx.ext.autodoc", "sphinx.ext.doctest", "sphinx.ext.coverage", "sphinx.ext.napoleon"] # Add any paths that contain templates here, relative to this directory. -templates_path = ['.templates'] +templates_path = [".templates"] # The suffix of source filenames. -source_suffix = '.rst' +source_suffix = ".rst" # The encoding of source files. # #source_encoding = 'utf-8-sig' # The master toctree document. -master_doc = 'index' +master_doc = "index" # General information about the project. -project = u'pyorbital' -copyright = u'2012-2023, The Pytroll crew' +project = u"pyorbital" +copyright = u"2012, 2024-{}, The PyTroll Team".format(dt.datetime.utcnow().strftime("%Y")) # noqa: A001 + + # The version info for the project you're documenting, acts as replacement for # |version| and |release|, also used in various other places throughout the # built documents. # # The short X.Y version. -version = __version__.split('+')[0] +version = __version__.split("+")[0] # The full version, including alpha/beta/rc tags. release = __version__ @@ -69,7 +72,7 @@ # List of patterns, relative to source directory, that match files and # directories to ignore when looking for source files. -exclude_patterns = [] +# exclude_patterns = [] # The reST default role (used for this markup: `text`) to use for all documents. # #default_role = None @@ -86,7 +89,7 @@ # show_authors = False # The name of the Pygments (syntax highlighting) style to use. -pygments_style = 'sphinx' +pygments_style = "sphinx" # A list of ignored prefixes for module index sorting. # #modindex_common_prefix = [] @@ -180,7 +183,7 @@ # #html_file_suffix = None # Output file base name for HTML help builder. -htmlhelp_basename = 'pyorbitaldoc' +htmlhelp_basename = "pyorbitaldoc" # -- Options for LaTeX output -------------------------------------------------- @@ -194,8 +197,8 @@ # Grouping the document tree into LaTeX files. List of tuples # (source start file, target name, title, author, documentclass [howto/manual]). latex_documents = [ - ('index', 'pyorbital.tex', u'pyorbital Documentation', - u'The Pytroll crew', 'manual'), + ("index", "pyorbital.tex", u"pyorbital Documentation", + u"The Pytroll crew", "manual"), ] # The name of an image file (relative to this directory) to place at the top of @@ -227,6 +230,6 @@ # One entry per manual page. List of tuples # (source start file, name, description, authors, manual section). man_pages = [ - ('index', 'pyorbital', u'pyorbital Documentation', - [u'The Pytroll crew'], 1) + ("index", "pyorbital", u"pyorbital Documentation", + [u"The Pytroll crew"], 1) ] diff --git a/doc/source/index.rst b/doc/source/index.rst index 45aee1ed..d93bd17b 100644 --- a/doc/source/index.rst +++ b/doc/source/index.rst @@ -15,7 +15,7 @@ the conda-forge conda channel. To install from PyPI in an existing environment: .. code-block:: bash pip install pyorbital - + Or in an existing conda-based environment: .. code-block:: bash @@ -31,7 +31,7 @@ from the latest in-development version on GitHub you can run: .. code-block:: bash pip install git+https://github.com/pytroll/pyorbital.git - + However, if you instead want to edit the source code and see the changes reflected when you run the code you can clone the git repository and install it in "editable" mode: diff --git a/pyorbital/__init__.py b/pyorbital/__init__.py index c72b692c..01cfa348 100644 --- a/pyorbital/__init__.py +++ b/pyorbital/__init__.py @@ -1,10 +1,6 @@ # -*- coding: utf-8 -*- -# Copyright (c) 2017 - -# Author(s): - -# Martin Raspaud +# Copyright (c) 2017-2024 Pytroll Community # This program is free software: you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by @@ -19,14 +15,16 @@ # You should have received a copy of the GNU General Public License # along with this program. If not, see . +"""Package file.""" + import numpy as np -from .version import get_versions -__version__ = get_versions()['version'] -del get_versions + +from pyorbital.version import __version__ # noqa def dt2np(utc_time): + """Convert datetime to numpy datetime64 object.""" try: return np.datetime64(utc_time) except ValueError: - return utc_time.astype('datetime64[ns]') + return utc_time.astype("datetime64[ns]") diff --git a/pyorbital/astronomy.py b/pyorbital/astronomy.py index 881e97d2..27935538 100644 --- a/pyorbital/astronomy.py +++ b/pyorbital/astronomy.py @@ -1,7 +1,7 @@ #!/usr/bin/env python # -*- coding: utf-8 -*- # -# Copyright (c) 2011, 2013 +# Copyright (c) 2011, 2013, 2024 # # Author(s): # @@ -36,7 +36,6 @@ produce scalar outputs. """ -import datetime import numpy as np @@ -48,23 +47,20 @@ def jdays2000(utc_time): - """Get the days since year 2000. - """ - return _days(dt2np(utc_time) - np.datetime64('2000-01-01T12:00')) + """Get the days since year 2000.""" + return _days(dt2np(utc_time) - np.datetime64("2000-01-01T12:00")) def jdays(utc_time): - """Get the julian day of *utc_time*. - """ + """Get the julian day of *utc_time*.""" return jdays2000(utc_time) + 2451545.0 def _days(dt): - """Get the days (floating point) from *d_t*. - """ + """Get the days (floating point) from *d_t*.""" if hasattr(dt, "shape"): dt = np.asanyarray(dt, dtype=np.timedelta64) - return dt / np.timedelta64(1, 'D') + return dt / np.timedelta64(1, "D") def gmst(utc_time): @@ -81,14 +77,16 @@ def gmst(utc_time): def _lmst(utc_time, longitude): """Local mean sidereal time, computed from *utc_time* and *longitude*. - In radians. + + utc_time: The UTC time as a datetime.datetime object. + Logitude: The longitude in radians. + Returns: local mean sideral time in radians. """ return gmst(utc_time) + longitude def sun_ecliptic_longitude(utc_time): - """Ecliptic longitude of the sun at *utc_time*. - """ + """Ecliptic longitude of the sun at *utc_time*.""" jdate = jdays2000(utc_time) / 36525.0 # mean anomaly, rad m_a = np.deg2rad(357.52910 + @@ -105,8 +103,7 @@ def sun_ecliptic_longitude(utc_time): def sun_ra_dec(utc_time): - """Right ascension and declination of the sun at *utc_time*. - """ + """Right ascension and declination of the sun at *utc_time*.""" jdate = jdays2000(utc_time) / 36525.0 eps = np.deg2rad(23.0 + 26.0 / 60.0 + 21.448 / 3600.0 - (46.8150 * jdate + 0.00059 * jdate * jdate - @@ -124,9 +121,12 @@ def sun_ra_dec(utc_time): def _local_hour_angle(utc_time, longitude, right_ascension): - """Hour angle at *utc_time* for the given *longitude* and - *right_ascension* - longitude in radians + """Derive the hour angle at *utc_time* for the given *longitude* and *right_ascension*. + + utc_time: datetime.datetime instance of the UTC time + longitude: Longitude in radians. + right_ascension: The right ascension in radians. + Returns: Hour angle in radians. """ return _lmst(utc_time, longitude) - right_ascension @@ -152,9 +152,12 @@ def get_alt_az(utc_time, lon, lat): def cos_zen(utc_time, lon, lat): - """Cosine of the sun-zenith angle for *lon*, *lat* at *utc_time*. + """Derive the cosine of the sun-zenith angle for *lon*, *lat* at *utc_time*. + utc_time: datetime.datetime instance of the UTC time - lon and lat in degrees. + lon: Longitude in degrees + lat: Latitude in degrees. + Returns: Cosine of the sun zenith angle. """ lon = np.deg2rad(lon) lat = np.deg2rad(lat) @@ -169,8 +172,9 @@ def cos_zen(utc_time, lon, lat): def sun_zenith_angle(utc_time, lon, lat): """Sun-zenith angle for *lon*, *lat* at *utc_time*. + lon,lat in degrees. - The angle returned is given in degrees + The sun zenith angle returned is in degrees. """ sza = np.rad2deg(np.arccos(cos_zen(utc_time, lon, lat))) if not isinstance(lon, float): @@ -179,8 +183,7 @@ def sun_zenith_angle(utc_time, lon, lat): def sun_earth_distance_correction(utc_time): - """Calculate the sun earth distance correction, relative to 1 AU. - """ + """Calculate the sun earth distance correction, relative to 1 AU.""" # Computation according to # https://web.archive.org/web/20150117190838/http://curious.astro.cornell.edu/question.php?number=582 # with @@ -208,7 +211,6 @@ def observer_position(utc_time, lon, lat, alt): http://celestrak.com/columns/v02n03/ """ - lon = np.deg2rad(lon) lat = np.deg2rad(lat) diff --git a/pyorbital/check_platform.py b/pyorbital/check_platform.py index 441a5e59..b292ed71 100755 --- a/pyorbital/check_platform.py +++ b/pyorbital/check_platform.py @@ -22,12 +22,13 @@ import argparse import logging -from pyorbital.tlefile import check_is_platform_supported + from pyorbital.logger import logging_on +from pyorbital.tlefile import check_is_platform_supported if __name__ == "__main__": parser = argparse.ArgumentParser( - description='Check if a satellite is supported.') + description="Check if a satellite is supported.") parser.add_argument("-s", "--satellite", help=("Name of the Satellite [in upper case] - following WMO Oscar naming."), default=None, diff --git a/bin/fetch_tles.py b/pyorbital/fetch_tles.py similarity index 71% rename from bin/fetch_tles.py rename to pyorbital/fetch_tles.py index e6461673..fd8805ae 100755 --- a/bin/fetch_tles.py +++ b/pyorbital/fetch_tles.py @@ -2,35 +2,36 @@ """Script to download and store satellite TLE data.""" -import sys import logging import logging.config +import sys import yaml + from pyorbital.tlefile import Downloader, SQLiteTLE def read_config(config_fname): """Read and parse config file.""" - with open(config_fname, 'r') as fid: + with open(config_fname, "r") as fid: config = yaml.load(fid, Loader=yaml.SafeLoader) return config -def main(): +def run(): """Run TLE downloader.""" config = read_config(sys.argv[1]) - if 'logging' in config: - logging.config.dictConfig(config['logging']) + if "logging" in config: + logging.config.dictConfig(config["logging"]) else: logging.basicConfig(level=logging.INFO) downloader = Downloader(config) - db = SQLiteTLE(config['database']['path'], config['platforms'], - config['text_writer']) + db = SQLiteTLE(config["database"]["path"], config["platforms"], + config["text_writer"]) logging.info("Start downloading TLEs") - for dl_ in config['downloaders']: + for dl_ in config["downloaders"]: fetcher = getattr(downloader, dl_) tles = fetcher() if isinstance(tles, dict): @@ -38,16 +39,12 @@ def main(): for tle in tles[source]: db.update_db(tle, source) else: - source = 'file' + source = "file" if "spacetrack" in dl_: - source = 'spacetrack' + source = "spacetrack" for tle in tles: db.update_db(tle, source) db.write_tle_txt() db.close() logging.info("TLE downloading finished") - - -if __name__ == "__main__": - main() diff --git a/pyorbital/geoloc.py b/pyorbital/geoloc.py index 00f10a52..60a324c8 100644 --- a/pyorbital/geoloc.py +++ b/pyorbital/geoloc.py @@ -1,7 +1,7 @@ #!/usr/bin/env python # -*- coding: utf-8 -*- -# Copyright (c) 2011 - 2019 Pytroll Community +# Copyright (c) 2011 - 2019, 2024 Pytroll Community # Author(s): @@ -21,8 +21,7 @@ # You should have received a copy of the GNU General Public License # along with this program. If not, see . -"""Module to compute geolocalization of a satellite scene. -""" +"""Module to compute geolocalization of a satellite scene.""" # TODO: # - Attitude correction @@ -31,13 +30,13 @@ # - test !!! from __future__ import print_function + import numpy as np # DIRTY STUFF. Needed the get_lonlatalt function to work on pos directly if # we want to print out lonlats in the end. from pyorbital import astronomy -from pyorbital.orbital import XKMPER, F -from pyorbital.orbital import Orbital +from pyorbital.orbital import XKMPER, F, Orbital A = 6378.137 # WGS84 Equatorial radius (km) B = 6356.75231414 # km, GRS80 @@ -45,6 +44,7 @@ def geodetic_lat(point, a=A, b=B): + """Get the Geodetic latitude of a point.""" x, y, z = point r = np.sqrt(x * x + y * y) geoc_lat = np.arctan2(z, r) @@ -84,8 +84,9 @@ class ScanGeometry(object): """ def __init__(self, fovs, times, attitude=(0, 0, 0)): + """Initialize the class.""" self.fovs = np.array(fovs) - self._times = np.array(times) * np.timedelta64(1000000000, 'ns') + self._times = np.array(times) * np.timedelta64(1000000000, "ns") self.attitude = attitude def vectors(self, pos, vel, roll=0.0, pitch=0.0, yaw=0.0): @@ -120,6 +121,7 @@ def vectors(self, pos, vel, roll=0.0, pitch=0.0, yaw=0.0): return qrotate(xy_rotated, nadir, yaw) def times(self, start_of_scan): + """Return an array with the times of each scan line.""" # tds = [timedelta(seconds=i) for i in self._times] # tds = self._times.astype('timedelta64[us]') try: @@ -129,12 +131,15 @@ def times(self, start_of_scan): class Quaternion(object): + """Some class, that I don't know what is doing...""" def __init__(self, scalar, vector): + """Initialize the class.""" self.__x, self.__y, self.__z = vector.reshape((3, -1)) self.__w = scalar.ravel() def rotation_matrix(self): + """Get the rotation matrix.""" x, y, z, w = self.__x, self.__y, self.__z, self.__w zero = np.zeros_like(x) return np.array( @@ -240,27 +245,28 @@ def compute_pixels(orb, sgeom, times, rpy=(0.0, 0.0, 0.0)): def norm(v): + """Return the norm of the vector *v*.""" return np.sqrt(np.dot(v, v.conj())) def mnorm(m, axis=None): - """norm of a matrix of vectors stacked along the *axis* dimension.""" + """Norm of a matrix of vectors stacked along the *axis* dimension.""" if axis is None: axis = np.ndim(m) - 1 return np.sqrt((m**2).sum(axis)) def vnorm(m): - """norms of a matrix of column vectors.""" + """Norms of a matrix of column vectors.""" return np.sqrt((m**2).sum(0)) def hnorm(m): - """norms of a matrix of row vectors.""" + """Norms of a matrix of row vectors.""" return np.sqrt((m**2).sum(1)) -if __name__ == '__main__': +if __name__ == "__main__": # NOAA 18 (from the 2011-10-12, 16:55 utc) # 1 28654U 05018A 11284.35271227 .00000478 00000-0 28778-3 0 9246 # 2 28654 99.0096 235.8581 0014859 135.4286 224.8087 14.11526826329313 diff --git a/pyorbital/geoloc_example.py b/pyorbital/geoloc_example.py index c3c12965..1b8b5671 100644 --- a/pyorbital/geoloc_example.py +++ b/pyorbital/geoloc_example.py @@ -1,7 +1,7 @@ #!/usr/bin/env python # -*- coding: utf-8 -*- -# Copyright (c) 2013 Martin Raspaud +# Copyright (c) 2013, 2024 Martin Raspaud # Author(s): @@ -20,14 +20,15 @@ # You should have received a copy of the GNU General Public License # along with this program. If not, see . -"""Simple usage for geoloc. -""" +"""Simple usage for geoloc.""" -import numpy as np from datetime import datetime -from pyorbital.geoloc import ScanGeometry, compute_pixels, get_lonlatalt -from mpl_toolkits.basemap import Basemap + import matplotlib.pyplot as plt +import numpy as np +from mpl_toolkits.basemap import Basemap + +from pyorbital.geoloc import ScanGeometry, compute_pixels, get_lonlatalt # Couple of example Two Line Elements tle1 = "1 33591U 09005A 12345.45213434 .00000391 00000-0 24004-3 0 6113" @@ -72,14 +73,14 @@ print(pos_time) # Plot the result -m = Basemap(projection='stere', llcrnrlat=24, urcrnrlat=70, llcrnrlon=-25, urcrnrlon=120, - lat_ts=58, lat_0=58, lon_0=14, resolution='l') +m = Basemap(projection="stere", llcrnrlat=24, urcrnrlat=70, llcrnrlon=-25, urcrnrlon=120, + lat_ts=58, lat_0=58, lon_0=14, resolution="l") # convert and plot the predicted pixels in red x, y = m(pos_time[0], pos_time[1]) -p1 = m.plot(x, y, marker='+', color='red', markerfacecolor='red', markeredgecolor='red', markersize=1, markevery=1, +p1 = m.plot(x, y, marker="+", color="red", markerfacecolor="red", markeredgecolor="red", markersize=1, markevery=1, zorder=4, linewidth=0.0) -m.fillcontinents(color='0.85', lake_color=None, zorder=3) +m.fillcontinents(color="0.85", lake_color=None, zorder=3) m.drawparallels(np.arange(-90., 90., 5.), labels=[1, 0, 1, 0], fontsize=10, dashes=[1, 0], color=[0.8, 0.8, 0.8], zorder=1) m.drawmeridians(np.arange(-180., 180., 5.), labels=[0, 1, 0, 1], fontsize=10, dashes=[1, 0], diff --git a/pyorbital/geoloc_instrument_definitions.py b/pyorbital/geoloc_instrument_definitions.py index 465ebc63..3c8f311d 100644 --- a/pyorbital/geoloc_instrument_definitions.py +++ b/pyorbital/geoloc_instrument_definitions.py @@ -1,7 +1,7 @@ #!/usr/bin/env python # -*- coding: utf-8 -*- -# Copyright (c) 2013 - 2021 PyTroll Community +# Copyright (c) 2013 - 2021, 2024 PyTroll Community # Author(s): @@ -41,7 +41,6 @@ from pyorbital.geoloc import ScanGeometry - ################################################################ # # AVHRR @@ -78,7 +77,7 @@ def avhrr(scans_nb, scan_points, def avhrr_gac(scan_times, scan_points, scan_angle=55.37, frequency=0.5): - """Definition of the avhrr instrument, gac version + """Definition of the avhrr instrument, gac version. Source: NOAA KLM User's Guide, Appendix J http://www.ncdc.noaa.gov/oa/pod-guide/ncdc/docs/klm/html/j/app-j.htm @@ -108,6 +107,7 @@ def avhrr_gac(scan_times, scan_points, def avhrr_all_geom(scans_nb): + """Get all the AVHRR scan points.""" # we take all pixels scan_points = np.arange(2048) return avhrr(scans_nb, scan_points) @@ -119,7 +119,7 @@ def avhrr_all_geom(scans_nb): def avhrr_edge_geom(scans_nb): - # we take only edge pixels + """Getting the AVHRR scan edges only.""" scan_points = np.array([0, 2047]) return avhrr(scans_nb, scan_points) @@ -130,7 +130,7 @@ def avhrr_edge_geom(scans_nb): def avhrr_40_geom(scans_nb): - # we take only every 40th pixel + """Description of the AVHRR scan in terms of every 40th pixel per line.""" scan_points = np.arange(24, 2048, 40) return avhrr(scans_nb, scan_points) @@ -144,6 +144,7 @@ def avhrr_40_geom(scans_nb): def viirs(scans_nb, scan_indices=slice(0, None), chn_pixels=6400, scan_lines=32, scan_step=1): """Describe VIIRS instrument geometry, I-band by default. + VIIRS scans several lines simultaneously (there are 16 detectors for each M-band, 32 detectors for each I-band) so the scan angles (and times) are two-dimensional arrays, contrary to AVHRR for example. @@ -154,9 +155,8 @@ def viirs(scans_nb, scan_indices=slice(0, None), 99 emtpy (excluded) scans """ - entire_width = np.arange(chn_pixels) - scan_points = entire_width[scan_indices].astype('int') + scan_points = entire_width[scan_indices].astype("int") scan_pixels = len(scan_points) # Initial angle 55.84 deg replaced with 56.28 deg found in @@ -198,7 +198,7 @@ def viirs(scans_nb, scan_indices=slice(0, None), def viirs_edge_geom(scans_nb): - # we take only edge pixels + """Definition of the VIIRS scane edges.""" scan_indices = [0, -1] return viirs(scans_nb, scan_indices) @@ -210,7 +210,7 @@ def viirs_edge_geom(scans_nb): ################################################################ def amsua(scans_nb, scan_points=None): - """ Describe AMSU-A instrument geometry + """Describe AMSU-A instrument geometry. Parameters: scans_nb | int - number of scan lines @@ -222,7 +222,6 @@ def amsua(scans_nb, scan_points=None): pyorbital.geoloc.ScanGeometry object """ - scan_len = 30 # 30 samples per scan scan_rate = 8 # single scan, seconds scan_angle = -48.3 # swath, degrees @@ -255,7 +254,7 @@ def amsua(scans_nb, scan_points=None): ################################################################ def mhs(scans_nb, scan_points=None): - """ Describe MHS instrument geometry + """Describe MHS instrument geometry. See: @@ -274,7 +273,6 @@ def mhs(scans_nb, scan_points=None): pyorbital.geoloc.ScanGeometry object """ - scan_len = 90 # 90 samples per scan scan_rate = 8 / 3. # single scan, seconds scan_angle = -49.444 # swath, degrees @@ -332,7 +330,6 @@ def hirs4(scans_nb, scan_points=None): pyorbital.geoloc.ScanGeometry object """ - scan_len = 56 # 56 samples per scan scan_rate = 6.4 # single scan, seconds scan_angle = -49.5 # swath, degrees @@ -363,7 +360,8 @@ def hirs4(scans_nb, scan_points=None): ################################################################ def atms(scans_nb, scan_points=None): - """ Describe ATMS instrument geometry + """Describe ATMS instrument geometry. + See: - https://dtcenter.org/com-GSI/users/docs/presentations/2013_workshop/ @@ -382,7 +380,6 @@ def atms(scans_nb, scan_points=None): pyorbital.geoloc.ScanGeometry object """ - scan_len = 96 # 96 samples per scan scan_rate = 8 / 3. # single scan, seconds scan_angle = -52.7 # swath, degrees @@ -413,7 +410,7 @@ def atms(scans_nb, scan_points=None): ################################################################ def mwhs2(scans_nb, scan_points=None): - """Describe MWHS-2 instrument geometry + """Describe MWHS-2 instrument geometry. The scanning period is 2.667 s. Main beams of the antenna scan over the ob- serving swath (±53.35◦ from nadir) in the cross-track direction at a @@ -434,7 +431,6 @@ def mwhs2(scans_nb, scan_points=None): pyorbital.geoloc.ScanGeometry object """ - scan_len = 98 # 98 samples per scan scan_rate = 8 / 3. # single scan, seconds scan_angle = -53.35 # swath, degrees @@ -485,7 +481,6 @@ def olci(scans_nb, scan_points=None): Source: Sentinel-3 OLCI Coverage https://sentinel.esa.int/web/sentinel/user-guides/sentinel-3-olci/coverage """ - if scan_points is None: scan_len = 4000 # samples per scan scan_points = np.arange(4000) @@ -515,11 +510,11 @@ def olci(scans_nb, scan_points=None): def ascat(scan_nb, scan_points=None): - """ASCAT make two scans one to the left and one to the right of the - sub-satellite track. + """Describing the ASCAT scanning geometry. + make two scans one to the left and one to the right of the sub-satellite + track. """ - if scan_points is None: scan_len = 42 # samples per scan scan_points = np.arange(42) diff --git a/pyorbital/logger.py b/pyorbital/logger.py index b4506643..c3070a7b 100644 --- a/pyorbital/logger.py +++ b/pyorbital/logger.py @@ -38,12 +38,12 @@ def logging_on(level=logging.WARNING): console = logging.StreamHandler() console.setFormatter(logging.Formatter("[%(levelname)s: %(asctime)s :" " %(name)s] %(message)s", - '%Y-%m-%d %H:%M:%S')) + "%Y-%m-%d %H:%M:%S")) console.setLevel(level) - logging.getLogger('').addHandler(console) + logging.getLogger("").addHandler(console) _is_logging_on = True - log = logging.getLogger('') + log = logging.getLogger("") log.setLevel(level) for h in log.handlers: h.setLevel(level) @@ -58,7 +58,7 @@ def emit(self, record): def logging_off(): """Turn logging off.""" - logging.getLogger('').handlers = [NullHandler()] + logging.getLogger("").handlers = [NullHandler()] def get_logger(name): diff --git a/pyorbital/orbital.py b/pyorbital/orbital.py index 07f2e40c..aaf2d0ae 100644 --- a/pyorbital/orbital.py +++ b/pyorbital/orbital.py @@ -27,9 +27,10 @@ import logging import warnings from datetime import datetime, timedelta -import pytz +from functools import partial import numpy as np +import pytz from scipy import optimize from pyorbital import astronomy, dt2np, tlefile @@ -169,7 +170,8 @@ def __str__(self): def get_last_an_time(self, utc_time): """Calculate time of last ascending node relative to the specified time.""" # Propagate backwards to ascending node - dt = np.timedelta64(10, 'm') + dt = np.timedelta64(10, "m") + t_old = np.datetime64(_get_tz_unaware_utctime(utc_time)) t_new = t_old - dt pos0, vel0 = self.get_position(t_old, normalize=False) @@ -298,6 +300,7 @@ def get_orbit_number(self, utc_time, tbus_style=False, as_float=False): """Calculate orbit number at specified time. Args: + utc_time: UTC time as a datetime.datetime object. tbus_style: If True, use TBUS-style orbit numbering (TLE orbit number + 1) as_float: Return a continuous orbit number as float. """ @@ -318,7 +321,7 @@ def get_orbit_number(self, utc_time, tbus_style=False, as_float=False): self.orbit_elements.an_period = self.orbit_elements.an_time - \ self.get_last_an_time(self.orbit_elements.an_time - - np.timedelta64(10, 'm')) + - np.timedelta64(10, "m")) dt = astronomy._days(utc_time - self.orbit_elements.an_time) orbit_period = astronomy._days(self.orbit_elements.an_period) @@ -350,60 +353,6 @@ def get_next_passes(self, utc_time, length, lon, lat, alt, tol=0.001, horizon=0) :return: [(rise-time, fall-time, max-elevation-time), ...] """ - def elevation(minutes): - """Compute the elevation.""" - return self.get_observer_look(utc_time + - timedelta( - minutes=np.float64(minutes)), - lon, lat, alt)[1] - horizon - - def elevation_inv(minutes): - """Compute the inverse of elevation.""" - return -elevation(minutes) - - def get_root(fun, start, end, tol=0.01): - """Root finding scheme.""" - x_0 = end - x_1 = start - fx_0 = fun(end) - fx_1 = fun(start) - if abs(fx_0) < abs(fx_1): - fx_0, fx_1 = fx_1, fx_0 - x_0, x_1 = x_1, x_0 - - x_n = optimize.brentq(fun, x_0, x_1) - return x_n - - def get_max_parab(fun, start, end, tol=0.01): - """Successive parabolic interpolation.""" - a = float(start) - c = float(end) - b = (a + c) / 2.0 - - f_a = fun(a) - f_b = fun(b) - f_c = fun(c) - - x = b - with np.errstate(invalid='raise'): - while True: - try: - x = x - 0.5 * (((b - a) ** 2 * (f_b - f_c) - - (b - c) ** 2 * (f_b - f_a)) / - ((b - a) * (f_b - f_c) - (b - c) * (f_b - f_a))) - except FloatingPointError: - return b - if abs(b - x) <= tol: - return x - f_x = fun(x) - # sometimes the estimation diverges... return best guess - if f_x > f_b: - logger.info('Parabolic interpolation did not converge, returning best guess so far.') - return b - - a, b, c = (a + x) / 2.0, x, (x + c) / 2.0 - f_a, f_b, f_c = fun(a), f_x, fun(c) - # every minute times = utc_time + np.array([timedelta(minutes=minutes) for minutes in range(length * 60)]) @@ -411,9 +360,11 @@ def get_max_parab(fun, start, end, tol=0.01): zcs = np.where(np.diff(np.sign(elev)))[0] res = [] risetime = None + risemins = None + elev_func = partial(self._elevation, utc_time, lon, lat, alt, horizon) + elev_inv_func = partial(self._elevation_inv, utc_time, lon, lat, alt, horizon) for guess in zcs: - horizon_mins = get_root( - elevation, guess, guess + 1.0, tol=tol / 60.0) + horizon_mins = _get_root(elev_func, guess, guess + 1.0, tol=tol / 60.0) horizon_time = utc_time + timedelta(minutes=horizon_mins) if elev[guess] < 0: risetime = horizon_time @@ -421,18 +372,18 @@ def get_max_parab(fun, start, end, tol=0.01): else: falltime = horizon_time fallmins = horizon_mins - if risetime: - int_start = max(0, int(np.floor(risemins))) - int_end = min(len(elev), int(np.ceil(fallmins) + 1)) - middle = int_start + np.argmax(elev[int_start:int_end]) - highest = utc_time + \ - timedelta(minutes=get_max_parab( - elevation_inv, - max(risemins, middle - 1), min(fallmins, middle + 1), - tol=tol / 60.0 - )) - res += [(risetime, falltime, highest)] - risetime = None + if risetime is None: + continue + int_start = max(0, int(np.floor(risemins))) + int_end = min(len(elev), int(np.ceil(fallmins) + 1)) + middle = int_start + np.argmax(elev[int_start:int_end]) + highest = utc_time + \ + timedelta(minutes=_get_max_parab( + elev_inv_func, + max(risemins, middle - 1), min(fallmins, middle + 1), + tol=tol / 60.0 + )) + res += [(risetime, falltime, highest)] return res def _get_time_at_horizon(self, utc_time, obslon, obslat, **kwargs): @@ -449,7 +400,7 @@ def _get_time_at_horizon(self, utc_time, obslon, obslat, **kwargs): warnings.warn("_get_time_at_horizon is replaced with get_next_passes", DeprecationWarning, stacklevel=2) if "precision" in kwargs: - precision = kwargs['precision'] + precision = kwargs["precision"] else: precision = timedelta(seconds=0.001) if "max_iterations" in kwargs: @@ -497,7 +448,7 @@ def utc2local(self, utc_time): lon, _, _ = self.get_lonlatalt(utc_time) return utc_time + timedelta(hours=lon * 24 / 360.0) - def get_equatorial_crossing_time(self, tstart, tend, node='ascending', local_time=False, + def get_equatorial_crossing_time(self, tstart, tend, node="ascending", local_time=False, rtol=1E-9): """Estimate the equatorial crossing time of an orbit. @@ -524,19 +475,19 @@ def get_equatorial_crossing_time(self, tstart, tend, node='ascending', local_tim # Orbit doesn't cross the equator in the given time interval return None elif n_end - n_start > 1: - warnings.warn('Multiple revolutions between start and end time. Computing crossing ' - 'time for the last revolution in that interval.', stacklevel=2) + warnings.warn("Multiple revolutions between start and end time. Computing crossing " + "time for the last revolution in that interval.", stacklevel=2) # Let n'(t) = n(t) - offset. Determine offset so that n'(tstart) < 0 and n'(tend) > 0 and # n'(tcross) = 0. offset = int(n_end) - if node == 'descending': + if node == "descending": offset = offset + 0.5 # Use bisection algorithm to find the root of n'(t), which is the crossing time. The # algorithm requires continuous time coordinates, so convert timestamps to microseconds # since 1970. - time_unit = 'us' # same precision as datetime + time_unit = "us" # same precision as datetime def _nprime(time_f): """Continuous orbit number as a function of time.""" @@ -561,6 +512,63 @@ def _nprime(time_f): return tcross + def _elevation(self, utc_time, lon, lat, alt, horizon, minutes): + """Compute the elevation.""" + return self.get_observer_look(utc_time + + timedelta(minutes=np.float64(minutes)), + lon, lat, alt)[1] - horizon + + + def _elevation_inv(self, utc_time, lon, lat, alt, horizon, minutes): + """Compute the inverse of elevation.""" + return -self._elevation(utc_time, lon, lat, alt, horizon, minutes) + + +def _get_root(fun, start, end, tol=0.01): + """Root finding scheme.""" + x_0 = end + x_1 = start + fx_0 = fun(end) + fx_1 = fun(start) + if abs(fx_0) < abs(fx_1): + fx_0, fx_1 = fx_1, fx_0 + x_0, x_1 = x_1, x_0 + + x_n = optimize.brentq(fun, x_0, x_1) + return x_n + + +def _get_max_parab(fun, start, end, tol=0.01): + """Successive parabolic interpolation.""" + a = float(start) + c = float(end) + b = (a + c) / 2.0 + + f_a = fun(a) + f_b = fun(b) + f_c = fun(c) + + x = b + with np.errstate(invalid="raise"): + while True: + try: + x = x - 0.5 * (((b - a) ** 2 * (f_b - f_c) + - (b - c) ** 2 * (f_b - f_a)) / + ((b - a) * (f_b - f_c) - (b - c) * (f_b - f_a))) + except FloatingPointError: + return b + if abs(b - x) <= tol: + return x + f_x = fun(x) + # sometimes the estimation diverges... return best guess + if f_x > f_b: + logger.info("Parabolic interpolation did not converge, returning best guess so far.") + return b + + a, b, c = (a + x) / 2.0, x, (x + c) / 2.0 + f_a, f_b, f_c = fun(a), f_x, fun(c) + + class OrbitElements(object): """Class holding the orbital elements.""" @@ -617,7 +625,8 @@ def __init__(self, tle): class _SGDP4(object): """Class for the SGDP4 computations.""" - def __init__(self, orbit_elements): + def __init__(self, orbit_elements): # noqa: C901 + """Initialize class.""" self.mode = None # perigee = orbit_elements.perigee @@ -636,11 +645,11 @@ def __init__(self, orbit_elements): # A30 = -XJ3 * AE**3 if not (0 < self.eo < ECC_LIMIT_HIGH): - raise OrbitalError('Eccentricity out of range: %e' % self.eo) + raise OrbitalError("Eccentricity out of range: %e" % self.eo) elif not ((0.0035 * 2 * np.pi / XMNPDA) < self.xn_0 < (18 * 2 * np.pi / XMNPDA)): - raise OrbitalError('Mean motion out of range: %e' % self.xn_0) + raise OrbitalError("Mean motion out of range: %e" % self.xn_0) elif not (0 < self.xincl < np.pi): - raise OrbitalError('Inclination out of range: %e' % self.xincl) + raise OrbitalError("Inclination out of range: %e" % self.xincl) if self.eo < 0: self.mode = self.SGDP4_ZERO_ECC @@ -776,7 +785,7 @@ def __init__(self, orbit_elements): 15.0 * c1sq * (2.0 * self.d2 + c1sq))) elif self.mode == SGDP4_DEEP_NORM: - raise NotImplementedError('Deep space calculations not supported') + raise NotImplementedError("Deep space calculations not supported") def propagate(self, utc_time): kep = {} @@ -786,7 +795,7 @@ def propagate(self, utc_time): # print utc_time.shape # print self.t_0 utc_time = dt2np(utc_time) - ts = (utc_time - self.t_0) / np.timedelta64(1, 'm') + ts = (utc_time - self.t_0) / np.timedelta64(1, "m") em = self.eo xinc = self.xincl @@ -796,7 +805,7 @@ def propagate(self, utc_time): omega = self.omegao + self.omgdot * ts if self.mode == SGDP4_ZERO_ECC: - raise NotImplementedError('Mode SGDP4_ZERO_ECC not implemented') + raise NotImplementedError("Mode SGDP4_ZERO_ECC not implemented") elif self.mode == SGDP4_NEAR_SIMP: raise NotImplementedError('Mode "Near-space, simplified equations"' ' not implemented') @@ -819,12 +828,12 @@ def propagate(self, utc_time): xl = xmp + omega + xnode + self.xnodp * templ else: - raise NotImplementedError('Deep space calculations not supported') + raise NotImplementedError("Deep space calculations not supported") if np.any(a < 1): - raise Exception('Satellite crashed at time %s', utc_time) + raise Exception("Satellite crashed at time %s", utc_time) elif np.any(e < ECC_LIMIT_LOW): - raise ValueError('Satellite modified eccentricity too low: %s < %e' + raise ValueError("Satellite modified eccentricity too low: %s < %e" % (str(e[e < ECC_LIMIT_LOW]), ECC_LIMIT_LOW)) e = np.where(e < ECC_EPS, ECC_EPS, e) @@ -844,14 +853,14 @@ def propagate(self, utc_time): elsq = axn**2 + ayn**2 if np.any(elsq >= 1): - raise Exception('e**2 >= 1 at %s', utc_time) + raise Exception("e**2 >= 1 at %s", utc_time) - kep['ecc'] = np.sqrt(elsq) + kep["ecc"] = np.sqrt(elsq) epw = np.fmod(xlt - xnode, 2 * np.pi) # needs a copy in case of an array capu = np.array(epw) - maxnr = kep['ecc'] + maxnr = kep["ecc"] for i in range(10): sinEPW = np.sin(epw) cosEPW = np.cos(epw) @@ -899,7 +908,7 @@ def propagate(self, utc_time): xinck = xinc + 1.5 * temp2 * self.cosIO * self.sinIO * cos2u if np.any(rk < 1): - raise Exception('Satellite crashed at time %s', utc_time) + raise Exception("Satellite crashed at time %s", utc_time) temp0 = np.sqrt(a) temp2 = XKE / (a * temp0) @@ -909,14 +918,14 @@ def propagate(self, utc_time): (self.x1mth2 * cos2u + 1.5 * self.x3thm1)) * (XKMPER / AE * XMNPDA / 86400.0)) - kep['radius'] = rk * XKMPER / AE - kep['theta'] = uk - kep['eqinc'] = xinck - kep['ascn'] = xnodek - kep['argp'] = omega - kep['smjaxs'] = a * XKMPER / AE - kep['rdotk'] = rdotk - kep['rfdotk'] = rfdotk + kep["radius"] = rk * XKMPER / AE + kep["theta"] = uk + kep["eqinc"] = xinck + kep["ascn"] = xnodek + kep["argp"] = omega + kep["smjaxs"] = a * XKMPER / AE + kep["rdotk"] = rdotk + kep["rfdotk"] = rfdotk return kep @@ -940,12 +949,12 @@ def kep2xyz(kep): (Not sure what 'kep' actually refers to, just guessing! FIXME!) """ - sinT = np.sin(kep['theta']) - cosT = np.cos(kep['theta']) - sinI = np.sin(kep['eqinc']) - cosI = np.cos(kep['eqinc']) - sinS = np.sin(kep['ascn']) - cosS = np.cos(kep['ascn']) + sinT = np.sin(kep["theta"]) + cosT = np.cos(kep["theta"]) + sinI = np.sin(kep["eqinc"]) + cosI = np.cos(kep["eqinc"]) + sinS = np.sin(kep["ascn"]) + cosS = np.cos(kep["ascn"]) xmx = -sinS * cosI xmy = cosS * cosI @@ -954,17 +963,17 @@ def kep2xyz(kep): uy = xmy * sinT + sinS * cosT uz = sinI * sinT - x = kep['radius'] * ux - y = kep['radius'] * uy - z = kep['radius'] * uz + x = kep["radius"] * ux + y = kep["radius"] * uy + z = kep["radius"] * uz vx = xmx * cosT - cosS * sinT vy = xmy * cosT - sinS * sinT vz = sinI * cosT - v_x = kep['rdotk'] * ux + kep['rfdotk'] * vx - v_y = kep['rdotk'] * uy + kep['rfdotk'] * vy - v_z = kep['rdotk'] * uz + kep['rfdotk'] * vz + v_x = kep["rdotk"] * ux + kep["rfdotk"] * vx + v_y = kep["rdotk"] * uy + kep["rfdotk"] * vy + v_z = kep["rdotk"] * uz + kep["rfdotk"] * vz return np.array((x, y, z)), np.array((v_x, v_y, v_z)) diff --git a/pyorbital/tests/SGP4-VER.TLE b/pyorbital/tests/SGP4-VER.TLE index cfa31334..a13a1698 100644 --- a/pyorbital/tests/SGP4-VER.TLE +++ b/pyorbital/tests/SGP4-VER.TLE @@ -99,7 +99,7 @@ # # check error code 4 1 33333U 05037B 05333.02012661 .25992681 00000-0 24476-3 0 1534 2 33333 96.4736 157.9986 9950000 244.0492 110.6523 4.00004038 10708 0.0 150.0 5.00 -# # try and check error code 2 but this +# # try and check error code 2 but this 1 33334U 78066F 06174.85818871 .00000620 00000-0 10000-3 0 6809 2 33334 68.4714 236.1303 5602877 123.7484 302.5767 0.00001000 67521 0.0 1440.0 1.00 # # try to check error code 3 looks like ep never goes below zero, tied close to ecc diff --git a/pyorbital/tests/test_aiaa.py b/pyorbital/tests/test_aiaa.py index 6362ab4e..9ec2596b 100644 --- a/pyorbital/tests/test_aiaa.py +++ b/pyorbital/tests/test_aiaa.py @@ -1,7 +1,7 @@ #!/usr/bin/env python # -*- coding: utf-8 -*- -# Copyright (c) 2011 - 2023 Pytroll Community +# Copyright (c) 2011 - 2024 Pytroll Community # Author(s): @@ -20,8 +20,8 @@ # You should have received a copy of the GNU General Public License # along with this program. If not, see . -"""Test cases from the AIAA article. -""" +"""Test cases from the AIAA article.""" + # TODO: right formal unit tests. from __future__ import print_function, with_statement @@ -38,10 +38,10 @@ class LineOrbital(Orbital): - """Read TLE lines instead of file. - """ + """Read TLE lines instead of file.""" def __init__(self, satellite, line1, line2): + """Initialize the class.""" satellite = satellite.upper() self.satellite_name = satellite self.tle = tlefile.read(satellite, line1=line1, line2=line2) @@ -50,8 +50,7 @@ def __init__(self, satellite, line1, line2): def get_results(satnumber, delay): - """Get expected results from result file. - """ + """Get expected results from result file.""" path = os.path.dirname(os.path.abspath(__file__)) with open(os.path.join(path, "aiaa_results")) as f_2: line = f_2.readline() @@ -87,10 +86,9 @@ class AIAAIntegrationTest(unittest.TestCase): @unittest.skipIf( not os.path.exists(os.path.join(_DATAPATH, "SGP4-VER.TLE")), - 'SGP4-VER.TLE not available') + "SGP4-VER.TLE not available") def test_aiaa(self): - """Do the tests against AIAA test cases. - """ + """Do the tests against AIAA test cases.""" path = os.path.dirname(os.path.abspath(__file__)) with open(os.path.join(path, "SGP4-VER.TLE")) as f__: test_line = f__.readline() @@ -100,50 +98,52 @@ def test_aiaa(self): if test_line.startswith("1 "): line1 = test_line if test_line.startswith("2 "): - line2 = test_line[:69] - times = str.split(test_line[69:]) - times = np.arange(float(times[0]), - float(times[1]) + 1, - float(times[2])) - if test_name.startswith("# SL-14 DEB"): - # FIXME: we have to handle decaying satellites! - test_line = f__.readline() - continue - - try: - o = LineOrbital("unknown", line1, line2) - except NotImplementedError: - test_line = f__.readline() - continue - except ChecksumError: - self.assertTrue(test_line.split()[1] in [ - "33333", "33334", "33335"]) - for delay in times: - try: - test_time = delay.astype( - 'timedelta64[m]') + o.tle.epoch - pos, vel = o.get_position(test_time, False) - res = get_results( - int(o.tle.satnumber), float(delay)) - except NotImplementedError: - # Skipping deep-space - break - # except ValueError, e: - # from warnings import warn - # warn(test_name + ' ' + str(e)) - # break - - delta_pos = 5e-6 # km = 5 mm - delta_vel = 5e-9 # km/s = 5 um/s - delta_time = 1e-3 # 1 millisecond - self.assertTrue(abs(res[0] - pos[0]) < delta_pos) - self.assertTrue(abs(res[1] - pos[1]) < delta_pos) - self.assertTrue(abs(res[2] - pos[2]) < delta_pos) - self.assertTrue(abs(res[3] - vel[0]) < delta_vel) - self.assertTrue(abs(res[4] - vel[1]) < delta_vel) - self.assertTrue(abs(res[5] - vel[2]) < delta_vel) - if res[6] is not None: - dt = astronomy._days(res[6] - test_time) * 24 * 60 - self.assertTrue(abs(dt) < delta_time) + _check_line2(f__, test_name, line1, test_line) test_line = f__.readline() + + +def _check_line2(f__, test_name: str, line1: str, test_line: str) -> None: + line2 = test_line[:69] + times_str = str.split(test_line[69:]) + times = np.arange(float(times_str[0]), + float(times_str[1]) + 1, + float(times_str[2])) + if test_name.startswith("# SL-14 DEB"): + # FIXME: we have to handle decaying satellites! + return + + try: + o = LineOrbital("unknown", line1, line2) + except NotImplementedError: + return + except ChecksumError: + assert test_line.split()[1] in ["33333", "33334", "33335"] + return + + for delay in times: + try: + test_time = delay.astype("timedelta64[m]") + o.tle.epoch + pos, vel = o.get_position(test_time, False) + res = get_results( + int(o.tle.satnumber), float(delay)) + except NotImplementedError: + # Skipping deep-space + break + # except ValueError, e: + # from warnings import warn + # warn(test_name + ' ' + str(e)) + # break + + delta_pos = 5e-6 # km = 5 mm + delta_vel = 5e-9 # km/s = 5 um/s + delta_time = 1e-3 # 1 millisecond + assert abs(res[0] - pos[0]) < delta_pos + assert abs(res[1] - pos[1]) < delta_pos + assert abs(res[2] - pos[2]) < delta_pos + assert abs(res[3] - vel[0]) < delta_vel + assert abs(res[4] - vel[1]) < delta_vel + assert abs(res[5] - vel[2]) < delta_vel + if res[6] is not None: + dt = astronomy._days(res[6] - test_time) * 24 * 60 + assert abs(dt) < delta_time diff --git a/pyorbital/tests/test_astronomy.py b/pyorbital/tests/test_astronomy.py index 6eba33a8..8e4895ba 100644 --- a/pyorbital/tests/test_astronomy.py +++ b/pyorbital/tests/test_astronomy.py @@ -1,7 +1,7 @@ #!/usr/bin/env python # -*- coding: utf-8 -*- -# Copyright (c) 2013, 2014, 2022 Pytroll Community +# Copyright (c) 2013, 2014, 2022, 2024 Pytroll Community # Author(s): @@ -20,6 +20,9 @@ # You should have received a copy of the GNU General Public License # along with this program. If not, see . +"""Unit testing the Astronomy methods and functions.""" + + from datetime import datetime import dask.array as da @@ -36,21 +39,25 @@ def _create_dask_array(input_list: list, dtype: npt.DTypeLike) -> da.Array: + """Create a dummy dask array for testing.""" np_arr = np.array(input_list, dtype=dtype) return da.from_array(np_arr) def _create_xarray_numpy(input_list: list, dtype: npt.DTypeLike) -> DataArray: + """Create a dummy xarray DataArray for testing.""" np_arr = np.array(input_list, dtype=dtype) return DataArray(np_arr) def _create_xarray_dask(input_list: list, dtype: npt.DTypeLike) -> DataArray: + """Create a dummy daskified xarray DataArray for testing.""" dask_arr = _create_dask_array(input_list, dtype) return DataArray(dask_arr) class TestAstronomy: + """Testing the Astronomy class.""" @pytest.mark.parametrize( ("dt", "exp_jdays", "exp_j2000"), diff --git a/pyorbital/tests/test_geoloc.py b/pyorbital/tests/test_geoloc.py index e5797784..eceb9d33 100644 --- a/pyorbital/tests/test_geoloc.py +++ b/pyorbital/tests/test_geoloc.py @@ -23,10 +23,11 @@ """Test the geoloc module.""" from datetime import datetime + import numpy as np from pyorbital.geoloc import ScanGeometry, geodetic_lat, qrotate, subpoint -from pyorbital.geoloc_instrument_definitions import avhrr, viirs, amsua, mhs, hirs4, atms, ascat +from pyorbital.geoloc_instrument_definitions import amsua, ascat, atms, avhrr, hirs4, mhs, viirs class TestQuaternion: @@ -110,8 +111,8 @@ def test_scan_geometry(self): times = instrument.times(start_of_scan) assert times[0, 1] == start_of_scan - assert times[0, 0] == start_of_scan - np.timedelta64(100, 'ms') - assert times[0, 2] == start_of_scan + np.timedelta64(100, 'ms') + assert times[0, 0] == start_of_scan - np.timedelta64(100, "ms") + assert times[0, 2] == start_of_scan + np.timedelta64(100, "ms") def test_geodetic_lat(self): """Test the determination of the geodetic latitude.""" diff --git a/pyorbital/tests/test_logging.py b/pyorbital/tests/test_logging.py new file mode 100644 index 00000000..35f56ccf --- /dev/null +++ b/pyorbital/tests/test_logging.py @@ -0,0 +1,38 @@ +#!/usr/bin/env python +# -*- coding: utf-8 -*- + +# Copyright (c) 2024 Pytroll Community + +# This program is free software: you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. + +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. + +# You should have received a copy of the GNU General Public License +# along with this program. If not, see . + +"""Test the logging module.""" + +import logging + +from pyorbital.logger import get_logger, logging_off, logging_on + + +def test_logging_on_and_off(caplog): + """Test that switching logging on and off works.""" + logger = get_logger("pyorbital.spam") + logging_on() + with caplog.at_level(logging.WARNING): + logger.debug("I'd like to leave the army please, sir.") + logger.warning("Stop that! It's SPAM.") + assert "Stop that! It's SPAM" in caplog.text + assert "I'd like to leave the army please, sir." not in caplog.text + logging_off() + with caplog.at_level(logging.DEBUG): + logger.warning("You've got a nice army base here, Colonel.") + assert "You've got a nice army base here, Colonel." not in caplog.text diff --git a/pyorbital/tests/test_orbital.py b/pyorbital/tests/test_orbital.py index 74e9dacb..dbc57625 100644 --- a/pyorbital/tests/test_orbital.py +++ b/pyorbital/tests/test_orbital.py @@ -22,12 +22,14 @@ """Test the geoloc orbital.""" -import pytest import unittest -from unittest import mock from datetime import datetime, timedelta -import pytz +from unittest import mock + import numpy as np +import pytest +import pytz + from pyorbital import orbital eps_deg = 10e-3 @@ -45,7 +47,7 @@ def test_get_orbit_number(self): "92.4533 267.6830 14.19582686 11574") dobj = datetime(2012, 1, 18, 8, 4, 19) orbnum = sat.get_orbit_number(dobj) - self.assertEqual(orbnum, 1163) + assert orbnum == 1163 def test_sublonlat(self): """Test getting the sub-satellite position.""" @@ -59,12 +61,9 @@ def test_sublonlat(self): expected_lon = -68.199894472013213 expected_lat = 23.159747677881075 expected_alt = 392.01953430856935 - self.assertTrue(np.abs(lon - expected_lon) < eps_deg, - 'Calculation of sublon failed') - self.assertTrue(np.abs(lat - expected_lat) < eps_deg, - 'Calculation of sublat failed') - self.assertTrue(np.abs(alt - expected_alt) < eps_deg, - 'Calculation of altitude failed') + assert np.abs(lon - expected_lon) < eps_deg, "Calculation of sublon failed" + assert np.abs(lat - expected_lat) < eps_deg, "Calculation of sublat failed" + assert np.abs(alt - expected_alt) < eps_deg, "Calculation of altitude failed" def test_observer_look(self): """Test getting the observer look angles.""" @@ -77,10 +76,8 @@ def test_observer_look(self): az, el = sat.get_observer_look(d, -84.39733, 33.775867, 0) expected_az = 122.45169655331965 expected_el = 1.9800219611255456 - self.assertTrue(np.abs(az - expected_az) < eps_deg, - 'Calculation of azimut failed') - self.assertTrue(np.abs(el - expected_el) < eps_deg, - 'Calculation of elevation failed') + assert np.abs(az - expected_az) < eps_deg, "Calculation of azimut failed" + assert np.abs(el - expected_el) < eps_deg, "Calculation of elevation failed" def test_orbit_num_an(self): """Test getting orbit number - ascending node.""" @@ -90,7 +87,7 @@ def test_orbit_num_an(self): line2="2 29499 98.6804 312.6735 0001758 " "111.9178 248.2152 14.21501774254058") d = datetime(2011, 9, 14, 5, 30) - self.assertEqual(sat.get_orbit_number(d), 25437) + assert sat.get_orbit_number(d) == 25437 def test_orbit_num_non_an(self): """Test getting orbit number - not ascending node.""" @@ -99,8 +96,8 @@ def test_orbit_num_non_an(self): ".00000017 00000-0 27793-4 0 9819", line2="2 29499 98.6639 121.6164 0001449 " "71.9056 43.3132 14.21510544330271") - dt = np.timedelta64(98, 'm') - self.assertEqual(sat.get_orbit_number(sat.tle.epoch + dt), 33028) + dt = np.timedelta64(98, "m") + assert sat.get_orbit_number(sat.tle.epoch + dt) == 33028 def test_orbit_num_equator(self): """Test getting orbit numbers when being around equator.""" @@ -113,13 +110,13 @@ def test_orbit_num_equator(self): t2 = datetime(2013, 3, 2, 22, 2, 26) on1 = sat.get_orbit_number(t1) on2 = sat.get_orbit_number(t2) - self.assertEqual(on1, 6973) - self.assertEqual(on2, 6974) + assert on1 == 6973 + assert on2 == 6974 pos1, vel1 = sat.get_position(t1, normalize=False) pos2, vel2 = sat.get_position(t2, normalize=False) del vel1, vel2 - self.assertTrue(pos1[2] < 0) - self.assertTrue(pos2[2] > 0) + assert pos1[2] < 0 + assert pos2[2] > 0 def test_get_next_passes_apogee(self): """Regression test #22.""" @@ -128,12 +125,10 @@ def test_get_next_passes_apogee(self): line2 = "2 24793 86.3994 209.3241 0002020 " \ "89.8714 270.2713 14.34246429 90794" - orb = orbital.Orbital('IRIDIUM 7 [+]', line1=line1, line2=line2) + orb = orbital.Orbital("IRIDIUM 7 [+]", line1=line1, line2=line2) d = datetime(2018, 3, 7, 3, 30, 15) res = orb.get_next_passes(d, 1, 170.556, -43.368, 0.5, horizon=40) - self.assertTrue(abs( - res[0][2] - datetime(2018, 3, 7, 3, 48, 13, 178439)) < - timedelta(seconds=0.01)) + assert abs(res[0][2] - datetime(2018, 3, 7, 3, 48, 13, 178439)) < timedelta(seconds=0.01) def test_get_next_passes_tricky(self): """Check issue #34 for reference.""" @@ -143,33 +138,28 @@ def test_get_next_passes_tricky(self): line2 = "2 43125 097.5269 314.3317 0010735 "\ "157.6344 202.5362 15.23132245036381" - orb = orbital.Orbital('LEMUR-2-BROWNCOW', line1=line1, line2=line2) + orb = orbital.Orbital("LEMUR-2-BROWNCOW", line1=line1, line2=line2) d = datetime(2018, 9, 8) res = orb.get_next_passes(d, 72, -8.174163, 51.953319, 0.05, horizon=5) - self.assertTrue(abs( - res[0][2] - datetime(2018, 9, 8, 9, 5, 46, 375248)) < - timedelta(seconds=0.01)) - self.assertTrue(abs( - res[-1][2] - datetime(2018, 9, 10, 22, 15, 3, 143469)) < - timedelta(seconds=0.01)) + assert abs(res[0][2] - datetime(2018, 9, 8, 9, 5, 46, 375248)) < timedelta(seconds=0.01) + assert abs(res[-1][2] - datetime(2018, 9, 10, 22, 15, 3, 143469)) < timedelta(seconds=0.01) - self.assertTrue(len(res) == 15) + assert len(res) == 15 def test_get_next_passes_issue_22(self): """Check that max.""" - line1 = '1 28654U 05018A 21083.16603416 .00000102 00000-0 79268-4 0 9999' - line2 = '2 28654 99.0035 147.6583 0014816 159.4931 200.6838 14.12591533816498' + line1 = "1 28654U 05018A 21083.16603416 .00000102 00000-0 79268-4 0 9999" + line2 = "2 28654 99.0035 147.6583 0014816 159.4931 200.6838 14.12591533816498" orb = orbital.Orbital("NOAA 18", line1=line1, line2=line2) t = datetime(2021, 3, 9, 22) next_passes = orb.get_next_passes(t, 1, -15.6335, 27.762, 0.) rise, fall, max_elevation = next_passes[0] assert rise < max_elevation < fall - print(next_passes) - @mock.patch('pyorbital.orbital.Orbital.get_lonlatalt') + @mock.patch("pyorbital.orbital.Orbital.get_lonlatalt") def test_utc2local(self, get_lonlatalt): """Test converting UTC to local time.""" get_lonlatalt.return_value = -45, None, None @@ -178,20 +168,19 @@ def test_utc2local(self, get_lonlatalt): ".00000017 00000-0 27793-4 0 9819", line2="2 29499 98.6639 121.6164 0001449 " "71.9056 43.3132 14.21510544330271") - self.assertEqual(sat.utc2local(datetime(2009, 7, 1, 12)), - datetime(2009, 7, 1, 9)) + assert sat.utc2local(datetime(2009, 7, 1, 12)) == datetime(2009, 7, 1, 9) - @mock.patch('pyorbital.orbital.Orbital.utc2local') - @mock.patch('pyorbital.orbital.Orbital.get_orbit_number') + @mock.patch("pyorbital.orbital.Orbital.utc2local") + @mock.patch("pyorbital.orbital.Orbital.get_orbit_number") def test_get_equatorial_crossing_time(self, get_orbit_number, utc2local): """Test get the equatorial crossing time.""" def get_orbit_number_patched(utc_time, **kwargs): utc_time = np.datetime64(utc_time) - diff = (utc_time - np.datetime64('2009-07-01 12:38:12')) / np.timedelta64(7200, 's') + diff = (utc_time - np.datetime64("2009-07-01 12:38:12")) / np.timedelta64(7200, "s") return 1234 + diff get_orbit_number.side_effect = get_orbit_number_patched - utc2local.return_value = 'local_time' + utc2local.return_value = "local_time" sat = orbital.Orbital("METOP-A", line1="1 29499U 06044A 13060.48822809 " ".00000017 00000-0 27793-4 0 9819", @@ -202,20 +191,20 @@ def get_orbit_number_patched(utc_time, **kwargs): res = sat.get_equatorial_crossing_time(tstart=datetime(2009, 7, 1, 12), tend=datetime(2009, 7, 1, 13)) exp = datetime(2009, 7, 1, 12, 38, 12) - self.assertTrue((res - exp) < timedelta(seconds=0.01)) + assert res - exp < timedelta(seconds=0.01) # Descending node res = sat.get_equatorial_crossing_time(tstart=datetime(2009, 7, 1, 12), tend=datetime(2009, 7, 1, 14, 0), - node='descending') + node="descending") exp = datetime(2009, 7, 1, 13, 38, 12) - self.assertTrue((res - exp) < timedelta(seconds=0.01)) + assert res - exp < timedelta(seconds=0.01) # Conversion to local time res = sat.get_equatorial_crossing_time(tstart=datetime(2009, 7, 1, 12), tend=datetime(2009, 7, 1, 14), local_time=True) - self.assertEqual(res, 'local_time') + assert res == "local_time" class TestGetObserverLook(unittest.TestCase): @@ -250,8 +239,9 @@ def test_basic_numpy(self): def test_basic_dask(self): """Test with dask array inputs.""" - from pyorbital import orbital import dask.array as da + + from pyorbital import orbital sat_lon = da.from_array(self.sat_lon, chunks=2) sat_lat = da.from_array(self.sat_lat, chunks=2) sat_alt = da.from_array(self.sat_alt, chunks=2) @@ -266,11 +256,12 @@ def test_basic_dask(self): def test_xarray_with_numpy(self): """Test with xarray DataArray with numpy array as inputs.""" - from pyorbital import orbital import xarray as xr - def _xarr_conv(input): - return xr.DataArray(input) + from pyorbital import orbital + + def _xarr_conv(input_array): + return xr.DataArray(input_array) sat_lon = _xarr_conv(self.sat_lon) sat_lat = _xarr_conv(self.sat_lat) sat_alt = _xarr_conv(self.sat_alt) @@ -285,12 +276,13 @@ def _xarr_conv(input): def test_xarray_with_dask(self): """Test with xarray DataArray with dask array as inputs.""" - from pyorbital import orbital import dask.array as da import xarray as xr - def _xarr_conv(input): - return xr.DataArray(da.from_array(input, chunks=2)) + from pyorbital import orbital + + def _xarr_conv(input_array): + return xr.DataArray(da.from_array(input_array, chunks=2)) sat_lon = _xarr_conv(self.sat_lon) sat_lat = _xarr_conv(self.sat_lat) sat_alt = _xarr_conv(self.sat_alt) @@ -339,14 +331,15 @@ def test_basic_numpy(self): azi, elev = orbital.get_observer_look(self.sat_lon, self.sat_lat, self.sat_alt, self.t, self.lon, self.lat, self.alt) - self.assertEqual(np.sum(np.isnan(azi)), 0) - self.assertFalse(np.isnan(azi).any()) + assert np.sum(np.isnan(azi)) == 0 + assert not np.isnan(azi).any() np.testing.assert_allclose(elev, self.exp_elev) def test_basic_dask(self): """Test with dask array inputs.""" - from pyorbital import orbital import dask.array as da + + from pyorbital import orbital sat_lon = da.from_array(self.sat_lon, chunks=2) sat_lat = da.from_array(self.sat_lat, chunks=2) sat_alt = da.from_array(self.sat_alt, chunks=2) @@ -356,17 +349,18 @@ def test_basic_dask(self): azi, elev = orbital.get_observer_look(sat_lon, sat_lat, sat_alt, self.t, lon, lat, alt) - self.assertEqual(np.sum(np.isnan(azi)), 0) - self.assertFalse(np.isnan(azi).any()) + assert np.sum(np.isnan(azi)) == 0 + assert not np.isnan(azi).any() np.testing.assert_allclose(elev.compute(), self.exp_elev) def test_xarray_with_numpy(self): """Test with xarray DataArray with numpy array as inputs.""" - from pyorbital import orbital import xarray as xr - def _xarr_conv(input): - return xr.DataArray(input) + from pyorbital import orbital + + def _xarr_conv(input_array): + return xr.DataArray(input_array) sat_lon = _xarr_conv(self.sat_lon) sat_lat = _xarr_conv(self.sat_lat) sat_alt = _xarr_conv(self.sat_alt) @@ -376,18 +370,19 @@ def _xarr_conv(input): azi, elev = orbital.get_observer_look(sat_lon, sat_lat, sat_alt, self.t, lon, lat, alt) - self.assertEqual(np.sum(np.isnan(azi)), 0) - self.assertFalse(np.isnan(azi).any()) + assert np.sum(np.isnan(azi)) == 0 + assert not np.isnan(azi).any() np.testing.assert_allclose(elev.data, self.exp_elev) def test_xarray_with_dask(self): """Test with xarray DataArray with dask array as inputs.""" - from pyorbital import orbital import dask.array as da import xarray as xr - def _xarr_conv(input): - return xr.DataArray(da.from_array(input, chunks=2)) + from pyorbital import orbital + + def _xarr_conv(input_array): + return xr.DataArray(da.from_array(input_array, chunks=2)) sat_lon = _xarr_conv(self.sat_lon) sat_lat = _xarr_conv(self.sat_lat) sat_alt = _xarr_conv(self.sat_alt) @@ -397,8 +392,8 @@ def _xarr_conv(input): azi, elev = orbital.get_observer_look(sat_lon, sat_lat, sat_alt, self.t, lon, lat, alt) - self.assertEqual(np.sum(np.isnan(azi)), 0) - self.assertFalse(np.isnan(azi).any()) + assert np.sum(np.isnan(azi)) == 0 + assert not np.isnan(azi).any() np.testing.assert_allclose(elev.data.compute(), self.exp_elev) @@ -408,47 +403,45 @@ class TestRegressions(unittest.TestCase): def test_63(self): """Check that no runtimewarning is raised, #63.""" import warnings + from pyorbital.orbital import Orbital - from dateutil import parser - warnings.filterwarnings('error') + warnings.filterwarnings("error") orb = Orbital("Suomi-NPP", line1="1 37849U 11061A 19292.84582509 .00000011 00000-0 25668-4 0 9997", line2="2 37849 98.7092 229.3263 0000715 98.5313 290.6262 14.19554485413345") - orb.get_next_passes(parser.parse("2019-10-21 16:00:00"), 12, 123.29736, -13.93763, 0) - warnings.filterwarnings('default') + orb.get_next_passes(datetime(2019, 10, 21, 16, 0, 0), 12, 123.29736, -13.93763, 0) + warnings.filterwarnings("default") -@pytest.mark.parametrize('dtime', +@pytest.mark.parametrize("dtime", [datetime(2024, 6, 25, 11, 0, 18), datetime(2024, 6, 25, 11, 5, 0, 0, pytz.UTC), - np.datetime64('2024-06-25T11:10:00.000000') + np.datetime64("2024-06-25T11:10:00.000000") ] ) def test_get_last_an_time_scalar_input(dtime): """Test getting the time of the last ascending node - input time is a scalar.""" from pyorbital.orbital import Orbital orb = Orbital("NOAA-20", - line1='1 43013U 17073A 24176.73674251 .00000000 00000+0 11066-3 0 00014', - line2='2 43013 98.7060 114.5340 0001454 139.3958 190.7541 14.19599847341971') + line1="1 43013U 17073A 24176.73674251 .00000000 00000+0 11066-3 0 00014", + line2="2 43013 98.7060 114.5340 0001454 139.3958 190.7541 14.19599847341971") - expected = np.datetime64('2024-06-25T10:44:18.234375') + expected = np.datetime64("2024-06-25T10:44:18.234375") result = orb.get_last_an_time(dtime) - assert abs(expected - result) < np.timedelta64(1, 's') + assert abs(expected - result) < np.timedelta64(1, "s") -@pytest.mark.parametrize('dtime', - [datetime(2024, 6, 25, 11, 5, 0, 0, pytz.timezone('Europe/Stockholm')), +@pytest.mark.parametrize("dtime", + [datetime(2024, 6, 25, 11, 5, 0, 0, pytz.timezone("Europe/Stockholm")), ] ) def test_get_last_an_time_wrong_input(dtime): """Test getting the time of the last ascending node - wrong input.""" from pyorbital.orbital import Orbital orb = Orbital("NOAA-20", - line1='1 43013U 17073A 24176.73674251 .00000000 00000+0 11066-3 0 00014', - line2='2 43013 98.7060 114.5340 0001454 139.3958 190.7541 14.19599847341971') - - with pytest.raises(ValueError) as exec_info: - _ = orb.get_last_an_time(dtime) + line1="1 43013U 17073A 24176.73674251 .00000000 00000+0 11066-3 0 00014", + line2="2 43013 98.7060 114.5340 0001454 139.3958 190.7541 14.19599847341971") expected = "UTC time expected! Parsing a timezone aware datetime object requires it to be UTC!" - assert str(exec_info.value) == expected + with pytest.raises(ValueError, match=expected): + _ = orb.get_last_an_time(dtime) diff --git a/pyorbital/tests/test_tlefile.py b/pyorbital/tests/test_tlefile.py index b7eccc36..c04572d1 100644 --- a/pyorbital/tests/test_tlefile.py +++ b/pyorbital/tests/test_tlefile.py @@ -1,7 +1,7 @@ #!/usr/bin/env python # -*- coding: utf-8 -*- # -# Copyright (c) 2014-2023 Pytroll Community +# Copyright (c) 2014-2024 Pytroll Community # # Author(s): # @@ -24,30 +24,32 @@ """Test TLE file reading, TLE downloading and stroging TLEs to database.""" -from pyorbital.tlefile import Tle -from pyorbital.tlefile import (_get_config_path, - read_platform_numbers, - _get_local_tle_path_from_env, - _get_uris_and_open_func, - check_is_platform_supported, - PKG_CONFIG_DIR) - -import logging import datetime +import logging +import os +import time import unittest -from unittest.mock import patch +from contextlib import suppress from unittest import mock + import pytest -import os -from contextlib import suppress -import time -line0 = "ISS (ZARYA)" -line1 = "1 25544U 98067A 08264.51782528 -.00002182 00000-0 -11606-4 0 2927" -line2 = "2 25544 51.6416 247.4627 0006703 130.5360 325.0288 15.72125391563537" +from pyorbital.tlefile import ( + PKG_CONFIG_DIR, + Tle, + _get_config_path, + _get_local_tle_path_from_env, + _get_uris_and_open_func, + check_is_platform_supported, + read_platform_numbers, +) -line1_2 = "1 38771U 12049A 21137.30264622 .00000000 00000+0 -49996-5 0 00017" -line2_2 = "2 38771 98.7162 197.7716 0002383 106.1049 122.6344 14.21477797449453" +LINE0 = "ISS (ZARYA)" +LINE1 = "1 25544U 98067A 08264.51782528 -.00002182 00000-0 -11606-4 0 2927" +LINE2 = "2 25544 51.6416 247.4627 0006703 130.5360 325.0288 15.72125391563537" + +LINE1_2 = "1 38771U 12049A 21137.30264622 .00000000 00000+0 -49996-5 0 00017" +LINE2_2 = "2 38771 98.7162 197.7716 0002383 106.1049 122.6344 14.21477797449453" NOAA19_2LINES = """1 33591U 09005A 21355.91138073 .00000074 00000+0 65091-4 0 9998 @@ -56,102 +58,104 @@ NOAA19_3LINES = "NOAA 19\n" + NOAA19_2LINES -tle_xml = '\n'.join( +tle_xml = "\n".join( ('', - '', - '', - '', - '', - '' + line1 + '', - '' + line2 + '', - '', - '', - '', - '', - '', - '', - '' + line1_2 + '', - '' + line2_2 + '', - '', - '', - '', - '')) + "", + "", + "", + "", + "" + LINE1 + "", + "" + LINE2 + "", + "", + "", + "", + "", + "", + "", + "" + LINE1_2 + "", + "" + LINE2_2 + "", + "", + "", + "", + "")) @pytest.fixture def fake_platforms_file(tmp_path): """Return file path to a fake platforms.txt file.""" - file_path = tmp_path / 'platforms.txt' - lines = ['# Some header lines - line 1\n', - '# Some header lines - line 2\n', - 'NOAA-21 54234\n', - 'NOAA-20 43013\n', - 'UNKNOWN SATELLITE 99999\n' + file_path = tmp_path / "platforms.txt" + lines = ["# Some header lines - line 1\n", + "# Some header lines - line 2\n", + "NOAA-21 54234\n", + "NOAA-20 43013\n", + "UNKNOWN SATELLITE 99999\n" ] - with open(file_path, 'w') as fpt: + with open(file_path, "w") as fpt: fpt.writelines(lines) - yield file_path + return file_path @pytest.fixture(scope="session") def fake_local_tles_dir(tmp_path_factory): """Make a list of fake tle files in a directory.""" - tle_dir = tmp_path_factory.mktemp('tle_files') - file_path = tle_dir / 'tle-202211180230.txt' + tle_dir = tmp_path_factory.mktemp("tle_files") + file_path = tle_dir / "tle-202211180230.txt" file_path.touch() time.sleep(1) - file_path = tle_dir / 'tle-202211180430.txt' + file_path = tle_dir / "tle-202211180430.txt" file_path.touch() time.sleep(1) - file_path = tle_dir / 'tle-202211180630.txt' + file_path = tle_dir / "tle-202211180630.txt" file_path.touch() time.sleep(1) - file_path = tle_dir / 'tle-202211180830.txt' + file_path = tle_dir / "tle-202211180830.txt" file_path.touch() - yield tle_dir + return tle_dir @pytest.fixture -def mock_env_ppp_config_dir(monkeypatch): +def _mock_env_ppp_config_dir(monkeypatch): """Mock environment variable PPP_CONFIG_DIR.""" - monkeypatch.setenv('PPP_CONFIG_DIR', '/path/to/old/mpop/config/dir') + monkeypatch.setenv("PPP_CONFIG_DIR", "/path/to/old/mpop/config/dir") @pytest.fixture -def mock_env_ppp_config_dir_missing(monkeypatch): +def _mock_env_ppp_config_dir_missing(monkeypatch): """Mock that the environment variable PPP_CONFIG_DIR is missing.""" - monkeypatch.delenv('PPP_CONFIG_DIR', raising=False) + monkeypatch.delenv("PPP_CONFIG_DIR", raising=False) @pytest.fixture -def mock_env_tles_missing(monkeypatch): +def _mock_env_tles_missing(monkeypatch): """Mock that the environment variable TLES is missing.""" - monkeypatch.delenv('TLES', raising=False) + monkeypatch.delenv("TLES", raising=False) @pytest.fixture -def mock_env_tles(monkeypatch, fake_local_tles_dir): +def _mock_env_tles(monkeypatch, fake_local_tles_dir): """Mock environment variable TLES.""" - monkeypatch.setenv('TLES', os.path.join(fake_local_tles_dir, '*')) + monkeypatch.setenv("TLES", os.path.join(fake_local_tles_dir, "*")) -def test_get_config_path_no_env_defined(caplog, mock_env_ppp_config_dir_missing): +@pytest.mark.usefixtures("_mock_env_ppp_config_dir_missing") +def test_get_config_path_no_env_defined(caplog): """Test getting the config path.""" with caplog.at_level(logging.WARNING): res = _get_config_path() assert res == PKG_CONFIG_DIR - assert caplog.text == '' + assert caplog.text == "" -def test_check_is_platform_supported_existing(caplog, mock_env_ppp_config_dir_missing): +@pytest.mark.usefixtures("_mock_env_ppp_config_dir_missing") +def test_check_is_platform_supported_existing(caplog): """Test the function to check if an existing platform is supported on default.""" with caplog.at_level(logging.INFO): - check_is_platform_supported('NOAA-21') + check_is_platform_supported("NOAA-21") - logoutput_lines = caplog.text.split('\n') + logoutput_lines = caplog.text.split("\n") expected1 = "Satellite NOAA-21 is supported. NORAD number: 54234" expected2 = "Satellite names and NORAD numbers are defined in {path}".format(path=PKG_CONFIG_DIR) @@ -160,13 +164,14 @@ def test_check_is_platform_supported_existing(caplog, mock_env_ppp_config_dir_mi assert expected2 in logoutput_lines[1] -def test_check_is_platform_supported_unknown(caplog, mock_env_ppp_config_dir_missing): +@pytest.mark.usefixtures("_mock_env_ppp_config_dir_missing") +def test_check_is_platform_supported_unknown(caplog): """Test the function to check if an unknown platform is supported on default.""" - sat = 'UNKNOWN' + sat = "UNKNOWN" with caplog.at_level(logging.INFO): check_is_platform_supported(sat) - logoutput_lines = caplog.text.split('\n') + logoutput_lines = caplog.text.split("\n") expected1 = "Satellite {satellite} is NOT supported.".format(satellite=sat) expected2 = ("Please add it to a local copy of the platforms.txt file and put in " + @@ -178,27 +183,18 @@ def test_check_is_platform_supported_unknown(caplog, mock_env_ppp_config_dir_mis assert expected3 in logoutput_lines[2] -@patch( - 'pyorbital.version.get_versions', - return_value=dict([('version', '1.9.1+1.some-futur.dirty'), - ('full-revisionid', 'some-future-git-version-hash'), - ('dirty', True), - ('error', None), - ('date', '2023-01-20T09:37:30+0100') - ]) -) -def test_get_config_path_ppp_config_set_but_not_pyorbital_future(mock, caplog, monkeypatch): - """Test getting the config path.""" - monkeypatch.setenv('SATPY_CONFIG_PATH', '/path/to/satpy/etc') - monkeypatch.setenv('PPP_CONFIG_DIR', '/path/to/old/mpop/config/dir') - - with caplog.at_level(logging.WARNING): - res = _get_config_path() - - log_output = ("The use of PPP_CONFIG_DIR is no longer supported! " + - "Please use PYORBITAL_CONFIG_PATH if you need a custom config path for pyorbital!") - assert log_output in caplog.text - assert res == PKG_CONFIG_DIR +#def test_get_config_path_ppp_config_set_but_not_pyorbital_future(mock, caplog, monkeypatch): +# """Test getting the config path.""" +# monkeypatch.setenv("SATPY_CONFIG_PATH", "/path/to/satpy/etc") +# monkeypatch.setenv("PPP_CONFIG_DIR", "/path/to/old/mpop/config/dir") +# +# with caplog.at_level(logging.WARNING): +# res = _get_config_path() +# +# log_output = ("The use of PPP_CONFIG_DIR is no longer supported! " + +# "Please use PYORBITAL_CONFIG_PATH if you need a custom config path for pyorbital!") +# assert log_output in caplog.text +# assert res == PKG_CONFIG_DIR def test_get_config_path_ppp_config_set_but_not_pyorbital_is_deprecated(caplog, monkeypatch): @@ -208,40 +204,41 @@ def test_get_config_path_ppp_config_set_but_not_pyorbital_is_deprecated(caplog, set but the deprecated (old) Satpy/MPOP one is set. """ - monkeypatch.setenv('SATPY_CONFIG_PATH', '/path/to/satpy/etc') - monkeypatch.setenv('PPP_CONFIG_DIR', '/path/to/old/mpop/config/dir') + monkeypatch.setenv("SATPY_CONFIG_PATH", "/path/to/satpy/etc") + monkeypatch.setenv("PPP_CONFIG_DIR", "/path/to/old/mpop/config/dir") with caplog.at_level(logging.WARNING): res = _get_config_path() - assert res == '/path/to/old/mpop/config/dir' + assert res == "/path/to/old/mpop/config/dir" - log_output = ('The use of PPP_CONFIG_DIR is deprecated and will be removed in version 1.9!' + - ' Please use PYORBITAL_CONFIG_PATH if you need a custom config path for pyorbital!') + log_output = ("The use of PPP_CONFIG_DIR is deprecated and will be removed in version 1.9!" + + " Please use PYORBITAL_CONFIG_PATH if you need a custom config path for pyorbital!") assert log_output in caplog.text def test_get_config_path_ppp_config_set_and_pyorbital(caplog, monkeypatch): """Test getting the config path.""" - pyorbital_config_dir = '/path/to/pyorbital/config/dir' - monkeypatch.setenv('PYORBITAL_CONFIG_PATH', pyorbital_config_dir) - monkeypatch.setenv('PPP_CONFIG_DIR', '/path/to/old/mpop/config/dir') + pyorbital_config_dir = "/path/to/pyorbital/config/dir" + monkeypatch.setenv("PYORBITAL_CONFIG_PATH", pyorbital_config_dir) + monkeypatch.setenv("PPP_CONFIG_DIR", "/path/to/old/mpop/config/dir") with caplog.at_level(logging.WARNING): res = _get_config_path() assert res == pyorbital_config_dir - assert caplog.text == '' + assert caplog.text == "" -def test_get_config_path_pyorbital_ppp_missing(caplog, monkeypatch, mock_env_ppp_config_dir_missing): +@pytest.mark.usefixtures("_mock_env_ppp_config_dir_missing") +def test_get_config_path_pyorbital_ppp_missing(caplog, monkeypatch): """Test getting the config path. The old mpop PPP_CONFIG_PATH is not set but the PYORBITAL one is. """ - pyorbital_config_dir = '/path/to/pyorbital/config/dir' - monkeypatch.setenv('PYORBITAL_CONFIG_PATH', pyorbital_config_dir) + pyorbital_config_dir = "/path/to/pyorbital/config/dir" + monkeypatch.setenv("PYORBITAL_CONFIG_PATH", pyorbital_config_dir) with caplog.at_level(logging.DEBUG): res = _get_config_path() @@ -255,16 +252,18 @@ def test_get_config_path_pyorbital_ppp_missing(caplog, monkeypatch, mock_env_ppp def test_read_platform_numbers(fake_platforms_file): """Test reading the platform names and associated catalougue numbers.""" res = read_platform_numbers(str(fake_platforms_file)) - assert res == {'NOAA-21': '54234', 'NOAA-20': '43013', 'UNKNOWN SATELLITE': '99999'} + assert res == {"NOAA-21": "54234", "NOAA-20": "43013", "UNKNOWN SATELLITE": "99999"} -def test_get_local_tle_path_tle_env_missing(mock_env_tles_missing): +@pytest.mark.usefixtures("_mock_env_tles_missing") +def test_get_local_tle_path_tle_env_missing(): """Test getting the path to local TLE files - env TLES missing.""" res = _get_local_tle_path_from_env() assert res is None -def test_get_local_tle_path(mock_env_tles, fake_local_tles_dir): +@pytest.mark.usefixtures("_mock_env_tles") +def test_get_local_tle_path(fake_local_tles_dir): """Test getting the path to local TLE files.""" res = _get_local_tle_path_from_env() assert res == os.path.join(fake_local_tles_dir, "*") @@ -277,12 +276,12 @@ def test_get_uris_and_open_func_using_tles_env(caplog, fake_local_tles_dir, monk """ from collections.abc import Sequence - monkeypatch.setenv('TLES', str(os.path.join(fake_local_tles_dir, "*"))) + monkeypatch.setenv("TLES", str(os.path.join(fake_local_tles_dir, "*"))) with caplog.at_level(logging.DEBUG): uris, _ = _get_uris_and_open_func() assert isinstance(uris, Sequence) - assert uris[0] == str(fake_local_tles_dir / 'tle-202211180830.txt') + assert uris[0] == str(fake_local_tles_dir / "tle-202211180830.txt") log_message = "Reading TLE from {msg}".format(msg=str(fake_local_tles_dir)) assert log_message in caplog.text @@ -301,43 +300,43 @@ class TLETest(unittest.TestCase): def check_example(self, tle): """Check the *tle* instance against predetermined values.""" # line 1 - self.assertEqual(tle.satnumber, "25544") - self.assertEqual(tle.classification, "U") - self.assertEqual(tle.id_launch_year, "98") - self.assertEqual(tle.id_launch_number, "067") - self.assertEqual(tle.id_launch_piece.strip(), "A") - self.assertEqual(tle.epoch_year, "08") - self.assertEqual(tle.epoch_day, 264.51782528) + assert tle.satnumber == "25544" + assert tle.classification == "U" + assert tle.id_launch_year == "98" + assert tle.id_launch_number == "067" + assert tle.id_launch_piece.strip() == "A" + assert tle.epoch_year == "08" + assert tle.epoch_day == 264.51782528 epoch = (datetime.datetime(2008, 1, 1) + datetime.timedelta(days=264.51782528 - 1)) - self.assertEqual(tle.epoch, epoch) - self.assertEqual(tle.mean_motion_derivative, -.00002182) - self.assertEqual(tle.mean_motion_sec_derivative, 0.0) - self.assertEqual(tle.bstar, -.11606e-4) - self.assertEqual(tle.ephemeris_type, 0) - self.assertEqual(tle.element_number, 292) + assert tle.epoch == epoch + assert tle.mean_motion_derivative == -2.182e-05 + assert tle.mean_motion_sec_derivative == 0.0 + assert tle.bstar == -1.1606e-05 + assert tle.ephemeris_type == 0 + assert tle.element_number == 292 # line 2 - self.assertEqual(tle.inclination, 51.6416) - self.assertEqual(tle.right_ascension, 247.4627) - self.assertEqual(tle.excentricity, .0006703) - self.assertEqual(tle.arg_perigee, 130.5360) - self.assertEqual(tle.mean_anomaly, 325.0288) - self.assertEqual(tle.mean_motion, 15.72125391) - self.assertEqual(tle.orbit, 56353) + assert tle.inclination == 51.6416 + assert tle.right_ascension == 247.4627 + assert tle.excentricity == 0.0006703 + assert tle.arg_perigee == 130.536 + assert tle.mean_anomaly == 325.0288 + assert tle.mean_motion == 15.72125391 + assert tle.orbit == 56353 def test_from_line(self): """Test parsing from line elements.""" - tle = Tle("ISS (ZARYA)", line1=line1, line2=line2) + tle = Tle("ISS (ZARYA)", line1=LINE1, line2=LINE2) self.check_example(tle) def test_from_file(self): """Test reading and parsing from a file.""" + from os import close, remove, write from tempfile import mkstemp - from os import write, close, remove filehandle, filename = mkstemp() try: - write(filehandle, "\n".join([line0, line1, line2]).encode('utf-8')) + write(filehandle, "\n".join([LINE0, LINE1, LINE2]).encode("utf-8")) close(filehandle) tle = Tle("ISS (ZARYA)", filename) self.check_example(tle) @@ -346,11 +345,11 @@ def test_from_file(self): def test_from_file_with_hyphenated_platform_name(self): """Test reading and parsing from a file with a slightly different name.""" + from os import close, remove, write from tempfile import mkstemp - from os import write, close, remove filehandle, filename = mkstemp() try: - write(filehandle, NOAA19_3LINES.encode('utf-8')) + write(filehandle, NOAA19_3LINES.encode("utf-8")) close(filehandle) tle = Tle("NOAA-19", filename) assert tle.satnumber == "33591" @@ -359,11 +358,11 @@ def test_from_file_with_hyphenated_platform_name(self): def test_from_file_with_no_platform_name(self): """Test reading and parsing from a file with a slightly different name.""" + from os import close, remove, write from tempfile import mkstemp - from os import write, close, remove filehandle, filename = mkstemp() try: - write(filehandle, NOAA19_2LINES.encode('utf-8')) + write(filehandle, NOAA19_2LINES.encode("utf-8")) close(filehandle) tle = Tle("NOAA-19", filename) assert tle.satnumber == "33591" @@ -376,8 +375,8 @@ def test_from_mmam_xml(self): save_dir = TemporaryDirectory() with save_dir: - fname = os.path.join(save_dir.name, '20210420_Metop-B_ADMIN_MESSAGE_NO_127.xml') - with open(fname, 'w') as fid: + fname = os.path.join(save_dir.name, "20210420_Metop-B_ADMIN_MESSAGE_NO_127.xml") + with open(fname, "w") as fid: fid.write(tle_xml) tle = Tle("", tle_file=fname) self.check_example(tle) @@ -410,7 +409,7 @@ def test_init(self): """Test the initialization.""" assert self.dl.config is self.config - @mock.patch('pyorbital.tlefile.requests') + @mock.patch("pyorbital.tlefile.requests") def test_fetch_plain_tle_not_configured(self, requests): """Test downloading and a TLE file from internet.""" requests.get = mock.MagicMock() @@ -419,10 +418,10 @@ def test_fetch_plain_tle_not_configured(self, requests): # Not configured self.dl.config["downloaders"] = {} res = self.dl.fetch_plain_tle() - self.assertTrue(res == {}) + assert res == {} requests.get.assert_not_called() - @mock.patch('pyorbital.tlefile.requests') + @mock.patch("pyorbital.tlefile.requests") def test_fetch_plain_tle_two_sources(self, requests): """Test downloading and a TLE file from internet.""" requests.get = mock.MagicMock() @@ -432,16 +431,16 @@ def test_fetch_plain_tle_two_sources(self, requests): self.dl.config["downloaders"] = FETCH_PLAIN_TLE_CONFIG res = self.dl.fetch_plain_tle() - self.assertTrue("source_1" in res) - self.assertEqual(len(res["source_1"]), 3) - self.assertEqual(res["source_1"][0].line1, line1) - self.assertEqual(res["source_1"][0].line2, line2) - self.assertTrue("source_2" in res) - self.assertEqual(len(res["source_2"]), 1) - self.assertTrue(mock.call("mocked_url_1") in requests.get.mock_calls) - self.assertEqual(len(requests.get.mock_calls), 4) - - @mock.patch('pyorbital.tlefile.requests') + assert "source_1" in res + assert len(res["source_1"]) == 3 + assert res["source_1"][0].line1 == LINE1 + assert res["source_1"][0].line2 == LINE2 + assert "source_2" in res + assert len(res["source_2"]) == 1 + assert mock.call("mocked_url_1", timeout=15) in requests.get.mock_calls + assert len(requests.get.mock_calls) == 4 + + @mock.patch("pyorbital.tlefile.requests") def test_fetch_plain_tle_server_is_a_teapot(self, requests): """Test downloading a TLE file from internet.""" requests.get = mock.MagicMock() @@ -453,14 +452,15 @@ def test_fetch_plain_tle_server_is_a_teapot(self, requests): res = self.dl.fetch_plain_tle() # The sources are in the dict ... - self.assertEqual(len(res), 2) + assert len(res) == 2 # ... but there are no TLEs - self.assertEqual(len(res["source_1"]), 0) - self.assertEqual(len(res["source_2"]), 0) - self.assertTrue(mock.call("mocked_url_1") in requests.get.mock_calls) - self.assertEqual(len(requests.get.mock_calls), 4) + assert len(res["source_1"]) == 0 + assert len(res["source_2"]) == 0 + + assert mock.call("mocked_url_1", timeout=15) in requests.get.mock_calls + assert len(requests.get.mock_calls) == 4 - @mock.patch('pyorbital.tlefile.requests') + @mock.patch("pyorbital.tlefile.requests") def test_fetch_spacetrack_login_fails(self, requests): """Test downloading TLEs from space-track.org.""" mock_post = mock.MagicMock() @@ -469,7 +469,7 @@ def test_fetch_spacetrack_login_fails(self, requests): requests.Session.return_value.__enter__.return_value = mock_session self.dl.config["platforms"] = { - 25544: 'ISS' + 25544: "ISS" } self.dl.config["downloaders"] = FETCH_SPACETRACK_CONFIG @@ -477,13 +477,13 @@ def test_fetch_spacetrack_login_fails(self, requests): mock_post.return_value.status_code = 418 res = self.dl.fetch_spacetrack() # Empty list of TLEs is returned - self.assertTrue(res == []) + assert res == [] # The login was anyway attempted mock_post.assert_called_with( - 'https://www.space-track.org/ajaxauth/login', - data={'identity': 'username', 'password': 'passw0rd'}) + "https://www.space-track.org/ajaxauth/login", + data={"identity": "username", "password": "passw0rd"}) - @mock.patch('pyorbital.tlefile.requests') + @mock.patch("pyorbital.tlefile.requests") def test_fetch_spacetrack_get_fails(self, requests): """Test downloading TLEs from space-track.org.""" mock_post = mock.MagicMock() @@ -494,7 +494,7 @@ def test_fetch_spacetrack_get_fails(self, requests): requests.Session.return_value.__enter__.return_value = mock_session self.dl.config["platforms"] = { - 25544: 'ISS' + 25544: "ISS" } self.dl.config["downloaders"] = FETCH_SPACETRACK_CONFIG @@ -502,12 +502,12 @@ def test_fetch_spacetrack_get_fails(self, requests): mock_post.return_value.status_code = 200 mock_get.return_value.status_code = 418 res = self.dl.fetch_spacetrack() - self.assertTrue(res == []) + assert res == [] mock_get.assert_called_with("https://www.space-track.org/" "basicspacedata/query/class/tle_latest/" "ORDINAL/1/NORAD_CAT_ID/25544/format/tle") - @mock.patch('pyorbital.tlefile.requests') + @mock.patch("pyorbital.tlefile.requests") def test_fetch_spacetrack_success(self, requests): """Test downloading TLEs from space-track.org.""" mock_post = mock.MagicMock() @@ -517,9 +517,9 @@ def test_fetch_spacetrack_success(self, requests): mock_session.get = mock_get requests.Session.return_value.__enter__.return_value = mock_session - tle_text = '\n'.join((line0, line1, line2)) + tle_text = "\n".join((LINE0, LINE1, LINE2)) self.dl.config["platforms"] = { - 25544: 'ISS' + 25544: "ISS" } self.dl.config["downloaders"] = FETCH_SPACETRACK_CONFIG @@ -528,34 +528,34 @@ def test_fetch_spacetrack_success(self, requests): mock_get.return_value.status_code = 200 mock_get.return_value.text = tle_text res = self.dl.fetch_spacetrack() - self.assertEqual(len(res), 1) - self.assertEqual(res[0].line1, line1) - self.assertEqual(res[0].line2, line2) + assert len(res) == 1 + assert res[0].line1 == LINE1 + assert res[0].line2 == LINE2 def test_read_tle_files(self): """Test reading TLE files from a file system.""" from tempfile import TemporaryDirectory - tle_text = '\n'.join((line0, line1, line2)) + tle_text = "\n".join((LINE0, LINE1, LINE2)) save_dir = TemporaryDirectory() with save_dir: - fname = os.path.join(save_dir.name, 'tle_20200129_1600.txt') - with open(fname, 'w') as fid: + fname = os.path.join(save_dir.name, "tle_20200129_1600.txt") + with open(fname, "w") as fid: fid.write(tle_text) # Add a non-existent file, it shouldn't cause a crash - nonexistent = os.path.join(save_dir.name, 'not_here.txt') + nonexistent = os.path.join(save_dir.name, "not_here.txt") # Use a wildcard to collect files (passed to glob) - starred_fname = os.path.join(save_dir.name, 'tle*txt') + starred_fname = os.path.join(save_dir.name, "tle*txt") self.dl.config["downloaders"] = { "read_tle_files": { "paths": [fname, nonexistent, starred_fname] } } res = self.dl.read_tle_files() - self.assertEqual(len(res), 2) - self.assertEqual(res[0].line1, line1) - self.assertEqual(res[0].line2, line2) + assert len(res) == 2 + assert res[0].line1 == LINE1 + assert res[0].line2 == LINE2 def test_read_xml_admin_messages(self): """Test reading TLE files from a file system.""" @@ -563,13 +563,13 @@ def test_read_xml_admin_messages(self): save_dir = TemporaryDirectory() with save_dir: - fname = os.path.join(save_dir.name, '20210420_Metop-B_ADMIN_MESSAGE_NO_127.xml') - with open(fname, 'w') as fid: + fname = os.path.join(save_dir.name, "20210420_Metop-B_ADMIN_MESSAGE_NO_127.xml") + with open(fname, "w") as fid: fid.write(tle_xml) # Add a non-existent file, it shouldn't cause a crash - nonexistent = os.path.join(save_dir.name, 'not_here.txt') + nonexistent = os.path.join(save_dir.name, "not_here.txt") # Use a wildcard to collect files (passed to glob) - starred_fname = os.path.join(save_dir.name, '*.xml') + starred_fname = os.path.join(save_dir.name, "*.xml") self.dl.config["downloaders"] = { "read_xml_admin_messages": { "paths": [fname, nonexistent, starred_fname] @@ -579,17 +579,17 @@ def test_read_xml_admin_messages(self): # There are two sets of TLEs in the file. And as the same file is # parsed twice, 4 TLE objects are returned - self.assertEqual(len(res), 4) - self.assertEqual(res[0].line1, line1) - self.assertEqual(res[0].line2, line2) - self.assertEqual(res[1].line1, line1_2) - self.assertEqual(res[1].line2, line2_2) + assert len(res) == 4 + assert res[0].line1 == LINE1 + assert res[0].line2 == LINE2 + assert res[1].line1 == LINE1_2 + assert res[1].line2 == LINE2_2 def _get_req_response(code): req = mock.MagicMock() req.status_code = code - req.text = '\n'.join((line0, line1, line2)) + req.text = "\n".join((LINE0, LINE1, LINE2)) return req @@ -598,21 +598,21 @@ class TestSQLiteTLE(unittest.TestCase): def setUp(self): """Create a database instance.""" - from pyorbital.tlefile import SQLiteTLE - from pyorbital.tlefile import Tle from tempfile import TemporaryDirectory + from pyorbital.tlefile import SQLiteTLE, Tle + self.temp_dir = TemporaryDirectory() - self.db_fname = os.path.join(self.temp_dir.name, 'tle.db') + self.db_fname = os.path.join(self.temp_dir.name, "tle.db") self.platforms = {25544: "ISS"} self.writer_config = { - "output_dir": os.path.join(self.temp_dir.name, 'tle_dir'), + "output_dir": os.path.join(self.temp_dir.name, "tle_dir"), "filename_pattern": "tle_%Y%m%d_%H%M%S.%f.txt", "write_name": True, "write_always": False } self.db = SQLiteTLE(self.db_fname, self.platforms, self.writer_config) - self.tle = Tle('ISS', line1=line1, line2=line2) + self.tle = Tle("ISS", line1=LINE1, line2=LINE2) def tearDown(self): """Clean temporary files.""" @@ -621,73 +621,72 @@ def tearDown(self): def test_init(self): """Test that the init did what it should have.""" - from pyorbital.tlefile import table_exists, PLATFORM_NAMES_TABLE + from pyorbital.tlefile import PLATFORM_NAMES_TABLE, table_exists columns = [col.strip() for col in - PLATFORM_NAMES_TABLE.strip('()').split(',')] + PLATFORM_NAMES_TABLE.strip("()").split(",")] num_columns = len(columns) - self.assertTrue(os.path.exists(self.db_fname)) - self.assertTrue(table_exists(self.db.db, "platform_names")) - res = self.db.db.execute('select * from platform_names') + assert os.path.exists(self.db_fname) + assert table_exists(self.db.db, "platform_names") + res = self.db.db.execute("select * from platform_names") names = [description[0] for description in res.description] - self.assertEqual(len(names), num_columns) + assert len(names) == num_columns for col in columns: - self.assertTrue(col.split(' ')[0] in names) + assert col.split(" ")[0] in names def test_update_db(self): """Test updating database with new data.""" - from pyorbital.tlefile import (table_exists, SATID_TABLE, - ISO_TIME_FORMAT) + from pyorbital.tlefile import ISO_TIME_FORMAT, SATID_TABLE, table_exists # Get the column names columns = [col.strip() for col in - SATID_TABLE.replace("'{}' (", "").strip(')').split(',')] + SATID_TABLE.replace("'{}' (", "").strip(")").split(",")] # Platform number - satid = str(list(self.platforms.keys())[0]) + satid = int(list(self.platforms.keys())[0]) # Data from a platform that isn't configured self.db.platforms = {} - self.db.update_db(self.tle, 'foo') - self.assertFalse(table_exists(self.db.db, satid)) - self.assertFalse(self.db.updated) + self.db.update_db(self.tle, "foo") + assert not table_exists(self.db.db, satid) + assert not self.db.updated # Configured platform self.db.platforms = self.platforms - self.db.update_db(self.tle, 'foo') - self.assertTrue(table_exists(self.db.db, satid)) - self.assertTrue(self.db.updated) + self.db.update_db(self.tle, "foo") + assert table_exists(self.db.db, satid) + assert self.db.updated # Check that all the columns were added - res = self.db.db.execute("select * from '%s'" % satid) + res = self.db.db.execute(f"select * from '{satid:d}'") # noseq names = [description[0] for description in res.description] for col in columns: - self.assertTrue(col.split(' ')[0] in names) + assert col.split(" ")[0] in names # Check the data data = res.fetchall() - self.assertEqual(len(data), 1) + assert len(data) == 1 # epoch - self.assertEqual(data[0][0], '2008-09-20T12:25:40.104192') + assert data[0][0] == "2008-09-20T12:25:40.104192" # TLE - self.assertEqual(data[0][1], '\n'.join((line1, line2))) + assert data[0][1] == "\n".join((LINE1, LINE2)) # Date when the data were added should be close to current time date_added = datetime.datetime.strptime(data[0][2], ISO_TIME_FORMAT) now = datetime.datetime.utcnow() - self.assertTrue((now - date_added).total_seconds() < 1.0) + assert (now - date_added).total_seconds() < 1.0 # Source of the data - self.assertTrue(data[0][3] == 'foo') + assert data[0][3] == "foo" # Try to add the same data again. Nothing should change even # if the source is different if the epoch is the same - self.db.update_db(self.tle, 'bar') - res = self.db.db.execute("select * from '%s'" % satid) + self.db.update_db(self.tle, "bar") + res = self.db.db.execute(f"select * from '{satid:d}'") # noseq data = res.fetchall() - self.assertEqual(len(data), 1) + assert len(data) == 1 date_added2 = datetime.datetime.strptime(data[0][2], ISO_TIME_FORMAT) - self.assertEqual(date_added, date_added2) + assert date_added == date_added2 # Source of the data - self.assertTrue(data[0][3] == 'foo') + assert data[0][3] == "foo" def test_write_tle_txt(self): """Test reading data from the database and writing it to a file.""" @@ -695,7 +694,7 @@ def test_write_tle_txt(self): tle_dir = self.writer_config["output_dir"] # Put some data in the database - self.db.update_db(self.tle, 'foo') + self.db.update_db(self.tle, "foo") # Fake that the database hasn't been updated self.db.updated = False @@ -704,34 +703,34 @@ def test_write_tle_txt(self): self.db.write_tle_txt() # The output dir hasn't been created - self.assertFalse(os.path.exists(tle_dir)) + assert not os.path.exists(tle_dir) self.db.updated = True self.db.write_tle_txt() # The dir should be there - self.assertTrue(os.path.exists(tle_dir)) + assert os.path.exists(tle_dir) # There should be one file in the directory - files = glob.glob(os.path.join(tle_dir, 'tle_*txt')) - self.assertEqual(len(files), 1) + files = glob.glob(os.path.join(tle_dir, "tle_*txt")) + assert len(files) == 1 # The file should have been named with the date ('%' characters # not there anymore) - self.assertTrue('%' not in files[0]) + assert "%" not in files[0] # The satellite name should be in the file - with open(files[0], 'r') as fid: - data = fid.read().split('\n') - self.assertEqual(len(data), 3) - self.assertTrue('ISS' in data[0]) - self.assertEqual(data[1], line1) - self.assertEqual(data[2], line2) + with open(files[0], "r") as fid: + data = fid.read().split("\n") + assert len(data) == 3 + assert "ISS" in data[0] + assert data[1] == LINE1 + assert data[2] == LINE2 # Call the writing again, nothing should be written. In # real-life this assumes a re-run has been done without new # TLE data self.db.updated = False self.db.write_tle_txt() - files = glob.glob(os.path.join(tle_dir, 'tle_*txt')) - self.assertEqual(len(files), 1) + files = glob.glob(os.path.join(tle_dir, "tle_*txt")) + assert len(files) == 1 # Force writing with every call # Do not write the satellite name @@ -740,10 +739,18 @@ def test_write_tle_txt(self): # Wait a bit to ensure different filename time.sleep(2) self.db.write_tle_txt() - files = sorted(glob.glob(os.path.join(tle_dir, 'tle_*txt'))) - self.assertEqual(len(files), 2) - with open(files[1], 'r') as fid: - data = fid.read().split('\n') - self.assertEqual(len(data), 2) - self.assertEqual(data[0], line1) - self.assertEqual(data[1], line2) + files = sorted(glob.glob(os.path.join(tle_dir, "tle_*txt"))) + assert len(files) == 2 + with open(files[1], "r") as fid: + data = fid.read().split("\n") + assert len(data) == 2 + assert data[0] == LINE1 + assert data[1] == LINE2 + +def test_tle_instance_printing(): + """Test the print the Tle instance.""" + tle = Tle("ISS", line1=LINE1, line2=LINE2) + + expected = "{'arg_perigee': 130.536,\n 'bstar': -1.1606e-05,\n 'classification': 'U',\n 'element_number': 292,\n 'ephemeris_type': 0,\n 'epoch': np.datetime64('2008-09-20T12:25:40.104192'),\n 'epoch_day': 264.51782528,\n 'epoch_year': '08',\n 'excentricity': 0.0006703,\n 'id_launch_number': '067',\n 'id_launch_piece': 'A ',\n 'id_launch_year': '98',\n 'inclination': 51.6416,\n 'mean_anomaly': 325.0288,\n 'mean_motion': 15.72125391,\n 'mean_motion_derivative': -2.182e-05,\n 'mean_motion_sec_derivative': 0.0,\n 'orbit': 56353,\n 'right_ascension': 247.4627,\n 'satnumber': '25544'}" # noqa + + assert str(tle) == expected diff --git a/pyorbital/tlefile.py b/pyorbital/tlefile.py index d2f04100..e8f03052 100644 --- a/pyorbital/tlefile.py +++ b/pyorbital/tlefile.py @@ -1,7 +1,7 @@ #!/usr/bin/env python # -*- coding: utf-8 -*- # -# Copyright (c) 2011-2023 Pytroll Community +# Copyright (c) 2011-2024 Pytroll Community # # Author(s): # @@ -25,58 +25,56 @@ """Classes and functions for handling TLE files.""" +import datetime as dt +import glob import io import logging -import datetime as dt -from urllib.request import urlopen import os -import glob -import numpy as np -import requests import sqlite3 -from xml.etree import ElementTree as ET from itertools import zip_longest +from urllib.request import urlopen + +#from xml.etree import ElementTree as ET +import defusedxml.ElementTree as ET +import numpy as np +import requests -TLE_GROUPS = ('active', - 'weather', - 'resource', - 'cubesat', - 'stations', - 'sarsat', - 'noaa', - 'amateur', - 'engineering') - -TLE_URLS = [f'https://celestrak.org/NORAD/elements/gp.php?GROUP={group}&FORMAT=tle' +TLE_GROUPS = ("active", + "weather", + "resource", + "cubesat", + "stations", + "sarsat", + "noaa", + "amateur", + "engineering") + +TLE_URLS = [f"https://celestrak.org/NORAD/elements/gp.php?GROUP={group}&FORMAT=tle" for group in TLE_GROUPS] LOGGER = logging.getLogger(__name__) -PKG_CONFIG_DIR = os.path.join(os.path.realpath(os.path.dirname(__file__)), 'etc') - +PKG_CONFIG_DIR = os.path.join(os.path.realpath(os.path.dirname(__file__)), "etc") -def _check_support_limit_ppp_config_dir(): - """Check the version where PPP_CONFIG_DIR will no longer be supported.""" - from pyorbital import version - return version.get_versions()['version'] >= '1.9' +class TleDownloadTimeoutError(Exception): + """TLE download timeout exception.""" def _get_config_path(): """Get the config path for Pyorbital.""" - if 'PPP_CONFIG_DIR' in os.environ and 'PYORBITAL_CONFIG_PATH' not in os.environ: - if _check_support_limit_ppp_config_dir(): - LOGGER.warning( - 'The use of PPP_CONFIG_DIR is no longer supported!' + - ' Please use PYORBITAL_CONFIG_PATH if you need a custom config path for pyorbital!') - LOGGER.debug('Using the package default for configuration: %s', PKG_CONFIG_DIR) - return PKG_CONFIG_DIR - else: - LOGGER.warning( - 'The use of PPP_CONFIG_DIR is deprecated and will be removed in version 1.9!' + - ' Please use PYORBITAL_CONFIG_PATH if you need a custom config path for pyorbital!') - pyorbital_config_path = os.getenv('PPP_CONFIG_DIR', PKG_CONFIG_DIR) + if "PPP_CONFIG_DIR" in os.environ and "PYORBITAL_CONFIG_PATH" not in os.environ: + # XXX: Swap when pyorbital 1.9 is released + #LOGGER.warning( + # "The use of PPP_CONFIG_DIR is no longer supported!" + + # " Please use PYORBITAL_CONFIG_PATH if you need a custom config path for pyorbital!") + #LOGGER.debug("Using the package default for configuration: %s", PKG_CONFIG_DIR) + #return PKG_CONFIG_DIR + LOGGER.warning( + "The use of PPP_CONFIG_DIR is deprecated and will be removed in version 1.9!" + + " Please use PYORBITAL_CONFIG_PATH if you need a custom config path for pyorbital!") + pyorbital_config_path = os.getenv("PPP_CONFIG_DIR", PKG_CONFIG_DIR) else: - pyorbital_config_path = os.getenv('PYORBITAL_CONFIG_PATH', PKG_CONFIG_DIR) + pyorbital_config_path = os.getenv("PYORBITAL_CONFIG_PATH", PKG_CONFIG_DIR) LOGGER.debug("Path to the Pyorbital configuration (where e.g. platforms.txt is found): %s", str(pyorbital_config_path)) @@ -89,9 +87,9 @@ def get_platforms_filepath(): Check that the file exists or raise an error. """ config_path = _get_config_path() - platform_file = os.path.join(config_path, 'platforms.txt') + platform_file = os.path.join(config_path, "platforms.txt") if not os.path.isfile(platform_file): - platform_file = os.path.join(PKG_CONFIG_DIR, 'platforms.txt') + platform_file = os.path.join(PKG_CONFIG_DIR, "platforms.txt") if not os.path.isfile(platform_file): raise OSError("Platform file {filepath} does not exist!".format(filepath=platform_file)) @@ -102,15 +100,15 @@ def read_platform_numbers(filename, in_upper=False, num_as_int=False): """Read platform numbers from $PYORBITAL_CONFIG_PATH/platforms.txt.""" out_dict = {} - with open(filename, 'r') as fid: + with open(filename, "r") as fid: for row in fid: # skip comment lines - if not row.startswith('#'): + if not row.startswith("#"): parts = row.split() if len(parts) < 2: continue # The satellite name might have whitespace - platform = ' '.join(parts[:-1]) + platform = " ".join(parts[:-1]) num = parts[-1] if in_upper: platform = platform.upper() @@ -166,7 +164,9 @@ def fetch(destination): """Fetch TLE from internet and save it to `destination`.""" with io.open(destination, mode="w", encoding="utf-8") as dest: for url in TLE_URLS: - response = urlopen(url) + if not url.lower().startswith("http"): + raise ValueError(f"{str(url)} is not accepted!") + response = urlopen(url) # nosec dest.write(response.read().decode("utf-8")) @@ -248,7 +248,7 @@ def _read_tle(self): if not tle: raise KeyError("Found no TLE entry for '%s'" % self._platform) - self._line1, self._line2 = tle.split('\n') + self._line1, self._line2 = tle.split("\n") def _parse_tle(self): """Parse values from TLE data.""" @@ -272,7 +272,7 @@ def _read_tle_decimal(rep): self.epoch_day = float(self._line1[20:32]) self.epoch = \ np.datetime64(dt.datetime.strptime(self.epoch_year, "%y") + - dt.timedelta(days=self.epoch_day - 1), 'us') + dt.timedelta(days=self.epoch_day - 1), "us") self.mean_motion_derivative = float(self._line1[33:43]) self.mean_motion_sec_derivative = _read_tle_decimal(self._line1[44:52]) self.bstar = _read_tle_decimal(self._line1[53:61]) @@ -295,20 +295,20 @@ def __str__(self): import pprint s_var = io.StringIO() d_var = dict(([(k, v) for k, v in - list(self.__dict__.items()) if k[0] != '_'])) + list(self.__dict__.items()) if k[0] != "_"])) pprint.pprint(d_var, s_var) return s_var.getvalue()[:-1] def _get_local_tle_path_from_env(): """Get the path to possible local TLE files using the environment variable.""" - return os.environ.get('TLES') + return os.environ.get("TLES") def _get_uris_and_open_func(tle_file=None): """Get the uri's and the adequate file open call for the TLE files.""" def _open(filename): - return io.open(filename, 'rb') + return io.open(filename, "rb") local_tle_path = _get_local_tle_path_from_env() @@ -337,13 +337,13 @@ def _open(filename): return uris, open_func -def _get_first_tle(uris, open_func, platform=''): +def _get_first_tle(uris, open_func, platform=""): return _get_tles_from_uris(uris, open_func, platform=platform, only_first=True) -def _get_tles_from_uris(uris, open_func, platform='', only_first=True): +def _get_tles_from_uris(uris, open_func, platform="", only_first=True): tles = [] - designator = "1 " + SATELLITES.get(platform, '') + designator = "1 " + SATELLITES.get(platform, "") for url in uris: fid = open_func(url) for l_0 in fid: @@ -375,7 +375,7 @@ def _get_tles_from_uris(uris, open_func, platform='', only_first=True): def _decode(itm): if isinstance(itm, str): return itm - return itm.decode('utf-8') + return itm.decode("utf-8") PLATFORM_NAMES_TABLE = "(satid text primary key, platform_name text)" @@ -402,7 +402,10 @@ def fetch_plain_tle(self): tles[source] = [] failures = [] for uri in sources[source]: - req = requests.get(uri) + try: + req = requests.get(uri, timeout=15) # 15 seconds + except requests.exceptions.Timeout: + raise TleDownloadTimeoutError(f"Failed to make request to {str(uri)} within 15 seconds!") if req.status_code == 200: tles[source] += _parse_tles_for_downloader((req.text,), io.StringIO) else: @@ -410,7 +413,7 @@ def fetch_plain_tle(self): if len(failures) > 0: logging.error( "Could not fetch TLEs from %s, %d failure(s): [%s]", - source, len(failures), ', '.join(failures)) + source, len(failures), ", ".join(failures)) logging.info("Downloaded %d TLEs from %s", len(tles[source]), source) return tles @@ -422,8 +425,8 @@ def fetch_spacetrack(self): download_url = ("https://www.space-track.org/basicspacedata/query/" "class/tle_latest/ORDINAL/1/NORAD_CAT_ID/%s/format/" "tle") - download_url = download_url % ','.join( - [str(key) for key in self.config['platforms']]) + download_url = download_url % ",".join( + [str(key) for key in self.config["platforms"]]) user = self.config["downloaders"]["fetch_spacetrack"]["user"] password = self.config["downloaders"]["fetch_spacetrack"]["password"] @@ -470,15 +473,15 @@ def read_xml_admin_messages(self): def _parse_tles_for_downloader(item, open_func): - return [Tle('', tle_file=io.StringIO(tle)) for tle in - _get_tles_from_uris(item, open_func, platform='', only_first=False)] + return [Tle("", tle_file=io.StringIO(tle)) for tle in + _get_tles_from_uris(item, open_func, platform="", only_first=False)] def collect_filenames(paths): """Collect all filenames from *paths*.""" fnames = [] for path in paths: - if '*' in path: + if "*" in path: fnames += glob.glob(path) else: if not os.path.exists(path): @@ -494,10 +497,10 @@ def read_tles_from_mmam_xml_files(paths): fnames = collect_filenames(paths) tles = [] for fname in fnames: - data = read_tle_from_mmam_xml_file(fname).split('\n') + data = read_tle_from_mmam_xml_file(fname).split("\n") for two_lines in _group_iterable_to_chunks(2, data): - tl_stream = io.StringIO('\n'.join(two_lines)) - tles.append(Tle('', tle_file=tl_stream)) + tl_stream = io.StringIO("\n".join(two_lines)) + tles.append(Tle("", tle_file=tl_stream)) return tles @@ -559,7 +562,7 @@ def update_db(self, tle, source): self.platforms[num], num) cmd = SATID_VALUES.format(num) epoch = tle.epoch.item().isoformat() - tle = '\n'.join([tle.line1, tle.line2]) + tle = "\n".join([tle.line1, tle.line2]) now = dt.datetime.utcnow().isoformat() try: with self.db: @@ -572,7 +575,7 @@ def update_db(self, tle, source): def write_tle_txt(self): """Write TLE data to a text file.""" - if not self.updated and not self.writer_config.get('write_always', + if not self.updated and not self.writer_config.get("write_always", False): return pattern = os.path.join(self.writer_config["output_dir"], @@ -588,9 +591,8 @@ def write_tle_txt(self): for satid, platform_name in self.platforms.items(): if self.writer_config.get("write_name", False): data.append(platform_name) - query = ("SELECT epoch, tle FROM '%s' ORDER BY " - "epoch DESC LIMIT 1" % satid) - epoch, tle = self.db.execute(query).fetchone() + query = f"SELECT epoch, tle FROM '{satid:d}' ORDER BY epoch DESC LIMIT 1" # nosec + epoch, tle = self.db.execute(query).fetchone() # nosec date_epoch = dt.datetime.strptime(epoch, ISO_TIME_FORMAT) tle_age = ( dt.datetime.utcnow() - date_epoch).total_seconds() / 3600. @@ -598,8 +600,8 @@ def write_tle_txt(self): satid, platform_name, int(tle_age)) data.append(tle) - with open(fname, 'w') as fid: - fid.write('\n'.join(data)) + with open(fname, "w") as fid: + fid.write("\n".join(data)) logging.info("Wrote %d TLEs to %s", len(data), fname) @@ -612,14 +614,14 @@ def table_exists(db, name): """Check if the table 'name' exists in the database.""" name = str(name) query = "SELECT 1 FROM sqlite_master WHERE type='table' and name=?" - return db.execute(query, (name,)).fetchone() is not None + return db.execute(query, (name,)).fetchone() is not None # nosec def main(): """Run a test TLE reading.""" - tle_data = read('Noaa-19') + tle_data = read("Noaa-19") print(tle_data) -if __name__ == '__main__': +if __name__ == "__main__": main() diff --git a/pyorbital/version.py b/pyorbital/version.py deleted file mode 100644 index 152e2d52..00000000 --- a/pyorbital/version.py +++ /dev/null @@ -1,658 +0,0 @@ - -# This file helps to compute a version number in source trees obtained from -# git-archive tarball (such as those provided by githubs download-from-tag -# feature). Distribution tarballs (built by setup.py sdist) and build -# directories (produced by setup.py build) will contain a much shorter file -# that just contains the computed version number. - -# This file is released into the public domain. -# Generated by versioneer-0.28 -# https://github.com/python-versioneer/python-versioneer - -"""Git implementation of _version.py.""" - -import errno -import os -import re -import subprocess -import sys -from typing import Callable, Dict -import functools - - -def get_keywords(): - """Get the keywords needed to look up the version information.""" - # these strings will be replaced by git during git-archive. - # setup.py/versioneer.py will grep for the variable names, so they must - # each be defined on a line of their own. _version.py will just call - # get_keywords(). - git_refnames = "$Format:%d$" - git_full = "$Format:%H$" - git_date = "$Format:%ci$" - keywords = {"refnames": git_refnames, "full": git_full, "date": git_date} - return keywords - - -class VersioneerConfig: - """Container for Versioneer configuration parameters.""" - - -def get_config(): - """Create, populate and return the VersioneerConfig() object.""" - # these strings are filled in when 'setup.py versioneer' creates - # _version.py - cfg = VersioneerConfig() - cfg.VCS = "git" - cfg.style = "pep440" - cfg.tag_prefix = "v" - cfg.parentdir_prefix = "None" - cfg.versionfile_source = "pyorbital/version.py" - cfg.verbose = False - return cfg - - -class NotThisMethod(Exception): - """Exception raised if a method is not valid for the current scenario.""" - - -LONG_VERSION_PY: Dict[str, str] = {} -HANDLERS: Dict[str, Dict[str, Callable]] = {} - - -def register_vcs_handler(vcs, method): # decorator - """Create decorator to mark a method as the handler of a VCS.""" - def decorate(f): - """Store f in HANDLERS[vcs][method].""" - if vcs not in HANDLERS: - HANDLERS[vcs] = {} - HANDLERS[vcs][method] = f - return f - return decorate - - -def run_command(commands, args, cwd=None, verbose=False, hide_stderr=False, - env=None): - """Call the given command(s).""" - assert isinstance(commands, list) - process = None - - popen_kwargs = {} - if sys.platform == "win32": - # This hides the console window if pythonw.exe is used - startupinfo = subprocess.STARTUPINFO() - startupinfo.dwFlags |= subprocess.STARTF_USESHOWWINDOW - popen_kwargs["startupinfo"] = startupinfo - - for command in commands: - try: - dispcmd = str([command] + args) - # remember shell=False, so use git.cmd on windows, not just git - process = subprocess.Popen([command] + args, cwd=cwd, env=env, - stdout=subprocess.PIPE, - stderr=(subprocess.PIPE if hide_stderr - else None), **popen_kwargs) - break - except OSError: - e = sys.exc_info()[1] - if e.errno == errno.ENOENT: - continue - if verbose: - print("unable to run %s" % dispcmd) - print(e) - return None, None - else: - if verbose: - print("unable to find command, tried %s" % (commands,)) - return None, None - stdout = process.communicate()[0].strip().decode() - if process.returncode != 0: - if verbose: - print("unable to run %s (error)" % dispcmd) - print("stdout was %s" % stdout) - return None, process.returncode - return stdout, process.returncode - - -def versions_from_parentdir(parentdir_prefix, root, verbose): - """Try to determine the version from the parent directory name. - - Source tarballs conventionally unpack into a directory that includes both - the project name and a version string. We will also support searching up - two directory levels for an appropriately named parent directory - """ - rootdirs = [] - - for _ in range(3): - dirname = os.path.basename(root) - if dirname.startswith(parentdir_prefix): - return {"version": dirname[len(parentdir_prefix):], - "full-revisionid": None, - "dirty": False, "error": None, "date": None} - rootdirs.append(root) - root = os.path.dirname(root) # up a level - - if verbose: - print("Tried directories %s but none started with prefix %s" % - (str(rootdirs), parentdir_prefix)) - raise NotThisMethod("rootdir doesn't start with parentdir_prefix") - - -@register_vcs_handler("git", "get_keywords") -def git_get_keywords(versionfile_abs): - """Extract version information from the given file.""" - # the code embedded in _version.py can just fetch the value of these - # keywords. When used from setup.py, we don't want to import _version.py, - # so we do it with a regexp instead. This function is not used from - # _version.py. - keywords = {} - try: - with open(versionfile_abs, "r") as fobj: - for line in fobj: - if line.strip().startswith("git_refnames ="): - mo = re.search(r'=\s*"(.*)"', line) - if mo: - keywords["refnames"] = mo.group(1) - if line.strip().startswith("git_full ="): - mo = re.search(r'=\s*"(.*)"', line) - if mo: - keywords["full"] = mo.group(1) - if line.strip().startswith("git_date ="): - mo = re.search(r'=\s*"(.*)"', line) - if mo: - keywords["date"] = mo.group(1) - except OSError: - pass - return keywords - - -@register_vcs_handler("git", "keywords") -def git_versions_from_keywords(keywords, tag_prefix, verbose): - """Get version information from git keywords.""" - if "refnames" not in keywords: - raise NotThisMethod("Short version file found") - date = keywords.get("date") - if date is not None: - # Use only the last line. Previous lines may contain GPG signature - # information. - date = date.splitlines()[-1] - - # git-2.2.0 added "%cI", which expands to an ISO-8601 -compliant - # datestamp. However we prefer "%ci" (which expands to an "ISO-8601 - # -like" string, which we must then edit to make compliant), because - # it's been around since git-1.5.3, and it's too difficult to - # discover which version we're using, or to work around using an - # older one. - date = date.strip().replace(" ", "T", 1).replace(" ", "", 1) - refnames = keywords["refnames"].strip() - if refnames.startswith("$Format"): - if verbose: - print("keywords are unexpanded, not using") - raise NotThisMethod("unexpanded keywords, not a git-archive tarball") - refs = {r.strip() for r in refnames.strip("()").split(",")} - # starting in git-1.8.3, tags are listed as "tag: foo-1.0" instead of - # just "foo-1.0". If we see a "tag: " prefix, prefer those. - TAG = "tag: " - tags = {r[len(TAG):] for r in refs if r.startswith(TAG)} - if not tags: - # Either we're using git < 1.8.3, or there really are no tags. We use - # a heuristic: assume all version tags have a digit. The old git %d - # expansion behaves like git log --decorate=short and strips out the - # refs/heads/ and refs/tags/ prefixes that would let us distinguish - # between branches and tags. By ignoring refnames without digits, we - # filter out many common branch names like "release" and - # "stabilization", as well as "HEAD" and "master". - tags = {r for r in refs if re.search(r'\d', r)} - if verbose: - print("discarding '%s', no digits" % ",".join(refs - tags)) - if verbose: - print("likely tags: %s" % ",".join(sorted(tags))) - for ref in sorted(tags): - # sorting will prefer e.g. "2.0" over "2.0rc1" - if ref.startswith(tag_prefix): - r = ref[len(tag_prefix):] - # Filter out refs that exactly match prefix or that don't start - # with a number once the prefix is stripped (mostly a concern - # when prefix is '') - if not re.match(r'\d', r): - continue - if verbose: - print("picking %s" % r) - return {"version": r, - "full-revisionid": keywords["full"].strip(), - "dirty": False, "error": None, - "date": date} - # no suitable tags, so version is "0+unknown", but full hex is still there - if verbose: - print("no suitable tags, using unknown + full revision id") - return {"version": "0+unknown", - "full-revisionid": keywords["full"].strip(), - "dirty": False, "error": "no suitable tags", "date": None} - - -@register_vcs_handler("git", "pieces_from_vcs") -def git_pieces_from_vcs(tag_prefix, root, verbose, runner=run_command): - """Get version from 'git describe' in the root of the source tree. - - This only gets called if the git-archive 'subst' keywords were *not* - expanded, and _version.py hasn't already been rewritten with a short - version string, meaning we're inside a checked out source tree. - """ - GITS = ["git"] - if sys.platform == "win32": - GITS = ["git.cmd", "git.exe"] - - # GIT_DIR can interfere with correct operation of Versioneer. - # It may be intended to be passed to the Versioneer-versioned project, - # but that should not change where we get our version from. - env = os.environ.copy() - env.pop("GIT_DIR", None) - runner = functools.partial(runner, env=env) - - _, rc = runner(GITS, ["rev-parse", "--git-dir"], cwd=root, - hide_stderr=not verbose) - if rc != 0: - if verbose: - print("Directory %s not under git control" % root) - raise NotThisMethod("'git rev-parse --git-dir' returned error") - - # if there is a tag matching tag_prefix, this yields TAG-NUM-gHEX[-dirty] - # if there isn't one, this yields HEX[-dirty] (no NUM) - describe_out, rc = runner(GITS, [ - "describe", "--tags", "--dirty", "--always", "--long", - "--match", f"{tag_prefix}[[:digit:]]*" - ], cwd=root) - # --long was added in git-1.5.5 - if describe_out is None: - raise NotThisMethod("'git describe' failed") - describe_out = describe_out.strip() - full_out, rc = runner(GITS, ["rev-parse", "HEAD"], cwd=root) - if full_out is None: - raise NotThisMethod("'git rev-parse' failed") - full_out = full_out.strip() - - pieces = {} - pieces["long"] = full_out - pieces["short"] = full_out[:7] # maybe improved later - pieces["error"] = None - - branch_name, rc = runner(GITS, ["rev-parse", "--abbrev-ref", "HEAD"], - cwd=root) - # --abbrev-ref was added in git-1.6.3 - if rc != 0 or branch_name is None: - raise NotThisMethod("'git rev-parse --abbrev-ref' returned error") - branch_name = branch_name.strip() - - if branch_name == "HEAD": - # If we aren't exactly on a branch, pick a branch which represents - # the current commit. If all else fails, we are on a branchless - # commit. - branches, rc = runner(GITS, ["branch", "--contains"], cwd=root) - # --contains was added in git-1.5.4 - if rc != 0 or branches is None: - raise NotThisMethod("'git branch --contains' returned error") - branches = branches.split("\n") - - # Remove the first line if we're running detached - if "(" in branches[0]: - branches.pop(0) - - # Strip off the leading "* " from the list of branches. - branches = [branch[2:] for branch in branches] - if "master" in branches: - branch_name = "master" - elif not branches: - branch_name = None - else: - # Pick the first branch that is returned. Good or bad. - branch_name = branches[0] - - pieces["branch"] = branch_name - - # parse describe_out. It will be like TAG-NUM-gHEX[-dirty] or HEX[-dirty] - # TAG might have hyphens. - git_describe = describe_out - - # look for -dirty suffix - dirty = git_describe.endswith("-dirty") - pieces["dirty"] = dirty - if dirty: - git_describe = git_describe[:git_describe.rindex("-dirty")] - - # now we have TAG-NUM-gHEX or HEX - - if "-" in git_describe: - # TAG-NUM-gHEX - mo = re.search(r'^(.+)-(\d+)-g([0-9a-f]+)$', git_describe) - if not mo: - # unparsable. Maybe git-describe is misbehaving? - pieces["error"] = ("unable to parse git-describe output: '%s'" - % describe_out) - return pieces - - # tag - full_tag = mo.group(1) - if not full_tag.startswith(tag_prefix): - if verbose: - fmt = "tag '%s' doesn't start with prefix '%s'" - print(fmt % (full_tag, tag_prefix)) - pieces["error"] = ("tag '%s' doesn't start with prefix '%s'" - % (full_tag, tag_prefix)) - return pieces - pieces["closest-tag"] = full_tag[len(tag_prefix):] - - # distance: number of commits since tag - pieces["distance"] = int(mo.group(2)) - - # commit: short hex revision ID - pieces["short"] = mo.group(3) - - else: - # HEX: no tags - pieces["closest-tag"] = None - out, rc = runner(GITS, ["rev-list", "HEAD", "--left-right"], cwd=root) - pieces["distance"] = len(out.split()) # total number of commits - - # commit date: see ISO-8601 comment in git_versions_from_keywords() - date = runner(GITS, ["show", "-s", "--format=%ci", "HEAD"], cwd=root)[0].strip() - # Use only the last line. Previous lines may contain GPG signature - # information. - date = date.splitlines()[-1] - pieces["date"] = date.strip().replace(" ", "T", 1).replace(" ", "", 1) - - return pieces - - -def plus_or_dot(pieces): - """Return a + if we don't already have one, else return a .""" - if "+" in pieces.get("closest-tag", ""): - return "." - return "+" - - -def render_pep440(pieces): - """Build up version string, with post-release "local version identifier". - - Our goal: TAG[+DISTANCE.gHEX[.dirty]] . Note that if you - get a tagged build and then dirty it, you'll get TAG+0.gHEX.dirty - - Exceptions: - 1: no tags. git_describe was just HEX. 0+untagged.DISTANCE.gHEX[.dirty] - """ - if pieces["closest-tag"]: - rendered = pieces["closest-tag"] - if pieces["distance"] or pieces["dirty"]: - rendered += plus_or_dot(pieces) - rendered += "%d.g%s" % (pieces["distance"], pieces["short"]) - if pieces["dirty"]: - rendered += ".dirty" - else: - # exception #1 - rendered = "0+untagged.%d.g%s" % (pieces["distance"], - pieces["short"]) - if pieces["dirty"]: - rendered += ".dirty" - return rendered - - -def render_pep440_branch(pieces): - """TAG[[.dev0]+DISTANCE.gHEX[.dirty]] . - - The ".dev0" means not master branch. Note that .dev0 sorts backwards - (a feature branch will appear "older" than the master branch). - - Exceptions: - 1: no tags. 0[.dev0]+untagged.DISTANCE.gHEX[.dirty] - """ - if pieces["closest-tag"]: - rendered = pieces["closest-tag"] - if pieces["distance"] or pieces["dirty"]: - if pieces["branch"] != "master": - rendered += ".dev0" - rendered += plus_or_dot(pieces) - rendered += "%d.g%s" % (pieces["distance"], pieces["short"]) - if pieces["dirty"]: - rendered += ".dirty" - else: - # exception #1 - rendered = "0" - if pieces["branch"] != "master": - rendered += ".dev0" - rendered += "+untagged.%d.g%s" % (pieces["distance"], - pieces["short"]) - if pieces["dirty"]: - rendered += ".dirty" - return rendered - - -def pep440_split_post(ver): - """Split pep440 version string at the post-release segment. - - Returns the release segments before the post-release and the - post-release version number (or -1 if no post-release segment is present). - """ - vc = str.split(ver, ".post") - return vc[0], int(vc[1] or 0) if len(vc) == 2 else None - - -def render_pep440_pre(pieces): - """TAG[.postN.devDISTANCE] -- No -dirty. - - Exceptions: - 1: no tags. 0.post0.devDISTANCE - """ - if pieces["closest-tag"]: - if pieces["distance"]: - # update the post release segment - tag_version, post_version = pep440_split_post(pieces["closest-tag"]) - rendered = tag_version - if post_version is not None: - rendered += ".post%d.dev%d" % (post_version + 1, pieces["distance"]) - else: - rendered += ".post0.dev%d" % (pieces["distance"]) - else: - # no commits, use the tag as the version - rendered = pieces["closest-tag"] - else: - # exception #1 - rendered = "0.post0.dev%d" % pieces["distance"] - return rendered - - -def render_pep440_post(pieces): - """TAG[.postDISTANCE[.dev0]+gHEX] . - - The ".dev0" means dirty. Note that .dev0 sorts backwards - (a dirty tree will appear "older" than the corresponding clean one), - but you shouldn't be releasing software with -dirty anyways. - - Exceptions: - 1: no tags. 0.postDISTANCE[.dev0] - """ - if pieces["closest-tag"]: - rendered = pieces["closest-tag"] - if pieces["distance"] or pieces["dirty"]: - rendered += ".post%d" % pieces["distance"] - if pieces["dirty"]: - rendered += ".dev0" - rendered += plus_or_dot(pieces) - rendered += "g%s" % pieces["short"] - else: - # exception #1 - rendered = "0.post%d" % pieces["distance"] - if pieces["dirty"]: - rendered += ".dev0" - rendered += "+g%s" % pieces["short"] - return rendered - - -def render_pep440_post_branch(pieces): - """TAG[.postDISTANCE[.dev0]+gHEX[.dirty]] . - - The ".dev0" means not master branch. - - Exceptions: - 1: no tags. 0.postDISTANCE[.dev0]+gHEX[.dirty] - """ - if pieces["closest-tag"]: - rendered = pieces["closest-tag"] - if pieces["distance"] or pieces["dirty"]: - rendered += ".post%d" % pieces["distance"] - if pieces["branch"] != "master": - rendered += ".dev0" - rendered += plus_or_dot(pieces) - rendered += "g%s" % pieces["short"] - if pieces["dirty"]: - rendered += ".dirty" - else: - # exception #1 - rendered = "0.post%d" % pieces["distance"] - if pieces["branch"] != "master": - rendered += ".dev0" - rendered += "+g%s" % pieces["short"] - if pieces["dirty"]: - rendered += ".dirty" - return rendered - - -def render_pep440_old(pieces): - """TAG[.postDISTANCE[.dev0]] . - - The ".dev0" means dirty. - - Exceptions: - 1: no tags. 0.postDISTANCE[.dev0] - """ - if pieces["closest-tag"]: - rendered = pieces["closest-tag"] - if pieces["distance"] or pieces["dirty"]: - rendered += ".post%d" % pieces["distance"] - if pieces["dirty"]: - rendered += ".dev0" - else: - # exception #1 - rendered = "0.post%d" % pieces["distance"] - if pieces["dirty"]: - rendered += ".dev0" - return rendered - - -def render_git_describe(pieces): - """TAG[-DISTANCE-gHEX][-dirty]. - - Like 'git describe --tags --dirty --always'. - - Exceptions: - 1: no tags. HEX[-dirty] (note: no 'g' prefix) - """ - if pieces["closest-tag"]: - rendered = pieces["closest-tag"] - if pieces["distance"]: - rendered += "-%d-g%s" % (pieces["distance"], pieces["short"]) - else: - # exception #1 - rendered = pieces["short"] - if pieces["dirty"]: - rendered += "-dirty" - return rendered - - -def render_git_describe_long(pieces): - """TAG-DISTANCE-gHEX[-dirty]. - - Like 'git describe --tags --dirty --always -long'. - The distance/hash is unconditional. - - Exceptions: - 1: no tags. HEX[-dirty] (note: no 'g' prefix) - """ - if pieces["closest-tag"]: - rendered = pieces["closest-tag"] - rendered += "-%d-g%s" % (pieces["distance"], pieces["short"]) - else: - # exception #1 - rendered = pieces["short"] - if pieces["dirty"]: - rendered += "-dirty" - return rendered - - -def render(pieces, style): - """Render the given version pieces into the requested style.""" - if pieces["error"]: - return {"version": "unknown", - "full-revisionid": pieces.get("long"), - "dirty": None, - "error": pieces["error"], - "date": None} - - if not style or style == "default": - style = "pep440" # the default - - if style == "pep440": - rendered = render_pep440(pieces) - elif style == "pep440-branch": - rendered = render_pep440_branch(pieces) - elif style == "pep440-pre": - rendered = render_pep440_pre(pieces) - elif style == "pep440-post": - rendered = render_pep440_post(pieces) - elif style == "pep440-post-branch": - rendered = render_pep440_post_branch(pieces) - elif style == "pep440-old": - rendered = render_pep440_old(pieces) - elif style == "git-describe": - rendered = render_git_describe(pieces) - elif style == "git-describe-long": - rendered = render_git_describe_long(pieces) - else: - raise ValueError("unknown style '%s'" % style) - - return {"version": rendered, "full-revisionid": pieces["long"], - "dirty": pieces["dirty"], "error": None, - "date": pieces.get("date")} - - -def get_versions(): - """Get version information or return default if unable to do so.""" - # I am in _version.py, which lives at ROOT/VERSIONFILE_SOURCE. If we have - # __file__, we can work backwards from there to the root. Some - # py2exe/bbfreeze/non-CPython implementations don't do __file__, in which - # case we can only use expanded keywords. - - cfg = get_config() - verbose = cfg.verbose - - try: - return git_versions_from_keywords(get_keywords(), cfg.tag_prefix, - verbose) - except NotThisMethod: - pass - - try: - root = os.path.realpath(__file__) - # versionfile_source is the relative path from the top of the source - # tree (where the .git directory might live) to this file. Invert - # this to find the root from __file__. - for _ in cfg.versionfile_source.split('/'): - root = os.path.dirname(root) - except NameError: - return {"version": "0+unknown", "full-revisionid": None, - "dirty": None, - "error": "unable to find root of source tree", - "date": None} - - try: - pieces = git_pieces_from_vcs(cfg.tag_prefix, root, verbose) - return render(pieces, cfg.style) - except NotThisMethod: - pass - - try: - if cfg.parentdir_prefix: - return versions_from_parentdir(cfg.parentdir_prefix, root, verbose) - except NotThisMethod: - pass - - return {"version": "0+unknown", "full-revisionid": None, - "dirty": None, - "error": "unable to compute version", "date": None} diff --git a/pyproject.toml b/pyproject.toml new file mode 100644 index 00000000..788f65ac --- /dev/null +++ b/pyproject.toml @@ -0,0 +1,92 @@ +[project] +name = "pyorbital" +dynamic = ["version"] +description = "Scheduling satellite passes in Python" +authors = [ + { name = "The Pytroll Team", email = "pytroll@googlegroups.com" } +] +dependencies = ["numpy>=1.19.0", + "scipy", + "requests", + "pytz", + "defusedxml", + ] +readme = "README.md" +requires-python = ">=3.10" +license = {file = "LICENSE.txt"} +classifiers = [ + "Development Status :: 5 - Production/Stable", + "Intended Audience :: Science/Research", + "License :: OSI Approved :: GNU General Public License v3 or later (GPLv3+)", + "Operating System :: OS Independent", + "Programming Language :: Python", + "Topic :: Scientific/Engineering", + "Topic :: Scientific/Engineering :: Astronomy" +] + +[project.scripts] +"fetch_tles.py" = "pyorbital.fetch_tles:run" +fetch_tles = "pyorbital.fetch_tles:run" + +[project.urls] +"Documentation" = "https://pyorbital.readthedocs.io/en/latest/" + +[project.optional-dependencies] +doc = ["sphinx", "sphinx_rtd_theme", "sphinxcontrib-apidoc"] + +[build-system] +requires = ["hatchling", "hatch-vcs"] +build-backend = "hatchling.build" + +[tool.rye] +managed = true +dev-dependencies = [] + +[tool.hatch.metadata] +allow-direct-references = true + +[tool.hatch.build.targets.wheel] +packages = ["pyorbital"] + +[tool.hatch.version] +source = "vcs" + +[tool.hatch.build.hooks.vcs] +version-file = "pyorbital/version.py" + +[tool.isort] +sections = ["FUTURE", "STDLIB", "THIRDPARTY", "FIRSTPARTY", "LOCALFOLDER"] +profile = "black" +skip_gitignore = true +default_section = "THIRDPARTY" +known_first_party = "pyorbital" +line_length = 120 + +[tool.ruff] +line-length = 120 +extend-exclude = ["pyorbital/tests/SGP4-VER.TLE"] + +[tool.ruff.lint] +# See https://docs.astral.sh/ruff/rules/ +# In the future, add "B", "S", "N" +select = ["A", "D", "E", "W", "F", "I", "PT", "TID", "C90", "Q", "T10", "T20", "NPY"] + +[tool.ruff.lint.per-file-ignores] +"pyorbital/tests/*" = ["S101"] # assert allowed in tests +"pyorbital/version.py" = ["D100", "Q000"] # automatically generated by hatch-vcs +"pyorbital/tlefile.py" = ["T201", "T203"] # allow print and pprint +"pyorbital/geoloc.py" = ["T201"] # allow print +"pyorbital/geoloc_example.py" = ["T201"] # allow print +"pyorbital/orbital.py" = ["T201"] # allow print + + +[tool.ruff.lint.pydocstyle] +convention = "google" + +[tool.ruff.lint.mccabe] +# Unlike Flake8, default to a complexity level of 10. +max-complexity = 10 + +[tool.coverage.run] +relative_files = true +omit = ["pyorbital/version.py"] diff --git a/setup.cfg b/setup.cfg deleted file mode 100644 index 5ba5071e..00000000 --- a/setup.cfg +++ /dev/null @@ -1,23 +0,0 @@ -[bdist_rpm] -requires=numpy -release=1 -doc_files = doc/Makefile doc/source/*.rst - -[bdist_wheel] -universal=1 - -[flake8] -max-line-length = 120 - -[versioneer] -VCS = git -style = pep440 -versionfile_source = pyorbital/version.py -versionfile_build = -tag_prefix = v - -[coverage:run] -relative_files = True -omit = - pyorbital/version.py - versioneer.py diff --git a/setup.py b/setup.py deleted file mode 100644 index 43b8fbe8..00000000 --- a/setup.py +++ /dev/null @@ -1,62 +0,0 @@ -#!/usr/bin/env python -# -*- coding: utf-8 -*- -# -# Copyright (c) 2011-2023 Pytroll Community -# -# Author(s): -# -# Martin Raspaud -# Panu Lahtinen -# Adam Dybbroe -# -# This program is free software: you can redistribute it and/or modify -# it under the terms of the GNU General Public License as published by -# the Free Software Foundation, either version 3 of the License, or -# (at your option) any later version. -# -# This program is distributed in the hope that it will be useful, -# but WITHOUT ANY WARRANTY; without even the implied warranty of -# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -# GNU General Public License for more details. -# -# You should have received a copy of the GNU General Public License -# along with this program. If not, see . - -"""Setup for pyorbital.""" - -import os -from setuptools import setup, find_packages -import versioneer - -try: - with open('./README.md', 'r') as fd: - long_description = fd.read() -except IOError: - long_description = '' - - -setup(name='pyorbital', - version=versioneer.get_version(), - cmdclass=versioneer.get_cmdclass(), - description='Orbital parameters and astronomical computations in Python', - author='The Pytroll Team', - author_email='pytroll@googlegroups.com', - classifiers=["Development Status :: 5 - Production/Stable", - "Intended Audience :: Science/Research", - "License :: OSI Approved :: GNU General Public License v3 " + - "or later (GPLv3+)", - "Operating System :: OS Independent", - "Programming Language :: Python", - "Topic :: Scientific/Engineering", - "Topic :: Scientific/Engineering :: Astronomy"], - url="https://github.com/pytroll/pyorbital", - long_description=long_description, - long_description_content_type='text/markdown', - packages=find_packages(), - package_data={'pyorbital': [os.path.join('etc', 'platforms.txt')]}, - scripts=['bin/fetch_tles.py', ], - install_requires=['numpy>=1.19.0', 'scipy', 'requests'], - python_requires='>=3.9', - extras_require={"doc": ["sphinx", "sphinx_rtd_theme", "sphinxcontrib-apidoc"]}, - zip_safe=False, - ) diff --git a/versioneer.py b/versioneer.py deleted file mode 100644 index 18e34c2f..00000000 --- a/versioneer.py +++ /dev/null @@ -1,2205 +0,0 @@ - -# Version: 0.28 - -"""The Versioneer - like a rocketeer, but for versions. - -The Versioneer -============== - -* like a rocketeer, but for versions! -* https://github.com/python-versioneer/python-versioneer -* Brian Warner -* License: Public Domain (Unlicense) -* Compatible with: Python 3.7, 3.8, 3.9, 3.10 and pypy3 -* [![Latest Version][pypi-image]][pypi-url] -* [![Build Status][travis-image]][travis-url] - -This is a tool for managing a recorded version number in setuptools-based -python projects. The goal is to remove the tedious and error-prone "update -the embedded version string" step from your release process. Making a new -release should be as easy as recording a new tag in your version-control -system, and maybe making new tarballs. - - -## Quick Install - -Versioneer provides two installation modes. The "classic" vendored mode installs -a copy of versioneer into your repository. The experimental build-time dependency mode -is intended to allow you to skip this step and simplify the process of upgrading. - -### Vendored mode - -* `pip install versioneer` to somewhere in your $PATH - * A [conda-forge recipe](https://github.com/conda-forge/versioneer-feedstock) is - available, so you can also use `conda install -c conda-forge versioneer` -* add a `[tool.versioneer]` section to your `pyproject.toml` or a - `[versioneer]` section to your `setup.cfg` (see [Install](INSTALL.md)) - * Note that you will need to add `tomli; python_version < "3.11"` to your - build-time dependencies if you use `pyproject.toml` -* run `versioneer install --vendor` in your source tree, commit the results -* verify version information with `python setup.py version` - -### Build-time dependency mode - -* `pip install versioneer` to somewhere in your $PATH - * A [conda-forge recipe](https://github.com/conda-forge/versioneer-feedstock) is - available, so you can also use `conda install -c conda-forge versioneer` -* add a `[tool.versioneer]` section to your `pyproject.toml` or a - `[versioneer]` section to your `setup.cfg` (see [Install](INSTALL.md)) -* add `versioneer` (with `[toml]` extra, if configuring in `pyproject.toml`) - to the `requires` key of the `build-system` table in `pyproject.toml`: - ```toml - [build-system] - requires = ["setuptools", "versioneer[toml]"] - build-backend = "setuptools.build_meta" - ``` -* run `versioneer install --no-vendor` in your source tree, commit the results -* verify version information with `python setup.py version` - -## Version Identifiers - -Source trees come from a variety of places: - -* a version-control system checkout (mostly used by developers) -* a nightly tarball, produced by build automation -* a snapshot tarball, produced by a web-based VCS browser, like github's - "tarball from tag" feature -* a release tarball, produced by "setup.py sdist", distributed through PyPI - -Within each source tree, the version identifier (either a string or a number, -this tool is format-agnostic) can come from a variety of places: - -* ask the VCS tool itself, e.g. "git describe" (for checkouts), which knows - about recent "tags" and an absolute revision-id -* the name of the directory into which the tarball was unpacked -* an expanded VCS keyword ($Id$, etc) -* a `_version.py` created by some earlier build step - -For released software, the version identifier is closely related to a VCS -tag. Some projects use tag names that include more than just the version -string (e.g. "myproject-1.2" instead of just "1.2"), in which case the tool -needs to strip the tag prefix to extract the version identifier. For -unreleased software (between tags), the version identifier should provide -enough information to help developers recreate the same tree, while also -giving them an idea of roughly how old the tree is (after version 1.2, before -version 1.3). Many VCS systems can report a description that captures this, -for example `git describe --tags --dirty --always` reports things like -"0.7-1-g574ab98-dirty" to indicate that the checkout is one revision past the -0.7 tag, has a unique revision id of "574ab98", and is "dirty" (it has -uncommitted changes). - -The version identifier is used for multiple purposes: - -* to allow the module to self-identify its version: `myproject.__version__` -* to choose a name and prefix for a 'setup.py sdist' tarball - -## Theory of Operation - -Versioneer works by adding a special `_version.py` file into your source -tree, where your `__init__.py` can import it. This `_version.py` knows how to -dynamically ask the VCS tool for version information at import time. - -`_version.py` also contains `$Revision$` markers, and the installation -process marks `_version.py` to have this marker rewritten with a tag name -during the `git archive` command. As a result, generated tarballs will -contain enough information to get the proper version. - -To allow `setup.py` to compute a version too, a `versioneer.py` is added to -the top level of your source tree, next to `setup.py` and the `setup.cfg` -that configures it. This overrides several distutils/setuptools commands to -compute the version when invoked, and changes `setup.py build` and `setup.py -sdist` to replace `_version.py` with a small static file that contains just -the generated version data. - -## Installation - -See [INSTALL.md](./INSTALL.md) for detailed installation instructions. - -## Version-String Flavors - -Code which uses Versioneer can learn about its version string at runtime by -importing `_version` from your main `__init__.py` file and running the -`get_versions()` function. From the "outside" (e.g. in `setup.py`), you can -import the top-level `versioneer.py` and run `get_versions()`. - -Both functions return a dictionary with different flavors of version -information: - -* `['version']`: A condensed version string, rendered using the selected - style. This is the most commonly used value for the project's version - string. The default "pep440" style yields strings like `0.11`, - `0.11+2.g1076c97`, or `0.11+2.g1076c97.dirty`. See the "Styles" section - below for alternative styles. - -* `['full-revisionid']`: detailed revision identifier. For Git, this is the - full SHA1 commit id, e.g. "1076c978a8d3cfc70f408fe5974aa6c092c949ac". - -* `['date']`: Date and time of the latest `HEAD` commit. For Git, it is the - commit date in ISO 8601 format. This will be None if the date is not - available. - -* `['dirty']`: a boolean, True if the tree has uncommitted changes. Note that - this is only accurate if run in a VCS checkout, otherwise it is likely to - be False or None - -* `['error']`: if the version string could not be computed, this will be set - to a string describing the problem, otherwise it will be None. It may be - useful to throw an exception in setup.py if this is set, to avoid e.g. - creating tarballs with a version string of "unknown". - -Some variants are more useful than others. Including `full-revisionid` in a -bug report should allow developers to reconstruct the exact code being tested -(or indicate the presence of local changes that should be shared with the -developers). `version` is suitable for display in an "about" box or a CLI -`--version` output: it can be easily compared against release notes and lists -of bugs fixed in various releases. - -The installer adds the following text to your `__init__.py` to place a basic -version in `YOURPROJECT.__version__`: - - from ._version import get_versions - __version__ = get_versions()['version'] - del get_versions - -## Styles - -The setup.cfg `style=` configuration controls how the VCS information is -rendered into a version string. - -The default style, "pep440", produces a PEP440-compliant string, equal to the -un-prefixed tag name for actual releases, and containing an additional "local -version" section with more detail for in-between builds. For Git, this is -TAG[+DISTANCE.gHEX[.dirty]] , using information from `git describe --tags ---dirty --always`. For example "0.11+2.g1076c97.dirty" indicates that the -tree is like the "1076c97" commit but has uncommitted changes (".dirty"), and -that this commit is two revisions ("+2") beyond the "0.11" tag. For released -software (exactly equal to a known tag), the identifier will only contain the -stripped tag, e.g. "0.11". - -Other styles are available. See [details.md](details.md) in the Versioneer -source tree for descriptions. - -## Debugging - -Versioneer tries to avoid fatal errors: if something goes wrong, it will tend -to return a version of "0+unknown". To investigate the problem, run `setup.py -version`, which will run the version-lookup code in a verbose mode, and will -display the full contents of `get_versions()` (including the `error` string, -which may help identify what went wrong). - -## Known Limitations - -Some situations are known to cause problems for Versioneer. This details the -most significant ones. More can be found on Github -[issues page](https://github.com/python-versioneer/python-versioneer/issues). - -### Subprojects - -Versioneer has limited support for source trees in which `setup.py` is not in -the root directory (e.g. `setup.py` and `.git/` are *not* siblings). The are -two common reasons why `setup.py` might not be in the root: - -* Source trees which contain multiple subprojects, such as - [Buildbot](https://github.com/buildbot/buildbot), which contains both - "master" and "slave" subprojects, each with their own `setup.py`, - `setup.cfg`, and `tox.ini`. Projects like these produce multiple PyPI - distributions (and upload multiple independently-installable tarballs). -* Source trees whose main purpose is to contain a C library, but which also - provide bindings to Python (and perhaps other languages) in subdirectories. - -Versioneer will look for `.git` in parent directories, and most operations -should get the right version string. However `pip` and `setuptools` have bugs -and implementation details which frequently cause `pip install .` from a -subproject directory to fail to find a correct version string (so it usually -defaults to `0+unknown`). - -`pip install --editable .` should work correctly. `setup.py install` might -work too. - -Pip-8.1.1 is known to have this problem, but hopefully it will get fixed in -some later version. - -[Bug #38](https://github.com/python-versioneer/python-versioneer/issues/38) is tracking -this issue. The discussion in -[PR #61](https://github.com/python-versioneer/python-versioneer/pull/61) describes the -issue from the Versioneer side in more detail. -[pip PR#3176](https://github.com/pypa/pip/pull/3176) and -[pip PR#3615](https://github.com/pypa/pip/pull/3615) contain work to improve -pip to let Versioneer work correctly. - -Versioneer-0.16 and earlier only looked for a `.git` directory next to the -`setup.cfg`, so subprojects were completely unsupported with those releases. - -### Editable installs with setuptools <= 18.5 - -`setup.py develop` and `pip install --editable .` allow you to install a -project into a virtualenv once, then continue editing the source code (and -test) without re-installing after every change. - -"Entry-point scripts" (`setup(entry_points={"console_scripts": ..})`) are a -convenient way to specify executable scripts that should be installed along -with the python package. - -These both work as expected when using modern setuptools. When using -setuptools-18.5 or earlier, however, certain operations will cause -`pkg_resources.DistributionNotFound` errors when running the entrypoint -script, which must be resolved by re-installing the package. This happens -when the install happens with one version, then the egg_info data is -regenerated while a different version is checked out. Many setup.py commands -cause egg_info to be rebuilt (including `sdist`, `wheel`, and installing into -a different virtualenv), so this can be surprising. - -[Bug #83](https://github.com/python-versioneer/python-versioneer/issues/83) describes -this one, but upgrading to a newer version of setuptools should probably -resolve it. - - -## Updating Versioneer - -To upgrade your project to a new release of Versioneer, do the following: - -* install the new Versioneer (`pip install -U versioneer` or equivalent) -* edit `setup.cfg` and `pyproject.toml`, if necessary, - to include any new configuration settings indicated by the release notes. - See [UPGRADING](./UPGRADING.md) for details. -* re-run `versioneer install --[no-]vendor` in your source tree, to replace - `SRC/_version.py` -* commit any changed files - -## Future Directions - -This tool is designed to make it easily extended to other version-control -systems: all VCS-specific components are in separate directories like -src/git/ . The top-level `versioneer.py` script is assembled from these -components by running make-versioneer.py . In the future, make-versioneer.py -will take a VCS name as an argument, and will construct a version of -`versioneer.py` that is specific to the given VCS. It might also take the -configuration arguments that are currently provided manually during -installation by editing setup.py . Alternatively, it might go the other -direction and include code from all supported VCS systems, reducing the -number of intermediate scripts. - -## Similar projects - -* [setuptools_scm](https://github.com/pypa/setuptools_scm/) - a non-vendored build-time - dependency -* [minver](https://github.com/jbweston/miniver) - a lightweight reimplementation of - versioneer -* [versioningit](https://github.com/jwodder/versioningit) - a PEP 518-based setuptools - plugin - -## License - -To make Versioneer easier to embed, all its code is dedicated to the public -domain. The `_version.py` that it creates is also in the public domain. -Specifically, both are released under the "Unlicense", as described in -https://unlicense.org/. - -[pypi-image]: https://img.shields.io/pypi/v/versioneer.svg -[pypi-url]: https://pypi.python.org/pypi/versioneer/ -[travis-image]: -https://img.shields.io/travis/com/python-versioneer/python-versioneer.svg -[travis-url]: https://travis-ci.com/github/python-versioneer/python-versioneer - -""" -# pylint:disable=invalid-name,import-outside-toplevel,missing-function-docstring -# pylint:disable=missing-class-docstring,too-many-branches,too-many-statements -# pylint:disable=raise-missing-from,too-many-lines,too-many-locals,import-error -# pylint:disable=too-few-public-methods,redefined-outer-name,consider-using-with -# pylint:disable=attribute-defined-outside-init,too-many-arguments - -import configparser -import errno -import json -import os -import re -import subprocess -import sys -from pathlib import Path -from typing import Callable, Dict -import functools - -have_tomllib = True -if sys.version_info >= (3, 11): - import tomllib -else: - try: - import tomli as tomllib - except ImportError: - have_tomllib = False - - -class VersioneerConfig: - """Container for Versioneer configuration parameters.""" - - -def get_root(): - """Get the project root directory. - - We require that all commands are run from the project root, i.e. the - directory that contains setup.py, setup.cfg, and versioneer.py . - """ - root = os.path.realpath(os.path.abspath(os.getcwd())) - setup_py = os.path.join(root, "setup.py") - versioneer_py = os.path.join(root, "versioneer.py") - if not (os.path.exists(setup_py) or os.path.exists(versioneer_py)): - # allow 'python path/to/setup.py COMMAND' - root = os.path.dirname(os.path.realpath(os.path.abspath(sys.argv[0]))) - setup_py = os.path.join(root, "setup.py") - versioneer_py = os.path.join(root, "versioneer.py") - if not (os.path.exists(setup_py) or os.path.exists(versioneer_py)): - err = ("Versioneer was unable to run the project root directory. " - "Versioneer requires setup.py to be executed from " - "its immediate directory (like 'python setup.py COMMAND'), " - "or in a way that lets it use sys.argv[0] to find the root " - "(like 'python path/to/setup.py COMMAND').") - raise VersioneerBadRootError(err) - try: - # Certain runtime workflows (setup.py install/develop in a setuptools - # tree) execute all dependencies in a single python process, so - # "versioneer" may be imported multiple times, and python's shared - # module-import table will cache the first one. So we can't use - # os.path.dirname(__file__), as that will find whichever - # versioneer.py was first imported, even in later projects. - my_path = os.path.realpath(os.path.abspath(__file__)) - me_dir = os.path.normcase(os.path.splitext(my_path)[0]) - vsr_dir = os.path.normcase(os.path.splitext(versioneer_py)[0]) - if me_dir != vsr_dir and "VERSIONEER_PEP518" not in globals(): - print("Warning: build in %s is using versioneer.py from %s" - % (os.path.dirname(my_path), versioneer_py)) - except NameError: - pass - return root - - -def get_config_from_root(root): - """Read the project setup.cfg file to determine Versioneer config.""" - # This might raise OSError (if setup.cfg is missing), or - # configparser.NoSectionError (if it lacks a [versioneer] section), or - # configparser.NoOptionError (if it lacks "VCS="). See the docstring at - # the top of versioneer.py for instructions on writing your setup.cfg . - root = Path(root) - pyproject_toml = root / "pyproject.toml" - setup_cfg = root / "setup.cfg" - section = None - if pyproject_toml.exists() and have_tomllib: - try: - with open(pyproject_toml, 'rb') as fobj: - pp = tomllib.load(fobj) - section = pp['tool']['versioneer'] - except (tomllib.TOMLDecodeError, KeyError): - pass - if not section: - parser = configparser.ConfigParser() - with open(setup_cfg) as cfg_file: - parser.read_file(cfg_file) - parser.get("versioneer", "VCS") # raise error if missing - - section = parser["versioneer"] - - cfg = VersioneerConfig() - cfg.VCS = section['VCS'] - cfg.style = section.get("style", "") - cfg.versionfile_source = section.get("versionfile_source") - cfg.versionfile_build = section.get("versionfile_build") - cfg.tag_prefix = section.get("tag_prefix") - if cfg.tag_prefix in ("''", '""', None): - cfg.tag_prefix = "" - cfg.parentdir_prefix = section.get("parentdir_prefix") - cfg.verbose = section.get("verbose") - return cfg - - -class NotThisMethod(Exception): - """Exception raised if a method is not valid for the current scenario.""" - - -# these dictionaries contain VCS-specific tools -LONG_VERSION_PY: Dict[str, str] = {} -HANDLERS: Dict[str, Dict[str, Callable]] = {} - - -def register_vcs_handler(vcs, method): # decorator - """Create decorator to mark a method as the handler of a VCS.""" - def decorate(f): - """Store f in HANDLERS[vcs][method].""" - HANDLERS.setdefault(vcs, {})[method] = f - return f - return decorate - - -def run_command(commands, args, cwd=None, verbose=False, hide_stderr=False, - env=None): - """Call the given command(s).""" - assert isinstance(commands, list) - process = None - - popen_kwargs = {} - if sys.platform == "win32": - # This hides the console window if pythonw.exe is used - startupinfo = subprocess.STARTUPINFO() - startupinfo.dwFlags |= subprocess.STARTF_USESHOWWINDOW - popen_kwargs["startupinfo"] = startupinfo - - for command in commands: - try: - dispcmd = str([command] + args) - # remember shell=False, so use git.cmd on windows, not just git - process = subprocess.Popen([command] + args, cwd=cwd, env=env, - stdout=subprocess.PIPE, - stderr=(subprocess.PIPE if hide_stderr - else None), **popen_kwargs) - break - except OSError: - e = sys.exc_info()[1] - if e.errno == errno.ENOENT: - continue - if verbose: - print("unable to run %s" % dispcmd) - print(e) - return None, None - else: - if verbose: - print("unable to find command, tried %s" % (commands,)) - return None, None - stdout = process.communicate()[0].strip().decode() - if process.returncode != 0: - if verbose: - print("unable to run %s (error)" % dispcmd) - print("stdout was %s" % stdout) - return None, process.returncode - return stdout, process.returncode - - -LONG_VERSION_PY['git'] = r''' -# This file helps to compute a version number in source trees obtained from -# git-archive tarball (such as those provided by githubs download-from-tag -# feature). Distribution tarballs (built by setup.py sdist) and build -# directories (produced by setup.py build) will contain a much shorter file -# that just contains the computed version number. - -# This file is released into the public domain. -# Generated by versioneer-0.28 -# https://github.com/python-versioneer/python-versioneer - -"""Git implementation of _version.py.""" - -import errno -import os -import re -import subprocess -import sys -from typing import Callable, Dict -import functools - - -def get_keywords(): - """Get the keywords needed to look up the version information.""" - # these strings will be replaced by git during git-archive. - # setup.py/versioneer.py will grep for the variable names, so they must - # each be defined on a line of their own. _version.py will just call - # get_keywords(). - git_refnames = "%(DOLLAR)sFormat:%%d%(DOLLAR)s" - git_full = "%(DOLLAR)sFormat:%%H%(DOLLAR)s" - git_date = "%(DOLLAR)sFormat:%%ci%(DOLLAR)s" - keywords = {"refnames": git_refnames, "full": git_full, "date": git_date} - return keywords - - -class VersioneerConfig: - """Container for Versioneer configuration parameters.""" - - -def get_config(): - """Create, populate and return the VersioneerConfig() object.""" - # these strings are filled in when 'setup.py versioneer' creates - # _version.py - cfg = VersioneerConfig() - cfg.VCS = "git" - cfg.style = "%(STYLE)s" - cfg.tag_prefix = "%(TAG_PREFIX)s" - cfg.parentdir_prefix = "%(PARENTDIR_PREFIX)s" - cfg.versionfile_source = "%(VERSIONFILE_SOURCE)s" - cfg.verbose = False - return cfg - - -class NotThisMethod(Exception): - """Exception raised if a method is not valid for the current scenario.""" - - -LONG_VERSION_PY: Dict[str, str] = {} -HANDLERS: Dict[str, Dict[str, Callable]] = {} - - -def register_vcs_handler(vcs, method): # decorator - """Create decorator to mark a method as the handler of a VCS.""" - def decorate(f): - """Store f in HANDLERS[vcs][method].""" - if vcs not in HANDLERS: - HANDLERS[vcs] = {} - HANDLERS[vcs][method] = f - return f - return decorate - - -def run_command(commands, args, cwd=None, verbose=False, hide_stderr=False, - env=None): - """Call the given command(s).""" - assert isinstance(commands, list) - process = None - - popen_kwargs = {} - if sys.platform == "win32": - # This hides the console window if pythonw.exe is used - startupinfo = subprocess.STARTUPINFO() - startupinfo.dwFlags |= subprocess.STARTF_USESHOWWINDOW - popen_kwargs["startupinfo"] = startupinfo - - for command in commands: - try: - dispcmd = str([command] + args) - # remember shell=False, so use git.cmd on windows, not just git - process = subprocess.Popen([command] + args, cwd=cwd, env=env, - stdout=subprocess.PIPE, - stderr=(subprocess.PIPE if hide_stderr - else None), **popen_kwargs) - break - except OSError: - e = sys.exc_info()[1] - if e.errno == errno.ENOENT: - continue - if verbose: - print("unable to run %%s" %% dispcmd) - print(e) - return None, None - else: - if verbose: - print("unable to find command, tried %%s" %% (commands,)) - return None, None - stdout = process.communicate()[0].strip().decode() - if process.returncode != 0: - if verbose: - print("unable to run %%s (error)" %% dispcmd) - print("stdout was %%s" %% stdout) - return None, process.returncode - return stdout, process.returncode - - -def versions_from_parentdir(parentdir_prefix, root, verbose): - """Try to determine the version from the parent directory name. - - Source tarballs conventionally unpack into a directory that includes both - the project name and a version string. We will also support searching up - two directory levels for an appropriately named parent directory - """ - rootdirs = [] - - for _ in range(3): - dirname = os.path.basename(root) - if dirname.startswith(parentdir_prefix): - return {"version": dirname[len(parentdir_prefix):], - "full-revisionid": None, - "dirty": False, "error": None, "date": None} - rootdirs.append(root) - root = os.path.dirname(root) # up a level - - if verbose: - print("Tried directories %%s but none started with prefix %%s" %% - (str(rootdirs), parentdir_prefix)) - raise NotThisMethod("rootdir doesn't start with parentdir_prefix") - - -@register_vcs_handler("git", "get_keywords") -def git_get_keywords(versionfile_abs): - """Extract version information from the given file.""" - # the code embedded in _version.py can just fetch the value of these - # keywords. When used from setup.py, we don't want to import _version.py, - # so we do it with a regexp instead. This function is not used from - # _version.py. - keywords = {} - try: - with open(versionfile_abs, "r") as fobj: - for line in fobj: - if line.strip().startswith("git_refnames ="): - mo = re.search(r'=\s*"(.*)"', line) - if mo: - keywords["refnames"] = mo.group(1) - if line.strip().startswith("git_full ="): - mo = re.search(r'=\s*"(.*)"', line) - if mo: - keywords["full"] = mo.group(1) - if line.strip().startswith("git_date ="): - mo = re.search(r'=\s*"(.*)"', line) - if mo: - keywords["date"] = mo.group(1) - except OSError: - pass - return keywords - - -@register_vcs_handler("git", "keywords") -def git_versions_from_keywords(keywords, tag_prefix, verbose): - """Get version information from git keywords.""" - if "refnames" not in keywords: - raise NotThisMethod("Short version file found") - date = keywords.get("date") - if date is not None: - # Use only the last line. Previous lines may contain GPG signature - # information. - date = date.splitlines()[-1] - - # git-2.2.0 added "%%cI", which expands to an ISO-8601 -compliant - # datestamp. However we prefer "%%ci" (which expands to an "ISO-8601 - # -like" string, which we must then edit to make compliant), because - # it's been around since git-1.5.3, and it's too difficult to - # discover which version we're using, or to work around using an - # older one. - date = date.strip().replace(" ", "T", 1).replace(" ", "", 1) - refnames = keywords["refnames"].strip() - if refnames.startswith("$Format"): - if verbose: - print("keywords are unexpanded, not using") - raise NotThisMethod("unexpanded keywords, not a git-archive tarball") - refs = {r.strip() for r in refnames.strip("()").split(",")} - # starting in git-1.8.3, tags are listed as "tag: foo-1.0" instead of - # just "foo-1.0". If we see a "tag: " prefix, prefer those. - TAG = "tag: " - tags = {r[len(TAG):] for r in refs if r.startswith(TAG)} - if not tags: - # Either we're using git < 1.8.3, or there really are no tags. We use - # a heuristic: assume all version tags have a digit. The old git %%d - # expansion behaves like git log --decorate=short and strips out the - # refs/heads/ and refs/tags/ prefixes that would let us distinguish - # between branches and tags. By ignoring refnames without digits, we - # filter out many common branch names like "release" and - # "stabilization", as well as "HEAD" and "master". - tags = {r for r in refs if re.search(r'\d', r)} - if verbose: - print("discarding '%%s', no digits" %% ",".join(refs - tags)) - if verbose: - print("likely tags: %%s" %% ",".join(sorted(tags))) - for ref in sorted(tags): - # sorting will prefer e.g. "2.0" over "2.0rc1" - if ref.startswith(tag_prefix): - r = ref[len(tag_prefix):] - # Filter out refs that exactly match prefix or that don't start - # with a number once the prefix is stripped (mostly a concern - # when prefix is '') - if not re.match(r'\d', r): - continue - if verbose: - print("picking %%s" %% r) - return {"version": r, - "full-revisionid": keywords["full"].strip(), - "dirty": False, "error": None, - "date": date} - # no suitable tags, so version is "0+unknown", but full hex is still there - if verbose: - print("no suitable tags, using unknown + full revision id") - return {"version": "0+unknown", - "full-revisionid": keywords["full"].strip(), - "dirty": False, "error": "no suitable tags", "date": None} - - -@register_vcs_handler("git", "pieces_from_vcs") -def git_pieces_from_vcs(tag_prefix, root, verbose, runner=run_command): - """Get version from 'git describe' in the root of the source tree. - - This only gets called if the git-archive 'subst' keywords were *not* - expanded, and _version.py hasn't already been rewritten with a short - version string, meaning we're inside a checked out source tree. - """ - GITS = ["git"] - if sys.platform == "win32": - GITS = ["git.cmd", "git.exe"] - - # GIT_DIR can interfere with correct operation of Versioneer. - # It may be intended to be passed to the Versioneer-versioned project, - # but that should not change where we get our version from. - env = os.environ.copy() - env.pop("GIT_DIR", None) - runner = functools.partial(runner, env=env) - - _, rc = runner(GITS, ["rev-parse", "--git-dir"], cwd=root, - hide_stderr=not verbose) - if rc != 0: - if verbose: - print("Directory %%s not under git control" %% root) - raise NotThisMethod("'git rev-parse --git-dir' returned error") - - # if there is a tag matching tag_prefix, this yields TAG-NUM-gHEX[-dirty] - # if there isn't one, this yields HEX[-dirty] (no NUM) - describe_out, rc = runner(GITS, [ - "describe", "--tags", "--dirty", "--always", "--long", - "--match", f"{tag_prefix}[[:digit:]]*" - ], cwd=root) - # --long was added in git-1.5.5 - if describe_out is None: - raise NotThisMethod("'git describe' failed") - describe_out = describe_out.strip() - full_out, rc = runner(GITS, ["rev-parse", "HEAD"], cwd=root) - if full_out is None: - raise NotThisMethod("'git rev-parse' failed") - full_out = full_out.strip() - - pieces = {} - pieces["long"] = full_out - pieces["short"] = full_out[:7] # maybe improved later - pieces["error"] = None - - branch_name, rc = runner(GITS, ["rev-parse", "--abbrev-ref", "HEAD"], - cwd=root) - # --abbrev-ref was added in git-1.6.3 - if rc != 0 or branch_name is None: - raise NotThisMethod("'git rev-parse --abbrev-ref' returned error") - branch_name = branch_name.strip() - - if branch_name == "HEAD": - # If we aren't exactly on a branch, pick a branch which represents - # the current commit. If all else fails, we are on a branchless - # commit. - branches, rc = runner(GITS, ["branch", "--contains"], cwd=root) - # --contains was added in git-1.5.4 - if rc != 0 or branches is None: - raise NotThisMethod("'git branch --contains' returned error") - branches = branches.split("\n") - - # Remove the first line if we're running detached - if "(" in branches[0]: - branches.pop(0) - - # Strip off the leading "* " from the list of branches. - branches = [branch[2:] for branch in branches] - if "master" in branches: - branch_name = "master" - elif not branches: - branch_name = None - else: - # Pick the first branch that is returned. Good or bad. - branch_name = branches[0] - - pieces["branch"] = branch_name - - # parse describe_out. It will be like TAG-NUM-gHEX[-dirty] or HEX[-dirty] - # TAG might have hyphens. - git_describe = describe_out - - # look for -dirty suffix - dirty = git_describe.endswith("-dirty") - pieces["dirty"] = dirty - if dirty: - git_describe = git_describe[:git_describe.rindex("-dirty")] - - # now we have TAG-NUM-gHEX or HEX - - if "-" in git_describe: - # TAG-NUM-gHEX - mo = re.search(r'^(.+)-(\d+)-g([0-9a-f]+)$', git_describe) - if not mo: - # unparsable. Maybe git-describe is misbehaving? - pieces["error"] = ("unable to parse git-describe output: '%%s'" - %% describe_out) - return pieces - - # tag - full_tag = mo.group(1) - if not full_tag.startswith(tag_prefix): - if verbose: - fmt = "tag '%%s' doesn't start with prefix '%%s'" - print(fmt %% (full_tag, tag_prefix)) - pieces["error"] = ("tag '%%s' doesn't start with prefix '%%s'" - %% (full_tag, tag_prefix)) - return pieces - pieces["closest-tag"] = full_tag[len(tag_prefix):] - - # distance: number of commits since tag - pieces["distance"] = int(mo.group(2)) - - # commit: short hex revision ID - pieces["short"] = mo.group(3) - - else: - # HEX: no tags - pieces["closest-tag"] = None - out, rc = runner(GITS, ["rev-list", "HEAD", "--left-right"], cwd=root) - pieces["distance"] = len(out.split()) # total number of commits - - # commit date: see ISO-8601 comment in git_versions_from_keywords() - date = runner(GITS, ["show", "-s", "--format=%%ci", "HEAD"], cwd=root)[0].strip() - # Use only the last line. Previous lines may contain GPG signature - # information. - date = date.splitlines()[-1] - pieces["date"] = date.strip().replace(" ", "T", 1).replace(" ", "", 1) - - return pieces - - -def plus_or_dot(pieces): - """Return a + if we don't already have one, else return a .""" - if "+" in pieces.get("closest-tag", ""): - return "." - return "+" - - -def render_pep440(pieces): - """Build up version string, with post-release "local version identifier". - - Our goal: TAG[+DISTANCE.gHEX[.dirty]] . Note that if you - get a tagged build and then dirty it, you'll get TAG+0.gHEX.dirty - - Exceptions: - 1: no tags. git_describe was just HEX. 0+untagged.DISTANCE.gHEX[.dirty] - """ - if pieces["closest-tag"]: - rendered = pieces["closest-tag"] - if pieces["distance"] or pieces["dirty"]: - rendered += plus_or_dot(pieces) - rendered += "%%d.g%%s" %% (pieces["distance"], pieces["short"]) - if pieces["dirty"]: - rendered += ".dirty" - else: - # exception #1 - rendered = "0+untagged.%%d.g%%s" %% (pieces["distance"], - pieces["short"]) - if pieces["dirty"]: - rendered += ".dirty" - return rendered - - -def render_pep440_branch(pieces): - """TAG[[.dev0]+DISTANCE.gHEX[.dirty]] . - - The ".dev0" means not master branch. Note that .dev0 sorts backwards - (a feature branch will appear "older" than the master branch). - - Exceptions: - 1: no tags. 0[.dev0]+untagged.DISTANCE.gHEX[.dirty] - """ - if pieces["closest-tag"]: - rendered = pieces["closest-tag"] - if pieces["distance"] or pieces["dirty"]: - if pieces["branch"] != "master": - rendered += ".dev0" - rendered += plus_or_dot(pieces) - rendered += "%%d.g%%s" %% (pieces["distance"], pieces["short"]) - if pieces["dirty"]: - rendered += ".dirty" - else: - # exception #1 - rendered = "0" - if pieces["branch"] != "master": - rendered += ".dev0" - rendered += "+untagged.%%d.g%%s" %% (pieces["distance"], - pieces["short"]) - if pieces["dirty"]: - rendered += ".dirty" - return rendered - - -def pep440_split_post(ver): - """Split pep440 version string at the post-release segment. - - Returns the release segments before the post-release and the - post-release version number (or -1 if no post-release segment is present). - """ - vc = str.split(ver, ".post") - return vc[0], int(vc[1] or 0) if len(vc) == 2 else None - - -def render_pep440_pre(pieces): - """TAG[.postN.devDISTANCE] -- No -dirty. - - Exceptions: - 1: no tags. 0.post0.devDISTANCE - """ - if pieces["closest-tag"]: - if pieces["distance"]: - # update the post release segment - tag_version, post_version = pep440_split_post(pieces["closest-tag"]) - rendered = tag_version - if post_version is not None: - rendered += ".post%%d.dev%%d" %% (post_version + 1, pieces["distance"]) - else: - rendered += ".post0.dev%%d" %% (pieces["distance"]) - else: - # no commits, use the tag as the version - rendered = pieces["closest-tag"] - else: - # exception #1 - rendered = "0.post0.dev%%d" %% pieces["distance"] - return rendered - - -def render_pep440_post(pieces): - """TAG[.postDISTANCE[.dev0]+gHEX] . - - The ".dev0" means dirty. Note that .dev0 sorts backwards - (a dirty tree will appear "older" than the corresponding clean one), - but you shouldn't be releasing software with -dirty anyways. - - Exceptions: - 1: no tags. 0.postDISTANCE[.dev0] - """ - if pieces["closest-tag"]: - rendered = pieces["closest-tag"] - if pieces["distance"] or pieces["dirty"]: - rendered += ".post%%d" %% pieces["distance"] - if pieces["dirty"]: - rendered += ".dev0" - rendered += plus_or_dot(pieces) - rendered += "g%%s" %% pieces["short"] - else: - # exception #1 - rendered = "0.post%%d" %% pieces["distance"] - if pieces["dirty"]: - rendered += ".dev0" - rendered += "+g%%s" %% pieces["short"] - return rendered - - -def render_pep440_post_branch(pieces): - """TAG[.postDISTANCE[.dev0]+gHEX[.dirty]] . - - The ".dev0" means not master branch. - - Exceptions: - 1: no tags. 0.postDISTANCE[.dev0]+gHEX[.dirty] - """ - if pieces["closest-tag"]: - rendered = pieces["closest-tag"] - if pieces["distance"] or pieces["dirty"]: - rendered += ".post%%d" %% pieces["distance"] - if pieces["branch"] != "master": - rendered += ".dev0" - rendered += plus_or_dot(pieces) - rendered += "g%%s" %% pieces["short"] - if pieces["dirty"]: - rendered += ".dirty" - else: - # exception #1 - rendered = "0.post%%d" %% pieces["distance"] - if pieces["branch"] != "master": - rendered += ".dev0" - rendered += "+g%%s" %% pieces["short"] - if pieces["dirty"]: - rendered += ".dirty" - return rendered - - -def render_pep440_old(pieces): - """TAG[.postDISTANCE[.dev0]] . - - The ".dev0" means dirty. - - Exceptions: - 1: no tags. 0.postDISTANCE[.dev0] - """ - if pieces["closest-tag"]: - rendered = pieces["closest-tag"] - if pieces["distance"] or pieces["dirty"]: - rendered += ".post%%d" %% pieces["distance"] - if pieces["dirty"]: - rendered += ".dev0" - else: - # exception #1 - rendered = "0.post%%d" %% pieces["distance"] - if pieces["dirty"]: - rendered += ".dev0" - return rendered - - -def render_git_describe(pieces): - """TAG[-DISTANCE-gHEX][-dirty]. - - Like 'git describe --tags --dirty --always'. - - Exceptions: - 1: no tags. HEX[-dirty] (note: no 'g' prefix) - """ - if pieces["closest-tag"]: - rendered = pieces["closest-tag"] - if pieces["distance"]: - rendered += "-%%d-g%%s" %% (pieces["distance"], pieces["short"]) - else: - # exception #1 - rendered = pieces["short"] - if pieces["dirty"]: - rendered += "-dirty" - return rendered - - -def render_git_describe_long(pieces): - """TAG-DISTANCE-gHEX[-dirty]. - - Like 'git describe --tags --dirty --always -long'. - The distance/hash is unconditional. - - Exceptions: - 1: no tags. HEX[-dirty] (note: no 'g' prefix) - """ - if pieces["closest-tag"]: - rendered = pieces["closest-tag"] - rendered += "-%%d-g%%s" %% (pieces["distance"], pieces["short"]) - else: - # exception #1 - rendered = pieces["short"] - if pieces["dirty"]: - rendered += "-dirty" - return rendered - - -def render(pieces, style): - """Render the given version pieces into the requested style.""" - if pieces["error"]: - return {"version": "unknown", - "full-revisionid": pieces.get("long"), - "dirty": None, - "error": pieces["error"], - "date": None} - - if not style or style == "default": - style = "pep440" # the default - - if style == "pep440": - rendered = render_pep440(pieces) - elif style == "pep440-branch": - rendered = render_pep440_branch(pieces) - elif style == "pep440-pre": - rendered = render_pep440_pre(pieces) - elif style == "pep440-post": - rendered = render_pep440_post(pieces) - elif style == "pep440-post-branch": - rendered = render_pep440_post_branch(pieces) - elif style == "pep440-old": - rendered = render_pep440_old(pieces) - elif style == "git-describe": - rendered = render_git_describe(pieces) - elif style == "git-describe-long": - rendered = render_git_describe_long(pieces) - else: - raise ValueError("unknown style '%%s'" %% style) - - return {"version": rendered, "full-revisionid": pieces["long"], - "dirty": pieces["dirty"], "error": None, - "date": pieces.get("date")} - - -def get_versions(): - """Get version information or return default if unable to do so.""" - # I am in _version.py, which lives at ROOT/VERSIONFILE_SOURCE. If we have - # __file__, we can work backwards from there to the root. Some - # py2exe/bbfreeze/non-CPython implementations don't do __file__, in which - # case we can only use expanded keywords. - - cfg = get_config() - verbose = cfg.verbose - - try: - return git_versions_from_keywords(get_keywords(), cfg.tag_prefix, - verbose) - except NotThisMethod: - pass - - try: - root = os.path.realpath(__file__) - # versionfile_source is the relative path from the top of the source - # tree (where the .git directory might live) to this file. Invert - # this to find the root from __file__. - for _ in cfg.versionfile_source.split('/'): - root = os.path.dirname(root) - except NameError: - return {"version": "0+unknown", "full-revisionid": None, - "dirty": None, - "error": "unable to find root of source tree", - "date": None} - - try: - pieces = git_pieces_from_vcs(cfg.tag_prefix, root, verbose) - return render(pieces, cfg.style) - except NotThisMethod: - pass - - try: - if cfg.parentdir_prefix: - return versions_from_parentdir(cfg.parentdir_prefix, root, verbose) - except NotThisMethod: - pass - - return {"version": "0+unknown", "full-revisionid": None, - "dirty": None, - "error": "unable to compute version", "date": None} -''' - - -@register_vcs_handler("git", "get_keywords") -def git_get_keywords(versionfile_abs): - """Extract version information from the given file.""" - # the code embedded in _version.py can just fetch the value of these - # keywords. When used from setup.py, we don't want to import _version.py, - # so we do it with a regexp instead. This function is not used from - # _version.py. - keywords = {} - try: - with open(versionfile_abs, "r") as fobj: - for line in fobj: - if line.strip().startswith("git_refnames ="): - mo = re.search(r'=\s*"(.*)"', line) - if mo: - keywords["refnames"] = mo.group(1) - if line.strip().startswith("git_full ="): - mo = re.search(r'=\s*"(.*)"', line) - if mo: - keywords["full"] = mo.group(1) - if line.strip().startswith("git_date ="): - mo = re.search(r'=\s*"(.*)"', line) - if mo: - keywords["date"] = mo.group(1) - except OSError: - pass - return keywords - - -@register_vcs_handler("git", "keywords") -def git_versions_from_keywords(keywords, tag_prefix, verbose): - """Get version information from git keywords.""" - if "refnames" not in keywords: - raise NotThisMethod("Short version file found") - date = keywords.get("date") - if date is not None: - # Use only the last line. Previous lines may contain GPG signature - # information. - date = date.splitlines()[-1] - - # git-2.2.0 added "%cI", which expands to an ISO-8601 -compliant - # datestamp. However we prefer "%ci" (which expands to an "ISO-8601 - # -like" string, which we must then edit to make compliant), because - # it's been around since git-1.5.3, and it's too difficult to - # discover which version we're using, or to work around using an - # older one. - date = date.strip().replace(" ", "T", 1).replace(" ", "", 1) - refnames = keywords["refnames"].strip() - if refnames.startswith("$Format"): - if verbose: - print("keywords are unexpanded, not using") - raise NotThisMethod("unexpanded keywords, not a git-archive tarball") - refs = {r.strip() for r in refnames.strip("()").split(",")} - # starting in git-1.8.3, tags are listed as "tag: foo-1.0" instead of - # just "foo-1.0". If we see a "tag: " prefix, prefer those. - TAG = "tag: " - tags = {r[len(TAG):] for r in refs if r.startswith(TAG)} - if not tags: - # Either we're using git < 1.8.3, or there really are no tags. We use - # a heuristic: assume all version tags have a digit. The old git %d - # expansion behaves like git log --decorate=short and strips out the - # refs/heads/ and refs/tags/ prefixes that would let us distinguish - # between branches and tags. By ignoring refnames without digits, we - # filter out many common branch names like "release" and - # "stabilization", as well as "HEAD" and "master". - tags = {r for r in refs if re.search(r'\d', r)} - if verbose: - print("discarding '%s', no digits" % ",".join(refs - tags)) - if verbose: - print("likely tags: %s" % ",".join(sorted(tags))) - for ref in sorted(tags): - # sorting will prefer e.g. "2.0" over "2.0rc1" - if ref.startswith(tag_prefix): - r = ref[len(tag_prefix):] - # Filter out refs that exactly match prefix or that don't start - # with a number once the prefix is stripped (mostly a concern - # when prefix is '') - if not re.match(r'\d', r): - continue - if verbose: - print("picking %s" % r) - return {"version": r, - "full-revisionid": keywords["full"].strip(), - "dirty": False, "error": None, - "date": date} - # no suitable tags, so version is "0+unknown", but full hex is still there - if verbose: - print("no suitable tags, using unknown + full revision id") - return {"version": "0+unknown", - "full-revisionid": keywords["full"].strip(), - "dirty": False, "error": "no suitable tags", "date": None} - - -@register_vcs_handler("git", "pieces_from_vcs") -def git_pieces_from_vcs(tag_prefix, root, verbose, runner=run_command): - """Get version from 'git describe' in the root of the source tree. - - This only gets called if the git-archive 'subst' keywords were *not* - expanded, and _version.py hasn't already been rewritten with a short - version string, meaning we're inside a checked out source tree. - """ - GITS = ["git"] - if sys.platform == "win32": - GITS = ["git.cmd", "git.exe"] - - # GIT_DIR can interfere with correct operation of Versioneer. - # It may be intended to be passed to the Versioneer-versioned project, - # but that should not change where we get our version from. - env = os.environ.copy() - env.pop("GIT_DIR", None) - runner = functools.partial(runner, env=env) - - _, rc = runner(GITS, ["rev-parse", "--git-dir"], cwd=root, - hide_stderr=not verbose) - if rc != 0: - if verbose: - print("Directory %s not under git control" % root) - raise NotThisMethod("'git rev-parse --git-dir' returned error") - - # if there is a tag matching tag_prefix, this yields TAG-NUM-gHEX[-dirty] - # if there isn't one, this yields HEX[-dirty] (no NUM) - describe_out, rc = runner(GITS, [ - "describe", "--tags", "--dirty", "--always", "--long", - "--match", f"{tag_prefix}[[:digit:]]*" - ], cwd=root) - # --long was added in git-1.5.5 - if describe_out is None: - raise NotThisMethod("'git describe' failed") - describe_out = describe_out.strip() - full_out, rc = runner(GITS, ["rev-parse", "HEAD"], cwd=root) - if full_out is None: - raise NotThisMethod("'git rev-parse' failed") - full_out = full_out.strip() - - pieces = {} - pieces["long"] = full_out - pieces["short"] = full_out[:7] # maybe improved later - pieces["error"] = None - - branch_name, rc = runner(GITS, ["rev-parse", "--abbrev-ref", "HEAD"], - cwd=root) - # --abbrev-ref was added in git-1.6.3 - if rc != 0 or branch_name is None: - raise NotThisMethod("'git rev-parse --abbrev-ref' returned error") - branch_name = branch_name.strip() - - if branch_name == "HEAD": - # If we aren't exactly on a branch, pick a branch which represents - # the current commit. If all else fails, we are on a branchless - # commit. - branches, rc = runner(GITS, ["branch", "--contains"], cwd=root) - # --contains was added in git-1.5.4 - if rc != 0 or branches is None: - raise NotThisMethod("'git branch --contains' returned error") - branches = branches.split("\n") - - # Remove the first line if we're running detached - if "(" in branches[0]: - branches.pop(0) - - # Strip off the leading "* " from the list of branches. - branches = [branch[2:] for branch in branches] - if "master" in branches: - branch_name = "master" - elif not branches: - branch_name = None - else: - # Pick the first branch that is returned. Good or bad. - branch_name = branches[0] - - pieces["branch"] = branch_name - - # parse describe_out. It will be like TAG-NUM-gHEX[-dirty] or HEX[-dirty] - # TAG might have hyphens. - git_describe = describe_out - - # look for -dirty suffix - dirty = git_describe.endswith("-dirty") - pieces["dirty"] = dirty - if dirty: - git_describe = git_describe[:git_describe.rindex("-dirty")] - - # now we have TAG-NUM-gHEX or HEX - - if "-" in git_describe: - # TAG-NUM-gHEX - mo = re.search(r'^(.+)-(\d+)-g([0-9a-f]+)$', git_describe) - if not mo: - # unparsable. Maybe git-describe is misbehaving? - pieces["error"] = ("unable to parse git-describe output: '%s'" - % describe_out) - return pieces - - # tag - full_tag = mo.group(1) - if not full_tag.startswith(tag_prefix): - if verbose: - fmt = "tag '%s' doesn't start with prefix '%s'" - print(fmt % (full_tag, tag_prefix)) - pieces["error"] = ("tag '%s' doesn't start with prefix '%s'" - % (full_tag, tag_prefix)) - return pieces - pieces["closest-tag"] = full_tag[len(tag_prefix):] - - # distance: number of commits since tag - pieces["distance"] = int(mo.group(2)) - - # commit: short hex revision ID - pieces["short"] = mo.group(3) - - else: - # HEX: no tags - pieces["closest-tag"] = None - out, rc = runner(GITS, ["rev-list", "HEAD", "--left-right"], cwd=root) - pieces["distance"] = len(out.split()) # total number of commits - - # commit date: see ISO-8601 comment in git_versions_from_keywords() - date = runner(GITS, ["show", "-s", "--format=%ci", "HEAD"], cwd=root)[0].strip() - # Use only the last line. Previous lines may contain GPG signature - # information. - date = date.splitlines()[-1] - pieces["date"] = date.strip().replace(" ", "T", 1).replace(" ", "", 1) - - return pieces - - -def do_vcs_install(versionfile_source, ipy): - """Git-specific installation logic for Versioneer. - - For Git, this means creating/changing .gitattributes to mark _version.py - for export-subst keyword substitution. - """ - GITS = ["git"] - if sys.platform == "win32": - GITS = ["git.cmd", "git.exe"] - files = [versionfile_source] - if ipy: - files.append(ipy) - if "VERSIONEER_PEP518" not in globals(): - try: - my_path = __file__ - if my_path.endswith((".pyc", ".pyo")): - my_path = os.path.splitext(my_path)[0] + ".py" - versioneer_file = os.path.relpath(my_path) - except NameError: - versioneer_file = "versioneer.py" - files.append(versioneer_file) - present = False - try: - with open(".gitattributes", "r") as fobj: - for line in fobj: - if line.strip().startswith(versionfile_source): - if "export-subst" in line.strip().split()[1:]: - present = True - break - except OSError: - pass - if not present: - with open(".gitattributes", "a+") as fobj: - fobj.write(f"{versionfile_source} export-subst\n") - files.append(".gitattributes") - run_command(GITS, ["add", "--"] + files) - - -def versions_from_parentdir(parentdir_prefix, root, verbose): - """Try to determine the version from the parent directory name. - - Source tarballs conventionally unpack into a directory that includes both - the project name and a version string. We will also support searching up - two directory levels for an appropriately named parent directory - """ - rootdirs = [] - - for _ in range(3): - dirname = os.path.basename(root) - if dirname.startswith(parentdir_prefix): - return {"version": dirname[len(parentdir_prefix):], - "full-revisionid": None, - "dirty": False, "error": None, "date": None} - rootdirs.append(root) - root = os.path.dirname(root) # up a level - - if verbose: - print("Tried directories %s but none started with prefix %s" % - (str(rootdirs), parentdir_prefix)) - raise NotThisMethod("rootdir doesn't start with parentdir_prefix") - - -SHORT_VERSION_PY = """ -# This file was generated by 'versioneer.py' (0.28) from -# revision-control system data, or from the parent directory name of an -# unpacked source archive. Distribution tarballs contain a pre-generated copy -# of this file. - -import json - -version_json = ''' -%s -''' # END VERSION_JSON - - -def get_versions(): - return json.loads(version_json) -""" - - -def versions_from_file(filename): - """Try to determine the version from _version.py if present.""" - try: - with open(filename) as f: - contents = f.read() - except OSError: - raise NotThisMethod("unable to read _version.py") - mo = re.search(r"version_json = '''\n(.*)''' # END VERSION_JSON", - contents, re.M | re.S) - if not mo: - mo = re.search(r"version_json = '''\r\n(.*)''' # END VERSION_JSON", - contents, re.M | re.S) - if not mo: - raise NotThisMethod("no version_json in _version.py") - return json.loads(mo.group(1)) - - -def write_to_version_file(filename, versions): - """Write the given version number to the given _version.py file.""" - os.unlink(filename) - contents = json.dumps(versions, sort_keys=True, - indent=1, separators=(",", ": ")) - with open(filename, "w") as f: - f.write(SHORT_VERSION_PY % contents) - - print("set %s to '%s'" % (filename, versions["version"])) - - -def plus_or_dot(pieces): - """Return a + if we don't already have one, else return a .""" - if "+" in pieces.get("closest-tag", ""): - return "." - return "+" - - -def render_pep440(pieces): - """Build up version string, with post-release "local version identifier". - - Our goal: TAG[+DISTANCE.gHEX[.dirty]] . Note that if you - get a tagged build and then dirty it, you'll get TAG+0.gHEX.dirty - - Exceptions: - 1: no tags. git_describe was just HEX. 0+untagged.DISTANCE.gHEX[.dirty] - """ - if pieces["closest-tag"]: - rendered = pieces["closest-tag"] - if pieces["distance"] or pieces["dirty"]: - rendered += plus_or_dot(pieces) - rendered += "%d.g%s" % (pieces["distance"], pieces["short"]) - if pieces["dirty"]: - rendered += ".dirty" - else: - # exception #1 - rendered = "0+untagged.%d.g%s" % (pieces["distance"], - pieces["short"]) - if pieces["dirty"]: - rendered += ".dirty" - return rendered - - -def render_pep440_branch(pieces): - """TAG[[.dev0]+DISTANCE.gHEX[.dirty]] . - - The ".dev0" means not master branch. Note that .dev0 sorts backwards - (a feature branch will appear "older" than the master branch). - - Exceptions: - 1: no tags. 0[.dev0]+untagged.DISTANCE.gHEX[.dirty] - """ - if pieces["closest-tag"]: - rendered = pieces["closest-tag"] - if pieces["distance"] or pieces["dirty"]: - if pieces["branch"] != "master": - rendered += ".dev0" - rendered += plus_or_dot(pieces) - rendered += "%d.g%s" % (pieces["distance"], pieces["short"]) - if pieces["dirty"]: - rendered += ".dirty" - else: - # exception #1 - rendered = "0" - if pieces["branch"] != "master": - rendered += ".dev0" - rendered += "+untagged.%d.g%s" % (pieces["distance"], - pieces["short"]) - if pieces["dirty"]: - rendered += ".dirty" - return rendered - - -def pep440_split_post(ver): - """Split pep440 version string at the post-release segment. - - Returns the release segments before the post-release and the - post-release version number (or -1 if no post-release segment is present). - """ - vc = str.split(ver, ".post") - return vc[0], int(vc[1] or 0) if len(vc) == 2 else None - - -def render_pep440_pre(pieces): - """TAG[.postN.devDISTANCE] -- No -dirty. - - Exceptions: - 1: no tags. 0.post0.devDISTANCE - """ - if pieces["closest-tag"]: - if pieces["distance"]: - # update the post release segment - tag_version, post_version = pep440_split_post(pieces["closest-tag"]) - rendered = tag_version - if post_version is not None: - rendered += ".post%d.dev%d" % (post_version + 1, pieces["distance"]) - else: - rendered += ".post0.dev%d" % (pieces["distance"]) - else: - # no commits, use the tag as the version - rendered = pieces["closest-tag"] - else: - # exception #1 - rendered = "0.post0.dev%d" % pieces["distance"] - return rendered - - -def render_pep440_post(pieces): - """TAG[.postDISTANCE[.dev0]+gHEX] . - - The ".dev0" means dirty. Note that .dev0 sorts backwards - (a dirty tree will appear "older" than the corresponding clean one), - but you shouldn't be releasing software with -dirty anyways. - - Exceptions: - 1: no tags. 0.postDISTANCE[.dev0] - """ - if pieces["closest-tag"]: - rendered = pieces["closest-tag"] - if pieces["distance"] or pieces["dirty"]: - rendered += ".post%d" % pieces["distance"] - if pieces["dirty"]: - rendered += ".dev0" - rendered += plus_or_dot(pieces) - rendered += "g%s" % pieces["short"] - else: - # exception #1 - rendered = "0.post%d" % pieces["distance"] - if pieces["dirty"]: - rendered += ".dev0" - rendered += "+g%s" % pieces["short"] - return rendered - - -def render_pep440_post_branch(pieces): - """TAG[.postDISTANCE[.dev0]+gHEX[.dirty]] . - - The ".dev0" means not master branch. - - Exceptions: - 1: no tags. 0.postDISTANCE[.dev0]+gHEX[.dirty] - """ - if pieces["closest-tag"]: - rendered = pieces["closest-tag"] - if pieces["distance"] or pieces["dirty"]: - rendered += ".post%d" % pieces["distance"] - if pieces["branch"] != "master": - rendered += ".dev0" - rendered += plus_or_dot(pieces) - rendered += "g%s" % pieces["short"] - if pieces["dirty"]: - rendered += ".dirty" - else: - # exception #1 - rendered = "0.post%d" % pieces["distance"] - if pieces["branch"] != "master": - rendered += ".dev0" - rendered += "+g%s" % pieces["short"] - if pieces["dirty"]: - rendered += ".dirty" - return rendered - - -def render_pep440_old(pieces): - """TAG[.postDISTANCE[.dev0]] . - - The ".dev0" means dirty. - - Exceptions: - 1: no tags. 0.postDISTANCE[.dev0] - """ - if pieces["closest-tag"]: - rendered = pieces["closest-tag"] - if pieces["distance"] or pieces["dirty"]: - rendered += ".post%d" % pieces["distance"] - if pieces["dirty"]: - rendered += ".dev0" - else: - # exception #1 - rendered = "0.post%d" % pieces["distance"] - if pieces["dirty"]: - rendered += ".dev0" - return rendered - - -def render_git_describe(pieces): - """TAG[-DISTANCE-gHEX][-dirty]. - - Like 'git describe --tags --dirty --always'. - - Exceptions: - 1: no tags. HEX[-dirty] (note: no 'g' prefix) - """ - if pieces["closest-tag"]: - rendered = pieces["closest-tag"] - if pieces["distance"]: - rendered += "-%d-g%s" % (pieces["distance"], pieces["short"]) - else: - # exception #1 - rendered = pieces["short"] - if pieces["dirty"]: - rendered += "-dirty" - return rendered - - -def render_git_describe_long(pieces): - """TAG-DISTANCE-gHEX[-dirty]. - - Like 'git describe --tags --dirty --always -long'. - The distance/hash is unconditional. - - Exceptions: - 1: no tags. HEX[-dirty] (note: no 'g' prefix) - """ - if pieces["closest-tag"]: - rendered = pieces["closest-tag"] - rendered += "-%d-g%s" % (pieces["distance"], pieces["short"]) - else: - # exception #1 - rendered = pieces["short"] - if pieces["dirty"]: - rendered += "-dirty" - return rendered - - -def render(pieces, style): - """Render the given version pieces into the requested style.""" - if pieces["error"]: - return {"version": "unknown", - "full-revisionid": pieces.get("long"), - "dirty": None, - "error": pieces["error"], - "date": None} - - if not style or style == "default": - style = "pep440" # the default - - if style == "pep440": - rendered = render_pep440(pieces) - elif style == "pep440-branch": - rendered = render_pep440_branch(pieces) - elif style == "pep440-pre": - rendered = render_pep440_pre(pieces) - elif style == "pep440-post": - rendered = render_pep440_post(pieces) - elif style == "pep440-post-branch": - rendered = render_pep440_post_branch(pieces) - elif style == "pep440-old": - rendered = render_pep440_old(pieces) - elif style == "git-describe": - rendered = render_git_describe(pieces) - elif style == "git-describe-long": - rendered = render_git_describe_long(pieces) - else: - raise ValueError("unknown style '%s'" % style) - - return {"version": rendered, "full-revisionid": pieces["long"], - "dirty": pieces["dirty"], "error": None, - "date": pieces.get("date")} - - -class VersioneerBadRootError(Exception): - """The project root directory is unknown or missing key files.""" - - -def get_versions(verbose=False): - """Get the project version from whatever source is available. - - Returns dict with two keys: 'version' and 'full'. - """ - if "versioneer" in sys.modules: - # see the discussion in cmdclass.py:get_cmdclass() - del sys.modules["versioneer"] - - root = get_root() - cfg = get_config_from_root(root) - - assert cfg.VCS is not None, "please set [versioneer]VCS= in setup.cfg" - handlers = HANDLERS.get(cfg.VCS) - assert handlers, "unrecognized VCS '%s'" % cfg.VCS - verbose = verbose or cfg.verbose - assert cfg.versionfile_source is not None, \ - "please set versioneer.versionfile_source" - assert cfg.tag_prefix is not None, "please set versioneer.tag_prefix" - - versionfile_abs = os.path.join(root, cfg.versionfile_source) - - # extract version from first of: _version.py, VCS command (e.g. 'git - # describe'), parentdir. This is meant to work for developers using a - # source checkout, for users of a tarball created by 'setup.py sdist', - # and for users of a tarball/zipball created by 'git archive' or github's - # download-from-tag feature or the equivalent in other VCSes. - - get_keywords_f = handlers.get("get_keywords") - from_keywords_f = handlers.get("keywords") - if get_keywords_f and from_keywords_f: - try: - keywords = get_keywords_f(versionfile_abs) - ver = from_keywords_f(keywords, cfg.tag_prefix, verbose) - if verbose: - print("got version from expanded keyword %s" % ver) - return ver - except NotThisMethod: - pass - - try: - ver = versions_from_file(versionfile_abs) - if verbose: - print("got version from file %s %s" % (versionfile_abs, ver)) - return ver - except NotThisMethod: - pass - - from_vcs_f = handlers.get("pieces_from_vcs") - if from_vcs_f: - try: - pieces = from_vcs_f(cfg.tag_prefix, root, verbose) - ver = render(pieces, cfg.style) - if verbose: - print("got version from VCS %s" % ver) - return ver - except NotThisMethod: - pass - - try: - if cfg.parentdir_prefix: - ver = versions_from_parentdir(cfg.parentdir_prefix, root, verbose) - if verbose: - print("got version from parentdir %s" % ver) - return ver - except NotThisMethod: - pass - - if verbose: - print("unable to compute version") - - return {"version": "0+unknown", "full-revisionid": None, - "dirty": None, "error": "unable to compute version", - "date": None} - - -def get_version(): - """Get the short version string for this project.""" - return get_versions()["version"] - - -def get_cmdclass(cmdclass=None): - """Get the custom setuptools subclasses used by Versioneer. - - If the package uses a different cmdclass (e.g. one from numpy), it - should be provide as an argument. - """ - if "versioneer" in sys.modules: - del sys.modules["versioneer"] - # this fixes the "python setup.py develop" case (also 'install' and - # 'easy_install .'), in which subdependencies of the main project are - # built (using setup.py bdist_egg) in the same python process. Assume - # a main project A and a dependency B, which use different versions - # of Versioneer. A's setup.py imports A's Versioneer, leaving it in - # sys.modules by the time B's setup.py is executed, causing B to run - # with the wrong versioneer. Setuptools wraps the sub-dep builds in a - # sandbox that restores sys.modules to it's pre-build state, so the - # parent is protected against the child's "import versioneer". By - # removing ourselves from sys.modules here, before the child build - # happens, we protect the child from the parent's versioneer too. - # Also see https://github.com/python-versioneer/python-versioneer/issues/52 - - cmds = {} if cmdclass is None else cmdclass.copy() - - # we add "version" to setuptools - from setuptools import Command - - class cmd_version(Command): - description = "report generated version string" - user_options = [] - boolean_options = [] - - def initialize_options(self): - pass - - def finalize_options(self): - pass - - def run(self): - vers = get_versions(verbose=True) - print("Version: %s" % vers["version"]) - print(" full-revisionid: %s" % vers.get("full-revisionid")) - print(" dirty: %s" % vers.get("dirty")) - print(" date: %s" % vers.get("date")) - if vers["error"]: - print(" error: %s" % vers["error"]) - cmds["version"] = cmd_version - - # we override "build_py" in setuptools - # - # most invocation pathways end up running build_py: - # distutils/build -> build_py - # distutils/install -> distutils/build ->.. - # setuptools/bdist_wheel -> distutils/install ->.. - # setuptools/bdist_egg -> distutils/install_lib -> build_py - # setuptools/install -> bdist_egg ->.. - # setuptools/develop -> ? - # pip install: - # copies source tree to a tempdir before running egg_info/etc - # if .git isn't copied too, 'git describe' will fail - # then does setup.py bdist_wheel, or sometimes setup.py install - # setup.py egg_info -> ? - - # pip install -e . and setuptool/editable_wheel will invoke build_py - # but the build_py command is not expected to copy any files. - - # we override different "build_py" commands for both environments - if 'build_py' in cmds: - _build_py = cmds['build_py'] - else: - from setuptools.command.build_py import build_py as _build_py - - class cmd_build_py(_build_py): - def run(self): - root = get_root() - cfg = get_config_from_root(root) - versions = get_versions() - _build_py.run(self) - if getattr(self, "editable_mode", False): - # During editable installs `.py` and data files are - # not copied to build_lib - return - # now locate _version.py in the new build/ directory and replace - # it with an updated value - if cfg.versionfile_build: - target_versionfile = os.path.join(self.build_lib, - cfg.versionfile_build) - print("UPDATING %s" % target_versionfile) - write_to_version_file(target_versionfile, versions) - cmds["build_py"] = cmd_build_py - - if 'build_ext' in cmds: - _build_ext = cmds['build_ext'] - else: - from setuptools.command.build_ext import build_ext as _build_ext - - class cmd_build_ext(_build_ext): - def run(self): - root = get_root() - cfg = get_config_from_root(root) - versions = get_versions() - _build_ext.run(self) - if self.inplace: - # build_ext --inplace will only build extensions in - # build/lib<..> dir with no _version.py to write to. - # As in place builds will already have a _version.py - # in the module dir, we do not need to write one. - return - # now locate _version.py in the new build/ directory and replace - # it with an updated value - if not cfg.versionfile_build: - return - target_versionfile = os.path.join(self.build_lib, - cfg.versionfile_build) - if not os.path.exists(target_versionfile): - print(f"Warning: {target_versionfile} does not exist, skipping " - "version update. This can happen if you are running build_ext " - "without first running build_py.") - return - print("UPDATING %s" % target_versionfile) - write_to_version_file(target_versionfile, versions) - cmds["build_ext"] = cmd_build_ext - - if "cx_Freeze" in sys.modules: # cx_freeze enabled? - from cx_Freeze.dist import build_exe as _build_exe - # nczeczulin reports that py2exe won't like the pep440-style string - # as FILEVERSION, but it can be used for PRODUCTVERSION, e.g. - # setup(console=[{ - # "version": versioneer.get_version().split("+", 1)[0], # FILEVERSION - # "product_version": versioneer.get_version(), - # ... - - class cmd_build_exe(_build_exe): - def run(self): - root = get_root() - cfg = get_config_from_root(root) - versions = get_versions() - target_versionfile = cfg.versionfile_source - print("UPDATING %s" % target_versionfile) - write_to_version_file(target_versionfile, versions) - - _build_exe.run(self) - os.unlink(target_versionfile) - with open(cfg.versionfile_source, "w") as f: - LONG = LONG_VERSION_PY[cfg.VCS] - f.write(LONG % - {"DOLLAR": "$", - "STYLE": cfg.style, - "TAG_PREFIX": cfg.tag_prefix, - "PARENTDIR_PREFIX": cfg.parentdir_prefix, - "VERSIONFILE_SOURCE": cfg.versionfile_source, - }) - cmds["build_exe"] = cmd_build_exe - del cmds["build_py"] - - if 'py2exe' in sys.modules: # py2exe enabled? - try: - from py2exe.setuptools_buildexe import py2exe as _py2exe - except ImportError: - from py2exe.distutils_buildexe import py2exe as _py2exe - - class cmd_py2exe(_py2exe): - def run(self): - root = get_root() - cfg = get_config_from_root(root) - versions = get_versions() - target_versionfile = cfg.versionfile_source - print("UPDATING %s" % target_versionfile) - write_to_version_file(target_versionfile, versions) - - _py2exe.run(self) - os.unlink(target_versionfile) - with open(cfg.versionfile_source, "w") as f: - LONG = LONG_VERSION_PY[cfg.VCS] - f.write(LONG % - {"DOLLAR": "$", - "STYLE": cfg.style, - "TAG_PREFIX": cfg.tag_prefix, - "PARENTDIR_PREFIX": cfg.parentdir_prefix, - "VERSIONFILE_SOURCE": cfg.versionfile_source, - }) - cmds["py2exe"] = cmd_py2exe - - # sdist farms its file list building out to egg_info - if 'egg_info' in cmds: - _egg_info = cmds['egg_info'] - else: - from setuptools.command.egg_info import egg_info as _egg_info - - class cmd_egg_info(_egg_info): - def find_sources(self): - # egg_info.find_sources builds the manifest list and writes it - # in one shot - super().find_sources() - - # Modify the filelist and normalize it - root = get_root() - cfg = get_config_from_root(root) - self.filelist.append('versioneer.py') - if cfg.versionfile_source: - # There are rare cases where versionfile_source might not be - # included by default, so we must be explicit - self.filelist.append(cfg.versionfile_source) - self.filelist.sort() - self.filelist.remove_duplicates() - - # The write method is hidden in the manifest_maker instance that - # generated the filelist and was thrown away - # We will instead replicate their final normalization (to unicode, - # and POSIX-style paths) - from setuptools import unicode_utils - normalized = [unicode_utils.filesys_decode(f).replace(os.sep, '/') - for f in self.filelist.files] - - manifest_filename = os.path.join(self.egg_info, 'SOURCES.txt') - with open(manifest_filename, 'w') as fobj: - fobj.write('\n'.join(normalized)) - - cmds['egg_info'] = cmd_egg_info - - # we override different "sdist" commands for both environments - if 'sdist' in cmds: - _sdist = cmds['sdist'] - else: - from setuptools.command.sdist import sdist as _sdist - - class cmd_sdist(_sdist): - def run(self): - versions = get_versions() - self._versioneer_generated_versions = versions - # unless we update this, the command will keep using the old - # version - self.distribution.metadata.version = versions["version"] - return _sdist.run(self) - - def make_release_tree(self, base_dir, files): - root = get_root() - cfg = get_config_from_root(root) - _sdist.make_release_tree(self, base_dir, files) - # now locate _version.py in the new base_dir directory - # (remembering that it may be a hardlink) and replace it with an - # updated value - target_versionfile = os.path.join(base_dir, cfg.versionfile_source) - print("UPDATING %s" % target_versionfile) - write_to_version_file(target_versionfile, - self._versioneer_generated_versions) - cmds["sdist"] = cmd_sdist - - return cmds - - -CONFIG_ERROR = """ -setup.cfg is missing the necessary Versioneer configuration. You need -a section like: - - [versioneer] - VCS = git - style = pep440 - versionfile_source = src/myproject/_version.py - versionfile_build = myproject/_version.py - tag_prefix = - parentdir_prefix = myproject- - -You will also need to edit your setup.py to use the results: - - import versioneer - setup(version=versioneer.get_version(), - cmdclass=versioneer.get_cmdclass(), ...) - -Please read the docstring in ./versioneer.py for configuration instructions, -edit setup.cfg, and re-run the installer or 'python versioneer.py setup'. -""" - -SAMPLE_CONFIG = """ -# See the docstring in versioneer.py for instructions. Note that you must -# re-run 'versioneer.py setup' after changing this section, and commit the -# resulting files. - -[versioneer] -#VCS = git -#style = pep440 -#versionfile_source = -#versionfile_build = -#tag_prefix = -#parentdir_prefix = - -""" - -OLD_SNIPPET = """ -from ._version import get_versions -__version__ = get_versions()['version'] -del get_versions -""" - -INIT_PY_SNIPPET = """ -from . import {0} -__version__ = {0}.get_versions()['version'] -""" - - -def do_setup(): - """Do main VCS-independent setup function for installing Versioneer.""" - root = get_root() - try: - cfg = get_config_from_root(root) - except (OSError, configparser.NoSectionError, - configparser.NoOptionError) as e: - if isinstance(e, (OSError, configparser.NoSectionError)): - print("Adding sample versioneer config to setup.cfg", - file=sys.stderr) - with open(os.path.join(root, "setup.cfg"), "a") as f: - f.write(SAMPLE_CONFIG) - print(CONFIG_ERROR, file=sys.stderr) - return 1 - - print(" creating %s" % cfg.versionfile_source) - with open(cfg.versionfile_source, "w") as f: - LONG = LONG_VERSION_PY[cfg.VCS] - f.write(LONG % {"DOLLAR": "$", - "STYLE": cfg.style, - "TAG_PREFIX": cfg.tag_prefix, - "PARENTDIR_PREFIX": cfg.parentdir_prefix, - "VERSIONFILE_SOURCE": cfg.versionfile_source, - }) - - ipy = os.path.join(os.path.dirname(cfg.versionfile_source), - "__init__.py") - if os.path.exists(ipy): - try: - with open(ipy, "r") as f: - old = f.read() - except OSError: - old = "" - module = os.path.splitext(os.path.basename(cfg.versionfile_source))[0] - snippet = INIT_PY_SNIPPET.format(module) - if OLD_SNIPPET in old: - print(" replacing boilerplate in %s" % ipy) - with open(ipy, "w") as f: - f.write(old.replace(OLD_SNIPPET, snippet)) - elif snippet not in old: - print(" appending to %s" % ipy) - with open(ipy, "a") as f: - f.write(snippet) - else: - print(" %s unmodified" % ipy) - else: - print(" %s doesn't exist, ok" % ipy) - ipy = None - - # Make VCS-specific changes. For git, this means creating/changing - # .gitattributes to mark _version.py for export-subst keyword - # substitution. - do_vcs_install(cfg.versionfile_source, ipy) - return 0 - - -def scan_setup_py(): - """Validate the contents of setup.py against Versioneer's expectations.""" - found = set() - setters = False - errors = 0 - with open("setup.py", "r") as f: - for line in f.readlines(): - if "import versioneer" in line: - found.add("import") - if "versioneer.get_cmdclass()" in line: - found.add("cmdclass") - if "versioneer.get_version()" in line: - found.add("get_version") - if "versioneer.VCS" in line: - setters = True - if "versioneer.versionfile_source" in line: - setters = True - if len(found) != 3: - print("") - print("Your setup.py appears to be missing some important items") - print("(but I might be wrong). Please make sure it has something") - print("roughly like the following:") - print("") - print(" import versioneer") - print(" setup( version=versioneer.get_version(),") - print(" cmdclass=versioneer.get_cmdclass(), ...)") - print("") - errors += 1 - if setters: - print("You should remove lines like 'versioneer.VCS = ' and") - print("'versioneer.versionfile_source = ' . This configuration") - print("now lives in setup.cfg, and should be removed from setup.py") - print("") - errors += 1 - return errors - - -def setup_command(): - """Set up Versioneer and exit with appropriate error code.""" - errors = do_setup() - errors += scan_setup_py() - sys.exit(1 if errors else 0) - - -if __name__ == "__main__": - cmd = sys.argv[1] - if cmd == "setup": - setup_command()