Update command and fetch file templates from GitHub

While feature is still experimental, this allows files to be modified
without having to ship a new version of spaCy.
This commit is contained in:
ines 2017-03-21 11:17:36 +01:00
parent 5230ed5b98
commit 3f4e3fda1d

View File

@ -3,6 +3,7 @@ from __future__ import unicode_literals
import json import json
import shutil import shutil
import requests
from pathlib import Path from pathlib import Path
from .. import about from .. import about
@ -14,7 +15,11 @@ def package(input_dir, output_dir, force):
output_path = Path(output_dir) output_path = Path(output_dir)
check_dirs(input_path, output_path) check_dirs(input_path, output_path)
template_setup = get_template('setup.py')
template_init = get_template('en_model_name/__init__.py')
template_manifest = 'include meta.json'
meta = generate_meta() meta = generate_meta()
model_name = meta['lang'] + '_' + meta['name'] model_name = meta['lang'] + '_' + meta['name']
model_name_v = model_name + '-' + meta['version'] model_name_v = model_name + '-' + meta['version']
main_path = output_path / model_name_v main_path = output_path / model_name_v
@ -23,13 +28,13 @@ def package(input_dir, output_dir, force):
create_dirs(package_path, force) create_dirs(package_path, force)
shutil.copytree(input_path, package_path / model_name_v) shutil.copytree(input_path, package_path / model_name_v)
create_file(main_path / 'meta.json', json.dumps(meta, indent=2)) create_file(main_path / 'meta.json', json.dumps(meta, indent=2))
create_file(main_path / 'setup.py', TEMPLATE_SETUP.strip()) create_file(main_path / 'setup.py', template_setup)
create_file(main_path / 'MANIFEST.in', TEMPLATE_MANIFEST.strip()) create_file(main_path / 'MANIFEST.in', template_manifest)
create_file(package_path / '__init__.py', TEMPLATE_INIT.strip()) create_file(package_path / '__init__.py', template_init)
util.print_msg( util.print_msg(
main_path.as_posix(), main_path.as_posix(),
"To build the package, run python setup.py sdist in that directory.", "To build the package, run `python setup.py sdist` in that directory.",
title="Successfully created package {p}".format(p=model_name_v)) title="Successfully created package {p}".format(p=model_name_v))
@ -60,7 +65,7 @@ def generate_meta():
settings = [('lang', 'Model language', 'en'), settings = [('lang', 'Model language', 'en'),
('name', 'Model name', 'model'), ('name', 'Model name', 'model'),
('version', 'Model version', '0.0.0'), ('version', 'Model version', '0.0.0'),
('spacy_version', 'Required spaCy version', '>=2.0.0,<3.0.0'), ('spacy_version', 'Required spaCy version', '>=1.7.0,<2.0.0'),
('description', 'Model description', False), ('description', 'Model description', False),
('author', 'Author', False), ('author', 'Author', False),
('email', 'Author email', False), ('email', 'Author email', False),
@ -76,85 +81,11 @@ def generate_meta():
return meta return meta
TEMPLATE_MANIFEST = """ def get_template(filepath):
include meta.json url = 'https://raw.githubusercontent.com/explosion/spacy-dev-resources/master/templates/model/'
""" r = requests.get(url + filepath)
if r.status_code != 200:
util.sys_exit(
TEMPLATE_SETUP = """ "Couldn't fetch template files from GitHub.",
#!/usr/bin/env python title="Server error ({c})".format(c=r.status_code))
# coding: utf8 return r.text
from __future__ import unicode_literals
import io
import json
from os import path, walk
from shutil import copy
from setuptools import setup
def load_meta(fp):
with io.open(fp, encoding='utf8') as f:
return json.load(f)
def list_files(data_dir):
output = []
for root, _, filenames in walk(data_dir):
for filename in filenames:
if not filename.startswith('.'):
output.append(path.join(root, filename))
output = [path.relpath(p, path.dirname(data_dir)) for p in output]
output.append('meta.json')
return output
def setup_package():
root = path.abspath(path.dirname(__file__))
meta_path = path.join(root, 'meta.json')
meta = load_meta(meta_path)
model_name = str(meta['lang'] + '_' + meta['name'])
model_dir = path.join(model_name, model_name + '-' + meta['version'])
copy(meta_path, path.join(root, model_name))
copy(meta_path, path.join(root, model_dir))
setup(
name=model_name,
description=meta['description'],
author=meta['author'],
author_email=meta['email'],
url=meta['url'],
version=meta['version'],
license=meta['license'],
packages=[model_name],
package_data={model_name: list_files(model_dir)},
install_requires=['spacy' + meta['spacy_version']],
zip_safe=False,
)
if __name__ == '__main__':
setup_package()
"""
TEMPLATE_INIT = """
from pathlib import Path
from spacy.util import get_lang_class
import pkg_resources
import json
def load_meta():
with (Path(__file__).parent / 'meta.json').open() as f:
return json.load(f)
def load(**kwargs):
meta = load_meta()
version = meta['version']
data_dir = pkg_resources.resource_filename(__name__, __name__ + '-' + version)
lang = get_lang_class(meta['lang'])
return lang(path=Path(data_dir), **kwargs)
"""