spaCy/spacy/en/download.py

59 lines
1.7 KiB
Python
Raw Normal View History

from __future__ import print_function
import sys
2015-01-02 13:44:41 +03:00
import os
import shutil
2015-11-15 17:58:21 +03:00
import plac
2016-01-14 14:02:56 +03:00
import sputnik
2016-01-16 14:23:45 +03:00
from sputnik.package_list import (PackageNotFoundException,
CompatiblePackageNotFoundException)
2016-01-14 14:02:56 +03:00
from .. import about
2015-01-02 13:44:41 +03:00
2015-11-15 17:58:21 +03:00
def migrate(path):
data_path = os.path.join(path, 'data')
2015-12-18 11:49:45 +03:00
if os.path.isdir(data_path):
if os.path.islink(data_path):
os.unlink(data_path)
else:
shutil.rmtree(data_path)
2015-11-15 17:58:21 +03:00
for filename in os.listdir(path):
2015-11-18 19:35:21 +03:00
if filename.endswith('.tgz'):
2015-11-15 17:58:21 +03:00
os.unlink(os.path.join(path, filename))
@plac.annotations(
force=("Force overwrite", "flag", "f", bool),
)
2015-11-18 19:35:21 +03:00
def main(data_size='all', force=False):
2015-11-15 17:58:21 +03:00
if force:
2016-01-15 20:57:01 +03:00
sputnik.purge(about.__name__, about.__version__)
2015-11-15 17:58:21 +03:00
2016-01-16 14:23:45 +03:00
try:
sputnik.package(about.__name__, about.__version__, about.__default_model__)
print("Model already installed. Please run 'python -m "
"spacy.en.download --force' to reinstall.", file=sys.stderr)
sys.exit(1)
2016-01-16 14:44:53 +03:00
except (PackageNotFoundException, CompatiblePackageNotFoundException):
2016-01-16 14:23:45 +03:00
pass
2016-01-15 20:57:01 +03:00
package = sputnik.install(about.__name__, about.__version__, about.__default_model__)
try:
2016-01-15 20:57:01 +03:00
sputnik.package(about.__name__, about.__version__, about.__default_model__)
2016-01-16 14:44:53 +03:00
except (PackageNotFoundException, CompatiblePackageNotFoundException):
print("Model failed to install. Please run 'python -m "
"spacy.en.download --force'.", file=sys.stderr)
sys.exit(1)
2015-10-20 20:11:29 +03:00
2015-11-15 17:58:21 +03:00
# FIXME clean up old-style packages
2016-01-16 14:23:45 +03:00
migrate(os.path.dirname(os.path.abspath(__file__)))
print("Model successfully installed.", file=sys.stderr)
2015-01-02 13:44:41 +03:00
if __name__ == '__main__':
2015-01-31 05:51:56 +03:00
plac.call(main)