You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

95 lines
3.0 KiB

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