From e771fc11e43da60c25112b85cd5024eec74393cd Mon Sep 17 00:00:00 2001 From: Michael Ziegler Date: Thu, 3 Dec 2009 22:58:15 +0100 Subject: [PATCH] add hook scripts to make life a little easier --- hg/checkexec.py | 60 +++++++++++++++++++++++++++++++++++++++++++++ hg/checkincoming.py | 34 +++++++++++++++++++++++++ hg/licensecheck.py | 44 +++++++++++++++++++++++++++++++++ 3 files changed, 138 insertions(+) create mode 100644 hg/checkexec.py create mode 100644 hg/checkincoming.py create mode 100644 hg/licensecheck.py diff --git a/hg/checkexec.py b/hg/checkexec.py new file mode 100644 index 0000000..3b3b5c0 --- /dev/null +++ b/hg/checkexec.py @@ -0,0 +1,60 @@ +# -*- coding: utf-8 -*- + +""" + * Copyright (C) 2009, Michael "Svedrin" Ziegler + * + * Mumble-Django 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 package 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. +""" + +import os, stat + +from mercurial import hg + +type_whitelist = [ + 'application/x-executable; charset=binary', + 'application/x-sharedlib; charset=binary', + ]; + +def check_mimetype( filename ): + try: + fileproc = os.popen( 'file -ib %s' % filename ); + mtype = fileproc.read().strip(); + return (mtype in type_whitelist); + finally: + fileproc.close(); + +def check_script( filename ): + try: + f = open( filename, "rb" ); + return f.read(3) == '#!/'; + finally: + f.close(); + +def checkexec( ui, repo, **kwargs ): + modified, added, removed, deleted, unknown, ignored, clean = repo.status(); + + # We only care about modified and added files. Check their perms to see if +x + for curfile in (modified+added): + fullpath = os.path.join( repo.root, curfile ); + + statinfo = os.stat( fullpath ); + mode = statinfo.st_mode; + + # If executable without being an executable or a script, warn and fail + if ( (mode & stat.S_IXUSR) or (mode & stat.S_IXGRP) or (mode & stat.S_IXOTH) ) and \ + not ( check_mimetype( fullpath ) or check_script( fullpath ) ): + ui.warn( "Executable bit set on file %s but not ELF or script\n" % curfile ); + return True; + + return False; + + + diff --git a/hg/checkincoming.py b/hg/checkincoming.py new file mode 100644 index 0000000..05bb704 --- /dev/null +++ b/hg/checkincoming.py @@ -0,0 +1,34 @@ +# -*- coding: utf-8 -*- + +""" + * Copyright (C) 2009, Michael "Svedrin" Ziegler + * + * Mumble-Django 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 package 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. +""" + +from mercurial import hg + +def checkincoming( ui, repo, **kwargs ): + url = ui.config( 'paths', 'default' ); + remote = hg.repository( ui, url ); + + inc = repo.findincoming( remote ); + + if inc: + ui.status( 'Found %d incoming changesets.\n' % len(inc) ); + resp = ui.prompt( 'Do you want to abort the commit and pull/update first? [y/N]', choices=('&yes', '&no'), default='n' ); + return resp == 'y'; + else: + ui.status( 'Found no incoming changesets, proceeding to commit.\n' ); + return False; + + + diff --git a/hg/licensecheck.py b/hg/licensecheck.py new file mode 100644 index 0000000..90037ec --- /dev/null +++ b/hg/licensecheck.py @@ -0,0 +1,44 @@ +# -*- coding: utf-8 -*- + +""" + * Copyright (C) 2009, Michael "Svedrin" Ziegler + * + * Mumble-Django 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 package 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. +""" + +import os + +from mercurial import hg + +def checkfile( filename ): + try: + fileproc = os.popen( 'licensecheck %s' % filename ); + result = fileproc.read().strip(); + return not result.endswith( "*No copyright* UNKNOWN" ); + finally: + fileproc.close(); + + +def licensecheck( ui, repo, **kwargs ): + modified, added, removed, deleted, unknown, ignored, clean = repo.status(); + + # We only care about modified and added files. + for curfile in (modified+added): + fullpath = os.path.join( repo.root, curfile ); + + if not checkfile( fullpath ): + ui.warn( "License check failed for %s\n" % curfile ); + return True; + + return False; + + +