#! /usr/bin/env python
## -*- Mode: python; py-indent-offset: 4; indent-tabs-mode: nil; coding: utf-8; -*-
#
# Copyright (c) 2009 University of Washington
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License version 2 as
# published by the Free Software Foundation;
#
# 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 General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
#

import os
import sys
import time
import optparse
import subprocess
import threading
import Queue
import signal
import xml.dom.minidom
import shutil
import re

from utils import get_list_from_file

#
# XXX This should really be part of a waf command to list the configuration
# items relative to optional ns-3 pieces.
#
# A list of interesting configuration items in the waf configuration
# cache which we may be interested in when deciding on which examples
# to run and how to run them.  These are set by waf during the
# configuration phase and the corresponding assignments are usually
# found in the associated subdirectory wscript files.
#
interesting_config_items = [
    "NS3_ENABLED_MODULES",
    "NS3_MODULE_PATH",
    "NSC_ENABLED",
    "ENABLE_REAL_TIME",
    "ENABLE_THREADING",
    "ENABLE_EXAMPLES",
    "ENABLE_TESTS",
    "EXAMPLE_DIRECTORIES",
    "ENABLE_PYTHON_BINDINGS",
    "ENABLE_CLICK",
    "ENABLE_BRITE",
    "ENABLE_OPENFLOW",
    "APPNAME",
    "BUILD_PROFILE",
    "VERSION",
    "PYTHON",
    "VALGRIND_FOUND",
]

NSC_ENABLED = False
ENABLE_REAL_TIME = False
ENABLE_THREADING = False
ENABLE_EXAMPLES = True
ENABLE_TESTS = True
ENABLE_CLICK = False
ENABLE_BRITE = False
ENABLE_OPENFLOW = False
EXAMPLE_DIRECTORIES = []
APPNAME = ""
BUILD_PROFILE = ""
BUILD_PROFILE_SUFFIX = ""
VERSION = ""
PYTHON = ""
VALGRIND_FOUND = True

#
# This will be given a prefix and a suffix when the waf config file is
# read.
#
test_runner_name = "test-runner"

#
# If the user has constrained us to run certain kinds of tests, we can tell waf
# to only build
#
core_kinds = ["bvt", "core", "performance", "system", "unit"]

#
# There are some special cases for test suites that kill valgrind.  This is
# because NSC causes illegal instruction crashes when run under valgrind.
#
core_valgrind_skip_tests = [
    "ns3-tcp-cwnd",
    "nsc-tcp-loss",
    "ns3-tcp-interoperability",
    "routing-click",
    "lte-rr-ff-mac-scheduler",
    "lte-tdmt-ff-mac-scheduler",
    "lte-fdmt-ff-mac-scheduler",
    "lte-pf-ff-mac-scheduler",
    "lte-tta-ff-mac-scheduler",
    "lte-fdbet-ff-mac-scheduler",
    "lte-ttbet-ff-mac-scheduler",
    "lte-fdtbfq-ff-mac-scheduler",
    "lte-tdtbfq-ff-mac-scheduler",
    "lte-pss-ff-mac-scheduler",
]

#
# There are some special cases for test suites that fail when NSC is
# missing.
#
core_nsc_missing_skip_tests = [
    "ns3-tcp-cwnd",
    "nsc-tcp-loss",
    "ns3-tcp-interoperability",
]

#
# Parse the examples-to-run file if it exists.
#
# This function adds any C++ examples or Python examples that are to be run
# to the lists in example_tests and python_tests, respectively.
#
def parse_examples_to_run_file(
    examples_to_run_path,
    cpp_executable_dir,
    python_script_dir,
    example_tests,
    example_names_original,
    python_tests):

    # Look for the examples-to-run file exists.
    if os.path.exists(examples_to_run_path):

        # Each tuple in the C++ list of examples to run contains
        #
        #     (example_name, do_run, do_valgrind_run)
        #
        # where example_name is the executable to be run, do_run is a
        # condition under which to run the example, and do_valgrind_run is
        # a condition under which to run the example under valgrind.  This
        # is needed because NSC causes illegal instruction crashes with
        # some tests when they are run under valgrind.
        #
        # Note that the two conditions are Python statements that
        # can depend on waf configuration variables.  For example,
        #
        #     ("tcp-nsc-lfn", "NSC_ENABLED == True", "NSC_ENABLED == False"),
        #
        cpp_examples = get_list_from_file(examples_to_run_path, "cpp_examples")
        for example_name, do_run, do_valgrind_run in cpp_examples:

            # Seperate the example name from its arguments.
            example_name_original = example_name
            example_name_parts = example_name.split(' ', 1)
            if len(example_name_parts) == 1:
                example_name      = example_name_parts[0]
                example_arguments = ""
            else:
                example_name      = example_name_parts[0]
                example_arguments = example_name_parts[1]

            # Add the proper prefix and suffix to the example name to
            # match what is done in the wscript file.
            example_name = "%s%s-%s%s" % (APPNAME, VERSION, example_name, BUILD_PROFILE_SUFFIX)

            # Set the full path for the example.
            example_path = os.path.join(cpp_executable_dir, example_name)
            # Add all of the C++ examples that were built, i.e. found
            # in the directory, to the list of C++ examples to run.
            if os.path.exists(example_path):
                # Add any arguments to the path.
                if len(example_name_parts) != 1:
                    example_path = "%s %s" % (example_path, example_arguments)

                # Add this example.
                example_tests.append((example_path, do_run, do_valgrind_run))
                example_names_original.append(example_name_original)

        # Each tuple in the Python list of examples to run contains
        #
        #     (example_name, do_run)
        #
        # where example_name is the Python script to be run and
        # do_run is a condition under which to run the example.
        #
        # Note that the condition is a Python statement that can
        # depend on waf configuration variables.  For example,
        #
        #     ("realtime-udp-echo.py", "ENABLE_REAL_TIME == True"),
        #
        python_examples = get_list_from_file(examples_to_run_path, "python_examples")
        for example_name, do_run in python_examples:
            # Seperate the example name from its arguments.
            example_name_parts = example_name.split(' ', 1)
            if len(example_name_parts) == 1:
                example_name      = example_name_parts[0]
                example_arguments = ""
            else:
                example_name      = example_name_parts[0]
                example_arguments = example_name_parts[1]

            # Set the full path for the example.
            example_path = os.path.join(python_script_dir, example_name)

            # Add all of the Python examples that were found to the
            # list of Python examples to run.
            if os.path.exists(example_path):
                # Add any arguments to the path.
                if len(example_name_parts) != 1:
                    example_path = "%s %s" % (example_path, example_arguments)

                # Add this example.
                python_tests.append((example_path, do_run))

#
# The test suites are going to want to output status.  They are running
# concurrently.  This means that unless we are careful, the output of
# the test suites will be interleaved.  Rather than introducing a lock
# file that could unintentionally start serializing execution, we ask
# the tests to write their output to a temporary directory and then
# put together the final output file when we "join" the test tasks back
# to the main thread.  In addition to this issue, the example programs
# often write lots and lots of trace files which we will just ignore.
# We put all of them into the temp directory as well, so they can be
# easily deleted.
#
TMP_OUTPUT_DIR = "testpy-output"

def read_test(test):
    result = test.find('Result').text
    name = test.find('Name').text
    if not test.find('Time') is None:
        time_real = test.find('Time').get('real')
    else:
        time_real = ''
    return (result, name, time_real)

#
# A simple example of writing a text file with a test result summary.  It is
# expected that this output will be fine for developers looking for problems.
#
def node_to_text (test, f):
    (result, name, time_real) = read_test(test)
    output = "%s: Test Suite \"%s\" (%s)\n" % (result, name, time_real)
    f.write(output)
    for details in test.findall('FailureDetails'):
        f.write("    Details:\n")
        f.write("      Message:   %s\n" % details.find('Message').text)
        f.write("      Condition: %s\n" % details.find('Condition').text)
        f.write("      Actual:    %s\n" % details.find('Actual').text)
        f.write("      Limit:     %s\n" % details.find('Limit').text)
        f.write("      File:      %s\n" % details.find('File').text)
        f.write("      Line:      %s\n" % details.find('Line').text)
    for child in test.findall('Test'):
        node_to_text(child, f)

def translate_to_text(results_file, text_file):
    f = open(text_file, 'w')
    import xml.etree.ElementTree as ET
    et = ET.parse (results_file)
    for test in et.findall('Test'):
        node_to_text (test, f)

    for example in et.findall('Example'):
        result = example.find('Result').text
        name = example.find('Name').text
        if not example.find('Time') is None:
            time_real = example.find('Time').get('real')
        else:
            time_real = ''
        output = "%s: Example \"%s\" (%s)\n" % (result, name, time_real)
        f.write(output)

    f.close()

#
# A simple example of writing an HTML file with a test result summary.  It is
# expected that this will eventually be made prettier as time progresses and
# we have time to tweak it.  This may end up being moved to a separate module
# since it will probably grow over time.
#
def translate_to_html(results_file, html_file):
    f = open(html_file, 'w')
    f.write("\n")
    f.write("\n")
    f.write("
ns-3 Test Results
\n")

    #
    # Read and parse the whole results file.
    #
    import xml.etree.ElementTree as ET
    et = ET.parse(results_file)

    #
    # Iterate through the test suites
    #
    f.write("
Test Suites
\n")
    for suite in et.findall('Test'):
        #
        # For each test suite, get its name, result and execution time info
        #
        (result, name, time) = read_test (suite)

        #
        # Print a level three header with the result, name and time.  If the
        # test suite passed, the header is printed in green. If the suite was
        # skipped, print it in orange, otherwise assume something bad happened
        # and print in red.
        #
        if result == "PASS":
            f.write("
%s: %s (%s)
\n" % (result, name, time))
        elif result == "SKIP":
            f.write("
%s: %s (%s)
\n" % (result, name, time))
        else:
            f.write("
%s: %s (%s)
\n" % (result, name, time))

        #
        # The test case information goes in a table.
        #
        f.write("
\n")

        #
        # The first column of the table has the heading Result
        #
        f.write(" Result 	\n")

        #
        # If the suite crashed or is skipped, there is no further information, so just
        # delare a new table row with the result (CRASH or SKIP) in it.  Looks like:
        #
        #   +--------+
        #   | Result |
        #   +--------+
        #   | CRASH  |
        #   +--------+
        #
        # Then go on to the next test suite.  Valgrind and skipped errors look the same.
        #
        if result in ["CRASH", "SKIP", "VALGR"]:
            f.write("
\n")
            if result == "SKIP":
                f.write("%s	\n" % result)
            else:
                f.write("%s	\n" % result)
            f.write("
\n")
            f.write("
\n")
            continue

        #
        # If the suite didn't crash, we expect more information, so fill out
        # the table heading row.  Like,
        #
        #   +--------+----------------+------+
        #   | Result | Test Case Name | Time |
        #   +--------+----------------+------+
        #
        f.write("Test Case Name	\n")
        f.write(" Time 	\n")

        #
        # If the test case failed, we need to print out some failure details
        # so extend the heading row again.  Like,
        #
        #   +--------+----------------+------+-----------------+
        #   | Result | Test Case Name | Time | Failure Details |
        #   +--------+----------------+------+-----------------+
        #
        if result == "FAIL":
            f.write("Failure Details	\n")

        #
        # Now iterate through all of the test cases.
        #
        for case in suite.findall('Test'):

            #
            # Get the name, result and timing information from xml to use in
            # printing table below.
            #
            (result, name, time) = read_test(case)

            #
            # If the test case failed, we iterate through possibly multiple
            # failure details
            #
            if result == "FAIL":
                #
                # There can be multiple failures for each test case.  The first
                # row always gets the result, name and timing information along
                # with the failure details.  Remaining failures don't duplicate
                # this information but just get blanks for readability.  Like,
                #
                #   +--------+----------------+------+-----------------+
                #   | Result | Test Case Name | Time | Failure Details |
                #   +--------+----------------+------+-----------------+
                #   |  FAIL  | The name       | time | It's busted     |
                #   +--------+----------------+------+-----------------+
                #   |        |                |      | Really broken   |
                #   +--------+----------------+------+-----------------+
                #   |        |                |      | Busted bad      |
                #   +--------+----------------+------+-----------------+
                #

                first_row = True
                for details in case.findall('FailureDetails'):

                    #
                    # Start a new row in the table for each possible Failure Detail
                    #
                    f.write("
\n")

                    if first_row:
                        first_row = False
                        f.write("%s	\n" % result)
                        f.write("%s	\n" % name)
                        f.write("%s	\n" % time)
                    else:
                        f.write("	\n")
                        f.write("	\n")
                        f.write("	\n")

                    f.write("")
                    f.write("Message: %s, " % details.find('Message').text)
                    f.write("Condition: %s, " % details.find('Condition').text)
                    f.write("Actual: %s, " % details.find('Actual').text)
                    f.write("Limit: %s, " % details.find('Limit').text)
                    f.write("File: %s, " % details.find('File').text)
                    f.write("Line: %s" % details.find('Line').text)
                    f.write("	\n")

                    #
                    # End the table row
                    #
                    f.write("	\n")
            else:
                #
                # If this particular test case passed, then we just print the PASS
                # result in green, followed by the test case name and its execution
                # time information.  These go off in  ... 	 table data.
                # The details table entry is left blank.
                #
                #   +--------+----------------+------+---------+
                #   | Result | Test Case Name | Time | Details |
                #   +--------+----------------+------+---------+
                #   |  PASS  | The name       | time |         |
                #   +--------+----------------+------+---------+
                #
                f.write("
\n")
                f.write("%s	\n" % result)
                f.write("%s	\n" % name)
                f.write("%s	\n" % time)
                f.write("	\n")
                f.write("
\n")
        #
        # All of the rows are written, so we need to end the table.
        #
        f.write("
\n")

    #
    # That's it for all of the test suites.  Now we have to do something about
    # our examples.
    #
    f.write("
Examples
\n")

    #
    # Example status is rendered in a table just like the suites.
    #
    f.write("
\n")

    #
    # The table headings look like,
    #
    #   +--------+--------------+--------------+
    #   | Result | Example Name | Elapsed Time |
    #   +--------+--------------+--------------+
    #
    f.write(" Result 	\n")
    f.write("Example Name	\n")
    f.write("Elapsed Time	\n")

    #
    # Now iterate through all of the examples
    #
    for example in et.findall("Example"):

        #
        # Start a new row for each example
        #
        f.write("
\n")

        #
        # Get the result and name of the example in question
        #
        (result, name, time) = read_test(example)

        #
        # If the example either failed or crashed, print its result status
        # in red; otherwise green.  This goes in a  ... 	 table data
        #
        if result == "PASS":
            f.write("%s	\n" % result)
        elif result == "SKIP":
            f.write("%s</fd>\n" % result)
        else:
            f.write("%s	\n" % result)

        #
        # Write the example name as a new tag data.
        #
        f.write("%s	\n" % name)

        #
        # Write the elapsed time as a new tag data.
        #
        f.write("%s	\n" % time)

        #
        # That's it for the current example, so terminate the row.
        #
        f.write("
\n")

    #
    # That's it for the table of examples, so terminate the table.
    #
    f.write("
\n")

    #
    # And that's it for the report, so finish up.
    #
    f.write("\n")
    f.write("\n")
    f.close()

#
# Python Control-C handling is broken in the presence of multiple threads.
# Signals get delivered to the runnable/running thread by default and if
# it is blocked, the signal is simply ignored.  So we hook sigint and set
# a global variable telling the system to shut down gracefully.
#
thread_exit = False

def sigint_hook(signal, frame):
    global thread_exit
    thread_exit = True
    return 0

#
# In general, the build process itself naturally takes care of figuring out
# which tests are built into the test runner.  For example, if waf configure
# determines that ENABLE_EMU is false due to some missing dependency,
# the tests for the emu net device simply will not be built and will
# therefore not be included in the built test runner.
#
# Examples, however, are a different story.  In that case, we are just given
# a list of examples that could be run.  Instead of just failing, for example,
# nsc-tcp-zoo if NSC is not present, we look into the waf saved configuration
# for relevant configuration items.
#
# XXX This function pokes around in the waf internal state file.  To be a
# little less hacky, we should add a commmand to waf to return this info
# and use that result.
#
def read_waf_config():
    for line in open(".lock-waf_" + sys.platform + "_build", "rt"):
        if line.startswith("top_dir ="):
            key, val = line.split('=')
            top_dir = eval(val.strip())
        if line.startswith("out_dir ="):
            key, val = line.split('=')
            out_dir = eval(val.strip())
    global NS3_BASEDIR
    NS3_BASEDIR = top_dir
    global NS3_BUILDDIR
    NS3_BUILDDIR = out_dir
    for line in open("%s/c4che/_cache.py" % out_dir).readlines():
        for item in interesting_config_items:
            if line.startswith(item):
                exec(line, globals())

    if options.verbose:
        for item in interesting_config_items:
            print "%s ==" % item, eval(item)

#
# It seems pointless to fork a process to run waf to fork a process to run
# the test runner, so we just run the test runner directly.  The main thing
# that waf would do for us would be to sort out the shared library path but
# we can deal with that easily and do here.
#
# There can be many different ns-3 repositories on a system, and each has
# its own shared libraries, so ns-3 doesn't hardcode a shared library search
# path -- it is cooked up dynamically, so we do that too.
#
def make_paths():
    have_DYLD_LIBRARY_PATH = False
    have_LD_LIBRARY_PATH = False
    have_PATH = False
    have_PYTHONPATH = False

    keys = os.environ.keys()
    for key in keys:
        if key == "DYLD_LIBRARY_PATH":
            have_DYLD_LIBRARY_PATH = True
        if key == "LD_LIBRARY_PATH":
            have_LD_LIBRARY_PATH = True
        if key == "PATH":
            have_PATH = True
        if key == "PYTHONPATH":
            have_PYTHONPATH = True

    pypath = os.environ["PYTHONPATH"] = os.path.join (NS3_BUILDDIR, "bindings", "python")

    if not have_PYTHONPATH:
        os.environ["PYTHONPATH"] = pypath
    else:
        os.environ["PYTHONPATH"] += ":" + pypath

    if options.verbose:
        print "os.environ[\"PYTHONPATH\"] == %s" % os.environ["PYTHONPATH"]

    if sys.platform == "darwin":
        if not have_DYLD_LIBRARY_PATH:
            os.environ["DYLD_LIBRARY_PATH"] = ""
        for path in NS3_MODULE_PATH:
            os.environ["DYLD_LIBRARY_PATH"] += ":" + path
        if options.verbose:
            print "os.environ[\"DYLD_LIBRARY_PATH\"] == %s" % os.environ["DYLD_LIBRARY_PATH"]
    elif sys.platform == "win32":
        if not have_PATH:
            os.environ["PATH"] = ""
        for path in NS3_MODULE_PATH:
            os.environ["PATH"] += ';' + path
        if options.verbose:
            print "os.environ[\"PATH\"] == %s" % os.environ["PATH"]
    elif sys.platform == "cygwin":
        if not have_PATH:
            os.environ["PATH"] = ""
        for path in NS3_MODULE_PATH:
            os.environ["PATH"] += ":" + path
        if options.verbose:
            print "os.environ[\"PATH\"] == %s" % os.environ["PATH"]
    else:
        if not have_LD_LIBRARY_PATH:
            os.environ["LD_LIBRARY_PATH"] = ""
        for path in NS3_MODULE_PATH:
            os.environ["LD_LIBRARY_PATH"] += ":" + path
        if options.verbose:
            print "os.environ[\"LD_LIBRARY_PATH\"] == %s" % os.environ["LD_LIBRARY_PATH"]

#
# Short note on generating suppressions:
#
# See the valgrind documentation for a description of suppressions.  The easiest
# way to generate a suppression expression is by using the valgrind
# --gen-suppressions option.  To do that you have to figure out how to run the
# test in question.
#
# If you do "test.py -v -g -s <suitename> then test.py will output most of what
# you need.  For example, if you are getting a valgrind error in the
# devices-mesh-dot11s-regression test suite, you can run:
#
#   ./test.py -v -g -s devices-mesh-dot11s-regression
#
# You should see in the verbose output something that looks like:
#
#   Synchronously execute valgrind --suppressions=/home/craigdo/repos/ns-3-allinone-dev/ns-3-dev/testpy.supp
#   --leak-check=full --error-exitcode=2 /home/craigdo/repos/ns-3-allinone-dev/ns-3-dev/build/debug/utils/ns3-dev-test-runner-debug
#   --suite=devices-mesh-dot11s-regression --basedir=/home/craigdo/repos/ns-3-allinone-dev/ns-3-dev
#   --tempdir=testpy-output/2010-01-12-22-47-50-CUT
#   --out=testpy-output/2010-01-12-22-47-50-CUT/devices-mesh-dot11s-regression.xml
#
# You need to pull out the useful pieces, and so could run the following to
# reproduce your error:
#
#   valgrind --suppressions=/home/craigdo/repos/ns-3-allinone-dev/ns-3-dev/testpy.supp
#   --leak-check=full --error-exitcode=2 /home/craigdo/repos/ns-3-allinone-dev/ns-3-dev/build/debug/utils/ns3-dev-test-runner-debug
#   --suite=devices-mesh-dot11s-regression --basedir=/home/craigdo/repos/ns-3-allinone-dev/ns-3-dev
#   --tempdir=testpy-output
#
# Hint: Use the first part of the command as is, and point the "tempdir" to
# somewhere real.  You don't need to specify an "out" file.
#
# When you run the above command you should see your valgrind error.  The
# suppression expression(s) can be generated by adding the --gen-suppressions=yes
# option to valgrind.  Use something like:
#
#   valgrind --gen-suppressions=yes --suppressions=/home/craigdo/repos/ns-3-allinone-dev/ns-3-dev/testpy.supp
#   --leak-check=full --error-exitcode=2 /home/craigdo/repos/ns-3-allinone-dev/ns-3-dev/build/debug/utils/ns3-dev-test-runner-debug
#   --suite=devices-mesh-dot11s-regression --basedir=/home/craigdo/repos/ns-3-allinone-dev/ns-3-dev
#   --tempdir=testpy-output
#
# Now when valgrind detects an error it will ask:
#
#   ==27235== ---- Print suppression ? --- [Return/N/n/Y/y/C/c] ----
#
# to which you just enter 'y'<ret>.
#
# You will be provided with a suppression expression that looks something like
# the following:
#   {
#     <insert_a_suppression_name_here>
#     Memcheck:Addr8
#     fun:_ZN3ns36dot11s15HwmpProtocolMac8SendPreqESt6vectorINS0_6IePreqESaIS3_EE
#     fun:_ZN3ns36dot11s15HwmpProtocolMac10SendMyPreqEv
#     fun:_ZN3ns36dot11s15HwmpProtocolMac18RequestDestinationENS_12Mac48AddressEjj
#     ...
#     the rest of the stack frame
#     ...
#   }
#
# You need to add a supression name which will only be printed out by valgrind in
# verbose mode (but it needs to be there in any case).  The entire stack frame is
# shown to completely characterize the error, but in most cases you won't need
# all of that info.  For example, if you want to turn off all errors that happen
# when the function (fun:) is called, you can just delete the rest of the stack
# frame.  You can also use wildcards to make the mangled signatures more readable.
#
# I added the following to the testpy.supp file for this particular error:
#
#   {
#     Supress invalid read size errors in SendPreq() when using HwmpProtocolMac
#     Memcheck:Addr8
#     fun:*HwmpProtocolMac*SendPreq*
#   }
#
# Now, when you run valgrind the error will be suppressed.
#
VALGRIND_SUPPRESSIONS_FILE = "testpy.supp"

def run_job_synchronously(shell_command, directory, valgrind, is_python, build_path=""):
    suppressions_path = os.path.join (NS3_BASEDIR, VALGRIND_SUPPRESSIONS_FILE)

    if is_python:
        path_cmd = PYTHON[0] + " " + os.path.join (NS3_BASEDIR, shell_command)
    else:
        if len(build_path):
            path_cmd = os.path.join (build_path, shell_command)
        else:
            path_cmd = os.path.join (NS3_BUILDDIR, shell_command)

    if valgrind:
        cmd = "valgrind --suppressions=%s --leak-check=full --show-reachable=yes --error-exitcode=2 %s" % (suppressions_path,
            path_cmd)
    else:
        cmd = path_cmd

    if options.verbose:
        print "Synchronously execute %s" % cmd

    start_time = time.time()
    proc = subprocess.Popen(cmd, shell = True, cwd = directory, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
    stdout_results, stderr_results = proc.communicate()
    elapsed_time = time.time() - start_time

    retval = proc.returncode

    #
    # valgrind sometimes has its own idea about what kind of memory management
    # errors are important.  We want to detect *any* leaks, so the way to do
    # that is to look for the presence of a valgrind leak summary section.
    #
    # If another error has occurred (like a test suite has failed), we don't
    # want to trump that error, so only do the valgrind output scan if the
    # test has otherwise passed (return code was zero).
    #
    if valgrind and retval == 0 and "== LEAK SUMMARY:" in stderr_results:
        retval = 2

    if options.verbose:
        print "Return code = ", retval
        print "stderr = ", stderr_results

    return (retval, stdout_results, stderr_results, elapsed_time)

#
# This class defines a unit of testing work.  It will typically refer to
# a test suite to run using the test-runner, or an example to run directly.
#
class Job:
    def __init__(self):
        self.is_break = False
        self.is_skip = False
        self.is_example = False
        self.is_pyexample = False
        self.shell_command = ""
        self.display_name = ""
        self.basedir = ""
        self.tempdir = ""
        self.cwd = ""
        self.tmp_file_name = ""
        self.returncode = False
        self.elapsed_time = 0
        self.build_path = ""

    #
    # A job is either a standard job or a special job indicating that a worker
    # thread should exist.  This special job is indicated by setting is_break
    # to true.
    #
    def set_is_break(self, is_break):
        self.is_break = is_break

    #
    # If a job is to be skipped, we actually run it through the worker threads
    # to keep the PASS, FAIL, CRASH and SKIP processing all in one place.
    #
    def set_is_skip(self, is_skip):
        self.is_skip = is_skip

    #
    # Examples are treated differently than standard test suites.  This is
    # mostly because they are completely unaware that they are being run as
    # tests.  So we have to do some special case processing to make them look
    # like tests.
    #
    def set_is_example(self, is_example):
        self.is_example = is_example

    #
    # Examples are treated differently than standard test suites.  This is
    # mostly because they are completely unaware that they are being run as
    # tests.  So we have to do some special case processing to make them look
    # like tests.
    #
    def set_is_pyexample(self, is_pyexample):
        self.is_pyexample = is_pyexample

    #
    # This is the shell command that will be executed in the job.  For example,
    #
    #  "utils/ns3-dev-test-runner-debug --test-name=some-test-suite"
    #
    def set_shell_command(self, shell_command):
        self.shell_command = shell_command

    #
    # This is the build path where ns-3 was built.  For example,
    #
    #  "/home/craigdo/repos/ns-3-allinone-test/ns-3-dev/build/debug"
    #
    def set_build_path(self, build_path):
        self.build_path = build_path

    #
    # This is the dispaly name of the job, typically the test suite or example
    # name.  For example,
    #
    #  "some-test-suite" or "udp-echo"
    #
    def set_display_name(self, display_name):
        self.display_name = display_name

    #
    # This is the base directory of the repository out of which the tests are
    # being run.  It will be used deep down in the testing framework to determine
    # where the source directory of the test was, and therefore where to find
    # provided test vectors.  For example,
    #
    #  "/home/user/repos/ns-3-dev"
    #
    def set_basedir(self, basedir):
        self.basedir = basedir

    #
    # This is the directory to which a running test suite should write any
    # temporary files.
    #
    def set_tempdir(self, tempdir):
        self.tempdir = tempdir

    #
    # This is the current working directory that will be given to an executing
    # test as it is being run.  It will be used for examples to tell them where
    # to write all of the pcap files that we will be carefully ignoring.  For
    # example,
    #
    #  "/tmp/unchecked-traces"
    #
    def set_cwd(self, cwd):
        self.cwd = cwd

    #
    # This is the temporary results file name that will be given to an executing
    # test as it is being run.  We will be running all of our tests in parallel
    # so there must be multiple temporary output files.  These will be collected
    # into a single XML file at the end and then be deleted.
    #
    def set_tmp_file_name(self, tmp_file_name):
        self.tmp_file_name = tmp_file_name

    #
    # The return code received when the job process is executed.
    #
    def set_returncode(self, returncode):
        self.returncode = returncode

    #
    # The elapsed real time for the job execution.
    #
    def set_elapsed_time(self, elapsed_time):
        self.elapsed_time = elapsed_time

#
# The worker thread class that handles the actual running of a given test.
# Once spawned, it receives requests for work through its input_queue and
# ships the results back through the output_queue.
#
class worker_thread(threading.Thread):
    def __init__(self, input_queue, output_queue):
        threading.Thread.__init__(self)
        self.input_queue = input_queue
        self.output_queue = output_queue

    def run(self):
        while True:
            job = self.input_queue.get()
            #
            # Worker threads continue running until explicitly told to stop with
            # a special job.
            #
            if job.is_break:
                return
            #
            # If the global interrupt handler sets the thread_exit variable,
            # we stop doing real work and just report back a "break" in the
            # normal command processing has happened.
            #
            if thread_exit == True:
                job.set_is_break(True)
                self.output_queue.put(job)
                continue

            #
            # If we are actually supposed to skip this job, do so.  Note that
            # if is_skip is true, returncode is undefined.
            #
            if job.is_skip:
                if options.verbose:
                    print "Skip %s" % job.shell_command
                self.output_queue.put(job)
                continue

            #
            # Otherwise go about the business of running tests as normal.
            #
            else:
                if options.verbose:
                    print "Launch %s" % job.shell_command

                if job.is_example or job.is_pyexample:
                    #
                    # If we have an example, the shell command is all we need to
                    # know.  It will be something like "examples/udp/udp-echo" or
                    # "examples/wireless/mixed-wireless.py"
                    #
                    (job.returncode, standard_out, standard_err, et) = run_job_synchronously(job.shell_command,
                        job.cwd, options.valgrind, job.is_pyexample, job.build_path)
                else:
                    #
                    # If we're a test suite, we need to provide a little more info
                    # to the test runner, specifically the base directory and temp
                    # file name
                    #
                    if options.update_data:
                        update_data = '--update-data'
                    else:
                        update_data = ''
                    (job.returncode, standard_out, standard_err, et) = run_job_synchronously(job.shell_command +
                        " --xml --tempdir=%s --out=%s %s" % (job.tempdir, job.tmp_file_name, update_data),
                        job.cwd, options.valgrind, False)

                job.set_elapsed_time(et)

                if options.verbose:
                    print "returncode = %d" % job.returncode
                    print "---------- begin standard out ----------"
                    print standard_out
                    print "---------- begin standard err ----------"
                    print standard_err
                    print "---------- end standard err ----------"

                self.output_queue.put(job)

#
# This is the main function that does the work of interacting with the
# test-runner itself.
#
def run_tests():
    #
    # Pull some interesting configuration information out of waf, primarily
    # so we can know where executables can be found, but also to tell us what
    # pieces of the system have been built.  This will tell us what examples
    # are runnable.
    #
    read_waf_config()

    #
    # Set the proper suffix.
    #
    global BUILD_PROFILE_SUFFIX
    if BUILD_PROFILE == 'release':
        BUILD_PROFILE_SUFFIX = ""
    else:
        BUILD_PROFILE_SUFFIX = "-" + BUILD_PROFILE

    #
    # Add the proper prefix and suffix to the test-runner name to
    # match what is done in the wscript file.
    #
    test_runner_name = "%s%s-%s%s" % (APPNAME, VERSION, "test-runner", BUILD_PROFILE_SUFFIX)

    #
    # Run waf to make sure that everything is built, configured and ready to go
    # unless we are explicitly told not to.  We want to be careful about causing
    # our users pain while waiting for extraneous stuff to compile and link, so
    # we allow users that know what they''re doing to not invoke waf at all.
    #
    if not options.nowaf:

        #
        # If the user is running the "kinds" or "list" options, there is an
        # implied dependency on the test-runner since we call that program
        # if those options are selected.  We will exit after processing those
        # options, so if we see them, we can safely only build the test-runner.
        #
        # If the user has constrained us to running only a particular type of
        # file, we can only ask waf to build what we know will be necessary.
        # For example, if the user only wants to run BVT tests, we only have
        # to build the test-runner and can ignore all of the examples.
        #
        # If the user only wants to run a single example, then we can just build
        # that example.
        #
        # If there is no constraint, then we have to build everything since the
        # user wants to run everything.
        #
        if options.kinds or options.list or (len(options.constrain) and options.constrain in core_kinds):
            if sys.platform == "win32":
                waf_cmd = "waf --target=test-runner"
            else:
                waf_cmd = "./waf --target=test-runner"
        elif len(options.example):
            if sys.platform == "win32":
                waf_cmd = "waf --target=%s" % os.path.basename(options.example)
            else:
                waf_cmd = "./waf --target=%s" % os.path.basename(options.example)

        else:
            if sys.platform == "win32":
                waf_cmd = "waf"
            else:
                waf_cmd = "./waf"

        if options.verbose:
            print "Building: %s" % waf_cmd

        proc = subprocess.Popen(waf_cmd, shell = True)
        proc.communicate()
        if proc.returncode:
            print >> sys.stderr, "Waf died. Not running tests"
            return proc.returncode

    #
    # Dynamically set up paths.
    #
    make_paths()

    #
    # Get the information from the build status file.
    #
    build_status_file = os.path.join (NS3_BUILDDIR, 'build-status.py')
    if os.path.exists(build_status_file):
        ns3_runnable_programs = get_list_from_file(build_status_file, "ns3_runnable_programs")
        ns3_runnable_scripts = get_list_from_file(build_status_file, "ns3_runnable_scripts")
    else:
        print >> sys.stderr, 'The build status file was not found.  You must do waf build before running test.py.'
        sys.exit(2)

    #
    # Make a dictionary that maps the name of a program to its path.
    #
    ns3_runnable_programs_dictionary = {}
    for program in ns3_runnable_programs:
        # Remove any directory names from path.
        program_name = os.path.basename(program)
        ns3_runnable_programs_dictionary[program_name] = program

    # Generate the lists of examples to run as smoke tests in order to
    # ensure that they remain buildable and runnable over time.
    #
    example_tests = []
    example_names_original = []
    python_tests = []
    for directory in EXAMPLE_DIRECTORIES:
        # Set the directories and paths for this example.
        example_directory   = os.path.join("examples", directory)
        examples_to_run_path = os.path.join(example_directory, "examples-to-run.py")
        cpp_executable_dir   = os.path.join(NS3_BUILDDIR, example_directory)
        python_script_dir    = os.path.join(example_directory)

        # Parse this example directory's file.
        parse_examples_to_run_file(
            examples_to_run_path,
            cpp_executable_dir,
            python_script_dir,
            example_tests,
            example_names_original,
            python_tests)

    for module in NS3_ENABLED_MODULES:
        # Remove the "ns3-" from the module name.
        module = module[len("ns3-"):]

        # Set the directories and paths for this example.
        module_directory     = os.path.join("src", module)
        example_directory    = os.path.join(module_directory, "examples")
        examples_to_run_path = os.path.join(module_directory, "test", "examples-to-run.py")
        cpp_executable_dir   = os.path.join(NS3_BUILDDIR, example_directory)
        python_script_dir    = os.path.join(example_directory)

        # Parse this module's file.
        parse_examples_to_run_file(
            examples_to_run_path,
            cpp_executable_dir,
            python_script_dir,
            example_tests,
            example_names_original,
            python_tests)

    #
    # If lots of logging is enabled, we can crash Python when it tries to
    # save all of the text.  We just don't allow logging to be turned on when
    # test.py runs.  If you want to see logging output from your tests, you
    # have to run them using the test-runner directly.
    #
    os.environ["NS_LOG"] = ""

    #
    # There are a couple of options that imply we can to exit before starting
    # up a bunch of threads and running tests.  Let's detect these cases and
    # handle them without doing all of the hard work.
    #
    if options.kinds:
        path_cmd = os.path.join("utils", test_runner_name + " --print-test-type-list")
        (rc, standard_out, standard_err, et) = run_job_synchronously(path_cmd, os.getcwd(), False, False)
        print standard_out

    if options.list:
        if len(options.constrain):
            path_cmd = os.path.join("utils", test_runner_name + " --print-test-name-list --print-test-types --test-type=%s" % options.constrain)
        else:
            path_cmd = os.path.join("utils", test_runner_name + " --print-test-name-list --print-test-types")
        (rc, standard_out, standard_err, et) = run_job_synchronously(path_cmd, os.getcwd(), False, False)
        list_items = standard_out.split('\n')
        list_items.sort()
        print "Test Type    Test Name"
        print "---------    ---------"
        for item in list_items:
            if len(item.strip()):
                print item
        example_names_original.sort()
        for item in example_names_original:
                print "example     ", item
        print

    if options.kinds or options.list:
        return

    #
    # We communicate results in two ways.  First, a simple message relating
    # PASS, FAIL, CRASH or SKIP is always written to the standard output.  It
    # is expected that this will be one of the main use cases.  A developer can
    # just run test.py with no options and see that all of the tests still
    # pass.
    #
    # The second main use case is when detailed status is requested (with the
    # --text or --html options).  Typicall this will be text if a developer
    # finds a problem, or HTML for nightly builds.  In these cases, an
    # XML file is written containing the status messages from the test suites.
    # This file is then read and translated into text or HTML.  It is expected
    # that nobody will really be interested in the XML, so we write it somewhere
    # with a unique name (time) to avoid collisions.  In case an error happens, we
    # provide a runtime option to retain the temporary files.
    #
    # When we run examples as smoke tests, they are going to want to create
    # lots and lots of trace files.  We aren't really interested in the contents
    # of the trace files, so we also just stash them off in the temporary dir.
    # The retain option also causes these unchecked trace files to be kept.
    #
    date_and_time = time.strftime("%Y-%m-%d-%H-%M-%S-CUT", time.gmtime())

    if not os.path.exists(TMP_OUTPUT_DIR):
        os.makedirs(TMP_OUTPUT_DIR)

    testpy_output_dir = os.path.join(TMP_OUTPUT_DIR, date_and_time);

    if not os.path.exists(testpy_output_dir):
        os.makedirs(testpy_output_dir)

    #
    # Create the main output file and start filling it with XML.  We need to
    # do this since the tests will just append individual results to this file.
    #
    xml_results_file = os.path.join(testpy_output_dir, "results.xml")
    f = open(xml_results_file, 'w')
    f.write('<?xml version="1.0"?>\n')
    f.write('<Results>\n')
    f.close()

    #
    # We need to figure out what test suites to execute.  We are either given one
    # suite or example explicitly via the --suite or --example/--pyexample option,
    # or we need to call into the test runner and ask it to list all of the available
    # test suites.  Further, we need to provide the constraint information if it
    # has been given to us.
    #
    # This translates into allowing the following options with respect to the
    # suites
    #
    #  ./test,py:                                           run all of the suites and examples
    #  ./test.py --constrain=core:                          run all of the suites of all kinds
    #  ./test.py --constrain=unit:                          run all unit suites
    #  ./test.py --suite=some-test-suite:                   run a single suite
    #  ./test.py --example=examples/udp/udp-echo:           run single example
    #  ./test.py --pyexample=examples/wireless/mixed-wireless.py:  run python example
    #  ./test.py --suite=some-suite --example=some-example: run the single suite
    #
    # We can also use the --constrain option to provide an ordering of test
    # execution quite easily.
    #
    if len(options.suite):
        # See if this is a valid test suite.
        path_cmd = os.path.join("utils", test_runner_name + " --print-test-name-list")
        (rc, suites, standard_err, et) = run_job_synchronously(path_cmd, os.getcwd(), False, False)
        if options.suite in suites.split('\n'):
            suites = options.suite + "\n"
        else:
            print >> sys.stderr, 'The test suite was not run because an unknown test suite name was requested.'
            sys.exit(2)

    elif len(options.example) == 0 and len(options.pyexample) == 0:
        if len(options.constrain):
            path_cmd = os.path.join("utils", test_runner_name + " --print-test-name-list --test-type=%s" % options.constrain)
            (rc, suites, standard_err, et) = run_job_synchronously(path_cmd, os.getcwd(), False, False)
        else:
            path_cmd = os.path.join("utils", test_runner_name + " --print-test-name-list")
            (rc, suites, standard_err, et) = run_job_synchronously(path_cmd, os.getcwd(), False, False)
    else:
        suites = ""

    #
    # suite_list will either a single test suite name that the user has
    # indicated she wants to run or a list of test suites provided by
    # the test-runner possibly according to user provided constraints.
    # We go through the trouble of setting up the parallel execution
    # even in the case of a single suite to avoid having two process the
    # results in two different places.
    #
    suite_list = suites.split('\n')

    #
    # Performance tests should only be run when they are requested,
    # i.e. they are not run by default in test.py.
    #
    if options.constrain != 'performance':

        # Get a list of all of the performance tests.
        path_cmd = os.path.join("utils", test_runner_name + " --print-test-name-list --test-type=%s" % "performance")
        (rc, performance_tests, standard_err, et) = run_job_synchronously(path_cmd, os.getcwd(), False, False)
        performance_test_list = performance_tests.split('\n')

        # Remove any performance tests from the suites list.
        for performance_test in performance_test_list:
            if performance_test in suite_list:
                suite_list.remove(performance_test)

    # We now have a possibly large number of test suites to run, so we want to
    # run them in parallel.  We're going to spin up a number of worker threads
    # that will run our test jobs for us.
    #
    input_queue = Queue.Queue(0)
    output_queue = Queue.Queue(0)

    jobs = 0
    threads=[]

    #
    # In Python 2.6 you can just use multiprocessing module, but we don't want
    # to introduce that dependency yet; so we jump through a few hoops.
    #
    processors = 1

    if sys.platform != "win32":
        if 'SC_NPROCESSORS_ONLN'in os.sysconf_names:
            processors = os.sysconf('SC_NPROCESSORS_ONLN')
        else:
            proc = subprocess.Popen("sysctl -n hw.ncpu", shell = True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
            stdout_results, stderr_results = proc.communicate()
            if len(stderr_results) == 0:
                processors = int(stdout_results)

    #
    # Now, spin up one thread per processor which will eventually mean one test
    # per processor running concurrently.
    #
    for i in range(processors):
        thread = worker_thread(input_queue, output_queue)
        threads.append(thread)
        thread.start()

    #
    # Keep track of some summary statistics
    #
    total_tests = 0
    skipped_tests = 0

    #
    # We now have worker threads spun up, and a list of work to do.  So, run
    # through the list of test suites and dispatch a job to run each one.
    #
    # Dispatching will run with unlimited speed and the worker threads will
    # execute as fast as possible from the queue.
    #
    # Note that we actually dispatch tests to be skipped, so all of the
    # PASS, FAIL, CRASH and SKIP processing is done in the same place.
    #
    for test in suite_list:
        test = test.strip()
        if len(test):
            job = Job()
            job.set_is_example(False)
            job.set_is_pyexample(False)
            job.set_display_name(test)
            job.set_tmp_file_name(os.path.join(testpy_output_dir, "%s.xml" % test))
            job.set_cwd(os.getcwd())
            job.set_basedir(os.getcwd())
            job.set_tempdir(testpy_output_dir)
            if (options.multiple):
                multiple = ""
            else:
                multiple = " --stop-on-failure"
            if (len(options.fullness)):
                fullness = options.fullness.upper()


#
# Newfies-Dialer License
# http://www.newfies-dialer.org
#
# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this file,
# You can obtain one at http://mozilla.org/MPL/2.0/.
#
# Copyright (C) 2011-2012 Star2Billing S.L.
#
# The primary maintainer of this project is
# Arezqui Belaid <info@star2billing.com>
#

from django.contrib import admin
from django.contrib import messages
from django.conf.urls import patterns
from django.core.urlresolvers import reverse
from django.utils.translation import ugettext as _
from django.contrib.admin.options import IncorrectLookupParameters
from django.contrib.admin.views.main import ERROR_FLAG
from django.template import RequestContext
from django.http import HttpResponse, HttpResponseRedirect
from django.shortcuts import render_to_response
from django.db.models import Count
from django.utils.translation import ungettext
from dialer_campaign.function_def import dialer_setting_limit
from django_lets_go.common_functions import variable_value
from mod_utils.helper import Export_choice
from mod_sms.models import SMSCampaign, SMSCampaignSubscriber, SMSMessage, SMSTemplate
from mod_sms.function_def import check_sms_dialer_setting,\
    sms_record_common_fun, sms_search_admin_form_fun
from mod_sms.forms import AdminSMSSearchForm
from genericadmin.admin import GenericAdminModelAdmin
from datetime import datetime
from django.utils.timezone import utc
import tablib

class SMSCampaignAdmin(GenericAdminModelAdmin):

    """
    Allows the administrator to view and modify certain attributes
    of a SMSCampaign.
    """
    fieldsets = (
        (_('Standard options'), {
            'fields': ('campaign_code', 'name', 'description', 'callerid',
                       'user', 'status', 'startingdate', 'expirationdate',
                       'sms_gateway', 'text_message',
                       'extra_data', 'phonebook',
                       ),
        }),
        (_('Advanced options'), {
            'classes': ('collapse',),
            'fields': ('frequency', 'maxretry',
                       'intervalretry', 'imported_phonebook', 'daily_start_time',
                       'daily_stop_time', 'monday', 'tuesday', 'wednesday',
                       'thursday', 'friday', 'saturday', 'sunday')
        }),
    )
    list_display = ('id', 'name', 'campaign_code', 'user',
                    'startingdate', 'expirationdate', 'frequency',
                    'maxretry', 'sms_gateway', 'status',
                    'update_sms_campaign_status', 'count_contact_of_phonebook',
                    'sms_campaignsubscriber_detail', 'progress_bar')

    list_display_links = ('id', 'name', )
    #list_filter = ['user', 'status', 'startingdate', 'created_date']
    ordering = ('id', )
    filter_horizontal = ('phonebook',)

    def get_urls(self):
        urls = super(SMSCampaignAdmin, self).get_urls()
        my_urls = patterns('',
                           (r'^$', self.admin_site.admin_view(self.changelist_view)),
                           (r'^add/$', self.admin_site.admin_view(self.add_view)),
                           )
        return my_urls + urls

    def add_view(self, request, extra_context=None):
        """
        Override django add_view method for checking the dialer setting limit

        **Logic Description**:

            * Before adding sms campaign, check dialer setting limit if applicable
              to the user, if matched then the user will be redirected to
              the sms campaign list
        """
        # Check dialer setting limit
        # check Max Number of running sms campaigns
        if check_sms_dialer_setting(request, check_for="smscampaign"):
            msg = _("you have too many sms campaigns. Max allowed %(limit)s") \
                % {'limit': dialer_setting_limit(request, limit_for="smscampaign")}
            messages.error(request, msg)

            # campaign limit reached
            #common_send_notification(request, '3')
            return HttpResponseRedirect(reverse("admin:sms_campaign_campaign_changelist"))
        ctx = {}
        return super(SMSCampaignAdmin, self).add_view(request, extra_context=ctx)
admin.site.register(SMSCampaign, SMSCampaignAdmin)

class SMSCampaignSubscriberAdmin(admin.ModelAdmin):

    """Allows the administrator to view and modify certain attributes
    of a SMSCampaignSubscriber."""
    list_display = ('id', 'contact', 'sms_campaign',
                    'last_attempt', 'count_attempt', 'duplicate_contact',
                    'contact_name', 'status', 'created_date')
    list_filter = ['sms_campaign', 'status', 'created_date', 'last_attempt']
    ordering = ('id', )

admin.site.register(SMSCampaignSubscriber, SMSCampaignSubscriberAdmin)

class SMSMessageAdmin(admin.ModelAdmin):

    """Allows the administrator to view and modify certain attributes
    of a SMSMessage."""
    can_add = True
    detail_title = _("SMS report")
    list_display = ('sender', 'recipient_number', 'send_date', 'uuid',
                    'status', 'status_message', 'sms_gateway')
    ordering = ('-id', )

    def has_add_permission(self, request):
        """Remove add permission on SMSMessage Report model

        **Logic Description**:

            * Override django admin has_add_permission method to remove add
              permission on SMSMessage Report model
        """
        if not self.can_add:
            return False
        return super(SMSMessageAdmin, self).has_add_permission(request)

    def get_urls(self):
        urls = super(SMSMessageAdmin, self).get_urls()
        my_urls = patterns('',
                           (r'^$', self.admin_site.admin_view(self.changelist_view)),
                           (r'^sms_daily_report/$', self.admin_site.admin_view(self.sms_daily_report)),
                           (r'^export_sms_report/$', self.admin_site.admin_view(self.export_sms_report)),
                           )
        return my_urls + urls

    def changelist_view(self, request, extra_context=None):
        """Override changelist_view method of django-admin for search parameters

        **Attributes**:

            * ``form`` - AdminSMSSearchForm
            * ``template`` - admin/mod_sms/smsmessage/sms_report.html

        **Logic Description**:

            * SMSMessage report Record Listing with search option
              search Parameters: by date, by status and by billed.
        """
        opts = SMSMessage._meta

        query_string = ''
        form = AdminSMSSearchForm(request.POST or None)
        if form.is_valid():
            query_string = sms_search_admin_form_fun(request)
            return HttpResponseRedirect(
                "/admin/" + opts.app_label + "/" + opts.object_name.lower() + "/?" + query_string)
        else:
            status = ''
            from_date = ''
            to_date = ''
            smscampaign = ''
            if request.GET.get('send_date__gte'):
                from_date = variable_value(request, 'send_date__gte')
            if request.GET.get('send_date__lte'):
                to_date = variable_value(request, 'send_date__lte')[0:10]
            if request.GET.get('status__exact'):
                status = variable_value(request, 'status__exact')
            if request.GET.get('sms_campaign'):
                smscampaign = variable_value(request, 'sms_campaign')
            form = AdminSMSSearchForm(initial={'status': status,
                                               'from_date': from_date,
                                               'to_date': to_date,
                                               'smscampaign': smscampaign})

        ChangeList = self.get_changelist(request)
        try:
            cl = ChangeList(request, self.model, self.list_display,
                            self.list_display_links, self.list_filter, self.date_hierarchy,
                            self.search_fields, self.list_select_related,
                            self.list_per_page, self.list_max_show_all, self.list_editable,
                            self)
        except IncorrectLookupParameters:
            if ERROR_FLAG in request.GET.keys():
                return render_to_response('admin/invalid_setup.html',
                                          {'title': _('Database error')})
            return HttpResponseRedirect(request.path + '?' + ERROR_FLAG + '=1')

        kwargs = {}
        if request.META['QUERY_STRING'] == '':
            tday = datetime.utcnow().replace(tzinfo=utc)
            kwargs['send_date__gte'] = datetime(tday.year, tday.month, tday.day,


""" Utility class for dealing with MyProxy
"""

from __future__ import print_function
from __future__ import absolute_import
from __future__ import division

__RCSID__ = "$Id$"

import re
from DIRAC import gLogger, S_OK, S_ERROR
from DIRAC.Core.Utilities.Subprocess import shellCall
from DIRAC.Core.Utilities import List
from DIRAC.Core.Security.ProxyFile import multiProxyArgument, deleteMultiProxy
from DIRAC.Core.Security.X509Chain import X509Chain  # pylint: disable=import-error
from DIRAC.Core.Security.BaseSecurity import BaseSecurity

class MyProxy(BaseSecurity):
    def uploadProxy(self, proxy=False, useDNAsUserName=False):
        """
        Upload a proxy to myproxy service.
          proxy param can be:
            : Default -> use current proxy
            : string -> upload file specified as proxy
            : X509Chain -> use chain
        """
        retVal = multiProxyArgument(proxy)
        if not retVal["OK"]:
            return retVal
        proxyDict = retVal["Value"]
        chain = proxyDict["chain"]
        proxyLocation = proxyDict["file"]

        timeLeft = int(chain.getRemainingSecs()["Value"] / 3600)

        cmdArgs = ["-n"]
        cmdArgs.append('-s "%s"' % self._secServer)
        cmdArgs.append('-c "%s"' % (timeLeft - 1))
        cmdArgs.append('-t "%s"' % self._secMaxProxyHours)
        cmdArgs.append('-C "%s"' % proxyLocation)
        cmdArgs.append('-y "%s"' % proxyLocation)
        if useDNAsUserName:
            cmdArgs.append("-d")
        else:
            retVal = self._getUsername(chain)
            if not retVal["OK"]:
                deleteMultiProxy(proxyDict)
                return retVal
            mpUsername = retVal["Value"]
            cmdArgs.append('-l "%s"' % mpUsername)

        mpEnv = self._getExternalCmdEnvironment()
        # Hack to upload properly
        mpEnv["GT_PROXY_MODE"] = "old"

        cmd = "myproxy-init %s" % " ".join(cmdArgs)
        result = shellCall(self._secCmdTimeout, cmd, env=mpEnv)

        deleteMultiProxy(proxyDict)

        if not result["OK"]:
            errMsg = "Call to myproxy-init failed: %s" % retVal["Message"]
            return S_ERROR(errMsg)

        status, _, error = result["Value"]

        # Clean-up files
        if status:
            errMsg = "Call to myproxy-init failed"
            extErrMsg = "Command: %s; StdOut: %s; StdErr: %s" % (cmd, result, error)
            return S_ERROR("%s %s" % (errMsg, extErrMsg))

        return S_OK()

    def getDelegatedProxy(self, proxyChain, lifeTime=604800, useDNAsUserName=False):
        """
        Get delegated proxy from MyProxy server
        return S_OK( X509Chain ) / S_ERROR
        """
        # TODO: Set the proxy coming in proxyString to be the proxy to use

        # Get myproxy username diracgroup:diracuser
        retVal = multiProxyArgument(proxyChain)
        if not retVal["OK"]:
            return retVal
        proxyDict = retVal["Value"]
        chain = proxyDict["chain"]
        proxyLocation = proxyDict["file"]

        retVal = self._generateTemporalFile()
        if not retVal["OK"]:
            deleteMultiProxy(proxyDict)
            return retVal
        newProxyLocation = retVal["Value"]

        # myproxy-get-delegation works only with environment variables
        cmdEnv = self._getExternalCmdEnvironment()
        if self._secRunningFromTrustedHost:
            cmdEnv["X509_USER_CERT"] = self._secCertLoc
            cmdEnv["X509_USER_KEY"] = self._secKeyLoc
            if "X509_USER_PROXY" in cmdEnv:
                del cmdEnv["X509_USER_PROXY"]
        else:
            cmdEnv["X509_USER_PROXY"] = proxyLocation

        cmdArgs = []
        cmdArgs.append("-s '%s'" % self._secServer)
        cmdArgs.append("-t '%s'" % (int(lifeTime / 3600)))
        cmdArgs.append("-a '%s'" % proxyLocation)
        cmdArgs.append("-o '%s'" % newProxyLocation)
        if useDNAsUserName:
            cmdArgs.append("-d")
        else:
            retVal = self._getUsername(chain)
            if not retVal["OK"]:
                deleteMultiProxy(proxyDict)
                return retVal
            mpUsername = retVal["Value"]
            cmdArgs.append('-l "%s"' % mpUsername)

        cmd = "myproxy-logon %s" % " ".join(cmdArgs)
        gLogger.verbose("myproxy-logon command:\n%s" % cmd)

        result = shellCall(self._secCmdTimeout, cmd, env=cmdEnv)

        deleteMultiProxy(proxyDict)

        if not result["OK"]:
            errMsg = "Call to myproxy-logon failed: %s" % result["Message"]
            deleteMultiProxy(proxyDict)
            return S_ERROR(errMsg)

        status, _, error = result["Value"]

        # Clean-up files
        if status:
            errMsg = "Call to myproxy-logon failed"
            extErrMsg = "Command: %s; StdOut: %s; StdErr: %s" % (cmd, result, error)
            deleteMultiProxy(proxyDict)
            return S_ERROR("%s %s" % (errMsg, extErrMsg))

        chain = X509Chain()
        retVal = chain.loadProxyFromFile(newProxyLocation)
        if not retVal["OK"]:
            deleteMultiProxy(proxyDict)
            return S_ERROR("myproxy-logon failed when reading delegated file: %s" % retVal["Message"])

        deleteMultiProxy(proxyDict)
        return S_OK(chain)

    def getInfo(self, proxyChain, useDNAsUserName=False):
        """
        Get info from myproxy server

        :return: S_OK( { 'username' : myproxyusername,
                       'owner' : owner DN,
                       'timeLeft' : secs left } ) / S_ERROR
        """
        # TODO: Set the proxy coming in proxyString to be the proxy to use

        # Get myproxy username diracgroup:diracuser
        retVal = multiProxyArgument(proxyChain)
        if not retVal["OK"]:
            return retVal
        proxyDict = retVal["Value"]
        chain = proxyDict["chain"]
        proxyLocation = proxyDict["file"]

        # myproxy-get-delegation works only with environment variables
        cmdEnv = self._getExternalCmdEnvironment()
        if self._secRunningFromTrustedHost:
            cmdEnv["X509_USER_CERT"] = self._secCertLoc
            cmdEnv["X509_USER_KEY"] = self._secKeyLoc
            if "X509_USER_PROXY" in cmdEnv:
                del cmdEnv["X509_USER_PROXY"]
        else:
            cmdEnv["X509_USER_PROXY"] = proxyLocation

        cmdArgs = []
        cmdArgs.append("-s '%s'" % self._secServer)
        if useDNAsUserName:
            cmdArgs.append("-d")
        else:
            retVal = self._getUsername(chain)
            if not retVal["OK"]:
                deleteMultiProxy(proxyDict)
                return retVal
            mpUsername = retVal["Value"]
            cmdArgs.append('-l "%s"' % mpUsername)

        cmd = "myproxy-info %s" % " ".join(cmdArgs)
        gLogger.verbose("myproxy-info command:\n%s" % cmd)

        result = shellCall(self._secCmdTimeout, cmd, env=cmdEnv)

        deleteMultiProxy(proxyDict)

        if not result["OK"]:
            errMsg = "Call to myproxy-info failed: %s" % result["Message"]
            deleteMultiProxy(proxyDict)
            return S_ERROR(errMsg)

        status, output, error = result["Value"]

        # Clean-up files
        if status:
            errMsg = "Call to myproxy-info failed"
            extErrMsg = "Command: %s; StdOut: %s; StdErr: %s" % (cmd, result, error)
            return S_ERROR("%s %s" % (errMsg, extErrMsg))

        infoDict = {}
        usernameRE = re.compile(r"username\s*:\s*(\S*)")
        ownerRE = re.compile(r"owner\s*:\s*(\S*)")
        timeLeftRE = re.compile(r"timeleft\s*:\s*(\S*)")
        for line in List.fromChar(output, "\n"):
            match = usernameRE.search(line)
            if match:
                infoDict["username"] = match.group(1)
            match = ownerRE.search(line)
            if match:
                infoDict["owner"] = match.group(1)
            match = timeLeftRE.search(line)
            if match:
                try:
                    fields = List.fromChar(match.group(1), ":")
                    fields.reverse()
                    secsLeft = 0
                    for iP in range(len(fields)):
                        if iP == 0:
                            secsLeft += int(fields[iP])
                        elif iP == 1:


# husk:
# Exit*2?  remove pylab.show()
# close button
# DFT
# ADOS
# grey-out stuff after one second: vmd, rasmol, ...
# Show with ....
# rasmol: set same rotation as ag
# Graphs: save, Python, 3D
# start from python (interactive mode?)
# ascii-art option (colored)|
# option -o (output) and -f (force overwrite)
# surfacebuilder
# screen-dump
# icon
# ag-community-server
# translate option: record all translations,
# and check for missing translations.

#TODO: Add possible way of choosing orinetations. \
#TODO: Two atoms defines a direction, three atoms their normal does
#TODO: Align orientations chosen in Rot_selected v unselcted
#TODO: Get the atoms_rotate_0 thing string
#TODO: Use set atoms instead og the get atoms
#TODO: Arrow keys will decide how the orientation changes
#TODO: Undo redo que should be implemented
#TODO: Update should have possibility to change positions
#TODO: Window for rotation modes and move moves which can be chosen
#TODO: WHen rotate and move / hide the movie menu

import os
import sys
import weakref
import pickle
from gettext import gettext as _
from gettext import ngettext

import numpy as np
import pygtk
pygtk.require("2.0")
import gtk

from ase.gui.view import View
from ase.gui.status import Status
from ase.gui.widgets import pack, help, Help, oops
from ase.gui.settings import Settings
from ase.gui.crystal import SetupBulkCrystal
from ase.gui.surfaceslab import SetupSurfaceSlab
from ase.gui.nanoparticle import SetupNanoparticle
from ase.gui.nanotube import SetupNanotube
from ase.gui.graphene import SetupGraphene
from ase.gui.calculator import SetCalculator
from ase.gui.energyforces import EnergyForces
from ase.gui.minimize import Minimize
from ase.gui.scaling import HomogeneousDeformation
from ase.gui.quickinfo import QuickInfo
from ase.version import version

ui_info = """\
<ui>
  <menubar name='MenuBar'>
    <menu action='FileMenu'>
      <menuitem action='Open'/>
      <menuitem action='New'/>
      <menuitem action='Save'/>
      <separator/>
      <menuitem action='Quit'/>
    </menu>
    <menu action='EditMenu'>
      <menuitem action='SelectAll'/>
      <menuitem action='Invert'/>
      <menuitem action='SelectConstrained'/>
      <menuitem action='SelectImmobile'/>
      <separator/>
      <menuitem action='Copy'/>
      <menuitem action='Paste'/>
      <separator/>
      <menuitem action='HideAtoms'/>
      <menuitem action='ShowAtoms'/>
      <separator/>
      <menuitem action='Modify'/>
      <menuitem action='AddAtoms'/>
      <menuitem action='DeleteAtoms'/>
      <separator/>
      <menuitem action='First'/>
      <menuitem action='Previous'/>
      <menuitem action='Next'/>
      <menuitem action='Last'/>
    </menu>
    <menu action='ViewMenu'>
      <menuitem action='ShowUnitCell'/>
      <menuitem action='ShowAxes'/>
      <menuitem action='ShowBonds'/>
      <menuitem action='ShowVelocities'/>
      <menuitem action='ShowForces'/>
      <menu action='ShowLabels'>
        <menuitem action='NoLabel'/>
        <menuitem action='AtomIndex'/>
        <menuitem action='MagMom'/>
        <menuitem action='Element'/>
      </menu>
      <separator/>
      <menuitem action='QuickInfo'/>
      <menuitem action='Repeat'/>
      <menuitem action='Rotate'/>
      <menuitem action='Colors'/>
      <menuitem action='Focus'/>
      <menuitem action='ZoomIn'/>
      <menuitem action='ZoomOut'/>
      <menu action='ChangeView'>
        <menuitem action='ResetView'/>
        <menuitem action='xyPlane'/>
        <menuitem action='yzPlane'/>
        <menuitem action='zxPlane'/>
        <menuitem action='yxPlane'/>
        <menuitem action='zyPlane'/>
        <menuitem action='xzPlane'/>
        <menuitem action='a2a3Plane'/>
        <menuitem action='a3a1Plane'/>
        <menuitem action='a1a2Plane'/>
        <menuitem action='a3a2Plane'/>
        <menuitem action='a2a1Plane'/>
        <menuitem action='a1a3Plane'/>
      </menu>
      <menuitem action='Settings'/>
      <menuitem action='VMD'/>
      <menuitem action='RasMol'/>
      <menuitem action='XMakeMol'/>
      <menuitem action='Avogadro'/>
    </menu>
    <menu action='ToolsMenu'>
      <menuitem action='Graphs'/>
      <menuitem action='Movie'/>
      <menuitem action='EModify'/>
      <menuitem action='Constraints'/>
      <menuitem action='RenderScene'/>
      <menuitem action='MoveAtoms'/>
      <menuitem action='RotateAtoms'/>
      <menuitem action='OrientAtoms'/>
      <menuitem action='DFT'/>
      <menuitem action='NEB'/>
      <menuitem action='BulkModulus'/>
    </menu>
    <menu action='SetupMenu'>
      <menuitem action='Bulk'/>
      <menuitem action='Surface'/>
      <menuitem action='Nanoparticle'/>
      <menuitem action='Graphene'/>
      <menuitem action='Nanotube'/>
    </menu>
    <menu action='CalculateMenu'>
      <menuitem action='SetCalculator'/>
      <separator/>
      <menuitem action='EnergyForces'/>
      <menuitem action='Minimize'/>
      <menuitem action='Scaling'/>
    </menu>
    <menu action='HelpMenu'>
      <menuitem action='About'/>
      <menuitem action='Webpage'/>
      <menuitem action='Debug'/>
    </menu>
  </menubar>
</ui>"""

class GUI(View, Status):
    def __init__(self, images, rotations='', show_unit_cell=True,
                 show_bonds=False):
        # Try to change into directory of file you are viewing
        try:
            os.chdir(os.path.split(sys.argv[1])[0])
        # This will fail sometimes (e.g. for starting a new session)
        except:
            pass
        self.images = images
        self.window = gtk.Window(gtk.WINDOW_TOPLEVEL)
        #self.window.set_icon(gtk.gdk.pixbuf_new_from_file('guiase.png'))
        self.window.set_position(gtk.WIN_POS_CENTER)
        #self.window.connect("destroy", lambda w: gtk.main_quit())
        self.window.connect('delete_event', self.exit)
        vbox = gtk.VBox()
        self.window.add(vbox)
        if gtk.pygtk_version < (2, 12):
            self.set_tip = gtk.Tooltips().set_tip

        actions = gtk.ActionGroup("Actions")
        actions.add_actions([
            ('FileMenu', None, _('_File')),
            ('EditMenu', None, _('_Edit')),
            ('ViewMenu', None, _('_View')),
            ('ToolsMenu', None, _('_Tools')),
            # TRANSLATORS: Set up (i.e. build) surfaces, nanoparticles, ...
            ('SetupMenu', None, _('_Setup')),
            ('CalculateMenu', None, _('_Calculate')),
            ('HelpMenu', None, _('_Help')),
            ('Open', gtk.STOCK_OPEN, _('_Open'), '<control>O',
             _('Create a new file'),
             self.open),
             ('New', gtk.STOCK_NEW, _('_New'), '<control>N',
             _('New ase.gui window'),
             lambda widget: os.system('ag &')),
            ('Save', gtk.STOCK_SAVE, _('_Save'), '<control>S',
             _('Save current file'),
             self.save),
            ('Quit', gtk.STOCK_QUIT, _('_Quit'), '<control>Q',
             _('Quit'),
             self.exit),
            ('SelectAll', None, _('Select _all'), None,
             '',
             self.select_all),
            ('Invert', None, _('_Invert selection'), None,
             '',
             self.invert_selection),
            ('SelectConstrained', None, _('Select _constrained atoms'), None,
             '',
             self.select_constrained_atoms),
            ('SelectImmobile', None, _('Select _immobile atoms'), '<control>I',
             '',
             self.select_immobile_atoms),
             ('Copy', None, _('_Copy'), '<control>C',
              _('Copy current selection and its orientation to clipboard'),
              self.copy_atoms),
             ('Paste', None, _('_Paste'), '<control>V',
              _('Insert current clipboard selection'),
              self.paste_atoms),
             ('Modify', None, _('_Modify'), '<control>Y',
              _('Change tags, moments and atom types of the selected atoms'),
              self.modify_atoms),
             ('AddAtoms', None, _('_Add atoms'), '<control>A',
              _('Insert or import atoms and molecules'),
              self.add_atoms),
             ('DeleteAtoms', None, _('_Delete selected atoms'), 'BackSpace',
              _('Delete the selected atoms'),
              self.delete_selected_atoms),
            ('First', gtk.STOCK_GOTO_FIRST, _('_First image'), 'Home',
             '',
             self.step),
            ('Previous', gtk.STOCK_GO_BACK, _('_Previous image'), 'Page_Up',
             '',
             self.step),
            ('Next', gtk.STOCK_GO_FORWARD, _('_Next image'), 'Page_Down',
             '',
             self.step),
            ('Last', gtk.STOCK_GOTO_LAST, _('_Last image'), 'End',
             '',
             self.step),
            ('ShowLabels', None, _('Show _Labels')),
            ('HideAtoms', None, _('Hide selected atoms'), None,
             '',
             self.hide_selected),
            ('ShowAtoms', None, _('Show selected atoms'), None,
             '',
             self.show_selected),
            ('QuickInfo', None, _('Quick Info ...'), None,
             '',
             self.quick_info_window),
            ('Repeat', None, _('Repeat ...'), None,
             '',
             self.repeat_window),
            ('Rotate', None, _('Rotate ...'), None,
             '',
             self.rotate_window),
            ('Colors', None, _('Colors ...'), None, '',
             self.colors_window),
            # TRANSLATORS: verb
            ('Focus', gtk.STOCK_ZOOM_FIT, _('Focus'), 'F',
             '',
             self.focus),
            ('ZoomIn', gtk.STOCK_ZOOM_IN, _('Zoom in'), 'plus',
             '',
             self.zoom),
            ('ZoomOut', gtk.STOCK_ZOOM_OUT, _('Zoom out'), 'minus',
             '',
             self.zoom),
            ('ChangeView', None, _('Change View')),
            ('ResetView', None, _('Reset View'), 'equal',
             '',
             self.reset_view),
            ('xyPlane', None, _('\'xy\' Plane'), 'z', '', self.set_view),
            ('yzPlane', None, _('\'yz\' Plane'), 'x', '', self.set_view),
            ('zxPlane', None, _('\'zx\' Plane'), 'y', '', self.set_view),
            ('yxPlane', None, _('\'yx\' Plane'), '<alt>z', '', self.set_view),
            ('zyPlane', None, _('\'zy\' Plane'), '<alt>x', '', self.set_view),
            ('xzPlane', None, _('\'xz\' Plane'), '<alt>y', '', self.set_view),
            ('a2a3Plane', None, _('\'a2 a3\' Plane'), '1', '', self.set_view),
            ('a3a1Plane', None, _('\'a3 a1\' Plane'), '2', '', self.set_view),
            ('a1a2Plane', None, _('\'a1 a2\' Plane'), '3', '', self.set_view),
            ('a3a2Plane', None, _('\'a3 a2\' Plane'), '<alt>1', '', self.set_view),
            ('a1a3Plane', None, _('\'a1 a3\' Plane'), '<alt>2', '', self.set_view),
            ('a2a1Plane', None, _('\'a2 a1\' Plane'), '<alt>3', '', self.set_view),
            ('Settings', gtk.STOCK_PREFERENCES, _('Settings ...'), None,
             '',
             self.settings),
            ('VMD', None, _('VMD'), None,
             '',
             self.external_viewer),
            ('RasMol', None, _('RasMol'), None,
             '',
             self.external_viewer),
            ('XMakeMol', None, _('xmakemol'), None,
             '',
             self.external_viewer),
            ('Avogadro', None, _('avogadro'), None,
             '',
             self.external_viewer),
            ('Graphs', None, _('Graphs ...'), None,
             '',
             self.plot_graphs),
            ('Movie', None, _('Movie ...'), None,
             '',
             self.movie),
            ('EModify', None, _('Expert mode ...'), '<control>E',
             '',
             self.execute),
            ('Constraints', None, _('Constraints ...'), None,
             '',
             self.constraints_window),
            ('RenderScene', None, _('Render scene ...'), None,
             '',
             self.render_window),
            ('DFT', None, _('DFT ...'), None,
             '',
             self.dft_window),
            ('NEB', None, _('NE_B'), None,
             '',
             self.NEB),
            ('BulkModulus', None, _('B_ulk Modulus'), None,
             '',
             self.bulk_modulus),
            ('Bulk', None, _('_Bulk Crystal'), None,
             _("Create a bulk crystal with arbitrary orientation"),
             self.bulk_window),
            ('Surface', None, _('_Surface slab'), None,
             _("Create the most common surfaces"),
             self.surface_window),
            ('Nanoparticle', None, _('_Nanoparticle'), None,
             _("Create a crystalline nanoparticle"),
             self.nanoparticle_window),
            ('Nanotube', None, _('Nano_tube'), None,
             _("Create a nanotube"),
             self.nanotube_window),
            ('Graphene', None, _('Graphene'), None,
             _("Create a graphene sheet or nanoribbon"),
             self.graphene_window),
            ('SetCalculator', None, _('Set _Calculator'), None,
             _("Set a calculator used in all calculation modules"),
             self.calculator_window),
            ('EnergyForces', None, _('_Energy and Forces'), None,
             _("Calculate energy and forces"),
             self.energy_window),
            ('Minimize', None, _('Energy _Minimization'), None,
             _("Minimize the energy"),
             self.energy_minimize_window),
            ('Scaling', None, _('Scale system'), None,
             _("Deform system by scaling it"),
             self.scaling_window),
            ('About', None, _('_About'), None,
             None,
             self.about),
            ('Webpage', gtk.STOCK_HELP, _('Webpage ...'), None, None, webpage),
            ('Debug', None, _('Debug ...'), None, None, self.debug)])
        actions.add_toggle_actions([
            ('ShowUnitCell', None, _('Show _unit cell'), '<control>U',
             'Bold',
             self.toggle_show_unit_cell,
             show_unit_cell > 0),
            ('ShowAxes', None, _('Show _axes'), None,
             'Bold',
             self.toggle_show_axes,
             True),
            ('ShowBonds', None, _('Show _bonds'), '<control>B',
             'Bold',
             self.toggle_show_bonds,
             show_bonds),
            ('ShowVelocities', None, _('Show _velocities'),
             '<control>G', 'Bold',
             self.toggle_show_velocities,
             False),
            ('ShowForces', None, _('Show _forces'), '<control>F',
             'Bold',
             self.toggle_show_forces,
             False),
            ('MoveAtoms', None, _('_Move atoms'), '<control>M',
             'Bold',
             self.toggle_move_mode,
             False),
            ('RotateAtoms', None, _('_Rotate atoms'), '<control>R',
             'Bold',
             self.toggle_rotate_mode,
             False),
            ('OrientAtoms', None, _('Orien_t atoms'), '<control>T',
             'Bold',
             self.toggle_orient_mode,
             False)
            ])
        actions.add_radio_actions((
            ('NoLabel', None, _('_None'), None, None, 0),
            ('AtomIndex', None, _('Atom _Index'), None, None, 1),
            ('MagMom', None, _('_Magnetic Moments'), None, None, 2),
            ('Element', None, _('_Element Symbol'), None, None, 3)),
            0, self.show_labels)
        self.ui = ui = gtk.UIManager()
        ui.insert_action_group(actions, 0)
        self.window.add_accel_group(ui.get_accel_group())

        try:
            mergeid = ui.add_ui_from_string(ui_info)
        except gobject.GError, msg:
            print _('building menus failed: %s') % msg

        vbox.pack_start(ui.get_widget('/MenuBar'), False, False, 0)

        View.__init__(self, vbox, rotations)
        Status.__init__(self, vbox)
        vbox.show()
        #self.window.set_events(gtk.gdk.BUTTON_PRESS_MASK)
        self.window.connect('key-press-event', self.scroll)
        self.window.connect('scroll_event', self.scroll_event)
        self.window.show()
        self.graphs = []       # List of open pylab windows
        self.graph_wref = []   # List of weakrefs to Graph objects
        self.movie_window = None
        self.vulnerable_windows = []
        self.simulation = {}   # Used by modules on Calculate menu.
        self.module_state = {} # Used by modules to store their state.

    def run(self, expr=None):
        self.set_colors()
        self.set_coordinates(self.images.nimages - 1, focus=True)

        if self.images.nimages > 1:
            self.movie()

        if expr is None and not np.isnan(self.images.E[0]):
            expr = self.config['gui_graphs_string']

        if expr is not None and expr != '' and self.images.nimages > 1:
            self.plot_graphs(expr=expr)

        gtk.main()

    def step(self, action):
        d = {'First': -10000000,
             'Previous': -1,
             'Next': 1,
             'Last': 10000000}[action.get_name()]
        i = max(0, min(self.images.nimages - 1, self.frame + d))
        self.set_frame(i)
        if self.movie_window is not None:
            self.movie_window.frame_number.value = i

    def _do_zoom(self, x):
        """Utility method for zooming"""
        self.scale *= x
        self.draw()

    def zoom(self, action):
        """Zoom in/out on keypress or clicking menu item"""
        x = {'ZoomIn': 1.2, 'ZoomOut':1 /1.2}[action.get_name()]
        self._do_zoom(x)

    def scroll_event(self, window, event):
        """Zoom in/out when using mouse wheel"""
        SHIFT = event.state == gtk.gdk.SHIFT_MASK
        x = 1.0
        if event.direction == gtk.gdk.SCROLL_UP:
            x = 1.0 + (1-SHIFT)*0.2 + SHIFT*0.01
        elif event.direction == gtk.gdk.SCROLL_DOWN: