Source

JYUQuota / update_version.py

#!/usr/bin/env python
# -*- coding: utf-8 -*-
'''
Created on Apr 15, 2010

@author: epeli
'''
import os
import sys
from datetime import datetime
from ConfigParser import SafeConfigParser

PACKAGE="jyuquota"


def update_hook(ui=None, repo=None, **kwargs):
    rv = open("release_version.txt", 'r')
    release_version = rv.read().strip()
    rv.close()
    
    version = release_version + datetime.now().strftime("-%Y%m%d-%H%M")
    
    
    v = open(os.path.join(PACKAGE, "data/version.txt"), 'w')
    v.write(version)
    v.close()
    
    
def install():
    hgrc = SafeConfigParser()
    hgrc.read(".hg/hgrc")
    
    if not hgrc.has_section("hooks"):
        hgrc.add_section("hooks")
        
    hgrc.set("hooks", "pre-commit.update_version", 
             "python:%s.update_hook" % __file__[2:-3]    )
    
    f = open(".hg/hgrc", 'w')
    hgrc.write(f)
    f.close()
    


def main():
    if len(sys.argv) > 1:
        if sys.argv[1] == "install":
            install()
        elif sys.argv[1] == "update":
            update_hook()            
    else:
        sys.stderr.write("""install using 'install' argument or update version 
with 'update'""")
    
        
        
        

if __name__ == '__main__':
    main()