annotate frontends/jp/jp @ 0:c4bc297b82f0

sat: - first public release, initial commit
author goffi@necton2
date Sat, 29 Aug 2009 13:34:59 +0200
parents
children bb72c29f3432
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
goffi@necton2
parents:
diff changeset
1 #! /usr/bin/python
goffi@necton2
parents:
diff changeset
2 # -*- coding: utf-8 -*-
goffi@necton2
parents:
diff changeset
3
goffi@necton2
parents:
diff changeset
4 """
goffi@necton2
parents:
diff changeset
5 jp: a SAT command line tool
goffi@necton2
parents:
diff changeset
6 Copyright (C) 2009 Jérôme Poisson (goffi@goffi.org)
goffi@necton2
parents:
diff changeset
7
goffi@necton2
parents:
diff changeset
8 This program is free software: you can redistribute it and/or modify
goffi@necton2
parents:
diff changeset
9 it under the terms of the GNU General Public License as published by
goffi@necton2
parents:
diff changeset
10 the Free Software Foundation, either version 3 of the License, or
goffi@necton2
parents:
diff changeset
11 (at your option) any later version.
goffi@necton2
parents:
diff changeset
12
goffi@necton2
parents:
diff changeset
13 This program is distributed in the hope that it will be useful,
goffi@necton2
parents:
diff changeset
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
goffi@necton2
parents:
diff changeset
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
goffi@necton2
parents:
diff changeset
16 GNU General Public License for more details.
goffi@necton2
parents:
diff changeset
17
goffi@necton2
parents:
diff changeset
18 You should have received a copy of the GNU General Public License
goffi@necton2
parents:
diff changeset
19 along with this program. If not, see <http://www.gnu.org/licenses/>.
goffi@necton2
parents:
diff changeset
20 """
goffi@necton2
parents:
diff changeset
21
goffi@necton2
parents:
diff changeset
22 #consts
goffi@necton2
parents:
diff changeset
23 name = "jp"
goffi@necton2
parents:
diff changeset
24 version = "0.0.1"
goffi@necton2
parents:
diff changeset
25 about = name+" v"+version+""" (c) Jérôme Poisson (aka Goffi) 2009
goffi@necton2
parents:
diff changeset
26
goffi@necton2
parents:
diff changeset
27 ---
goffi@necton2
parents:
diff changeset
28 """+name+""" Copyright (C) 2009 Jérôme Poisson (aka Goffi)
goffi@necton2
parents:
diff changeset
29 This program comes with ABSOLUTELY NO WARRANTY;
goffi@necton2
parents:
diff changeset
30 This is free software, and you are welcome to redistribute it
goffi@necton2
parents:
diff changeset
31 under certain conditions.
goffi@necton2
parents:
diff changeset
32 ---
goffi@necton2
parents:
diff changeset
33
goffi@necton2
parents:
diff changeset
34 This software is a command line tool for jabber
goffi@necton2
parents:
diff changeset
35 Get the latest version at http://www.goffi.org
goffi@necton2
parents:
diff changeset
36 """
goffi@necton2
parents:
diff changeset
37
goffi@necton2
parents:
diff changeset
38 global pbar_available
goffi@necton2
parents:
diff changeset
39 pbar_available = True #checked before using ProgressBar
goffi@necton2
parents:
diff changeset
40
goffi@necton2
parents:
diff changeset
41 ### logging ###
goffi@necton2
parents:
diff changeset
42 import logging
goffi@necton2
parents:
diff changeset
43 from logging import debug, info, error, warning
goffi@necton2
parents:
diff changeset
44 logging.basicConfig(level=logging.DEBUG,
goffi@necton2
parents:
diff changeset
45 format='%(message)s')
goffi@necton2
parents:
diff changeset
46 ###
goffi@necton2
parents:
diff changeset
47
goffi@necton2
parents:
diff changeset
48 import sys
goffi@necton2
parents:
diff changeset
49 import os
goffi@necton2
parents:
diff changeset
50 from os.path import abspath, basename, dirname
goffi@necton2
parents:
diff changeset
51 from optparse import OptionParser
goffi@necton2
parents:
diff changeset
52 import pdb
goffi@necton2
parents:
diff changeset
53 from tools.jid import JID
goffi@necton2
parents:
diff changeset
54 import gobject
goffi@necton2
parents:
diff changeset
55 from sat_bridge_frontend.DBus import DBusBridgeFrontend
goffi@necton2
parents:
diff changeset
56 import tarfile
goffi@necton2
parents:
diff changeset
57 try:
goffi@necton2
parents:
diff changeset
58 from progressbar import ProgressBar, Percentage, Bar, ETA, FileTransferSpeed
goffi@necton2
parents:
diff changeset
59 except ImportError, e:
goffi@necton2
parents:
diff changeset
60 info ('ProgressBar not available, please download it at http://pypi.python.org/pypi/progressbar')
goffi@necton2
parents:
diff changeset
61 info ('Progress bar deactivated\n--\n')
goffi@necton2
parents:
diff changeset
62 pbar_available=False
goffi@necton2
parents:
diff changeset
63
goffi@necton2
parents:
diff changeset
64
goffi@necton2
parents:
diff changeset
65
goffi@necton2
parents:
diff changeset
66
goffi@necton2
parents:
diff changeset
67 class JP():
goffi@necton2
parents:
diff changeset
68 def __init__(self):
goffi@necton2
parents:
diff changeset
69 self.bridge=DBusBridgeFrontend()
goffi@necton2
parents:
diff changeset
70 self.transfert_id = None
goffi@necton2
parents:
diff changeset
71
goffi@necton2
parents:
diff changeset
72 def check_options(self):
goffi@necton2
parents:
diff changeset
73 """Check command line options"""
goffi@necton2
parents:
diff changeset
74 usage="""
goffi@necton2
parents:
diff changeset
75 %prog [options] [FILE1 FILE2 ...] JID
goffi@necton2
parents:
diff changeset
76 %prog -w [options] [JID1 JID2 ...]
goffi@necton2
parents:
diff changeset
77
goffi@necton2
parents:
diff changeset
78 %prog --help for options list
goffi@necton2
parents:
diff changeset
79 """
goffi@necton2
parents:
diff changeset
80 parser = OptionParser(usage=usage,version=about)
goffi@necton2
parents:
diff changeset
81
goffi@necton2
parents:
diff changeset
82 parser.add_option("-b", "--bz2", action="store_true", default=False,
goffi@necton2
parents:
diff changeset
83 help="Make a bzip2 tarball")
goffi@necton2
parents:
diff changeset
84 parser.add_option("-w", "--wait-file", action="store_true", default=False,
goffi@necton2
parents:
diff changeset
85 help="Wait for a file to be sent by a contact")
goffi@necton2
parents:
diff changeset
86 parser.add_option("-m", "--multiple", action="store_true", default=False,
goffi@necton2
parents:
diff changeset
87 help="Accept multiple files (you'll have to stop manually)")
goffi@necton2
parents:
diff changeset
88 parser.add_option("-f", "--force", action="store_true", default=False,
goffi@necton2
parents:
diff changeset
89 help="Force overwritting of existing files")
goffi@necton2
parents:
diff changeset
90 parser.add_option("-p", "--progress", action="store_true", default=False,
goffi@necton2
parents:
diff changeset
91 help="Show progress bar")
goffi@necton2
parents:
diff changeset
92 parser.add_option("-s", "--separate", action="store_true", default=False,
goffi@necton2
parents:
diff changeset
93 help="Separate xmpp messages: send one message per line instead of one message alone.")
goffi@necton2
parents:
diff changeset
94 parser.add_option("-n", "--new-line", action="store_true", default=False,
goffi@necton2
parents:
diff changeset
95 help="Add a new line at the beginning of the input (usefull for ascii art ;))")
goffi@necton2
parents:
diff changeset
96
goffi@necton2
parents:
diff changeset
97 (self.options, args) = parser.parse_args()
goffi@necton2
parents:
diff changeset
98
goffi@necton2
parents:
diff changeset
99 if len(args) < 1 and not self.options.wait_file:
goffi@necton2
parents:
diff changeset
100 parser.error("You must specify the destination JID (Jabber ID)")
goffi@necton2
parents:
diff changeset
101
goffi@necton2
parents:
diff changeset
102 if self.options.wait_file:
goffi@necton2
parents:
diff changeset
103 #several jid
goffi@necton2
parents:
diff changeset
104 self.dest_jids = args
goffi@necton2
parents:
diff changeset
105 else:
goffi@necton2
parents:
diff changeset
106 #one dest_jid, other args are files
goffi@necton2
parents:
diff changeset
107 self.dest_jid = JID(args[-1])
goffi@necton2
parents:
diff changeset
108 if not self.dest_jid.is_valid:
goffi@necton2
parents:
diff changeset
109 error ("%s is not a valid JID !", self.dest_jid)
goffi@necton2
parents:
diff changeset
110 exit(1)
goffi@necton2
parents:
diff changeset
111 self.files = args[:-1]
goffi@necton2
parents:
diff changeset
112
goffi@necton2
parents:
diff changeset
113 if not pbar_available and self.options.progress:
goffi@necton2
parents:
diff changeset
114 self.options.progress = False
goffi@necton2
parents:
diff changeset
115 error ("Option progress is not available, deactivated.")
goffi@necton2
parents:
diff changeset
116
goffi@necton2
parents:
diff changeset
117 if self.options.progress or self.options.wait_file:
goffi@necton2
parents:
diff changeset
118 self.start_loop = True #We have to use loop for these options
goffi@necton2
parents:
diff changeset
119 else:
goffi@necton2
parents:
diff changeset
120 self.start_loop = False
goffi@necton2
parents:
diff changeset
121
goffi@necton2
parents:
diff changeset
122
goffi@necton2
parents:
diff changeset
123 return args
goffi@necton2
parents:
diff changeset
124
goffi@necton2
parents:
diff changeset
125 def check_jabber_status(self):
goffi@necton2
parents:
diff changeset
126 """Check that jabber status is allright"""
goffi@necton2
parents:
diff changeset
127 if not self.bridge.isConnected():
goffi@necton2
parents:
diff changeset
128 error("SAT is not conneted, please connect before using jp")
goffi@necton2
parents:
diff changeset
129 exit(1)
goffi@necton2
parents:
diff changeset
130
goffi@necton2
parents:
diff changeset
131
goffi@necton2
parents:
diff changeset
132 def send_stdin(self):
goffi@necton2
parents:
diff changeset
133 """Send incomming data on stdin to jabber contact"""
goffi@necton2
parents:
diff changeset
134 header = "\n" if self.options.new_line else ""
goffi@necton2
parents:
diff changeset
135
goffi@necton2
parents:
diff changeset
136 if self.options.separate: #we send stdin in several messages
goffi@necton2
parents:
diff changeset
137 if header:
goffi@necton2
parents:
diff changeset
138 self.bridge.sendMessage(self.dest_jid, header)
goffi@necton2
parents:
diff changeset
139 while (True):
goffi@necton2
parents:
diff changeset
140 line = sys.stdin.readline()
goffi@necton2
parents:
diff changeset
141 if not line:
goffi@necton2
parents:
diff changeset
142 break
goffi@necton2
parents:
diff changeset
143 self.bridge.sendMessage(self.dest_jid, line.replace("\n",""))
goffi@necton2
parents:
diff changeset
144 else:
goffi@necton2
parents:
diff changeset
145 self.bridge.sendMessage(self.dest_jid, header + "".join(sys.stdin.readlines()))
goffi@necton2
parents:
diff changeset
146
goffi@necton2
parents:
diff changeset
147 def send_files(self):
goffi@necton2
parents:
diff changeset
148 """Send files to jabber contact"""
goffi@necton2
parents:
diff changeset
149
goffi@necton2
parents:
diff changeset
150 for file in self.files:
goffi@necton2
parents:
diff changeset
151 if not os.path.exists(file):
goffi@necton2
parents:
diff changeset
152 error ("File [%s] doesn't exist !" % file)
goffi@necton2
parents:
diff changeset
153 exit(1)
goffi@necton2
parents:
diff changeset
154 if not self.options.bz2 and os.path.isdir(file):
goffi@necton2
parents:
diff changeset
155 error ("[%s] is a dir ! Please send files inside or use compression" % file)
goffi@necton2
parents:
diff changeset
156 exit(1)
goffi@necton2
parents:
diff changeset
157
goffi@necton2
parents:
diff changeset
158 if self.options.bz2:
goffi@necton2
parents:
diff changeset
159 tmpfile = (basename(self.files[0]) or basename(dirname(self.files[0])) ) + '.tar.bz2' #FIXME: tmp, need an algorithm to find a good name/path
goffi@necton2
parents:
diff changeset
160 if os.path.exists(tmpfile):
goffi@necton2
parents:
diff changeset
161 error ("tmp file (%s) already exists ! Please remove it", tmpfile)
goffi@necton2
parents:
diff changeset
162 exit(1)
goffi@necton2
parents:
diff changeset
163 warning("bz2 is an experimental option at an early dev stage, use with caution")
goffi@necton2
parents:
diff changeset
164 #FIXME: check free space, writting perm, tmp dir, filename (watch for OS used)
goffi@necton2
parents:
diff changeset
165 info("Starting compression, please wait...")
goffi@necton2
parents:
diff changeset
166 sys.stdout.flush()
goffi@necton2
parents:
diff changeset
167 bz2=tarfile.open(tmpfile, "w:bz2")
goffi@necton2
parents:
diff changeset
168 for file in self.files:
goffi@necton2
parents:
diff changeset
169 info("Adding %s", file)
goffi@necton2
parents:
diff changeset
170 bz2.add(file)
goffi@necton2
parents:
diff changeset
171 bz2.close()
goffi@necton2
parents:
diff changeset
172 info("OK !")
goffi@necton2
parents:
diff changeset
173 path = abspath(tmpfile)
goffi@necton2
parents:
diff changeset
174 self.transfert_id = self.bridge.sendFile(self.dest_jid, path)
goffi@necton2
parents:
diff changeset
175 else:
goffi@necton2
parents:
diff changeset
176 for file in self.files:
goffi@necton2
parents:
diff changeset
177 path = abspath(file)
goffi@necton2
parents:
diff changeset
178 self.transfert_id = self.bridge.sendFile(self.dest_jid, path) #FIXME: show progress only for last transfert_id
goffi@necton2
parents:
diff changeset
179
goffi@necton2
parents:
diff changeset
180 #TODO: manage ProgressBar
goffi@necton2
parents:
diff changeset
181
goffi@necton2
parents:
diff changeset
182 def askConfirmation(self, type, id, data):
goffi@necton2
parents:
diff changeset
183 """CB used for file transfert, accept files depending on parameters"""
goffi@necton2
parents:
diff changeset
184 answer_data={}
goffi@necton2
parents:
diff changeset
185 if type == "FILE_TRANSFERT":
goffi@necton2
parents:
diff changeset
186 if self.dest_jids and not data['from'] in self.dest_jids:
goffi@necton2
parents:
diff changeset
187 return #file is not sent by a filtered jid
goffi@necton2
parents:
diff changeset
188
goffi@necton2
parents:
diff changeset
189 answer_data["dest_path"] = os.getcwd()+'/'+data['filename']
goffi@necton2
parents:
diff changeset
190
goffi@necton2
parents:
diff changeset
191 if self.options.force or not os.path.exists(answer_data["dest_path"]):
goffi@necton2
parents:
diff changeset
192 self.bridge.confirmationAnswer(id, True, answer_data)
goffi@necton2
parents:
diff changeset
193 info("Accepted file [%s] from %s", data['filename'], data['from'])
goffi@necton2
parents:
diff changeset
194 self.transfert_id = id
goffi@necton2
parents:
diff changeset
195 else:
goffi@necton2
parents:
diff changeset
196 self.bridge.confirmationAnswer(id, False, answer_data)
goffi@necton2
parents:
diff changeset
197 warning("Refused file [%s] from %s: a file with the same name already exist", data['filename'], data['from'])
goffi@necton2
parents:
diff changeset
198
goffi@necton2
parents:
diff changeset
199
goffi@necton2
parents:
diff changeset
200 if not self.options.multiple and not self.options.progress:
goffi@necton2
parents:
diff changeset
201 #we just accept one file
goffi@necton2
parents:
diff changeset
202 self.loop.quit()
goffi@necton2
parents:
diff changeset
203
goffi@necton2
parents:
diff changeset
204
goffi@necton2
parents:
diff changeset
205 def wait_file(self):
goffi@necton2
parents:
diff changeset
206 """Wait for a file and write it on local dir"""
goffi@necton2
parents:
diff changeset
207 self.bridge.register("askConfirmation", self.askConfirmation, "request")
goffi@necton2
parents:
diff changeset
208
goffi@necton2
parents:
diff changeset
209 def progressCB(self):
goffi@necton2
parents:
diff changeset
210 if self.transfert_id:
goffi@necton2
parents:
diff changeset
211 data = self.bridge.getProgress(self.transfert_id)
goffi@necton2
parents:
diff changeset
212 if data:
goffi@necton2
parents:
diff changeset
213 if not data['position']:
goffi@necton2
parents:
diff changeset
214 data['position'] = '0'
goffi@necton2
parents:
diff changeset
215 if not self.pbar:
goffi@necton2
parents:
diff changeset
216 #first answer, we must construct the bar
goffi@necton2
parents:
diff changeset
217 self.pbar = ProgressBar(int(data['size']),["Progress: ",Percentage()," ",Bar()," ",FileTransferSpeed()," ",ETA()])
goffi@necton2
parents:
diff changeset
218 self.pbar.start()
goffi@necton2
parents:
diff changeset
219
goffi@necton2
parents:
diff changeset
220 self.pbar.update(int(data['position']))
goffi@necton2
parents:
diff changeset
221 elif self.pbar:
goffi@necton2
parents:
diff changeset
222 self.pbar.finish()
goffi@necton2
parents:
diff changeset
223 if not self.options.multiple:
goffi@necton2
parents:
diff changeset
224 self.loop.quit()
goffi@necton2
parents:
diff changeset
225 return False
goffi@necton2
parents:
diff changeset
226
goffi@necton2
parents:
diff changeset
227 return True
goffi@necton2
parents:
diff changeset
228
goffi@necton2
parents:
diff changeset
229 def go(self):
goffi@necton2
parents:
diff changeset
230 self.check_options()
goffi@necton2
parents:
diff changeset
231 self.check_jabber_status()
goffi@necton2
parents:
diff changeset
232 if self.options.wait_file:
goffi@necton2
parents:
diff changeset
233 self.wait_file()
goffi@necton2
parents:
diff changeset
234 else:
goffi@necton2
parents:
diff changeset
235 if not self.files: #we send message only if there are no files to send
goffi@necton2
parents:
diff changeset
236 self.send_stdin()
goffi@necton2
parents:
diff changeset
237 else:
goffi@necton2
parents:
diff changeset
238 self.send_files()
goffi@necton2
parents:
diff changeset
239
goffi@necton2
parents:
diff changeset
240 if self.start_loop:
goffi@necton2
parents:
diff changeset
241 self.loop = gobject.MainLoop()
goffi@necton2
parents:
diff changeset
242 if self.options.progress:
goffi@necton2
parents:
diff changeset
243 self.pbar = None
goffi@necton2
parents:
diff changeset
244 gobject.timeout_add(10, self.progressCB)
goffi@necton2
parents:
diff changeset
245 try:
goffi@necton2
parents:
diff changeset
246 self.loop.run()
goffi@necton2
parents:
diff changeset
247 except KeyboardInterrupt:
goffi@necton2
parents:
diff changeset
248 info("User interruption: good bye")
goffi@necton2
parents:
diff changeset
249
goffi@necton2
parents:
diff changeset
250
goffi@necton2
parents:
diff changeset
251 if __name__ == "__main__":
goffi@necton2
parents:
diff changeset
252 jp = JP()
goffi@necton2
parents:
diff changeset
253 jp.go()