TAG

首都機能移轉 (2) 歌詞 (2) 靠北文 (40) 戲言 (30) 糟糕 (7) ACG (23) Assembly (2) Boost (2) C (31) C++ (69) CMake (4) CSIE (67) Debian (34) Design_Pattern (2) Django (1) Eclipse (1) en_US (13) FFmpeg (3) FoolproofProject (26) FreeBSD (2) Git (4) GNU_Linux (65) IDE (5) Java (11) JavaScript (19) KDE (15) Khopper (16) KomiX (3) Kubuntu (18) Life (1) Lighttpd (2) Mac_OS_X (2) Opera (1) PHP (2) PicKing (2) Programing (21) Prolog (1) Python (7) QSnapshot (2) Qt (30) Qt_Jambi (1) Regular_Expression (1) Shell_Script (7) Talk (98) VirtualBox (7) Visual_Studio (13) Windows (18) zh_TW (36)

2008年6月6日 星期五

git compress tarbell script - in Python

#! /usr/bin/env python
#-*- coding: utf-8 -*-

import sys, os, bz2, time, optparse

BASE = os.path.basename( os.getcwd() )
TIMESTAMP = time.strftime( '%Y%m%d' )

def main( args = None ):
    if args is None:
        args = sys.argv
    parser = optparse.OptionParser( usage='usage: %prog [options] ' )
    parser.add_option( '-s', '--spec', type='string', dest='spec', help='explicitly sets version string' )
    parser.add_option( '--prefix', type='string', dest='prefix', help='string that before version' )
    parser.add_option( '--postfix', type='string', dest='postfix', help='string that append to timestamp' )
    parser.add_option( '-q', '--quiet', action='store_false', dest='verbose', help='be vewwy quiet (I\'m hunting wabbits)' )
    parser.set_defaults( verbose=True )
    opts, args = parser.parse_args( args=args[1:] )
    if len( args ) < 1:
        parser.print_help()
        return 1

    fileName = '%s-%s.%s' % ( BASE if opts.prefix is None else opts.prefix, args[0] if opts.spec is None else opts.spec, TIMESTAMP )
    if opts.postfix is not None:
        fileName += '-' + opts.postfix
    rData = os.popen( 'git archive --format=tar %s --prefix=%s/ %s' % ( '--verbose' if opts.verbose else '', fileName, args[0] ) ).read()
    cData = bz2.compress( rData )
    fout = open( '%s.tbz2' % fileName , 'wb' )
    fout.write( cData )
    fout.close()
    if opts.verbose:
        print "== compression ==\nbefore:\t%d\nafter:\t%d\nrate:\t%f%%" % ( len( rData ), len( cData ), 100.0 - ( 100.0 * len( cData ) / len( rData ) ) )
 
    return 0

if __name__ == '__main__':
    sys.exit( main() )

我真的覺得Python太好用了....還有我一直沒在做正事= =

1 則留言: