From 136f8ca94dc8c9eee6f8dbca413ea8725fa05dc0 Mon Sep 17 00:00:00 2001 From: guoling Date: Thu, 27 Jun 2024 21:36:35 +0800 Subject: [PATCH] adapt Win32 support #1289 #1302 --- Python/CMakeLists.txt | 7 ++++++- Python/demo.py | 19 ++++++++++++------ Python/libmmkv_python.cpp | 41 ++++++++++++++++++++++----------------- Python/setup.py | 27 +++++++++++++------------- Python/unit_test.py | 5 ++++- 5 files changed, 59 insertions(+), 40 deletions(-) diff --git a/Python/CMakeLists.txt b/Python/CMakeLists.txt index a4ee7344..eeeb9baa 100644 --- a/Python/CMakeLists.txt +++ b/Python/CMakeLists.txt @@ -53,5 +53,10 @@ set_target_properties(mmkv PROPERTIES target_link_libraries( # Specifies the target library. mmkv PRIVATE core - pthread ) + +IF (NOT WIN32) + target_link_libraries(mmkv + PRIVATE + pthread) +ENDIF() diff --git a/Python/demo.py b/Python/demo.py index 72ae8985..62814997 100644 --- a/Python/demo.py +++ b/Python/demo.py @@ -4,6 +4,7 @@ import sys import mmkv import time +import tempfile def functional_test(mmap_id, decode_only): @@ -58,7 +59,8 @@ def functional_test(mmap_id, decode_only): def test_backup(): - root_dir = "/tmp/mmkv_backup" + temp_dir = tempfile.gettempdir() + root_dir = temp_dir + "/mmkv_backup" mmap_id = "test_python" ret = mmkv.MMKV.backupOneToDirectory(mmap_id, root_dir) print("backup one return: ", ret) @@ -100,7 +102,8 @@ def test_expected_capacity(): def test_restore(): - root_dir = "/tmp/mmkv_backup" + temp_dir = tempfile.gettempdir() + root_dir = temp_dir + "/mmkv_backup" mmap_id = "test/Encrypt" aes_key = "cryptKey" a_kv = mmkv.MMKV(mmap_id, mmkv.MMKVMode.SingleProcess, aes_key) @@ -161,7 +164,7 @@ def test_compare_before_set(): actualSize2 = kv.actualSize() print("testCompareBeforeSet: actualSize2 = ", actualSize2) if actualSize1 != actualSize2: - panic("size not match") + raise("size not match") kv.set(False, key) print("testCompareBeforeSet: bool value = ", kv.getBool(key)) @@ -201,7 +204,8 @@ def test_remove_storage(): if kv.count() != 0: print("storage not successfully remove") - rootDir = "/dev/mmkv_sg" + temp_dir = tempfile.gettempdir() + rootDir = temp_dir + "/dev/mmkv_sg" kv = mmkv.MMKV("test_remove/sg", rootDir = rootDir) kv.set(True, "bool") @@ -230,9 +234,12 @@ def content_change_handler(mmap_id): if __name__ == '__main__': + temp_dir = tempfile.gettempdir() + root_dir = temp_dir + '/mmkv' + # you can enable logging & log handler - # mmkv.MMKV.initializeMMKV('/tmp/mmkv', mmkv.MMKVLogLevel.Info, logger) - mmkv.MMKV.initializeMMKV('/tmp/mmkv') + # mmkv.MMKV.initializeMMKV(root_dir, mmkv.MMKVLogLevel.Info, logger) + mmkv.MMKV.initializeMMKV(root_dir) # redirect logging # mmkv.MMKV.registerLogHandler(logger) diff --git a/Python/libmmkv_python.cpp b/Python/libmmkv_python.cpp index da810ac8..4a109751 100644 --- a/Python/libmmkv_python.cpp +++ b/Python/libmmkv_python.cpp @@ -23,6 +23,11 @@ #include #include +#ifdef MMKV_WIN32 +#include +typedef SSIZE_T ssize_t; +#endif + using namespace mmkv; using namespace std; namespace py = pybind11; @@ -99,10 +104,10 @@ PYBIND11_MODULE(mmkv, m) { // py::arg("cryptKey") = (string*) nullptr, // py::arg("rootDir") = (string*) nullptr); - clsMMKV.def(py::init([](const string &mmapID, MMKVMode mode, const string &cryptKey, const string &rootDir, + clsMMKV.def(py::init([](const string &mmapID, MMKVMode mode, const string &cryptKey, const MMKVPath_t &rootDir, const size_t expectedCapacity) { string *cryptKeyPtr = (cryptKey.length() > 0) ? (string *) &cryptKey : nullptr; - string *rootDirPtr = (rootDir.length() > 0) ? (string *) &rootDir : nullptr; + MMKVPath_t *rootDirPtr = (rootDir.length() > 0) ? (MMKVPath_t *) &rootDir : nullptr; return MMKV::mmkvWithID(mmapID, mode, cryptKeyPtr, rootDirPtr, expectedCapacity); }), "Parameters:\n" @@ -118,7 +123,7 @@ PYBIND11_MODULE(mmkv, m) { clsMMKV.def_static( "initializeMMKV", - [](const string &rootDir, MMKVLogLevel logLevel, decltype(g_logHandler) logHandler) { + [](const MMKVPath_t &rootDir, MMKVLogLevel logLevel, decltype(g_logHandler) logHandler) { if (logHandler) { g_logHandler = std::move(logHandler); MMKV::initializeMMKV(rootDir, logLevel, MyLogHandler); @@ -339,46 +344,46 @@ PYBIND11_MODULE(mmkv, m) { clsMMKV.def_static( "backupOneToDirectory", - [](const string &mmapID, const string &dstDir, const string &srcDir) { - string *srcDirPtr = (srcDir.length() > 0) ? (string *) &srcDir : nullptr; + [](const string &mmapID, const MMKVPath_t &dstDir, const MMKVPath_t &srcDir) { + MMKVPath_t *srcDirPtr = (srcDir.length() > 0) ? (MMKVPath_t *) &srcDir : nullptr; return MMKV::backupOneToDirectory(mmapID, dstDir, srcDirPtr); }, "backup one MMKV instance from srcDir (default to the root dir of MMKV) to dstDir", py::arg("mmapID"), - py::arg("dstDir"), py::arg("srcDir") = string()); + py::arg("dstDir"), py::arg("srcDir") = MMKVPath_t()); clsMMKV.def_static( "restoreOneFromDirectory", - [](const string &mmapID, const string &srcDir, const string &dstDir) { - string *dstDirPtr = (dstDir.length() > 0) ? (string *) &dstDir : nullptr; + [](const string &mmapID, const MMKVPath_t &srcDir, const MMKVPath_t &dstDir) { + MMKVPath_t *dstDirPtr = (dstDir.length() > 0) ? (MMKVPath_t *) &dstDir : nullptr; return MMKV::restoreOneFromDirectory(mmapID, srcDir, dstDirPtr); }, "restore one MMKV instance from srcDir to dstDir (default to the root dir of MMKV)", py::arg("mmapID"), - py::arg("srcDir"), py::arg("dstDir") = string()); + py::arg("srcDir"), py::arg("dstDir") = MMKVPath_t()); clsMMKV.def_static( "backupAllToDirectory", - [](const string &dstDir, const string &srcDir) { - string *srcDirPtr = (srcDir.length() > 0) ? (string *) &srcDir : nullptr; + [](const MMKVPath_t &dstDir, const MMKVPath_t &srcDir) { + MMKVPath_t *srcDirPtr = (srcDir.length() > 0) ? (MMKVPath_t *) &srcDir : nullptr; return MMKV::backupAllToDirectory(dstDir, srcDirPtr); }, "backup all MMKV instance from srcDir (default to the root dir of MMKV) to dstDir", py::arg("dstDir"), - py::arg("srcDir") = string()); + py::arg("srcDir") = MMKVPath_t()); clsMMKV.def_static( "restoreAllFromDirectory", - [](const string &srcDir, const string &dstDir) { - string *dstDirPtr = (dstDir.length() > 0) ? (string *) &dstDir : nullptr; + [](const MMKVPath_t &srcDir, const MMKVPath_t &dstDir) { + MMKVPath_t *dstDirPtr = (dstDir.length() > 0) ? (MMKVPath_t *) &dstDir : nullptr; return MMKV::restoreAllFromDirectory(srcDir, dstDirPtr); }, "restore all MMKV instance from srcDir to dstDir (default to the root dir of MMKV)", py::arg("srcDir"), - py::arg("dstDir") = string()); + py::arg("dstDir") = MMKVPath_t()); clsMMKV.def_static( "removeStorage", - [](const string &mmapID, const string &rootDir) { - string *rootDirPtr = (rootDir.length() > 0) ? (string *) &rootDir : nullptr; + [](const string &mmapID, const MMKVPath_t &rootDir) { + MMKVPath_t *rootDirPtr = (rootDir.length() > 0) ? (MMKVPath_t *) &rootDir : nullptr; return MMKV::removeStorage(mmapID, rootDirPtr); }, "remove the storage of the MMKV, including the data file & meta file (.crc)", py::arg("mmapID"), - py::arg("rootDir") = string()); + py::arg("rootDir") = MMKVPath_t()); } diff --git a/Python/setup.py b/Python/setup.py index 340f3f69..32cf20e5 100644 --- a/Python/setup.py +++ b/Python/setup.py @@ -2,9 +2,9 @@ # -*- coding: utf-8 -*- import os -# import re +import re import sys -# import platform +import platform import subprocess from setuptools import setup, Extension @@ -26,10 +26,10 @@ class CMakeBuild(build_ext): raise RuntimeError("CMake must be installed to build the following extensions: " + ", ".join(e.name for e in self.extensions)) - # if platform.system() == "Windows": - # cmake_version = LooseVersion(re.search(r'version\s*([\d.]+)', out.decode()).group(1)) - # if cmake_version < '3.1.0': - # raise RuntimeError("CMake >= 3.1.0 is required on Windows") + if platform.system() == "Windows": + cmake_version = LooseVersion(re.search(r'version\s*([\d.]+)', out.decode()).group(1)) + if cmake_version < '3.1.0': + raise RuntimeError("CMake >= 3.1.0 is required on Windows") for ext in self.extensions: self.build_extension(ext) @@ -46,13 +46,12 @@ class CMakeBuild(build_ext): cfg = 'Debug' if self.debug else 'Release' build_args = ['--config', cfg] - # if platform.system() == "Windows": - # cmake_args += ['-DCMAKE_LIBRARY_OUTPUT_DIRECTORY_{}={}'.format(cfg.upper(), extdir)] - # if sys.maxsize > 2 ** 32: - # cmake_args += ['-A', 'x64'] - # build_args += ['--', '/m'] - # else: - if True: + if platform.system() == "Windows": + cmake_args += ['-DCMAKE_LIBRARY_OUTPUT_DIRECTORY_{}={}'.format(cfg.upper(), extdir)] + if sys.maxsize > 2 ** 32: + cmake_args += ['-A', 'x64'] + build_args += ['--', '/m'] + else: cmake_args += ['-DCMAKE_BUILD_TYPE=' + cfg] build_args += ['--', '-j8'] @@ -66,7 +65,7 @@ class CMakeBuild(build_ext): setup( name='mmkv', - version='1.2.13', + version='1.3.6', author='Tencent', description='An efficient, small key-value storage framework developed by WeChat Team.', long_description='The MMKV, for Python. MMKV is an efficient, generic, easy-to-use key-value storage framework ' diff --git a/Python/unit_test.py b/Python/unit_test.py index 72a9a106..388d2da7 100644 --- a/Python/unit_test.py +++ b/Python/unit_test.py @@ -3,6 +3,7 @@ import sys import mmkv +import tempfile KeyNotExist = 'KeyNotExist' @@ -193,7 +194,9 @@ def test_equal(kv, mmap_id): if __name__ == '__main__': - mmkv.MMKV.initializeMMKV('/tmp/mmkv') + temp_dir = tempfile.gettempdir() + root_dir = temp_dir + '/mmkv' + mmkv.MMKV.initializeMMKV(root_dir) kv = mmkv.MMKV('unit_test_python') test_bool(kv) test_int(kv)