2017-03-20 21:50:13 +00:00
|
|
|
# coding: utf8
|
|
|
|
from __future__ import unicode_literals
|
|
|
|
|
|
|
|
import json
|
2017-03-21 01:06:29 +00:00
|
|
|
import shutil
|
2017-03-21 10:17:36 +00:00
|
|
|
import requests
|
2017-03-20 21:50:13 +00:00
|
|
|
from pathlib import Path
|
|
|
|
|
|
|
|
from .. import about
|
|
|
|
from .. import util
|
|
|
|
|
|
|
|
|
2017-03-21 01:05:34 +00:00
|
|
|
def package(input_dir, output_dir, force):
|
2017-03-20 21:50:13 +00:00
|
|
|
input_path = Path(input_dir)
|
|
|
|
output_path = Path(output_dir)
|
|
|
|
check_dirs(input_path, output_path)
|
|
|
|
|
2017-03-21 10:17:36 +00:00
|
|
|
template_setup = get_template('setup.py')
|
|
|
|
template_init = get_template('en_model_name/__init__.py')
|
|
|
|
template_manifest = 'include meta.json'
|
2017-03-20 21:50:13 +00:00
|
|
|
meta = generate_meta()
|
2017-03-21 10:17:36 +00:00
|
|
|
|
2017-03-20 21:50:13 +00: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 01:06:53 +00:00
|
|
|
create_dirs(package_path, force)
|
2017-03-21 01:06:29 +00:00
|
|
|
shutil.copytree(input_path, package_path / model_name_v)
|
2017-03-20 21:50:13 +00:00
|
|
|
create_file(main_path / 'meta.json', json.dumps(meta, indent=2))
|
2017-03-21 10:17:36 +00: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-20 21:50:13 +00:00
|
|
|
|
|
|
|
util.print_msg(
|
|
|
|
main_path.as_posix(),
|
2017-03-21 10:17:36 +00:00
|
|
|
"To build the package, run `python setup.py sdist` in that directory.",
|
2017-03-21 01:06:37 +00:00
|
|
|
title="Successfully created package {p}".format(p=model_name_v))
|
2017-03-20 21:50:13 +00:00
|
|
|
|
|
|
|
|
|
|
|
def check_dirs(input_path, output_path):
|
|
|
|
if not input_path.exists():
|
|
|
|
util.sys_exit(input_path.as_poisx(), title="Model directory not found")
|
|
|
|
if not output_path.exists():
|
|
|
|
util.sys_exit(output_path.as_posix(), title="Output directory not found")
|
|
|
|
|
|
|
|
|
2017-03-21 01:06:53 +00:00
|
|
|
def create_dirs(package_path, force):
|
|
|
|
if package_path.exists():
|
|
|
|
if force:
|
|
|
|
shutil.rmtree(package_path)
|
|
|
|
else:
|
|
|
|
util.sys_exit(package_path.as_posix(),
|
|
|
|
"Please delete the directory and try again.",
|
|
|
|
title="Package directory already exists")
|
|
|
|
Path.mkdir(package_path, parents=True)
|
|
|
|
|
|
|
|
|
2017-03-20 21:50:13 +00:00
|
|
|
def create_file(file_path, contents):
|
|
|
|
file_path.touch()
|
|
|
|
file_path.write_text(contents, encoding='utf-8')
|
|
|
|
|
|
|
|
|
|
|
|
def generate_meta():
|
|
|
|
settings = [('lang', 'Model language', 'en'),
|
|
|
|
('name', 'Model name', 'model'),
|
|
|
|
('version', 'Model version', '0.0.0'),
|
2017-03-21 10:17:36 +00:00
|
|
|
('spacy_version', 'Required spaCy version', '>=1.7.0,<2.0.0'),
|
2017-03-20 21:50:13 +00:00
|
|
|
('description', 'Model description', False),
|
|
|
|
('author', 'Author', False),
|
|
|
|
('email', 'Author email', False),
|
|
|
|
('url', 'Author website', False),
|
|
|
|
('license', 'License', 'MIT')]
|
|
|
|
|
|
|
|
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-03-21 10:17:36 +00: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
|