Allow calling the executable from any directory

When the tool is invoked like /path/to/git-arr, it currently fails because
both the serving of static files and bottle templates assume they're on the
current working directory.

This patch fixes that by computing the directories based on the executable
location.

Note this is assuming the static directory and the templates live next to the
executable, which will not always be the case, and eventually it should be
configurable; but it's ok for the time being.

Signed-off-by: Alberto Bertogli <albertito@blitiri.com.ar>
This commit is contained in:
Alberto Bertogli 2013-03-10 00:32:15 +00:00
parent 18e8599bfa
commit c72278c97c

23
git-arr

@ -5,6 +5,7 @@ git-arr: A git web html generator.
from __future__ import print_function from __future__ import print_function
import sys
import os import os
import math import math
import optparse import optparse
@ -20,6 +21,18 @@ import git
import utils import utils
# Tell bottle where to find the views.
# Note this assumes they live next to the executable, and that is not a good
# assumption; but it's good enough for now.
bottle.TEMPLATE_PATH.insert(
0, os.path.abspath(os.path.dirname(sys.argv[0])) + '/views/')
# The path to our static files.
# Note this assumes they live next to the executable, and that is not a good
# assumption; but it's good enough for now.
static_path = os.path.abspath(os.path.dirname(sys.argv[0])) + '/static/'
# The list of repositories is a global variable for convenience. It will be # The list of repositories is a global variable for convenience. It will be
# populated by load_config(). # populated by load_config().
repos = {} repos = {}
@ -240,7 +253,7 @@ def blob(repo, bname, fname, dirname = ''):
@bottle.route('/static/<path:path>') @bottle.route('/static/<path:path>')
def static(path): def static(path):
return bottle.static_file(path, root = './static/') return bottle.static_file(path, root = static_path)
# #
@ -328,10 +341,10 @@ def generate(output, skip_index = False):
# We can't call static() because it relies on HTTP headers. # We can't call static() because it relies on HTTP headers.
read_f = lambda f: open(f).read() read_f = lambda f: open(f).read()
write_to('static/git-arr.css', read_f, ['static/git-arr.css'], write_to('static/git-arr.css', read_f, [static_path + '/git-arr.css'],
os.stat('static/git-arr.css').st_mtime) os.stat(static_path + '/git-arr.css').st_mtime)
write_to('static/syntax.css', read_f, ['static/syntax.css'], write_to('static/syntax.css', read_f, [static_path + '/syntax.css'],
os.stat('static/syntax.css').st_mtime) os.stat(static_path + '/syntax.css').st_mtime)
for r in sorted(repos.values(), key = lambda r: r.name): for r in sorted(repos.values(), key = lambda r: r.name):
write_to('r/%s/index.html' % r.name, summary(r)) write_to('r/%s/index.html' % r.name, summary(r))