home *** CD-ROM | disk | FTP | other *** search
/ Clickx 115 / Clickx 115.iso / software / tools / windows / tails-i386-0.16.iso / live / filesystem.squashfs / usr / share / perl5 / Dpkg / Compression.pm < prev    next >
Encoding:
Perl POD Document  |  2012-09-17  |  5.2 KB  |  219 lines

  1. # Copyright ┬⌐ 2010 Rapha├½l Hertzog <hertzog@debian.org>
  2. #
  3. # This program is free software; you can redistribute it and/or modify
  4. # it under the terms of the GNU General Public License as published by
  5. # the Free Software Foundation; either version 2 of the License, or
  6. # (at your option) any later version.
  7. #
  8. # This program is distributed in the hope that it will be useful,
  9. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  11. # GNU General Public License for more details.
  12. #
  13. # You should have received a copy of the GNU General Public License
  14. # along with this program.  If not, see <http://www.gnu.org/licenses/>.
  15.  
  16. package Dpkg::Compression;
  17.  
  18. use strict;
  19. use warnings;
  20.  
  21. our $VERSION = "1.00";
  22.  
  23. use Dpkg::ErrorHandling;
  24. use Dpkg::Gettext;
  25.  
  26. use base qw(Exporter);
  27. our @EXPORT = qw($compression_re_file_ext compression_get_list
  28.          compression_is_supported compression_get_property
  29.          compression_guess_from_filename
  30.          compression_get_default compression_set_default
  31.          compression_get_default_level
  32.          compression_set_default_level
  33.          compression_is_valid_level);
  34.  
  35. =encoding utf8
  36.  
  37. =head1 NAME
  38.  
  39. Dpkg::Compression - simple database of available compression methods
  40.  
  41. =head1 DESCRIPTION
  42.  
  43. This modules provides a few public funcions and a public regex to
  44. interact with the set of supported compression methods.
  45.  
  46. =head1 EXPORTED VARIABLES
  47.  
  48. =over 4
  49.  
  50. =cut
  51.  
  52. my $COMP = {
  53.     "gzip" => {
  54.     "file_ext" => "gz",
  55.     "comp_prog" => [ "gzip", "--no-name", "--rsyncable" ],
  56.     "decomp_prog" => [ "gunzip" ],
  57.     },
  58.     "bzip2" => {
  59.     "file_ext" => "bz2",
  60.     "comp_prog" => [ "bzip2" ],
  61.     "decomp_prog" => [ "bunzip2" ],
  62.     },
  63.     "lzma" => {
  64.     "file_ext" => "lzma",
  65.     "comp_prog" => [ 'xz', '--format=lzma' ],
  66.     "decomp_prog" => [ 'unxz', '--format=lzma' ],
  67.     },
  68.     "xz" => {
  69.     "file_ext" => "xz",
  70.     "comp_prog" => [ "xz" ],
  71.     "decomp_prog" => [ "unxz" ],
  72.     },
  73. };
  74.  
  75. our $default_compression = "gzip";
  76. our $default_compression_level = 9;
  77.  
  78. =item $compression_re_file_ext
  79.  
  80. A regex that matches a file extension of a file compressed with one of the
  81. supported compression methods.
  82.  
  83. =back
  84.  
  85. =cut
  86.  
  87. my $regex = join "|", map { $_->{"file_ext"} } values %$COMP;
  88. our $compression_re_file_ext = qr/(?:$regex)/;
  89.  
  90. =head1 EXPORTED FUNCTIONS
  91.  
  92. =over 4
  93.  
  94. =item my @list = compression_get_list()
  95.  
  96. Returns a list of supported compression methods (sorted alphabetically).
  97.  
  98. =cut
  99.  
  100. sub compression_get_list {
  101.     return sort keys %$COMP;
  102. }
  103.  
  104. =item compression_is_supported($comp)
  105.  
  106. Returns a boolean indicating whether the give compression method is
  107. known and supported.
  108.  
  109. =cut
  110.  
  111. sub compression_is_supported {
  112.     return exists $COMP->{$_[0]};
  113. }
  114.  
  115. =item compression_get_property($comp, $property)
  116.  
  117. Returns the requested property of the compression method. Returns undef if
  118. either the property or the compression method doesn't exist. Valid
  119. properties currently include "file_ext" for the file extension,
  120. "comp_prog" for the name of the compression program and "decomp_prog" for
  121. the name of the decompression program.
  122.  
  123. =cut
  124.  
  125. sub compression_get_property {
  126.     my ($comp, $property) = @_;
  127.     return undef unless compression_is_supported($comp);
  128.     return $COMP->{$comp}{$property} if exists $COMP->{$comp}{$property};
  129.     return undef;
  130. }
  131.  
  132. =item compression_guess_from_filename($filename)
  133.  
  134. Returns the compression method that is likely used on the indicated
  135. filename based on its file extension.
  136.  
  137. =cut
  138.  
  139. sub compression_guess_from_filename {
  140.     my $filename = shift;
  141.     foreach my $comp (compression_get_list()) {
  142.     my $ext = compression_get_property($comp, "file_ext");
  143.         if ($filename =~ /^(.*)\.\Q$ext\E$/) {
  144.         return $comp;
  145.         }
  146.     }
  147.     return undef;
  148. }
  149.  
  150. =item my $comp = compression_get_default()
  151.  
  152. Return the default compression method. It's "gzip" unless
  153. C<compression_set_default> has been used to change it.
  154.  
  155. =item compression_set_default($comp)
  156.  
  157. Change the default compression method. Errors out if the
  158. given compression method is not supported.
  159.  
  160. =cut
  161.  
  162. sub compression_get_default {
  163.     return $default_compression;
  164. }
  165.  
  166. sub compression_set_default {
  167.     my ($method) = @_;
  168.     error(_g("%s is not a supported compression"), $method)
  169.             unless compression_is_supported($method);
  170.     $default_compression = $method;
  171. }
  172.  
  173. =item my $level = compression_get_default_level()
  174.  
  175. Return the default compression level used when compressing data. It's "9"
  176. unless C<compression_set_default_level> has been used to change it.
  177.  
  178. =item compression_set_default_level($level)
  179.  
  180. Change the default compression level. Errors out if the
  181. level is not valid (see C<compression_is_valid_level>).
  182. either a number between 1 and 9 or "fast"
  183. or "best".
  184.  
  185. =cut
  186.  
  187. sub compression_get_default_level {
  188.     return $default_compression_level;
  189. }
  190.  
  191. sub compression_set_default_level {
  192.     my ($level) = @_;
  193.     error(_g("%s is not a compression level"), $level)
  194.             unless compression_is_valid_level($level);
  195.     $default_compression_level = $level;
  196. }
  197.  
  198. =item compression_is_valid_level($level)
  199.  
  200. Returns a boolean indicating whether $level is a valid compression level
  201. (it must be either a number between 1 and 9 or "fast" or "best")
  202.  
  203. =cut
  204.  
  205. sub compression_is_valid_level {
  206.     my ($level) = @_;
  207.     return $level =~ /^([1-9]|fast|best)$/;
  208. }
  209.  
  210. =back
  211.  
  212. =head1 AUTHOR
  213.  
  214. Rapha├½l Hertzog <hertzog@debian.org>.
  215.  
  216. =cut
  217.  
  218. 1;
  219.