winfreed.py: rename 'package' field to 'package_name'
[winfreed.git] / winfreed.py
1 #!/usr/bin/env python
2 #
3 # winfreed - download a selection of Free Software for MS Windows.
4 #
5 # Copyright (C) 2011  Antonio Ospite <ospite@studenti.unina.it>
6 #
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.
11 #
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.
16 #
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/>.
19
20 import os
21 import sys
22 import glob
23 import json
24 import urllib2
25 from progressbar import Bar, ETA, FileTransferSpeed, Percentage, ProgressBar
26
27 # TODO make OUTPUT_DIR and LANGCODE configurable from command line
28 OUTPUT_DIR = 'downloads'
29 LANGCODE = 'en-US'
30
31 # TODO PKG_DIR in $(datadir) or something like that for python projects
32 PKG_DIR = 'pkgs'
33 CHUNK_SIZE = 8192
34
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:
39             pkg = json.load(f)
40             basename = os.path.basename(json_file)
41             package_name = os.path.splitext(basename)[0]
42             pkg['package_name'] = package_name
43             cb(pkg)
44             f.close()
45
46 def show(pkg):
47     print 'Package:  ', pkg['package_name']
48     print 'Program:  ', pkg['name']
49     print 'Homepage: ', pkg['homepage']
50     print
51
52 def download(pkg):
53     # the "%s" in URLs are meant to be replaced with LANGCODE
54     try:
55         url = pkg['URL'] % LANGCODE
56     except:
57         url = pkg['URL']
58         pass
59
60     response = urllib2.urlopen(url)
61     filename = urllib2.unquote(os.path.basename(response.geturl()))
62     if filename == "":
63         sys.stderr.write("Debug (%s): filename: %s url: %s\n" %(pkg['package_name'], filename, response.geturl()))
64         return
65
66     destfile = os.path.join(OUTPUT_DIR, filename)
67     if os.path.exists(destfile):
68         sys.stderr.write("Warning (%s): %s exists!\n" % (pkg['package_name'], destfile))
69         return
70
71     outfile = open(destfile, mode='w')
72
73     total_size = response.info().getheader('Content-Length').strip()
74     total_size = int(total_size)
75
76     widgets = [pkg['name'], ' ', Percentage(), ' ', Bar(marker='=', left='[', right=']'),
77                ' ', ETA(), ' ', FileTransferSpeed()]
78     pbar = ProgressBar(widgets=widgets, maxval=total_size).start()
79
80     bytes_so_far = 0
81     while 1:
82         chunk = response.read(CHUNK_SIZE)
83         if not chunk:
84             break
85
86         bytes_so_far += len(chunk)
87         outfile.write(chunk)
88         pbar.update(bytes_so_far)
89     pbar.finish()
90
91 def show_all():
92     process_all(PKG_DIR, show)
93
94 def download_all():
95     if os.path.exists(OUTPUT_DIR) == False:
96         os.mkdir(OUTPUT_DIR, 0755)
97
98     process_all(PKG_DIR, download)
99
100 def usage():
101     usage = "winfreed - download a selection of Free Software for MS Windows.\n\n"
102     usage += "usage: %s <COMMAND>\n\n" % sys.argv[0]
103     usage += "COMMANDS:\n"
104     usage += "\tshow        Show info about all the available packages\n"
105     usage += "\tdownload    Download all the packages\n"
106     print usage
107
108 if __name__ == "__main__":
109
110     if len(sys.argv) < 2:
111         usage()
112         sys.exit(1)
113
114     if sys.argv[1] == 'download':
115         download_all()
116     elif sys.argv[1] == 'show':
117         show_all()
118     else:
119         usage()
120         sys.exit(1)
121     
122     sys.exit(0)