Skip to content

Commit

Permalink
[backend] refactor lsprojects_local and lspackages_local
Browse files Browse the repository at this point in the history
  • Loading branch information
mlschroe committed Apr 18, 2016
1 parent b4b9b00 commit 7531eb4
Show file tree
Hide file tree
Showing 3 changed files with 80 additions and 80 deletions.
34 changes: 34 additions & 0 deletions src/backend/BSRevision.pm
Original file line number Diff line number Diff line change
Expand Up @@ -397,4 +397,38 @@ sub undelete_rev {
return $nrev;
}

sub lsprojects_local {
my ($deleted) = @_;
if ($deleted) {
my @projids = grep {s/\.pkg$//} ls("$projectsdir/_deleted");
@projids = grep {! -e "$projectsdir/$_.xml"} @projids;
return sort @projids;
}
local *D;
return () unless opendir(D, $projectsdir);
my @projids = grep {s/\.xml$//} readdir(D);
closedir(D);
return sort @projids;
}

sub lspackages_local {
my ($projid, $deleted) = @_;

if ($deleted) {
my @packids;
if (! -e "$projectsdir/$projid.xml" && -d "$projectsdir/_deleted/$projid.pkg") {
@packids = grep {$_ ne '_meta' && $_ ne '_project'} grep {s/\.mrev$//} ls("$projectsdir/_deleted/$projid.pkg");
} else {
@packids = grep {s/\.mrev\.del$//} ls("$projectsdir/$projid.pkg");
@packids = grep {! -e "$projectsdir/$projid.pkg/$_.xml"} @packids;
}
return sort @packids;
}
local *D;
return () unless opendir(D, "$projectsdir/$projid.pkg");
my @packids = grep {s/\.xml$//} readdir(D);
closedir(D);
return sort @packids;
}

1;
13 changes: 3 additions & 10 deletions src/backend/bs_admin
Original file line number Diff line number Diff line change
Expand Up @@ -212,14 +212,7 @@ Debug Options
}
my $emptymd5 = 'd41d8cd98f00b204e9800998ecf8427e';

#### FIXME: these functions are copied from src server. We should move it to some util class maybe.
sub findprojects {
return sort(grep {s/\.xml$//} ls($projectsdir));
}
sub findpackages {
my ($projid) = @_;
return sort(grep {s/\.xml$//} ls("$projectsdir/$projid.pkg"));
}
#### FIXME: this function is copied from src server. We should move it to some util module maybe.
sub findfile {
my ($rev, $repoid, $ext, $files) = @_;
$files = BSSrcrep::lsrev($rev) unless ref $files;
Expand Down Expand Up @@ -585,12 +578,12 @@ while (@ARGV) {
}
} elsif ($arg eq "--update-source-db") {
BSUtil::drop_privs_to($BSConfig::bsuser, $BSConfig::bsgroup);
my @prjs = findprojects();
my @prjs = BSRevision::lsprojects_local();
if (@ARGV == 1) {
@prjs = (shift @ARGV);
}
for my $projid (@prjs) {
for my $packid (findpackages($projid)) {
for my $packid (BSRevision::lspackages_local($projid)) {
print "$projid/$packid\n";
my $rev = BSRevision::getrev_local($projid, $packid);
BSRevision::updatelinkinfodb($projid, $packid, $rev, BSSrcrep::lsrev($rev));
Expand Down
113 changes: 43 additions & 70 deletions src/backend/bs_srcserver
Original file line number Diff line number Diff line change
Expand Up @@ -1962,85 +1962,57 @@ sub findlastworkinglink {

sub findprojects {
my ($deleted) = @_;
if ($deleted) {
my @projids = grep {s/\.pkg$//} ls("$projectsdir/_deleted");
@projids = grep {! -e "$projectsdir/$_.xml"} @projids;
return sort @projids;
}
local *D;
mkdir_p("$projectsdir") || die("creating $projectsdir: $!\n");
opendir(D, $projectsdir) || die("$projectsdir: $!\n");
my @projids = grep {s/\.xml$//} readdir(D);
closedir(D);
return sort @projids;
return BSRevision::lsprojects_local($deleted);
}

sub findpackages {
my ($projid, $proj, $nonfatal, $seen, $origins, $noexpand, $deleted) = @_;
$proj ||= readproj($projid, 1) || {};
local *D;
my @packids;
sub findpackages_remote {
my ($projid, $proj, $nonfatal, $origins, $noexpand, $deleted) = @_;

# if this is a remote project, forward to remote server
if ($proj->{'remoteurl'}) {
my $r;
my @args;
push @args, 'deleted=1' if $deleted;
push @args, 'expand=1' unless $noexpand || $deleted;
eval {
$r = BSRPC::rpc({'uri' => "$proj->{'remoteurl'}/source/$proj->{'remoteproject'}", 'proxy' => $proxy}, $BSXML::dir, @args);
};
if ($@ && $@ =~ /^404/) {
# remote project does not exist
die($@) unless $nonfatal;
return @packids;
}
if ($@) {
die($@) unless $nonfatal && $nonfatal > 0; # -1: internal projectlink recursion, errors are still fatal
warn($@);
push @packids, ':missing_packages' if $nonfatal == 2;
return @packids;
}
@packids = map {$_->{'name'}} @{($r || {})->{'entry'} || []};
if ($origins) {
for my $entry (@{($r || {})->{'entry'} || []}) {
$origins->{$entry->{'name'}} = defined($entry->{'originproject'}) ? maptoremote($proj, $entry->{'originproject'}) : $projid;
}
}
my @packids;
my @args;
push @args, 'deleted=1' if $deleted;
push @args, 'expand=1' unless $noexpand || $deleted;
my $r;
eval {
$r = BSRPC::rpc({'uri' => "$proj->{'remoteurl'}/source/$proj->{'remoteproject'}", 'proxy' => $proxy}, $BSXML::dir, @args);
};
if ($@ && $@ =~ /^404/) {
# remote project does not exist
die($@) unless $nonfatal;
return ();
}
if ($@) {
die($@) unless $nonfatal && $nonfatal > 0; # -1: internal projectlink recursion, errors are still fatal
warn($@);
push @packids, ':missing_packages' if $nonfatal == 2;
return @packids;
}

# handle deleted packages
if ($deleted) {
# we never expand deleted packages
if (! -e "$projectsdir/$projid.xml" && -d "$projectsdir/_deleted/$projid.pkg") {
@packids = grep {$_ ne '_meta' && $_ ne '_project'} grep {s/\.mrev$//} ls("$projectsdir/_deleted/$projid.pkg");
} else {
@packids = grep {s/\.mrev\.del$//} ls("$projectsdir/$projid.pkg");
@packids = grep {! -e "$projectsdir/$projid.pkg/$_.xml"} @packids;
}
@packids = sort @packids;
if ($origins) {
for (@packids) {
$origins->{$_} = $projid unless defined $origins->{$_};
}
@packids = map {$_->{'name'}} @{($r || {})->{'entry'} || []};
if ($origins) {
for my $entry (@{($r || {})->{'entry'} || []}) {
$origins->{$entry->{'name'}} = defined($entry->{'originproject'}) ? maptoremote($proj, $entry->{'originproject'}) : $projid;
}
return @packids;
}
return @packids;
}

sub findpackages {
my ($projid, $proj, $nonfatal, $seen, $origins, $noexpand, $deleted) = @_;

# check remote case
$proj ||= readproj($projid, 1) || {};
return findpackages_remote($projid, $proj, $nonfatal, $origins, $noexpand, $deleted) if $proj->{'remoteurl'};

# get local packages
if (opendir(D, "$projectsdir/$projid.pkg")) {
@packids = grep {s/\.xml$//} readdir(D);
closedir(D);
if ($origins) {
for (@packids) {
$origins->{$_} = $projid unless defined $origins->{$_};
}
my @packids = BSRevision::lspackages_local($projid, $deleted);
if ($origins) {
for (@packids) {
$origins->{$_} = $projid unless defined $origins->{$_};
}
}

# handle project links
if ($proj->{'link'} && !$noexpand) {
# handle project links (but not if deleted)
if ($proj->{'link'} && !$noexpand && !$deleted) {
$seen ||= {};
$seen->{$projid} = 1;
my $frozen = get_frozenlinks($projid);
Expand Down Expand Up @@ -2073,10 +2045,10 @@ sub findpackages {
}
}
}
@packids = BSUtil::unify(@packids);
@packids = sort(BSUtil::unify(@packids));
}

return sort @packids;
return @packids;
}

sub readproj {
Expand Down Expand Up @@ -3325,7 +3297,8 @@ sub undeleteproject {
sub getpackagelist {
my ($cgi, $projid, $repoid, $arch) = @_;
my $origins = $cgi->{'noorigins'} ? undef : {};
my $proj = checkprojrepoarch($projid, $repoid, $arch, 1) unless $cgi->{'deleted'};
my $proj;
$proj = checkprojrepoarch($projid, $repoid, $arch, 1) unless $cgi->{'deleted'};
my @packages = findpackages($projid, $proj, 0, {}, $origins, !$cgi->{'expand'}, $cgi->{'deleted'});
for (@packages) {
$_ = {'name' => $_};
Expand Down

0 comments on commit 7531eb4

Please sign in to comment.