Never been to DZone Snippets before?

Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

« Newer Snippets
Older Snippets »
Showing 1-6 of 6 total  RSS 

Recursively remove CVS or Subversion files from folders

You could just swap CVS with .svn for subversion etc.

find -d . -name 'CVS' -exec rm -rf '{}' \; -print

cvs printModified, removeUnmodified, chroot

This script perfoms three utilities on CVS working directories.

-m, --printModified print modified cvs files

This prints out the modified files by comparing the timestamps to those in the CVS/Entries file in all subdirectories.

--removeUnmodified delete unmodified cvs files

This deletes all unmodified files. This saves disk space. Later you can run "cvs update" and the unmodified files will be restored.

3. This changes the cvs root in all subdirectories
-d, --chroot [ROOT] change CVS root

-h, --help Show this message

#! /usr/bin/ruby -W0

require 'Time'
require 'Find'
require 'optparse'

def splitEntryLine(line)
	re = /\/(.*)\/(.*)\/(.*)\/(.*)\/(.*)/
	m=re.match(line)
	if m
		return m[1..-1]	
	else
		return nil
	end
end
		

def parseCvsEntries(dirname, modified)
#print "CVS: ", dirname
		array = []
    File.open(File.join(dirname,"CVS", "Entries"), "r") { | entriesFile |
        entriesFile.each_line { | line |
					name, ver, date, k, t=splitEntryLine(line)
					if name
							file=File.join(dirname, name)
							if File.exists?(file) and not File.directory?(file)
								orig=Time.parse(date)
								orig = orig+orig.gmt_offset
								mtime = File.new(file).mtime
								if mtime != orig
									if modified
										array.push(file)
									end
								else
									if not modified
										array.push(file)
									end
								end
							end
					end
        }
     }
	return array
end

def checkFile(dirname, filename, modified)
	array = []
	path = File.join(dirname, filename)
  if File.directory?(path)
		if filename == "CVS"
			array += parseCvsEntries(dirname, modified);
		elsif not filename == "." and not filename == ".."
			array += checkDir(path, modified)
		end	
	end
	return array
end

def checkDir(dirname, modified)
	array = Array.new
	Dir.entries(dirname).each do |filename|
		array += checkFile(dirname, filename, modified);
	end 
	return array
end

def removeUnmodified() 
	list = checkDir(".", false)
	list.each { |file|
		cmd = "rm -vf "+ file
		system(cmd)
	}
end
def printModified() 
	list = checkDir(".", true)
	list.each { |file|
		print file, "\n"
	}
end
def change_root(new_root)
	print new_root, "\n"
	Find.find(".") do |path|
		if File.basename(path) == "Root" and File.basename(File.dirname(path)) == "CVS"
			print path, "\n"
			File.open(path, "w") do |fout|
				fout << new_root << "\n"
			end
		end
	end
	print new_root, "\n"
end
def hello()
	print "Hi\n"
end
def usage() 
	puts "cvs.rb"
	puts "  options:"
	puts "    --removeUnmodified"
	puts "    --chroot(-d)"
	puts "    --printModified(-m)"
	puts "    --help(-h)"
	exit 1
end
def main() 
	opts = OptionParser.new {|opts|
		opts.on("-e", "--eval [TEXT]" ,"evaluate ruby code") do |text| 
			eval(text)
		end
		opts.on_tail("-h", "--help", "Show this message") do
          puts opts
          exit
        end
		opts.on("--removeUnmodified","delete unmodified cvs files") do 
			removeUnmodified	
		end
		opts.on("--printModified","-m", "print modified cvs files") do 
			printModified
		end
		opts.on("-d", "--chroot [ROOT]" ,"change CVS root") do |root| 
			change_root(root)
		end
	}.parse!
end
main

cvs update non verbose output

I don't like "cvs update" to print out which directories it updates, nor the files not under revision control. Following snippet go directly to the point : print the files that changed during last changeset

import pexpect

def update():
    child = pexpect.spawn('cvs update -dP')

    for line in child:
        if not line.startswith('?') and not line.startswith('cvs update'):
            print line[:-1]

Automatic CVS add

Spider a directory using:

find . -type d ! \( -name "*CVS" \) -exec python cvsAdd.py {} \;
find . -name "*.py" -exec python cvsAdd.py {} \;


in cvsAdd.py, put the following:

import sys, pexpect, getpass

PASS='myPass'

def cvsAdd(fname):
    global PASS
    if not PASS:
        PASS = getpass.getpass()
    
    child = pexpect.spawn('cvs add "%s"' % fname)
    child.expect("me@my-cvs-host password:")
    child.sendline(PASS)

    for line in child:
        print line

cvsAdd(sys.argv[1])

Remove CVS Directories with Ruby

Here's a simple Ruby script to remove CVS directories. Useful if you move a large codebase from one repository to another. To use it just go to the root directory where you want to start the deleting and run the script. The script will start in the current working directory and apply the removal recursively.

def deleteDir(dir)
    puts "cd #{dir}"
    Dir.chdir(dir)
    Dir.foreach(dir) do |file|
        if file != "." and file != ".."
            if File.directory?(file)
                deleteDir("#{dir}/#{file}")
            else
                puts "delete file #{file}"
                File.delete(file)
            end
        end
    end
    Dir.chdir("..")
    puts "delete directory #{dir}"
    Dir.delete(dir)
end

def processDir(dir)
    #puts "Processing directory #{dir}"
    Dir.chdir(dir)
    Dir.foreach(dir) do |file|
        if File.directory?(file)
            if file == "CVS"
                puts "Deleting directory #{dir}/#{file}"
                deleteDir("#{dir}/#{file}")
            elsif file != "." and file != ".."
                processDir("#{dir}/#{file}")
            end
        end
    end
    Dir.chdir("..")
end

puts "Working directory: #{Dir.pwd}"
processDir(Dir.pwd)

Doing something equivalent of "svn status" in CVS

The "cvs status" command is practically useless, given the amount of stuff it spews out. So I do the following to find out the status of my local working copy. This shows all updated, added, or deleted files, as well as those CVS know nothing about (and which perhaps should be added):

cvs status 2>&1 | egrep "(^\? |Status: )" | grep -v Up-to-date
« Newer Snippets
Older Snippets »
Showing 1-6 of 6 total  RSS