in reply to Re^2: Getting all subpaths from a path
in thread Getting all subpaths from a path

Can someone suggest on strategy on how to solve it? I tried some other similar things but it got too complicated and failed.

Replies are listed 'Best First'.
Re^4: Getting all subpaths from a path
by choroba (Cardinal) on Mar 31, 2021 at 16:13 UTC
    You cannot create the links before both the source and target directories exist. So, postpone their creation to the end. The directories are processed from the shortest to the longest, so we always know the parent path exists. You should also add a check that a link doesn't point outside of the given directory tree.

    I created the following Makefile to experiment with your data:

    And this was the script 1.pl:

    #!/usr/bin/perl use warnings; use strict; use feature qw{ say }; use Path::Tiny qw{ path }; my $path = path(shift); my $paths = $path->visit( sub { $_[1]->{$_} = (-f) ? 'file' : (-l) ? [readlink] : (-d) ? 'dir' : 'unknown' }, { recurse => 1 } ); my @links; for my $found (sort { length $a <=> length $b } keys %$paths) { if ('file' eq $paths->{$found}) { say qq(cp '$found' "\$target/$found"); } elsif ('dir' eq $paths->{$found}) { say qq(mkdir "\$target/$found"); } elsif (ref [] eq ref $paths->{$found}) { my $to = path($paths->{$found}[0]); $to = $to->relative(path($found)->absolute->parent) if $to->is_absolute; my $source = path('$target', $found); push @links, qq(ln -s "$to" "$source"); } } say for @links;

    It's just a toy. Use some of the ShellQuote modules to fix the filenames; but for the example given, it works.

    map{substr$_->[0],$_->[1]||0,1}[\*||{},3],[[]],[ref qr-1,-,-1],[{}],[sub{}^*ARGV,3]
      Hi choroba! Thank you for your input as always. I have a few questions:
      1. As I understand the first part does opendir (somewhere) on the path my $path = path("/usr/vsa/pkgs/python3/3.6.3a/bin/python3.6"); and insert all of the files inside the directory into $paths but I'm interested only in the path itself. The input of the script is an array of paths. Each path (and only those paths) interest me. So if for example I have /usr/vsa/pkgs/python3/3.6.3a/bin/python3.6a (note the "a" at the end) and it does not exist in the array of paths, I don't want to copy it (only /usr/vsa/pkgs/python3/3.6.3a/bin/python3.6. See more info at (*)).
      2. It does opendir for some reason so it fails for files (the path /usr/vsa/pkgs/python3/3.6.3a/bin/python3.6 is a file and it fails with: Error opendir on '/usr/vsa/pkgs/python3/3.6.3a/bin/python3.6': Not a directory at line 113). Line 113 is "{ recurse => 1 }"
      3. It looks like path can't handle with some symlinks. I have a link /a -> /c/d/e and path("/a") returns nothing, while path("/a/b") returns paths under "/c/d/e/b".
      4. Also what if there is a link /a/ -> /b/ -> /c? Should it work?

      The cp/mkdir/ln part I will do myself. I'm just struggling to build the arrays of paths that are involved (and only them) - splitted into categories (file-links, dir-links, files, dirs).

      * A bit more explanation about the "involved paths": I have an array @array=("/usr/vsa/pkgs/python3/3.6.3a/bin/python3.6", ...). I want to iterate over this array and mkdir the directories that exist in the array (recursively), copy the files (not the directories but I want to have only the paths that are located in @array) and set the same links. Think of that as a container which you want to run your tool on and you know those paths are needed for running your tool (but any other path is not needed). So if your tool uses /a/b/c.file and under /a/b you also have /a/b/d.file, then it will create /a/b directory and copy /a/b/c to $target/a/b/c (and set links if needed). If I wanted to copy all of the files under directory, I would just copy the directory (instead of mkdir and cp files under directory).
Re^4: Getting all subpaths from a path
by tybalt89 (Monsignor) on Mar 31, 2021 at 18:41 UTC

    It seems like you are trying to re-invent 'tar'.

      Hi tybalt89, thanks for the comment. Do you talk about tarball (tar)? Then no. I'm trying to create a Singularity recipes builder. So I want to have the same "environment" (files/dir/links) as outside. In other words, the script should create a def file that creates directories, copies the files and creates the link - only of the paths I gave him. If you are not familiar with Singularity, it's similar to Docker containers but you can copy stuff from your work area.
Re^4: Getting all subpaths from a path
by haukex (Archbishop) on Mar 31, 2021 at 15:18 UTC

    Sorry, I've been quite busy, but I hope to get a chance to read your post tomorrow.

      That's ok! My comment was for all the Monks :) (Sorry that it's under your post, my detailed explanation is in this thread).