[Date Prev][Date Next] [Thread Prev][Thread Next] [Date Index] [Thread Index]

[lintian] 02/02: L::R::ResMan: Rename resources by default



This is an automated email from the git hooks/post-receive script.

nthykier pushed a commit to branch master
in repository lintian.

commit 17726b3e3f3b46613426e6482d1d4a1009ffe645
Author: Niels Thykier <niels@thykier.net>
Date:   Sun Jan 24 09:39:20 2016 +0000

    L::R::ResMan: Rename resources by default
    
    When the reporting framework is generating graphs, the vast majority
    of all resources are generated.  Which means we end up doing a huge
    number of "copy + remove" when we can just do a trivial rename.
    
    Signed-off-by: Niels Thykier <niels@thykier.net>
---
 lib/Lintian/Reporting/ResourceManager.pm | 40 +++++++++++++++++++++++++++-----
 reporting/html_reports                   |  4 ++--
 2 files changed, 36 insertions(+), 8 deletions(-)

diff --git a/lib/Lintian/Reporting/ResourceManager.pm b/lib/Lintian/Reporting/ResourceManager.pm
index 8237f7b..96661ae 100644
--- a/lib/Lintian/Reporting/ResourceManager.pm
+++ b/lib/Lintian/Reporting/ResourceManager.pm
@@ -40,8 +40,10 @@ Lintian::Reporting::ResourceManager -- A simple resource manager for html_report
  my $resMan = Lintian::Reporting::ResourceManager->new(
     'html_dir' => 'path/to/HTML-root',
  );
- $resMan->install_resource('path/to/my-image.png');
- $resMan->install_resource('path/to/my-styles.css');
+ # Copy the resource
+ $resMan->install_resource('path/to/my-image.png', { install_method => 'copy'} );
+ # Move the resource
+ $resMan->install_resource('path/to/generated-styles.css');
  print 'Image: ' . $resMan->resource_URL('my-image.png'), "\n";
  print 'CSS: ' . $resMan->resource_URL('my-styles.css'), "\n";
 
@@ -80,7 +82,7 @@ sub new {
 
 =over 4
 
-=item install_resource(RESOURCE)
+=item install_resource(RESOURCE[, OPT])
 
 Installs RESOURCE into the html root.  The resource may be renamed
 (based on content etc.).
@@ -88,13 +90,29 @@ Installs RESOURCE into the html root.  The resource may be renamed
 Note that the basename of RESOURCE must be unique between all
 resources installed.  See L</resource_URL(RESOURCE_NAME)>.
 
+If OPT is given, it must be a hashref with 0 or more of the following
+keys (and values).
+
+=over 4
+
+=item install_method
+
+Can be "copy" or "move" (default).  If set to "move", the original file
+will be renamed into its new location.  Otherwise, a copy is done and
+the original file is left in place.
+
+=back
+
 =cut
 
 sub install_resource {
-    my ($self, $resource) = @_;
+    my ($self, $resource, $opt) = @_;
     my $install_name = get_file_checksum('sha1', $resource);
     my $basename = basename($resource);
     my $resource_root = $self->{'html_dir'} . '/resources';
+    my $method = 'move';
+    $method = $opt->{'install_method'}
+      if $opt && exists($opt->{'install_method'});
 
     croak("Resource name ${basename} already in use")
       if defined($self->{'_resource_cache'}{$basename});
@@ -103,8 +121,18 @@ sub install_resource {
         $install_name .= $ext;
     }
     mkdir($resource_root, 0755) if not -d $resource_root;
-    copy($resource, "$resource_root/$install_name")
-      or croak("Cannot copy $resource to $resource_root/$install_name: $!");
+    if ($method eq 'move') {
+        rename($resource, "$resource_root/$install_name");
+    } elsif ($method eq 'copy') {
+        copy($resource, "$resource_root/$install_name")
+          or
+          croak("Cannot copy $resource to $resource_root/$install_name: $!");
+    } else {
+        croak(
+            join(' ',
+                "Unknown install method ${method}",
+                '- please use "move" or "copy"'));
+    }
     $self->{'_resource_cache'}{$basename} = "resources/$install_name";
     return;
 }
diff --git a/reporting/html_reports b/reporting/html_reports
index e5e8665..d2bf079 100755
--- a/reporting/html_reports
+++ b/reporting/html_reports
@@ -145,7 +145,8 @@ for my $dir_basename (qw(resources images)) {
     opendir(my $dirfd, $dir);
     for my $resname (readdir($dirfd)) {
         next if $resname eq '.' or $resname eq '..';
-        $RESOURCE_MANAGER->install_resource("$dir/$resname");
+        $RESOURCE_MANAGER->install_resource("$dir/$resname",
+            { install_method => 'copy'});
     }
     closedir($dirfd);
 }
@@ -157,7 +158,6 @@ output_template(
     $templates{'lintian.css'},
     { 'path_prefix' => '../' });
 $RESOURCE_MANAGER->install_resource("$HTML_TMP_DIR/lintian.css");
-unlink("$HTML_TMP_DIR/lintian.css");
 
 my $STATE_CACHE = load_state_cache($HARNESS_STATE_DIR);
 

-- 
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/lintian/lintian.git


Reply to: