view frontends/src/jp/pipe.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
line wrap: on
line source

#! /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/>.

import base

import tempfile
import os
import shutil
from sat.core.i18n import _


parser = base.subparser.add_parser('pipe',
                                           help = "File managment")

parser.add_argument("-p", "--profile", action="store", type=str, default='@DEFAULT@',
            help=_("Use PROFILE profile key (default: %(default)s)"))

subparser = parser.add_subparsers()
pipein = subparser.add_parser('out',
                                      help = "Send a pipe to a contact")
pipein.add_argument("jid", type=str,
                         help=_("The destination jid"))

pipein.set_defaults(func=lambda args : PipeOut(args.profile, args.jid).go())

pipeout = subparser.add_parser('in',
                                      help = "Send a pipe to a contact")
pipeout.add_argument("jids", type=str, nargs="*",
                     help=_("The destination jid"))

pipeout.set_defaults(func=lambda args : PipeIn(args.profile, args.jids).go())

class PipeOut(base.JPWithProfile):
    def __init__(self, profile, dest_jid):
        base.JPWithProfile.__init__(self,profile)
        self.dest_jid = dest_jid

    def pipe_out(self, dest_jid):
        """Create named pipe, and send stdin to it"""
        tmp_dir = tempfile.mkdtemp()
        fifopath = os.path.join(tmp_dir,"pipe_out")
        os.mkfifo(fifopath)
        self.bridge.pipeOut(self._getFullJid(dest_jid), fifopath, {}, self.profile)
        with open(fifopath, 'w') as f:
            shutil.copyfileobj(sys.stdin, f)
        shutil.rmtree(tmp_dir)

    def run(self):
        jids = self.check_jids([self.dest_jid])
        jid = jids[0]
        self.pipe_out(jid)

class PipeIn(base.JPAsk):
    def __init__(self, profile, dest_jids):
        base.JPAsk.__init__(self,profile, start_mainloop = True)
        self._dest_jids = dest_jids

    def dest_jids(self):
        return self._dest_jids

    def confirm_type(self):
        return "PIPE_TRANSFER"

    def ask(self, data):
        answer_data = {}
        tmp_dir = tempfile.mkdtemp()
        fifopath = os.path.join(tmp_dir,"pipe_in")
        answer_data["dest_path"] = fifopath
        os.mkfifo(fifopath)
        self.answer(True, answer_data)
        with open(fifopath, 'r') as f:
            shutil.copyfileobj(f, sys.stdout)
        shutil.rmtree(tmp_dir)
        self.loop.quit()