spaCy/spacy/en/download.py

67 lines
1.5 KiB
Python
Raw Normal View History

2015-02-01 02:22:23 +00:00
from __future__ import print_function
import sys
2015-01-02 10:44:41 +00:00
import os
import tarfile
import shutil
2015-01-31 02:51:56 +00:00
import plac
2015-01-02 10:44:41 +00:00
2015-10-19 06:32:41 +00:00
from . import uget
2015-10-22 23:56:13 +00:00
try:
FileExistsError
except NameError:
FileExistsError = Exception
2015-02-07 16:32:33 +00:00
# TODO: Read this from the same source as the setup
2015-11-08 10:24:25 +00:00
VERSION = '0.9.9'
2015-01-02 10:44:41 +00:00
AWS_STORE = 'https://s3-us-west-1.amazonaws.com/media.spacynlp.com'
2015-02-07 16:32:33 +00:00
ALL_DATA_DIR_URL = '%s/en_data_all-%s.tgz' % (AWS_STORE, VERSION)
2015-10-21 05:59:34 +00:00
DEST_DIR = os.path.dirname(os.path.abspath(__file__))
2015-01-02 10:44:41 +00:00
2015-10-21 05:59:34 +00:00
def download_file(url, download_path):
return uget.download(url, download_path, console=sys.stdout)
2015-01-02 10:44:41 +00:00
2015-10-21 05:59:34 +00:00
def install_data(url, extract_path, download_path):
2015-10-20 17:11:29 +00:00
try:
2015-10-21 05:59:34 +00:00
os.makedirs(extract_path)
2015-10-20 17:11:29 +00:00
except FileExistsError:
pass
2015-10-20 17:33:59 +00:00
tmp = download_file(url, download_path)
assert tmp == download_path
t = tarfile.open(download_path)
2015-10-21 05:59:34 +00: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 17:11:29 +00:00
filename = ALL_DATA_DIR_URL.rsplit('/', 1)[1]
2015-10-21 05:59:34 +00: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 17:11:29 +00:00
2015-10-21 05:59:34 +00:00
if os.path.exists(data_path):
print('data already installed at %s, overwrite with --force' % DEST_DIR)
sys.exit(1)
2015-10-21 05:59:34 +00:00
install_data(ALL_DATA_DIR_URL, DEST_DIR, download_path)
2015-01-02 10:44:41 +00:00
if __name__ == '__main__':
2015-01-31 02:51:56 +00:00
plac.call(main)