Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

How to find ant home/specific directory in Linux using perl

by sandeepda (Novice)
on Feb 05, 2014 at 08:01 UTC ( [id://1073526]=perlquestion: print w/replies, xml ) Need Help??

sandeepda has asked for the wisdom of the Perl Monks concerning the following question:

I have 2 version of a product in X version the ANT_HOME is in $testDir/modules and version Y the ANT_HOME is in $testDir/common/modules .I am using below script to get ANT_HOME in version X for version Y this is failing saying Error in opening dir /test123/modules because in version y it is changed to /test123/common/modules
sub getAntHome() { my ($testDir) = @_; my $modulesDir = File::Spec->catfile($testDir, "modules"); my $foundAnt = 0; my $antDir; opendir (DIR, $modulesDir) || die "Error in opening dir $modulesDi +r\n"; while (($filename = readdir(DIR)) and ($foundAnt == 0)){ my $temp1 = rindex $filename, "org.apache.ant_", 1; if ($temp1 == 0) { $antDir = $filename; $foundAnt = 1; } } closedir(DIR); if ($foundAnt) { return File::Spec->catfile($testDir, $antDir); } }
How can I change above script to dynamically get ANT_HOME inside my testDir?Thanks for suggestion

Replies are listed 'Best First'.
Re: How to find ant home/specific directory in Linux using perl
by Athanasius (Archbishop) on Feb 05, 2014 at 08:37 UTC

    If I understand the question correctly, you need to change this line:

    my $modulesDir = File::Spec->catfile($testDir, "modules");

    to:

    my $modulesDir = File::Spec->catfile(($testDir, 'common'), "modules");

    Also, this line:

    return File::Spec->catfile($testDir, $antDir);

    should be:

    return File::Spec->catfile($modulesDir, $antDir);

    See File::Spec.

    Hope that helps,

    Athanasius <°(((><contra mundum Iustus alius egestas vitae, eros Piratica,

      Let me rephrase the question Lets assume I have a ant directory "org.apache.ant_" at different location in 2 diffrent version of weblogic server In weblogic 5 it is in A/B/org.apache.ant.1.3/bin In weblogic 6 it is A/B/C/org.apache.ant.1.4/bin I know it exists in Directory A but trying to write a script inside A where it exists soo my script should find for org.apache.ant.x.y and return A/B/org.apache.ant.1.3 or A/B/C/org.apache.ant.1.4

        OK, you need to search directory A recursively until you find the first sub-directory named org.apache.ant.x.y (where x and y are integers). The module File::Find::Rule is useful for this.

        I created a directory structure like this:

        862_SoPW |-- weblogic_5 |-- A |-- B |-- org.apache.ant.1.3 |-- bin |-- weblogic_6 |-- A |-- B |-- C |-- org.apache.ant.1.4 |-- bin

        and ran the following script from directory 862_SoPW:

        #! perl use strict; use warnings; use File::Find::Rule; for (5 .. 7) { my $ant_home = get_ant_home('weblogic_' . $_) // 'not found'; print "In weblogic_$_, ant home is: $ant_home\n"; } sub get_ant_home { my $test_dir = shift; my $module_dir = $test_dir . '/A'; my @subdirs = File::Find::Rule->directory->in($module_dir); for (@subdirs) { return $_ if /org\.apache\.ant\.\d+\.\d+$/; } }

        Output:

        1:45 >perl 862_SoPW.pl In weblogic_5, ant home is: weblogic_5/A/B/org.apache.ant.1.3 In weblogic_6, ant home is: weblogic_6/A/B/C/org.apache.ant.1.4 In weblogic_7, ant home is: not found 1:46 >

        I think you should be able to adapt this to your requirements.

        Hope that helps,

        Athanasius <°(((><contra mundum Iustus alius egestas vitae, eros Piratica,

Re: How to find ant home/specific directory in Linux using perl
by kcott (Archbishop) on Feb 05, 2014 at 08:47 UTC

    G'day sandeepda,

    I'm not sure that I've fully understood your question, particularly with respect to what's happening with versions X and Y. My apologies up-front if the following doesn't help.

    ANT_HOME is an environment variable; Perl provides access to environment variables via the %ENV hash (see perlvar: General Variables); so the value you want should be $ENV{ANT_HOME}.

    -- Ken

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://1073526]
Approved by Athanasius
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others exploiting the Monastery: (5)
As of 2024-04-19 04:57 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found