Welcome, guest | Sign In | My Account | Store | Cart

There are times were I need to generate a manifest, or perform an export of my current CVS sandbox. cvs export works similarly to cvs co, i.e. extracts directly from a repository. I want to generate a clean export, without all the log files, etc, that tend to pollute my work area. I started using os.path.walk and remove what's not in CVS, but in the end the following code was much simpler (especially after taking a look at the source code for os.path.walk :-)

Python, 30 lines
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
import os

def cvs_walk(currdir):
    """ walk through a CVS hierarchy

    Modeled after os.walk, this generator returns a topdown list of (currdir, dirs,
    files) tuples based on the CVS hierarchy.

    """
    dirs = []
    files = []
    if os.path.isdir(dir) and os.path.isdir(os.path.join(currdir, 'CVS')):
        for entry in open(os.path.join(currdir, 'CVS', 'Entries')):
            e = entry.split('/')
            if (len(e) > 1):
                if e[0] == 'D':
                    dirs.append(e[1])
                else:
                    files.append(e[1])
    yield (currdir, dirs, files)
    for d in dirs:
        for x in cvs_walk(os.path.join(currdir, d)):
            yield x
#
# use it just like os.path.walk
#

for (a_dir, dirs, files) in cvs_walk(top):
    for file in files:
        print os.path.join(a_dir, file)

2 comments

Senthil Kumaran 15 years, 8 months ago  # | flag

1) Don't use keywords like file, dir for variable names. 2) cvs_walk gives a wrong notion that it will walk the Remote CVS Directory. It does not. It does a os.path.walk on local cvs directory, skipping the CVS maintenance files. Remote directory walk is desirable.

Alain Mellan (author) 15 years, 8 months ago  # | flag

You are right about dir & file. Sorry if the cvs_walk name misled you, but it does exactly what it is intended: walk a local CVS directory tree, skipping the CVS maintenance files/directories, for the purpose of making a release, building a snapshot tar file, etc. For remote CVS (i.e. to extract data directly from the CVS repository), there is 'cvs export' (or just plain 'cvs checkout' :-)

Created by Alain Mellan on Fri, 25 Jul 2008 (MIT)
Python recipes (4591)
Alain Mellan's recipes (4)

Required Modules

Other Information and Tasks