src/p4l/admin/views.py
author ymh <ymh.work@gmail.com>
Sat, 05 Apr 2014 03:51:11 +0200
changeset 166 0f99a7fff851
parent 153 50f01260eef4
permissions -rw-r--r--
upgrade js lib + add confirmation message on leaving page with edits
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
145
7c6fe1dab213 Add a screen in the admin to launch commands. Fix bug #4 (https://github.com/IRI-Research/plan4learning/issues/4).
ymh <ymh.work@gmail.com>
parents:
diff changeset
     1
# -*- coding: utf-8 -*-
7c6fe1dab213 Add a screen in the admin to launch commands. Fix bug #4 (https://github.com/IRI-Research/plan4learning/issues/4).
ymh <ymh.work@gmail.com>
parents:
diff changeset
     2
#
7c6fe1dab213 Add a screen in the admin to launch commands. Fix bug #4 (https://github.com/IRI-Research/plan4learning/issues/4).
ymh <ymh.work@gmail.com>
parents:
diff changeset
     3
# Copyright IRI (c) 2013
7c6fe1dab213 Add a screen in the admin to launch commands. Fix bug #4 (https://github.com/IRI-Research/plan4learning/issues/4).
ymh <ymh.work@gmail.com>
parents:
diff changeset
     4
#
7c6fe1dab213 Add a screen in the admin to launch commands. Fix bug #4 (https://github.com/IRI-Research/plan4learning/issues/4).
ymh <ymh.work@gmail.com>
parents:
diff changeset
     5
# contact@iri.centrepompidou.fr
7c6fe1dab213 Add a screen in the admin to launch commands. Fix bug #4 (https://github.com/IRI-Research/plan4learning/issues/4).
ymh <ymh.work@gmail.com>
parents:
diff changeset
     6
#
7c6fe1dab213 Add a screen in the admin to launch commands. Fix bug #4 (https://github.com/IRI-Research/plan4learning/issues/4).
ymh <ymh.work@gmail.com>
parents:
diff changeset
     7
# This software is governed by the CeCILL-B license under French law and
7c6fe1dab213 Add a screen in the admin to launch commands. Fix bug #4 (https://github.com/IRI-Research/plan4learning/issues/4).
ymh <ymh.work@gmail.com>
parents:
diff changeset
     8
# abiding by the rules of distribution of free software.  You can  use, 
7c6fe1dab213 Add a screen in the admin to launch commands. Fix bug #4 (https://github.com/IRI-Research/plan4learning/issues/4).
ymh <ymh.work@gmail.com>
parents:
diff changeset
     9
# modify and/ or redistribute the software under the terms of the CeCILL-B
7c6fe1dab213 Add a screen in the admin to launch commands. Fix bug #4 (https://github.com/IRI-Research/plan4learning/issues/4).
ymh <ymh.work@gmail.com>
parents:
diff changeset
    10
# license as circulated by CEA, CNRS and INRIA at the following URL
7c6fe1dab213 Add a screen in the admin to launch commands. Fix bug #4 (https://github.com/IRI-Research/plan4learning/issues/4).
ymh <ymh.work@gmail.com>
parents:
diff changeset
    11
# "http://www.cecill.info". 
7c6fe1dab213 Add a screen in the admin to launch commands. Fix bug #4 (https://github.com/IRI-Research/plan4learning/issues/4).
ymh <ymh.work@gmail.com>
parents:
diff changeset
    12
#
7c6fe1dab213 Add a screen in the admin to launch commands. Fix bug #4 (https://github.com/IRI-Research/plan4learning/issues/4).
ymh <ymh.work@gmail.com>
parents:
diff changeset
    13
# As a counterpart to the access to the source code and  rights to copy,
7c6fe1dab213 Add a screen in the admin to launch commands. Fix bug #4 (https://github.com/IRI-Research/plan4learning/issues/4).
ymh <ymh.work@gmail.com>
parents:
diff changeset
    14
# modify and redistribute granted by the license, users are provided only
7c6fe1dab213 Add a screen in the admin to launch commands. Fix bug #4 (https://github.com/IRI-Research/plan4learning/issues/4).
ymh <ymh.work@gmail.com>
parents:
diff changeset
    15
# with a limited warranty  and the software's author,  the holder of the
7c6fe1dab213 Add a screen in the admin to launch commands. Fix bug #4 (https://github.com/IRI-Research/plan4learning/issues/4).
ymh <ymh.work@gmail.com>
parents:
diff changeset
    16
# economic rights,  and the successive licensors  have only  limited
7c6fe1dab213 Add a screen in the admin to launch commands. Fix bug #4 (https://github.com/IRI-Research/plan4learning/issues/4).
ymh <ymh.work@gmail.com>
parents:
diff changeset
    17
# liability. 
7c6fe1dab213 Add a screen in the admin to launch commands. Fix bug #4 (https://github.com/IRI-Research/plan4learning/issues/4).
ymh <ymh.work@gmail.com>
parents:
diff changeset
    18
#
7c6fe1dab213 Add a screen in the admin to launch commands. Fix bug #4 (https://github.com/IRI-Research/plan4learning/issues/4).
ymh <ymh.work@gmail.com>
parents:
diff changeset
    19
# In this respect, the user's attention is drawn to the risks associated
7c6fe1dab213 Add a screen in the admin to launch commands. Fix bug #4 (https://github.com/IRI-Research/plan4learning/issues/4).
ymh <ymh.work@gmail.com>
parents:
diff changeset
    20
# with loading,  using,  modifying and/or developing or reproducing the
7c6fe1dab213 Add a screen in the admin to launch commands. Fix bug #4 (https://github.com/IRI-Research/plan4learning/issues/4).
ymh <ymh.work@gmail.com>
parents:
diff changeset
    21
# software by the user in light of its specific status of free software,
7c6fe1dab213 Add a screen in the admin to launch commands. Fix bug #4 (https://github.com/IRI-Research/plan4learning/issues/4).
ymh <ymh.work@gmail.com>
parents:
diff changeset
    22
# that may mean  that it is complicated to manipulate,  and  that  also
7c6fe1dab213 Add a screen in the admin to launch commands. Fix bug #4 (https://github.com/IRI-Research/plan4learning/issues/4).
ymh <ymh.work@gmail.com>
parents:
diff changeset
    23
# therefore means  that it is reserved for developers  and  experienced
7c6fe1dab213 Add a screen in the admin to launch commands. Fix bug #4 (https://github.com/IRI-Research/plan4learning/issues/4).
ymh <ymh.work@gmail.com>
parents:
diff changeset
    24
# professionals having in-depth computer knowledge. Users are therefore
7c6fe1dab213 Add a screen in the admin to launch commands. Fix bug #4 (https://github.com/IRI-Research/plan4learning/issues/4).
ymh <ymh.work@gmail.com>
parents:
diff changeset
    25
# encouraged to load and test the software's suitability as regards their
7c6fe1dab213 Add a screen in the admin to launch commands. Fix bug #4 (https://github.com/IRI-Research/plan4learning/issues/4).
ymh <ymh.work@gmail.com>
parents:
diff changeset
    26
# requirements in conditions enabling the security of their systems and/or 
7c6fe1dab213 Add a screen in the admin to launch commands. Fix bug #4 (https://github.com/IRI-Research/plan4learning/issues/4).
ymh <ymh.work@gmail.com>
parents:
diff changeset
    27
# data to be ensured and,  more generally, to use and operate it in the 
7c6fe1dab213 Add a screen in the admin to launch commands. Fix bug #4 (https://github.com/IRI-Research/plan4learning/issues/4).
ymh <ymh.work@gmail.com>
parents:
diff changeset
    28
# same conditions as regards security. 
7c6fe1dab213 Add a screen in the admin to launch commands. Fix bug #4 (https://github.com/IRI-Research/plan4learning/issues/4).
ymh <ymh.work@gmail.com>
parents:
diff changeset
    29
#
7c6fe1dab213 Add a screen in the admin to launch commands. Fix bug #4 (https://github.com/IRI-Research/plan4learning/issues/4).
ymh <ymh.work@gmail.com>
parents:
diff changeset
    30
# The fact that you are presently reading this means that you have had
7c6fe1dab213 Add a screen in the admin to launch commands. Fix bug #4 (https://github.com/IRI-Research/plan4learning/issues/4).
ymh <ymh.work@gmail.com>
parents:
diff changeset
    31
# knowledge of the CeCILL-B license and that you accept its terms.
7c6fe1dab213 Add a screen in the admin to launch commands. Fix bug #4 (https://github.com/IRI-Research/plan4learning/issues/4).
ymh <ymh.work@gmail.com>
parents:
diff changeset
    32
#
7c6fe1dab213 Add a screen in the admin to launch commands. Fix bug #4 (https://github.com/IRI-Research/plan4learning/issues/4).
ymh <ymh.work@gmail.com>
parents:
diff changeset
    33
'''
7c6fe1dab213 Add a screen in the admin to launch commands. Fix bug #4 (https://github.com/IRI-Research/plan4learning/issues/4).
ymh <ymh.work@gmail.com>
parents:
diff changeset
    34
Created on Oct 9, 2013
7c6fe1dab213 Add a screen in the admin to launch commands. Fix bug #4 (https://github.com/IRI-Research/plan4learning/issues/4).
ymh <ymh.work@gmail.com>
parents:
diff changeset
    35
7c6fe1dab213 Add a screen in the admin to launch commands. Fix bug #4 (https://github.com/IRI-Research/plan4learning/issues/4).
ymh <ymh.work@gmail.com>
parents:
diff changeset
    36
@author: ymh
7c6fe1dab213 Add a screen in the admin to launch commands. Fix bug #4 (https://github.com/IRI-Research/plan4learning/issues/4).
ymh <ymh.work@gmail.com>
parents:
diff changeset
    37
'''
7c6fe1dab213 Add a screen in the admin to launch commands. Fix bug #4 (https://github.com/IRI-Research/plan4learning/issues/4).
ymh <ymh.work@gmail.com>
parents:
diff changeset
    38
7c6fe1dab213 Add a screen in the admin to launch commands. Fix bug #4 (https://github.com/IRI-Research/plan4learning/issues/4).
ymh <ymh.work@gmail.com>
parents:
diff changeset
    39
import itertools
7c6fe1dab213 Add a screen in the admin to launch commands. Fix bug #4 (https://github.com/IRI-Research/plan4learning/issues/4).
ymh <ymh.work@gmail.com>
parents:
diff changeset
    40
import os
7c6fe1dab213 Add a screen in the admin to launch commands. Fix bug #4 (https://github.com/IRI-Research/plan4learning/issues/4).
ymh <ymh.work@gmail.com>
parents:
diff changeset
    41
import signal
7c6fe1dab213 Add a screen in the admin to launch commands. Fix bug #4 (https://github.com/IRI-Research/plan4learning/issues/4).
ymh <ymh.work@gmail.com>
parents:
diff changeset
    42
from subprocess import PIPE, Popen, STDOUT
7c6fe1dab213 Add a screen in the admin to launch commands. Fix bug #4 (https://github.com/IRI-Research/plan4learning/issues/4).
ymh <ymh.work@gmail.com>
parents:
diff changeset
    43
import time
7c6fe1dab213 Add a screen in the admin to launch commands. Fix bug #4 (https://github.com/IRI-Research/plan4learning/issues/4).
ymh <ymh.work@gmail.com>
parents:
diff changeset
    44
import uuid
7c6fe1dab213 Add a screen in the admin to launch commands. Fix bug #4 (https://github.com/IRI-Research/plan4learning/issues/4).
ymh <ymh.work@gmail.com>
parents:
diff changeset
    45
7c6fe1dab213 Add a screen in the admin to launch commands. Fix bug #4 (https://github.com/IRI-Research/plan4learning/issues/4).
ymh <ymh.work@gmail.com>
parents:
diff changeset
    46
from django.conf import settings
7c6fe1dab213 Add a screen in the admin to launch commands. Fix bug #4 (https://github.com/IRI-Research/plan4learning/issues/4).
ymh <ymh.work@gmail.com>
parents:
diff changeset
    47
from django.http.response import StreamingHttpResponse, HttpResponse
7c6fe1dab213 Add a screen in the admin to launch commands. Fix bug #4 (https://github.com/IRI-Research/plan4learning/issues/4).
ymh <ymh.work@gmail.com>
parents:
diff changeset
    48
from django.utils.translation import ugettext
7c6fe1dab213 Add a screen in the admin to launch commands. Fix bug #4 (https://github.com/IRI-Research/plan4learning/issues/4).
ymh <ymh.work@gmail.com>
parents:
diff changeset
    49
from django.views.generic.base import TemplateView, View
7c6fe1dab213 Add a screen in the admin to launch commands. Fix bug #4 (https://github.com/IRI-Research/plan4learning/issues/4).
ymh <ymh.work@gmail.com>
parents:
diff changeset
    50
7c6fe1dab213 Add a screen in the admin to launch commands. Fix bug #4 (https://github.com/IRI-Research/plan4learning/issues/4).
ymh <ymh.work@gmail.com>
parents:
diff changeset
    51
7c6fe1dab213 Add a screen in the admin to launch commands. Fix bug #4 (https://github.com/IRI-Research/plan4learning/issues/4).
ymh <ymh.work@gmail.com>
parents:
diff changeset
    52
class ConfirmScriptView(TemplateView):
7c6fe1dab213 Add a screen in the admin to launch commands. Fix bug #4 (https://github.com/IRI-Research/plan4learning/issues/4).
ymh <ymh.work@gmail.com>
parents:
diff changeset
    53
    template_name = "p4l/admin/confirm_run_script.html"
7c6fe1dab213 Add a screen in the admin to launch commands. Fix bug #4 (https://github.com/IRI-Research/plan4learning/issues/4).
ymh <ymh.work@gmail.com>
parents:
diff changeset
    54
    
7c6fe1dab213 Add a screen in the admin to launch commands. Fix bug #4 (https://github.com/IRI-Research/plan4learning/issues/4).
ymh <ymh.work@gmail.com>
parents:
diff changeset
    55
    def get_context_data(self, **kwargs):
7c6fe1dab213 Add a screen in the admin to launch commands. Fix bug #4 (https://github.com/IRI-Research/plan4learning/issues/4).
ymh <ymh.work@gmail.com>
parents:
diff changeset
    56
        return {
153
50f01260eef4 Correction on admin script configuration.
ymh <ymh.work@gmail.com>
parents: 145
diff changeset
    57
            'command_line' : " ".join(getattr(settings,"ADMIN_SCRIPT", {}).get('args',"")),
145
7c6fe1dab213 Add a screen in the admin to launch commands. Fix bug #4 (https://github.com/IRI-Research/plan4learning/issues/4).
ymh <ymh.work@gmail.com>
parents:
diff changeset
    58
            'env' : repr(getattr(settings,"ADMIN_SCRIPT", {}).get('env',{})),
7c6fe1dab213 Add a screen in the admin to launch commands. Fix bug #4 (https://github.com/IRI-Research/plan4learning/issues/4).
ymh <ymh.work@gmail.com>
parents:
diff changeset
    59
            'cwd' : repr(getattr(settings,"ADMIN_SCRIPT", {}).get('cwd',"")),
7c6fe1dab213 Add a screen in the admin to launch commands. Fix bug #4 (https://github.com/IRI-Research/plan4learning/issues/4).
ymh <ymh.work@gmail.com>
parents:
diff changeset
    60
        }
7c6fe1dab213 Add a screen in the admin to launch commands. Fix bug #4 (https://github.com/IRI-Research/plan4learning/issues/4).
ymh <ymh.work@gmail.com>
parents:
diff changeset
    61
7c6fe1dab213 Add a screen in the admin to launch commands. Fix bug #4 (https://github.com/IRI-Research/plan4learning/issues/4).
ymh <ymh.work@gmail.com>
parents:
diff changeset
    62
class RunScriptView(View):
7c6fe1dab213 Add a screen in the admin to launch commands. Fix bug #4 (https://github.com/IRI-Research/plan4learning/issues/4).
ymh <ymh.work@gmail.com>
parents:
diff changeset
    63
    
7c6fe1dab213 Add a screen in the admin to launch commands. Fix bug #4 (https://github.com/IRI-Research/plan4learning/issues/4).
ymh <ymh.work@gmail.com>
parents:
diff changeset
    64
    def __init__(self, **kwargs):
7c6fe1dab213 Add a screen in the admin to launch commands. Fix bug #4 (https://github.com/IRI-Research/plan4learning/issues/4).
ymh <ymh.work@gmail.com>
parents:
diff changeset
    65
        View.__init__(self, **kwargs)
7c6fe1dab213 Add a screen in the admin to launch commands. Fix bug #4 (https://github.com/IRI-Research/plan4learning/issues/4).
ymh <ymh.work@gmail.com>
parents:
diff changeset
    66
        self.boundary = "--BOUNDARY--==--%s" % str(uuid.uuid4())
7c6fe1dab213 Add a screen in the admin to launch commands. Fix bug #4 (https://github.com/IRI-Research/plan4learning/issues/4).
ymh <ymh.work@gmail.com>
parents:
diff changeset
    67
7c6fe1dab213 Add a screen in the admin to launch commands. Fix bug #4 (https://github.com/IRI-Research/plan4learning/issues/4).
ymh <ymh.work@gmail.com>
parents:
diff changeset
    68
    
7c6fe1dab213 Add a screen in the admin to launch commands. Fix bug #4 (https://github.com/IRI-Research/plan4learning/issues/4).
ymh <ymh.work@gmail.com>
parents:
diff changeset
    69
    def get(self, request):
7c6fe1dab213 Add a screen in the admin to launch commands. Fix bug #4 (https://github.com/IRI-Research/plan4learning/issues/4).
ymh <ymh.work@gmail.com>
parents:
diff changeset
    70
        resp = StreamingHttpResponse()
7c6fe1dab213 Add a screen in the admin to launch commands. Fix bug #4 (https://github.com/IRI-Research/plan4learning/issues/4).
ymh <ymh.work@gmail.com>
parents:
diff changeset
    71
        
7c6fe1dab213 Add a screen in the admin to launch commands. Fix bug #4 (https://github.com/IRI-Research/plan4learning/issues/4).
ymh <ymh.work@gmail.com>
parents:
diff changeset
    72
        command_kwargs = {
7c6fe1dab213 Add a screen in the admin to launch commands. Fix bug #4 (https://github.com/IRI-Research/plan4learning/issues/4).
ymh <ymh.work@gmail.com>
parents:
diff changeset
    73
            'shell':False,
7c6fe1dab213 Add a screen in the admin to launch commands. Fix bug #4 (https://github.com/IRI-Research/plan4learning/issues/4).
ymh <ymh.work@gmail.com>
parents:
diff changeset
    74
            'env':None,
7c6fe1dab213 Add a screen in the admin to launch commands. Fix bug #4 (https://github.com/IRI-Research/plan4learning/issues/4).
ymh <ymh.work@gmail.com>
parents:
diff changeset
    75
            'cwd':None
7c6fe1dab213 Add a screen in the admin to launch commands. Fix bug #4 (https://github.com/IRI-Research/plan4learning/issues/4).
ymh <ymh.work@gmail.com>
parents:
diff changeset
    76
        }
7c6fe1dab213 Add a screen in the admin to launch commands. Fix bug #4 (https://github.com/IRI-Research/plan4learning/issues/4).
ymh <ymh.work@gmail.com>
parents:
diff changeset
    77
        admin_script = getattr(settings,"ADMIN_SCRIPT", {})
7c6fe1dab213 Add a screen in the admin to launch commands. Fix bug #4 (https://github.com/IRI-Research/plan4learning/issues/4).
ymh <ymh.work@gmail.com>
parents:
diff changeset
    78
        command = admin_script.get('args',"")
7c6fe1dab213 Add a screen in the admin to launch commands. Fix bug #4 (https://github.com/IRI-Research/plan4learning/issues/4).
ymh <ymh.work@gmail.com>
parents:
diff changeset
    79
        
7c6fe1dab213 Add a screen in the admin to launch commands. Fix bug #4 (https://github.com/IRI-Research/plan4learning/issues/4).
ymh <ymh.work@gmail.com>
parents:
diff changeset
    80
        if not command:
7c6fe1dab213 Add a screen in the admin to launch commands. Fix bug #4 (https://github.com/IRI-Research/plan4learning/issues/4).
ymh <ymh.work@gmail.com>
parents:
diff changeset
    81
            return resp
7c6fe1dab213 Add a screen in the admin to launch commands. Fix bug #4 (https://github.com/IRI-Research/plan4learning/issues/4).
ymh <ymh.work@gmail.com>
parents:
diff changeset
    82
7c6fe1dab213 Add a screen in the admin to launch commands. Fix bug #4 (https://github.com/IRI-Research/plan4learning/issues/4).
ymh <ymh.work@gmail.com>
parents:
diff changeset
    83
        command_kwargs.update(admin_script,
7c6fe1dab213 Add a screen in the admin to launch commands. Fix bug #4 (https://github.com/IRI-Research/plan4learning/issues/4).
ymh <ymh.work@gmail.com>
parents:
diff changeset
    84
            stdout=PIPE,
7c6fe1dab213 Add a screen in the admin to launch commands. Fix bug #4 (https://github.com/IRI-Research/plan4learning/issues/4).
ymh <ymh.work@gmail.com>
parents:
diff changeset
    85
            stderr=STDOUT,
7c6fe1dab213 Add a screen in the admin to launch commands. Fix bug #4 (https://github.com/IRI-Research/plan4learning/issues/4).
ymh <ymh.work@gmail.com>
parents:
diff changeset
    86
            bufsize=0,
7c6fe1dab213 Add a screen in the admin to launch commands. Fix bug #4 (https://github.com/IRI-Research/plan4learning/issues/4).
ymh <ymh.work@gmail.com>
parents:
diff changeset
    87
            close_fds=True,
7c6fe1dab213 Add a screen in the admin to launch commands. Fix bug #4 (https://github.com/IRI-Research/plan4learning/issues/4).
ymh <ymh.work@gmail.com>
parents:
diff changeset
    88
            preexec_fn=os.setsid
7c6fe1dab213 Add a screen in the admin to launch commands. Fix bug #4 (https://github.com/IRI-Research/plan4learning/issues/4).
ymh <ymh.work@gmail.com>
parents:
diff changeset
    89
        )
7c6fe1dab213 Add a screen in the admin to launch commands. Fix bug #4 (https://github.com/IRI-Research/plan4learning/issues/4).
ymh <ymh.work@gmail.com>
parents:
diff changeset
    90
7c6fe1dab213 Add a screen in the admin to launch commands. Fix bug #4 (https://github.com/IRI-Research/plan4learning/issues/4).
ymh <ymh.work@gmail.com>
parents:
diff changeset
    91
        resp['Connection'] = "Keep-Alive"
7c6fe1dab213 Add a screen in the admin to launch commands. Fix bug #4 (https://github.com/IRI-Research/plan4learning/issues/4).
ymh <ymh.work@gmail.com>
parents:
diff changeset
    92
        doc_start = [
7c6fe1dab213 Add a screen in the admin to launch commands. Fix bug #4 (https://github.com/IRI-Research/plan4learning/issues/4).
ymh <ymh.work@gmail.com>
parents:
diff changeset
    93
             '<!DOCTYPE html>',
7c6fe1dab213 Add a screen in the admin to launch commands. Fix bug #4 (https://github.com/IRI-Research/plan4learning/issues/4).
ymh <ymh.work@gmail.com>
parents:
diff changeset
    94
             '<html lang="en">',
7c6fe1dab213 Add a screen in the admin to launch commands. Fix bug #4 (https://github.com/IRI-Research/plan4learning/issues/4).
ymh <ymh.work@gmail.com>
parents:
diff changeset
    95
             '<head>',
7c6fe1dab213 Add a screen in the admin to launch commands. Fix bug #4 (https://github.com/IRI-Research/plan4learning/issues/4).
ymh <ymh.work@gmail.com>
parents:
diff changeset
    96
             '<meta charset="utf-8">',
7c6fe1dab213 Add a screen in the admin to launch commands. Fix bug #4 (https://github.com/IRI-Research/plan4learning/issues/4).
ymh <ymh.work@gmail.com>
parents:
diff changeset
    97
             '<title>output</title>',
7c6fe1dab213 Add a screen in the admin to launch commands. Fix bug #4 (https://github.com/IRI-Research/plan4learning/issues/4).
ymh <ymh.work@gmail.com>
parents:
diff changeset
    98
             '<style>body {font-family: monospace; white-space: pre;}</style>',
7c6fe1dab213 Add a screen in the admin to launch commands. Fix bug #4 (https://github.com/IRI-Research/plan4learning/issues/4).
ymh <ymh.work@gmail.com>
parents:
diff changeset
    99
             '</head>',
7c6fe1dab213 Add a screen in the admin to launch commands. Fix bug #4 (https://github.com/IRI-Research/plan4learning/issues/4).
ymh <ymh.work@gmail.com>
parents:
diff changeset
   100
             '<body>',
7c6fe1dab213 Add a screen in the admin to launch commands. Fix bug #4 (https://github.com/IRI-Research/plan4learning/issues/4).
ymh <ymh.work@gmail.com>
parents:
diff changeset
   101
        ]
7c6fe1dab213 Add a screen in the admin to launch commands. Fix bug #4 (https://github.com/IRI-Research/plan4learning/issues/4).
ymh <ymh.work@gmail.com>
parents:
diff changeset
   102
        
7c6fe1dab213 Add a screen in the admin to launch commands. Fix bug #4 (https://github.com/IRI-Research/plan4learning/issues/4).
ymh <ymh.work@gmail.com>
parents:
diff changeset
   103
        doc_end = [
7c6fe1dab213 Add a screen in the admin to launch commands. Fix bug #4 (https://github.com/IRI-Research/plan4learning/issues/4).
ymh <ymh.work@gmail.com>
parents:
diff changeset
   104
             '<script>parent.done();</script>',
7c6fe1dab213 Add a screen in the admin to launch commands. Fix bug #4 (https://github.com/IRI-Research/plan4learning/issues/4).
ymh <ymh.work@gmail.com>
parents:
diff changeset
   105
             '</html>',
7c6fe1dab213 Add a screen in the admin to launch commands. Fix bug #4 (https://github.com/IRI-Research/plan4learning/issues/4).
ymh <ymh.work@gmail.com>
parents:
diff changeset
   106
             '</body>'
7c6fe1dab213 Add a screen in the admin to launch commands. Fix bug #4 (https://github.com/IRI-Research/plan4learning/issues/4).
ymh <ymh.work@gmail.com>
parents:
diff changeset
   107
        ]
7c6fe1dab213 Add a screen in the admin to launch commands. Fix bug #4 (https://github.com/IRI-Research/plan4learning/issues/4).
ymh <ymh.work@gmail.com>
parents:
diff changeset
   108
        
7c6fe1dab213 Add a screen in the admin to launch commands. Fix bug #4 (https://github.com/IRI-Research/plan4learning/issues/4).
ymh <ymh.work@gmail.com>
parents:
diff changeset
   109
        scroll_to_bottom = '<script type="text/javascript">window.scrollBy(0,50);</script>'
7c6fe1dab213 Add a screen in the admin to launch commands. Fix bug #4 (https://github.com/IRI-Research/plan4learning/issues/4).
ymh <ymh.work@gmail.com>
parents:
diff changeset
   110
7c6fe1dab213 Add a screen in the admin to launch commands. Fix bug #4 (https://github.com/IRI-Research/plan4learning/issues/4).
ymh <ymh.work@gmail.com>
parents:
diff changeset
   111
        process = Popen(**command_kwargs)
7c6fe1dab213 Add a screen in the admin to launch commands. Fix bug #4 (https://github.com/IRI-Research/plan4learning/issues/4).
ymh <ymh.work@gmail.com>
parents:
diff changeset
   112
         
7c6fe1dab213 Add a screen in the admin to launch commands. Fix bug #4 (https://github.com/IRI-Research/plan4learning/issues/4).
ymh <ymh.work@gmail.com>
parents:
diff changeset
   113
        # Save the pid in the user's session (a thread-safe place)
7c6fe1dab213 Add a screen in the admin to launch commands. Fix bug #4 (https://github.com/IRI-Research/plan4learning/issues/4).
ymh <ymh.work@gmail.com>
parents:
diff changeset
   114
        request.session['pid'] = process.pid
7c6fe1dab213 Add a screen in the admin to launch commands. Fix bug #4 (https://github.com/IRI-Research/plan4learning/issues/4).
ymh <ymh.work@gmail.com>
parents:
diff changeset
   115
 
7c6fe1dab213 Add a screen in the admin to launch commands. Fix bug #4 (https://github.com/IRI-Research/plan4learning/issues/4).
ymh <ymh.work@gmail.com>
parents:
diff changeset
   116
        def read_output():
7c6fe1dab213 Add a screen in the admin to launch commands. Fix bug #4 (https://github.com/IRI-Research/plan4learning/issues/4).
ymh <ymh.work@gmail.com>
parents:
diff changeset
   117
            for line in iter(process.stdout.readline, b''):
7c6fe1dab213 Add a screen in the admin to launch commands. Fix bug #4 (https://github.com/IRI-Research/plan4learning/issues/4).
ymh <ymh.work@gmail.com>
parents:
diff changeset
   118
                yield "%s%s" %(line, scroll_to_bottom)
7c6fe1dab213 Add a screen in the admin to launch commands. Fix bug #4 (https://github.com/IRI-Research/plan4learning/issues/4).
ymh <ymh.work@gmail.com>
parents:
diff changeset
   119
             
7c6fe1dab213 Add a screen in the admin to launch commands. Fix bug #4 (https://github.com/IRI-Research/plan4learning/issues/4).
ymh <ymh.work@gmail.com>
parents:
diff changeset
   120
        resp.streaming_content = itertools.chain(doc_start, read_output(), doc_end)
7c6fe1dab213 Add a screen in the admin to launch commands. Fix bug #4 (https://github.com/IRI-Research/plan4learning/issues/4).
ymh <ymh.work@gmail.com>
parents:
diff changeset
   121
        
7c6fe1dab213 Add a screen in the admin to launch commands. Fix bug #4 (https://github.com/IRI-Research/plan4learning/issues/4).
ymh <ymh.work@gmail.com>
parents:
diff changeset
   122
        return resp
7c6fe1dab213 Add a screen in the admin to launch commands. Fix bug #4 (https://github.com/IRI-Research/plan4learning/issues/4).
ymh <ymh.work@gmail.com>
parents:
diff changeset
   123
7c6fe1dab213 Add a screen in the admin to launch commands. Fix bug #4 (https://github.com/IRI-Research/plan4learning/issues/4).
ymh <ymh.work@gmail.com>
parents:
diff changeset
   124
7c6fe1dab213 Add a screen in the admin to launch commands. Fix bug #4 (https://github.com/IRI-Research/plan4learning/issues/4).
ymh <ymh.work@gmail.com>
parents:
diff changeset
   125
def check_pid(pid):        
7c6fe1dab213 Add a screen in the admin to launch commands. Fix bug #4 (https://github.com/IRI-Research/plan4learning/issues/4).
ymh <ymh.work@gmail.com>
parents:
diff changeset
   126
    """ Check For the existence of a unix pid. """
7c6fe1dab213 Add a screen in the admin to launch commands. Fix bug #4 (https://github.com/IRI-Research/plan4learning/issues/4).
ymh <ymh.work@gmail.com>
parents:
diff changeset
   127
    try:
7c6fe1dab213 Add a screen in the admin to launch commands. Fix bug #4 (https://github.com/IRI-Research/plan4learning/issues/4).
ymh <ymh.work@gmail.com>
parents:
diff changeset
   128
        os.kill(pid, 0)
7c6fe1dab213 Add a screen in the admin to launch commands. Fix bug #4 (https://github.com/IRI-Research/plan4learning/issues/4).
ymh <ymh.work@gmail.com>
parents:
diff changeset
   129
    except OSError:
7c6fe1dab213 Add a screen in the admin to launch commands. Fix bug #4 (https://github.com/IRI-Research/plan4learning/issues/4).
ymh <ymh.work@gmail.com>
parents:
diff changeset
   130
        return False
7c6fe1dab213 Add a screen in the admin to launch commands. Fix bug #4 (https://github.com/IRI-Research/plan4learning/issues/4).
ymh <ymh.work@gmail.com>
parents:
diff changeset
   131
    else:
7c6fe1dab213 Add a screen in the admin to launch commands. Fix bug #4 (https://github.com/IRI-Research/plan4learning/issues/4).
ymh <ymh.work@gmail.com>
parents:
diff changeset
   132
        return True
7c6fe1dab213 Add a screen in the admin to launch commands. Fix bug #4 (https://github.com/IRI-Research/plan4learning/issues/4).
ymh <ymh.work@gmail.com>
parents:
diff changeset
   133
7c6fe1dab213 Add a screen in the admin to launch commands. Fix bug #4 (https://github.com/IRI-Research/plan4learning/issues/4).
ymh <ymh.work@gmail.com>
parents:
diff changeset
   134
class KillScriptView(View):
7c6fe1dab213 Add a screen in the admin to launch commands. Fix bug #4 (https://github.com/IRI-Research/plan4learning/issues/4).
ymh <ymh.work@gmail.com>
parents:
diff changeset
   135
    
7c6fe1dab213 Add a screen in the admin to launch commands. Fix bug #4 (https://github.com/IRI-Research/plan4learning/issues/4).
ymh <ymh.work@gmail.com>
parents:
diff changeset
   136
    def get(self, request):
7c6fe1dab213 Add a screen in the admin to launch commands. Fix bug #4 (https://github.com/IRI-Research/plan4learning/issues/4).
ymh <ymh.work@gmail.com>
parents:
diff changeset
   137
        
7c6fe1dab213 Add a screen in the admin to launch commands. Fix bug #4 (https://github.com/IRI-Research/plan4learning/issues/4).
ymh <ymh.work@gmail.com>
parents:
diff changeset
   138
        resp = HttpResponse()
7c6fe1dab213 Add a screen in the admin to launch commands. Fix bug #4 (https://github.com/IRI-Research/plan4learning/issues/4).
ymh <ymh.work@gmail.com>
parents:
diff changeset
   139
        
7c6fe1dab213 Add a screen in the admin to launch commands. Fix bug #4 (https://github.com/IRI-Research/plan4learning/issues/4).
ymh <ymh.work@gmail.com>
parents:
diff changeset
   140
        pid = request.session.get('pid', None)
7c6fe1dab213 Add a screen in the admin to launch commands. Fix bug #4 (https://github.com/IRI-Research/plan4learning/issues/4).
ymh <ymh.work@gmail.com>
parents:
diff changeset
   141
        
7c6fe1dab213 Add a screen in the admin to launch commands. Fix bug #4 (https://github.com/IRI-Research/plan4learning/issues/4).
ymh <ymh.work@gmail.com>
parents:
diff changeset
   142
        if not pid:
7c6fe1dab213 Add a screen in the admin to launch commands. Fix bug #4 (https://github.com/IRI-Research/plan4learning/issues/4).
ymh <ymh.work@gmail.com>
parents:
diff changeset
   143
            resp.content = ugettext("No active process to kill")
7c6fe1dab213 Add a screen in the admin to launch commands. Fix bug #4 (https://github.com/IRI-Research/plan4learning/issues/4).
ymh <ymh.work@gmail.com>
parents:
diff changeset
   144
        else:
7c6fe1dab213 Add a screen in the admin to launch commands. Fix bug #4 (https://github.com/IRI-Research/plan4learning/issues/4).
ymh <ymh.work@gmail.com>
parents:
diff changeset
   145
            os.kill(pid, signal.SIGINT)
7c6fe1dab213 Add a screen in the admin to launch commands. Fix bug #4 (https://github.com/IRI-Research/plan4learning/issues/4).
ymh <ymh.work@gmail.com>
parents:
diff changeset
   146
            i = 0
7c6fe1dab213 Add a screen in the admin to launch commands. Fix bug #4 (https://github.com/IRI-Research/plan4learning/issues/4).
ymh <ymh.work@gmail.com>
parents:
diff changeset
   147
            while i <= settings.SCRIPT_MAX_WAIT and check_pid(pid):
7c6fe1dab213 Add a screen in the admin to launch commands. Fix bug #4 (https://github.com/IRI-Research/plan4learning/issues/4).
ymh <ymh.work@gmail.com>
parents:
diff changeset
   148
                time.sleep(settings.SCRIPT_WAIT)
7c6fe1dab213 Add a screen in the admin to launch commands. Fix bug #4 (https://github.com/IRI-Research/plan4learning/issues/4).
ymh <ymh.work@gmail.com>
parents:
diff changeset
   149
                i += 1
7c6fe1dab213 Add a screen in the admin to launch commands. Fix bug #4 (https://github.com/IRI-Research/plan4learning/issues/4).
ymh <ymh.work@gmail.com>
parents:
diff changeset
   150
7c6fe1dab213 Add a screen in the admin to launch commands. Fix bug #4 (https://github.com/IRI-Research/plan4learning/issues/4).
ymh <ymh.work@gmail.com>
parents:
diff changeset
   151
            if check_pid(pid):
7c6fe1dab213 Add a screen in the admin to launch commands. Fix bug #4 (https://github.com/IRI-Research/plan4learning/issues/4).
ymh <ymh.work@gmail.com>
parents:
diff changeset
   152
                os.killpg(pid, signal.SIGKILL)
7c6fe1dab213 Add a screen in the admin to launch commands. Fix bug #4 (https://github.com/IRI-Research/plan4learning/issues/4).
ymh <ymh.work@gmail.com>
parents:
diff changeset
   153
            
7c6fe1dab213 Add a screen in the admin to launch commands. Fix bug #4 (https://github.com/IRI-Research/plan4learning/issues/4).
ymh <ymh.work@gmail.com>
parents:
diff changeset
   154
            resp.content = ugettext("Success: The process was killed successfully.")
7c6fe1dab213 Add a screen in the admin to launch commands. Fix bug #4 (https://github.com/IRI-Research/plan4learning/issues/4).
ymh <ymh.work@gmail.com>
parents:
diff changeset
   155
            
7c6fe1dab213 Add a screen in the admin to launch commands. Fix bug #4 (https://github.com/IRI-Research/plan4learning/issues/4).
ymh <ymh.work@gmail.com>
parents:
diff changeset
   156
        return resp