gitoyen.py 4.25 KB
Newer Older
1
#! /usr/bin/env python
Maxime Vidori's avatar
Maxime Vidori committed
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
# coding: utf8

import click

import livereload
import jinja2
import path
import pelican.utils as utils

import datetime
import mimetypes
import subprocess

import SimpleHTTPServer
import SocketServer

PELICAN_CONF = 'pelicanconf.py'
OUTPUT = 'output'
CONTENT = 'content'
BLOG = 'content/blog'
22
THEME = './theme'
Maxime Vidori's avatar
Maxime Vidori committed
23
24
25

class Config(object):
    def __init__(self):
Julien Rabier's avatar
Julien Rabier committed
26
27
28
29
30
        self.settings = path.Path(PELICAN_CONF)
        self.output = path.Path(OUTPUT)
        self.content = path.Path(CONTENT)
        self.blog = path.Path(BLOG)
        self.theme = path.Path(THEME)
Maxime Vidori's avatar
Maxime Vidori committed
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
        self.jinja = jinja2.Environment(
            loader=jinja2.PackageLoader('gitoyen', 'templates')
        )


class Application(object):

    def __init__(self, config):
        self.config = config
        self.chunk_size = 64 * 1024

    def _not_found(self, sr):
        sr('404 NOT FOUND', [('Content-Type', 'text/plain')])
        return ['Not Found']

    def _serve_file(self, f, sr):
47
48
        mime, _ = mimetypes.guess_type(f)
        sr('200 OK', [('Content-Type', mime or 'application/octet-stream')])
Maxime Vidori's avatar
Maxime Vidori committed
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
        return f.chunks(self.chunk_size, 'rb')

    def _list_dir(self, d, sr):
        sr('200 OK', [('Content-Type', 'text/html')])

        context = {
            'directory': d.relpath(self.config.output),
            'links': [f.relpath(d) for f in d.listdir()]
        }

        return (self.config.jinja.get_template('list_dir.tplt')
                .stream(**context))

    def __call__(self, env, start_response):
        path = self.config.output + env.get('PATH_INFO')
        path_index = path + 'index.html'

        if not path.exists():
            return self._not_found(start_response)
        if path.isfile():
            return self._serve_file(path, start_response)
        if path_index.exists():
            return self._serve_file(path_index, start_response)

        return self._list_dir(path, start_response)


pass_config = click.make_pass_decorator(Config, ensure=True)


@click.group()
def cli():
    pass


@cli.command()
@pass_config
def clean(config):
    '''Clean the "output" directory'''
    click.echo('Clean output directory')
    for dir in config.output.dirs():
        dir.rmtree()
    for file in config.output.files():
        file.remove()


@cli.command()
@pass_config
def build(config):
    '''Build the pelican static site'''
    subprocess.call(['pelican', '-s', config.settings])


@cli.command()
@click.pass_context
def rebuild(ctx):
    '''Run clean then build'''
    ctx.invoke(clean)
    ctx.invoke(build)


@cli.command()
@click.option('--no-debug', is_flag=True, help='remove the debug output')
@click.option('--no-lr', is_flag=True, help='remove the livereload support')
@click.argument('port', default=8000, required=False)
@pass_config
@click.pass_context
def serve(ctx, config, no_debug, no_lr, port):
    '''Serve the 'output' directory, default port is 8000'''
    if port < 1024 or port > 65535:
        error_msg = 'port must be an integer between 1024 and 65535'
        raise click.BadParameter(error_msg, param_hint='port')

    debug = not no_debug
    inner_build = lambda: ctx.invoke(build)

    inner_build()
    if no_lr:
        with config.output:
            Handler = SimpleHTTPServer.SimpleHTTPRequestHandler
            httpd = SocketServer.TCPServer(('', port), Handler)

            click.echo('serving at port %d' % port)
            httpd.serve_forever()
    else:
        server = livereload.Server(Application(config))
        server.watch(config.content, inner_build)
        server.watch(config.theme, inner_build)
        server.serve(port=port, debug=debug)


@cli.command()
@click.argument('title')
@pass_config
def new_post(config, title):
    '''Create a new blog entry'''
    date = datetime.date.today().isoformat()
    filename = '.'.join([date, utils.slugify(title), 'md'])
    filename = config.blog / filename
    click.echo('Create new post: %s' % click.style(filename, fg='green'))

    (config.jinja.get_template('new_post.tplt')
     .stream(title=title)
     .dump(filename, 'utf8'))


@cli.command()
@pass_config
@click.pass_context
def publish(ctx, config):
    ctx.invoke(build)
    subprocess.call(['ghp-import', config.output])
    subprocess.call(['git', 'push', 'site', '-f', 'gh-pages:master'])


if __name__ == '__main__':
    cli()