Skip to content

Commit e7663bd

Browse files
committed
Refine sppmode
1 parent 898dde1 commit e7663bd

File tree

3 files changed

+69
-12
lines changed

3 files changed

+69
-12
lines changed

src/config.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,3 +50,12 @@
5050
reg_url = 'https://api.dashingsoft.com/product/key/activate/%s/'
5151
buy_url = 'https://order.shareit.com/cart/add?vendorid=200089125&PRODUCT[300871197]=1'
5252
help_url = 'https://pyarmor.readthedocs.io/{lang}/v%s/{page}' % version
53+
54+
sppmode_info = {
55+
'version': 'r48.2',
56+
'platforms': {
57+
'darwin.x86_64': '',
58+
'windows.x86_64': '',
59+
'linux.x86_64': '',
60+
}
61+
}

src/sppmode.py

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import ast
22
import logging
33
import os
4-
import platform
54
import struct
65
import sys
76

@@ -62,17 +61,10 @@ def build(source, modname, destname=None):
6261
def _load_sppbuild():
6362
global _spplib
6463
if _spplib is None:
65-
from utils import PYARMOR_PATH as libpath, PYARMOR_HOME as homepath
66-
plat = platform.system().lower()
67-
mach = platform.machine().lower()
68-
if mach not in ('x86_64', 'amd64'):
69-
raise RuntimeError('sppmode now only works in x86_64 platform')
70-
mach = 'x86_64'
71-
ext = '.dll' if plat.startswith('win') else '.so'
72-
name = os.path.join(libpath, 'platforms', plat, mach, 'sppmode' + ext)
64+
from utils import get_sppmode_files
65+
name, licfile = get_sppmode_files()
7366
_spplib = cdll.LoadLibrary(name)
7467
sppinit = PYFUNCTYPE(c_int, c_void_p, c_void_p)(('sppinit', _spplib))
75-
licfile = os.path.expanduser(os.path.join(homepath, 'license.lic'))
7668
logging.debug('Check license file "%s"', licfile)
7769
ret = sppinit(pythonapi._handle, licfile.encode())
7870
if ret == -1:

src/utils.py

Lines changed: 58 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,6 @@
3030
import shutil
3131
import struct
3232
import sys
33-
import tempfile
3433
from base64 import b64encode
3534
from codecs import BOM_UTF8
3635
from glob import glob
@@ -47,7 +46,7 @@
4746
import pytransform
4847
from config import dll_ext, dll_name, entry_lines, protect_code_template, \
4948
platform_url, platform_config, key_url, reg_url, \
50-
core_version, capsule_filename, platform_old_urls
49+
core_version, capsule_filename, platform_old_urls, sppmode_info
5150
from sppmode import build as sppbuild, mixin as sppmixin
5251

5352
PYARMOR_PATH = os.getenv('PYARMOR_PATH', os.path.dirname(__file__))
@@ -1717,3 +1716,60 @@ def exclude_functions(names=''):
17171716
if pytransform._pytransform.set_option(7, names.encode()) == -1:
17181717
raise RuntimeError('Excluding functions is not supported by this '
17191718
'version, please upgrade pyarmor to the latest')
1719+
1720+
1721+
def get_sppmode_files(timeout=None):
1722+
licfile = os.path.join(HOME_PATH, 'license.lic')
1723+
sppver = sppmode_info['version']
1724+
spplatforms = sppmode_info['platforms']
1725+
1726+
platpath = pytransform.format_platform().replace('\\', '/')
1727+
platid = platpath.replace('/', '.')
1728+
if platid not in spplatforms.keys():
1729+
raise RuntimeError('sppmode does NOT work in platform "%s"' % platid)
1730+
1731+
ext = '.dll' if platid.startswith('win') else '.so'
1732+
libname = os.path.join(HOME_PATH, 'sppmode' + ext)
1733+
vername = os.path.join(HOME_PATH, '.sppver')
1734+
if os.path.exists(vername) and os.path.exists(libname):
1735+
with open(vername) as f:
1736+
libver = f.readline().strip()
1737+
else:
1738+
libver = ''
1739+
1740+
if libver != sppver:
1741+
rcode = get_registration_code()
1742+
if not rcode:
1743+
raise RuntimeError('sppmode is not available in the trial version')
1744+
rcode = rcode.replace('-sn-1.txt', '')
1745+
with open(licfile, 'rb') as f:
1746+
licdata = f.read()
1747+
secret = _get_user_secret(licdata)
1748+
1749+
url = platform_url.format(version=sppver)
1750+
url = '/'.join([url, 'spp', platpath])
1751+
logging.info('Getting remote file: %s', url)
1752+
1753+
timeout = PYARMOR_TIMEOUT if timeout is None else timeout
1754+
req = Request(url)
1755+
auth = b64encode(('%s:%s' % (rcode, secret)).encode())
1756+
req.add_header('Authorization', 'Basic ' + auth.decode())
1757+
res = _urlopen(req, None, timeout)
1758+
logging.info('Downloading sppmode library for "%s" ...', platid)
1759+
if res is None:
1760+
raise RuntimeError('Download sppmode library failed')
1761+
1762+
data = res.read()
1763+
if hashlib.sha256(data).hexdigest() != spplatforms[platid]:
1764+
raise RuntimeError('Incomplete sppmode library is downloaded')
1765+
1766+
logging.info('Writing target file: %s', libname)
1767+
with open(libname, 'wb') as f:
1768+
f.write(data)
1769+
logging.info('Writing version file: %s', vername)
1770+
with open(vername, 'w') as f:
1771+
f.write(sppver)
1772+
1773+
logging.info('Download sppmode library "%s" OK', platid)
1774+
1775+
return libname, licfile

0 commit comments

Comments
 (0)