3 """This is for cleaning up binary files improperly added to CVS. This script
4 scans the given path to find binary files; checks with CVS to see if the sticky
5 options are set to -kb; finally if sticky options are not -kb then uses 'cvs
6 admin' to set the -kb option.
8 This script ignores CVS directories, symbolic links, and files not known under
9 CVS control (cvs status is 'Unknown').
11 Run this on a CHECKED OUT module sandbox, not on the repository itself. After
12 if fixes the sticky options on any files you should manually do a 'cvs commit'
13 to accept the changes. Then be sure to have all users do a 'cvs up -A' to
14 update the Sticky Option status.
25 def is_binary (filename):
27 """Assume that any file with a character where the 8th bit is set is
30 fin = open(filename, 'rb')
31 wholething = fin.read()
38 def is_kb_sticky (filename):
40 """This checks if 'cvs status' reports '-kb' for Sticky options. If the
41 Sticky Option status is '-ks' then this returns 1. If the status is
42 'Unknown' then it returns 1. Otherwise 0 is returned. """
45 s = pexpect.spawn ('cvs status %s' % filename)
46 i = s.expect (['Sticky Options:\s*(.*)\r\n', 'Status: Unknown'])
48 print 'File not part of CVS repository:', filename
49 return 1 # Pretend it's OK.
50 if s.match.group(1) == '-kb':
54 print 'Something went wrong trying to run external cvs command.'
55 print ' cvs status %s' % filename
56 print 'The cvs command returned:'
60 def cvs_admin_kb (filename):
62 """This uses 'cvs admin' to set the '-kb' sticky option. """
64 s = pexpect.run ('cvs admin -kb %s' % filename)
65 # There is a timing issue. If I run 'cvs admin' too quickly
66 # cvs sometimes has trouble obtaining the directory lock.
69 def walk_and_clean_cvs_binaries (arg, dirname, names):
71 """This contains the logic for processing files. This is the os.path.walk
72 callback. This skips dirnames that end in CVS. """
74 if len(dirname)>3 and dirname[-3:]=='CVS':
77 fullpath = os.path.join (dirname, n)
78 if os.path.isdir(fullpath) or os.path.islink(fullpath):
80 if is_binary(fullpath):
81 if not is_kb_sticky (fullpath):
82 if VERBOSE: print fullpath
83 cvs_admin_kb (fullpath)
87 if len(sys.argv) == 1:
91 os.path.walk (root, walk_and_clean_cvs_binaries, None)
93 if __name__ == '__main__':