Skip to content

Commit

Permalink
Use NumPy methodology for generating version
Browse files Browse the repository at this point in the history
  • Loading branch information
WillAyd committed Mar 20, 2024
1 parent 710720e commit 24ea030
Show file tree
Hide file tree
Showing 7 changed files with 122 additions and 801 deletions.
69 changes: 0 additions & 69 deletions generate_version.py

This file was deleted.

23 changes: 3 additions & 20 deletions meson.build
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@
project(
'pandas',
'c', 'cpp', 'cython',
version: run_command(['generate_version.py', '--print'], check: true).stdout().strip(),
version: run_command(
['python', 'pandas/_build_utils/gitversion.py'],
check: true).stdout().strip(),
license: 'BSD-3',
meson_version: '>=1.2.1',
default_options: [
Expand All @@ -15,8 +17,6 @@ project(
fs = import('fs')
py = import('python').find_installation(pure: false)
tempita = files('generate_pxi.py')
versioneer = files('generate_version.py')


add_project_arguments('-DNPY_NO_DEPRECATED_API=0', language : 'c')
add_project_arguments('-DNPY_NO_DEPRECATED_API=0', language : 'cpp')
Expand All @@ -27,23 +27,6 @@ add_project_arguments('-DNPY_NO_DEPRECATED_API=0', language : 'cpp')
add_project_arguments('-DNPY_TARGET_VERSION=NPY_1_21_API_VERSION', language : 'c')
add_project_arguments('-DNPY_TARGET_VERSION=NPY_1_21_API_VERSION', language : 'cpp')


if fs.exists('_version_meson.py')
py.install_sources('_version_meson.py', subdir: 'pandas')
else
custom_target('write_version_file',
output: '_version_meson.py',
command: [
py, versioneer, '-o', '@OUTPUT@'
],
build_by_default: true,
build_always_stale: true,
install: true,
install_dir: py.get_install_dir() / 'pandas'
)
meson.add_dist_script(py, versioneer, '-o', '_version_meson.py')
endif

# Needed by pandas.test() when it looks for the pytest ini options
py.install_sources(
'pyproject.toml',
Expand Down
17 changes: 1 addition & 16 deletions pandas/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -171,22 +171,7 @@

from pandas.util._tester import test

# use the closest tagged version if possible
_built_with_meson = False
try:
from pandas._version_meson import ( # pyright: ignore [reportMissingImports]
__version__,
__git_version__,
)

_built_with_meson = True
except ImportError:
from pandas._version import get_versions

v = get_versions()
__version__ = v.get("closest-tag", v["version"])
__git_version__ = v.get("full-revisionid")
del get_versions, v
# If a version with git hash was stored, use that instead


# module level doc-string
Expand Down
90 changes: 90 additions & 0 deletions pandas/_build_utils/gitversion.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
# This file is vendored from NumPy. See NUMPY_LICENSE.txt

#!/usr/bin/env python3
import os
import textwrap


def init_version():
init = os.path.join(os.path.dirname(__file__), "../../pyproject.toml")
with open(init) as fid:
data = fid.readlines()

version_line = next(line for line in data if line.startswith("version ="))

version = version_line.strip().split(" = ")[1]
version = version.replace('"', "").replace("'", "")

return version


def git_version(version):
# Append last commit date and hash to dev version information,
# if available

import os.path
import subprocess

git_hash = ""
try:
p = subprocess.Popen(
["git", "log", "-1", '--format="%H %aI"'],
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
cwd=os.path.dirname(__file__),
)
except FileNotFoundError:
pass
else:
out, err = p.communicate()
if p.returncode == 0:
git_hash, git_date = (
out.decode("utf-8")
.strip()
.replace('"', "")
.split("T")[0]
.replace("-", "")
.split()
)

# Only attach git tag to development versions
if "dev" in version:
version += f"+git{git_date}.{git_hash[:7]}"

return version, git_hash


if __name__ == "__main__":
import argparse

parser = argparse.ArgumentParser()
parser.add_argument("--write", help="Save version to this file")
parser.add_argument(
"--meson-dist",
help="Output path is relative to MESON_DIST_ROOT",
action="store_true",
)
args = parser.parse_args()

version, git_hash = git_version(init_version())

template = textwrap.dedent(f"""
version = "{version}"
__version__ = version
""")

if args.write:
outfile = args.write
if args.meson_dist:
outfile = os.path.join(os.environ.get("MESON_DIST_ROOT", ""), outfile)

# Print human readable output path
relpath = os.path.relpath(outfile)
if relpath.startswith("."):
relpath = outfile

with open(outfile, "w") as f:
print(f"Saving version to {relpath}")
f.write(template)
else:
print(version)
Loading

0 comments on commit 24ea030

Please sign in to comment.