Path: csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!news.mixmin.net!rt.uk.eu.org!newsfeed.xs4all.nl!newsfeed1a.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.000 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'repository': 0.05; 'url:bitbucket': 0.05; '-*-': 0.07; 'error:': 0.07; 'linux,': 0.07; 'reject': 0.07; 'revision': 0.07; 'sys': 0.07; 'users,': 0.07; '"__main__":': 0.09; '__name__': 0.09; 'exceeds': 0.09; 'git': 0.09; 'mode:': 0.09; 'python': 0.11; 'def': 0.12; 'wrote': 0.14; 'windows': 0.15; '#####': 0.16; '(should': 0.16; '*must*': 0.16; '.py': 0.16; '100644': 0.16; '8bit%:1': 0.16; 'cmd': 0.16; 'fetches': 0.16; 'item:': 0.16; 'lambda': 0.16; 'os.getcwd()': 0.16; 'skip:~ 20': 0.16; 'skip:\xa0 70': 0.16; 'specifying': 0.16; 'subject:skip:e 10': 0.16; 'subject:windows': 0.16; 'url:peps': 0.16; 'windows?': 0.16; '(0)': 0.16; 'url:)': 0.16; 'commit': 0.19; 'addition,': 0.20; 'import': 0.22; 'print': 0.22; 'error': 0.23; 'bytes': 0.24; 'config': 0.24; 'url:dev': 0.24; 'header:Received:8': 0.24; 'script': 0.25; 'least': 0.26; 'tried': 0.27; 'mode': 0.30; 'skip:g 30': 0.30; "skip:' 10": 0.31; 'initialized': 0.31; 'pipe': 0.31; 'file': 0.32; 'supposed': 0.32; 'thanks!': 0.32; 'option': 0.32; 'url:python': 0.33; '(e.g.': 0.33; 'skip:# 10': 0.33; 'skip:s 30': 0.35; 'but': 0.35; 'add': 0.35; 'version': 0.36; 'really': 0.36; '8bit%:9': 0.36; 'otherwise.': 0.36; 'skip:~ 10': 0.36; 'done': 0.36; 'hi,': 0.36; 'url:org': 0.36; 'should': 0.36; 'feedback': 0.38; 'system,': 0.38; 'skip:. 20': 0.38; 'to:addr:python-list': 0.38; 'files': 0.38; 'does': 0.39; '\xa0\xa0\xa0': 0.39; 'to:addr:python.org': 0.39; 'changed': 0.39; 'how': 0.40; 'read': 0.60; '8bit%:29': 0.60; 'received:98.137': 0.60; 'name': 0.63; 'maximum': 0.63; 'total': 0.65; 'header:Reply-To:1': 0.67; 'received:gq1.yahoo.com': 0.68; 'received:mail.gq1.yahoo.com': 0.68; 'subject: & ': 0.68; 'received:98.136': 0.69; 'below.': 0.71; '8bit%:100': 0.72; 'apart': 0.72; 'touch': 0.74; 'received:bullet.mail.gq1.yahoo.com': 0.84; 'skip:/ 30': 0.84; 'us?': 0.84; 'medicine,': 0.91; 'education,': 0.96 X-Yahoo-Newman-Property: ymail-4 X-Yahoo-Newman-Id: 171078.1746.bm@omp1009.mail.gq1.yahoo.com DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=yahoo.com; s=s1024; t=1400777938; bh=15laefDNQRbkWLvQXSejoWBhXkv5fneAnK/bFNrltes=; h=Message-ID:Date:From:Reply-To:Subject:To:MIME-Version:Content-Type:Content-Transfer-Encoding; b=D3ITUDRyWTjZzGl7W540lcDwkRGEQUzbx7N35aMm0ZCtvk3diyXZfCeS/oVjKIQn99Wibxc7Gt42DDXxr8UBog4yz7nBc7PJ0OGrFjRoGb3UDTKukLe7Zs9G4Kvc8ehFw6tm3oWXRJcZMI29cp8F89InQfOVdlcWL4TmBx3tk3c= X-YMail-OSG: e8xQyx8VM1ljecL8MnSHevWbq9CzgRVRqWfgZCbxwFOtgBZ ndUSBqj3iXu5zy45odlExA3r.Th.CZDb.dvffCI5ljlmJ.wXyV5Fr0njfPVM FkWfTLRslYIgHKEjDV90leAxzyZ0Oz2Za1pn4iaWbd4ZD1qrMFLUCD.EQYUh aJ9y7E6wGjmjrI6zF.Fkkh0ib4JqvV47TNh4tPRHpUhpRLOM1CCn3owP09kR cCuAchzaztCA5v5BCQTpo_PLtmH8ThLx0CDwh_y9xbNyQu2bv9oW4EnNfhOm HPO78qiyjX.76XyAairNT.oYv2w43wzPy2tfxEfQD2lZN9.JySOQBD4Hzmdy Xjru49xmSB9rm0k0XatG_.wPov12bd42EBcwvBKaR9Dm6kaoFHS_9vCkAy3m rRN8o1i8BbaD8PKGRZqpDIQr2Qb9kxA.l6o2XySEPK7zm5EMDA8Yjp1acOcF XA6IPwo8bgsqf2N07TV8WHFWWlaezA9KGTzeG.0jmmFeRPYXQKVhPlkBmM4_ _hgrPEwecu.ZX3XqztykWrGGnfGGgE1YX3N0So42h3s50RtKclpFxmBOCeEo _ZdeVnw7ZTdzPxNSk9NgQCxBcWHFwGaHq7GGa4b7VS7EeKK3cNkoFn3j9eF5 7RhQQS4xW7cTRTv7I6n_Wf5.Mczb6TvZcGE.7WTP8pPJOeSNAoUkDjonKPwi O8RNkw5ddatPDGEl.Wd9IlIRDJug0yHD4vs5hRwNYHzsNcg4- X-Rocket-MIMEInfo: 002.001, SGksCgpJIHdyb3RlIHRoZSBnaXQgcHJlLWNvbW1pdCBob29rIGJlbG93LiBJdCBpcyBzdXBwb3NlZCB0byByZWplY3QgY29tbWl0cyB0aGF0IGNvbnRhaW4gbGFyZ2UgZmlsZXMgKGUuZy4gYWNjaWRlbnRhbCBjb21taXRzIGJ5IGluZXhwZXJpZW5jZWQgdXNlcnMsIHRoaW5rIG9mICJnaXQgYWRkIC4iKQoKCkFueXdheSwgSSB0cmllZCB0aGlzIHVuZGVyIExpbnV4LCBidXQgdGhlIHRhcmdldCBwbGF0Zm9ybSBpcyBXaW5kb3dzLiBBcyBwZXIgR2l0IGRlc2lnbiB0aGUgaG9vayBuYW1lICptdXN0KiBiZSAicHIBMAEBAQE- X-Mailer: YahooMailWebService/0.8.188.663 Date: Thu, 22 May 2014 09:58:57 -0700 (PDT) From: Albert-Jan Roskam Subject: shebang & windows: call an extensionless git hook To: Python MIME-Version: 1.0 Content-Type: text/plain; charset=iso-8859-1 Content-Transfer-Encoding: quoted-printable X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.15 Precedence: list Reply-To: Albert-Jan Roskam List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 51 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1400778299 news.xs4all.nl 2958 [2001:888:2000:d::a6]:52729 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:71903 Hi,=0A=0AI wrote the git pre-commit hook below. It is supposed to reject co= mmits that contain large files (e.g. accidental commits by inexperienced us= ers, think of "git add .")=0A=0A=0AAnyway, I tried this under Linux, but th= e target platform is Windows. As per Git design the hook name *must* be "pr= e-commit" (no .py extension). How will Windows know that Python should be r= un? And (should it be relevant): how does Windows know which Python version= to invoke? I read about custom shebangs with Pylauncher. Is that my only o= ption? (see: https://bitbucket.org/vinay.sajip/pylauncher, http://legacy.py= thon.org/dev/peps/pep-0397/)=0A=0A=0AIn addition, I would really appreciate= general feedback on the hook script below.=A0=0A=0AThanks!=0A=0AAlbert-Jan= =0A=0Aalbertjan@debian ~/Desktop/test_repo $ git config --global init.templ= atedir ~/Desktop/git_template_dir=0Aalbertjan@debian ~/Desktop/test_repo $ = cd ~/Desktop/git_template_dir=0Aalbertjan@debian ~/Desktop/git_template_dir= $ cat hooks/pre-commit=0A#!/usr/bin/python=0A#-*- mode: python -*-=0A=0A""= "Git pre-commit hook: reject large files"""=0A=0Aimport=0Asys=0Aimport os= =0Aimport re=0Afrom subprocess import Popen, PIPE=0A=0Adef git_filesize_hoo= k(megabytes_cutoff=3D5, verbose=3DFalse):=0A=A0=A0=A0 """Git pre-commit hoo= k: Return error if the maximum file size in the HEAD=0A=A0=A0=A0 revision e= xceeds , succes (0) otherwise. You can bypass =0A=A0=A0= =A0 this hook by specifying '--no-verify' as an option in 'git commit'."""= =0A=A0=A0=A0 if verbose: print os.getcwd()=0A=A0=A0=A0 cmd =3D "git ls-tree= --full-tree -r -l HEAD"=0A=A0=A0=A0 git =3D Popen(cmd, shell=3DTrue, stdou= t=3DPIPE, cwd=3Dos.getcwd())=0A=A0=A0=A0 get_size =3D lambda item: int(re.s= plit(" +",=0Aitem)[3].split("\t")[0])=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0 =0A=A0= =A0=A0 sizes =3D map(get_size, git.stdout.readlines())=0A=A0=A0=A0 cut_off_= bytes =3D megabytes_cutoff * 2 ** 20=0A=A0=A0=A0 if max(sizes) > cut_off_by= tes:=0A=A0=A0=A0=A0=A0=A0=A0 return ("ERROR: your commit contains at least = one file "=0A=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0 "that is larger = than %d bytes" % cut_off_bytes)=0A=A0=A0=A0 return 0=0A=0Aif __name__ =3D= =3D "__main__":=0A=A0=A0=A0 sys.exit(git_filesize_hook(0.000001, True))=0A= =0Aalbertjan@debian ~/Desktop/git_template_dir $ cd -=0A/home/antonia/Deskt= op/test_repo=0Aalbertjan@debian ~/Desktop/test_repo $ git init=A0 ## this a= lso fetches my own pre-commit hook from template_dir=0AInitialized empty Gi= t repository in /home/antonia/Desktop/test_repo/.git/=0Aalbertjan@debian ~/= Desktop/test_repo $ touch foo.txt=0Aalbertjan@debian ~/Desktop/test_repo $ = git add foo.txt=0Aalbertjan@debian ~/Desktop/test_repo $ ls -l .git/hooks= =0Atotal 4=0A-rw-r--r-- 1 albertjan albertjan 1468 May 22 14:49 pre-commit= =0Aalbertjan@debian ~/Desktop/test_repo $ git commit -a -m "commit"=A0=A0 #= #### hook does not yet work=0A[master (root-commit) dc82f3d] commit=0A=A00 = files changed=0A=A0create mode 100644 foo.txt=0Aalbertjan@debian=0A~/Deskto= p/test_repo $ chmod +x .git/hooks/pre-commit=A0=A0 ###### can I avoid this = in Linux? What should I do in Windows?=0Aalbertjan@debian ~/Desktop/test_re= po $ echo "blaah\n" >> foo.txt=0Aalbertjan@debian ~/Desktop/test_repo $ git= commit -a -m "commit"=A0 ##### now the hook does its job=0A/home/antonia/D= esktop/test_repo=0AERROR: your commit contains at least one file that is la= rger than 1 bytes=0A=0ARegards,=0A=0AAlbert-Jan=0A=0A=0A=0A=0A~~~~~~~~~~~~~= ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~=0A=0AAll right, b= ut apart from the sanitation, the medicine, education, wine, public order, = irrigation, roads, a =0A=0Afresh water system, and public health, what have= the Romans ever done for us?=0A=0A=A0~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~= ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~