aboutsummaryrefslogtreecommitdiff
path: root/lazygal/__init__.py
blob: 475bd7b7b72aacf9e57eda704ba8057e0313783a (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
87
88
89
90
91
92
# Lazygal, a lazy static web gallery generator.
# Copyright (C) 2007-2012 Alexandre Rossi <alexandre.rossi@gmail.com>
#
# 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 sys
import os


__all__ = ['generators', ]


# Compute installation prefix
if os.path.isfile(os.path.join(os.path.dirname(__file__), '..', 'setup.py')):
    INSTALL_MODE = 'source'
    INSTALL_PREFIX = ''
else:
    # Lazygal is installed, assume we are in
    # $prefix/lib/pythonX/dist-packages/lazygal
    INSTALL_MODE = 'installed'
    INSTALL_PREFIX = os.path.join(os.path.dirname(__file__),
                                  '..', '..', '..', '..')
    INSTALL_PREFIX = os.path.normpath(INSTALL_PREFIX)


def query_git_for_tag(gitdir):
    import subprocess
    o = subprocess.check_output(('git', 'describe', '--tags')) \
        .decode(sys.stdout.encoding).strip()
    tokens = o.split('-')
    h = tokens[-1]
    if len(h) != 8:
        # this is not a hash
        h = None
    return '+'.join(tokens), h


def git_repo_mtime(gitdir):
    return max([
        os.path.getmtime(os.path.join(gitdir, 'index')),
        os.path.getmtime(os.path.join(gitdir, 'HEAD')),
        os.path.getmtime(os.path.join(gitdir, 'refs', 'tags')),
    ])


def get_git_rev():
    gitdir = os.path.join(os.path.dirname(__file__), '..', '.git')
    if os.path.isdir(gitdir):
        last_revision_cache = os.path.join(gitdir, 'last_revision')

        if os.path.isfile(last_revision_cache)\
        and os.path.getmtime(last_revision_cache) > git_repo_mtime(gitdir):
            with open(last_revision_cache, 'r') as fp:
                return fp.read()
        else:
            import subprocess
            try:
                lastrev = query_git_for_tag(gitdir)
            except subprocess.CalledProcessError:
                os.unlink(last_revision_cache)
                return ''
            else:
                v, h = lastrev
                if h:
                    with open(last_revision_cache, 'w') as fp:
                        fp.write(v)
                    return v
                else:
                    return ''
    else:
        return ''


__version__ = '0.9.3'

rev = get_git_rev()
if rev: __version__ = rev

# vim: ts=4 sw=4 expandtab