tags:

views:

416

answers:

6

I have a Perl script to copy some config files from remote servers to the SVN repo and execute a commit. The config files on remote server are managed by different users. Sometimes the files are deleted by users. I want to make my script intelligent enough to detect that file as been deleted and able to issue a subsequent delete and report in SVN server too.

How can I programmatically detect deleted files?

The process I am using is

1) On remote server, the files are gzipping every midnight and placed at backup path.
2) on local server, script is using Net::SSH::Perl to copy the gzipped file and untar at svn repo path and execute a commit.

Lets say yesterday someone deleted one file (eg. aa.txt) and that will also not available in gzip backup file. I want to programmatically detect that file (aa.txt) before or after unzipping on local server and delete from svn too before commit.

Hope I am clear now.

A: 

Cool.

What's your question?

Maybe you're looking for post-commit hooks?

Noon Silk
I want to know some perl code to detect that deleted file while commit and issue a subsequent delete in SVN too.
Space
Well you can just poll the repo by updating and seeing the differences. Or go the hook method.
Noon Silk
@sillky: is there any link for hook method, I can look at.
Space
This describes the hooks: http://svnbook.red-bean.com/en/1.1/ch05s02.html
Noon Silk
+2  A: 

Are you using rsync to mirror the config files from the remote machine to your local working copy?

If so, you can use the --delete flag to make it delete the local files when they have been deleted on the remote server.

You can then use svn status to figure out which files have been added or deleted, and then issue appropriate svn commands to add and delete them, before committing the changes.

Avi
I am using Net::SSH::Perl (perl script) to copy files from remote server.
Space
A: 

Do you want to delete from the server, files that are removed from your working copy?

If so, the commit step will do this. The trick is to copy the directory containing the files, not just individual files and commit that. All files removed from that directory will be deleted from the repo.

gbjbaanb
A: 

How can I programmatically detect deleted files?

If you want to detect deleted files only by executing shell commands on the remote hosts through Net::SSH::Perl, then the answer depends on the remote shell. Here is an example if the remote shell is bash:

my $ssh = Net::SSH::Perl->new("host1");
$ssh->login("user", "password");
$ssh->cmd("if [ ! -f /var/log/messages ]; then echo \"file missing!\"");

You can replace the file path and the echo command by something else.

I know this probably doesn't answer your question. To get better answers, you really should hit the 'edit' button and add some examples of the commands you are already sending to the remote host. It's not clear how you are "copying files" exactly.

Wim Coenen
I have edited my problem, hope this is clear now.
Space
+1  A: 

So on the remote server, the files are not under svn control, and you're pulling them into your svn project nightly?

When a file has been deleted in your working copy, "svn status" will show that filename with "!" in front. See this doc for svn status for more details.

$ rm README.txt
...
$ svn status
!      README.txt

You can parse the output of "svn status" in Perl to look for files that're missing and issue the "svn delete" command for each of those, followed by the single "svn commit".

Harold L
yes on remote server the files are not under svn control. I am pulling them into svn every night. If any file is deleted on remote server I want to delete that in svn also.
Space
A: 

I can see that my question is now changed and have different meaning now. I would like to answer my own origional question. Hope this make the things clear.

1) copy the tar archvie at some /tmp path as you are doing now.
2) make an array with the files in the archvie. I suggest below code.
3) Read the files from svn repo and create another array. Remember to remove the .svn directories.
4) Compare both array for the missing files in tar archvie and use svn delete command to delete missing files from svn repo. Dont forgot to commit after that.

This is some part of my code.

#! /usr/bin/perl
use strict;
use warnings;

my @tararray = tarData("tar_archive");
my @configarray = configFiles();
foreach my $i (@configarray) {
    $i =~ s/^\.\///;
          my @diffs = "$i" if ! grep {$i eq $_} @tararray;
          foreach my $diff (@diffs) {
     system("svn -q delete $diff") || print $!;
                   }
system("svn commit -m 'message'");
}


sub tarData {
        my $tarfile = shift;
        my @lists = Archive::Tar->list_archive($tarfile);
        return @lists;
}


sub configFiles {
        use Cwd;
        my $path = getcwd;
        my @config;
        find(\&d, "$path");

        foreach my $file (@files) {
                chomp($file);
                next if $file =~ s/.svn//g;
                push @config, $file;
        }
        return @config;
}

sub d {
        -f and -r and push @files, $File::Find::name;
}
Space