3 # winfreed - download a selection of Free Software for MS Windows.
5 # Copyright (C) 2011 Antonio Ospite <ospite@studenti.unina.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 # TODO make OUTPUT_DIR and LANGCODE configurable from command line
28 OUTPUT_DIR = 'downloads'
31 # TODO PKG_DIR in $(datadir) or something like that for python projects
35 def process_all(path, cb):
36 listing = glob.glob(os.path.join(path, '*.json'))
37 for json_file in listing:
38 with open(json_file, mode='r') as f:
40 basename = os.path.basename(json_file)
41 package_name = os.path.splitext(basename)[0]
42 entry['package'] = package_name
47 print 'Package: ', entry['package']
48 print 'Program: ', entry['name']
49 print 'Homepage: ', entry['homepage']
53 # the "%s" in URLs are meant to be replaced with LANGCODE
55 url = entry['URL'] % LANGCODE
60 response = urllib2.urlopen(url)
61 filename = urllib2.unquote(os.path.basename(response.geturl()))
62 destfile = os.path.join(OUTPUT_DIR, filename)
63 if os.path.exists(destfile):
64 sys.stderr.write("Warning: %s exists!\n" % destfile)
66 outfile = open(destfile, mode='w')
68 total_size = response.info().getheader('Content-Length').strip()
69 total_size = int(total_size)
71 widgets = [entry['name'], ' ', Percentage(), ' ', Bar(marker='=', left='[', right=']'),
72 ' ', ETA(), ' ', FileTransferSpeed()]
73 pbar = ProgressBar(widgets=widgets, maxval=total_size).start()
77 chunk = response.read(CHUNK_SIZE)
81 bytes_so_far += len(chunk)
83 pbar.update(bytes_so_far)
87 process_all(PKG_DIR, show)
90 if os.path.exists(OUTPUT_DIR) == False:
91 os.mkdir(OUTPUT_DIR, 0755)
93 process_all(PKG_DIR, download)
96 usage = "winfreed - download a selection of Free Software for MS Windows.\n\n"
97 usage += "usage: %s <COMMAND>\n\n" % sys.argv[0]
98 usage += "COMMANDS:\n"
99 usage += "\tshow Show info about all the available packages\n"
100 usage += "\tdownload Download all the packages\n"
103 if __name__ == "__main__":
105 if len(sys.argv) < 2:
109 if sys.argv[1] == 'download':
111 elif sys.argv[1] == 'show':