[Zope3-dev] [Zope-Coders] Denying commit for carriage returns in text

Fred L. Drake, Jr. fred@zope.com
Thu, 14 Nov 2002 13:32:59 -0500


--12ys5fZPhv
Content-Type: text/plain; charset=us-ascii
Content-Description: message body and .signature
Content-Transfer-Encoding: 7bit


Toby Dickenson writes:
 > So what mistake will get caught by this filter?

This filter denies commit of updates that:

- contain Tabs in .py files

- contain carriage returns in text files

The filter only checks files that already exist in the repository; it
can't tell whether a file being added is expected to be text or
binary.  ;-(

Anyway, I've modified the script so it will work with files that only
exist on branches -- the CVS Attic is a terrible idea.  ;(  I've heard
we use branches for Zope, though.  ;-)


  -Fred

-- 
Fred L. Drake, Jr.  <fred at zope.com>
PythonLabs at Zope Corporation


--12ys5fZPhv
Content-Type: text/x-python
Content-Description: updated script
Content-Disposition: inline;
	filename="check-commit.py"
Content-Transfer-Encoding: 7bit

#! /usr/local/bin/python

import os.path
import sys

REPODIR = sys.argv[1]

def get_rcs_filename(filename):
    fn = os.path.join(REPODIR, filename) + ',v'
    if os.path.isfile(fn):
        return fn
    fn = os.path.join(REPODIR, "Attic", filename) + ',v'
    return fn

def main():
    items = []
    files = sys.argv[2:]
    for file in files:
        rcsfile = get_rcs_filename(file)
        if os.path.isfile(file) and os.path.isfile(rcsfile):
            items.append((file, rcsfile))
    if not items:
        return 0
    rlog_cmd = "rlog -h"
    for file, rcsfile in items:
        assert "'" not in rcsfile
        rlog_cmd += " '%s'" % rcsfile
    fp = os.popen(rlog_cmd, "r")

    failures = []
    while 1:
        line = fp.readline()
        if items and not line:
            fp.close()
            print >>sys.stderr, \
                  "No more input before determining the nature of all files."
            return 1
        assert line
        parts = line.split(": ", 1)
        parts[0] = parts[0].lower()
        if parts[0] == "rcs file":
            filename = parts[-1][:-1]
            file, rcsfile = items[0]
            assert rcsfile == filename
        elif parts[0] == "keyword substitution":
            file, rcsfile = items.pop(0)
            if "b" in parts[1]:
                continue
            text = open(file, "rb").read()
            if "\r" in text:
                failures.append(file + " (contains carriage return)")
            else:
                ext = os.path.splitext(file)[1]
                if ext == ".py" and "\t" in text:
                    failures.append(file + " (contains tab in Python code)")
                else:
                    print file + ": ok" 
            if not items:
                # all done
                break

    if failures:
        print "***"
        print "*** Found disallowed characters in text file; commit denied!"
        print "***"
        for msg in failures:
            print "*** File:", msg
        print "***"
        fp.close()
        return 1
    else:
        return fp.close()

if __name__ == "__main__":
    sys.exit(main())

--12ys5fZPhv--