summaryrefslogtreecommitdiff
path: root/pelican/tools/pelican_quickstart.py
blob: 2e0e239985736ce72f405b28bdfb20e55d15f810 (plain)
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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
#!/usr/bin/env python
# -*- coding: utf-8 -*-

import argparse
import locale
import os

from jinja2 import Environment, FileSystemLoader

import pytz

try:
    import readline  # NOQA
except ImportError:
    pass

try:
    import tzlocal
    _DEFAULT_TIMEZONE = tzlocal.get_localzone().zone
except ImportError:
    _DEFAULT_TIMEZONE = 'Europe/Paris'

from pelican import __version__

locale.setlocale(locale.LC_ALL, '')
try:
    _DEFAULT_LANGUAGE = locale.getlocale()[0]
except ValueError:
    # Don't fail on macosx: "unknown locale: UTF-8"
    _DEFAULT_LANGUAGE = None
if _DEFAULT_LANGUAGE is None:
    _DEFAULT_LANGUAGE = 'en'
else:
    _DEFAULT_LANGUAGE = _DEFAULT_LANGUAGE.split('_')[0]

_TEMPLATES_DIR = os.path.join(os.path.dirname(os.path.abspath(__file__)),
                              "templates")
_jinja_env = Environment(
    loader=FileSystemLoader(_TEMPLATES_DIR),
    trim_blocks=True,
)


_GITHUB_PAGES_BRANCHES = {
    'personal': 'master',
    'project': 'gh-pages'
}

CONF = {
    'pelican': 'pelican',
    'pelicanopts': '',
    'basedir': os.curdir,
    'ftp_host': 'localhost',
    'ftp_user': 'anonymous',
    'ftp_target_dir': '/',
    'ssh_host': 'localhost',
    'ssh_port': 22,
    'ssh_user': 'root',
    'ssh_target_dir': '/var/www',
    's3_bucket': 'my_s3_bucket',
    'cloudfiles_username': 'my_rackspace_username',
    'cloudfiles_api_key': 'my_rackspace_api_key',
    'cloudfiles_container': 'my_cloudfiles_container',
    'dropbox_dir': '~/Dropbox/Public/',
    'github_pages_branch': _GITHUB_PAGES_BRANCHES['project'],
    'default_pagination': 10,
    'siteurl': '',
    'lang': _DEFAULT_LANGUAGE,
    'timezone': _DEFAULT_TIMEZONE
}

# url for list of valid timezones
_TZ_URL = 'https://en.wikipedia.org/wiki/List_of_tz_database_time_zones'


# Create a 'marked' default path, to determine if someone has supplied
# a path on the command-line.
class _DEFAULT_PATH_TYPE(str):
    is_default_path = True


_DEFAULT_PATH = _DEFAULT_PATH_TYPE(os.curdir)


def ask(question, answer=str, default=None, length=None):
    if answer == str:
        r = ''
        while True:
            if default:
                r = input('> {0} [{1}] '.format(question, default))
            else:
                r = input('> {0} '.format(question, default))

            r = r.strip()

            if len(r) <= 0:
                if default:
                    r = default
                    break
                else:
                    print('You must enter something')
            else:
                if length and len(r) != length:
                    print('Entry must be {0} characters long'.format(length))
                else:
                    break

        return r

    elif answer == bool:
        r = None
        while True:
            if default is True:
                r = input('> {0} (Y/n) '.format(question))
            elif default is False:
                r = input('> {0} (y/N) '.format(question))
            else:
                r = input('> {0} (y/n) '.format(question))

            r = r.strip().lower()

            if r in ('y', 'yes'):
                r = True
                break
            elif r in ('n', 'no'):
                r = False
                break
            elif not r:
                r = default
                break
            else:
                print("You must answer 'yes' or 'no'")
        return r
    elif answer == int:
        r = None
        while True:
            if default:
                r = input('> {0} [{1}] '.format(question, default))
            else:
                r = input('> {0} '.format(question))

            r = r.strip()

            if not r:
                r = default
                break

            try:
                r = int(r)
                break
            except ValueError:
                print('You must enter an integer')
        return r
    else:
        raise NotImplementedError(
            'Argument `answer` must be str, bool, or integer')


def ask_timezone(question, default, tzurl):
    """Prompt for time zone and validate input"""
    lower_tz = [tz.lower() for tz in pytz.all_timezones]
    while True:
        r = ask(question, str, default)
        r = r.strip().replace(' ', '_').lower()
        if r in lower_tz:
            r = pytz.all_timezones[lower_tz.index(r)]
            break
        else:
            print('Please enter a valid time zone:\n'
                  ' (check [{0}])'.format(tzurl))
    return r


def main():
    parser = argparse.ArgumentParser(
        description="A kickstarter for Pelican",
        formatter_class=argparse.ArgumentDefaultsHelpFormatter)
    parser.add_argument('-p', '--path', default=_DEFAULT_PATH,
                        help="The path to generate the blog into")
    parser.add_argument('-t', '--title', metavar="title",
                        help='Set the title of the website')
    parser.add_argument('-a', '--author', metavar="author",
                        help='Set the author name of the website')
    parser.add_argument('-l', '--lang', metavar="lang",
                        help='Set the default web site language')

    args = parser.parse_args()

    print('''Welcome to pelican-quickstart v{v}.

This script will help you create a new Pelican-based website.

Please answer the following questions so this script can generate the files
needed by Pelican.

    '''.format(v=__version__))

    project = os.path.join(
        os.environ.get('VIRTUAL_ENV', os.curdir), '.project')
    no_path_was_specified = hasattr(args.path, 'is_default_path')
    if os.path.isfile(project) and no_path_was_specified:
        CONF['basedir'] = open(project, 'r').read().rstrip("\n")
        print('Using project associated with current virtual environment. '
              'Will save to:\n%s\n' % CONF['basedir'])
    else:
        CONF['basedir'] = os.path.abspath(os.path.expanduser(
            ask('Where do you want to create your new web site?',
                answer=str, default=args.path)))

    CONF['sitename'] = ask('What will be the title of this web site?',
                           answer=str, default=args.title)
    CONF['author'] = ask('Who will be the author of this web site?',
                         answer=str, default=args.author)
    CONF['lang'] = ask('What will be the default language of this web site?',
                       str, args.lang or CONF['lang'], 2)

    if ask('Do you want to specify a URL prefix? e.g., https://example.com  ',
           answer=bool, default=True):
        CONF['siteurl'] = ask('What is your URL prefix? (see '
                              'above example; no trailing slash)',
                              str, CONF['siteurl'])

    CONF['with_pagination'] = ask('Do you want to enable article pagination?',
                                  bool, bool(CONF['default_pagination']))

    if CONF['with_pagination']:
        CONF['default_pagination'] = ask('How many articles per page '
                                         'do you want?',
                                         int, CONF['default_pagination'])
    else:
        CONF['default_pagination'] = False

    CONF['timezone'] = ask_timezone('What is your time zone?',
                                    CONF['timezone'], _TZ_URL)

    automation = ask('Do you want to generate a tasks.py/Makefile '
                     'to automate generation and publishing?', bool, True)

    if automation:
        if ask('Do you want to upload your website using FTP?',
               answer=bool, default=False):
            CONF['ftp'] = True,
            CONF['ftp_host'] = ask('What is the hostname of your FTP server?',
                                   str, CONF['ftp_host'])
            CONF['ftp_user'] = ask('What is your username on that server?',
                                   str, CONF['ftp_user'])
            CONF['ftp_target_dir'] = ask('Where do you want to put your '
                                         'web site on that server?',
                                         str, CONF['ftp_target_dir'])
        if ask('Do you want to upload your website using SSH?',
               answer=bool, default=False):
            CONF['ssh'] = True,
            CONF['ssh_host'] = ask('What is the hostname of your SSH server?',
                                   str, CONF['ssh_host'])
            CONF['ssh_port'] = ask('What is the port of your SSH server?',
                                   int, CONF['ssh_port'])
            CONF['ssh_user'] = ask('What is your username on that server?',
                                   str, CONF['ssh_user'])
            CONF['ssh_target_dir'] = ask('Where do you want to put your '
                                         'web site on that server?',
                                         str, CONF['ssh_target_dir'])

        if ask('Do you want to upload your website using Dropbox?',
               answer=bool, default=False):
            CONF['dropbox'] = True,
            CONF['dropbox_dir'] = ask('Where is your Dropbox directory?',
                                      str, CONF['dropbox_dir'])

        if ask('Do you want to upload your website using S3?',
               answer=bool, default=False):
            CONF['s3'] = True,
            CONF['s3_bucket'] = ask('What is the name of your S3 bucket?',
                                    str, CONF['s3_bucket'])

        if ask('Do you want to upload your website using '
               'Rackspace Cloud Files?', answer=bool, default=False):
            CONF['cloudfiles'] = True,
            CONF['cloudfiles_username'] = ask('What is your Rackspace '
                                              'Cloud username?', str,
                                              CONF['cloudfiles_username'])
            CONF['cloudfiles_api_key'] = ask('What is your Rackspace '
                                             'Cloud API key?', str,
                                             CONF['cloudfiles_api_key'])
            CONF['cloudfiles_container'] = ask('What is the name of your '
                                               'Cloud Files container?',
                                               str,
                                               CONF['cloudfiles_container'])

        if ask('Do you want to upload your website using GitHub Pages?',
               answer=bool, default=False):
            CONF['github'] = True,
            if ask('Is this your personal page (username.github.io)?',
                   answer=bool, default=False):
                CONF['github_pages_branch'] = \
                    _GITHUB_PAGES_BRANCHES['personal']
            else:
                CONF['github_pages_branch'] = \
                    _GITHUB_PAGES_BRANCHES['project']

    try:
        os.makedirs(os.path.join(CONF['basedir'], 'content'))
    except OSError as e:
        print('Error: {0}'.format(e))

    try:
        os.makedirs(os.path.join(CONF['basedir'], 'output'))
    except OSError as e:
        print('Error: {0}'.format(e))

    try:
        with open(os.path.join(CONF['basedir'], 'pelicanconf.py'),
                  'w', encoding='utf-8') as fd:
            conf_python = dict()
            for key, value in CONF.items():
                conf_python[key] = repr(value)

            _template = _jinja_env.get_template('pelicanconf.py.jinja2')
            fd.write(_template.render(**conf_python))
            fd.close()
    except OSError as e:
        print('Error: {0}'.format(e))

    try:
        with open(os.path.join(CONF['basedir'], 'publishconf.py'),
                  'w', encoding='utf-8') as fd:
            _template = _jinja_env.get_template('publishconf.py.jinja2')
            fd.write(_template.render(**CONF))
            fd.close()
    except OSError as e:
        print('Error: {0}'.format(e))

    if automation:
        try:
            with open(os.path.join(CONF['basedir'], 'tasks.py'),
                      'w', encoding='utf-8') as fd:
                _template = _jinja_env.get_template('tasks.py.jinja2')
                fd.write(_template.render(**CONF))
                fd.close()
        except OSError as e:
            print('Error: {0}'.format(e))
        try:
            with open(os.path.join(CONF['basedir'], 'Makefile'),
                      'w', encoding='utf-8') as fd:
                py_v = 'python3'
                _template = _jinja_env.get_template('Makefile.jinja2')
                fd.write(_template.render(py_v=py_v, **CONF))
                fd.close()
        except OSError as e:
            print('Error: {0}'.format(e))

    print('Done. Your new project is available at %s' % CONF['basedir'])


if __name__ == "__main__":
    main()