Mercurial > libervia-backend
annotate frontends/src/jp/file.py @ 815:f8d534ed1d1e
jp: added missing license headers
author | Goffi <goffi@goffi.org> |
---|---|
date | Wed, 05 Feb 2014 14:52:38 +0100 |
parents | 59c7bc51c323 |
children |
rev | line source |
---|---|
815 | 1 #! /usr/bin/python |
2 # -*- coding: utf-8 -*- | |
3 | |
4 # jp: a SAT command line tool | |
5 # Copyright (C) 2009, 2010, 2011, 2012, 2013, 2014 Jérôme Poisson (goffi@goffi.org) | |
6 | |
7 # This program is free software: you can redistribute it and/or modify | |
8 # it under the terms of the GNU Affero 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 Affero General Public License for more details. | |
16 | |
17 # You should have received a copy of the GNU Affero General Public License | |
18 # along with this program. If not, see <http://www.gnu.org/licenses/>. | |
19 | |
814 | 20 from logging import debug, info, error, warning |
0 | 21 |
814 | 22 import base |
23 import sys | |
24 import os | |
25 import os.path | |
26 import tarfile | |
27 #import tempfile | |
28 from os.path import abspath, basename, dirname | |
771 | 29 from sat.core.i18n import _ |
402
f03688bdb858
jp: use with statement to open fifo
Goffi <goffi@goffi.org>
parents:
401
diff
changeset
|
30 |
814 | 31 file_parser = base.subparser.add_parser('file', |
32 help = "File managment") | |
0 | 33 |
814 | 34 file_parser.add_argument("-p", "--profile", action="store", type=str, default='@DEFAULT@', |
35 help=_("Use PROFILE profile key (default: %(default)s)")) | |
36 file_subparser = file_parser.add_subparsers() | |
37 file_send = file_subparser.add_parser('send', | |
38 help = "Send a file to a contact") | |
39 file_send.add_argument("file", type=str, nargs = '*', | |
40 help=_("A list of file")) | |
41 file_send.add_argument("jid", type=str, | |
42 help=_("The destination jid")) | |
43 file_send.add_argument("-b", "--bz2", action="store_true", default=False, | |
44 help=_("Make a bzip2 tarball")) | |
0 | 45 |
814 | 46 file_send.set_defaults(func=lambda args : FileSend(args.profile, args.jid, args.file, args.bz2).go()) |
47 | |
0 | 48 |
814 | 49 file_recv = file_subparser.add_parser('recv', |
50 help = "Receive a file to a contact") | |
51 file_recv.add_argument("jids", type=str, nargs="*", | |
52 help=_("A list of destination jids")) | |
53 file_recv.add_argument("-m", "--multiple", action="store_true", default=False, | |
54 help=_("Wait for a file to be sent by a contact")) | |
55 file_recv.add_argument("-f", "--force", action="store_true", default=False, | |
56 help=_("Force overwritting of existing files")) | |
0 | 57 |
814 | 58 file_recv.set_defaults(func=lambda args : FileRecv(args.profile, |
59 args.jids, | |
60 args.multiple, | |
61 args.force).go()) | |
0 | 62 |
63 | |
64 | |
814 | 65 class FileSend(base.JPWithProfile): |
66 def __init__(self, profile, dest_jid, files, bz2): | |
67 base.JPWithProfile.__init__(self,profile) | |
68 self.dest_jid = dest_jid | |
69 self.files = files | |
70 self.bz2 = bz2 | |
401
b2caa2615c4c
jp roster name manegement + Pipe transfer
Goffi <goffi@goffi.org>
parents:
393
diff
changeset
|
71 |
0 | 72 def send_files(self): |
73 """Send files to jabber contact""" | |
74 for file in self.files: | |
75 if not os.path.exists(file): | |
538
2c4016921403
core, frontends, bridgen plugins: fixed methods which were unproperly managing multi-profiles
Goffi <goffi@goffi.org>
parents:
493
diff
changeset
|
76 error (_(u"File [%s] doesn't exist !") % file) |
0 | 77 exit(1) |
814 | 78 if not self.bz2 and os.path.isdir(file): |
70 | 79 error (_("[%s] is a dir ! Please send files inside or use compression") % file) |
0 | 80 exit(1) |
81 | |
401
b2caa2615c4c
jp roster name manegement + Pipe transfer
Goffi <goffi@goffi.org>
parents:
393
diff
changeset
|
82 full_dest_jid = self._getFullJid(self.dest_jid) |
814 | 83 if self.bz2: |
0 | 84 tmpfile = (basename(self.files[0]) or basename(dirname(self.files[0])) ) + '.tar.bz2' #FIXME: tmp, need an algorithm to find a good name/path |
85 if os.path.exists(tmpfile): | |
70 | 86 error (_("tmp file (%s) already exists ! Please remove it"), tmpfile) |
0 | 87 exit(1) |
70 | 88 warning(_("bz2 is an experimental option at an early dev stage, use with caution")) |
0 | 89 #FIXME: check free space, writting perm, tmp dir, filename (watch for OS used) |
70 | 90 info(_("Starting compression, please wait...")) |
0 | 91 sys.stdout.flush() |
92 bz2=tarfile.open(tmpfile, "w:bz2") | |
93 for file in self.files: | |
70 | 94 info(_("Adding %s"), file) |
0 | 95 bz2.add(file) |
96 bz2.close() | |
70 | 97 info(_("OK !")) |
0 | 98 path = abspath(tmpfile) |
720 | 99 self.transfer_data = self.bridge.sendFile(full_dest_jid, path, {}, self.profile) |
0 | 100 else: |
101 for file in self.files: | |
102 path = abspath(file) | |
720 | 103 self.transfer_data = self.bridge.sendFile(full_dest_jid, path, {}, self.profile) #FIXME: show progress only for last transfer_id |
401
b2caa2615c4c
jp roster name manegement + Pipe transfer
Goffi <goffi@goffi.org>
parents:
393
diff
changeset
|
104 |
814 | 105 def run(self): |
106 self.send_files() | |
587
952322b1d490
Remove trailing whitespaces.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
572
diff
changeset
|
107 |
952322b1d490
Remove trailing whitespaces.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
572
diff
changeset
|
108 |
814 | 109 class FileRecv(base.JPAsk): |
110 def __init__(self, profile, dest_jids, multiple, force, progress = False): | |
111 base.JPAsk.__init__(self,profile, start_mainloop = True) | |
112 self._dest_jids = dest_jids | |
113 self.multiple = multiple | |
114 self.force = force | |
115 self.progress = progress | |
0 | 116 |
814 | 117 def dest_jids(self): |
118 return self._dest_jids | |
0 | 119 |
814 | 120 def confirm_type(self): |
121 return "FILE_TRANSFER" | |
122 | |
123 def ask(self, data): | |
124 answer_data = {} | |
125 answer_data["dest_path"] = os.getcwd()+'/'+data['filename'] | |
393 | 126 |
814 | 127 if self.force or not os.path.exists(answer_data["dest_path"]): |
128 self.answer(True, answer_data) | |
129 info(_("Accepted file [%(filename)s] from %(sender)s") % {'filename':data['filename'], 'sender':data['from']}) | |
130 # self.transfer_data = self.confirm_id # Used by progress | |
0 | 131 else: |
814 | 132 self.answer(False, answer_data) |
133 warning(_("Refused file [%(filename)s] from %(sender)s: a file with the same name already exist") % {'filename':data['filename'], 'sender':data['from']}) | |
0 | 134 |
135 | |
814 | 136 if not self.multiple and not self.progress: |
137 #we just accept one file | |
138 self.loop.quit() |