Path.pm 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. # Copyright 2007 Raphaël Hertzog <hertzog@debian.org>
  2. # This program is free software; you can redistribute it and/or modify
  3. # it under the terms of the GNU General Public License as published by
  4. # the Free Software Foundation; either version 2 of the License, or
  5. # (at your option) any later version.
  6. # This program is distributed in the hope that it will be useful,
  7. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  8. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  9. # GNU General Public License for more details.
  10. # You should have received a copy of the GNU General Public License along
  11. # with this program; if not, write to the Free Software Foundation, Inc.,
  12. # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  13. package Dpkg::Path;
  14. use strict;
  15. use warnings;
  16. use Exporter;
  17. our @ISA = qw(Exporter);
  18. our @EXPORT_OK = qw(get_pkg_root_dir relative_to_pkg_root);
  19. =head1 NAME
  20. Dpkg::Path - some common path handling functions
  21. =head1 DESCRIPTION
  22. It provides some functions to handle various path.
  23. =head1 METHODS
  24. =over 8
  25. =item get_pkg_root_dir($file)
  26. This function will scan upwards the hierarchy of directory to find out
  27. the directory which contains the "DEBIAN" sub-directory and it will return
  28. its path. This directory is the root directory of a package being built.
  29. =cut
  30. sub get_pkg_root_dir($) {
  31. my $file = shift;
  32. $file =~ s{/+$}{};
  33. $file =~ s{/+[^/]+$}{} if not -d $file;
  34. do {
  35. return $file if -d "$file/DEBIAN";
  36. last if $file !~ m{/};
  37. $file =~ s{/+[^/]+$}{};
  38. } while ($file);
  39. return undef;
  40. }
  41. =item relative_to_pkg_root($file)
  42. Returns the filename relative to get_pkg_root_dir($file).
  43. =cut
  44. sub relative_to_pkg_root($) {
  45. my $file = shift;
  46. my $pkg_root = get_pkg_root_dir($file);
  47. if (defined $pkg_root) {
  48. $pkg_root .= "/";
  49. return $file if ($file =~ s/^\Q$pkg_root\E//);
  50. }
  51. return undef;
  52. }
  53. =back
  54. =head1 AUTHOR
  55. Raphael Hertzog <hertzog@debian.org>.
  56. =cut
  57. 1;