138 lines
5.6 KiB
Python
138 lines
5.6 KiB
Python
#! /usr/bin/python
|
|
# -*- coding: utf-8 -*-
|
|
|
|
# jp: a SAT command line tool
|
|
# Copyright (C) 2009, 2010, 2011, 2012, 2013, 2014 Jérôme Poisson (goffi@goffi.org)
|
|
|
|
# This program is free software: you can redistribute it and/or modify
|
|
# it under the terms of the GNU Affero General Public License as published by
|
|
# the Free Software Foundation, either version 3 of the License, or
|
|
# (at your option) any later version.
|
|
|
|
# This program is distributed in the hope that it will be useful,
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
# GNU Affero General Public License for more details.
|
|
|
|
# You should have received a copy of the GNU Affero General Public License
|
|
# along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
from logging import debug, info, error, warning
|
|
|
|
import base
|
|
import sys
|
|
import os
|
|
import os.path
|
|
import tarfile
|
|
#import tempfile
|
|
from os.path import abspath, basename, dirname
|
|
from sat.core.i18n import _
|
|
|
|
file_parser = base.subparser.add_parser('file',
|
|
help = "File managment")
|
|
|
|
file_parser.add_argument("-p", "--profile", action="store", type=str, default='@DEFAULT@',
|
|
help=_("Use PROFILE profile key (default: %(default)s)"))
|
|
file_subparser = file_parser.add_subparsers()
|
|
file_send = file_subparser.add_parser('send',
|
|
help = "Send a file to a contact")
|
|
file_send.add_argument("file", type=str, nargs = '*',
|
|
help=_("A list of file"))
|
|
file_send.add_argument("jid", type=str,
|
|
help=_("The destination jid"))
|
|
file_send.add_argument("-b", "--bz2", action="store_true", default=False,
|
|
help=_("Make a bzip2 tarball"))
|
|
|
|
file_send.set_defaults(func=lambda args : FileSend(args.profile, args.jid, args.file, args.bz2).go())
|
|
|
|
|
|
file_recv = file_subparser.add_parser('recv',
|
|
help = "Receive a file to a contact")
|
|
file_recv.add_argument("jids", type=str, nargs="*",
|
|
help=_("A list of destination jids"))
|
|
file_recv.add_argument("-m", "--multiple", action="store_true", default=False,
|
|
help=_("Wait for a file to be sent by a contact"))
|
|
file_recv.add_argument("-f", "--force", action="store_true", default=False,
|
|
help=_("Force overwritting of existing files"))
|
|
|
|
file_recv.set_defaults(func=lambda args : FileRecv(args.profile,
|
|
args.jids,
|
|
args.multiple,
|
|
args.force).go())
|
|
|
|
|
|
|
|
class FileSend(base.JPWithProfile):
|
|
def __init__(self, profile, dest_jid, files, bz2):
|
|
base.JPWithProfile.__init__(self,profile)
|
|
self.dest_jid = dest_jid
|
|
self.files = files
|
|
self.bz2 = bz2
|
|
|
|
def send_files(self):
|
|
"""Send files to jabber contact"""
|
|
for file in self.files:
|
|
if not os.path.exists(file):
|
|
error (_(u"File [%s] doesn't exist !") % file)
|
|
exit(1)
|
|
if not self.bz2 and os.path.isdir(file):
|
|
error (_("[%s] is a dir ! Please send files inside or use compression") % file)
|
|
exit(1)
|
|
|
|
full_dest_jid = self._getFullJid(self.dest_jid)
|
|
if self.bz2:
|
|
tmpfile = (basename(self.files[0]) or basename(dirname(self.files[0])) ) + '.tar.bz2' #FIXME: tmp, need an algorithm to find a good name/path
|
|
if os.path.exists(tmpfile):
|
|
error (_("tmp file (%s) already exists ! Please remove it"), tmpfile)
|
|
exit(1)
|
|
warning(_("bz2 is an experimental option at an early dev stage, use with caution"))
|
|
#FIXME: check free space, writting perm, tmp dir, filename (watch for OS used)
|
|
info(_("Starting compression, please wait..."))
|
|
sys.stdout.flush()
|
|
bz2=tarfile.open(tmpfile, "w:bz2")
|
|
for file in self.files:
|
|
info(_("Adding %s"), file)
|
|
bz2.add(file)
|
|
bz2.close()
|
|
info(_("OK !"))
|
|
path = abspath(tmpfile)
|
|
self.transfer_data = self.bridge.sendFile(full_dest_jid, path, {}, self.profile)
|
|
else:
|
|
for file in self.files:
|
|
path = abspath(file)
|
|
self.transfer_data = self.bridge.sendFile(full_dest_jid, path, {}, self.profile) #FIXME: show progress only for last transfer_id
|
|
|
|
def run(self):
|
|
self.send_files()
|
|
|
|
|
|
class FileRecv(base.JPAsk):
|
|
def __init__(self, profile, dest_jids, multiple, force, progress = False):
|
|
base.JPAsk.__init__(self,profile, start_mainloop = True)
|
|
self._dest_jids = dest_jids
|
|
self.multiple = multiple
|
|
self.force = force
|
|
self.progress = progress
|
|
|
|
def dest_jids(self):
|
|
return self._dest_jids
|
|
|
|
def confirm_type(self):
|
|
return "FILE_TRANSFER"
|
|
|
|
def ask(self, data):
|
|
answer_data = {}
|
|
answer_data["dest_path"] = os.getcwd()+'/'+data['filename']
|
|
|
|
if self.force or not os.path.exists(answer_data["dest_path"]):
|
|
self.answer(True, answer_data)
|
|
info(_("Accepted file [%(filename)s] from %(sender)s") % {'filename':data['filename'], 'sender':data['from']})
|
|
# self.transfer_data = self.confirm_id # Used by progress
|
|
else:
|
|
self.answer(False, answer_data)
|
|
warning(_("Refused file [%(filename)s] from %(sender)s: a file with the same name already exist") % {'filename':data['filename'], 'sender':data['from']})
|
|
|
|
|
|
if not self.multiple and not self.progress:
|
|
#we just accept one file
|
|
self.loop.quit()
|