pelican/pelican/tools/pelican_quickstart.py
Justin Mayer 72421d4438 Support arbitrary SSH ports in pelican-quickstart
Some folks choose non-standard SSH ports for security reasons, so it
makes sense to try to support that in the pelican-quickstart script.
2012-07-05 11:34:45 -07:00

200 lines
6.7 KiB
Python
Executable file

#!/usr/bin/env python
# -*- coding: utf-8 -*- #
import os
import string
import argparse
from pelican import __version__
_TEMPLATES_DIR = os.path.join(os.path.dirname(os.path.abspath(__file__)), \
"templates")
CONF = {
'pelican' : 'pelican',
'pelicanopts' : '',
'basedir': '.',
'ftp_host': 'localhost',
'ftp_user': 'anonymous',
'ftp_target_dir': '/',
'ssh_host': 'locahost',
'ssh_port': 22,
'ssh_user': 'root',
'ssh_target_dir': '/var/www',
'dropbox_dir' : '~/Dropbox/Public/',
'default_pagination' : 10,
'lang': 'en'
}
def get_template(name):
template = os.path.join(_TEMPLATES_DIR, "{0}.in".format(name))
if not os.path.isfile(template):
raise RuntimeError("Cannot open {0}".format(template))
with open(template, 'r') as fd:
line = fd.readline()
while line:
yield line
line = fd.readline()
fd.close()
def ask(question, answer=str, default=None, l=None):
if answer == str:
r = ''
while True:
if default:
r = raw_input('> {0} [{1}] '.format(question, default))
else:
r = raw_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 l and len(r) != l:
print('You must enter a {0} letters long string'.format(l))
else:
break
return r
elif answer == bool:
r = None
while True:
if default is True:
r = raw_input('> {0} (Y/n) '.format(question))
elif default is False:
r = raw_input('> {0} (y/N) '.format(question))
else:
r = raw_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 = raw_input('> {0} [{1}] '.format(question, default))
else:
r = raw_input('> {0} '.format(question))
r = r.strip()
if not r:
r = default
break
try:
r = int(r)
break
except:
print('You must enter an integer')
return r
else:
raise NotImplemented('Arguent `answer` must be str, bool or integer')
def main():
parser = argparse.ArgumentParser(
description="A kickstarter for pelican",
formatter_class=argparse.ArgumentDefaultsHelpFormatter)
parser.add_argument('-p', '--path', default=".",
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 lang of the website')
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__))
CONF['basedir'] = os.path.abspath(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)
CONF['with_pagination'] = ask('Do you want to enable article pagination ?', bool, bool(CONF['default_pagination']))
if CONF['with_pagination']:
CONF['default_pagination'] = ask('So how many articles per page do you want ?', int, CONF['default_pagination'])
else:
CONF['default_pagination'] = False
mkfile = ask('Do you want to generate a Makefile to easily manage your website ?', bool, True)
if mkfile:
if ask('Do you want to upload your website using FTP ?', answer=bool, default=False):
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 this server ?', str, CONF['ftp_user'])
CONF['ftp_target_dir'] = ask('Where do you want to put your website on this server ?', str, CONF['ftp_target_dir'])
if ask('Do you want to upload your website using SSH ?', answer=bool, default=False):
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 this server ?', str, CONF['ssh_user'])
CONF['ssh_target_dir'] = ask('Where do you want to put your website on this server ?', str, CONF['ssh_target_dir'])
if ask('Do you want to upload your website using Dropbox ?', answer=bool, default=False):
CONF['dropbox_dir'] = ask('Where is your Dropbox directory ?', str, CONF['dropbox_dir'])
try:
os.makedirs(os.path.join(CONF['basedir'], 'src'))
except OSError, e:
print('Error: {0}'.format(e))
try:
os.makedirs(os.path.join(CONF['basedir'], 'output'))
except OSError, e:
print('Error: {0}'.format(e))
try:
with open(os.path.join(CONF['basedir'], 'pelican.conf.py'), 'w') as fd:
for line in get_template('pelican.conf.py'):
template = string.Template(line)
fd.write(template.safe_substitute(CONF))
fd.close()
except OSError, e:
print('Error: {0}'.format(e))
if mkfile:
try:
with open(os.path.join(CONF['basedir'], 'Makefile'), 'w') as fd:
for line in get_template('Makefile'):
template = string.Template(line)
fd.write(template.safe_substitute(CONF))
fd.close()
except OSError, e:
print('Error: {0}'.format(e))
print('Done. Your new project is available at %s' % CONF['basedir'])