in reply to Re^2: shedding a bash wrapper and updating to Path::Tiny
in thread shedding a bash wrapper and updating to Path::Tiny
I finally got it to work. It's hugely verbose, so I'll put output in readmore tags. I just kept on trying things until something worked. Path::Tiny examples are in the middle of this script. I worked the examples of Path::Class given on a path class page on perladvert. Now I'm gonna start editing, making it less verbose.
$ ./8.clone.pl 2.med 1.medit 1.pop ------------- making directories stringified abs to template is /home/bob/1.scripts/pages/1.medit/templ +ate_stuff string abs from is /home/bob/1.scripts/pages/2.med/template_stuff ------------- copying files return is /home/bob/1.scripts/pages/1.medit/template_stuff/5.unicode1. +css return is /home/bob/1.scripts/pages/1.medit/template_stuff/utils1.pm return is /home/bob/1.scripts/pages/1.medit/template_stuff/nibley1.pm ... [some get copied twice because of pl endings] return is /home/bob/1.scripts/pages/1.medit/template_stuff/code1.tmpl return is /home/bob/1.scripts/pages/1.medit/template_stuff/code1.tmpl ------------- dollar one is 1 munge is 1.medit1.css name is /home/bob/1.scripts/pages/1.medit/template_stuff/1.medit1.css return2 is /home/bob/1.scripts/pages/1.medit/template_stuff/1.medit1.c +ss ------------- matched is /home/bob/1.scripts/pages/2.med/2.med1.pl b is /home/bob/1.scripts/pages/1.medit/1.medit1.pl return3 is /home/bob/1.scripts/pages/1.medit/1.medit1.pl end of clone master_list/2013/North_America master_list/2013/North_America /home/bob/1.scripts/pages/master_list/2013/North_America master_list/2013 master_list/2013/North_America/Vancouver/ETHER /tmp/delivery_list_p2kJ5S string abs pop is /home/bob/1.scripts/pages/1.pop/template_stuff e is /home/bob/1.scripts/pages/1.pop/template_stuff/ruscaptions base dir is ruscaptions folder is 1 string folder is /home/bob/1.scripts/pages/1.medit/template_stuff/rusc +aptions default is /home/bob/1.scripts/pages/1.pop/template_stuff/ruscaptions/ +a.txt base is a.txt to name is /home/bob/1.scripts/pages/1.medit/template_stuff/ruscaption +s/a.txt return4 is /home/bob/1.scripts/pages/1.medit/template_stuff/ruscaption +s/a.txt default is /home/bob/1.scripts/pages/1.pop/template_stuff/ruscaptions/ +b.txt base is b.txt to name is /home/bob/1.scripts/pages/1.medit/template_stuff/ruscaption +s/b.txt return4 is /home/bob/1.scripts/pages/1.medit/template_stuff/ruscaption +s/b.txt e is /home/bob/1.scripts/pages/1.pop/template_stuff/logs base dir is logs folder is 1 string folder is /home/bob/1.scripts/pages/1.medit/template_stuff/logs e is /home/bob/1.scripts/pages/1.pop/template_stuff/aimages base dir is aimages folder is 1 string folder is /home/bob/1.scripts/pages/1.medit/template_stuff/aima +ges default is /home/bob/1.scripts/pages/1.pop/template_stuff/aimages/b.pn +g base is b.png to name is /home/bob/1.scripts/pages/1.medit/template_stuff/aimages/b. +png return4 is /home/bob/1.scripts/pages/1.medit/template_stuff/aimages/b. +png default is /home/bob/1.scripts/pages/1.pop/template_stuff/aimages/a.pn +g base is a.png to name is /home/bob/1.scripts/pages/1.medit/template_stuff/aimages/a. +png return4 is /home/bob/1.scripts/pages/1.medit/template_stuff/aimages/a. +png e is /home/bob/1.scripts/pages/1.pop/template_stuff/captions base dir is captions folder is 1 string folder is /home/bob/1.scripts/pages/1.medit/template_stuff/capt +ions default is /home/bob/1.scripts/pages/1.pop/template_stuff/captions/a.t +xt base is a.txt to name is /home/bob/1.scripts/pages/1.medit/template_stuff/captions/a +.txt return4 is /home/bob/1.scripts/pages/1.medit/template_stuff/captions/a +.txt default is /home/bob/1.scripts/pages/1.pop/template_stuff/captions/b.t +xt base is b.txt to name is /home/bob/1.scripts/pages/1.medit/template_stuff/captions/b +.txt return4 is /home/bob/1.scripts/pages/1.medit/template_stuff/captions/b +.txt return5 is 1 /home/bob/1.scripts/pages/1.medit 1.medit1.pl template_stuff path1 is /home/bob/1.scripts/pages/1.medit base is 1.medit values are home349337426.1and1-data.host redacted redacted object created, back in main word is 1.medit dir2 is perlmonks files are old num is 0 remote_dir is 1.medit1 dir is /home/bob/1.scripts/pages/1.medit/template_stuff/captions dir is /home/bob/1.scripts/pages/1.medit/template_stuff/ruscaptions tmpl is /home/bob/1.scripts/pages/1.medit/template_stuff/code2.tmpl tmpl is /home/bob/1.scripts/pages/1.medit/template_stuff/code3.tmpl mkdir1 failed path3 is /home/bob/1.scripts/pages/1.medit/template_stuff/1.medit1.css mkdir2 failed mkdir3 failed /pmimage/1.medit1 a is /home/bob/1.scripts/pages/1.medit/template_stuff/aimages/a.png b is a.png a is /home/bob/1.scripts/pages/1.medit/template_stuff/aimages/b.png b is b.png new file is 1.medit1.html $
The script is still in transition, and the produced html page has the code from the $from directory and the content from the $pop directory: new html page with new content . This is what exists now. The %vars is not hooked up to anything yet in this script.
#!/usr/bin/perl -w use strict; use 5.010; use utf8; use open qw/:std :utf8/; use Path::Tiny; my %vars = ( place => 'Vancouver', book => 'Медитац&# +1080;я на perlем', chapter => 'populated title', print_module => 1, script_file => undef, server_dir => 'perlmonks', image_dir => 'pmimage', ); # This script clones the template directory in $1 to $2. # Some names need munging. # $from is a populated child directory; $to is child dir to be create +d. $pop is the folder with the data. my ( $from, $to, $pop ) = @ARGV; my $ts = "template_stuff"; my $current = path(".")->absolute; #say "current is $current"; say "-------------"; say "making directories"; # define the paths within the target directory: my $string_abs_to = path( "$current", "$to", "$ts" )->mkpath; $string_abs_to = path( "$current", "$to", "$ts" )->stringify; say "stringified abs to template is $string_abs_to"; # $from template directory: my $abs_from = path( "$current", "$from", "$ts" ); my $string_abs_from = path( "$current", "$from", "$ts" )->stringify; say "string abs from is $string_abs_from"; say "-------------"; say "copying files"; for ( $abs_from->children ) { #say "default is $_"; next unless -f; if (m/(txt|pm|css|tmpl)$/) { my $base = $_->basename; #say "base is $base"; #syntax is from to to my $return = path($_)->copy( "$string_abs_to", "$base" ); say "return is $return"; } if (m/(pl|sh)$/) { my $base = $_->basename; my $return = path($_)->copy( "$string_abs_to", "$base" ); say "return is $return"; chmod 0755, $return; } } say "-------------"; # copy css file to template with munged name for ( $abs_from->children ) { #say "default is $_"; #say "from is $from"; my $base = $_->basename; #say "base is $base"; if ( $base =~ m/^$from(\d*)\.css$/ ) { #say "matching is $base"; say "dollar one is $1"; my $munge = $to . "1" . ".css"; say "munge is $munge"; my $name = path( "$string_abs_to", "$munge" ); say "name is $name"; #syntax is from to to my $return = path( "$string_abs_from", "$base" )->copy("$name"); say "return2 is $return"; } } ## munge and copy executable, change permissions say "-------------"; my $d = path( "$current", "$from" ); my @matching; for ( $d->children ) { #say "default is $_"; if (m/$from(\d*)\.pl$/i) { push @matching, $_; } } @matching = sort @matching; say "matched is @matching"; my $winner = pop @matching; my $newfile = "${to}1.pl"; my $b = path( $current, $to, $newfile ); print "b is $b\n"; my $return3 = path("$winner")->copy("$b"); say "return3 is $return3"; chmod 0755, $return3; say "end of clone"; my $path = path(qw(master_list 2013 North_America)); # prints 'master_list/2013/North_America'; say $path; # prints 'master_list/2013/North_America' on unix, # 'master_list\2013\North_America' on win32 say $path->canonpath; # prints /home/santa/master_list/2013/North_America when $CWD is /home +/santa say $path->absolute; # prints 'master_list/2013' say $path->parent; # prints 'master_list/2013/North_America/Vancouver/ETHER' say $path->child( 'Vancouver', 'ETHER' ); my $tempdir = Path::Tiny->tempdir('delivery_list_XXXXXX'); my $tempfile = Path::Tiny->tempfile( TEMPLATE => 'delivery_list_XXXXXX', suffix => +'.bin' ); my $scratch_file = $tempdir->child( 'batch_01', 'scratchfile.txt' )->t +ouchpath; chdir $tempdir unless $tempdir->subsumes( Path::Tiny->cwd ); system("pwd &"); my $abs_pop = path( "$current", "$pop", "$ts" ); my $string_abs_pop = path( "$current", "$pop", "$ts" )->stringify; say "string abs pop is $string_abs_pop"; for ( $abs_pop->children ) { next unless -d; my $e = $_; say "e is $e"; my $base_dir = $_->basename; say "base dir is $base_dir"; my $folder = path( "$current", "$to", "$ts", "$base_dir" )->mkpath; say "folder is $folder"; #my $string_folder = $folder->stringify(); my $string_folder = path( "$current", "$to", "$ts", "$base_dir" )->s +tringify; say "string folder is $string_folder"; my $pop_from = path("$e"); next if m/logs/; for ( $pop_from->children ) { say "default is $_\n"; my $base = $_->basename; say "base is $base"; my $to_name = path( "$string_folder", "$base" ); say "to name is $to_name"; my $return4 = path("$_")->copy("$to_name"); say "return4 is $return4"; } } my $exec_path = path( $current, $to ); my $return5 = chdir($exec_path); say "return5 is $return5"; system("pwd "); system("ls "); system ("./$newfile ");
I found that I had to stringify more than I was anticipating but could not stringify for ( $abs_pop->children ) {}. By the end, I figured out that I could use chdir and Path::Tiny to bring me where I needed to be instead of creating some ginormous path. It seems to work fine without quotes. (tested only once)
How do I change the logic so that .tmpl files don't get set to execute because they end in pl?
Accepting any criticisms of style. Does one use quotes in the path calls all the times or just sometimes?
|
---|
Replies are listed 'Best First'. | |
---|---|
Re^4: shedding a bash wrapper and updating to Path::Tiny
by kevbot (Vicar) on Jul 13, 2018 at 05:49 UTC | |
by Aldebaran (Curate) on Jul 25, 2018 at 05:45 UTC | |
by kevbot (Vicar) on Jul 26, 2018 at 04:23 UTC | |
by Aldebaran (Curate) on Jul 26, 2018 at 20:04 UTC |