2017-03-21 00:50:13 +03:00
|
|
|
# coding: utf8
|
|
|
|
from __future__ import unicode_literals
|
|
|
|
|
2017-03-21 04:06:29 +03:00
|
|
|
import shutil
|
2017-03-21 13:17:36 +03:00
|
|
|
import requests
|
2017-03-21 00:50:13 +03:00
|
|
|
from pathlib import Path
|
|
|
|
|
2017-04-15 13:11:16 +03:00
|
|
|
from ..compat import unicode_, json_dumps
|
2017-03-21 00:50:13 +03:00
|
|
|
from .. import util
|
|
|
|
|
|
|
|
|
2017-04-16 14:06:02 +03:00
|
|
|
def package(input_dir, output_dir, meta_path, force):
|
2017-03-21 00:50:13 +03:00
|
|
|
input_path = Path(input_dir)
|
|
|
|
output_path = Path(output_dir)
|
2017-04-16 14:06:02 +03:00
|
|
|
meta_path = util.ensure_path(meta_path)
|
|
|
|
check_dirs(input_path, output_path, meta_path)
|
2017-03-21 00:50:13 +03:00
|
|
|
|
2017-03-21 13:17:36 +03:00
|
|
|
template_setup = get_template('setup.py')
|
2017-03-21 13:32:38 +03:00
|
|
|
template_manifest = get_template('MANIFEST.in')
|
2017-03-21 13:17:36 +03:00
|
|
|
template_init = get_template('en_model_name/__init__.py')
|
2017-04-16 14:06:02 +03:00
|
|
|
|
|
|
|
meta_path = meta_path or input_path / 'meta.json'
|
|
|
|
if meta_path.is_file():
|
|
|
|
util.print_msg(unicode_(meta_path), title="Reading meta.json from file")
|
|
|
|
meta = util.read_json(meta_path)
|
|
|
|
else:
|
|
|
|
meta = generate_meta()
|
2017-03-21 13:17:36 +03:00
|
|
|
|
2017-04-16 14:13:17 +03:00
|
|
|
validate_meta(meta, ['lang', 'name', 'version'])
|
2017-03-21 00:50:13 +03:00
|
|
|
model_name = meta['lang'] + '_' + meta['name']
|
|
|
|
model_name_v = model_name + '-' + meta['version']
|
|
|
|
main_path = output_path / model_name_v
|
|
|
|
package_path = main_path / model_name
|
|
|
|
|
2017-03-21 04:06:53 +03:00
|
|
|
create_dirs(package_path, force)
|
2017-04-15 13:11:16 +03:00
|
|
|
shutil.copytree(unicode_(input_path), unicode_(package_path / model_name_v))
|
2017-04-14 00:30:47 +03:00
|
|
|
create_file(main_path / 'meta.json', json_dumps(meta))
|
2017-03-21 13:17:36 +03:00
|
|
|
create_file(main_path / 'setup.py', template_setup)
|
|
|
|
create_file(main_path / 'MANIFEST.in', template_manifest)
|
|
|
|
create_file(package_path / '__init__.py', template_init)
|
2017-03-21 00:50:13 +03:00
|
|
|
|
|
|
|
util.print_msg(
|
2017-04-15 13:11:16 +03:00
|
|
|
unicode_(main_path),
|
2017-03-21 13:17:36 +03:00
|
|
|
"To build the package, run `python setup.py sdist` in that directory.",
|
2017-03-21 04:06:37 +03:00
|
|
|
title="Successfully created package {p}".format(p=model_name_v))
|
2017-03-21 00:50:13 +03:00
|
|
|
|
|
|
|
|
2017-04-16 14:06:02 +03:00
|
|
|
def check_dirs(input_path, output_path, meta_path):
|
2017-03-21 00:50:13 +03:00
|
|
|
if not input_path.exists():
|
2017-06-07 17:19:08 +03:00
|
|
|
util.sys_exit(unicode_(input_path.as_posix()), title="Model directory not found")
|
2017-03-21 00:50:13 +03:00
|
|
|
if not output_path.exists():
|
2017-04-15 13:11:16 +03:00
|
|
|
util.sys_exit(unicode_(output_path), title="Output directory not found")
|
2017-04-16 14:06:02 +03:00
|
|
|
if meta_path and not meta_path.exists():
|
|
|
|
util.sys_exit(unicode_(meta_path), title="meta.json not found")
|
2017-03-21 00:50:13 +03:00
|
|
|
|
|
|
|
|
2017-03-21 04:06:53 +03:00
|
|
|
def create_dirs(package_path, force):
|
|
|
|
if package_path.exists():
|
|
|
|
if force:
|
2017-04-16 14:03:58 +03:00
|
|
|
shutil.rmtree(unicode_(package_path))
|
2017-03-21 04:06:53 +03:00
|
|
|
else:
|
2017-04-16 14:03:58 +03:00
|
|
|
util.sys_exit(unicode_(package_path),
|
2017-04-16 14:14:36 +03:00
|
|
|
"Please delete the directory and try again, or use the --force "
|
|
|
|
"flag to overwrite existing directories.",
|
2017-03-21 04:06:53 +03:00
|
|
|
title="Package directory already exists")
|
|
|
|
Path.mkdir(package_path, parents=True)
|
|
|
|
|
|
|
|
|
2017-03-21 00:50:13 +03:00
|
|
|
def create_file(file_path, contents):
|
|
|
|
file_path.touch()
|
2017-03-29 10:11:02 +03:00
|
|
|
file_path.open('w', encoding='utf-8').write(contents)
|
2017-03-21 00:50:13 +03:00
|
|
|
|
|
|
|
|
|
|
|
def generate_meta():
|
|
|
|
settings = [('lang', 'Model language', 'en'),
|
|
|
|
('name', 'Model name', 'model'),
|
|
|
|
('version', 'Model version', '0.0.0'),
|
2017-03-21 13:17:36 +03:00
|
|
|
('spacy_version', 'Required spaCy version', '>=1.7.0,<2.0.0'),
|
2017-03-21 00:50:13 +03:00
|
|
|
('description', 'Model description', False),
|
|
|
|
('author', 'Author', False),
|
|
|
|
('email', 'Author email', False),
|
|
|
|
('url', 'Author website', False),
|
2017-03-21 14:24:43 +03:00
|
|
|
('license', 'License', 'CC BY-NC 3.0')]
|
2017-03-21 00:50:13 +03:00
|
|
|
|
|
|
|
util.print_msg("Enter the package settings for your model.", title="Generating meta.json")
|
|
|
|
|
|
|
|
meta = {}
|
|
|
|
for setting, desc, default in settings:
|
|
|
|
response = util.get_raw_input(desc, default)
|
|
|
|
meta[setting] = default if response == '' and default else response
|
|
|
|
return meta
|
|
|
|
|
|
|
|
|
2017-04-16 14:13:17 +03:00
|
|
|
def validate_meta(meta, keys):
|
|
|
|
for key in keys:
|
|
|
|
if key not in meta or meta[key] == '':
|
|
|
|
util.sys_exit(
|
|
|
|
"This setting is required to build your package.",
|
|
|
|
title='No "{k}" setting found in meta.json'.format(k=key))
|
|
|
|
|
|
|
|
|
2017-03-21 13:17:36 +03:00
|
|
|
def get_template(filepath):
|
|
|
|
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(
|
|
|
|
"Couldn't fetch template files from GitHub.",
|
|
|
|
title="Server error ({c})".format(c=r.status_code))
|
|
|
|
return r.text
|