~ubuntu-branches/ubuntu/saucy/aptoncd/saucy-proposed

« back to all changes in this revision

Viewing changes to download.py

  • Committer: Bazaar Package Importer
  • Author(s): Fabrice Coutadeur
  • Date: 2009-08-17 22:04:26 UTC
  • mfrom: (1.1.1 upstream) (0.1.11 jaunty)
  • Revision ID: james.westby@ubuntu.com-20090817220426-bhxr3a21ff6y8edm
Tags: 0.1.98+bzr109-0.1
* Non-maintainer upload
* New upstream release (Closes: #452205, #423480, #433915, #541047, #427003,
  #493647, #484636)
* debian/control: 
  - Changed python build dependencies to Build-Depends-Indep
  - Moved url from Description to Homepage
  - Changed Standards-Version to 3.8.2
  - Changed Maintainer to myself
  - Deleted dependency on deprecated mkisofs 
  - Deleted recommend of nautilus-cd-burner
* debian/copyright: Changed (C) to © to make lintian happy
* debian/rules: 
  - deleted deprecated dh_desktop
  - added get-orig-source target to get the latest source from lp
* data/aptoncd.desktop.in: Fixed error and deprecated values in Desktop file

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
#!/usr/bin/env python
2
 
# -*- coding: iso-8859-15 -*-
3
 
######################################################
4
 
##
5
 
#  This program is free software; you can redistribute it and/or modify
6
 
#  it under the terms of the GNU General Public License as published
7
 
#  by the Free Software Foundation; version 2 only.
8
 
#
9
 
#  This program is distributed in the hope that it will be useful,
10
 
#  but WITHOUT ANY WARRANTY; without even the implied warranty of
11
 
#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12
 
#  GNU General Public License for more details.
13
 
##
14
 
######################################################
15
 
## 
16
 
#  Project: AptOnCd
17
 
#  File: download.py
18
 
#  Author: Alfredo Jr. <junix>
19
 
#  Creation: 28/10/2006
20
 
#  Changed: 08/12/2006 by Laudeci Oliveira <laudeci@gmail.com>
21
 
#  Purpose: Download Class
22
 
##
23
 
######################################################
24
 
 
25
 
import urllib2
26
 
import gtk
27
 
import os
28
 
import sys
29
 
import gobject
30
 
 
31
 
(PACKAGE, VERSION, FILENAME, SIZE, DEBFILENAME,REMOTEFILEPATH,SECTION) = range(7)
32
 
 
33
 
def getRemoteFileSize(url):
34
 
    try:
35
 
        instream=urllib2.urlopen(url, None)
36
 
        return instream.info().getheader("Content-Length")
37
 
    except:
38
 
        return 0
39
 
        
40
 
class Download(gobject.GObject):
41
 
    
42
 
    __gsignals__ = dict(download_status=(gobject.SIGNAL_RUN_FIRST, gobject.TYPE_NONE, (gobject.TYPE_STRING, gobject.TYPE_FLOAT,gobject.TYPE_FLOAT, gobject.TYPE_INT)))
43
 
 
44
 
    def __init__(self, url = "", filepath = ""):
45
 
        gobject.GObject.__init__(self)
46
 
        self.url = url
47
 
        self.FileName = ""
48
 
        self.downSize = 1024
49
 
        self.Folder = filepath
50
 
        self.downloadSize = 0
51
 
        self.canceled = False
52
 
    
53
 
    def cancelDownload(self):
54
 
        self.canceled = True
55
 
        
56
 
    def getDownloadSize(self):
57
 
         return self.downloadSize
58
 
     
59
 
    def getURLName(self):
60
 
        name = "%s%s%s" % ( self.Folder, os.sep, self.url.split("/")[-1] )
61
 
        return name
62
 
        
63
 
    def createDownload(self):       
64
 
        instream=urllib2.urlopen(self.url, None)
65
 
        #self.initialtime = time.time()
66
 
        return (instream, instream.info().getheader("Content-Length"))
67
 
 
68
 
    def download(self, progressgui = False):
69
 
        downloaded = False
70
 
        if self.url == "":
71
 
            return False
72
 
 
73
 
        #try:
74
 
        outfile=open( self.getURLName(), "wb")
75
 
        url, length = self.createDownload()
76
 
        fileName=outfile.name.split(os.sep)[-1]
77
 
        self.FileName = fileName
78
 
        
79
 
        if not length:
80
 
            length = "?"
81
 
 
82
 
        if length!="?":
83
 
            length=float(length)
84
 
 
85
 
        bytesRead = 0.0
86
 
        
87
 
        while True:
88
 
                bytes = url.read(int(self.downSize))
89
 
                bytesRead+= len(bytes)
90
 
                fraction = float((100 * bytesRead) / length)
91
 
                outfile.write(bytes)
92
 
                if progressgui:
93
 
                    self.emit('download_status',fileName, length, len(bytes) , (fraction))
94
 
 
95
 
                if bytes == "":
96
 
                    outfile.close()
97
 
                    break
98
 
                if self.canceled: 
99
 
                    try:
100
 
                        outfile.close()
101
 
                        os.remove(self.getURLName())
102
 
                    except:
103
 
                        pass
104
 
                    break
105
 
 
106
 
        url.close()
107
 
        outfile.close()
108
 
        
109
 
        downloaded = True
110
 
        #except :
111
 
         #   print "Error downloading %s" % (self.url)
112
 
 
113
 
        return downloaded
114
 
gobject.type_register(Download)