Skip to content

Add data updater #212

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 10 commits into from
Jul 29, 2017
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
Added core.updater module
  • Loading branch information
Ayuto committed Jun 21, 2017
commit 72681be0e1b61e196a299f75d671f93aac6351e1
94 changes: 94 additions & 0 deletions addons/source-python/packages/source-python/core/updater.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
# ../core/updater.py

"""Provides functions to update Source.Python's data files."""

# =============================================================================
# >> IMPORTS
# =============================================================================
# Python Imports
import hashlib

from zipfile import ZipFile
from urllib.request import urlopen

# Source.Python Imports
from paths import DATA_PATH
from paths import SP_DATA_PATH


# =============================================================================
# >> ALL DECLARATION
# =============================================================================
__all__ = (
'CHECKSUM_URL',
'DATA_URL',
'DATA_ZIP_FILE',
'download_latest_data',
'get_latest_data_checksum',
'is_new_data_available',
'unpack_data',
'update_data'
)


# =============================================================================
# >> GLOBAL VARIABLES
# =============================================================================
DATA_ZIP_FILE = DATA_PATH / 'source-python-data.zip'
CHECKSUM_URL = 'http://data.sourcepython.com/checksum.txt'
DATA_URL = 'http://data.sourcepython.com/source-python-data.zip'


# =============================================================================
# >> FUNCTIONS
# =============================================================================
def get_latest_data_checksum(timeout=3):
"""Return the MD5 checksum of the latest data from the build server.

:param float timeout:
Number of seconds that need to pass until a timeout occurs.
:rtype: str
"""
with urlopen(CHECKSUM_URL, timeout=timeout) as url:
return url.read().decode()

def download_latest_data(timeout=3):
"""Download the latest data from the build server.

:param float timeout:
Number of seconds that need to pass until a timeout occurs.
"""
with urlopen(DATA_URL, timeout=timeout) as url:
data = url.read()

with DATA_ZIP_FILE.open('wb') as f:
f.write(data)

def unpack_data():
"""Unpack ``source-python-data.zip``."""
with ZipFile(DATA_ZIP_FILE) as zip:
zip.extractall(DATA_PATH)

def update_data(timeout=3):
"""Download and unpack the latest data from the build server.

:param float timeout:
Number of seconds that need to pass until a timeout occurs.
"""
download_latest_data(timeout)
if SP_DATA_PATH.isdir():
SP_DATA_PATH.rmtree()

unpack_data()

def is_new_data_available(timeout=3):
"""Return ``True`` if new data is available.

:param float timeout:
Number of seconds that need to pass until a timeout occurs.
:rtype: bool
"""
if not DATA_ZIP_FILE.isfile():
return True

return DATA_ZIP_FILE.read_hexhash('md5') != get_latest_data_checksum(timeout)