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')
|
2017-03-21 10:32:38 +00:00
|
|
|
template_manifest = get_template('MANIFEST.in')
|
2017-03-21 10:17:36 +00:00
|
|
|
template_init = get_template('en_model_name/__init__.py')
|
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 11:26:00 +00:00
|
|
|
shutil.copytree(input_path.as_posix(), (package_path / model_name_v).as_posix())
|
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:
|
2017-03-21 11:15:33 +00:00
|
|
|
shutil.rmtree(package_path.as_posix())
|
2017-03-21 01:06:53 +00:00
|
|
|
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()
|
2017-03-21 11:35:22 +00:00
|
|
|
file_path.open('w').write(contents, encoding='utf-8')
|
2017-03-20 21:50:13 +00:00
|
|
|
|
|
|
|
|
|
|
|
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),
|
2017-03-21 11:24:43 +00:00
|
|
|
('license', 'License', 'CC BY-NC 3.0')]
|
2017-03-20 21:50:13 +00: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-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
|