forked from autotest/virt-test
-
Notifications
You must be signed in to change notification settings - Fork 0
/
passfd_setup.py
55 lines (41 loc) · 1.3 KB
/
passfd_setup.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
import os
import distutils.ccompiler
import distutils.sysconfig
import data_dir
PYTHON_HEADERS = distutils.sysconfig.get_python_inc()
PYTHON_VERSION = distutils.sysconfig.get_python_version()
PYTHON_LIB = "python%s" % PYTHON_VERSION
OUTPUT_DIR = os.path.dirname(__file__)
SOURCES = [os.path.join(OUTPUT_DIR, f) for f in ['passfd.c']]
SHARED_OBJECT = '_passfd.so'
def passfd_setup(output_dir=OUTPUT_DIR):
'''
Compiles the passfd python extension.
@param output_dir: where the _passfd.so module will be saved
@return: None
'''
if output_dir is None:
output_dir = OUTPUT_DIR
output_file = os.path.join(output_dir, SHARED_OBJECT)
c = distutils.ccompiler.new_compiler()
objects = c.compile(SOURCES, include_dirs=[PYTHON_HEADERS],
output_dir=data_dir.get_tmp_dir(),
extra_postargs=['-fPIC'])
c.link_shared_object(objects, output_file, libraries=[PYTHON_LIB])
def import_passfd():
'''
Imports and lazily sets up the passfd module
@return: passfd module
'''
try:
import passfd
except ImportError:
passfd_setup()
import passfd
return passfd
if __name__ == '__main__':
import sys
if len(sys.argv) > 1:
passfd_setup(sys.argv[1])
else:
passfd_setup()