Skip to content

Commit

Permalink
chore: update Post install script for downloading binary files
Browse files Browse the repository at this point in the history
  • Loading branch information
zry656565 committed Sep 12, 2018
1 parent b09c63d commit e3648fd
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 35 deletions.
44 changes: 9 additions & 35 deletions setup.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,13 @@
import platform
import os
import sys

from urllib.request import urlopen

from setuptools import setup
from setuptools.command.install import install as InstallCommand

from tools.download import get_remote_binary


version = '0.3.13'
local_build = os.environ.get('LOCAL', 0)
local_install = os.environ.get('LOCAL', 0)


with open('README.md', 'r') as f:
Expand All @@ -20,37 +18,13 @@ class PostInstallCommand(InstallCommand):
"""Post-installation for installation mode."""

def run(self):
version_tag = 'v{}'.format(version)
url_template = 'https://github.com/LeetCode-OpenSource/py-sourcemap/releases/download/{tag}/py_sourcemap.{py_ver}-{platform}.{ext}'
(major, minor, _) = platform.python_version_tuple()
if major != '3' or not(minor in ['5', '6', '7']):
raise Exception('Only python 3.5, 3.6, 3.7 are supported')
system = platform.system()
if system == 'Linux':
py_version = 'cpython-{}{}m'.format(major, minor)
usr_platform = 'x86_64-linux-gnu'
ext = 'so'
elif system == 'Darwin':
py_version = 'cpython-{}{}m'.format(major, minor)
usr_platform = 'x86_64-apple-darwin'
ext = 'so'
elif system == 'Windows':
py_version = 'cp{}{}'.format(major, minor)
# from https://docs.python.org/3/library/platform.html
is_64bits = sys.maxsize > 2**32
usr_platform = 'win_amd64' if is_64bits else 'win32'
ext = 'pyd'
else:
raise Exception('Your system is unrecognized: {}'.format(system))
download_url = url_template.format(tag=version_tag,
py_ver=py_version,
platform=usr_platform,
ext=ext)
dist = os.path.join(self.build_lib, 'py_sourcemap/py_sourcemap.so')
if not local_build:
if not local_install:
dist = os.path.join(self.build_lib, 'py_sourcemap/py_sourcemap.so')
binary_fp = get_remote_binary(version)

with open(dist, 'wb') as f:
built_lib = urlopen(download_url).read()
f.write(built_lib)
f.write(binary_fp.read())

InstallCommand.run(self)


Expand Down
Empty file added tools/__init__.py
Empty file.
69 changes: 69 additions & 0 deletions tools/download.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
import sys
import time
import platform
from urllib import request


PING_TIMEOUT = 3


def _get_download_urls(version):
s3_url_template = 'https://github.com/LeetCode-OpenSource/py-sourcemap/releases/download/{tag}/py_sourcemap.{py_ver}-{platform}.{ext}'
aliyun_url_template = 'https://static.leetcode-cn.com/packages/py_sourcemap/{package_ver}/py_sourcemap.{py_ver}-{platform}.{ext}'

version_tag = 'v{}'.format(version)
(major, minor, _) = platform.python_version_tuple()
if major != '3' or not(minor in ['5', '6', '7']):
raise Exception('Only python 3.5, 3.6, 3.7 are supported')
system = platform.system()
if system == 'Linux':
py_version = 'cpython-{}{}m'.format(major, minor)
usr_platform = 'x86_64-linux-gnu'
ext = 'so'
elif system == 'Darwin':
py_version = 'cpython-{}{}m'.format(major, minor)
usr_platform = 'x86_64-apple-darwin'
ext = 'so'
elif system == 'Windows':
py_version = 'cp{}{}'.format(major, minor)
# from https://docs.python.org/3/library/platform.html
is_64bits = sys.maxsize > 2**32
usr_platform = 'win_amd64' if is_64bits else 'win32'
ext = 'pyd'
else:
raise Exception('Your system is unrecognized: {}'.format(system))

return {
's3': s3_url_template.format(tag=version_tag,
py_ver=py_version,
platform=usr_platform,
ext=ext),
'aliyun': aliyun_url_template.format(package_ver=version,
py_ver=py_version,
platform=usr_platform,
ext=ext)
}


def _get_latency(url):
req = request.Request(url, method="HEAD")
try:
start = time.perf_counter()
request.urlopen(req, timeout=PING_TIMEOUT)
end = time.perf_counter()
return end - start
except Exception:
return 999


def get_remote_binary(version):
urls = _get_download_urls(version)
print('Try ping servers...')
s3_time = _get_latency(urls['s3'])
aliyun_time = _get_latency(urls['aliyun'])
print('s3: {:.3f}s, aliyun: {:.3f}s'.format(s3_time, aliyun_time))
url = urls['s3'] if s3_time <= aliyun_time else urls['aliyun']
print('Start downloading {}...'.format(url))
binary_fp = request.urlopen(url)
print('Downloaded {}'.format(url))
return binary_fp

0 comments on commit e3648fd

Please sign in to comment.