<commit_before>"""Test that lldb functions correctly after the inferior has asserted."""

from __future__ import print_function

import os, time
import lldb
import lldbsuite.test.lldbutil as lldbutil
import lldbsuite.test.lldbplatformutil as lldbplatformutil
from lldbsuite.test.lldbtest import *

class AssertingInferiorTestCase(TestBase):

    mydir = TestBase.compute_mydir(__file__)

    @expectedFailureWindows("llvm.org/pr21793: need to implement support for detecting assertion / abort on Windows")
    @expectedFailureLinux("llvm.org/pr25338", archs=['arm'])
    def test_inferior_asserting(self):
        """Test that lldb reliably catches the inferior asserting (command)."""
        self.build()
        self.inferior_asserting()

    @expectedFailureWindows("llvm.org/pr21793: need to implement support for detecting assertion / abort on Windows")
    @expectedFailureAndroid(api_levels=list(range(16 + 1))) # b.android.com/179836
    def test_inferior_asserting_register(self):
        """Test that lldb reliably reads registers from the inferior after asserting (command)."""
        self.build()
        self.inferior_asserting_registers()

    @expectedFailureWindows("llvm.org/pr21793: need to implement support for detecting assertion / abort on Windows")
    @expectedFailureLinux("llvm.org/pr25338", archs=['aarch64', 'arm'])
    def test_inferior_asserting_disassemble(self):
        """Test that lldb reliably disassembles frames after asserting (command)."""
        self.build()
        self.inferior_asserting_disassemble()

    @add_test_categories(['pyapi'])
    @expectedFailureWindows("llvm.org/pr21793: need to implement support for detecting assertion / abort on Windows")
    def test_inferior_asserting_python(self):
        """Test that lldb reliably catches the inferior asserting (Python API)."""
        self.build()
        self.inferior_asserting_python()

    @expectedFailureWindows("llvm.org/pr21793: need to implement support for detecting assertion / abort on Windows")
    @expectedFailureLinux("llvm.org/pr25338", archs=['aarch64', 'arm'])
    def test_inferior_asserting_expr(self):
        """Test that the lldb expression interpreter can read from the inferior after asserting (command)."""
        self.build()
        self.inferior_asserting_expr()

    @expectedFailureWindows("llvm.org/pr21793: need to implement support for detecting assertion / abort on Windows")
    @expectedFailureLinux("llvm.org/pr25338", archs=['aarch64', 'arm'])
    def test_inferior_asserting_step(self):
        """Test that lldb functions correctly after stepping through a call to assert()."""
        self.build()
        self.inferior_asserting_step()

    def set_breakpoint(self, line):
        lldbutil.run_break_set_by_file_and_line (self, "main.c", line, num_expected_locations=1, loc_exact=True)

    def check_stop_reason(self):
        match_failure_reason = matchAndroid(api_levels=list(range(1, 16+1)))(self)
        if match_failure_reason is None:
            # On android until API-16 the abort() call ended in a sigsegv instead of in a sigabrt
            stop_reason = 'stop reason = signal SIGSEGV'
        else:
            stop_reason = 'stop reason = signal SIGABRT'

        # The stop reason of the thread should be an abort signal or exception.
        self.expect("thread list", STOPPED_DUE_TO_ASSERT,
            substrs = ['stopped',
                       stop_reason])

        return stop_reason

    def setUp(self):
        # Call super's setUp().
        TestBase.setUp(self)
        # Find the line number of the call to assert.
        self.line = line_number('main.c', '// Assert here.')

    def inferior_asserting(self):
        """Inferior asserts upon launching; lldb should catch the event and stop."""
        exe = os.path.join(os.getcwd(), "a.out")
        self.runCmd("file " + exe, CURRENT_EXECUTABLE_SET)

        self.runCmd("run", RUN_SUCCEEDED)
        stop_reason = self.check_stop_reason()

        # And it should report a backtrace that includes the assert site.
        self.expect("thread backtrace all",
            substrs = [stop_reason, 'main', 'argc', 'argv'])

        # And it should report the correct line number.
        self.expect("thread backtrace all",
            substrs = [stop_reason,
                       'main.c:%d' % self.line])

    def inferior_asserting_python(self):
        """Inferior asserts upon launching; lldb should catch the event and stop."""
        exe = os.path.join(os.getcwd(), "a.out")

        target = self.dbg.CreateTarget(exe)
        self.assertTrue(target, VALID_TARGET)

        # Now launch the process, and do not stop at entry point.
        # Both argv and envp are null.
        process = target.LaunchSimple (None, None, self.get_process_working_directory())

        if process.GetState() != lldb.eStateStopped:
            self.fail("Process should be in the 'stopped' state, "
                      "instead the actual state is: '%s'" %
                      lldbutil.state_type_to_str(process.GetState()))

        thread = lldbutil.get_stopped_thread(process, lldb.eStopReasonSignal)
        if not thread:
            self.fail("Fail to stop the thread upon assert")

        if self.TraceOn():
            lldbutil.print_stacktrace(thread)

    def inferior_asserting_registers(self):
        """Test that lldb can read registers after asserting."""
        exe = os.path.join(os.getcwd(), "a.out")
        self.runCmd("file " + exe, CURRENT_EXECUTABLE_SET)

        self.runCmd("run", RUN_SUCCEEDED)
        self.check_stop_reason()

        # lldb should be able to read from registers from the inferior after asserting.
        lldbplatformutil.check_first_register_readable(self)

    def inferior_asserting_disassemble(self):
        """Test that lldb can disassemble frames after asserting."""
        exe = os.path.join(os.getcwd(), "a.out")

        # Create a target by the debugger.
        target = self.dbg.CreateTarget(exe)
        self.assertTrue(target, VALID_TARGET)

        # Launch the process, and do not stop at the entry point.
        target.LaunchSimple (None, None, self.get_process_working_directory())
        self.check_stop_reason()

        process = target.GetProcess()
        self.assertTrue(process.IsValid(), "current process is valid")

        thread = process.GetThreadAtIndex(0)
        self.assertTrue(thread.IsValid(), "current thread is valid")

        lastframeID = thread.GetFrameAtIndex(thread.GetNumFrames() - 1).GetFrameID()

        isi386Arch = False
        if "i386" in self.getArchitecture():
            isi386Arch = True

        # lldb should be able to disassemble frames from the inferior after asserting.
        for frame in thread:
            self.assertTrue(frame.IsValid(), "current frame is valid")

            self.runCmd("frame select " + str(frame.GetFrameID()), RUN_SUCCEEDED)

            # Don't expect the function name to be in the disassembly as the assert
            # function might be a no-return function where the PC is past the end
            # of the function and in the next function. We also can't back the PC up
            # because we don't know how much to back it up by on targets with opcodes
            # that have differing sizes
            pc_backup_offset = 1
            if frame.GetFrameID() == 0:
                pc_backup_offset = 0
            if isi386Arch == True:
                if lastframeID == frame.GetFrameID():
                    pc_backup_offset = 0
            self.expect("disassemble -a %s" % (frame.GetPC() - pc_backup_offset),
                    substrs = ['<+0>: '])

    def check_expr_in_main(self, thread):
        depth = thread.GetNumFrames()
        for i in range(depth):
            frame = thread.GetFrameAtIndex(i)
            self.assertTrue(frame.IsValid(), "current frame is valid")
            if self.TraceOn():
                print("Checking if function %s is main" % frame.GetFunctionName())

            if 'main' == frame.GetFunctionName():
                frame_id = frame.GetFrameID()
                self.runCmd("frame select " + str(frame_id), RUN_SUCCEEDED)
                self.expect("p argc", substrs = ['(int)', ' = 1'])
                self.expect("p hello_world", substrs = ['Hello'])
                self.expect("p argv[0]", substrs = ['a.out'])
                self.expect("p null_ptr", substrs = ['= 0x0'])
                return True
        return False

    def inferior_asserting_expr(self):
        """Test that the lldb expression interpreter can read symbols after asserting."""
        exe = os.path.join(os.getcwd(), "a.out")

        # Create a target by the debugger.
        target = self.dbg.CreateTarget(exe)
        self.assertTrue(target, VALID_TARGET)

        # Launch the process, and do not stop at the entry point.
        target.LaunchSimple (None, None, self.get_process_working_directory())
        self.check_stop_reason()

        process = target.GetProcess()
        self.assertTrue(process.IsValid(), "current process is valid")

        thread = process.GetThreadAtIndex(0)
        self.assertTrue(thread.IsValid(), "current thread is valid")

        # The lldb expression interpreter should be able to read from addresses of the inferior after a call to assert().
        self.assertTrue(self.check_expr_in_main(thread), "cannot find 'main' in the backtrace")

    def inferior_asserting_step(self):
        """Test that lldb functions correctly after stepping through a call to assert()."""
        exe = os.path.join(os.getcwd(), "a.out")

        # Create a target by the debugger.
        target = self.dbg.CreateTarget(exe)
        self.assertTrue(target, VALID_TARGET)

        # Launch the process, and do not stop at the entry point.
        self.set_breakpoint(self.line)
        target.LaunchSimple (None, None, self.get_process_working_directory())

        self.expect("thread list", STOPPED_DUE_TO_BREAKPOINT,
            substrs = ['main.c:%d' % self.line,
                       'stop reason = breakpoint'])

        self.runCmd("next")
        stop_reason = self.check_stop_reason()

        # lldb should be able to read from registers from the inferior after asserting.
        if "x86_64" in self.getArchitecture():
            self.expect("register read rbp", substrs = ['rbp = 0x'])
        if "i386" in self.getArchitecture():
            self.expect("register read ebp", substrs = ['ebp = 0x'])

        process = target.GetProcess()
        self.assertTrue(process.IsValid(), "current process is valid")

        thread = process.GetThreadAtIndex(0)
        self.assertTrue(thread.IsValid(), "current thread is valid")

        # The lldb expression interpreter should be able to read from addresses of the inferior after a call to assert().
        self.assertTrue(self.check_expr_in_main(thread), "cannot find 'main' in the backtrace")

        # And it should report the correct line number.
        self.expect("thread backtrace all",
            substrs = [stop_reason,
                       'main.c:%d' % self.line])
<commit_msg>Fix inverted conditional in TestInferiorAssert.py<commit_after>"""Test that lldb functions correctly after the inferior has asserted."""

from __future__ import print_function

import os, time
import lldb
import lldbsuite.test.lldbutil as lldbutil
import lldbsuite.test.lldbplatformutil as lldbplatformutil
from lldbsuite.test.lldbtest import *

class AssertingInferiorTestCase(TestBase):

    mydir = TestBase.compute_mydir(__file__)

    @expectedFailureWindows("llvm.org/pr21793: need to implement support for detecting assertion / abort on Windows")
    @expectedFailureLinux("llvm.org/pr25338", archs=['arm'])
    def test_inferior_asserting(self):
        """Test that lldb reliably catches the inferior asserting (command)."""
        self.build()
        self.inferior_asserting()

    @expectedFailureWindows("llvm.org/pr21793: need to implement support for detecting assertion / abort on Windows")
    @expectedFailureAndroid(api_levels=list(range(16 + 1))) # b.android.com/179836
    def test_inferior_asserting_register(self):
        """Test that lldb reliably reads registers from the inferior after asserting (command)."""
        self.build()
        self.inferior_asserting_registers()

    @expectedFailureWindows("llvm.org/pr21793: need to implement support for detecting assertion / abort on Windows")
    @expectedFailureLinux("llvm.org/pr25338", archs=['aarch64', 'arm'])
    def test_inferior_asserting_disassemble(self):
        """Test that lldb reliably disassembles frames after asserting (command)."""
        self.build()
        self.inferior_asserting_disassemble()

    @add_test_categories(['pyapi'])
    @expectedFailureWindows("llvm.org/pr21793: need to implement support for detecting assertion / abort on Windows")
    def test_inferior_asserting_python(self):
        """Test that lldb reliably catches the inferior asserting (Python API)."""
        self.build()
        self.inferior_asserting_python()

    @expectedFailureWindows("llvm.org/pr21793: need to implement support for detecting assertion / abort on Windows")
    @expectedFailureLinux("llvm.org/pr25338", archs=['aarch64', 'arm'])
    def test_inferior_asserting_expr(self):
        """Test that the lldb expression interpreter can read from the inferior after asserting (command)."""
        self.build()
        self.inferior_asserting_expr()

    @expectedFailureWindows("llvm.org/pr21793: need to implement support for detecting assertion / abort on Windows")
    @expectedFailureLinux("llvm.org/pr25338", archs=['aarch64', 'arm'])
    def test_inferior_asserting_step(self):
        """Test that lldb functions correctly after stepping through a call to assert()."""
        self.build()
        self.inferior_asserting_step()

    def set_breakpoint(self, line):
        lldbutil.run_break_set_by_file_and_line (self, "main.c", line, num_expected_locations=1, loc_exact=True)

    def check_stop_reason(self):
        match_result = matchAndroid(api_levels=list(range(1, 16+1)))(self)
        if match_result is not None:
            # On android until API-16 the abort() call ended in a sigsegv instead of in a sigabrt
            stop_reason = 'stop reason = signal SIGSEGV'
        else:
            stop_reason = 'stop reason = signal SIGABRT'

        # The stop reason of the thread should be an abort signal or exception.
        self.expect("thread list", STOPPED_DUE_TO_ASSERT,
            substrs = ['stopped',
                       stop_reason])

        return stop_reason

    def setUp(self):
        # Call super's setUp().
        TestBase.setUp(self)
        # Find the line number of the call to assert.
        self.line = line_number('main.c', '// Assert here.')

    def inferior_asserting(self):
        """Inferior asserts upon launching; lldb should catch the event and stop."""
        exe = os.path.join(os.getcwd(), "a.out")
        self.runCmd("file " + exe, CURRENT_EXECUTABLE_SET)

        self.runCmd("run", RUN_SUCCEEDED)
        stop_reason = self.check_stop_reason()

        # And it should report a backtrace that includes the assert site.
        self.expect("thread backtrace all",
            substrs = [stop_reason, 'main', 'argc', 'argv'])

        # And it should report the correct line number.
        self.expect("thread backtrace all",
            substrs = [stop_reason,
                       'main.c:%d' % self.line])

    def inferior_asserting_python(self):
        """Inferior asserts upon launching; lldb should catch the event and stop."""
        exe = os.path.join(os.getcwd(), "a.out")

        target = self.dbg.CreateTarget(exe)
        self.assertTrue(target, VALID_TARGET)

        # Now launch the process, and do not stop at entry point.
        # Both argv and envp are null.
        process = target.LaunchSimple (None, None, self.get_process_working_directory())

        if process.GetState() != lldb.eStateStopped:
            self.fail("Process should be in the 'stopped' state, "
                      "instead the actual state is: '%s'" %
                      lldbutil.state_type_to_str(process.GetState()))

        thread = lldbutil.get_stopped_thread(process, lldb.eStopReasonSignal)
        if not thread:
            self.fail("Fail to stop the thread upon assert")

        if self.TraceOn():
            lldbutil.print_stacktrace(thread)

    def inferior_asserting_registers(self):
        """Test that lldb can read registers after asserting."""
        exe = os.path.join(os.getcwd(), "a.out")
        self.runCmd("file " + exe, CURRENT_EXECUTABLE_SET)

        self.runCmd("run", RUN_SUCCEEDED)
        self.check_stop_reason()

        # lldb should be able to read from registers from the inferior after asserting.
        lldbplatformutil.check_first_register_readable(self)

    def inferior_asserting_disassemble(self):
        """Test that lldb can disassemble frames after asserting."""
        exe = os.path.join(os.getcwd(), "a.out")

        # Create a target by the debugger.
        target = self.dbg.CreateTarget(exe)
        self.assertTrue(target, VALID_TARGET)

        # Launch the process, and do not stop at the entry point.
        target.LaunchSimple (None, None, self.get_process_working_directory())
        self.check_stop_reason()

        process = target.GetProcess()
        self.assertTrue(process.IsValid(), "current process is valid")

        thread = process.GetThreadAtIndex(0)
        self.assertTrue(thread.IsValid(), "current thread is valid")

        lastframeID = thread.GetFrameAtIndex(thread.GetNumFrames() - 1).GetFrameID()

        isi386Arch = False
        if "i386" in self.getArchitecture():
            isi386Arch = True

        # lldb should be able to disassemble frames from the inferior after asserting.
        for frame in thread:
            self.assertTrue(frame.IsValid(), "current frame is valid")

            self.runCmd("frame select " + str(frame.GetFrameID()), RUN_SUCCEEDED)

            # Don't expect the function name to be in the disassembly as the assert
            # function might be a no-return function where the PC is past the end
            # of the function and in the next function. We also can't back the PC up
            # because we don't know how much to back it up by on targets with opcodes
            # that have differing sizes
            pc_backup_offset = 1
            if frame.GetFrameID() == 0:
                pc_backup_offset = 0
            if isi386Arch == True:
                if lastframeID == frame.GetFrameID():
                    pc_backup_offset = 0
            self.expect("disassemble -a %s" % (frame.GetPC() - pc_backup_offset),
                    substrs = ['<+0>: '])

    def check_expr_in_main(self, thread):
        depth = thread.GetNumFrames()
        for i in range(depth):
            frame = thread.GetFrameAtIndex(i)
            self.assertTrue(frame.IsValid(), "current frame is valid")
            if self.TraceOn():
                print("Checking if function %s is main" % frame.GetFunctionName())

            if 'main' == frame.GetFunctionName():
                frame_id = frame.GetFrameID()
                self.runCmd("frame select " + str(frame_id), RUN_SUCCEEDED)
                self.expect("p argc", substrs = ['(int)', ' = 1'])
                self.expect("p hello_world", substrs = ['Hello'])
                self.expect("p argv[0]", substrs = ['a.out'])
                self.expect("p null_ptr", substrs = ['= 0x0'])
                return True
        return False

    def inferior_asserting_expr(self):
        """Test that the lldb expression interpreter can read symbols after asserting."""
        exe = os.path.join(os.getcwd(), "a.out")

        # Create a target by the debugger.
        target = self.dbg.CreateTarget(exe)
        self.assertTrue(target, VALID_TARGET)

        # Launch the process, and do not stop at the entry point.
        target.LaunchSimple (None, None, self.get_process_working_directory())
        self.check_stop_reason()

        process = target.GetProcess()
        self.assertTrue(process.IsValid(), "current process is valid")

        thread = process.GetThreadAtIndex(0)
        self.assertTrue(thread.IsValid(), "current thread is valid")

        # The lldb expression interpreter should be able to read from addresses of the inferior after a call to assert().
        self.assertTrue(self.check_expr_in_main(thread), "cannot find 'main' in the backtrace")

    def inferior_asserting_step(self):
        """Test that lldb functions correctly after stepping through a call to assert()."""
        exe = os.path.join(os.getcwd(), "a.out")

        # Create a target by the debugger.
        target = self.dbg.CreateTarget(exe)
        self.assertTrue(target, VALID_TARGET)

        # Launch the process, and do not stop at the entry point.
        self.set_breakpoint(self.line)
        target.LaunchSimple (None, None, self.get_process_working_directory())

        self.expect("thread list", STOPPED_DUE_TO_BREAKPOINT,
            substrs = ['main.c:%d' % self.line,
                       'stop reason = breakpoint'])

        self.runCmd("next")
        stop_reason = self.check_stop_reason()

        # lldb should be able to read from registers from the inferior after asserting.
        if "x86_64" in self.getArchitecture():
            self.expect("register read rbp", substrs = ['rbp = 0x'])
        if "i386" in self.getArchitecture():
            self.expect("register read ebp", substrs = ['ebp = 0x'])

        process = target.GetProcess()
        self.assertTrue(process.IsValid(), "current process is valid")

        thread = process.GetThreadAtIndex(0)
        self.assertTrue(thread.IsValid(), "current thread is valid")

        # The lldb expression interpreter should be able to read from addresses of the inferior after a call to assert().
        self.assertTrue(self.check_expr_in_main(thread), "cannot find 'main' in the backtrace")

        # And it should report the correct line number.
        self.expect("thread backtrace all",
            substrs = [stop_reason,
                       'main.c:%d' % self.line])
<|endoftext|>


<commit_before># Copyright 2022 The pydemos Authors.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

"""Declares Streamlit Component to create an image carousel from a list of URLs.

When clicking an image from the carousel, its URL will be returned as the
component value.
"""

import os
from typing import List, Optional

import streamlit.components.v1 as components

# RELEASE constant, set to False during development to use node dev server,
# set to True on production to use component built version.
_RELEASE = True

if not _RELEASE:
  # `declare_component` returns a function that is used to create
  # instances of the component.
  _component_func = components.declare_component(
      "image_carousel",
      # URL of the local dev server
      url="http://localhost:5000")

else:
  # On production, points to to the component's build directory.
  parent_dir = os.path.dirname(os.path.abspath(__file__))
  build_dir = os.path.join(parent_dir, "frontend/public")
  _component_func = components.declare_component(
      "image_carousel", path=build_dir)

# We create a custom wrapper function that will serve as our component's
# public API, instead of exposing the `declare_component` function.
def image_carousel(image_list: List[str],
                   scroller_height: int = 200,
                   key: Optional[str] = None) -> str:
  """Creates a new instance of "image_carousel" component.

  Args:
    image_list: List of URL strings of images to display.
    scroller_height: Integer describing height in px of the image scroller.
      Defaults to 200px.
    key: An optional string that uniquely identifies this component. If this is
      None, and the component's arguments are changed, the component will be
      re-mounted in the Streamlit frontend and lose its current state.

  Returns:
    The URL link of the last clicked image in the scroller. Returned as a
    string.
  """

  component_value = _component_func(
      image_list=image_list,
      scroller_height=scroller_height,
      key=key,
      default="")  # Default, return empty string (empty URL)

  return component_value
<commit_msg>Adds `index` parameter to `image_carousel` function to choose default preselected image.<commit_after># Copyright 2022 The pydemos Authors.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

"""Declares Streamlit Component to create an image carousel from a list of URLs.

When clicking an image from the carousel, its URL will be returned as the
component value.
"""

import os
from typing import List, Optional

import streamlit.components.v1 as components

# RELEASE constant, set to False during development to use node dev server,
# set to True on production to use component built version.
_RELEASE = True

if not _RELEASE:
  # `declare_component` returns a function that is used to create
  # instances of the component.
  _component_func = components.declare_component(
      "image_carousel",
      # URL of the local dev server
      url="http://localhost:5000")

else:
  # On production, points to to the component's build directory.
  parent_dir = os.path.dirname(os.path.abspath(__file__))
  build_dir = os.path.join(parent_dir, "frontend/public")
  _component_func = components.declare_component(
      "image_carousel", path=build_dir)

# We create a custom wrapper function that will serve as our component's
# public API, instead of exposing the `declare_component` function.
def image_carousel(image_list: List[str],
                   scroller_height: int = 200,
                   index: int = 0,
                   key: Optional[str] = None) -> str:
  """Creates a new instance of "image_carousel" component.

  Args:
    image_list: List of URL strings of images to display.
    scroller_height: Integer describing height in px of the image scroller.
      Defaults to 200px.
    index: The index in `image_list` of the preselected option on first render.
    key: An optional string that uniquely identifies this component. If this is
      None, and the component's arguments are changed, the component will be
      re-mounted in the Streamlit frontend and lose its current state.

  Returns:
    The URL link of the last clicked image in the scroller. Returned as a
    string. Defaults to the image URL in position `index` of `image_list`.
  """

  component_value = _component_func(
      image_list=image_list,
      scroller_height=scroller_height,
      key=key,
      default=image_list[index]
  )  # Default return value, image URL in position `index` of `image_list`.

  return component_value
<|endoftext|>


<commit_before>#BSD 2-Clause (c) 2014: A.Doherty (Oxford), D.Jackson, N.Hammerla (Newcastle)
"""
This command line application calculates average daily activity from raw
accelerometer data as follows:
    1) Extract and filter sum vector magnitude values for <60>sec epochs
    2) Identify nonWear data in the epochs, and remove it
    3) Construct an avg movement value for each of 1440 minutes in an avg day
    4) Get overall average movement per second from step 3
=== === === ===
The application can be run as follows:
    python ActivitySummaryFromEpochs.py <input_file.CWA> <options>
e.g.
    python ActivitySummaryFromEpochs.py p001.CWA
    python ActivitySummaryFromEpochs.py p001.CWA min_freq:10
"""

import sys
import os
import datetime
import behaviourEpisode
import pandas as pd
from subprocess import call, Popen

def main():
    """
    Application entry point responsible for parsing command line requests
    """
    #check that enough command line arguments are entered
    if len(sys.argv)<2:
        msg = "\n Invalid input, please enter at least 1 parameter, e.g."
        msg += "\n python ActivitySummaryFromEpoch.py inputFile.CWA \n"
        print msg
        sys.exit(0)
    #store command line arguments to local variables
    rawFile = sys.argv[1]
    funcParams = sys.argv[2:]
    wavFile = rawFile.replace(".CWA", ".wav").replace(".cwa",".wav")
    epochFile = wavFile.replace(".wav","Epoch.csv")
    matlabPath = "matlab"
    skipMatlab = False
    skipJava = False
    deleteWav = False
    epochPeriodStr = "epochPeriod:60"
    #update default values by looping through user parameters
    for param in funcParams:
        #example param -> 'matlab:/Applications/MATLAB_R2014a.app/bin/matlab'
        if param.split(':')[0] == 'matlab':
            matlabPath = param.split(':')[1]
        elif param.split(':')[0] == 'skipMatlab':
            skipMatlab = param.split(':')[1] in ['true', 'True']
        elif param.split(':')[0] == 'deleteWav':
            deleteWav = param.split(':')[1] in ['true', 'True']
        elif param.split(':')[0] == 'skipJava':
            skipJava = param.split(':')[1] in ['true', 'True']
        elif param.split(':')[0] == 'epochPeriod':
            epochPeriodStr = param

    #check source cwa file exists
    if not os.path.isfile(rawFile):
        msg = "\n Invalid input"
        msg += "\n File does not exist: " + rawFile + "\n"
        print msg
        sys.exit(0)

    #interpolate and calibrate raw .CWA file, writing output to .wav file
    commandArgs = [matlabPath, "-nosplash",
            "-nodisplay", "-r", "cd matlab;readInterpolateCalibrate('" + rawFile
            + "', '" + wavFile + "');exit;"]
    if not skipMatlab:
        call(commandArgs)

    #calculate and write filtered AvgVm epochs from .wav file
    commandArgs = ["java", "-XX:ParallelGCThreads=1", "-mx64m",
            "AxivityAx3WavEpochs", wavFile, "outputFile:" + epochFile,
            "filter:true", epochPeriodStr]
    if not skipJava:
        call(commandArgs)
    if deleteWav:
        os.remove(wavFile)

    #identify and remove nonWear episodes
    firstDay, lastDay, wearTime, sumNonWear, numNonWearEpisodes = identifyAndRemoveNonWearTime(
            epochFile, funcParams)

    #calculate average sample score (diurnally adjusted)
    avgSampleVm = getAverageVmMinute(epochFile,0,0)

    #print processed summary variables from accelerometer file
    rawFileSize = os.path.getsize(rawFile)
    outputSummary = rawFile + ',' + str(rawFileSize) + ','
    outputSummary += str(avgSampleVm) + ','
    outputSummary += str(firstDay)[:-3] + ',' + str(lastDay)[:-3] + ','
    outputSummary += str(wearTime) + ',' + str(sumNonWear) + ','
    outputSummary += str(numNonWearEpisodes)
    f = open(rawFile.replace(".cwa","OutputSummary.csv"),'w')
    f.write(outputSummary)
    f.close()
    print outputSummary

def getAverageVmMinute(epochFile,headerSize,dateColumn):
    """
    Calculate diurnally adjusted average movement per minute from epoch file
    which has had nonWear episodes removed from it
    """
    #use python PANDAS framework to read in and store epochs
    e = pd.read_csv(epochFile, index_col=dateColumn, parse_dates=True,
                header=headerSize)
    #diurnal adjustment: construct average 1440 minute day
    avgDay = e[['AvgVm']].groupby([e.index.hour, e.index.minute]).mean()
    #return average minute score
    return avgDay.mean()[0]

def identifyAndRemoveNonWearTime(epochFile, funcParams):
    """
    Identify and remove nonWear episodes from an epoch CSV file
    Inputs:
    - epochFile: an epoch .csv file
    - funcParams: an array of [<name>:<value>] items, specifically:
        [nonWearEpisodesOutputFile:<name.csv>], default = <epochFile>_mvpa_bout_list.csv
        [headerSize:<lines>], default = 1
        [datetimeColumn:<int>], default = 0, index of datetime column
        [timeFormat:<python_timeFormat_string>], default = '%Y-%m-%d %H:%M:%S.%f'
        [xIndex:<int>], default = 8
        [yIndex:<int>], default = 9
        [zIndex:<int>], default = 10
        [targetWearTimeDays:<int>], default = 28
        [behavType:<string>], default = 'nonwear'
        [minFreq:<int>], default = 60, min num epochs in episode
        [maxRange:<float>], default = 0.013, movement below this indicates nonwear
        [graceMaxFreq:<int>], default = 0, max num "grace" epochs in episode outside <maxRange> thresholds
        [displayOutput:<bool>], default = False
    Output:
    - new file created (funcParams 'nonWearEpisodesOutputFile')
    """
    '''
    Firstly determine parameters to influence the calculation of epochs
    '''
    #variables to store default parameter options
    nonWearEpisodesOutputFile = epochFile.split('.')[0] + 'NonWearBouts.csv'
    headerSize = 1
    datetimeColumn, xIndex, yIndex, zIndex = 0, 8, 9, 10
    timeFormat = '%Y-%m-%d %H:%M:%S.%f'
    targetWearTimeDays, behavType = 28, 'nonwear'
    minFreq, maxRange, graceMaxFreq = 60, 0.013, 0
    displayOutput = False
    #update default values by looping through available user parameters
    for param in funcParams:
        #param will look like 'nonWearEpisodesOutputFile:aidenNonWearBouts.csv'
        if param.split(':')[0] == 'nonWearEpisodesOutputFile':
            nonWearEpisodesOutputFile = param.split(':')[1]
        elif param.split(':')[0] == 'headerSize':
            headerSize = int(param.split(':')[1])
        elif param.split(':')[0] == 'datetimeColumn':
            datetimeColumn = int(param.split(':')[1])
        elif param.split(':')[0] == 'timeFormat':
            timeFormat = param.replace('timeFormat:','')
        elif param.split(':')[0] == 'xIndex':
            xIndex = int(param.split(':')[1])
        elif param.split(':')[0] == 'yIndex':
            yIndex = int(param.split(':')[1])
        elif param.split(':')[0] == 'zIndex':
            zIndex = int(param.split(':')[1])
        elif param.split(':')[0] == 'targetWearTimeDays':
            targetWearTimeDays = int(param.split(':')[1])
        elif param.split(':')[0] == 'behavType':
            behavType = param.split(':')[1]
        elif param.split(':')[0] == 'minFreq':
            minFreq = int(param.split(':')[1])
        elif param.split(':')[0] == 'maxRange':
            maxRange = float(param.split(':')[1])
        elif param.split(':')[0] == 'graceMaxFreq':
            graceMaxFreq = int(param.split(':')[1])
        elif param.split(':')[0] == 'displayOutput':
            displayOutput = param.split(':')[1] in ['true', 'True']
    #now calculate nonwear episodes and store to list
    episodesList, firstDay, lastDay = behaviourEpisode.identifyNonWearEpisodes(
                    epochFile, headerSize, datetimeColumn, timeFormat, xIndex, yIndex,
                    zIndex, targetWearTimeDays, behavType, minFreq, maxRange,
                    graceMaxFreq)
    #print summary of each nonwear episode detected, returning sum nonwear time
    sumNonWear, numNonWearEpisodes = behaviourEpisode.writeSummaryOfEpisodes(
                    nonWearEpisodesOutputFile, episodesList, displayOutput)
    #calculate max possible wear time in minutes (pre Python 2.7 compatible)
    wearTime = ((lastDay-firstDay).days*3600*24) + ((lastDay - firstDay).seconds)
    wearTime = wearTime / 60 #convert from seconds to minutes
    wearTime -= sumNonWear #total wear = max possible wear - nonWear
    removeNonWearFromEpochFile(epochFile,episodesList,headerSize,timeFormat)
    return firstDay, lastDay, wearTime, sumNonWear, numNonWearEpisodes

def removeNonWearFromEpochFile(
            epochFile,
            nonWearEpisodes,
            headerSize,
            timeFormat):
    """
    Remove any nonWear episodes from the epochFile
    """
    #only run if there is nonWear data to remove
    if len(nonWearEpisodes) > 0:
        f = open(epochFile,'rU')
        epochs = f.readlines() #read file into memory
        f.close()
        f = open(epochFile,'w')

        #rewrite header lines
        for headerLine in epochs[:headerSize]:
            f.write(headerLine)

        #rewrite all epochs that are periods of wear
        episodeCounter = 0
        for epoch in epochs[headerSize:]:
            epochTime = datetime.datetime.strptime(epoch.split(',')[0],timeFormat)
            #write epoch if it is a period of wear i.e. it is not after the
            #   startTime of next nonWear episode, or it is after endTime of
            #   last nonWear episode
            if ( epochTime < nonWearEpisodes[episodeCounter].startTime or
                    (epochTime > nonWearEpisodes[episodeCounter].endTime and
                    episodeCounter == len(nonWearEpisodes)-1 ) ):
                f.write(epoch)
            #move counter to next nonWear episode if at end of current episode
            elif ( epochTime == nonWearEpisodes[episodeCounter].endTime and
                    episodeCounter < len(nonWearEpisodes)-1 ):
                episodeCounter += 1
        f.close()

"""
Standard boilerplate to call the main() function to begin the program.
"""
if __name__ == '__main__':
    main() #Standard boilerplate to call the main() function to begin the program.
<commit_msg>remove maximum memory heap size constraint, instead just rely on restraining the number of ParallelGCThreads. Seems to work better on sun grid engine clusters<commit_after>#BSD 2-Clause (c) 2014: A.Doherty (Oxford), D.Jackson, N.Hammerla (Newcastle)
"""
This command line application calculates average daily activity from raw
accelerometer data as follows:
    1) Extract and filter sum vector magnitude values for <60>sec epochs
    2) Identify nonWear data in the epochs, and remove it
    3) Construct an avg movement value for each of 1440 minutes in an avg day
    4) Get overall average movement per second from step 3
=== === === ===
The application can be run as follows:
    python ActivitySummaryFromEpochs.py <input_file.CWA> <options>
e.g.
    python ActivitySummaryFromEpochs.py p001.CWA
    python ActivitySummaryFromEpochs.py p001.CWA min_freq:10
"""

import sys
import os
import datetime
import behaviourEpisode
import pandas as pd
from subprocess import call, Popen

def main():
    """
    Application entry point responsible for parsing command line requests
    """
    #check that enough command line arguments are entered
    if len(sys.argv)<2:
        msg = "\n Invalid input, please enter at least 1 parameter, e.g."
        msg += "\n python ActivitySummaryFromEpoch.py inputFile.CWA \n"
        print msg
        sys.exit(0)
    #store command line arguments to local variables
    rawFile = sys.argv[1]
    funcParams = sys.argv[2:]
    wavFile = rawFile.replace(".CWA", ".wav").replace(".cwa",".wav")
    epochFile = wavFile.replace(".wav","Epoch.csv")
    matlabPath = "matlab"
    skipMatlab = False
    skipJava = False
    deleteWav = False
    epochPeriodStr = "epochPeriod:60"
    #update default values by looping through user parameters
    for param in funcParams:
        #example param -> 'matlab:/Applications/MATLAB_R2014a.app/bin/matlab'
        if param.split(':')[0] == 'matlab':
            matlabPath = param.split(':')[1]
        elif param.split(':')[0] == 'skipMatlab':
            skipMatlab = param.split(':')[1] in ['true', 'True']
        elif param.split(':')[0] == 'deleteWav':
            deleteWav = param.split(':')[1] in ['true', 'True']
        elif param.split(':')[0] == 'skipJava':
            skipJava = param.split(':')[1] in ['true', 'True']
        elif param.split(':')[0] == 'epochPeriod':
            epochPeriodStr = param

    #check source cwa file exists
    if not os.path.isfile(rawFile):
        msg = "\n Invalid input"
        msg += "\n File does not exist: " + rawFile + "\n"
        print msg
        sys.exit(0)

    #interpolate and calibrate raw .CWA file, writing output to .wav file
    commandArgs = [matlabPath, "-nosplash",
            "-nodisplay", "-r", "cd matlab;readInterpolateCalibrate('" + rawFile
            + "', '" + wavFile + "');exit;"]
    if not skipMatlab:
        call(commandArgs)

    #calculate and write filtered AvgVm epochs from .wav file
    commandArgs = ["java", "-XX:ParallelGCThreads=1", "AxivityAx3WavEpochs",
            wavFile, "outputFile:" + epochFile, "filter:true", epochPeriodStr]
    if not skipJava:
        call(commandArgs)
    if deleteWav:
        os.remove(wavFile)

    #identify and remove nonWear episodes
    firstDay, lastDay, wearTime, sumNonWear, numNonWearEpisodes = identifyAndRemoveNonWearTime(
            epochFile, funcParams)

    #calculate average sample score (diurnally adjusted)
    avgSampleVm = getAverageVmMinute(epochFile,0,0)

    #print processed summary variables from accelerometer file
    rawFileSize = os.path.getsize(rawFile)
    outputSummary = rawFile + ',' + str(rawFileSize) + ','
    outputSummary += str(avgSampleVm) + ','
    outputSummary += str(firstDay)[:-3] + ',' + str(lastDay)[:-3] + ','
    outputSummary += str(wearTime) + ',' + str(sumNonWear) + ','
    outputSummary += str(numNonWearEpisodes)
    f = open(rawFile.replace(".cwa","OutputSummary.csv"),'w')
    f.write(outputSummary)
    f.close()
    print outputSummary

def getAverageVmMinute(epochFile,headerSize,dateColumn):
    """
    Calculate diurnally adjusted average movement per minute from epoch file
    which has had nonWear episodes removed from it
    """
    #use python PANDAS framework to read in and store epochs
    e = pd.read_csv(epochFile, index_col=dateColumn, parse_dates=True,
                header=headerSize)
    #diurnal adjustment: construct average 1440 minute day
    avgDay = e[['AvgVm']].groupby([e.index.hour, e.index.minute]).mean()
    #return average minute score
    return avgDay.mean()[0]

def identifyAndRemoveNonWearTime(epochFile, funcParams):
    """
    Identify and remove nonWear episodes from an epoch CSV file
    Inputs:
    - epochFile: an epoch .csv file
    - funcParams: an array of [<name>:<value>] items, specifically:
        [nonWearEpisodesOutputFile:<name.csv>], default = <epochFile>_mvpa_bout_list.csv
        [headerSize:<lines>], default = 1
        [datetimeColumn:<int>], default = 0, index of datetime column
        [timeFormat:<python_timeFormat_string>], default = '%Y-%m-%d %H:%M:%S.%f'
        [xIndex:<int>], default = 8
        [yIndex:<int>], default = 9
        [zIndex:<int>], default = 10
        [targetWearTimeDays:<int>], default = 28
        [behavType:<string>], default = 'nonwear'
        [minFreq:<int>], default = 60, min num epochs in episode
        [maxRange:<float>], default = 0.013, movement below this indicates nonwear
        [graceMaxFreq:<int>], default = 0, max num "grace" epochs in episode outside <maxRange> thresholds
        [displayOutput:<bool>], default = False
    Output:
    - new file created (funcParams 'nonWearEpisodesOutputFile')
    """
    '''
    Firstly determine parameters to influence the calculation of epochs
    '''
    #variables to store default parameter options
    nonWearEpisodesOutputFile = epochFile.split('.')[0] + 'NonWearBouts.csv'
    headerSize = 1
    datetimeColumn, xIndex, yIndex, zIndex = 0, 8, 9, 10
    timeFormat = '%Y-%m-%d %H:%M:%S.%f'
    targetWearTimeDays, behavType = 28, 'nonwear'
    minFreq, maxRange, graceMaxFreq = 60, 0.013, 0
    displayOutput = False
    #update default values by looping through available user parameters
    for param in funcParams:
        #param will look like 'nonWearEpisodesOutputFile:aidenNonWearBouts.csv'
        if param.split(':')[0] == 'nonWearEpisodesOutputFile':
            nonWearEpisodesOutputFile = param.split(':')[1]
        elif param.split(':')[0] == 'headerSize':
            headerSize = int(param.split(':')[1])
        elif param.split(':')[0] == 'datetimeColumn':
            datetimeColumn = int(param.split(':')[1])
        elif param.split(':')[0] == 'timeFormat':
            timeFormat = param.replace('timeFormat:','')
        elif param.split(':')[0] == 'xIndex':
            xIndex = int(param.split(':')[1])
        elif param.split(':')[0] == 'yIndex':
            yIndex = int(param.split(':')[1])
        elif param.split(':')[0] == 'zIndex':
            zIndex = int(param.split(':')[1])
        elif param.split(':')[0] == 'targetWearTimeDays':
            targetWearTimeDays = int(param.split(':')[1])
        elif param.split(':')[0] == 'behavType':
            behavType = param.split(':')[1]
        elif param.split(':')[0] == 'minFreq':
            minFreq = int(param.split(':')[1])
        elif param.split(':')[0] == 'maxRange':
            maxRange = float(param.split(':')[1])
        elif param.split(':')[0] == 'graceMaxFreq':
            graceMaxFreq = int(param.split(':')[1])
        elif param.split(':')[0] == 'displayOutput':
            displayOutput = param.split(':')[1] in ['true', 'True']
    #now calculate nonwear episodes and store to list
    episodesList, firstDay, lastDay = behaviourEpisode.identifyNonWearEpisodes(
                    epochFile, headerSize, datetimeColumn, timeFormat, xIndex, yIndex,
                    zIndex, targetWearTimeDays, behavType, minFreq, maxRange,
                    graceMaxFreq)
    #print summary of each nonwear episode detected, returning sum nonwear time
    sumNonWear, numNonWearEpisodes = behaviourEpisode.writeSummaryOfEpisodes(
                    nonWearEpisodesOutputFile, episodesList, displayOutput)
    #calculate max possible wear time in minutes (pre Python 2.7 compatible)
    wearTime = ((lastDay-firstDay).days*3600*24) + ((lastDay - firstDay).seconds)
    wearTime = wearTime / 60 #convert from seconds to minutes
    wearTime -= sumNonWear #total wear = max possible wear - nonWear
    removeNonWearFromEpochFile(epochFile,episodesList,headerSize,timeFormat)
    return firstDay, lastDay, wearTime, sumNonWear, numNonWearEpisodes

def removeNonWearFromEpochFile(
            epochFile,
            nonWearEpisodes,
            headerSize,
            timeFormat):
    """
    Remove any nonWear episodes from the epochFile
    """
    #only run if there is nonWear data to remove
    if len(nonWearEpisodes) > 0:
        f = open(epochFile,'rU')
        epochs = f.readlines() #read file into memory
        f.close()
        f = open(epochFile,'w')

        #rewrite header lines
        for headerLine in epochs[:headerSize]:
            f.write(headerLine)

        #rewrite all epochs that are periods of wear
        episodeCounter = 0
        for epoch in epochs[headerSize:]:
            epochTime = datetime.datetime.strptime(epoch.split(',')[0],timeFormat)
            #write epoch if it is a period of wear i.e. it is not after the
            #   startTime of next nonWear episode, or it is after endTime of
            #   last nonWear episode
            if ( epochTime < nonWearEpisodes[episodeCounter].startTime or
                    (epochTime > nonWearEpisodes[episodeCounter].endTime and
                    episodeCounter == len(nonWearEpisodes)-1 ) ):
                f.write(epoch)
            #move counter to next nonWear episode if at end of current episode
            elif ( epochTime == nonWearEpisodes[episodeCounter].endTime and
                    episodeCounter < len(nonWearEpisodes)-1 ):
                episodeCounter += 1
        f.close()

"""
Standard boilerplate to call the main() function to begin the program.
"""
if __name__ == '__main__':
    main() #Standard boilerplate to call the main() function to begin the program.
<|endoftext|>


<commit_before>import sublime, sublime_plugin, os, re, codecs, threading, time

class AngularJSSublimePackage(sublime_plugin.EventListener):
	"""
	Provides AngularJS attribute and custom component completions
	"""

	def on_query_completions(self, view, prefix, locations):
		if not hasattr(self, 'settings'):
			self.settings = sublime.load_settings('AngularJS-sublime-package.sublime-settings')

			self.settings.add_on_change('angular_components', self.process_angular_components)
			self.process_angular_components()

			self.settings.add_on_change('core_attribute_list', self.process_attributes)
			self.settings.add_on_change('extended_attribute_list', self.process_attributes)
			self.settings.add_on_change('AngularUI_attribute_list', self.process_attributes)
			self.settings.add_on_change('enable_data_prefix', self.process_attributes)
			self.settings.add_on_change('enable_AngularUI_directives', self.process_attributes)
			self.process_attributes()

		if self.settings.get('disable_plugin'):
			return []

		single_match = False
		all_matched = True

		for scope in self.settings.get('attribute_avoided_scopes'):
			if view.match_selector(locations[0], scope):
				return []

		for scope in list(self.settings.get('attribute_defined_scopes')):
			if view.match_selector(locations[0], scope):
				single_match = True
			else:
				all_matched = False

		if not self.settings.get('ensure_all_scopes_are_matched') and single_match:
			return self.completions(view, locations, True)
		elif self.settings.get('ensure_all_scopes_are_matched') and all_matched:
			return self.completions(view, locations, True)
		else:
			return self.completions(view, locations, False)

	def completions(self, view, locations, is_inside_tag):

		if is_inside_tag :
			return (self.attributes, sublime.INHIBIT_WORD_COMPLETIONS | sublime.INHIBIT_EXPLICIT_COMPLETIONS)

		if not is_inside_tag:
			in_scope = False

			for scope in self.settings.get('component_defined_scopes'):
				if view.match_selector(locations[0], scope):
					in_scope = True

			if in_scope:
				return self.custom_components
			else:
				return []

	def process_attributes(self):
		self.attributes = []
		add_data_prefix = self.settings.get('enable_data_prefix')

		for attr in self.settings.get('core_attribute_list'):
			if add_data_prefix:
				attr[1] = "data-" + attr[1]

			self.attributes.append(attr)

		for attr in self.settings.get('extended_attribute_list'):
			if add_data_prefix:
				attr[1] = "data-" + attr[1]

			self.attributes.append(attr)

		if self.settings.get('enable_AngularUI_directives'):
			for attr in self.settings.get('AngularUI_attribute_list'):
				if add_data_prefix:
					attr[1] = "data-" + attr[1]

				self.attributes.append(attr)

		self.attributes = [tuple(attr) for attr in self.attributes]

	def process_angular_components(self):
		self.custom_components = []
		for component in self.settings.get('angular_components'):
			self.custom_components.append((component + "\tAngularJS Component", component + "$1>$2</" + component + '>'))

class AngularjsFileIndexCommand(sublime_plugin.WindowCommand):
	is_indexing = False
	windows = {}

	def run(self):
		is_indexing = True
		self.settings = sublime.load_settings('AngularJS-sublime-package.sublime-settings')

		thread = AngularjsWalkThread(
			sublime.active_window().folders(),
			self.settings.get('exclude_dirs'),
			self.settings.get('match_definitions'),
			self.settings.get('match_expression'),
			self.settings.get('match_expression_group')
		)

		thread.start()
		self.track_walk_thread(thread)

	def track_walk_thread(self, thread):
		sublime.status_message("AngularJS: indexing definitions")

		if thread.is_alive():
			sublime.set_timeout(lambda: self.track_walk_thread(thread), 1000)
		else:
			AngularjsFileIndexCommand.windows[sublime.active_window().id()] = thread.result
			sublime.status_message('AngularJS: indexing completed in ' + str(thread.time_taken))
			sublime.set_timeout(lambda: sublime.status_message(''), 1500)
			AngularjsFileIndexCommand.is_indexing = False

class AngularjsFindCommand(sublime_plugin.WindowCommand):
	def run(self):
		self.settings = sublime.load_settings('AngularJS-sublime-package.sublime-settings')
		self.current_window_id = sublime.active_window().id()

		if AngularjsFileIndexCommand.is_indexing:
			return

		if not sublime.active_window().id() in AngularjsFileIndexCommand.windows:
			sublime.active_window().run_command('angularjs_file_index')
			return

		self.definition_List = AngularjsFileIndexCommand.windows[self.current_window_id]
		self.current_window = sublime.active_window()
		self.current_file = self.current_window.active_view().file_name()

		if int(sublime.version()) >= 3000 and self.settings.get('show_file_preview'):
			self.current_window.show_quick_panel(self.definition_List, self.on_done, False, -1, self.on_highlight)
		else:
			self.current_window.show_quick_panel(self.definition_List, self.on_done)

	def on_highlight(self, index):
		self.current_window.open_file(self.definition_List[index][1], sublime.TRANSIENT)
		self.current_window.active_view().run_command("goto_line", {"line": int(self.definition_List[index][2])} )

	def on_done(self, index):
		if index > -1:
			self.current_window.open_file(self.definition_List[index][1])
			self.handle_file_open_go_to(int(self.definition_List[index][2]))
		else:
			self.current_window.open_file(self.current_file)

	def handle_file_open_go_to(self, line):
		if not self.current_window.active_view().is_loading():
			self.current_window.active_view().run_command("goto_line", {"line": line} )
		else:
			sublime.set_timeout(lambda: self.handle_file_open_go_to(line), 100)

class AngularjsLookUpDefinitionCommand(sublime_plugin.WindowCommand):
	def run(self):
		for region in sublime.active_window().active_view().sel():

			# temporarily change word_separators so that - and . are excluded from list
			sublime_preferences = sublime.load_settings("Preferences.sublime-settings")
			original_word_separators = sublime_preferences.get('word_separators')
			sublime_preferences.set('word_separators', "/\\()\"':,;<>~!@#$%^&*|+=[]{}`~?")

			word_point = sublime.active_window().active_view().word(region)
			definition = sublime.active_window().active_view().substr(word_point)

			# set word_separators back to their original value
			sublime_preferences.set('word_separators', original_word_separators)

			definition = re.sub(
				'(\w*)-(\w*)',
				lambda match: match.group(1) + match.group(2).capitalize(),
				definition
			)

		for item in AngularjsFileIndexCommand.windows[sublime.active_window().id()]:
			if(re.search(definition, item[0])):
				print(item)
				sublime.active_window().open_file(item[1])
				self.handle_file_open_go_to(int(item[2]))
				break

	def handle_file_open_go_to(self, line):
		active_view = sublime.active_window().active_view()
		if not active_view.is_loading():
			active_view.run_command("goto_line", {"line": line} )
		else:
			sublime.set_timeout(lambda: self.handle_file_open_go_to(line), 100)

class AngularjsWalkThread(threading.Thread):
	def __init__(self, folders, exclude_dirs, match_definitions, match_expression, match_expression_group):
		self.folders = folders
		self.exclude_dirs = exclude_dirs
		self.match_definitions = match_definitions
		self.match_expression = match_expression
		self.match_expression_group = match_expression_group
		self.match_expressions = []
		threading.Thread.__init__(self)

	def run(self):
		self.function_matches = []
		self.function_match_details = []
		start = time.time()
		for definition in self.match_definitions:
			self.match_expressions.append(
				(definition, re.compile(self.match_expression.format(definition)))
			)

		project_folders = self.folders
		skip_dirs = self.exclude_dirs

		for path in project_folders:
			for r,d,f in os.walk(path):
				if not [skip for skip in skip_dirs if path + '/' + skip in r]:
					for files in f:
						if files.endswith(".js"):
							_file = codecs.open(r+'/'+files)
							_lines = _file.readlines();
							_file.close()
							line_number = 1
							for line in _lines:
								matches = self.get_definition_details(line)
								if len(matches):
									for matched in matches:
										definition_name = matched[0] + ":  "
										definition_name += matched[1].group(int(self.match_expression_group))
										self.function_matches.append([definition_name, r+'/'+files, str(line_number)])
								line_number += 1
		self.time_taken = time.time() - start
		self.result = self.function_matches

	def get_definition_details(self, line_content):
		matches = []
		for expression in self.match_expressions:
			matched = expression[1].search(repr(line_content))
			if matched:
				#print('matched it', expression)
				matches.append((expression[0], matched))
		return matches
<commit_msg>Look Up Definition now checks to see if there is a text selection - If text is selected use that for the look up - If no text is selected use use sublimes select word at point to get the selection<commit_after>import sublime, sublime_plugin, os, re, codecs, threading, time

class AngularJSSublimePackage(sublime_plugin.EventListener):
	"""
	Provides AngularJS attribute and custom component completions
	"""

	def on_query_completions(self, view, prefix, locations):
		if not hasattr(self, 'settings'):
			self.settings = sublime.load_settings('AngularJS-sublime-package.sublime-settings')

			self.settings.add_on_change('angular_components', self.process_angular_components)
			self.process_angular_components()

			self.settings.add_on_change('core_attribute_list', self.process_attributes)
			self.settings.add_on_change('extended_attribute_list', self.process_attributes)
			self.settings.add_on_change('AngularUI_attribute_list', self.process_attributes)
			self.settings.add_on_change('enable_data_prefix', self.process_attributes)
			self.settings.add_on_change('enable_AngularUI_directives', self.process_attributes)
			self.process_attributes()

		if self.settings.get('disable_plugin'):
			return []

		single_match = False
		all_matched = True

		for scope in self.settings.get('attribute_avoided_scopes'):
			if view.match_selector(locations[0], scope):
				return []

		for scope in list(self.settings.get('attribute_defined_scopes')):
			if view.match_selector(locations[0], scope):
				single_match = True
			else:
				all_matched = False

		if not self.settings.get('ensure_all_scopes_are_matched') and single_match:
			return self.completions(view, locations, True)
		elif self.settings.get('ensure_all_scopes_are_matched') and all_matched:
			return self.completions(view, locations, True)
		else:
			return self.completions(view, locations, False)

	def completions(self, view, locations, is_inside_tag):

		if is_inside_tag :
			return (self.attributes, sublime.INHIBIT_WORD_COMPLETIONS | sublime.INHIBIT_EXPLICIT_COMPLETIONS)

		if not is_inside_tag:
			in_scope = False

			for scope in self.settings.get('component_defined_scopes'):
				if view.match_selector(locations[0], scope):
					in_scope = True

			if in_scope:
				return self.custom_components
			else:
				return []

	def process_attributes(self):
		self.attributes = []
		add_data_prefix = self.settings.get('enable_data_prefix')

		for attr in self.settings.get('core_attribute_list'):
			if add_data_prefix:
				attr[1] = "data-" + attr[1]

			self.attributes.append(attr)

		for attr in self.settings.get('extended_attribute_list'):
			if add_data_prefix:
				attr[1] = "data-" + attr[1]

			self.attributes.append(attr)

		if self.settings.get('enable_AngularUI_directives'):
			for attr in self.settings.get('AngularUI_attribute_list'):
				if add_data_prefix:
					attr[1] = "data-" + attr[1]

				self.attributes.append(attr)

		self.attributes = [tuple(attr) for attr in self.attributes]

	def process_angular_components(self):
		self.custom_components = []
		for component in self.settings.get('angular_components'):
			self.custom_components.append((component + "\tAngularJS Component", component + "$1>$2</" + component + '>'))

class AngularjsFileIndexCommand(sublime_plugin.WindowCommand):
	is_indexing = False
	windows = {}

	def run(self):
		is_indexing = True
		self.settings = sublime.load_settings('AngularJS-sublime-package.sublime-settings')

		thread = AngularjsWalkThread(
			sublime.active_window().folders(),
			self.settings.get('exclude_dirs'),
			self.settings.get('match_definitions'),
			self.settings.get('match_expression'),
			self.settings.get('match_expression_group')
		)

		thread.start()
		self.track_walk_thread(thread)

	def track_walk_thread(self, thread):
		sublime.status_message("AngularJS: indexing definitions")

		if thread.is_alive():
			sublime.set_timeout(lambda: self.track_walk_thread(thread), 1000)
		else:
			AngularjsFileIndexCommand.windows[sublime.active_window().id()] = thread.result
			sublime.status_message('AngularJS: indexing completed in ' + str(thread.time_taken))
			sublime.set_timeout(lambda: sublime.status_message(''), 1500)
			AngularjsFileIndexCommand.is_indexing = False

class AngularjsFindCommand(sublime_plugin.WindowCommand):
	def run(self):
		self.settings = sublime.load_settings('AngularJS-sublime-package.sublime-settings')
		self.current_window_id = sublime.active_window().id()

		if AngularjsFileIndexCommand.is_indexing:
			return

		if not sublime.active_window().id() in AngularjsFileIndexCommand.windows:
			sublime.active_window().run_command('angularjs_file_index')
			return

		self.definition_List = AngularjsFileIndexCommand.windows[self.current_window_id]
		self.current_window = sublime.active_window()
		self.current_file = self.current_window.active_view().file_name()

		if int(sublime.version()) >= 3000 and self.settings.get('show_file_preview'):
			self.current_window.show_quick_panel(self.definition_List, self.on_done, False, -1, self.on_highlight)
		else:
			self.current_window.show_quick_panel(self.definition_List, self.on_done)

	def on_highlight(self, index):
		self.current_window.open_file(self.definition_List[index][1], sublime.TRANSIENT)
		self.current_window.active_view().run_command("goto_line", {"line": int(self.definition_List[index][2])} )

	def on_done(self, index):
		if index > -1:
			self.current_window.open_file(self.definition_List[index][1])
			self.handle_file_open_go_to(int(self.definition_List[index][2]))
		else:
			self.current_window.open_file(self.current_file)

	def handle_file_open_go_to(self, line):
		if not self.current_window.active_view().is_loading():
			self.current_window.active_view().run_command("goto_line", {"line": line} )
		else:
			sublime.set_timeout(lambda: self.handle_file_open_go_to(line), 100)

class AngularjsLookUpDefinitionCommand(sublime_plugin.WindowCommand):
	def run(self):
		active_view = sublime.active_window().active_view()
		for region in sublime.active_window().active_view().sel():

			if not region.size():
				# nothing was selected so find word at point
				word_point = active_view.word(region)
				definition = active_view.substr(word_point)
			else:
				definition = active_view.substr(region)

			# ensure data- is striped out before trying to
			# normalize and look up
			definition = definition.replace('data-', '')

			# convert selections such as app-version to appVersion
			# for proper look up
			definition = re.sub(
				'(\w*)-(\w*)',
				lambda match: match.group(1) + match.group(2).capitalize(),
				definition
			)

		for item in AngularjsFileIndexCommand.windows[sublime.active_window().id()]:
			if(re.search(definition, item[0])):
				print(item)
				sublime.active_window().open_file(item[1])
				self.handle_file_open_go_to(int(item[2]))
				break

	def handle_file_open_go_to(self, line):
		active_view = sublime.active_window().active_view()
		if not active_view.is_loading():
			active_view.run_command("goto_line", {"line": line} )
		else:
			sublime.set_timeout(lambda: self.handle_file_open_go_to(line), 100)

class AngularjsWalkThread(threading.Thread):
	def __init__(self, folders, exclude_dirs, match_definitions, match_expression, match_expression_group):
		self.folders = folders
		self.exclude_dirs = exclude_dirs
		self.match_definitions = match_definitions
		self.match_expression = match_expression
		self.match_expression_group = match_expression_group
		self.match_expressions = []
		threading.Thread.__init__(self)

	def run(self):
		self.function_matches = []
		self.function_match_details = []
		start = time.time()
		for definition in self.match_definitions:
			self.match_expressions.append(
				(definition, re.compile(self.match_expression.format(definition)))
			)

		project_folders = self.folders
		skip_dirs = self.exclude_dirs

		for path in project_folders:
			for r,d,f in os.walk(path):
				if not [skip for skip in skip_dirs if path + '/' + skip in r]:
					for files in f:
						if files.endswith(".js"):
							_file = codecs.open(r+'/'+files)
							_lines = _file.readlines();
							_file.close()
							line_number = 1
							for line in _lines:
								matches = self.get_definition_details(line)
								if len(matches):
									for matched in matches:
										definition_name = matched[0] + ":  "
										definition_name += matched[1].group(int(self.match_expression_group))
										self.function_matches.append([definition_name, r+'/'+files, str(line_number)])
								line_number += 1
		self.time_taken = time.time() - start
		self.result = self.function_matches

	def get_definition_details(self, line_content):
		matches = []
		for expression in self.match_expressions:
			matched = expression[1].search(repr(line_content))
			if matched:
				#print('matched it', expression)
				matches.append((expression[0], matched))
		return matches
<|endoftext|>


<commit_before># Copyright (c) 2010-2013 ARM Limited
# All rights reserved.
#
# The license below extends only to copyright in the software and shall
# not be construed as granting a license to any other intellectual
# property including but not limited to intellectual property relating
# to a hardware implementation of the functionality of the software
# licensed hereunder.  You may use the software subject to the license
# terms below provided that you ensure that this notice is replicated
# unmodified and in its entirety in all distributions of the software,
# modified or unmodified, in source code or in binary form.
#
# Copyright (c) 2006-2007 The Regents of The University of Michigan
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are
# met: redistributions of source code must retain the above copyright
# notice, this list of conditions and the following disclaimer;
# redistributions in binary form must reproduce the above copyright
# notice, this list of conditions and the following disclaimer in the
# documentation and/or other materials provided with the distribution;
# neither the name of the copyright holders nor the names of its
# contributors may be used to endorse or promote products derived from
# this software without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#
# Authors: Ali Saidi

import optparse
import sys

import m5
from m5.defines import buildEnv
from m5.objects import *
from m5.util import addToPath, fatal

addToPath('../common')

from FSConfig import *
from SysPaths import *
from Benchmarks import *
import Simulation
import CacheConfig
import MemConfig
from Caches import *
import Options

parser = optparse.OptionParser()
Options.addCommonOptions(parser)
Options.addFSOptions(parser)

(options, args) = parser.parse_args()

if args:
    print "Error: script doesn't take any positional arguments"
    sys.exit(1)

# driver system CPU is always simple... note this is an assignment of
# a class, not an instance.
DriveCPUClass = AtomicSimpleCPU
drive_mem_mode = 'atomic'

# Check if KVM support has been enabled, we might need to do VM
# configuration if that's the case.
have_kvm_support = 'BaseKvmCPU' in globals()
def is_kvm_cpu(cpu_class):
    return have_kvm_support and cpu_class != None and \
        issubclass(cpu_class, BaseKvmCPU)

# system under test can be any CPU
(TestCPUClass, test_mem_mode, FutureClass) = Simulation.setCPUClass(options)

# Match the memories with the CPUs, the driver system always simple,
# and based on the options for the test system
DriveMemClass = SimpleMemory
TestMemClass = Simulation.setMemClass(options)

if options.benchmark:
    try:
        bm = Benchmarks[options.benchmark]
    except KeyError:
        print "Error benchmark %s has not been defined." % options.benchmark
        print "Valid benchmarks are: %s" % DefinedBenchmarks
        sys.exit(1)
else:
    if options.dual:
        bm = [SysConfig(disk=options.disk_image, mem=options.mem_size),
              SysConfig(disk=options.disk_image, mem=options.mem_size)]
    else:
        bm = [SysConfig(disk=options.disk_image, mem=options.mem_size)]

np = options.num_cpus

if buildEnv['TARGET_ISA'] == "alpha":
    test_sys = makeLinuxAlphaSystem(test_mem_mode, bm[0])
elif buildEnv['TARGET_ISA'] == "mips":
    test_sys = makeLinuxMipsSystem(test_mem_mode, bm[0])
elif buildEnv['TARGET_ISA'] == "sparc":
    test_sys = makeSparcSystem(test_mem_mode, bm[0])
elif buildEnv['TARGET_ISA'] == "x86":
    test_sys = makeLinuxX86System(test_mem_mode, options.num_cpus, bm[0])
elif buildEnv['TARGET_ISA'] == "arm":
    test_sys = makeArmSystem(test_mem_mode, options.machine_type, bm[0],
                             options.dtb_filename,
                             bare_metal=options.bare_metal)
    if options.enable_context_switch_stats_dump:
        test_sys.enable_context_switch_stats_dump = True
else:
    fatal("Incapable of building %s full system!", buildEnv['TARGET_ISA'])

# Create a top-level voltage domain
test_sys.voltage_domain = VoltageDomain(voltage = options.sys_voltage)

# Create a source clock for the system and set the clock period
test_sys.clk_domain = SrcClockDomain(clock =  options.sys_clock,
                                     voltage_domain = test_sys.voltage_domain)

# Create a CPU voltage domain
test_sys.cpu_voltage_domain = VoltageDomain()

# Create a source clock for the CPUs and set the clock period
test_sys.cpu_clk_domain = SrcClockDomain(clock = options.cpu_clock,
                                         voltage_domain =
                                         test_sys.cpu_voltage_domain)

if options.kernel is not None:
    test_sys.kernel = binary(options.kernel)

if options.script is not None:
    test_sys.readfile = options.script

if options.lpae:
    test_sys.have_lpae = True

if options.virtualisation:
    test_sys.have_virtualization = True

test_sys.init_param = options.init_param

# For now, assign all the CPUs to the same clock domain
test_sys.cpu = [TestCPUClass(clk_domain=test_sys.cpu_clk_domain, cpu_id=i)
                for i in xrange(np)]

if is_kvm_cpu(TestCPUClass) or is_kvm_cpu(FutureClass):
    test_sys.vm = KvmVM()

if options.caches or options.l2cache:
    # By default the IOCache runs at the system clock
    test_sys.iocache = IOCache(addr_ranges = test_sys.mem_ranges)
    test_sys.iocache.cpu_side = test_sys.iobus.master
    test_sys.iocache.mem_side = test_sys.membus.slave
else:
    test_sys.iobridge = Bridge(delay='50ns', ranges = test_sys.mem_ranges)
    test_sys.iobridge.slave = test_sys.iobus.master
    test_sys.iobridge.master = test_sys.membus.slave

# Sanity check
if options.fastmem:
    if TestCPUClass != AtomicSimpleCPU:
        fatal("Fastmem can only be used with atomic CPU!")
    if (options.caches or options.l2cache):
        fatal("You cannot use fastmem in combination with caches!")

for i in xrange(np):
    if options.fastmem:
        test_sys.cpu[i].fastmem = True
    if options.checker:
        test_sys.cpu[i].addCheckerCpu()
    test_sys.cpu[i].createThreads()

CacheConfig.config_cache(options, test_sys)
MemConfig.config_mem(options, test_sys)

if len(bm) == 2:
    if buildEnv['TARGET_ISA'] == 'alpha':
        drive_sys = makeLinuxAlphaSystem(drive_mem_mode, bm[1])
    elif buildEnv['TARGET_ISA'] == 'mips':
        drive_sys = makeLinuxMipsSystem(drive_mem_mode, bm[1])
    elif buildEnv['TARGET_ISA'] == 'sparc':
        drive_sys = makeSparcSystem(drive_mem_mode, bm[1])
    elif buildEnv['TARGET_ISA'] == 'x86':
        drive_sys = makeX86System(drive_mem_mode, np, bm[1])
    elif buildEnv['TARGET_ISA'] == 'arm':
        drive_sys = makeArmSystem(drive_mem_mode, options.machine_type, bm[1])

    # Create a top-level voltage domain
    drive_sys.voltage_domain = VoltageDomain(voltage = options.sys_voltage)

    # Create a source clock for the system and set the clock period
    drive_sys.clk_domain = SrcClockDomain(clock =  options.sys_clock)

    # Create a CPU voltage domain
    drive_sys.cpu_voltage_domain = VoltageDomain()

    # Create a source clock for the CPUs and set the clock period
    drive_sys.cpu_clk_domain = SrcClockDomain(clock = options.cpu_clock,
                                              voltage_domain =
                                              drive_sys.cpu_voltage_domain)

    drive_sys.cpu = DriveCPUClass(clk_domain=drive_sys.cpu_clk_domain,
                                  cpu_id=0)
    drive_sys.cpu.createThreads()
    drive_sys.cpu.createInterruptController()
    drive_sys.cpu.connectAllPorts(drive_sys.membus)
    if options.fastmem:
        drive_sys.cpu.fastmem = True
    if options.kernel is not None:
        drive_sys.kernel = binary(options.kernel)

    if is_kvm_cpu(DriveCPUClass):
        drive_sys.vm = KvmVM()

    drive_sys.iobridge = Bridge(delay='50ns',
                                ranges = drive_sys.mem_ranges)
    drive_sys.iobridge.slave = drive_sys.iobus.master
    drive_sys.iobridge.master = drive_sys.membus.slave

    # Create the appropriate memory controllers and connect them to the
    # memory bus
    drive_sys.mem_ctrls = [DriveMemClass(range = r)
                           for r in drive_sys.mem_ranges]
    for i in xrange(len(drive_sys.mem_ctrls)):
        drive_sys.mem_ctrls[i].port = drive_sys.membus.master

    drive_sys.init_param = options.init_param
    root = makeDualRoot(True, test_sys, drive_sys, options.etherdump)
elif len(bm) == 1:
    root = Root(full_system=True, system=test_sys)
else:
    print "Error I don't know how to create more than 2 systems."
    sys.exit(1)

if options.timesync:
    root.time_sync_enable = True

if options.frame_capture:
    VncServer.frame_capture = True

Simulation.setWorkCountOptions(test_sys, options)
Simulation.run(options, root, test_sys, FutureClass)
<commit_msg>config: correct bug in x86 drive sys instantiation (transplanted from 33db5d81c2cb4ea83b05b12c5a9870ab36abb9b6)<commit_after># Copyright (c) 2010-2013 ARM Limited
# All rights reserved.
#
# The license below extends only to copyright in the software and shall
# not be construed as granting a license to any other intellectual
# property including but not limited to intellectual property relating
# to a hardware implementation of the functionality of the software
# licensed hereunder.  You may use the software subject to the license
# terms below provided that you ensure that this notice is replicated
# unmodified and in its entirety in all distributions of the software,
# modified or unmodified, in source code or in binary form.
#
# Copyright (c) 2006-2007 The Regents of The University of Michigan
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are
# met: redistributions of source code must retain the above copyright
# notice, this list of conditions and the following disclaimer;
# redistributions in binary form must reproduce the above copyright
# notice, this list of conditions and the following disclaimer in the
# documentation and/or other materials provided with the distribution;
# neither the name of the copyright holders nor the names of its
# contributors may be used to endorse or promote products derived from
# this software without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#
# Authors: Ali Saidi

import optparse
import sys

import m5
from m5.defines import buildEnv
from m5.objects import *
from m5.util import addToPath, fatal

addToPath('../common')

from FSConfig import *
from SysPaths import *
from Benchmarks import *
import Simulation
import CacheConfig
import MemConfig
from Caches import *
import Options

parser = optparse.OptionParser()
Options.addCommonOptions(parser)
Options.addFSOptions(parser)

(options, args) = parser.parse_args()

if args:
    print "Error: script doesn't take any positional arguments"
    sys.exit(1)

# driver system CPU is always simple... note this is an assignment of
# a class, not an instance.
DriveCPUClass = AtomicSimpleCPU
drive_mem_mode = 'atomic'

# Check if KVM support has been enabled, we might need to do VM
# configuration if that's the case.
have_kvm_support = 'BaseKvmCPU' in globals()
def is_kvm_cpu(cpu_class):
    return have_kvm_support and cpu_class != None and \
        issubclass(cpu_class, BaseKvmCPU)

# system under test can be any CPU
(TestCPUClass, test_mem_mode, FutureClass) = Simulation.setCPUClass(options)

# Match the memories with the CPUs, the driver system always simple,
# and based on the options for the test system
DriveMemClass = SimpleMemory
TestMemClass = Simulation.setMemClass(options)

if options.benchmark:
    try:
        bm = Benchmarks[options.benchmark]
    except KeyError:
        print "Error benchmark %s has not been defined." % options.benchmark
        print "Valid benchmarks are: %s" % DefinedBenchmarks
        sys.exit(1)
else:
    if options.dual:
        bm = [SysConfig(disk=options.disk_image, mem=options.mem_size),
              SysConfig(disk=options.disk_image, mem=options.mem_size)]
    else:
        bm = [SysConfig(disk=options.disk_image, mem=options.mem_size)]

np = options.num_cpus

if buildEnv['TARGET_ISA'] == "alpha":
    test_sys = makeLinuxAlphaSystem(test_mem_mode, bm[0])
elif buildEnv['TARGET_ISA'] == "mips":
    test_sys = makeLinuxMipsSystem(test_mem_mode, bm[0])
elif buildEnv['TARGET_ISA'] == "sparc":
    test_sys = makeSparcSystem(test_mem_mode, bm[0])
elif buildEnv['TARGET_ISA'] == "x86":
    test_sys = makeLinuxX86System(test_mem_mode, options.num_cpus, bm[0])
elif buildEnv['TARGET_ISA'] == "arm":
    test_sys = makeArmSystem(test_mem_mode, options.machine_type, bm[0],
                             options.dtb_filename,
                             bare_metal=options.bare_metal)
    if options.enable_context_switch_stats_dump:
        test_sys.enable_context_switch_stats_dump = True
else:
    fatal("Incapable of building %s full system!", buildEnv['TARGET_ISA'])

# Create a top-level voltage domain
test_sys.voltage_domain = VoltageDomain(voltage = options.sys_voltage)

# Create a source clock for the system and set the clock period
test_sys.clk_domain = SrcClockDomain(clock =  options.sys_clock,
                                     voltage_domain = test_sys.voltage_domain)

# Create a CPU voltage domain
test_sys.cpu_voltage_domain = VoltageDomain()

# Create a source clock for the CPUs and set the clock period
test_sys.cpu_clk_domain = SrcClockDomain(clock = options.cpu_clock,
                                         voltage_domain =
                                         test_sys.cpu_voltage_domain)

if options.kernel is not None:
    test_sys.kernel = binary(options.kernel)

if options.script is not None:
    test_sys.readfile = options.script

if options.lpae:
    test_sys.have_lpae = True

if options.virtualisation:
    test_sys.have_virtualization = True

test_sys.init_param = options.init_param

# For now, assign all the CPUs to the same clock domain
test_sys.cpu = [TestCPUClass(clk_domain=test_sys.cpu_clk_domain, cpu_id=i)
                for i in xrange(np)]

if is_kvm_cpu(TestCPUClass) or is_kvm_cpu(FutureClass):
    test_sys.vm = KvmVM()

if options.caches or options.l2cache:
    # By default the IOCache runs at the system clock
    test_sys.iocache = IOCache(addr_ranges = test_sys.mem_ranges)
    test_sys.iocache.cpu_side = test_sys.iobus.master
    test_sys.iocache.mem_side = test_sys.membus.slave
else:
    test_sys.iobridge = Bridge(delay='50ns', ranges = test_sys.mem_ranges)
    test_sys.iobridge.slave = test_sys.iobus.master
    test_sys.iobridge.master = test_sys.membus.slave

# Sanity check
if options.fastmem:
    if TestCPUClass != AtomicSimpleCPU:
        fatal("Fastmem can only be used with atomic CPU!")
    if (options.caches or options.l2cache):
        fatal("You cannot use fastmem in combination with caches!")

for i in xrange(np):
    if options.fastmem:
        test_sys.cpu[i].fastmem = True
    if options.checker:
        test_sys.cpu[i].addCheckerCpu()
    test_sys.cpu[i].createThreads()

CacheConfig.config_cache(options, test_sys)
MemConfig.config_mem(options, test_sys)

if len(bm) == 2:
    if buildEnv['TARGET_ISA'] == 'alpha':
        drive_sys = makeLinuxAlphaSystem(drive_mem_mode, bm[1])
    elif buildEnv['TARGET_ISA'] == 'mips':
        drive_sys = makeLinuxMipsSystem(drive_mem_mode, bm[1])
    elif buildEnv['TARGET_ISA'] == 'sparc':
        drive_sys = makeSparcSystem(drive_mem_mode, bm[1])
    elif buildEnv['TARGET_ISA'] == 'x86':
        drive_sys = makeLinuxX86System(drive_mem_mode, np, bm[1])
    elif buildEnv['TARGET_ISA'] == 'arm':
        drive_sys = makeArmSystem(drive_mem_mode, options.machine_type, bm[1])

    # Create a top-level voltage domain
    drive_sys.voltage_domain = VoltageDomain(voltage = options.sys_voltage)

    # Create a source clock for the system and set the clock period
    drive_sys.clk_domain = SrcClockDomain(clock =  options.sys_clock)

    # Create a CPU voltage domain
    drive_sys.cpu_voltage_domain = VoltageDomain()

    # Create a source clock for the CPUs and set the clock period
    drive_sys.cpu_clk_domain = SrcClockDomain(clock = options.cpu_clock,
                                              voltage_domain =
                                              drive_sys.cpu_voltage_domain)

    drive_sys.cpu = DriveCPUClass(clk_domain=drive_sys.cpu_clk_domain,
                                  cpu_id=0)
    drive_sys.cpu.createThreads()
    drive_sys.cpu.createInterruptController()
    drive_sys.cpu.connectAllPorts(drive_sys.membus)
    if options.fastmem:
        drive_sys.cpu.fastmem = True
    if options.kernel is not None:
        drive_sys.kernel = binary(options.kernel)

    if is_kvm_cpu(DriveCPUClass):
        drive_sys.vm = KvmVM()

    drive_sys.iobridge = Bridge(delay='50ns',
                                ranges = drive_sys.mem_ranges)
    drive_sys.iobridge.slave = drive_sys.iobus.master
    drive_sys.iobridge.master = drive_sys.membus.slave

    # Create the appropriate memory controllers and connect them to the
    # memory bus
    drive_sys.mem_ctrls = [DriveMemClass(range = r)
                           for r in drive_sys.mem_ranges]
    for i in xrange(len(drive_sys.mem_ctrls)):
        drive_sys.mem_ctrls[i].port = drive_sys.membus.master

    drive_sys.init_param = options.init_param
    root = makeDualRoot(True, test_sys, drive_sys, options.etherdump)
elif len(bm) == 1:
    root = Root(full_system=True, system=test_sys)
else:
    print "Error I don't know how to create more than 2 systems."
    sys.exit(1)

if options.timesync:
    root.time_sync_enable = True

if options.frame_capture:
    VncServer.frame_capture = True

Simulation.setWorkCountOptions(test_sys, options)
Simulation.run(options, root, test_sys, FutureClass)
<|endoftext|>