Fix some bugs when testing opensds ansible
[stor4nfv.git] / src / ceph / src / pybind / cephfs / setup.py
1 from __future__ import print_function
2
3 import os
4 import pkgutil
5 import shutil
6 import subprocess
7 import sys
8 import tempfile
9 import textwrap
10 from distutils.ccompiler import new_compiler
11 from distutils.errors import CompileError, LinkError
12 from distutils.sysconfig import customize_compiler
13
14 if not pkgutil.find_loader('setuptools'):
15     from distutils.core import setup
16     from distutils.extension import Extension
17 else:
18     from setuptools import setup
19     from setuptools.extension import Extension
20
21 # PEP 440 versioning of the Ceph FS package on PyPI
22 # Bump this version, after every changeset
23
24 __version__ = '2.0.0'
25
26
27 def get_python_flags():
28     cflags = {'I': [], 'extras': []}
29     ldflags = {'l': [], 'L': [], 'extras': []}
30
31     if os.environ.get('VIRTUAL_ENV', None):
32         python = "python"
33     else:
34         python = 'python' + str(sys.version_info.major) + '.' + str(sys.version_info.minor)
35
36     python_config = python + '-config'
37
38     for cflag in subprocess.check_output(
39             [python_config, "--cflags"]
40     ).strip().decode('utf-8').split():
41         if cflag.startswith('-I'):
42             cflags['I'].append(cflag.replace('-I', ''))
43         else:
44             cflags['extras'].append(cflag)
45
46     for ldflag in subprocess.check_output(
47             [python_config, "--ldflags"]
48     ).strip().decode('utf-8').split():
49         if ldflag.startswith('-l'):
50             ldflags['l'].append(ldflag.replace('-l', ''))
51         if ldflag.startswith('-L'):
52             ldflags['L'].append(ldflag.replace('-L', ''))
53         else:
54             ldflags['extras'].append(ldflag)
55
56     return {
57         'cflags': cflags,
58         'ldflags': ldflags
59     }
60
61
62 def check_sanity():
63     """
64     Test if development headers and library for cephfs is available by compiling a dummy C program.
65     """
66     CEPH_SRC_DIR = os.path.join(
67         os.path.dirname(os.path.abspath(__file__)),
68         '..',
69         '..'
70     )
71
72     tmp_dir = tempfile.mkdtemp(dir=os.environ.get('TMPDIR', os.path.dirname(__file__)))
73     tmp_file = os.path.join(tmp_dir, 'cephfs_dummy.c')
74
75     with open(tmp_file, 'w') as fp:
76         dummy_prog = textwrap.dedent("""
77         #include <stddef.h>
78         #include "cephfs/libcephfs.h"
79
80         int main(void) {
81             struct ceph_mount_info *cmount = NULL;
82             ceph_init(cmount);
83             return 0;
84         }
85         """)
86         fp.write(dummy_prog)
87
88     compiler = new_compiler()
89     customize_compiler(compiler)
90
91     if {'MAKEFLAGS', 'MFLAGS', 'MAKELEVEL'}.issubset(set(os.environ.keys())):
92         # The setup.py has been invoked by a top-level Ceph make.
93         # Set the appropriate CFLAGS and LDFLAGS
94
95         compiler.set_library_dirs([os.environ.get('CEPH_LIBDIR')])
96
97     try:
98         compiler.define_macro('_FILE_OFFSET_BITS', '64')
99
100         link_objects = compiler.compile(
101             sources=[tmp_file],
102             output_dir=tmp_dir,
103             extra_preargs=['-iquote{path}'.format(path=os.path.join(CEPH_SRC_DIR, 'include'))]
104         )
105
106         compiler.link_executable(
107             objects=link_objects,
108             output_progname=os.path.join(tmp_dir, 'cephfs_dummy'),
109             libraries=['cephfs'],
110             output_dir=tmp_dir,
111         )
112
113     except CompileError:
114         print('\nCompile Error: Ceph FS development headers not found', file=sys.stderr)
115         return False
116     except LinkError:
117         print('\nLink Error: Ceph FS library not found', file=sys.stderr)
118         return False
119     else:
120         return True
121     finally:
122         shutil.rmtree(tmp_dir)
123
124
125 if 'BUILD_DOC' in os.environ.keys():
126     pass
127 elif check_sanity():
128     pass
129 else:
130     sys.exit(1)
131
132 cmdclass = {}
133 try:
134     from Cython.Build import cythonize
135     from Cython.Distutils import build_ext
136
137     cmdclass = {'build_ext': build_ext}
138 except ImportError:
139     print("WARNING: Cython is not installed.")
140
141     if not os.path.isfile('cephfs.c'):
142         print('ERROR: Cannot find Cythonized file cephfs.c', file=sys.stderr)
143         sys.exit(1)
144     else:
145         def cythonize(x, **kwargs):
146             return x
147
148         source = "cephfs.c"
149 else:
150     source = "cephfs.pyx"
151
152 # Disable cythonification if we're not really building anything
153 if (len(sys.argv) >= 2 and
154         any(i in sys.argv[1:] for i in ('--help', 'clean', 'egg_info', '--version')
155             )):
156     def cythonize(x, **kwargs):
157         return x
158
159 flags = get_python_flags()
160
161 setup(
162     name='cephfs',
163     version=__version__,
164     description="Python bindings for the Ceph FS library",
165     long_description=(
166         "This package contains Python bindings for interacting with the "
167         "Ceph Filesystem (Ceph FS) library. Ceph FS is a POSIX-compliant "
168         "filesystem that uses a Ceph Storage Cluster to store its data. The "
169         "Ceph filesystem uses the same Ceph Storage Cluster system as "
170         "Ceph Block Devices, Ceph Object Storage with its S3 and Swift APIs, "
171         "or native bindings (librados)."
172     ),
173     url='https://github.com/ceph/ceph/tree/master/src/pybind/cephfs',
174     license='LGPLv2+',
175     platforms='Linux',
176     ext_modules=cythonize(
177         [
178             Extension(
179                 "cephfs",
180                 [source],
181                 include_dirs=flags['cflags']['I'],
182                 library_dirs=flags['ldflags']['L'],
183                 libraries=['cephfs'] + flags['ldflags']['l'],
184                 extra_compile_args=flags['cflags']['extras'] + flags['ldflags']['extras'],
185             )
186         ],
187         build_dir=os.environ.get("CYTHON_BUILD_DIR", None),
188         include_path=[
189             os.path.join(os.path.dirname(__file__), "..", "rados")
190         ]
191     ),
192     classifiers=[
193         'Intended Audience :: Developers',
194         'Intended Audience :: System Administrators',
195         'License :: OSI Approved :: GNU Lesser General Public License v2 or later (LGPLv2+)',
196         'Operating System :: POSIX :: Linux',
197         'Programming Language :: Cython',
198         'Programming Language :: Python :: 2.7',
199         'Programming Language :: Python :: 3.4',
200         'Programming Language :: Python :: 3.5'
201     ],
202     cmdclass=cmdclass,
203 )