btrfs-progs/libbtrfsutil/python/setup.py
David Sterba 83bd999c3e libbtrfsutil: python: use version from primary VERSION file
The version file of the python subpackage had to have the version set
manually in setup.py due to the out-of-tree build where it was not
possible to access the file VERSION. Manual update was error prone.

Improve that by adding a separate file template that is finalized with
the version during the configure phase. Then it's inclded in setup.py as
it's in the same directory.

There are two exceptions when the file is not required to run setup.py:

- clean - allow running 'make clean' in partially configured directory
- (no arguments) - show the help and commands

In all other cases the file version.py must exist.

Signed-off-by: David Sterba <dsterba@suse.com>
2025-02-15 00:08:32 +01:00

124 lines
3.5 KiB
Python
Executable File

#!/usr/bin/env python3
# Copyright (C) 2018 Facebook
#
# This file is part of libbtrfsutil.
#
# libbtrfsutil is free software: you can redistribute it and/or modify
# it under the terms of the GNU Lesser General Public License as published by
# the Free Software Foundation, either version 2.1 of the License, or
# (at your option) any later version.
#
# libbtrfsutil 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 Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public License
# along with libbtrfsutil. If not, see <http://www.gnu.org/licenses/>.
import re
import os
import os.path
import sys
from setuptools import setup, Extension
from setuptools.command.build_ext import build_ext
import subprocess
def get_version():
version = 0
try:
import version
version = version.btrfs_util_py_version
except:
# Don't fail if this is only the 'clean' target or no command
if 'clean' in sys.argv or len(sys.argv) == 1:
version = '0.0'
else:
raise Exception("No generated version.py found, please configure")
return version
def out_of_date(dependencies, target):
dependency_mtimes = [os.path.getmtime(dependency) for dependency in dependencies]
try:
target_mtime = os.path.getmtime(target)
except OSError:
return True
return any(dependency_mtime >= target_mtime for dependency_mtime in dependency_mtimes)
def gen_constants():
with open('../btrfsutil.h', 'r') as f:
btrfsutil_h = f.read()
constants = re.findall(
r'^\s*(BTRFS_UTIL_ERROR_[a-zA-Z0-9_]+)',
btrfsutil_h, flags=re.MULTILINE)
with open('constants.c', 'w') as f:
f.write("""\
/* WARNING: this file is auto-generated by setup.py and any change would be lost */
#include <btrfsutil.h>
#include "btrfsutilpy.h"
void add_module_constants(PyObject *m)
{
""")
for constant in constants:
assert constant.startswith('BTRFS_UTIL_')
name = constant[len('BTRFS_UTIL_'):]
f.write('\tPyModule_AddIntConstant(m, "{}", {});\n'.format(name, constant))
f.write("""\
}
""")
class my_build_ext(build_ext):
def run(self):
# Running dist outside of git
if not os.path.exists('../btrfsutil.h'):
# But no generated constants.c found
if not os.path.exists('constants.c'):
raise Exception("No generated constants.c found, please fix")
elif out_of_date(['../btrfsutil.h'], 'constants.c'):
try:
gen_constants()
except Exception as e:
try:
os.remove('constants.c')
except OSError:
pass
raise e
super().run()
module = Extension(
name='btrfsutil',
sources=[
'constants.c',
'error.c',
'filesystem.c',
'module.c',
'qgroup.c',
'subvolume.c',
],
include_dirs=['..'],
library_dirs=['../..'],
libraries=['btrfsutil'],
)
setup(
name='btrfsutil',
version=get_version(),
description='Library for managing Btrfs filesystems',
long_description=open('README.md').read(),
long_description_content_type='text/markdown',
url='https://github.com/kdave/btrfs-progs',
license='LGPLv2+',
cmdclass={'build_ext': my_build_ext},
ext_modules=[module],
)