Jelajahi Sumber

create new platform_posix module

move common posix cython code there.
Thomas Waldmann 9 tahun lalu
induk
melakukan
d7500a1191
5 mengubah file dengan 16 tambahan dan 19 penghapusan
  1. 1 6
      borg/platform_darwin.pyx
  2. 1 6
      borg/platform_freebsd.pyx
  3. 2 6
      borg/platform_linux.pyx
  4. 5 0
      borg/platform_posix.pyx
  5. 7 1
      setup.py

+ 1 - 6
borg/platform_darwin.pyx

@@ -1,14 +1,9 @@
 import os
 from .helpers import user2uid, group2gid, safe_decode, safe_encode
+from .platform_posix import swidth
 
 API_VERSION = 3
 
-cdef extern from "wchar.h":
-    cdef int wcswidth(const Py_UNICODE *str, size_t n)
-
-def swidth(s):
-    return wcswidth(s, len(s))
-
 cdef extern from "sys/acl.h":
     ctypedef struct _acl_t:
         pass

+ 1 - 6
borg/platform_freebsd.pyx

@@ -1,5 +1,6 @@
 import os
 from .helpers import posix_acl_use_stored_uid_gid, safe_encode, safe_decode
+from .platform_posix import swidth
 
 API_VERSION = 3
 
@@ -7,12 +8,6 @@ cdef extern from "errno.h":
     int errno
     int EINVAL
 
-cdef extern from "wchar.h":
-    cdef int wcswidth(const Py_UNICODE *str, size_t n)
-
-def swidth(s):
-    return wcswidth(s, len(s))
-
 cdef extern from "sys/types.h":
     int ACL_TYPE_ACCESS
     int ACL_TYPE_DEFAULT

+ 2 - 6
borg/platform_linux.pyx

@@ -5,16 +5,12 @@ import stat
 
 from .helpers import posix_acl_use_stored_uid_gid, user2uid, group2gid, safe_decode, safe_encode
 from .platform_base import SyncFile as BaseSyncFile
+from .platform_posix import swidth
+
 from libc cimport errno
 
 API_VERSION = 3
 
-cdef extern from "wchar.h":
-    cdef int wcswidth(const Py_UNICODE *str, size_t n)
-
-def swidth(s):
-    return wcswidth(s, len(s))
-
 cdef extern from "sys/types.h":
     int ACL_TYPE_ACCESS
     int ACL_TYPE_DEFAULT

+ 5 - 0
borg/platform_posix.pyx

@@ -0,0 +1,5 @@
+cdef extern from "wchar.h":
+    cdef int wcswidth(const Py_UNICODE *str, size_t n)
+ 
+def swidth(s):
+    return wcswidth(s, len(s))

+ 7 - 1
setup.py

@@ -40,6 +40,7 @@ compress_source = 'borg/compress.pyx'
 crypto_source = 'borg/crypto.pyx'
 chunker_source = 'borg/chunker.pyx'
 hashindex_source = 'borg/hashindex.pyx'
+platform_posix_source = 'borg/platform_posix.pyx'
 platform_linux_source = 'borg/platform_linux.pyx'
 platform_darwin_source = 'borg/platform_darwin.pyx'
 platform_freebsd_source = 'borg/platform_freebsd.pyx'
@@ -60,6 +61,7 @@ try:
                 'borg/crypto.c',
                 'borg/chunker.c', 'borg/_chunker.c',
                 'borg/hashindex.c', 'borg/_hashindex.c',
+                'borg/platform_posix.c',
                 'borg/platform_linux.c',
                 'borg/platform_freebsd.c',
                 'borg/platform_darwin.c',
@@ -75,13 +77,14 @@ except ImportError:
     crypto_source = crypto_source.replace('.pyx', '.c')
     chunker_source = chunker_source.replace('.pyx', '.c')
     hashindex_source = hashindex_source.replace('.pyx', '.c')
+    platform_posix_source = platform_posix_source.replace('.pyx', '.c')
     platform_linux_source = platform_linux_source.replace('.pyx', '.c')
     platform_freebsd_source = platform_freebsd_source.replace('.pyx', '.c')
     platform_darwin_source = platform_darwin_source.replace('.pyx', '.c')
     from distutils.command.build_ext import build_ext
     if not on_rtd and not all(os.path.exists(path) for path in [
         compress_source, crypto_source, chunker_source, hashindex_source,
-        platform_linux_source, platform_freebsd_source]):
+        platform_posix_source, platform_linux_source, platform_freebsd_source]):
         raise ImportError('The GIT version of Borg needs Cython. Install Cython or use a released version.')
 
 
@@ -286,6 +289,9 @@ if not on_rtd:
     Extension('borg.chunker', [chunker_source]),
     Extension('borg.hashindex', [hashindex_source])
 ]
+    if sys.platform.startswith(('linux', 'freebsd', 'darwin')):
+        ext_modules.append(Extension('borg.platform_posix', [platform_posix_source]))
+
     if sys.platform == 'linux':
         ext_modules.append(Extension('borg.platform_linux', [platform_linux_source], libraries=['acl']))
     elif sys.platform.startswith('freebsd'):