-
Notifications
You must be signed in to change notification settings - Fork 2k
/
generate.py
114 lines (96 loc) · 3.95 KB
/
generate.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
# encoding: utf-8
import os
import sys
import click
import uuid
import random
import string
from ckan.cli import error_shout
@click.group(
name=u'generate',
short_help=u"Generate empty extension files to expand CKAN.",
invoke_without_command=True,
)
def generate():
try:
from cookiecutter.main import cookiecutter
except ImportError:
error_shout(u"`cookiecutter` library is missing from import path.")
error_shout(u"Make sure you have dev-dependencies installed:")
error_shout(u"\tpip install -r dev-requirements.txt")
raise click.Abort()
@generate.command(name=u'extension', short_help=u"Create empty extension.")
@click.option(u'-o', u'--output-dir', help=u"Location to put the generated "
u"template.",
default=u'.')
def extension(output_dir):
from cookiecutter.main import cookiecutter
cur_loc = os.path.dirname(os.path.abspath(__file__))
os.chdir(cur_loc)
os.chdir(u'../../contrib/cookiecutter/ckan_extension/')
template_loc = os.getcwd()
# Prompt user for information
click.echo(u"\n")
name = click.prompt(u"Extenion's name", default=u"must begin 'ckanext-'")
author = click.prompt(u"Author's name", default=u"")
email = click.prompt(u"Author's email", default=u"")
github = click.prompt(u"Your Github user or organization name",
default=u"")
description = click.prompt(u"Brief description of the project",
default=u"")
keywords = click.prompt(u"List of keywords (seperated by spaces)",
default=u"CKAN")
# Ensure one instance of 'CKAN' in keywords
keywords = keywords.strip().split()
keywords = [keyword for keyword in keywords
if keyword not in (u'ckan', u'CKAN')]
keywords.insert(0, u'CKAN')
keywords = u' '.join(keywords)
# Set short name and plugin class name
project_short = name[8:].lower().replace(u'-', u'_')
plugin_class_name = project_short.title().replace(u'_', u'') + u'Plugin'
context = {u"project": name,
u"description": description,
u"author": author,
u"author_email": email,
u"keywords": keywords,
u"github_user_name": github,
u"project_shortname": project_short,
u"plugin_class_name": plugin_class_name,
u"_source": u"cli"}
if output_dir == u'.':
os.chdir(u'../../../..')
output_dir = os.getcwd()
if not name.startswith(u"ckanext-"):
print(u"\nERROR: Project name must start with 'ckanext-' > {}"
.format(name))
sys.exit(1)
cookiecutter(template_loc, no_input=True, extra_context=context,
output_dir=output_dir)
@generate.command(name=u'config', short_help=u'Create config from template.')
@click.option(u'-o', u'--output-dir', help=u'Location to put the generated '
u'template.', default=u'.')
def make_config(output_dir):
if not any(name in output_dir for name in
['development.ini', 'production.ini']):
print('\nERROR: File name must be development.ini or production.ini')
sys.exit(1)
cur_loc = os.path.dirname(os.path.abspath(__file__))
os.chdir(cur_loc)
os.chdir('../config')
template_loc = os.getcwd() + '/deployment.ini_tmpl'
template_variables = {
'app_instance_uuid': uuid.uuid4(),
'app_instance_secret': ''.join(
random.SystemRandom().choice(string.ascii_letters + string.digits)
for _ in range(25))
}
with open(template_loc, 'r') as file_in:
template = string.Template(file_in.read())
print(output_dir)
try:
with open(output_dir, 'w') as file_out:
file_out.writelines(template.substitute(template_variables))
except IOError as e:
error_shout(e)
sys.exit(1)