Attached is a python script which will list all the files tracked by git in a particular language. Use -h or --help to get usage.

It's basic algorithm is this:

cd to specified dir

get all files tracked by git in that dir and below

exclude any files whose path matches any exclusion pattern

foreach path
    get extension from basename

    if extension is in list of valid extensions for language
    then
        output path
    else
        if file has shell interpreter as first line
            if interpreter in language
            then
                output path

WARNING!!!

If you run this from the root of our tree it will pick up a lot of files which do *not* belong in our pot file!!!

WARNING!!!

Therefore if you decide to use this instead of explicitly listing the files you're going to have to explicitly list what to exclude. Listing exclusions might be more robust than listing files to include. Either way there is an opportunity to produce the wrong file list.

If you do use this script it's easy to set PYTHON_POTFILES and C_POTFILES to the output of this script, see the info page on gmake for the proper syntax.


--
John Dennis <jden...@redhat.com>

Looking to carve out IT costs?
www.redhat.com/carveoutcosts/
#!/usr/bin/python

# Authors: John Dennis <jden...@redhat.com>
#
# Copyright (C) 2010 Red Hat
#
# This program 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; version 2 only
#
# This program 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 sys
import os
import os.path
import getopt
import re
import subprocess

#-------------------------------------------------------------------------------

prog_name = os.path.basename(sys.argv[0])
python_cmd_re = re.compile('^#!.*python')

valid_languages = ['python', 'c', 'c++', 'java']

language_extensions = {
    'python' : ['.py'],
    'c'      : ['.c', '.h'],
    'c++'    : ['.cxx', '.cpp', '.c++', '.hxx', '.hpp'],
    'java'   : ['.java'],
}

config = {
    'debug'      : False,
    'language'   : 'python',
    'directory'  : '.',
    'exclusions' : [],
}

exclusion_regexps = []

start_dir = os.getcwd()

#-------------------------------------------------------------------------------

def get_git_files():
    cmd = "git ls-files"
    files = []

    p = subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE, 
stderr=subprocess.PIPE)
    status = os.waitpid(p.pid, 0)[1]
    if (status != 0):
        err_msg = p.stderr.read()
        raise EnvironmentError(1, "Error getting git files\ncmd=\"%s\" 
status=%d\n%s" % \
                                   (cmd, status, err_msg))

    for line in p.stdout:
        files.append(line.rstrip())

    return files

def test_file(path):
    # Get basename and extension
    basename = os.path.basename(path)
    path, ext = os.path.splitext(path)

    if ext:
        # There was an extension, sois the extension one of
        # the known extensions for this language?
        if ext in language_extensions[config['language']]: return True
    else:
        # No extension, does it a shell interpreter?
        if config['language'] == 'python':
            # Does the first line reference a python interpreter?
            try:
                f = open(path)
                line = f.readline()
                f.close()
            except:
                return False

            if python_cmd_re.search(line): return True

    # Not a member of the language
    return False

#-------------------------------------------------------------------------------

class Usage(Exception):
    def __init__(self, msg):
        self.msg = msg

def usage():
    '''
    Print command help.
    '''

    return '''\
%(prog_name)s [-h]] [-l lang] [-d dir] [-x pat]

-h --help       Print help.
-l --language   Which language whose files should be listed.
-d --directory  Start at dir instead of current directory.
-x --exclusion  Regular expression applied to path name,
                if it matches it will be excluded.
                May be used multiple times.
-D --debug      Provide extra info to stderr
Examples:

# Get all python files tracked by git
%(prog_name)s -l python

# Get all python files tracked by git in the ~/src/project tree
# excluding those whose path name begins with "tests/"
%(prog_name)s -d ~/src/project -x '^tests/'
''' % {'prog_name' : prog_name,
      }

#-------------------------------------------------------------------------------

def main(argv=None):
    if argv is None:
        argv = sys.argv

    try:
        try:
            opts, args = getopt.getopt(argv[1:], 'hDl:d:x:',
                                       ['help', 'debug', 'language', 
'directory', 'exclusion'])
        except getopt.GetoptError, e:
            raise Usage(e)
            return 2

        for o, a in opts:
            if o in ('-h', '--help'):
                print >>sys.stdout, usage()
                return 0
            elif o in ('-D', '--debug'):
                config['debug'] = True
            elif o in ('-l', '--language'):
                language = a.lower()
                if not language in valid_languages:
                    print >>sys.stderr, "unsupported language (%s), valid 
languages are (%s)" % \
                        (language, ', '.join(valid_languages))
                    return 1
                config['language'] = language
            elif o in ('-d', '--directory'):
                config['directory'] = a
            elif o in ('-x', '--exclusion'):
                config['exclusions'].append(a)
            else:
                raise Usage("command argument '%s' not handled, internal error" 
% o)
    except Usage, e:
        print >>sys.stderr, e.msg
        print >>sys.stderr, "for help use --help"
        return 2

    # Pre-compile regular expressions
    try:
        for pattern in config['exclusions']:
            exclusion_regexps.append(re.compile(pattern))
    except Exception, e:
        print >>sys.stderr, "bad regular expression \"%s\" (%s)" % \
            (pattern, e)
        return 2

    try:
        # Get the list of files tracked by Git
        os.chdir(config['directory'])
        all_git_files = get_git_files()

        # Eliminate exclusions, regexp search on the path name
        if exclusion_regexps:
            filtered_git_files = []
            for file in all_git_files:
                for regexp in exclusion_regexps:
                    if regexp.search(file):
                        if config['debug']:
                            print >>sys.stderr, "[debug] excluding %s" % (file)
                    else:
                        filtered_git_files.append(file)
        else:
            filtered_git_files = all_git_files

        language_files = []
        for file in filtered_git_files:
            valid = test_file(file)
            if valid:
                language_files.append(file)
            if config['debug']:
                if valid:
                    print >>sys.stderr, "[debug]     match %s" % file
                else:
                    print >>sys.stderr, "[debug]  no match %s" % file
        print "\n".join(language_files)

    except EnvironmentError, e:
        print >>sys.stderr, e.strerror
        return e.errno

#-------------------------------------------------------------------------------

if __name__ == '__main__':
    sys.exit(main())
_______________________________________________
Freeipa-devel mailing list
Freeipa-devel@redhat.com
https://www.redhat.com/mailman/listinfo/freeipa-devel

Reply via email to