3 # winfreed - download a selection of Free Software for MS Windows.
5 # Copyright (C) 2011-2014 Antonio Ospite <ao2@ao2.it>
7 # This program is free software: you can redistribute it and/or modify
8 # it under the terms of the GNU General Public License as published by
9 # the Free Software Foundation, either version 3 of the License, or
10 # (at your option) any later version.
12 # This program is distributed in the hope that it will be useful,
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 # GNU General Public License for more details.
17 # You should have received a copy of the GNU General Public License
18 # along with this program. If not, see <http://www.gnu.org/licenses/>.
25 from progressbar import Bar, ETA, FileTransferSpeed, Percentage, ProgressBar
27 OUTPUT_DIR = 'downloads'
37 def get_pkg(pkg_file):
38 config = ConfigParser.SafeConfigParser({'language': LANGCODE})
39 with open(pkg_file, mode='r') as f:
41 pkg = dict(config.items('Package'))
42 basename = os.path.basename(pkg_file)
43 package_name = os.path.splitext(basename)[0]
44 pkg['package_name'] = package_name
51 def process_all(path, cb):
52 listing = sorted(glob.glob(os.path.join(path, '*.ini')))
53 for pkg_file in listing:
54 pkg = get_pkg(pkg_file)
56 sys.stderr.write("Error: cannot get a pkg for: %s\n" % pkg_file)
62 print 'Package: ', pkg['package_name']
63 print 'Program: ', pkg['name']
64 if pkg.has_key('version'):
65 print 'Version: ', pkg['version']
66 print 'Homepage: ', pkg['homepage']
70 def download_file(src_url, dest_dir):
72 response = urllib2.urlopen(src_url)
74 print 'Downloading from', src_url, 'failed.', e
77 # get the final URL after possible redirect have been followed
78 url = response.geturl()
82 # From http://paste.pound-python.org/show/9545/
83 # TODO: use a proper module to parse HTTP headers
84 if 'Content-Disposition' in response.info() and len(response.info()['Content-Disposition'].split('filename=')) > 1:
85 # If the response has Content-Disposition, we take file name from it
86 filename = response.info()['Content-Disposition'].split('filename=')[1].decode('utf-8')
87 if filename[0] == '"' or filename[0] == "'":
88 filename = urllib2.unquote(filename.split('"')[1])
90 filename = urllib2.unquote(url.split('/')[-1].decode('utf_8'))
93 sys.stderr.write("Debug (%s): filename: %s url: %s\n" % (pkg['package_name'], filename, url))
96 # TODO: Add some integrity verification of downloaded files (md5, sha256?)
98 destfile = os.path.join(dest_dir, filename)
99 if os.path.exists(destfile):
100 # TODO: check if the file is a full download from previous run,
101 # if not download again discarding the existing file?
102 sys.stderr.write("Warning: %s exists!\n" % destfile)
105 if 'Content-Length' not in response.info():
106 sys.stderr.write("Cannot get the size of the file at %s\n" % src_url)
109 total_size = response.info().getheader('Content-Length').strip()
110 total_size = int(total_size)
112 #widgets = [pkg['name'], ' ', Percentage(), ' ', Bar(marker='=', left='[', right=']'),
113 widgets = [filename, ' ', Percentage(), ' ', Bar(marker='=', left='[', right=']'),
114 ' ', ETA(), ' ', FileTransferSpeed()]
115 pbar = ProgressBar(widgets=widgets, maxval=total_size).start()
117 outfile = open(destfile, mode='w')
121 chunk = response.read(CHUNK_SIZE)
125 bytes_so_far += len(chunk)
127 pbar.update(bytes_so_far)
135 pkg_url = pkg['url_' + ARCHITECTURE]
138 download_file(pkg_url, OUTPUT_DIR)
142 process_all(PKG_DIR, show)
146 if os.path.exists(OUTPUT_DIR) == False:
147 os.mkdir(OUTPUT_DIR, 0755)
149 process_all(PKG_DIR, download)
153 usage = "winfreed - download a selection of Free Software for MS Windows.\n\n"
154 usage += "usage: %s <COMMAND>\n\n" % sys.argv[0]
155 usage += "COMMANDS:\n"
156 usage += "\tshow Show info about all the available packages\n"
157 usage += "\tdownload Download all the packages\n"
160 if __name__ == "__main__":
162 if len(sys.argv) < 2:
166 if sys.argv[1] == 'download':
167 if len(sys.argv) > 2:
168 pkg = get_pkg(sys.argv[2])
172 elif sys.argv[1] == 'show':