"Fossies" - the Fresh Open Source Software Archive

Member "btrfs-progs-v5.4/libbtrfsutil/python/setup.py" (3 Dec 2019, 3276 Bytes) of package /linux/misc/btrfs-progs-v5.4.tar.xz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) Python source code syntax highlighting (style: standard) with prefixed line numbers. Alternatively you can here view or download the uninterpreted source code file. For more information about "setup.py" see the Fossies "Dox" file reference documentation.

    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         'subvolume.c',
   97     ],
   98     include_dirs=['..'],
   99     library_dirs=['../..'],
  100     libraries=['btrfsutil'],
  101 )
  102 
  103 setup(
  104     name='btrfsutil',
  105     version=get_version(),
  106     description='Library for managing Btrfs filesystems',
  107     url='https://github.com/kdave/btrfs-progs',
  108     license='LGPLv3',
  109     cmdclass={'build_ext': my_build_ext},
  110     ext_modules=[module],
  111 )