Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??
Greetings Exalted Monks,

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


In reply to Adding/Committing Files to CVS by bassplayer

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others imbibing at the Monastery: (6)
As of 2024-04-25 13:51 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found