Skip to content

Commit cdd28a0

Browse files
committed
Include ICU data file as Python wheel data file
1 parent 23ec34b commit cdd28a0

12 files changed

+118
-47
lines changed

.github/workflows/debian.yml

+5-4
Original file line numberDiff line numberDiff line change
@@ -27,22 +27,23 @@ jobs:
2727

2828
- name: Install dependencies
2929
run: |
30-
sudo apt install libboost-dev libboost-system-dev libboost-iostreams-dev libboost-python-dev
30+
sudo apt install libboost-dev libboost-system-dev libboost-iostreams-dev libboost-filesystem-dev libboost-python-dev
3131
pip install --upgrade pip setuptools wheel pytest pytest-order
3232
3333
- name: Build wheel
3434
run: |
3535
python setup.py v8
3636
python setup.py sdist bdist_wheel -d stpyv8-debian-11-${{ matrix.python-version }}
37-
cp v8/out.gn/x64.release.sample/icudtl.dat stpyv8-debian-11-${{ matrix.python-version }}/icudtl.dat
37+
# cp v8/out.gn/x64.release.sample/icudtl.dat stpyv8-debian-11-${{ matrix.python-version }}/icudtl.dat
3838
env:
3939
V8_DEPS_LINUX: 0
4040
LDFLAGS: -L/usr/lib -L/usr/lib/x86_64-linux-gnu
4141

4242
- name: Install wheel
4343
run : |
44-
cp stpyv8-debian-11-${{ matrix.python-version }}/icudtl.dat /usr/share/stpyv8
45-
pip install --upgrade stpyv8-debian-11-${{ matrix.python-version }}/stpyv8*.whl
44+
# cp stpyv8-debian-11-${{ matrix.python-version }}/icudtl.dat /usr/share/stpyv8
45+
# pip install --upgrade stpyv8-debian-11-${{ matrix.python-version }}/stpyv8*.whl
46+
pip install .
4647
4748
- name: Test wheel
4849
run: |

.github/workflows/osx-debug.yml

+3-2
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,9 @@ jobs:
3636
run: |
3737
python setup.py v8
3838
sudo --preserve-env=STPYV8_DEBUG python setup.py sdist bdist_wheel -d stpyv8-${{ matrix.os }}-${{ matrix.python-version }}-debug
39-
sudo --preserve-env=STPYV8_DEBUG python setup.py install
40-
sudo --preserve-env=STPYV8_DEBUG cp v8/out.gn/x64.release.sample/icudtl.dat stpyv8-${{ matrix.os }}-${{ matrix.python-version }}-debug/icudtl.dat
39+
# sudo --preserve-env=STPYV8_DEBUG python setup.py install
40+
# sudo --preserve-env=STPYV8_DEBUG cp v8/out.gn/x64.release.sample/icudtl.dat stpyv8-${{ matrix.os }}-${{ matrix.python-version }}-debug/icudtl.dat
41+
sudo --preserve-env=STPYV8_DEBUG pip install .
4142
env:
4243
STPYV8_DEBUG: 1
4344

.github/workflows/osx.yml

+3-2
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,9 @@ jobs:
4242
run: |
4343
python setup.py v8
4444
sudo python setup.py sdist bdist_wheel -d stpyv8-${{ matrix.os }}-${{ matrix.python-version }}
45-
sudo python setup.py install
46-
sudo cp v8/out.gn/x64.release.sample/icudtl.dat stpyv8-${{ matrix.os }}-${{ matrix.python-version }}/icudtl.dat
45+
# sudo python setup.py install
46+
# sudo cp v8/out.gn/x64.release.sample/icudtl.dat stpyv8-${{ matrix.os }}-${{ matrix.python-version }}/icudtl.dat
47+
sudo pip install .
4748
4849
- name: Test wheel
4950
run: |

.github/workflows/ubuntu-debug.yml

+4-3
Original file line numberDiff line numberDiff line change
@@ -27,15 +27,16 @@ jobs:
2727

2828
- name: STEP 3. Install dependencies
2929
run: |
30-
sudo apt install libboost-dev libboost-system-dev libboost-iostreams-dev libboost-python-dev
30+
sudo apt install libboost-dev libboost-system-dev libboost-iostreams-dev libboost-filesystem-dev libboost-python-dev
3131
pip install --upgrade pip setuptools wheel pytest pytest-order
3232
3333
- name: STEP 4. Build wheel
3434
run: |
3535
python setup.py v8
3636
python setup.py sdist bdist_wheel -d stpyv8-${{ matrix.os }}-${{ matrix.python-version }}-debug
37-
python setup.py install
38-
cp v8/out.gn/x64.release.sample/icudtl.dat stpyv8-${{ matrix.os }}-${{ matrix.python-version }}-debug/icudtl.dat
37+
# python setup.py install
38+
# cp v8/out.gn/x64.release.sample/icudtl.dat stpyv8-${{ matrix.os }}-${{ matrix.python-version }}-debug/icudtl.dat
39+
pip install .
3940
env:
4041
STPYV8_DEBUG: 1
4142
V8_DEPS_LINUX: 0

.github/workflows/ubuntu.yml

+4-3
Original file line numberDiff line numberDiff line change
@@ -33,15 +33,16 @@ jobs:
3333

3434
- name: Install dependencies
3535
run: |
36-
sudo apt install libboost-dev libboost-system-dev libboost-iostreams-dev libboost-python-dev
36+
sudo apt install libboost-dev libboost-system-dev libboost-iostreams-dev libboost-filesystem-dev libboost-python-dev
3737
pip install --upgrade pip setuptools wheel pytest pytest-order
3838
3939
- name: Build wheel
4040
run: |
4141
python setup.py v8
4242
python setup.py sdist bdist_wheel -d stpyv8-${{ matrix.os }}-${{ matrix.python-version }}
43-
python setup.py install
44-
cp v8/out.gn/x64.release.sample/icudtl.dat stpyv8-${{ matrix.os }}-${{ matrix.python-version }}/icudtl.dat
43+
# python setup.py install
44+
# cp v8/out.gn/x64.release.sample/icudtl.dat stpyv8-${{ matrix.os }}-${{ matrix.python-version }}/icudtl.dat
45+
pip install .
4546
env:
4647
V8_DEPS_LINUX: 0
4748
LDFLAGS: -L/usr/lib -L/usr/lib/x86_64-linux-gnu

.github/workflows/windows.yml

+4-3
Original file line numberDiff line numberDiff line change
@@ -183,15 +183,16 @@ jobs:
183183
echo "::error::Wheel build failed"
184184
exit 1
185185
}
186-
python setup.py install
186+
# python setup.py install
187+
pip install .
187188
if (-not $?) {
188189
echo "::error::Wheel installation failed"
189190
exit 1
190191
}
191192
192-
ls v8\out.gn\x64.release.sample\
193+
# ls v8\out.gn\x64.release.sample\
193194
194-
cp v8\out.gn\x64.release.sample\icudtl.dat ${{ env.DIST_NAME }}
195+
# cp v8\out.gn\x64.release.sample\icudtl.dat ${{ env.DIST_NAME }}
195196
196197
- name: STEP 6. Test wheel
197198
run: |

STPyV8.py

+45
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@
44
from __future__ import with_statement
55
from __future__ import print_function
66

7+
import os
8+
import sys
9+
import importlib.resources
710
import re
811
import collections.abc
912

@@ -33,6 +36,18 @@
3336
"JSPlatform"]
3437

3538

39+
# ICU
40+
ICU_DATA_FOLDERS_UNIX = ("/usr/share/stpyv8", os.path.expanduser("~/.local/share/stpyv8"))
41+
ICU_DATA_FOLDERS_OSX = ("/Library/Application Support/STPyV8", os.path.expanduser('~/Library/Application Support/STPyV8'))
42+
ICU_DATA_FOLDERS_WINDOWS = (os.path.join(os.environ["PROGRAMDATA"], "STPyV8") if "PROGRAMDATA" in os.environ else None, )
43+
44+
icu_data_folders = None
45+
if os.name in ("posix", ):
46+
icu_data_folders = ICU_DATA_FOLDERS_OSX if sys.platform in ("darwin", ) else ICU_DATA_FOLDERS_UNIX
47+
else:
48+
icu_data_folders = ICU_DATA_FOLDERS_WINDOWS
49+
50+
3651
class JSAttribute:
3752
def __init__(self, name):
3853
self.name = name
@@ -326,6 +341,36 @@ def __exit__(self, exc_type, exc_value, traceback):
326341
del self
327342

328343

344+
345+
def icu_sync():
346+
try:
347+
files = importlib.resources.files('stpyv8-icu')
348+
except ModuleNotFoundError:
349+
return
350+
351+
for f in files.iterdir():
352+
if f.name not in ('icudtl.dat', ):
353+
continue
354+
355+
synced = False
356+
357+
data = f.read_bytes()
358+
359+
for folder in icu_data_folders:
360+
try:
361+
os.makedirs(folder, exist_ok = True)
362+
with open(os.path.join(folder, 'icudtl.dat'), mode = 'wb') as fd:
363+
fd.write(data)
364+
365+
synced = True
366+
except PermissionError:
367+
pass
368+
369+
if synced:
370+
f.unlink()
371+
372+
icu_sync()
373+
329374
v8_default_platform = JSPlatform()
330375
v8_default_platform.init()
331376

settings.py

+1-10
Original file line numberDiff line numberDiff line change
@@ -16,15 +16,6 @@
1616

1717
v8_deps_linux = os.environ.get('V8_DEPS_LINUX', '1') in ('1', )
1818

19-
ICU_DATA_FOLDER_UNIX = "/usr/share/stpyv8"
20-
ICU_DATA_FOLDER_OSX = "/Library/Application Support/STPyV8/"
21-
ICU_DATA_FOLDER_WINDOWS = os.path.join(os.environ["PROGRAMDATA"], "STPyV8") if "PROGRAMDATA" in os.environ else None
22-
23-
if os.name in ("posix", ):
24-
icu_data_folder = ICU_DATA_FOLDER_OSX if sys.platform in ("darwin", ) else ICU_DATA_FOLDER_UNIX
25-
else:
26-
icu_data_folder = None
27-
2819
os.environ['PATH'] = f"{os.environ.get('PATH', '')}:{DEPOT_HOME}"
2920

3021
gn_args = {
@@ -130,7 +121,7 @@ def get_libboost_python_name():
130121
os.environ["DEPOT_TOOLS_WIN_TOOLCHAIN"] = "0"
131122

132123
elif os.name in ("posix", ):
133-
libraries = ["boost_system", "boost_iostreams", "v8_monolith", STPYV8_BOOST_PYTHON]
124+
libraries = ["boost_system", "boost_iostreams", "boost_filesystem", "v8_monolith", STPYV8_BOOST_PYTHON]
134125
extra_compile_args.append('-std=c++17')
135126

136127
if platform.system() in ('Linux', ):

setup.py

+5-6
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@
1313

1414
log = logging.getLogger()
1515

16+
ICU_DATA_PACKAGE_FOLDER = os.path.join(os.pardir, os.pardir, "stpyv8-icu")
17+
ICU_DATA_V8_FILE_PATH = os.path.join("v8", "out.gn", "x64.release.sample", "icudtl.dat")
1618

1719
def exec_cmd(cmdline, *args, **kwargs):
1820
msg = kwargs.get('msg')
@@ -196,12 +198,6 @@ def run(self):
196198
class stpyv8_install(install):
197199
def run(self):
198200
self.skip_build = True # pylint:disable=attribute-defined-outside-init
199-
200-
if icu_data_folder:
201-
os.makedirs(icu_data_folder, exist_ok = True)
202-
shutil.copy(os.path.join(V8_HOME, os.path.join("out.gn", "x64.release.sample", "icudtl.dat")),
203-
icu_data_folder)
204-
205201
install.run(self)
206202

207203

@@ -230,6 +226,9 @@ def run(self):
230226
py_modules = ["STPyV8"],
231227
ext_modules = [stpyv8],
232228
install_requires = ["wheel"],
229+
data_files = [
230+
(ICU_DATA_PACKAGE_FOLDER, [ICU_DATA_V8_FILE_PATH]),
231+
],
233232
classifiers = [
234233
"Development Status :: 4 - Beta",
235234
"Environment :: Plugins",

src/Config.h

+12-5
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,20 @@
77
#define SUPPORT_TRACE_LIFECYCLE 1
88

99
// ICU data file
10-
#define ICU_DATA_UNIX "/usr/share/stpyv8/icudtl.dat"
11-
#define ICU_DATA_OSX "/Library/Application Support/STPyV8/icudtl.dat"
1210

1311
#if defined(__linux)
14-
# define ICU_DATA ICU_DATA_UNIX
12+
# define ICU_DATA_SYSTEM "/usr/share/stpyv8/icudtl.dat"
13+
# define ICU_DATA_USER ".local/share/stpyv8/icudtl.dat"
1514
#elif defined(__APPLE)
16-
# define ICU_DATA ICU_DATA_OSX
15+
# define ICU_DATA_SYSTEM "/Library/Application Support/STPyV8/icudtl.dat"
16+
# define ICU_DATA_USER "Library/Application Support/STPyV8/icudtl.dat"
17+
#elif defined(_WIN32)
18+
# define ICU_DATA_SYSTEM nullptr
19+
# define ICU_DATA_USER "\\STPyV8\\icudtl.dat"
20+
#elif defined (_WIN64)
21+
# define ICU_DATA_SYSTEM nullptr
22+
# define ICU_DATA_USER "\\STPyV8\\icudtl.dat"
1723
#else
18-
# define ICU_DATA nullptr
24+
# define ICU_DATA_SYSTEM nullptr
25+
# define ICU_DATA_USER nullptr
1926
#endif

src/Platform.cpp

+28
Original file line numberDiff line numberDiff line change
@@ -20,3 +20,31 @@ void CPlatform::Init()
2020

2121
inited = true;
2222
}
23+
24+
const char * CPlatform::GetICUDataFile()
25+
{
26+
#if defined(_WIN32) || defined (_WIN64)
27+
boost::filesystem::path icu_data_path = getenv("PROGRAMDATA");
28+
#else
29+
boost::filesystem::path icu_data_path = getenv("HOME");
30+
#endif
31+
32+
if (boost::filesystem::is_directory(icu_data_path)) {
33+
icu_data_path /= icu_data_user;
34+
35+
std::string icu_data_path_str = icu_data_path.string();
36+
const char *icu_data_path_ptr = icu_data_path_str.c_str();
37+
38+
std::ifstream ifile(icu_data_path_ptr);
39+
if (ifile.good())
40+
return icu_data_path_ptr;
41+
}
42+
43+
if (icu_data_system != nullptr) {
44+
std::ifstream ifile(icu_data_system);
45+
if (ifile.good())
46+
return icu_data_system;
47+
}
48+
49+
return nullptr;
50+
}

src/Platform.h

+4-9
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#pragma once
22

33
#include <fstream>
4+
#include <boost/filesystem.hpp>
45
#include <v8.h>
56

67
#include "Config.h"
@@ -11,17 +12,11 @@ class CPlatform
1112
private:
1213
static bool inited;
1314
static std::unique_ptr<v8::Platform> platform;
14-
constexpr static const char *icu_data = ICU_DATA;
1515

16-
const char *GetICUDataFile()
17-
{
18-
if (icu_data == nullptr) return nullptr;
16+
constexpr static const char *icu_data_system = ICU_DATA_SYSTEM;
17+
constexpr static const char *icu_data_user = ICU_DATA_USER;
1918

20-
std::ifstream ifile(icu_data);
21-
if (ifile) return icu_data;
22-
23-
return nullptr;
24-
}
19+
const char *GetICUDataFile();
2520

2621
std::string argv;
2722
public:

0 commit comments

Comments
 (0)