import os, sys
from git import *
import bottle
from bottle import default_app, route, run, request, template, static_file, redirect
from os.path import isdir
from string import lower, split
from urllib import unquote
import shutil
from datetime import datetime
import re
import Image
from markdown2 import markdown
conf = {}
conf['repo'] = '/srv/git/documents'
#conf['repo'] = '/srv/git/repo'
conf['ext_bundles'] = ['.pages', '.sparsebundle']
conf['ext_render'] = ['.md','.txt', '.jpg']
conf['ext_edit'] = ['.md','.txt','.rb','.py','.pl','.sh']
def sanitize_path(path):
return path.lstrip('./')
@route('/repo')
@route('/repo/')
@route('/repo/')
def public(name=''):
repo = Repo(conf['repo'])
repo.git.pull()
tree = []
filename, extension = os.path.splitext(name)
filename = os.path.basename(filename)
path = os.path.join(conf['repo'], sanitize_path(name))
if os.path.exists(path):
if isdir(path) and not lower(extension) in conf['ext_bundles']:
tree.append('
')
print "This is a regular dir: " + path
if not name == '':
n = name.rstrip('/')
l = split(n, '/')
tree.append('
')
if len(l) > 1:
tree.append('..')
else:
tree.append('..')
tree.append('
')
dirs = []
files = []
for f in sorted(os.listdir(path)):
if not f.startswith('.') and not f.startswith('_'):
if isdir(os.path.join(path,f)):
dirs.append('
')
files.append(''+f+'')
False,e = os.path.splitext(f)
if e in conf['ext_edit']:
files.append('(edit)')
files.append('(move)')
files.append('(delete)')
files.append('
')
for d in dirs: tree.append(d)
for f in files: tree.append(f)
tree.append('
')
return template('templates/yield', content='\n'.join(body), title=path+' deleted.')
@route('/edit/', method='GET')
@route('/edit', method="GET")
def edit(path=''):
if not path and request.query.path:
path = request.query.path
path = sanitize_path(path)
body = []
if path:
full = os.path.join(conf['repo'],path)
False, ext = os.path.splitext(path)
if os.path.exists(full) and not isdir(full) and ext in conf['ext_edit']:
body.append('')
body.append('
Editing '+ path +'
')
body.append('')
else:
body.append('
Path does not exist or is not editable.')
else:
body.append('
')
body.append('')
return template('templates/yield', content='\n'.join(body),\
title='move')
@route('/move', method="POST")
def rename():
current = os.path.join(conf['repo'],sanitize_path(request.forms.current))
new = os.path.join(conf['repo'],sanitize_path(request.forms.destination))
message = request.forms.message
body = []
if current and new:
if os.path.exists(current):
if not os.path.exists(new):
repo = Repo(conf['repo'])
repo.git.pull()
repo.git.mv(current,new)
if not message: message = datetime.now().isoformat()
commit = repo.git.commit(current,new,m=message)
repo.git.push()
body.append('
'+request.forms.current+' moved to '\
+request.forms.destination+'.
')
body.append('
Git: '+commit+'
')
else:
body.append('
'+request.forms.destination+' already exists.
')
else:
body.append('
'+request.forms.current+' does not exist.
')
else:
body.append('
Must have both a current and new name.
')
return template('templates/yield', content='\n'.join(body),\
title=request.forms.current+' moved to '+request.forms.destination)
@route('/mkdir/', method="GET")
@route('/mkdir', method="GET")
def mkdir(path=''):
body = []
path = sanitize_path(path)
body.append('')
body.append('
Create Directory
')
body.append('')
return template('templates/yield', content='\n'.join(body),\
title='Create Directory')
@route('/mkdir', method="POST")
def mkdir():
body = []
path = request.forms.path
path = sanitize_path(path)
if not os.path.exists(os.path.join(conf['repo'],path)):
os.mkdir(os.path.join(conf['repo'],path))
if isdir(os.path.join(conf['repo'],path)):
body.append('