summaryrefslogtreecommitdiff
path: root/raw_post
blob: f26765ca9a95c3d9da505078a2cb656af6c6ead4 (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
#!/usr/bin/env python
"""
 bloggit
 Author : Guillaume "iXce" Seguin
 Email  : guillaume@segu.in (or guillaume.seguin@ens.fr)

 Copyright (C) 2010 Guillaume Seguin

 This program is free software; you can redistribute it and/or
 modify it under the terms of the GNU General Public License
 as published by the Free Software Foundation; either version 2
 of the License, or (at your option) any later version.

 This program is distributed in the hope that it will be useful,
 but WITHOUT ANY WARRANTY; without even the implied warranty of
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 GNU General Public License for more details.

 You should have received a copy of the GNU General Public License
 along with this program; if not, write to the Free Software
 Foundation, Inc., 51 Franklin Street, Fifth Floor,
 Boston, MA  02110-1301, USA.
"""
import subprocess, os, sys
import base64, bz2

CONTENTS_FILE = "POST_CONTENTS"

repo = os.path.dirname (os.path.abspath (__file__))

os.chdir (repo)

if not os.path.exists (os.path.join (repo, ".git")):
    subprocess.call (["git", "init"])

produce_path = os.path.join (repo, "produce")
produce_script = bz2.decompress (base64.b64decode ("""
PRODUCE_SCRIPT
""".strip ().replace ("\n", "")))

if not os.path.exists (produce_path):
    open (produce_path, "w").write (produce_script)
    os.chmod (produce_path, 0755)

for hook in ("post-receive", "post-commit"):
    hook_path = os.path.join (repo, ".git", "hooks", hook)
    if not os.path.exists (hook_path) \
     or os.path.realpath (hook_path) != produce_path:
        if os.path.lexists (hook_path): os.remove (hook_path)
        os.symlink (produce_path, hook_path)

if "--unpack" in sys.argv:
    print "Unpack only"
    raise SystemExit

if "--raw" in sys.argv:
    p = subprocess.Popen (["git", "commit", "--allow-empty"])
    p.communicate ()
else:
    title = raw_input ("Post title ? ")
    editor = "vi"
    if "VISUAL" in os.environ:
        editor = os.environ["VISUAL"]
    elif "EDITOR" in os.environ:
        editor = os.environ["EDITOR"]

    contents_path = os.path.join (repo, CONTENTS_FILE)
    if os.path.exists (contents_path):
        os.remove (contents_path)
      
    ret = subprocess.call ([editor, contents_path])
    if ret == 0 and os.path.exists (contents_path):
        contents = open (contents_path).read ()
        os.remove (contents_path)
    else:
        print "A problem occured while editing the contents file"
        raise SystemExit

    p = subprocess.Popen (["git", "commit", "--allow-empty", "-F", "-"],
                          stdin = subprocess.PIPE)
    p.communicate ("%s\n\n%s" % (title, contents))

if p.returncode == 0: print "New post successfully added"
else:
    print "Couldn't add new post"
    sys.exit (1)