Skip to content

Instantly share code, notes, and snippets.

@M0ses
Created November 23, 2020 16:50
Show Gist options
  • Save M0ses/06976b9b13d98f3815ce700dfcc70745 to your computer and use it in GitHub Desktop.
Save M0ses/06976b9b13d98f3815ce700dfcc70745 to your computer and use it in GitHub Desktop.
Simply script to generate OBS package meta information from spec file and print an osc command which can be pasted in a shell
#!/usr/bin/perl
use strict;
use warnings;
my $prj = 'devel:languages:perl';
my $template = <<EOL;
####################################################
osc meta pkg -F - %s %s <<EOF
<package name="%s" project="%s">
<title>%s - %s</title>
<description>%s</description>
<url>%s</url>
</package>
EOF
EOL
die "No package given!\n" unless @ARGV;
for my $pkg (@ARGV) {
$pkg =~ s#/##;
opendir(my $dir, $pkg);
my @files = grep {/.*\.spec$/} readdir($dir);
closedir $dir;
die "No spec found!\n" unless @files;
die "More than one spec '@files'\n" if (@files > 1);
my $spec = "$pkg/$files[0]";
open(my $fh, '<', $spec) || die "Could not open $spec: $!\n";
my @lines = <$fh>;
close $fh;
my $sum = find_summary(\@lines);
die "No Summary found\n" unless $sum;
my $cpan_name = find_cpan_name(\@lines);
die "No %cpan_name found\n" unless $cpan_name;
my $url = find_url(\@lines, $cpan_name);
die "No URL found\n" unless $url;
my $desc = find_desc(\@lines, $cpan_name);
die "No %description found\n" unless $desc;
printf $template, $prj, $pkg, $pkg, $prj, $pkg, $sum, $desc, $url;
}
exit 0;
sub find_desc {
my ($lines , $cpan_name) = @_;
my $cp;
my $desc;
for my $line (@{$lines}) {
chomp($line);
if ($cp) {
if ($line =~ /^%[^{%]/) {
last;
} else {
$desc .= "$line ";
}
}
if ($line =~ /^%description/) {
$cp++;
}
}
$desc =~ s/\s+/ /g;
return $desc;
}
sub find_url {
my ($lines , $cpan_name) = @_;
for my $line (@{$lines}) {
if ($line =~ /^Url:\s*(.*)$/i) {
my $url = $1;
$url =~ s/(%cpan_name|%\{cpan_name\})/$cpan_name/;
return $url;
}
}
}
sub find_summary {
my ($lines) = @_;
for my $line (@{$lines}) {
if ($line =~ /^Summary:\s*(.*)$/i) {
return $1;
}
}
}
sub find_cpan_name {
my ($lines) = @_;
for my $line (@{$lines}) {
if ($line =~ /^%define\s+cpan_name\s+(.*)$/i) {
return $1;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment