On Tue, Jan 12, 2016 at 3:04 PM, Stephan Beal <sgb...@googlemail.com> wrote:

> how about adding CSV export feature to the /reports pages?
>

This would also be useful for ticket reports (both in the web UI and the
CLI)..

Currently (at work, so I had to get permission to post this), I use the
following script to get tickets in CSV form. It makes use of the existing
"fossil ticket show" command, which produces a very simple tab-separated
values format (same format used for "raw" ticket reports in the web UI).
(While most spreadsheet apps can handle TSV files, they don't handle the
way Fossil escapes tabs and line breaks.)

#!perl
use warnings;
use strict;

sub usage
{
$0 =~ m#([^\\/]+$)#;
my $name = $1 // $0;
print STDERR "$name report [repository]\n" . <<'_SYNOPSIS_'; #< @here
Extract issue ticket data, from a Fossil repository or working copy,
into a CSV file. Generates CSV per RFC 4180, which seems to be accepted
by most spreadsheet applications.
"report" is the name or number of an issue report in the repository.
"repository" is the path or URL of the repository to extract from.
_SYNOPSIS_
}

my @cmd = qw/fossil ticket show/; #< Command to run, plus initial options
and parameters.
                                  #  (Parameters will be appened.)
my @tco = qw/-q/;     #< Trailing command options. (Will be appended to
final command.)
                      #  '-q' required for proper escaping of special
characters.

use IO::Pipe;  # This works on MS Windows as well as on Linux/Unix/POSIX
use Text::CSV;

if ((@ARGV < 1) || (@ARGV > 2))
{
usage();
exit 1;
}

## Fossil specific un-escape.
# Tried String::Unescape, but doesn't do all of what's needed (and does too
much other stuff)
sub funescape ($)
{
local $_ = $_[0];
s#\\s# #g;
s#\\t#\t#g;
s#\\n#\012#g;
s#\\r#\015#g;
s#\\f#\014#g;
s#\\v#\013#g;
s#\\0#\c@#g;
s#\\\\#\\#g;
return $_;
}

my $csv = Text::CSV->new ( { binary => 1 } )  # always set binary attribute.
                or die "Cannot use CSV: ".Text::CSV->error_diag ();
$csv->eol("\n");

my $report     = $ARGV[0]; #< Name or number of report as defined
inrepository
my $repository = $ARGV[1] // ''; #< Path or URL to repository

my $newfile    = "Issues_$report.csv";
open(my $of, '>', $newfile) or die "Can't open $newfile for writing: $!\n";

push @cmd, $report;
if ($repository ne '')
{
push @cmd, '-R', $repository;
}
push @cmd, @tco;

my $ch = IO::Pipe->new();
$ch->reader(@cmd) or die "Can't run $cmd[0]: $!\n";

while (<$ch>)
{
chomp; # embedded line breaks will be escaped as "\n", "\r" or "\r\n", so
this is safe
my @f = split("\t", $_);
for (@f)
{
$_ = funescape($_);
}
$csv->print($of, \@f);
}

close($of) or die "Write to $newfile failed: $!\n";

exit 0;
_______________________________________________
fossil-users mailing list
fossil-users@lists.fossil-scm.org
http://lists.fossil-scm.org:8080/cgi-bin/mailman/listinfo/fossil-users

Reply via email to