|
30 | 30 | import shutil |
31 | 31 | import struct |
32 | 32 | import sys |
33 | | -import tempfile |
34 | 33 | from base64 import b64encode |
35 | 34 | from codecs import BOM_UTF8 |
36 | 35 | from glob import glob |
|
47 | 46 | import pytransform |
48 | 47 | from config import dll_ext, dll_name, entry_lines, protect_code_template, \ |
49 | 48 | 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 |
51 | 50 | from sppmode import build as sppbuild, mixin as sppmixin |
52 | 51 |
|
53 | 52 | PYARMOR_PATH = os.getenv('PYARMOR_PATH', os.path.dirname(__file__)) |
@@ -1717,3 +1716,60 @@ def exclude_functions(names=''): |
1717 | 1716 | if pytransform._pytransform.set_option(7, names.encode()) == -1: |
1718 | 1717 | raise RuntimeError('Excluding functions is not supported by this ' |
1719 | 1718 | '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