Find file Copy path
Fetching contributors…
Cannot retrieve contributors at this time
executable file 169 lines (140 sloc) 6.47 KB
#!/usr/bin/env python
This is an example parser for the <> specification.
It checks a single project/repository on disk, or a directory containing multiple
projects, and reports the status(es).
NOTE: This script works with Python 2.7 or newer.
Example Usage
On a single directory, containing a file with the identifier string present in it:
$ -p ~/GIT/
/home/jantman/GIT/ active
In pwd, containing a file with the identifier string present:
./: active
On a directory containing multiple git clones, some of which have the identifier present:
$ parsers/ -p ~/GIT
/home/jantman/GIT/php-nagios-xml: unsupported
/home/jantman/GIT/ec2machines: active
/home/jantman/GIT/nodejs-rpm-centos5: inactive
/home/jantman/GIT/updatewatcher: concept
/home/jantman/GIT/ active
/home/jantman/GIT/TuxTruck-wxPython: abandoned
Copyright 2014 Jason Antman <> <>
Free for any use provided that patches are submitted back to me.
The latest version of this script can be found at:
2014-12-25 jantman:
- initial script
import sys
import argparse
import logging
import os
import re
FORMAT = "[%(levelname)s %(filename)s:%(lineno)s - %(funcName)20s() ] %(message)s"
logging.basicConfig(level=logging.ERROR, format=FORMAT)
class RepoStatusOrg_Checker:
""" check directories on disk for status identifier """
readme_re = re.compile(r'^readme.*$', flags=re.I)
url_re = re.compile(r'http[s]?:\/\/.*repostatus\.org\/badges\/(.+)\/(.+)\.svg', flags=re.I)
def __init__(self, verbose=False):
self.logger = logging.getLogger(self.__class__.__name__)
if verbose:
def check(self, path):
check a path, that may be a single project
or a directory containing multiple projects
returns a dictionary of path to status name (or None if no status found)
:param path: path to check
:type path: string
:rtype: dict
# first, see if we have a matching file in this path
candidates = self._find_candidate_files(path)
self.logger.debug("Found {c} candidate files in path {p}: {candidates}".format(c=len(candidates),
if len(candidates) > 0:
# this path is a project itself, check for a match and return that
self.logger.debug("Found candidate files in {p}; checking only this path".format(p=path))
res = self._find_status_for_files(candidates)
return {path: res}
# else we found no candidates; look for child directories that might be projects
self.logger.debug("Found no candidate files in {p}; checking subdirectories".format(p=path))
res = {}
for d in os.listdir(path):
dpath = os.path.join(path, d)
if not os.path.isdir(dpath) or d == '.' or d == '..':
self.logger.debug("skipping non-directory or system directory: {dpath}".format(dpath=dpath))
candidates = self._find_candidate_files(dpath)
if len(candidates) < 1:
self.logger.debug("found 0 candidates in directory: {dpath}".format(dpath=dpath))
self.logger.debug("Found candidate files in subdirectory: {d}".format(d=dpath))
res[dpath] = self._find_status_for_files(candidates)
return res
def _find_status_for_files(self, flist):
Given a list of files to search, returns the version
and status name of the first matching status identifier URL found;
searches the files in list order. Returns None if no match found
:param flist: list of files to search through, in order
:type flist: list of strings (file paths)
:rtype: 2-tuple (version, status name) or None
for f in flist:
with open(f, 'r') as fh:
content =
res =
if res is not None:
self.logger.debug("Match found in {f}: {u}".format(f=f,
return (,
return None
def _find_candidate_files(self, path):
Return a list of all files under a given path,
which should be examined for a repostatus identifier.
List is in the order they should be checked.
candidates = []
files = os.listdir(path)
# sort files lexicographically
for fname in sorted(files, lambda x,y: cmp(x.lower(), y.lower()) or cmp(x,y)):
if self.readme_re.match(fname) and os.path.isfile(os.path.join(path, fname)):
candidates.append(os.path.join(path, fname))
for fname in [os.path.join(path, ''), os.path.join(path, '')]:
if os.path.exists(fname) and os.path.isfile(fname):
return candidates
def parse_args(argv):
parse command line arguments/options
p = argparse.ArgumentParser(description='Sample python script skeleton.')
p.add_argument('-v', '--verbose', dest='verbose', action='store_true', default=False,
help='verbose output (internal debugging).')
p.add_argument('-p', '--path', dest='path', type=str, default=os.getcwd(),
help='path to a project or directory of projects to check; default is cwd')
args = p.parse_args(argv)
return args
if __name__ == "__main__":
args = parse_args(sys.argv[1:])
# initialize the class
checker = RepoStatusOrg_Checker(verbose=args.verbose)
# run the check
result = checker.check(args.path)
for dir_path in result:
status = 'unknown'
if result[dir_path] is not None:
status = result[dir_path][1]
print("{dirpath}: {status}".format(dirpath=dir_path, status=status))