Subversion Repositories DevTools

Rev

Rev 6177 | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 6177 Rev 6387
Line 53... Line 53...
53
              rm_f
53
              rm_f
54
              mkpath
54
              mkpath
55
              printenv
55
              printenv
56
              printargs
56
              printargs
57
              echo
57
              echo
-
 
58
              copyDir
-
 
59
              unCopyDir
58
            );
60
            );
59
 
61
 
60
use File::Path qw(rmtree);
62
use File::Path qw(rmtree);
-
 
63
use JatsLocateFiles;
-
 
64
use JatsSystem;
-
 
65
 
61
our %opts;
66
our %opts;
62
 
67
 
63
#BEGIN
68
#BEGIN
64
#{
69
#{
65
#    print "-------jats_runtime initiated\n";
70
#    print "-------jats_runtime initiated\n";
Line 103... Line 108...
103
        }
108
        }
104
    }
109
    }
105
    #
110
    #
106
    #   Process some known options
111
    #   Process some known options
107
    #
112
    #
-
 
113
    $opts{'Verbose'} = $opts{'verbose'} if defined $opts{'verbose'}; 
108
    $opts{'Progress'} = $opts{'Verbose'}    if ( $opts{'Verbose'} );
114
    $opts{'Progress'} = $opts{'Verbose'}    if ( $opts{'Verbose'} );
109
    ErrorConfig( 'name', $opts{Name})       if ( $opts{'Name'} );
115
    ErrorConfig( 'name', $opts{Name})       if ( $opts{'Name'} );
110
    ErrorConfig( 'verbose', $opts{Verbose}) if ( $opts{'Verbose'} );
116
    ErrorConfig( 'verbose', $opts{Verbose}) if ( $opts{'Verbose'} );
111
    DebugDumpData("RunTime Opts", \%opts )  if ( $opts{'ShowOpts'} );;
117
    DebugDumpData("RunTime Opts", \%opts )  if ( $opts{'ShowOpts'} );;
112
    Message ("RunTime args: @ARGV")         if ( $opts{'ShowArgs'} );
118
    Message ("RunTime args: @ARGV")         if ( $opts{'ShowArgs'} );
Line 328... Line 334...
328
    expand_wildcards();
334
    expand_wildcards();
329
    File::Path::mkpath([@ARGV],0,0777);
335
    File::Path::mkpath([@ARGV],0,0777);
330
}
336
}
331
 
337
 
332
#-------------------------------------------------------------------------------
338
#-------------------------------------------------------------------------------
-
 
339
# Function        : copyDir 
-
 
340
#
-
 
341
# Description     : Copy a directory tree
-
 
342
#                   Used by PackageDir to perform run-time packaging
-
 
343
#
-
 
344
# Inputs          : @ARGV   - Options
-
 
345
#                       -mode=text
-
 
346
#                       -src=path
-
 
347
#                       -dst=path
-
 
348
#                       -execute        - Mark ALL as executable
-
 
349
#                       -noSymlink        
-
 
350
#                       -noRecurse
-
 
351
#                       -stripBase      - Strip first dir from the source
-
 
352
#                       -exclude+=filter
-
 
353
#                       -include+=filter
-
 
354
#
-
 
355
# Returns         : 
-
 
356
#
-
 
357
sub copyDir
-
 
358
{
-
 
359
    my $copts = processCopyDirArgs('copyDir');
-
 
360
    return unless $copts;
-
 
361
 
-
 
362
    #
-
 
363
    #   Create the target directory if required
-
 
364
    #
-
 
365
    unless (-d  $copts->{dst}) {
-
 
366
            Verbose("Create target directory: $copts->{dst}");
-
 
367
          File::Path::mkpath([$copts->{dst}],0,0777);
-
 
368
    }
-
 
369
 
-
 
370
    #
-
 
371
    #   Configure the use of the System function
-
 
372
    #   Don't exit on error - assume used in unpackaging
-
 
373
    #
-
 
374
    SystemConfig ( UseShell => 0, ExitOnError => 0);
-
 
375
 
-
 
376
    #
-
 
377
    #   Calc mode
-
 
378
    #
-
 
379
    my $fmode = '';
-
 
380
    $fmode .= '+x' if defined $copts->{execute}; 
-
 
381
    $fmode .= '+l' unless defined $copts->{noSymlink}; 
-
 
382
 
-
 
383
    #
-
 
384
    #   Configure the use of the System function
-
 
385
    #
-
 
386
    SystemConfig ( UseShell => 0, ExitOnError => 1);
-
 
387
 
-
 
388
    #
-
 
389
    #   Travserse the source directory and copy files
-
 
390
    #
-
 
391
    my @elements = $copts->{search}->search ( $copts->{src} );
-
 
392
 
-
 
393
    #
-
 
394
    #   Transfer each file
-
 
395
    #   Use the JatsFileUtil as it solves lots of problems
-
 
396
    #   Its args are strange - Historic (long Story). Args:
-
 
397
    #       'c0'        - Operation is Copy and debug level
-
 
398
    #       'Text'      - Text message to display
-
 
399
    #       DestPath
-
 
400
    #       SrcPath
-
 
401
    #       Modes       - wxl
-
 
402
    #
-
 
403
    #   Do not get the shell involved in invoking the command
-
 
404
    #   Quote args in '' not "" as "" will trigger shell usage
-
 
405
    #       
-
 
406
    #
-
 
407
    foreach my $file ( @elements)
-
 
408
    {
-
 
409
        my $dst = $file;
-
 
410
        #
-
 
411
        #   Calc target path name
-
 
412
        #
-
 
413
        if ($copts->{stripBase}) {
-
 
414
            $dst = substr($dst, $copts->{stripBase} );
-
 
415
        }
-
 
416
        $dst = $copts->{dst} . '/' . $dst;
-
 
417
 
-
 
418
        #
-
 
419
        #   If the file exists, then only copy it if the src is newer
-
 
420
        #
-
 
421
        if (-f $dst) {
-
 
422
            my ($file1, $file2) = @_;
-
 
423
 
-
 
424
            my $f1_timestamp = (stat($file))[9] || 0;
-
 
425
            my $f2_timestamp = (stat($dst))[9] || 0;
-
 
426
            next unless ($f1_timestamp > $f2_timestamp );
-
 
427
        }
-
 
428
 
-
 
429
        System('JatsFileUtil', 'c0', $copts->{mode} , $dst, $file, $fmode);
-
 
430
    }
-
 
431
}
-
 
432
 
-
 
433
#-------------------------------------------------------------------------------
-
 
434
# Function        : unCopyDir 
-
 
435
#
-
 
436
# Description     : Delete files copies with a copy dir command
-
 
437
#                   Delete directories if they are empty
-
 
438
#                   Used by PackageDir to perform run-time packaging
-
 
439
#
-
 
440
# Inputs          : @ARGV   - Options
-
 
441
#                       -mode=text
-
 
442
#                       -src=path
-
 
443
#                       -dst=path
-
 
444
#                       -execute        - Ignored
-
 
445
#                       -noSymlink      - Ignored
-
 
446
#                       -noRecurse
-
 
447
#                       -stripBase      - Strip first dir from the source
-
 
448
#                       -exclude+=filter
-
 
449
#                       -include+=filter
-
 
450
#                       -excludeRe+=filter
-
 
451
#                       -includeRe+=filter
-
 
452
#                       
-
 
453
#
-
 
454
# Returns         : 
-
 
455
#
-
 
456
sub unCopyDir
-
 
457
{
-
 
458
    my %dirList;
-
 
459
    my $copts = processCopyDirArgs('UnCopyDir');
-
 
460
    return unless $copts;
-
 
461
 
-
 
462
    #
-
 
463
    #   Configure the use of the System function
-
 
464
    #   Don't exit on error - assume used in unpackaging
-
 
465
    #
-
 
466
    SystemConfig ( UseShell => 0, ExitOnError => 0);
-
 
467
 
-
 
468
    #
-
 
469
    #   Nothing to do if the target directory does not exist
-
 
470
    #
-
 
471
    unless (-d $copts->{dst}) {
-
 
472
        Verbose("UnCopyDir: No target directory: $copts->{dst}");
-
 
473
        return;
-
 
474
    }
-
 
475
 
-
 
476
    #
-
 
477
    #   Travserse the source directory and find files that would have been copied
-
 
478
    #
-
 
479
    my @elements = $copts->{search}->search ( $copts->{src} );
-
 
480
 
-
 
481
    #
-
 
482
    #   Delete each file
-
 
483
    #   Use the JatsFileUtil as it solves lots of problems
-
 
484
    #   Its args are strange - Historic (long Story). Args:
-
 
485
    #       'd0'        - Operation is Copy and debug level
-
 
486
    #       'Text'      - Text message to display
-
 
487
    #       DestPath
-
 
488
    #
-
 
489
    #   Do not get the shell involved in invoking the command
-
 
490
    #   Quote args in '' not "" as "" will trigger shell usage
-
 
491
    #       
-
 
492
    #
-
 
493
    foreach my $file ( @elements)
-
 
494
    {
-
 
495
        my $dst = $file;
-
 
496
 
-
 
497
        #
-
 
498
        #   Calc target path name
-
 
499
        #
-
 
500
        if ($copts->{stripBase}) {
-
 
501
            $dst = substr($dst, $copts->{stripBase} );
-
 
502
        }
-
 
503
        $dst = $copts->{dst} . '/' . $dst;
-
 
504
 
-
 
505
        #
-
 
506
        #   Only delete if the file exists
-
 
507
        #
-
 
508
        next unless (-f $dst);
-
 
509
        System('JatsFileUtil', 'd0', $copts->{mode}, $dst);
-
 
510
 
-
 
511
        #   Save dir name for later cleanup
-
 
512
        if ($dst =~ s~/[^/]+$~~) {
-
 
513
            $dirList{$dst} = 1;
-
 
514
        }
-
 
515
    }
-
 
516
 
-
 
517
    #
-
 
518
    #   Delete all directories encountred in the tree - if they are empty
-
 
519
    #   Only delete up the base of the target directory
-
 
520
    #       Have a hash of directories - generated by the file deletion process
-
 
521
    #       Extend the hash to include ALL subdirectoroy paths too
-
 
522
    #   
-
 
523
    Verbose("Remove empty directories");
-
 
524
    foreach my $entry ( keys %dirList ) {
-
 
525
        while ($entry =~ s~/[^/]+$~~ ) {
-
 
526
            $dirList{$entry} = 2;
-
 
527
        }
-
 
528
    }
-
 
529
 
-
 
530
    my @dirList = sort { length $b <=> length $a } keys %dirList; 
-
 
531
    foreach my $tdir ( @dirList ) {
-
 
532
        Verbose("Remove dir: $tdir");
-
 
533
        rmdir $tdir;
-
 
534
    }
-
 
535
}
-
 
536
 
-
 
537
#-------------------------------------------------------------------------------
-
 
538
# Function        : processCopyDirArgs 
-
 
539
#
-
 
540
# Description     : Process the args for CopyDir and UnCopyDir so that the processing
-
 
541
#                   is identical
-
 
542
#
-
 
543
# Inputs          : $cmdName     - Command name
-
 
544
#                   From ARGV 
-
 
545
#
-
 
546
# Returns         : A hash containing
-
 
547
#                       copts   - Copy Options
-
 
548
#                       search  - For JatsLocateFiles 
-
 
549
#                   Empty if nothind to do    
-
 
550
#
-
 
551
sub processCopyDirArgs
-
 
552
{
-
 
553
    my ($cmdName) = @_;
-
 
554
    process_options();
-
 
555
 
-
 
556
    #
-
 
557
    #   Put the command line arguments into a hash
-
 
558
    #   Allow:
-
 
559
    #       aaa+=bbb        - An array
-
 
560
    #       aaa=bbb         - Value
-
 
561
    #       aaa             - Set to one
-
 
562
    #
-
 
563
    my %copts;
-
 
564
    foreach (@ARGV) {
-
 
565
        if (m~-(.*)\+=(.*)~) {
-
 
566
            push @{$copts{$1}}, $2;
-
 
567
 
-
 
568
        } elsif (m~-(.*)?=(.*)~){
-
 
569
            $copts{$1} = $2;
-
 
570
 
-
 
571
        } elsif (m~-(.*)~) {
-
 
572
            $copts{$1} = 1;
-
 
573
        }
-
 
574
    }
-
 
575
    Message ("$cmdName Dir Tree: $copts{src} -> $copts{dst}") if ($opts{'Progress'} );
-
 
576
 
-
 
577
    #
-
 
578
    #   Ensure the source exists
-
 
579
    #
-
 
580
    Warning ("$cmdName: Source directory does not exists:" . $copts{src}) unless -d $copts{src};
-
 
581
 
-
 
582
    #
-
 
583
    #   Calc strip length
-
 
584
    #
-
 
585
    if ($copts{stripBase}) {
-
 
586
        $copts{stripBase} = 1 + length($copts{src});
-
 
587
    }
-
 
588
 
-
 
589
    #
-
 
590
    #   Set up the search options to traverse the source directory and find files 
-
 
591
    #   to process
-
 
592
    #
-
 
593
    my $search = JatsLocateFiles->new('FullPath' );
-
 
594
    $search->recurse(1) unless $copts{noRecurse};
-
 
595
    $search->filter_in_re ( $_ ) foreach ( @{$copts{includeRe}} );
-
 
596
    $search->filter_out_re( $_ ) foreach ( @{$copts{excludeRe}} );
-
 
597
    $search->filter_in ( $_ ) foreach ( @{$copts{include}} );
-
 
598
    $search->filter_out( $_ ) foreach ( @{$copts{exclude}} );
-
 
599
    $search->filter_out_re( '/\.svn/' );
-
 
600
    $search->filter_out_re( '/\.git/' );
-
 
601
 
-
 
602
    #
-
 
603
    #   Return a hash
-
 
604
    #
-
 
605
    $copts{search} = $search;
-
 
606
    return \%copts;
-
 
607
}
-
 
608
 
-
 
609
 
-
 
610
#-------------------------------------------------------------------------------
333
# Function        : _unlink
611
# Function        : _unlink
334
#
612
#
335
# Description     : Helper function
613
# Description     : Helper function
336
#                   Unlink a list of files
614
#                   Unlink a list of files
337
#
615
#