summaryrefslogtreecommitdiff
path: root/raw_post
blob: 641a1a418fd413c2a6276699b01049513080cf23 (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
#!/usr/bin/env python

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

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)
  
ret = subprocess.call ([editor, contents_path])
if ret == 0:
  contents = open (contents_path).read ()
  os.remove (contents_path)
else:
  print "A problem occured while editing the contents file"
  raise SystemExit

token_path = os.path.join (repo, "token")
token = int (open (token_path).read ()) if os.path.exists (token_path) else 0
open (token_path, "w").write (str (token + 1))

subprocess.call (["git", "add", token_path])

p = subprocess.Popen (["git", "commit", "-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)