beautify examples a bit
authorMichael Schroeder <mls@suse.de>
Tue, 30 Oct 2012 10:27:34 +0000 (11:27 +0100)
committerMichael Schroeder <mls@suse.de>
Tue, 30 Oct 2012 10:27:34 +0000 (11:27 +0100)
examples/p5solv
examples/pysolv
examples/rbsolv

index 9333b44..a5193a5 100755 (executable)
@@ -41,7 +41,7 @@ sub calc_cookie_ext {
   my $chksum = solv::Chksum->new($solv::REPOKEY_TYPE_SHA256);
   $chksum->add("1.1");
   $chksum->add($cookie);
-  $chksum->add_fstat(fileno($f)) if $f;
+  $chksum->add_fstat(fileno($f));
   my $extcookie = $chksum->raw();
   substr($extcookie, 0, 1) = chr(1) if ord(substr($extcookie, 0, 1)) == 0;
   return $extcookie;
@@ -191,6 +191,11 @@ sub writecachedrepo {
   rename($tmpname, $self->cachepath($ext));
 }
 
+sub packagespath {
+  my ($self) = @_;
+  return '';
+}
+
 package Repo::rpmmd;
 
 our @ISA = ('Repo::generic');
@@ -448,6 +453,11 @@ sub load_if_changed {
   return undef;
 }
 
+sub packagespath {
+  my ($self) = @_;
+  return ($self->{'handle'}->lookup_str($solv::SOLVID_META, $solv::SUSETAGS_DATADIR) || 'suse') . '/';
+}
+
 package Repo::unknown;
 
 our @ISA = ('Repo::generic');
@@ -714,9 +724,7 @@ if ($cmd eq 'install' || $cmd eq 'erase' || $cmd eq 'up' || $cmd eq 'dup' || $cm
       my $repo = $p->{'repo'}->{'appdata'};
       my ($location, $medianr) = $p->lookup_location();
       next unless $location;
-      if ($repo->{'type'} eq 'susetags') {
-       $location = ($repo->{'handle'}->lookup_str($solv::SOLVID_META, $solv::SUSETAGS_DATADIR) || 'suse') ."/$location";
-      }
+      $location = $repo->packagespath() . $location;
       my $chksum = $p->lookup_checksum($solv::SOLVABLE_CHECKSUM);
       my $f = $repo->download($location, 0, $chksum);
       die("\n$repo->{'alias'}: $location not found in repository\n") unless $f;
index 479aba2..b6e7710 100755 (executable)
@@ -45,30 +45,6 @@ from optparse import OptionParser
 #import gc
 #gc.set_debug(gc.DEBUG_LEAK)
 
-def calc_cookie_file(filename):
-    chksum = solv.Chksum(solv.REPOKEY_TYPE_SHA256)
-    chksum.add("1.1")
-    chksum.add_stat(filename)
-    return chksum.raw()
-
-def calc_cookie_fp(fp):
-    chksum = solv.Chksum(solv.REPOKEY_TYPE_SHA256)
-    chksum.add("1.1");
-    chksum.add_fp(fp)
-    return chksum.raw()
-
-def calc_cookie_ext(f, cookie):
-    chksum = solv.Chksum(solv.REPOKEY_TYPE_SHA256)
-    chksum.add("1.1");
-    chksum.add(cookie)
-    if f:
-       chksum.add_fstat(f.fileno())
-    extcookie = chksum.raw()
-    # compatibility to c code
-    if ord(extcookie[0]) == 0:
-       extcookie[0] = chr(1)
-    return extcookie
-
 class repo_generic(dict):
     def __init__(self, name, type, attribs = {}):
         for k in attribs:
@@ -76,6 +52,29 @@ class repo_generic(dict):
         self.name = name
         self.type = type
 
+    def calc_cookie_file(self, filename):
+        chksum = solv.Chksum(solv.REPOKEY_TYPE_SHA256)
+        chksum.add("1.1")
+        chksum.add_stat(filename)
+        return chksum.raw()
+
+    def calc_cookie_fp(self, fp):
+        chksum = solv.Chksum(solv.REPOKEY_TYPE_SHA256)
+        chksum.add("1.1");
+        chksum.add_fp(fp)
+        return chksum.raw()
+
+    def calc_cookie_ext(self, f, cookie):
+        chksum = solv.Chksum(solv.REPOKEY_TYPE_SHA256)
+        chksum.add("1.1");
+        chksum.add(cookie)
+        chksum.add_fstat(f.fileno())
+        extcookie = chksum.raw()
+        # compatibility to c code
+        if ord(extcookie[0]) == 0:
+            extcookie[0] = chr(1)
+        return extcookie
+
     def cachepath(self, ext = None):
         path = re.sub(r'^\.', '_', self.name)
         if ext:
@@ -253,7 +252,7 @@ class repo_generic(dict):
                 self.handle.write_first_repodata(f)
             if self.type != 'system' and not ext:
                 if 'extcookie' not in self:
-                   self['extcookie'] = calc_cookie_ext(f, self['cookie'])
+                    self['extcookie'] = self.calc_cookie_ext(f, self['cookie'])
                 f.write(self['extcookie'])
             if not ext:
                 f.write(self['cookie'])
@@ -299,6 +298,9 @@ class repo_generic(dict):
             repodata.internalize()
             self.writecachedrepo(None, repodata)
 
+    def packagespath(self):
+        return ''
+
 class repo_repomd(repo_generic):
     def load_if_changed(self):
         print "rpmmd repo '%s':" % self.name,
@@ -309,7 +311,7 @@ class repo_repomd(repo_generic):
             self.handle.free(True)
             del self.handle
             return False
-        self['cookie'] = calc_cookie_fp(f)
+        self['cookie'] = self.calc_cookie_fp(f)
         if self.usecachedrepo(None, True):
             print "cached"
             solv.xfclose(f)
@@ -415,7 +417,7 @@ class repo_susetags(repo_generic):
             self.handle.free(True)
             del self.handle
             return False
-        self['cookie'] = calc_cookie_fp(f)
+        self['cookie'] = self.calc_cookie_fp(f)
         if self.usecachedrepo(None, True):
             print "cached"
             solv.xfclose(f)
@@ -531,6 +533,12 @@ class repo_susetags(repo_generic):
         self.writecachedrepo(ext, repodata)
         return True
 
+    def packagespath(self):
+        datadir = repo.handle.lookup_str(solv.SOLVID_META, solv.SUSETAGS_DATADIR)
+        if not datadir:
+            datadir = 'suse'
+        return datadir + '/'
+
 class repo_unknown(repo_generic):
     def load(self, pool):
         print "unsupported repo '%s': skipped" % self.name
@@ -542,7 +550,7 @@ class repo_system(repo_generic):
         self.handle.appdata = self
         pool.installed = self.handle
         print "rpm database:",
-        self['cookie'] = calc_cookie_file("/var/lib/rpm/Packages")
+        self['cookie'] = self.calc_cookie_file("/var/lib/rpm/Packages")
         if self.usecachedrepo(None):
             print "cached"
             return True
@@ -888,12 +896,8 @@ if cmd == 'install' or cmd == 'erase' or cmd == 'up' or cmd == 'dup' or cmd == '
                     sys.stdout.flush()
                     continue
                         
-            if repo.type == 'susetags':
-                datadir = repo.handle.lookup_str(solv.SOLVID_META, solv.SUSETAGS_DATADIR)
-                if not datadir:
-                    datadir = 'suse'
-                location = datadir + '/' + location
             chksum = p.lookup_checksum(solv.SOLVABLE_CHECKSUM)
+            location = repo.packagespath() + location
             f = repo.download(location, False, chksum)
             if not f:
                 sys.exit("\n%s: %s not found in repository" % (repo.name, location))
index 250f134..1d7369d 100755 (executable)
@@ -43,7 +43,7 @@ class Repo_generic
     chksum = Solv::Chksum.new(Solv::REPOKEY_TYPE_SHA256)
     chksum.add("1.1")
     chksum.add(cookie)
-    chksum.add_fstat(f.fileno) if f
+    chksum.add_fstat(f.fileno)
     extcookie = chksum.raw()
     extcookie[0] = 1 if extcookie[0] == 0
     return extcookie
@@ -115,12 +115,6 @@ class Repo_generic
     end
   end
 
-  def download_location(location, chksum)
-    f = download(location, false, chksum)
-    abort("\n#{@name}: #{location} not found in repository\n") unless f
-    return f
-  end
-
   def usecachedrepo(ext, mark = false)
     cookie = ext ? @extcookie : @cookie
     begin
@@ -213,6 +207,10 @@ class Repo_generic
     repodata.internalize()
     writecachedrepo(nil, repodata)
   end
+
+  def packagespath()
+    return ''
+  end
 end
 
 class Repo_rpmmd < Repo_generic
@@ -458,12 +456,11 @@ class Repo_susetags < Repo_generic
     return true
   end
 
-  def download_location(location, chksum)
+  def packagespath()
     datadir = @handle.lookup_str(Solv::SOLVID_META, Solv::SUSETAGS_DATADIR)
     datadir = "suse" unless datadir
-    super("#{datadir}/#{location}", chksum)
+    return datadir + '/'
   end
-
 end
 
 class Repo_unknown < Repo_generic
@@ -728,8 +725,10 @@ if cmd == 'install' || cmd == 'erase' || cmd == 'up' || cmd == 'dup' || cmd == '
       repo = p.repo.appdata
       location, medianr = p.lookup_location()
       next unless location
+      location = repo.packagespath + location
       chksum = p.lookup_checksum(Solv::SOLVABLE_CHECKSUM)
-      f = repo.download_location(location, chksum)
+      f = repo.download(location, false, chksum)
+      abort("\n#{@name}: #{location} not found in repository\n") unless f
       newpkgsfp[p.id] = f
       print "."
       STDOUT.flush()