[Date Prev][Date Next] [Thread Prev][Thread Next] [Date Index] [Thread Index]

Re: how to use debget?



On Tue, Jul 02, 2002 at 08:21:24PM +0200, Andreas Rottmann wrote:

> Can we get rid of it then? Its functionality seems mostly superceeded
> by apt-get, from what i can tell...

For source packages, yes, but not for binary packages.  Here's what I have
been using; it's far from perfect, but it gets the job done in most cases.

-- 
 - mdz
#!/usr/bin/python

#
# debget - fetch a Debian binary package
#
# Matt Zimmerman <mdz@debian.org>
#

from apt_pkg import *
import sys
import os
from urllib2 import *
from urlparse import *
import time

# Configure mirror here
mirror = 'http://debian'

def usage(exitcode):
    sys.stderr.write("debget <package>\n")
    sys.exit(exitcode)

if len(sys.argv) < 2:
    usage(1)

InitConfig()
InitSystem()

cache = GetCache()
pkgrecords = GetPkgRecords(cache)

package = cache[sys.argv[1]]
if package == None:
    sys.stderr.write("No such package found\n")
    sys.exit(1)

versionlist = package.VersionList

if versionlist == None or len(versionlist) == 0:
    sys.stderr.write("No versions available\n")
    sys.exit(1)

filelist = versionlist[0].FileList

pkgrecords.Lookup(filelist[0])
filename = pkgrecords.FileName

outfilename = os.path.basename(filename)
outfile = open(outfilename, 'w')
url = urljoin(mirror,filename)
infile = urlopen(url)

size = None
if infile.info().has_key('Content-Length'):
    size = int(infile.info()['Content-Length'])

bufsize = 8192
copied = 0
progress_update = 0
while 1:
    buf = infile.read(bufsize)
    if buf == '':
        break
    outfile.write(buf)
    copied += len(buf)

    if (time.time() - progress_update) >= 0.25:
        percent = copied * 100 / size
        if size:
            print '\r[%s %s/%s %s%%]' % (outfilename, copied, size, percent),
        else:
            print '\r[%s %s]' % (outfilename,copied),

        sys.stdout.flush()
        progress_update = time.time()
    
print

Reply to: