stack-kicker 0.0.5 → 0.0.6

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,5 +1,5 @@
1
1
  module Stack
2
2
  module Kicker
3
- VERSION = "0.0.5"
3
+ VERSION = "0.0.6"
4
4
  end
5
5
  end
@@ -0,0 +1,118 @@
1
+ #!/usr/bin/env python
2
+ # largely taken from python examples
3
+ # http://docs.python.org/library/email-examples.html
4
+
5
+ import os
6
+ import sys
7
+ import smtplib
8
+ # For guessing MIME type based on file name extension
9
+ import mimetypes
10
+
11
+ from email import encoders
12
+ from email.message import Message
13
+ from email.mime.base import MIMEBase
14
+ from email.mime.multipart import MIMEMultipart
15
+ from email.mime.text import MIMEText
16
+ from optparse import OptionParser
17
+ import gzip
18
+
19
+ from base64 import b64encode
20
+
21
+ COMMASPACE = ', '
22
+
23
+ starts_with_mappings={
24
+ '#include' : 'text/x-include-url',
25
+ '#!' : 'text/x-shellscript',
26
+ '#cloud-config' : 'text/cloud-config',
27
+ '#upstart-job' : 'text/upstart-job',
28
+ '#part-handler' : 'text/part-handler',
29
+ '#cloud-boothook' : 'text/cloud-boothook'
30
+ }
31
+
32
+ def get_type(fname,deftype):
33
+ f = file(fname,"rb")
34
+ line = f.readline()
35
+ f.close()
36
+ rtype = deftype
37
+ for str,mtype in starts_with_mappings.items():
38
+ if line.startswith(str):
39
+ rtype = mtype
40
+ break
41
+ return(rtype)
42
+
43
+ def main():
44
+ outer = MIMEMultipart()
45
+ #outer['Subject'] = 'Contents of directory %s' % os.path.abspath(directory)
46
+ #outer['To'] = COMMASPACE.join(opts.recipients)
47
+ #outer['From'] = opts.sender
48
+ #outer.preamble = 'You will not see this in a MIME-aware mail reader.\n'
49
+
50
+ parser = OptionParser()
51
+
52
+ parser.add_option("-o", "--output", dest="output",
53
+ help="write output to FILE [default %default]", metavar="FILE",
54
+ default="-")
55
+ parser.add_option("-z", "--gzip", dest="compress", action="store_true",
56
+ help="compress output", default=False)
57
+ parser.add_option("-d", "--default", dest="deftype",
58
+ help="default mime type [default %default]", default="text/plain")
59
+ parser.add_option("--delim", dest="delim",
60
+ help="delimiter [default %default]", default=":")
61
+ parser.add_option("-b", "--base64", dest="base64", action="store_true",
62
+ help="encode content base64", default=False)
63
+
64
+ (options, args) = parser.parse_args()
65
+
66
+ if (len(args)) < 1:
67
+ parser.error("Must give file list see '--help'")
68
+
69
+ for arg in args:
70
+ t = arg.split(options.delim, 1)
71
+ path=t[0]
72
+ if len(t) > 1:
73
+ mtype = t[1]
74
+ else:
75
+ mtype = get_type(path,options.deftype)
76
+
77
+ maintype, subtype = mtype.split('/', 1)
78
+ if maintype == 'text':
79
+ fp = open(path)
80
+ # Note: we should handle calculating the charset
81
+ msg = MIMEText(fp.read(), _subtype=subtype)
82
+ fp.close()
83
+ else:
84
+ fp = open(path, 'rb')
85
+ msg = MIMEBase(maintype, subtype)
86
+ msg.set_payload(fp.read())
87
+ fp.close()
88
+ # Encode the payload using Base64
89
+ encoders.encode_base64(msg)
90
+
91
+ # Set the filename parameter
92
+ msg.add_header('Content-Disposition', 'attachment',
93
+ filename=os.path.basename(path))
94
+
95
+ outer.attach(msg)
96
+
97
+ if options.output is "-":
98
+ ofile = sys.stdout
99
+ else:
100
+ ofile = file(options.output,"wb")
101
+
102
+ if options.base64:
103
+ output = b64encode(outer.as_string())
104
+ else:
105
+ output = outer.as_string()
106
+
107
+ if options.compress:
108
+ gfile = gzip.GzipFile(fileobj=ofile, filename = options.output )
109
+ gfile.write(output)
110
+ gfile.close()
111
+ else:
112
+ ofile.write(output)
113
+
114
+ ofile.close()
115
+
116
+ if __name__ == '__main__':
117
+ main()
118
+
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: stack-kicker
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.5
4
+ version: 0.0.6
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -130,6 +130,7 @@ files:
130
130
  - lib/stack-kicker.rb
131
131
  - lib/stack-kicker/version.rb
132
132
  - lib/stack.rb
133
+ - lib/write-mime-multipart
133
134
  - stack-kicker.gemspec
134
135
  - test/tc_something.rb
135
136
  homepage: https://github.com/simonmcc/stack-kicker