Back to home page

EIC code displayed by LXR

 
 

    


Warning, /iDDS/main/tools/git/pre-commit is written in an unsupported language. File is not indexed.

0001 #!/usr/bin/env python
0002 #
0003 # Licensed under the Apache License, Version 2.0 (the "License");
0004 # You may not use this file except in compliance with the License.
0005 # You may obtain a copy of the License at
0006 # http://www.apache.org/licenses/LICENSE-2.0OA
0007 #
0008 
0009 
0010 from __future__ import with_statement
0011 import os
0012 import re
0013 import shutil
0014 import subprocess
0015 import sys
0016 import tempfile
0017 
0018 
0019 def system(*args, **kwargs):
0020     kwargs.setdefault('stdout', subprocess.PIPE)
0021     proc = subprocess.Popen(args, **kwargs)
0022     out, err = proc.communicate()
0023     return out
0024 
0025 
0026 def main():
0027     currentbranch = system('git', 'rev-parse', '--abbrev-ref=strict', 'HEAD').rstrip('\n')
0028     if currentbranch=='master' or currentbranch=='dev':
0029         print "You are trying to commit on your master/dev, that's probably a mistake. Exiting..."
0030         sys.exit(1)
0031     modified = re.compile('^[AM]+\s+(?P<name>.*\.py)', re.MULTILINE)
0032     files = system('git', 'status', '--porcelain')
0033     files = modified.findall(files)
0034     tempdir = tempfile.mkdtemp()
0035     for name in files:
0036         filename = os.path.join(tempdir, name)
0037         filepath = os.path.dirname(filename)
0038         if not os.path.exists(filepath):
0039             os.makedirs(filepath)
0040         with file(filename, 'w') as f:
0041             system('git', 'show', ':' + name, stdout=f)
0042 
0043     f = open(tempdir + '/.pep8', 'w+')
0044     f.write('''[pep8]
0045 max-line-length=256
0046 
0047 [flake8]
0048 max-line-length=256
0049 ''')
0050     f.close()
0051     output = system('flake8', '.', cwd=tempdir)
0052     shutil.rmtree(tempdir)
0053     if output:
0054         print output,
0055         sys.exit(1)
0056 
0057 if __name__ == '__main__':
0058     main()