Rewrite archive script into Python
Perl version needed Archive::Zip that for some reason is not installed on WSL by default. Use this as an opportunity to remove the last Perl script.
This commit is contained in:
parent
c5223be434
commit
5ca7e7cffc
2
Makefile
2
Makefile
@ -82,7 +82,7 @@ docs: docs/quickstart.html docs/manual.html
|
||||
|
||||
build/pugixml-%: .FORCE | $(RELEASE)
|
||||
@mkdir -p $(BUILD)
|
||||
perl tests/archive.pl $@ $|
|
||||
python tests/archive.py $@ pugixml-$(VERSION) $|
|
||||
|
||||
$(EXECUTABLE): $(OBJECTS)
|
||||
$(CXX) $(OBJECTS) $(LDFLAGS) -o $@
|
||||
|
||||
@ -1,72 +0,0 @@
|
||||
#!/usr/bin/perl
|
||||
|
||||
use Archive::Tar;
|
||||
use Archive::Zip;
|
||||
use File::Basename;
|
||||
|
||||
my $target = shift @ARGV;
|
||||
my @sources = @ARGV;
|
||||
|
||||
my $basedir = basename($target, ('.zip', '.tar.gz', '.tgz')) . '/';
|
||||
|
||||
my $zip = $target =~ /\.zip$/;
|
||||
my $arch = $zip ? Archive::Zip->new : Archive::Tar->new;
|
||||
|
||||
for $source (sort {$a cmp $b} @sources)
|
||||
{
|
||||
my $contents = &readfile_contents($source);
|
||||
my $meta = &readfile_meta($source);
|
||||
my $file = $basedir . $source;
|
||||
|
||||
if (-T $source)
|
||||
{
|
||||
# convert all newlines to Unix format
|
||||
$contents =~ s/\r//g;
|
||||
|
||||
if ($zip)
|
||||
{
|
||||
# convert all newlines to Windows format for .zip distribution
|
||||
$contents =~ s/\n/\r\n/g;
|
||||
}
|
||||
}
|
||||
|
||||
if ($zip)
|
||||
{
|
||||
my $path = $file;
|
||||
$arch->addDirectory($path) if $path =~ s/\/[^\/]+$/\// && !defined($arch->memberNamed($path));
|
||||
|
||||
my $member = $arch->addString($contents, $file);
|
||||
|
||||
$member->desiredCompressionMethod(COMPRESSION_DEFLATED);
|
||||
$member->desiredCompressionLevel(9);
|
||||
|
||||
$member->setLastModFileDateTimeFromUnix($$meta{mtime});
|
||||
}
|
||||
else
|
||||
{
|
||||
$arch->add_data($file, $contents, $meta);
|
||||
}
|
||||
}
|
||||
|
||||
$zip ? $arch->overwriteAs($target) : $arch->write($target, 9);
|
||||
|
||||
sub readfile_contents
|
||||
{
|
||||
my $file = shift;
|
||||
|
||||
open FILE, $file or die "Can't open $file: $!";
|
||||
binmode FILE;
|
||||
my @contents = <FILE>;
|
||||
close FILE;
|
||||
|
||||
return join('', @contents);
|
||||
}
|
||||
|
||||
sub readfile_meta
|
||||
{
|
||||
my $file = shift;
|
||||
|
||||
my ($dev, $ino, $mode, $nlink, $uid, $gid, $rdev, $size, $atime, $mtime, $ctime, $blksize, $blocks) = stat($file);
|
||||
|
||||
return {mtime => $mtime};
|
||||
}
|
||||
46
tests/archive.py
Normal file
46
tests/archive.py
Normal file
@ -0,0 +1,46 @@
|
||||
import os.path
|
||||
import sys
|
||||
import tarfile
|
||||
import zipfile
|
||||
import StringIO
|
||||
|
||||
def read_file(path, use_crlf):
|
||||
with open(path, 'rb') as file:
|
||||
data = file.read()
|
||||
|
||||
if '\0' not in data:
|
||||
data = data.replace('\r', '')
|
||||
if use_crlf:
|
||||
data = data.replace('\n', '\r\n')
|
||||
|
||||
return data
|
||||
|
||||
def write_zip(target, arcprefix, sources):
|
||||
with zipfile.ZipFile(target, 'w', zipfile.ZIP_DEFLATED) as archive:
|
||||
for source in sorted(sources):
|
||||
data = read_file(source, use_crlf = True)
|
||||
path = os.path.join(arcprefix, source)
|
||||
archive.writestr(path, data)
|
||||
|
||||
def write_tar(target, arcprefix, sources, compression):
|
||||
with tarfile.open(target, 'w:' + compression) as archive:
|
||||
for source in sorted(sources):
|
||||
data = read_file(source, use_crlf = False)
|
||||
path = os.path.join(arcprefix, source)
|
||||
info = tarfile.TarInfo(path)
|
||||
info.size = len(data)
|
||||
archive.addfile(info, StringIO.StringIO(data))
|
||||
|
||||
if len(sys.argv) < 4:
|
||||
raise RuntimeError('Usage: python archive.py <target> <archive prefix> <source files>')
|
||||
|
||||
target = sys.argv[1]
|
||||
arcprefix = sys.argv[2]
|
||||
sources = sys.argv[3:]
|
||||
|
||||
if target.endswith('.zip'):
|
||||
write_zip(target, arcprefix, sources)
|
||||
elif target.endswith('.tar.gz') or target.endswith('.tar.bz2'):
|
||||
write_tar(target, arcprefix, sources, compression = os.path.splitext(target)[1][1:])
|
||||
else:
|
||||
raise NotImplementedError('File type not supported: ' + target)
|
||||
Loading…
Reference in New Issue
Block a user