Path.pm 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  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. If no DEBIAN subdirectory is found, it will return undef.
  30. =cut
  31. sub get_pkg_root_dir($) {
  32. my $file = shift;
  33. $file =~ s{/+$}{};
  34. $file =~ s{/+[^/]+$}{} if not -d $file;
  35. while ($file) {
  36. return $file if -d "$file/DEBIAN";
  37. last if $file !~ m{/};
  38. $file =~ s{/+[^/]+$}{};
  39. }
  40. return undef;
  41. }
  42. =item relative_to_pkg_root($file)
  43. Returns the filename relative to get_pkg_root_dir($file).
  44. =cut
  45. sub relative_to_pkg_root($) {
  46. my $file = shift;
  47. my $pkg_root = get_pkg_root_dir($file);
  48. if (defined $pkg_root) {
  49. $pkg_root .= "/";
  50. return $file if ($file =~ s/^\Q$pkg_root\E//);
  51. }
  52. return undef;
  53. }
  54. =back
  55. =head1 AUTHOR
  56. Raphael Hertzog <hertzog@debian.org>.
  57. =cut
  58. 1;