3 # Copyright (C) 2018 Facebook
5 # This file is part of libbtrfsutil.
7 # libbtrfsutil is free software: you can redistribute it and/or modify
8 # it under the terms of the GNU Lesser General Public License as published by
9 # the Free Software Foundation, either version 3 of the License, or
10 # (at your option) any later version.
12 # libbtrfsutil is distributed in the hope that it will be useful,
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 # GNU Lesser General Public License for more details.
17 # You should have received a copy of the GNU Lesser General Public License
18 # along with libbtrfsutil. If not, see <http://www.gnu.org/licenses/>.
23 from setuptools import setup, Extension
24 from setuptools.command.build_ext import build_ext
29 with open('../btrfsutil.h', 'r') as f:
30 btrfsutil_h = f.read()
31 major = re.search(r'^#define BTRFS_UTIL_VERSION_MAJOR ([0-9]+)$',
32 btrfsutil_h, flags=re.MULTILINE).group(1)
33 minor = re.search(r'^#define BTRFS_UTIL_VERSION_MINOR ([0-9]+)$',
34 btrfsutil_h, flags=re.MULTILINE).group(1)
35 patch = re.search(r'^#define BTRFS_UTIL_VERSION_PATCH ([0-9]+)$',
36 btrfsutil_h, flags=re.MULTILINE).group(1)
37 return major + '.' + minor + '.' + patch
40 def out_of_date(dependencies, target):
41 dependency_mtimes = [os.path.getmtime(dependency) for dependency in dependencies]
43 target_mtime = os.path.getmtime(target)
46 return any(dependency_mtime >= target_mtime for dependency_mtime in dependency_mtimes)
50 with open('../btrfsutil.h', 'r') as f:
51 btrfsutil_h = f.read()
53 constants = re.findall(
54 r'^\s*(BTRFS_UTIL_ERROR_[a-zA-Z0-9_]+)',
55 btrfsutil_h, flags=re.MULTILINE)
57 with open('constants.c', 'w') as f:
59 #include <btrfsutil.h>
60 #include "btrfsutilpy.h"
62 void add_module_constants(PyObject *m)
65 for constant in constants:
66 assert constant.startswith('BTRFS_UTIL_')
67 name = constant[len('BTRFS_UTIL_'):]
68 f.write('\tPyModule_AddIntConstant(m, "{}", {});\n'.format(name, constant))
74 class my_build_ext(build_ext):
76 if out_of_date(['../btrfsutil.h'], 'constants.c'):
79 except Exception as e:
81 os.remove('constants.c')
99 library_dirs=['../..'],
100 libraries=['btrfsutil'],
105 version=get_version(),
106 description='Library for managing Btrfs filesystems',
107 url='https://github.com/kdave/btrfs-progs',
109 cmdclass={'build_ext': my_build_ext},
110 ext_modules=[module],