spaCy/spacy/en/download.py

67 lines
1.5 KiB
Python
Raw Normal View History

2015-02-01 05:22:23 +03:00
from __future__ import print_function
import sys
2015-01-02 13:44:41 +03:00
import os
import tarfile
import shutil
2015-01-31 05:51:56 +03:00
import plac
2015-01-02 13:44:41 +03:00
2015-10-19 09:32:41 +03:00
from . import uget
2015-10-23 02:56:13 +03:00
try:
FileExistsError
except NameError:
FileExistsError = Exception
2015-02-07 19:32:33 +03:00
# TODO: Read this from the same source as the setup
2015-10-23 01:56:57 +03:00
VERSION = '0.9.6'
2015-01-02 13:44:41 +03:00
AWS_STORE = 'https://s3-us-west-1.amazonaws.com/media.spacynlp.com'
2015-02-07 19:32:33 +03:00
ALL_DATA_DIR_URL = '%s/en_data_all-%s.tgz' % (AWS_STORE, VERSION)
2015-10-21 08:59:34 +03:00
DEST_DIR = os.path.dirname(os.path.abspath(__file__))
2015-01-02 13:44:41 +03:00
2015-10-21 08:59:34 +03:00
def download_file(url, download_path):
return uget.download(url, download_path, console=sys.stdout)
2015-01-02 13:44:41 +03:00
2015-10-21 08:59:34 +03:00
def install_data(url, extract_path, download_path):
2015-10-20 20:11:29 +03:00
try:
2015-10-21 08:59:34 +03:00
os.makedirs(extract_path)
2015-10-20 20:11:29 +03:00
except FileExistsError:
pass
2015-10-20 20:33:59 +03:00
tmp = download_file(url, download_path)
assert tmp == download_path
t = tarfile.open(download_path)
2015-10-21 08:59:34 +03:00
t.extractall(extract_path)
os.unlink(download_path)
@plac.annotations(
force=("Force overwrite", "flag", "f", bool),
)
def main(data_size='all', force=False):
2015-10-20 20:11:29 +03:00
filename = ALL_DATA_DIR_URL.rsplit('/', 1)[1]
2015-10-21 08:59:34 +03:00
download_path = os.path.join(DEST_DIR, filename)
data_path = os.path.join(DEST_DIR, 'data')
if force and os.path.exists(download_path):
os.unlink(download_path)
if force and os.path.exists(data_path):
shutil.rmtree(data_path)
2015-10-20 20:11:29 +03:00
2015-10-21 08:59:34 +03:00
if os.path.exists(data_path):
print('data already installed at %s, overwrite with --force' % DEST_DIR)
sys.exit(1)
2015-10-21 08:59:34 +03:00
install_data(ALL_DATA_DIR_URL, DEST_DIR, download_path)
2015-01-02 13:44:41 +03:00
if __name__ == '__main__':
2015-01-31 05:51:56 +03:00
plac.call(main)