site: end of line message, adesklets is not dead.
[adesklets.git] / utils / adesklets_submit
blob83a321cd14ba8239242c45db68d34772e7290ba4
1 #! /usr/bin/env python
2 """
3 -------------------------------------------------------------------------------
4 Copyright (C) 2005, 2006 Sylvain Fourmanoit
6 Released under the GPL, version 2.
8 Permission is hereby granted, free of charge, to any person obtaining a copy
9 of this software and associated documentation files (the "Software"), to
10 deal in the Software without restriction, including without limitation the
11 rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
12 sell copies of the Software, and to permit persons to whom the Software is
13 furnished to do so, subject to the following conditions:
15 The above copyright notice and this permission notice shall be included in
16 all copies of the Software and its documentation and acknowledgment shall be
17 given in the documentation and software packages that this Software was
18 used.
20 THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
21 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
22 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
23 THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
24 IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
25 CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
26 -------------------------------------------------------------------------------
27 This is the adesklet's desklet submission script. Have a look at adesklets
28 documentation for details.
29 """
30 #------------------------------------------------------------------------------
31 import getopt
32 import urllib
33 import smtplib
34 from email import Encoders
35 from email.MIMEMultipart import MIMEMultipart
36 from email.MIMEText import MIMEText
37 from email.MIMEImage import MIMEImage
38 from email.MIMEBase import MIMEBase
39 from pprint import PrettyPrinter
40 from sys import argv, exit, stderr
41 from os import getenv
42 from os.path import join
44 #------------------------------------------------------------------------------
45 # Print usage function
47 def usage(msg=None):
48 print 'Usage: %s [--help] [--send] desklet_name' % argv[0],"""
49 desklet_name - name of the desklet
51 See official adesklets documentation for usage details
52 """
53 if msg: print 'Error:', msg
54 exit(1)
56 #------------------------------------------------------------------------------
57 # Read in the configuration
59 config_name=join(getenv('HOME'),'.adesklets_submit')
60 try:
61 f = file(config_name,'r')
62 except IOError:
63 usage("Could not read '%s' configuration file" % config_name)
64 config={}; exec f in config; del config['__builtins__']
65 f.close()
67 #------------------------------------------------------------------------------
68 # Determine what to do by looking at the command line
70 try:
71 opts, args = getopt.getopt(argv[1:],'',['send', 'help'])
72 opts=dict(opts)
73 except getopt.GetoptError,(errno, strerror):
74 usage(errno)
75 if opts.has_key('--help'): usage()
76 if len(args)==0: usage('desklet name not given')
78 #------------------------------------------------------------------------------
79 # Verify we are looking at an existing package
81 if not config['desklets'].has_key(args[0]):
82 raise KeyError("Given desklet entry '%s' does not exist" % args[0])
84 #------------------------------------------------------------------------------
85 # Get the three files for thumbnail, screenshot and download
87 def getfile(key):
88 try:
89 f=file(join(getenv('HOME'),
90 config['desklets'][args[0]][key]),'r')
91 except:
92 try:
93 f=urllib.urlopen(config['desklets'][args[0]][key])
94 except:
95 f = None
96 return f
97 files = dict([(name,getfile(name)) for name in
98 ['thumbnail','screenshot','download']])
99 if not files['download']:
100 raise IOError('Package file could not be found.')
102 #------------------------------------------------------------------------------
103 # Build the email body
105 # First, the enveloppe
107 msg = MIMEMultipart()
108 msg['Subject'] = 'New desklet: ' + args[0]
109 msg['From'] = '%s <%s>' % (config['info']['author_name'],
110 config['info']['author_email'])
111 recip = ['adesklets@mailworks.org'] # Do not use this address
112 # for anything else, it is useless:
113 # everything not conforming to this
114 # will be dropped without human
115 # intervention.
116 msg['To'] = 'S.Fourmanoit <%s>' % recip[0]
117 if config['cc_to_self']:
118 msg['Cc'] = '%s <%s>' % (config['info']['author_name'],
119 config['info']['author_email'])
120 recip.append(config['info']['author_email'])
121 msg.epilogue = ''
123 # Then the textual message
125 config['desklets'][args[0]]['name']=args[0]
126 pp = PrettyPrinter(width=70)
127 msg.attach(MIMEText('%s\n\n%s' % (
128 'info = ' + pp.pformat(config['info']),
129 'desklet = ' + pp.pformat(config['desklets'][args[0]]))))
131 # Then the images
133 for image in [files['thumbnail'], files['screenshot']]:
134 if image.__class__==file:
135 msg.attach(MIMEImage(image.read()))
137 # Finally, the desklet tarball
139 if files['download'].__class__==file:
140 payload = MIMEBase('application','octet-stream')
141 payload.set_payload(files['download'].read())
142 Encoders.encode_base64(payload)
143 msg.attach(payload)
144 else:
145 if not files['download']:
146 raise IOError('Could not retrieve desklet package')
148 #------------------------------------------------------------------------------
149 # Last step: we choose between sending the message, or keeping it
150 # for ourselves, depending of user choice and size.
152 size=len(msg.as_string())/1024
153 if size>300:
154 raise IOError('Message is %d KB - too big.' % size +
155 'Attach content as url.' % size)
156 if not opts.has_key('--send'):
157 print msg.as_string()
158 else:
159 s = None
160 if config.has_key('smtp_pass') and config['smtp_pass'] is not None:
161 s=smtplib.SMTP(config['smtp_host'],config['smtp_port'])
162 s.login(config['smtp_user'],config['smtp_pass'])
163 else:
164 s=smtplib.SMTP(config['smtp_host'],config['smtp_port'])
166 s.sendmail(config['info']['author_email'],
167 recip,
168 msg.as_string())
169 s.close()