Exemplo n.º 1
0
 def __init__(self):
     self.step = 0
     self.time = 0
     self.endTime = 0
     self.idCounter = 1000
     self.nextMomentDeltaT = 1
     self.model = None
     self.scenario = Scenario()
     self.FEL = EventList()
     self.objects = dict()
     self.stat = None
     self.nextEvtTime = 0
     pass
Exemplo n.º 2
0
class Simulator:
    time = 0
    event_list = EventList()

    def now(self):
        return self.time

    def insert_ev(self, ev):
        self.event_list.ins(ev)

    def do_all_events(self):
        ev = self.event_list.remove_first()
        while ev is not None:
            self.time = ev.time
            ev.execute(self)
            ev = self.event_list.remove_first()
Exemplo n.º 3
0
	def __init__(self, backend, parent=None, extra_panels=None, id=-1, pos=wx.DefaultPosition, size=wx.DefaultSize, style=0, name="ListBoxController"):
		wx.Panel.__init__(self, parent, id, pos, size, style, name)
		self.backend = backend
		
		self.nb = wx.Notebook(self, style=wx.NB_LEFT)
		
		self.children = []
		
		self.avatar_list = AvatarList(self.backend, self.nb)
		self.nb.AddPage(self.avatar_list, "Avatar")
		self.children.append(self.avatar_list)
		
		self.motion_list = MotionList(self.backend, self.nb)
		self.nb.AddPage(self.motion_list, "Motions")
		self.children.append(self.motion_list)
		
		self.motion_type_list = MotionTypeList(self.backend, self.nb)
		self.nb.AddPage(self.motion_type_list, "Motions Types")
		self.children.append(self.motion_type_list)
		
		self.event_list = EventList(self.backend, self.nb)
		self.nb.AddPage(self.event_list, "Events")
		self.children.append(self.event_list)
		
		self.parameter_window = ParameterWindow(self.backend, self.nb)
		self.nb.AddPage(self.parameter_window, "Parameters")
		self.children.append(self.parameter_window)
		
		self.auto_creators = AutoCreatorsWindow(self.backend, self.nb)
		self.nb.AddPage(self.auto_creators, "AutoCreators")
		self.children.append(self.auto_creators)
		
		self.extra_panels = []
		if extra_panels != None:
			for panel in extra_panels:
				self.extra_panels.append(panel(self.backend, self.nb))
				
				self.nb.AddPage(self.extra_panels[-1], self.extra_panels[-1].getName())
				self.children.append(self.extra_panels[-1])
		
		
		# finally, put the notebook in a sizer for the panel to manage
		# the layout
		sizer = wx.BoxSizer()
		sizer.Add(self.nb, 1, wx.EXPAND)
		self.SetSizer(sizer)
Exemplo n.º 4
0
 def __init__(self):
     self.monitor_list = EventList()
     threading.Thread.__init__(self)
Exemplo n.º 5
0
class MonitorScheduler(threading.Thread):
    def __init__(self):
        self.monitor_list = EventList()
        threading.Thread.__init__(self)

    def new_monitor(self, type, plugin, watch_rule):
        if type in ('socket', 'unix_socket'):
            from MonitorSocket import MonitorSocket
            monitor = MonitorSocket(plugin, watch_rule)
            self.monitor_list.appendRule(monitor)

        elif type == 'database':
            from MonitorDatabase import MonitorDatabase
            monitor = MonitorDatabase(plugin, watch_rule)
            self.monitor_list.appendRule(monitor)

        elif type == ('command'):
            from MonitorCommand import MonitorCommand
            monitor = MonitorCommand(plugin, watch_rule)
            self.monitor_list.appendRule(monitor)

        elif type == ('http'):
            from MonitorHTTP import MonitorHTTP
            monitor = MonitorHTTP(plugin, watch_rule)
            self.monitor_list.appendRule(monitor)

        elif type == ('session'):
            from MonitorSession import MonitorSession
            monitor = MonitorSession(plugin, watch_rule)
            self.monitor_list.appendRule(monitor)
#
#        TODO: still not implemented
#
#        elif type in ('log', 'file'):
#            from MonitorFile import MonitorFile
#            monitor = MonitorFile(plugin, watch_rule)
#            self.monitor_list.appendRule(monitor)
#

    def run(self):
        logger.debug("Monitor Scheduler started")

        while 1:
            remove_monitors = []

            for monitor in self.monitor_list:
                # get monitor from monitor list
                # process watch-rule and remove from list

                # TODO: check if monitor is a Monitor instance
                # if isinstance(monitor, Monitor)

                if monitor.process():
                    remove_monitors.append(monitor)

            for m in remove_monitors:
                self.monitor_list.removeRule(m)

            # don't overload agent
            time.sleep(2)
Exemplo n.º 6
0
    def process(self):
        """
        Main process. Do all works by «lazy way» — build the only objects,
        that is not already exists (Nano-«SCons/make»). 
        """
        filtereventspath = os.path.join(self.projectdir,"stw-filter-events.py")
        if not file_is_ok(filtereventspath):
            lf = open(filtereventspath, "w")
            lf.write(r"""# -*- coding: utf-8 -*-
import re
import time

def filter_events(event):
    # You can modify event attribute, or disable (filter) event, returning False
    # Sample processing below
    emailre_ = re.compile(r"(?P<email>[-a-z0-9_.]+@(?:[-a-z0-9]+\.)+[a-z]{2,6})",
                    re.IGNORECASE)

    if event.date > time.time()*1000:
       return False # Something wrong — event from future
    
    event.author = event.author.replace('OFFICE\\', '')
    event.author = event.author.lower().replace('"',"'")
    m = emailre_.search(event.author)
    if m:
        event.author = m.group('email') 
    event.author = event.author.replace('"',"'")
    if event.author in ["(no author)"]:
        event.author = "anonymous"
    
    event.comment = re.sub('[Bb][Uu][Gg]\s*\d+\.?', '', event.comment)
    if event.comment.startswith("*** empty log message ***"):
        event.comment = ""

    if len(event.comment) < 10:
        event.comment = ""

    #
    #crap_prefixes=[
    #    "/Users/dumb/myproject/cvs_root/",
    #    "/home/projects/myproject/cvsroot/"
    #]

    #for p in crap_prefixes:
    #    if event.filename.startswith(p):
    #       event.filename=event.filename.replace(p,"/") 

    #event.action=event.action
    return True
""")
            lf.close()

        hash_filter = hash4file(filtereventspath)

        self.activitypath = os.path.join(self.workdir,
                                         "".join(["activity-", hash_filter, ".xml"]))
        activitygourcepath  = os.path.join(self.workdir,
                                         "".join(["activity-", hash_filter, ".gource"]))
        
        events = EventList()    
        if not file_is_ok(self.activitypath) or not file_is_ok(activitygourcepath):
            localvars = {}
            try:
                execfile(filtereventspath, localvars)
            except:
                pass
            if "filter_events" in localvars:
                events.filter_events = localvars["filter_events"]
            else:
                print "Warning: can not load filter for events."
            events.read_log(self.inputfile) 
            events.write_xml(self.activitypath)
            events.write_gource_custom(activitygourcepath)
        else:    
            events.read_log(self.activitypath)
            
        self.movielength = self.get_movie_length(self.audiopath)

        self.analyze_events(events)
        self.ms4frame = int(float(self.historylength*1000)/(self.movielength*24)) - 1
        self.secs4day = float(self.movielength*24*3600)/self.historylength/math.pi

        self.scenariopath = os.path.join(self.projectdir, "stw-scenario.txt")
        if not file_is_ok(self.scenariopath):
            lf = open(self.scenariopath, "w")
        else:    
            lf = open(self.scenariopath + ".template", "w")
        lf.write(self.template.encode("utf-8"))
        lf.close()
        
        self.scenariohash = hash4file(self.scenariopath, self.version__)

        self.configpath = os.path.join(self.projectdir, "stw-config.py")
        self.configobjpath = os.path.join(self.workdir, "stw-config.txt")
        if not file_is_ok(self.configpath):
            self.write_default_config()
            
        self.confighash = hash4file(self.configpath, self.version__)
        self.process_user_config()

        if self.need_codeswarm:
            jarsep = ":"
            if os.name == "nt":
               jarsep = ";"
            if not file_is_ok(os.path.join(self.cwsnapshotdir, "cs-00007.png")):
                s = "".join([ 'java -Xmx1000m -classpath ',
                             self.toolsdir, '/code_swarm.jar', jarsep, 
                             self.toolsdir, '/lib/core.jar', jarsep,
                             self.toolsdir, '/lib/xml.jar', jarsep,
                             self.toolsdir, '/lib/vecmath.jar', jarsep, '. ',
                             'code_swarm ', self.configobjpath, '/' ])
                print s
                os.system(s)

        gourceconfhash = hash4file(None, "".join([self.exedir, r'\gource',
                     ' -', str(self.width), 'x', str(self.height),
                     ' --background ', self.background,
                     ' --seconds-per-day ', str(self.secs4day),
                     ]) )
        
        grsnapshotdir = os.path.join(self.workdir, "g-"+gourceconfhash)
        createdir(grsnapshotdir)
        gourcerawpath = os.path.join(grsnapshotdir, "gource-raw.avi")
        if not file_is_ok(gourcerawpath):
            s = "".join([self.exedir, r'\gource',
                         ' -', str(self.width), 'x', str(self.height),
                         ' --user-scale 2 --output-framerate 25 ',
                         ' --background ', self.background,
                         ' --stop-position 1 ',
                         ' --highlight-all-users ',
                         r' --date-format "%Y-%m-%d"',
                         ' --seconds-per-day ', str(self.secs4day),
                         ' --log-format custom ',
                         ' --output-ppm-stream ', gourcerawpath, '.ppm ',
                         activitygourcepath ])
            print s
            os.system(s)
            #PPM-file is very Huge. We need to compress it to h264-avi, and later kill it.
            s = "".join([self.exedir, r'\ffmpeg',
                         ' -y -b 9000K -f image2pipe -vcodec ppm ',
                         ' -i "', gourcerawpath, '.ppm"',
                         ' -fpre ', os.path.join(self.toolsdir,'ll.ffpreset'),
                         ' -i "', gourcerawpath, '.ppm"',
                         ' -vcodec libx264 "', gourcerawpath, '"'])
            print s
            os.system(s)
            if file_is_ok(gourcerawpath):
                os.unlink(gourcerawpath + ".ppm")
            else:
                raise Exception("Something goes wrong with \n" + s)
            
        srtpath = os.path.join(self.workdir, "".join(["movie-", self.scenariohash, ".srt"]))
        if not file_is_ok(srtpath):
            self.process_subtitles(srtpath)
        
        if self.need_codeswarm:
            moviedir = os.path.join(self.cwsnapshotdir, self.scenariohash)
            createdir(moviedir)
            codeswarmpath = os.path.join(moviedir, "codeswarm.avi")
            if not file_is_ok(codeswarmpath):
                os.chdir(self.cwsnapshotdir)
                framescount = len(os.listdir(self.cwsnapshotdir))
                codeswarmfps = framescount / self.movielength
                s = "".join([ self.exedir, r'\mencoder ',
                             ' mf://*.png -mf ',
                             ' fps=', str(codeswarmfps), ':type=png',
                             ' -ovc x264 -x264encopts pass=1:bitrate=10000 ',
                             ' -oac copy ',
                             ' -audiofile "', self.audiopath, '"',
                             ' -sub "', srtpath, '"',
                             ' -utf8 -font "', self.fontpath, '"',
                             ' -subfont-text-scale 3 -sub-bg-color 20 -sub-bg-alpha 70 ',
                             ' -o "', codeswarmpath, '"'])
                print s
                os.system(s)
                os.chdir(self.homedir)

        if self.need_gource:
            moviedir = os.path.join(grsnapshotdir, self.scenariohash)
            createdir(moviedir)
            gourcepath = os.path.join(moviedir, "gource.avi")
            if not file_is_ok(gourcepath):
                gourcerawlenght = self.get_movie_length(gourcerawpath)
                gourcefps = float(gourcerawlenght / self.movielength)
                os.chdir(self.cwsnapshotdir)
                if not file_is_ok(gourcerawpath + ".fps"):
                    s = "".join([self.exedir, r'\mencoder "', gourcerawpath, '"',
                               ' -ovc x264 -x264encopts pass=1:bitrate=10000 ',
                               ' -ofps 25 -speed ' , str(gourcefps),
                               ' -o "', gourcerawpath, '.fps"' ])
                    print s
                    os.system(s)
                s = "".join([self.exedir, r'\mencoder ',
                             gourcerawpath,'.fps',
                             ' -ovc x264 -x264encopts pass=1:bitrate=10000 ',
                             ' -oac copy -audiofile "', self.audiopath, '"',
                             ' -sub "', srtpath, '"',
                             ' -utf8 -font "', self.fontpath, '"',
                             ' -subfont-text-scale 3 ',
                             ' -sub-bg-color 20 -sub-bg-alpha 70 ',
                             ' -o "', gourcepath, '"' ])
                print s
                os.system(s)
                os.chdir(self.homedir)
            
        def get_result_path(sourcefile, suffix):
            """
              Get path to result video file, using
              @sourcefile and @suffix
            """
            hash_ = hash4file(sourcefile)
            res = os.path.join(self.projectdir,
                  "".join([self.projectname,"-", suffix, "-", hash_, ".avi"]))
            return res

            
        if self.need_codeswarm and file_is_ok(codeswarmpath):
            resultcodeswarmpath = get_result_path(codeswarmpath, "codeswarm")
            if not file_is_ok(resultcodeswarmpath):
                shutil.copy(codeswarmpath, resultcodeswarmpath)
        
        if self.need_gource and file_is_ok(gourcepath   ):
            resultgourcepath = get_result_path(gourcepath, "gource")
            if not file_is_ok(resultgourcepath ):
                shutil.copy(gourcepath, resultgourcepath)
Exemplo n.º 7
0
 def __init__(self, conf):
     self._conf = conf
     self.__event_list = EventList()
     self.start_time = time.time()
     self.enable = self.__is_consolidation_enable()
Exemplo n.º 8
0
class EventConsolidation:

    # name of consolidation section at config.cfg
    CONSOLIDATION_SECTION = "event-consolidation"
    MAX_TIME = 60.0
    EVENT_OCCURRENCES = "occurrences"  # event field for occurrences value

    def __init__(self, conf):
        self._conf = conf
        self.__event_list = EventList()
        self.start_time = time.time()
        self.enable = self.__is_consolidation_enable()

    def __is_consolidation_enable(self):

        section = EventConsolidation.CONSOLIDATION_SECTION

        for option in ("enable", "time"):
            if not self._conf.has_option(section, option):
                logger.warning("There is no %s option in %s section" % \
                    (option, EventConsolidation.CONSOLIDATION_SECTION))
                return False

        # exit if enable = False
        if not self._conf.getboolean(section, "enable"):
            logger.info("Consolidation is not enabled")
            return False

        # "time" must be a float number
        if not self._conf.getfloat(section, "time"):
            logger.warning("There is no time variable in %s section" % \
                           (EventConsolidation.CONSOLIDATION_SECTION))
            return False

        return True

    def __pass_filters(self, event):

        section = EventConsolidation.CONSOLIDATION_SECTION

        # 1) consolidation by plugin
        if self._conf.has_option(section, "by_plugin"):
            plugins = Config.split_sids(self._conf.get(section, "by_plugin"))

            if str(event["plugin_id"]) in plugins:
                return True

        # 2) consolidation by src_ip
        for target in ("src_ip", "dst_ip", "sensor"):
            option = "by_" + target

            if self._conf.has_option(section, option):
                ips = Config.split_sids(self._conf.get(section, option))

                if event[target]:
                    if str(event[target]) in ips:
                        return True

        return False

    def insert(self, event):

        if not self.enable:
            return False

        if not self.__pass_filters(event):
            return False

        self.__event_list.appendRule(event)
        logger.debug("Added event (id:%s, sid:%s) to consolidation queue" % \
                     (event["plugin_id"], event["plugin_sid"]))
        Stats.consolidation['total'] += 1

        return True

    # clear consolidation queue processing its events
    # and removing them from the list
    def clear(self):

        events_to_remove = []

        for event in self.__event_list:
            Output.event(event)
            events_to_remove.append(event)
            Stats.consolidation['consolidated'] += 1

        for e in events_to_remove:
            self.__event_list.removeRule(e)

        del events_to_remove

    def __process(self):

        # tmp set to store event representation
        event_tmp = {}

        events_to_remove = []

        for event in self.__event_list:

            # save non comparable attributes
            date = event["date"]
            log = event["log"]
            event["date"] = event["log"] = ""

            str_event = str(event).strip()

            if event_tmp.has_key(str_event):
                events_to_remove.append(event)
                event_tmp[str_event] += 1
            else:
                event_tmp[str_event] = 1

            # restore non comparable attributes
            if date:
                event["date"] = date

            if log:
                event["log"] = log

        # remove duplicated events
        for e in events_to_remove:
            self.__event_list.removeRule(e)

        del events_to_remove

        # fill "occurrences" field
        for event in self.__event_list:

            date = event["date"]
            log = event["log"]
            event["date"] = event["log"] = ""

            str_event = str(event).strip()
            if event_tmp.has_key(str_event):
                occurrences = int(event_tmp[str_event])

                if occurrences > 1:
                    event[EventConsolidation.EVENT_OCCURRENCES] = \
                        str(occurrences)

            if date:
                event["date"] = date

            if log:
                event["log"] = log

        self.clear()

    def process(self):

        if not self.enable:
            return False

        section = EventConsolidation.CONSOLIDATION_SECTION
        restart_time = self._conf.getfloat(section, "time")

        if restart_time > EventConsolidation.MAX_TIME:
            restart_time = EventConsolidation.MAX_TIME

        current_time = time.time()

        if self.start_time + restart_time < current_time:
            if len(self) > 0:
                self.__process()

            # back to begining
            self.start_time = time.time()

    def __len__(self):
        return len(self.__event_list)
Exemplo n.º 9
0
 def __init__(self, conf):
     self._conf = conf
     self.__event_list = EventList()
     self.start_time = time.time()
     self.enable = self.__is_consolidation_enable()
Exemplo n.º 10
0
class EventConsolidation:

    # name of consolidation section at config.cfg
    CONSOLIDATION_SECTION = "event-consolidation"
    MAX_TIME = 60.0
    EVENT_OCCURRENCES = "occurrences" # event field for occurrences value


    def __init__(self, conf):
        self._conf = conf
        self.__event_list = EventList()
        self.start_time = time.time()
        self.enable = self.__is_consolidation_enable()


    def __is_consolidation_enable(self):

        section = EventConsolidation.CONSOLIDATION_SECTION

        for option in ("enable", "time"):
            if not self._conf.has_option(section, option):
                logger.warning("There is no %s option in %s section" % \
                    (option, EventConsolidation.CONSOLIDATION_SECTION))
                return False 
        
        # exit if enable = False
        if not self._conf.getboolean(section, "enable"):
            logger.info("Consolidation is not enabled")
            return False

        # "time" must be a float number
        if not self._conf.getfloat(section, "time"):
            logger.warning("There is no time variable in %s section" % \
                           (EventConsolidation.CONSOLIDATION_SECTION))
            return False

        return True


    def __pass_filters(self, event):

        section = EventConsolidation.CONSOLIDATION_SECTION

        # 1) consolidation by plugin
        if self._conf.has_option(section, "by_plugin"):
            plugins = Config.split_sids(self._conf.get(section, "by_plugin"))

            if str(event["plugin_id"]) in plugins:
                return True

        # 2) consolidation by src_ip
        for target in ("src_ip", "dst_ip", "sensor"):
            option = "by_" + target

            if self._conf.has_option(section, option):
                ips = Config.split_sids(self._conf.get(section, option))

                if event[target]:
                    if str(event[target]) in ips:
                        return True

        return False


    def insert(self, event):

        if not self.enable:
            return False

        if not self.__pass_filters(event):
            return False

        self.__event_list.appendRule(event)
        logger.debug("Added event (id:%s, sid:%s) to consolidation queue" % \
                     (event["plugin_id"], event["plugin_sid"]))
        Stats.consolidation['total'] += 1

        return True


    # clear consolidation queue processing its events
    # and removing them from the list
    def clear(self):

        events_to_remove = []

        for event in self.__event_list:
            Output.event(event)
            events_to_remove.append(event)
            Stats.consolidation['consolidated'] += 1

        for e in events_to_remove:
            self.__event_list.removeRule(e)

        del events_to_remove


    def __process(self):

        # tmp set to store event representation
        event_tmp = {}

        events_to_remove = []

        for event in self.__event_list:

            # save non comparable attributes
            date = event["date"]
            log = event["log"]
            event["date"] = event["log"] = ""

            str_event = str(event).strip()

            if event_tmp.has_key(str_event):
                events_to_remove.append(event)
                event_tmp[str_event] += 1
            else:
                event_tmp[str_event] = 1

            # restore non comparable attributes
            if date:
                event["date"] = date

            if log:
                event["log"] = log

        # remove duplicated events
        for e in events_to_remove:
            self.__event_list.removeRule(e)

        del events_to_remove

        # fill "occurrences" field
        for event in self.__event_list:

            date = event["date"]
            log = event["log"]
            event["date"] = event["log"] = ""

            str_event = str(event).strip()
            if event_tmp.has_key(str_event):
                occurrences = int(event_tmp[str_event])

                if occurrences > 1:
                    event[EventConsolidation.EVENT_OCCURRENCES] = \
                        str(occurrences)

            if date:
                event["date"] = date

            if log:
                event["log"] = log

        self.clear()


    def process(self):

        if not self.enable:
            return False

        section = EventConsolidation.CONSOLIDATION_SECTION
        restart_time = self._conf.getfloat(section, "time")

        if restart_time > EventConsolidation.MAX_TIME:
            restart_time = EventConsolidation.MAX_TIME

        current_time = time.time()

        if self.start_time + restart_time < current_time:
            if len(self) > 0:
                self.__process()

            # back to begining
            self.start_time = time.time()


    def __len__(self):
        return len(self.__event_list)
 def __init__(self):
     self.monitor_list = EventList()
     threading.Thread.__init__(self)
class MonitorScheduler(threading.Thread):

    def __init__(self):
        self.monitor_list = EventList()
        threading.Thread.__init__(self)


    def new_monitor(self, type, plugin, watch_rule):
        if type in ('socket', 'unix_socket'):
            from MonitorSocket import MonitorSocket
            monitor = MonitorSocket(plugin, watch_rule)
            self.monitor_list.appendRule(monitor)

        elif type == 'database':
            from MonitorDatabase import MonitorDatabase
            monitor = MonitorDatabase(plugin, watch_rule)
            self.monitor_list.appendRule(monitor)

        elif type == ('command'):
            from MonitorCommand import MonitorCommand
            monitor = MonitorCommand(plugin, watch_rule)
            self.monitor_list.appendRule(monitor)

        elif type == ('http'):
            from MonitorHTTP import MonitorHTTP
            monitor = MonitorHTTP(plugin, watch_rule)
            self.monitor_list.appendRule(monitor)

        elif type == ('session'):
            from MonitorSession import MonitorSession
            monitor = MonitorSession(plugin, watch_rule)
            self.monitor_list.appendRule(monitor)
#
#        TODO: still not implemented
#
#        elif type in ('log', 'file'):
#            from MonitorFile import MonitorFile
#            monitor = MonitorFile(plugin, watch_rule)
#            self.monitor_list.appendRule(monitor)
#
         

    def run(self):
        logger.debug("Monitor Scheduler started")

        while 1:
            remove_monitors = []

            for monitor in self.monitor_list:
                # get monitor from monitor list
                # process watch-rule and remove from list

                # TODO: check if monitor is a Monitor instance
                # if isinstance(monitor, Monitor)

                if monitor.process():
                    remove_monitors.append(monitor)

            for m in remove_monitors:
                self.monitor_list.removeRule(m)

            # don't overload agent
            time.sleep(2)
Exemplo n.º 13
0
    def process(self):
        """
        Main process. Do all works by «lazy way» — build the only objects,
        that is not already exists (Nano-«SCons/make»). 
        """
        filtereventspath = os.path.join(self.projectdir,
                                        "stw-filter-events.py")
        if not file_is_ok(filtereventspath):
            lf = open(filtereventspath, "w")
            lf.write(r"""# -*- coding: utf-8 -*-
import re
import time

def filter_events(event):
    # You can modify event attribute, or disable (filter) event, returning False
    # Sample processing below
    emailre_ = re.compile(r"(?P<email>[-a-z0-9_.]+@(?:[-a-z0-9]+\.)+[a-z]{2,6})",
                    re.IGNORECASE)

    if event.date > time.time()*1000:
       return False # Something wrong — event from future
    
    event.author = event.author.replace('OFFICE\\', '')
    event.author = event.author.lower().replace('"',"'")
    m = emailre_.search(event.author)
    if m:
        event.author = m.group('email') 
    event.author = event.author.replace('"',"'")
    if event.author in ["(no author)"]:
        event.author = "anonymous"
    
    event.comment = re.sub('[Bb][Uu][Gg]\s*\d+\.?', '', event.comment)
    if event.comment.startswith("*** empty log message ***"):
        event.comment = ""

    if len(event.comment) < 10:
        event.comment = ""

    #
    #crap_prefixes=[
    #    "/Users/dumb/myproject/cvs_root/",
    #    "/home/projects/myproject/cvsroot/"
    #]

    #for p in crap_prefixes:
    #    if event.filename.startswith(p):
    #       event.filename=event.filename.replace(p,"/") 

    #event.action=event.action
    return True
""")
            lf.close()

        hash_filter = hash4file(filtereventspath)

        self.activitypath = os.path.join(
            self.workdir, "".join(["activity-", hash_filter, ".xml"]))
        activitygourcepath = os.path.join(
            self.workdir, "".join(["activity-", hash_filter, ".gource"]))

        events = EventList()
        if not file_is_ok(
                self.activitypath) or not file_is_ok(activitygourcepath):
            localvars = {}
            try:
                execfile(filtereventspath, localvars)
            except:
                pass
            if "filter_events" in localvars:
                events.filter_events = localvars["filter_events"]
            else:
                print "Warning: can not load filter for events."
            events.read_log(self.inputfile)
            events.write_xml(self.activitypath)
            events.write_gource_custom(activitygourcepath)
        else:
            events.read_log(self.activitypath)

        self.movielength = self.get_movie_length(self.audiopath)

        self.analyze_events(events)
        self.ms4frame = int(
            float(self.historylength * 1000) / (self.movielength * 24)) - 1
        self.secs4day = float(
            self.movielength * 24 * 3600) / self.historylength / math.pi

        self.scenariopath = os.path.join(self.projectdir, "stw-scenario.txt")
        if not file_is_ok(self.scenariopath):
            lf = open(self.scenariopath, "w")
        else:
            lf = open(self.scenariopath + ".template", "w")
        lf.write(self.template.encode("utf-8"))
        lf.close()

        self.scenariohash = hash4file(self.scenariopath, self.version__)

        self.configpath = os.path.join(self.projectdir, "stw-config.py")
        self.configobjpath = os.path.join(self.workdir, "stw-config.txt")
        if not file_is_ok(self.configpath):
            self.write_default_config()

        self.confighash = hash4file(self.configpath, self.version__)
        self.process_user_config()

        if self.need_codeswarm:
            jarsep = ":"
            if os.name == "nt":
                jarsep = ";"
            if not file_is_ok(os.path.join(self.cwsnapshotdir,
                                           "cs-00007.png")):
                s = "".join([
                    'java -Xmx1000m -classpath ', self.toolsdir,
                    '/code_swarm.jar', jarsep, self.toolsdir, '/lib/core.jar',
                    jarsep, self.toolsdir, '/lib/xml.jar', jarsep,
                    self.toolsdir, '/lib/vecmath.jar', jarsep, '. ',
                    'code_swarm ', self.configobjpath, '/'
                ])
                print s
                os.system(s)

        gourceconfhash = hash4file(
            None, "".join([
                self.exedir,
                r'\gource',
                ' -',
                str(self.width),
                'x',
                str(self.height),
                ' --background ',
                self.background,
                ' --seconds-per-day ',
                str(self.secs4day),
            ]))

        grsnapshotdir = os.path.join(self.workdir, "g-" + gourceconfhash)
        createdir(grsnapshotdir)
        gourcerawpath = os.path.join(grsnapshotdir, "gource-raw.avi")
        if not file_is_ok(gourcerawpath):
            s = "".join([
                self.exedir, r'\gource', ' -',
                str(self.width), 'x',
                str(self.height), ' --user-scale 2 --output-framerate 25 ',
                ' --background ', self.background, ' --stop-position 1 ',
                ' --highlight-all-users ', r' --date-format "%Y-%m-%d"',
                ' --seconds-per-day ',
                str(self.secs4day), ' --log-format custom ',
                ' --output-ppm-stream ', gourcerawpath, '.ppm ',
                activitygourcepath
            ])
            print s
            os.system(s)
            #PPM-file is very Huge. We need to compress it to h264-avi, and later kill it.
            s = "".join([
                self.exedir, r'\ffmpeg',
                ' -y -b 9000K -f image2pipe -vcodec ppm ', ' -i "',
                gourcerawpath, '.ppm"', ' -fpre ',
                os.path.join(self.toolsdir,
                             'll.ffpreset'), ' -i "', gourcerawpath, '.ppm"',
                ' -vcodec libx264 "', gourcerawpath, '"'
            ])
            print s
            os.system(s)
            if file_is_ok(gourcerawpath):
                os.unlink(gourcerawpath + ".ppm")
            else:
                raise Exception("Something goes wrong with \n" + s)

        srtpath = os.path.join(self.workdir,
                               "".join(["movie-", self.scenariohash, ".srt"]))
        if not file_is_ok(srtpath):
            self.process_subtitles(srtpath)

        if self.need_codeswarm:
            moviedir = os.path.join(self.cwsnapshotdir, self.scenariohash)
            createdir(moviedir)
            codeswarmpath = os.path.join(moviedir, "codeswarm.avi")
            if not file_is_ok(codeswarmpath):
                os.chdir(self.cwsnapshotdir)
                framescount = len(os.listdir(self.cwsnapshotdir))
                codeswarmfps = framescount / self.movielength
                s = "".join([
                    self.exedir, r'\mencoder ', ' mf://*.png -mf ', ' fps=',
                    str(codeswarmfps), ':type=png',
                    ' -ovc x264 -x264encopts pass=1:bitrate=10000 ',
                    ' -oac copy ', ' -audiofile "', self.audiopath, '"',
                    ' -sub "', srtpath, '"', ' -utf8 -font "', self.fontpath,
                    '"',
                    ' -subfont-text-scale 3 -sub-bg-color 20 -sub-bg-alpha 70 ',
                    ' -o "', codeswarmpath, '"'
                ])
                print s
                os.system(s)
                os.chdir(self.homedir)

        if self.need_gource:
            moviedir = os.path.join(grsnapshotdir, self.scenariohash)
            createdir(moviedir)
            gourcepath = os.path.join(moviedir, "gource.avi")
            if not file_is_ok(gourcepath):
                gourcerawlenght = self.get_movie_length(gourcerawpath)
                gourcefps = float(gourcerawlenght / self.movielength)
                os.chdir(self.cwsnapshotdir)
                if not file_is_ok(gourcerawpath + ".fps"):
                    s = "".join([
                        self.exedir, r'\mencoder "', gourcerawpath, '"',
                        ' -ovc x264 -x264encopts pass=1:bitrate=10000 ',
                        ' -ofps 25 -speed ',
                        str(gourcefps), ' -o "', gourcerawpath, '.fps"'
                    ])
                    print s
                    os.system(s)
                s = "".join([
                    self.exedir, r'\mencoder ', gourcerawpath, '.fps',
                    ' -ovc x264 -x264encopts pass=1:bitrate=10000 ',
                    ' -oac copy -audiofile "', self.audiopath, '"', ' -sub "',
                    srtpath, '"', ' -utf8 -font "', self.fontpath, '"',
                    ' -subfont-text-scale 3 ',
                    ' -sub-bg-color 20 -sub-bg-alpha 70 ', ' -o "', gourcepath,
                    '"'
                ])
                print s
                os.system(s)
                os.chdir(self.homedir)

        def get_result_path(sourcefile, suffix):
            """
              Get path to result video file, using
              @sourcefile and @suffix
            """
            hash_ = hash4file(sourcefile)
            res = os.path.join(
                self.projectdir,
                "".join([self.projectname, "-", suffix, "-", hash_, ".avi"]))
            return res

        if self.need_codeswarm and file_is_ok(codeswarmpath):
            resultcodeswarmpath = get_result_path(codeswarmpath, "codeswarm")
            if not file_is_ok(resultcodeswarmpath):
                shutil.copy(codeswarmpath, resultcodeswarmpath)

        if self.need_gource and file_is_ok(gourcepath):
            resultgourcepath = get_result_path(gourcepath, "gource")
            if not file_is_ok(resultgourcepath):
                shutil.copy(gourcepath, resultgourcepath)
Exemplo n.º 14
0
class Simulator:
    def __init__(self):
        self.step = 0
        self.time = 0
        self.endTime = 0
        self.idCounter = 1000
        self.nextMomentDeltaT = 1
        self.model = None
        self.scenario = Scenario()
        self.FEL = EventList()
        self.objects = dict()
        self.stat = None
        self.nextEvtTime = 0
        pass

    def initializeSimulator(self):
        if (self.model.time == "discrete"):
            self.nextMomentDeltaT = 1
        else:
            self.nextMomentDeltaT = 2

    def initializeScenarioRun(self):
        self.objects.clear()
        self.step = 0
        self.time = 0
        self.endTime = self.scenario.durationInSimTime or 100000  #must be set it to infinity
        self.idCounter = self.scenario.idCounter or 1000

        self.scenario.setupInitialState(self)

        if (self.model.setupStatistics(self) != False):
            stat = self.model.setupStatistics(self)

    def advanceSimulationTime(self):
        self.nextEvtTime = self.FEL.getNextOccurrenceTime()
        self.step += 1
        if (self.nextEvtTime > 0):
            self.time = self.nextEvtTime

    def runScenario(self):
        iteration = 0
        while (self.time < self.endTime and not self.FEL.isEmpty()):
            iteration += 1
            simulatorUI.logSimulationStep(self)
            self.advanceSimulationTime()
            nextEvents = self.FEL.removeNextEvents()
            for e in nextEvents:
                followUpEvents = e.onEvent(self)
                for f in followUpEvents:
                    self.FEL.add(f)

                # check if e is a recurrence
                if (e.recurrence() == 1):
                    self.FEL.add(e.createNextEvent(self))

        if (self.model.computeFinalStatistics(self) != False):
            stat = self.model.computeFinalStatistics(self)
            print(
                "\n-------------------- Compute Final Statistics --------------------------"
            )
            print("Stat:", stat)

    def runStandaloneScenario(self):
        self.initializeSimulator()
        self.initializeScenarioRun()
        self.runScenario()