df44809f2cc3d8082de3a5fbc0ccbdf1dff6d5ba
[platform/upstream/btrfs-progs.git] / libbtrfsutil / python / setup.py
1 #!/usr/bin/env python3
2
3 # Copyright (C) 2018 Facebook
4 #
5 # This file is part of libbtrfsutil.
6 #
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.
11 #
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.
16 #
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/>.
19
20 import re
21 import os
22 import os.path
23 from setuptools import setup, Extension
24 from setuptools.command.build_ext import build_ext
25 import subprocess
26
27
28 def get_version():
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
38
39
40 def out_of_date(dependencies, target):
41     dependency_mtimes = [os.path.getmtime(dependency) for dependency in dependencies]
42     try:
43         target_mtime = os.path.getmtime(target)
44     except OSError:
45         return True
46     return any(dependency_mtime >= target_mtime for dependency_mtime in dependency_mtimes)
47
48
49 def gen_constants():
50     with open('../btrfsutil.h', 'r') as f:
51         btrfsutil_h = f.read()
52
53     constants = re.findall(
54         r'^\s*(BTRFS_UTIL_ERROR_[a-zA-Z0-9_]+)',
55         btrfsutil_h, flags=re.MULTILINE)
56
57     with open('constants.c', 'w') as f:
58         f.write("""\
59 #include <btrfsutil.h>
60 #include "btrfsutilpy.h"
61
62 void add_module_constants(PyObject *m)
63 {
64 """)
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))
69         f.write("""\
70 }
71 """)
72
73
74 class my_build_ext(build_ext):
75     def run(self):
76         if out_of_date(['../btrfsutil.h'], 'constants.c'):
77             try:
78                 gen_constants()
79             except Exception as e:
80                 try:
81                     os.remove('constants.c')
82                 except OSError:
83                     pass
84                 raise e
85         super().run()
86
87
88 module = Extension(
89     name='btrfsutil',
90     sources=[
91         'constants.c',
92         'error.c',
93         'filesystem.c',
94         'module.c',
95         'qgroup.c',
96     ],
97     include_dirs=['..'],
98     library_dirs=['../..'],
99     libraries=['btrfsutil'],
100 )
101
102 setup(
103     name='btrfsutil',
104     version=get_version(),
105     description='Library for managing Btrfs filesystems',
106     url='https://github.com/kdave/btrfs-progs',
107     license='LGPLv3',
108     cmdclass={'build_ext': my_build_ext},
109     ext_modules=[module],
110 )