1From 9de73fefbe83c74840a93c039258845c49271b9b Mon Sep 17 00:00:00 2001
2From: Jeffery To <jeffery.to@gmail.com>
3Date: Sun, 8 Nov 2020 21:51:09 +0800
4Subject: [PATCH] Use CFFI in out-of-line API mode (#49)
5
6Currently, ffi.py is called during setup to generate augeas.py; this
7file would normally be used for out-of-line ABI mode. ffi.py is also
8imported at run-time, instead of the generated augeas.py, and used in
9in-line ABI mode.
10
11This changes usage of CFFI to out-of-line API mode (CFFI's "main mode of
12usage"): ffi.py is called during setup to generate _augeas.abi3.so (a C
13extension module); this generated module is imported at run-time.
14
15With this change, the headers/development files for augeas (i.e.
16libaugeas-dev on Debian, augeas-devel on Fedora, etc.) and the C
17compiler are required for build/setup. (These were not necessary
18previously.)
19
20Closes https://github.com/hercules-team/python-augeas/issues/48.
21
22Upstream: commit 712c2028568df7760bc98d95577e35709078bfea
23Signed-off-by: Thomas Petazzoni <thomas.petazzoni@bootlin.com>
24---
25 augeas/__init__.py |  2 +-
26 augeas/ffi.py      | 27 ++++++++++++++++++++++-----
27 setup.py           |  1 +
28 3 files changed, 24 insertions(+), 6 deletions(-)
29
30diff --git a/augeas/__init__.py b/augeas/__init__.py
31index 9bd97bf..1c0f580 100644
32--- a/augeas/__init__.py
33+++ b/augeas/__init__.py
34@@ -32,7 +32,7 @@ format and the transformation into a tree.
35
36 from sys import version_info as _pyver
37
38-from augeas.ffi import ffi, lib
39+from _augeas import ffi, lib
40
41 __author__ = "Nathaniel McCallum <nathaniel@natemccallum.com>"
42 __credits__ = """Jeff Schroeder <jeffschroeder@computer.org>
43diff --git a/augeas/ffi.py b/augeas/ffi.py
44index a24daf5..1931764 100644
45--- a/augeas/ffi.py
46+++ b/augeas/ffi.py
47@@ -1,9 +1,28 @@
48+import os
49+import subprocess
50+
51 from cffi import FFI
52
53+def get_include_dirs():
54+    XML2_CONFIG = os.environ.get('XML2_CONFIG', 'xml2-config')
55+    PKG_CONFIG = os.environ.get('PKG_CONFIG', 'pkg-config')
56+    try:
57+        stdout = subprocess.check_output([XML2_CONFIG, '--cflags'])
58+    except (OSError, subprocess.CalledProcessError):
59+        try:
60+            stdout = subprocess.check_output([PKG_CONFIG, '--cflags', 'libxml-2.0'])
61+        except (OSError, subprocess.CalledProcessError):
62+            stdout = b''
63+    cflags = stdout.decode('utf-8').split()
64+    return [cflag[2:] for cflag in cflags if cflag.startswith('-I')]
65+
66 ffi = FFI()
67-ffi.set_source("augeas",
68-               None,
69-               libraries=['augeas'])
70+ffi.set_source("_augeas",
71+               """
72+               #include <augeas.h>
73+               """,
74+               libraries=['augeas'],
75+               include_dirs=get_include_dirs())
76
77 ffi.cdef("""
78 typedef struct augeas augeas;
79@@ -44,7 +63,5 @@ const char *aug_error_details(augeas *aug);
80 void free(void *);
81 """)
82
83-lib = ffi.dlopen("augeas")
84-
85 if __name__ == "__main__":
86     ffi.compile(verbose=True)
87diff --git a/setup.py b/setup.py
88index 7d55877..17f9516 100755
89--- a/setup.py
90+++ b/setup.py
91@@ -22,6 +22,7 @@ setup(name=name,
92       setup_requires=["cffi>=1.0.0"],
93       cffi_modules=["augeas/ffi.py:ffi"],
94       install_requires=["cffi>=1.0.0"],
95+      zip_safe=False,
96       url="http://augeas.net/",
97       classifiers=[
98           "Programming Language :: Python :: 2.7",
99--
1002.31.1
101
102