bassplayer has asked for the wisdom of the Perl Monks concerning the following question:
I have been tasked with adding a huge amount of files to CVS. This large web directory was created by others who did not employ CVS (as they should have). I started adding a few files, before realizing what a tremendous amount of time was involved. Then I decided to write some Perl to make my life easier.
Basically, I now have a program that I run in the main directory, and it is supposed to find all acceptible files (.pl, .cgi, .htm, .html, .shtml) and then add and commit them to the repository. It also adds and commits images using the binary setting of CVS. If it finds a directory, it should recurse through it and add and commit files found inside this directory, etc.
I initially found that within directories, the files were not being added or committed, because CVS was being passed file names without full path names. I am using IO::Dir to get directory information. It is not giving me full path names, just file names.
I am storing the directory names in an array, then adding and committing later because I was getting a Deep recursion on subroutine warning.
I apologize for the crappy code, but I wanted to get it working before I later optimized it (and I'm no Perl genius anyway).
I guess my question is, can I even do this the way that I have set out (IO::Dir, Shell)? Is there another way that someone can suggest? Does CVS offer any automation like this?
This should be a simple bit of code, I know, and I'm embarrassed to post this, but at least I'll learn. Anyway, here's the code:
#! /usr/bin/perl -w use strict; use IO::Dir; use Shell; open (LOG_FILE, ">/home/bassplayer/add_to_cvs_log.txt") || die "Couldn +'t open log file"; my @all_directories; my @directories = get_directories( dir => '.', first => '1' ); while ( @directories ) { my @more_directories = get_directories( dir => $directories[0], first => '0' ); push @directories, @more_directories; push @all_directories, $directories[0]; shift @directories; } foreach ( @all_directories ) { print STDOUT "ALL_DIR: $_\n"; check_files( dir => $_ ); } close LOG_FILE; exit 0; ##################### sub get_directories { ##################### my %arg = @_; my @directories; my $d = new IO::Dir $arg{dir}; if ( defined $d ) { while ( defined( $_ = $d->read ) ) { unless ( $_ eq '..' || ( $_ eq '.' && $arg{first} ne '1' ) + || $_ eq 'CVS' ) {; if ( -d $_ ) { print STDOUT "DIR: $_\n"; push @directories, $_; } } } undef $d; } return @directories; } ################# sub check_files { ################# my %arg = @_; my $d = new IO::Dir $arg{dir}; if ( defined $d ) { while ( defined( $_ = $d->read ) ) { next if $_ eq '..' || $_ eq '.' || $_ eq 'CVS'; if ( -d $_ ) { print LOG_FILE "ADDING DIR: $_\n"; my $cvs_output = cvs( 'add', $_ ); print LOG_FILE "COMMITTING DIR: $_\n"; $cvs_output .= cvs( 'commit', '-m _', $_ ); print LOG_FILE $cvs_output; } if ( ( -B $_ ) && ( $_ =~ m/\.jpg$/ || $_ =~ m/\.jpeg$/ || + $_ =~ m/\.gif$/ ) ) { print LOG_FILE "ADDING FILE: $_\n"; my $cvs_output = cvs( 'add', '-kb', $_ ); print LOG_FILE "COMMITTING FILE: $_\n"; $cvs_output .= cvs( 'commit', '-m _', $_ ); print LOG_FILE $cvs_output; } elsif ( $_ =~ m/\.pl$/ || m/\.cgi$/ || m/\.htm$/ || $_ =~ +m/\.html$/ || $_ =~ m/\.shtml$/ ) { print LOG_FILE "ADDING FILE: $_\n"; my $cvs_output = cvs( 'add', $_ ); print LOG_FILE "COMMITTING FILE: $_\n"; $cvs_output .= cvs( 'commit', '-m _', $_ ); print LOG_FILE $cvs_output; } } undef $d; } }
Your help is, of course, very much appreciated. Optimization suggestions are also welcome, as I do not get code review from anywhere, and am very interested in improving my Perl.
bassplayer
|
---|
Replies are listed 'Best First'. | |
---|---|
Re: Adding/Committing Files to CVS
by jkahn (Friar) on Sep 12, 2002 at 22:45 UTC | |
Re: Adding/Committing Files to CVS
by mfriedman (Monk) on Sep 12, 2002 at 22:44 UTC | |
Re: Adding/Committing Files to CVS
by rbc (Curate) on Sep 12, 2002 at 22:37 UTC | |
Re: Adding/Committing Files to CVS
by TStanley (Canon) on Sep 13, 2002 at 11:10 UTC | |
Re: Adding/Committing Files to CVS
by csotzing (Sexton) on Sep 13, 2002 at 10:50 UTC | |
Re: Adding/Committing Files to CVS
by bassplayer (Monsignor) on Oct 03, 2002 at 20:29 UTC |