示例#1
0
文件: simTest.py 项目: laurakah/clyde
 def testInit_setsStartOrientationRandomly(self):
     try1 = []
     try2 = []
     for i in range(0, 5):
         s1 = sim.Sim(self.gameMapFile, self.brainClass)
         try1.append(s1.getStartOrientation())
     for i in range(0, 5):
         s2 = sim.Sim(self.gameMapFile, self.brainClass)
         try2.append(s2.getStartOrientation())
     self.assertNotEqual(try1, try2)
示例#2
0
文件: simTest.py 项目: laurakah/clyde
 def testInit_startPosition_isRandom(self):
     startPosList1 = []
     startPosList2 = []
     for i in range(0, 20):
         s1 = sim.Sim(self.gameMapFile, self.brainClass,
                      sim.Sim.DEFAULT_TIMEOUT)
         startPosList1.append(s1.getStartPosition())
         s2 = sim.Sim(self.gameMapFile, self.brainClass,
                      sim.Sim.DEFAULT_TIMEOUT)
         startPosList2.append(s2.getStartPosition())
     self.assertNotEqual(startPosList1, startPosList2)
示例#3
0
文件: simTest.py 项目: laurakah/clyde
 def testRun_doesNotCallDrawWhenFollowIsFalse(self):
     global drawCalled
     s = sim.Sim(self.gameMapFile, self.brainClass, 2, 0, False)
     s.draw = fakeDraw
     drawCalled = False
     s.run()
     self.assertEqual(False, drawCalled)
示例#4
0
文件: simTest.py 项目: laurakah/clyde
 def testRun_callsPrintWhenFollowIsTrue(self):
     global printCalled
     printCalled = False
     s = sim.Sim(self.gameMapFile, self.brainClass, 2, 0, True)
     s._print = fakePrint
     s.run()
     self.assertEqual(True, printCalled)
示例#5
0
文件: simTest.py 项目: laurakah/clyde
    def testDraw_returnsSimAndPlayerMapSideBySide(self):
        global drawPlayerMapValue

        s1 = sim.Sim("maps/test-room2-l-shape.txt", self.brainClass,
                     sim.Sim.DEFAULT_TIMEOUT)

        txtExpect = ""
        txtPlayerMap = "###\n#*#\n###\n"

        # install fake
        drawPlayerMapValue = txtPlayerMap
        s1.drawPlayerMap = fakeDrawPlayerMap

        # get text maps
        txtSimMapArr = s1.drawSimMap().rstrip("\n").split('\n')
        txtPlayerMapArr = s1.drawPlayerMap().rstrip("\n").split('\n')

        # get map dimensions
        simMapHeight = s1.getSimMap().getHeight()
        playerMapHeight = len(txtPlayerMapArr)
        playerMapOffset = simMapHeight - playerMapHeight

        # combine both maps
        player_i = 0
        for sim_i in range(0, simMapHeight):
            txtExpect += txtSimMapArr[sim_i]
            if sim_i >= playerMapOffset:
                txtExpect += " "
                txtExpect += txtPlayerMapArr[player_i]
                player_i += 1
            txtExpect += "\n"

        self.assertEqual(txtExpect, s1.draw())
示例#6
0
文件: user.py 项目: ssm2017/pyosm
    def load_sims(self, load=False, load_all=False):
        logging.main_logger.debug("[user] 'load_sims' called")

        user_sims = []
        # get the user folder
        user_os_folder_path = "/home/" + self.name + "/opensimulator/sims"
        if not os.path.isdir(user_os_folder_path):
            logging.main_logger.warning(
                "[user] no simulator folder found for user : %s" % (self.name))
            return False

        # get the simulators folders
        for entry in os.listdir(user_os_folder_path):
            if entry.endswith('.sim'):
                sim_path = user_os_folder_path + '/' + entry
                if os.path.isdir(sim_path):
                    import sim
                    mysim = sim.Sim(sim_path, load, load_all)
                    if not mysim is None:
                        mysim.is_alive()
                        user_sims.append(mysim)

        if not user_sims:
            logging.main_logger.warning(
                "[user] no sim folder found for user : %s" % (self.name))
            return False

        self.sims = user_sims
        return True
示例#7
0
文件: simTest.py 项目: laurakah/clyde
 def testInit_startPosition_raisesExceptionWhenStartPositionIsNotCoordinateObj(
         self):
     with self.assertRaises(Exception) as ex:
         s1 = sim.Sim(self.gameMapFile, self.brainClass,
                      sim.Sim.DEFAULT_TIMEOUT, 0, False, {})
     self.assertEqual("startPosition is not a Coordinate object!",
                      ex.exception.message)
示例#8
0
    def __init__(self, master=None, logger=None):
        super().__init__(master)
        self.master = master
        self.logger = logger

        self.configure(bg=DARKCOLOR)
        self.pack()
        self.master.title("MAIA - Maine AI Arena")

        # Create the msgr objs
        self.msg_queue = queue.Queue()
        self.imsgr = msgs.IMsgr(self.msg_queue)
        self.omsgr = msgs.OMsgr(self.msg_queue)

        self.ldr = loader.Loader(self.logger)
        self.sim = sim.Sim(self.imsgr)

        # log.LogInit()
        # log_setting = self.ldr.getMainConfigData('debug')
        # if type(log_setting)==bool:
        #     log.LogSetDebug(log_setting)
        #     log.LogDebug("DEBUG IS ON")

        self.combat_log = []

        self.BuildUI()

        self.UIMap = None
示例#9
0
文件: simTest.py 项目: laurakah/clyde
 def testInit_playerOrientation_isStartOrientation(self):
     try1 = []
     try2 = []
     for i in range(0, 10):
         s = sim.Sim(self.gameMapFile, self.brainClass)
         try1.append(s.getStartOrientation())
         try2.append(s.player.getOrientation())
     self.assertEqual(try1, try2)
示例#10
0
文件: simTest.py 项目: laurakah/clyde
 def testInit_startPosition_raisesExceptionWhenStartPositonIsNotANonCollisionField(
         self):
     with self.assertRaises(Exception) as ex:
         s1 = sim.Sim(self.gameMapFile, self.brainClass,
                      sim.Sim.DEFAULT_TIMEOUT, 0, False, c.Coordinate(2, 1))
     self.assertEqual(
         "startPosition is not a non-collision field (in this map)!",
         ex.exception.message)
示例#11
0
文件: simTest.py 项目: laurakah/clyde
 def testRun_doesNotCallDrawWhenRunningStateIsFalse(self):
     global drawCalledNtimes
     drawCalledNtimes = 0
     s = sim.Sim(self.gameMapFile, self.brainClass, 2, 0, True)
     s.draw = fakeDraw
     # override only so that the real print is not called
     # in order not to polute our test output
     s._print = fakePrint
     s.run()
     self.assertEqual(2, drawCalledNtimes)
示例#12
0
文件: simTest.py 项目: laurakah/clyde
 def testRun_callsDrawWhenFollowIsTrue(self):
     global drawCalled
     s = sim.Sim(self.gameMapFile, self.brainClass, 2, 0, True)
     s.draw = fakeDraw
     # override only so that the real print is not called
     # in order not to polute our test output
     s._print = fakePrint
     drawCalled = False
     s.run()
     self.assertEqual(True, drawCalled)
示例#13
0
文件: simTest.py 项目: laurakah/clyde
 def testRun_callsPrintWithReturnFromDrawWithNewLineWhenFollowIsTrue(self):
     global drawValue
     global printValue
     drawValue = "xxx"
     printValue = None
     s = sim.Sim(self.gameMapFile, self.brainClass, 2, 0, True)
     s.draw = fakeDraw
     s._print = fakePrint
     s.run()
     self.assertEqual("xxx\n", printValue)
示例#14
0
def show_log(password, sim_path, log_type):
    logging.main_logger.debug("[xml-rpc] Method 'show_log' called.")

    # check password
    if not check_password(password):
        return wrong_password

    # compute the answer
    import base64
    mysim = sim.Sim(sim_path)
    return {"success": True, "log": base64.b64encode(mysim.show_log(log_type))}
示例#15
0
文件: region.py 项目: ssm2017/pyosm
    def set_active(self, name=''):
        logging.main_logger.debug("[region] 'set_active' called")

        if name != '':
            self.name = name
        if self.load():
            import sim
            act_sim = sim.Sim(self.sim_path)
            act_sim.send_command('change region ' + self.name)
            return True
        return False
示例#16
0
文件: simTest.py 项目: laurakah/clyde
 def testRun_delaysStepsByStepDelay(self):
     delayMs = 50
     timeOut = 3
     deviationMs = 20
     expectedDelta = (timeOut) * delayMs
     s = sim.Sim(self.gameMapFile, self.brainClass, timeOut, delayMs)
     t1 = time.time()
     s.run()
     t2 = time.time()
     deltaMs = int((t2 - t1) * 1000)
     self.assertAlmostEqual(expectedDelta, deltaMs, None, None, deviationMs)
示例#17
0
def run_sim(password, sim_path, action):
    logging.main_logger.debug("[xml-rpc] Method 'run_sim' called.")

    # check password
    if not check_password(password):
        return wrong_password

    # compute the answer
    mysim = sim.Sim(sim_path)
    if mysim.load():
        return {"success": True, "run_sim": mysim.run(action)}
    return {"success": False, "message": 'Sim not found'}
示例#18
0
def get_sim(password, sim_path):
    logging.main_logger.debug("[xml-rpc] Method 'get_sim' called.")

    # check password
    if not check_password(password):
        return wrong_password

    # compute the answer
    if not os.path.isdir(sim_path):
        return {"success": False, "message": 'Sim not found'}
    mysim = sim.Sim(sim_path, load_all=True)
    if mysim.load():
        mysim.is_alive()
        return {"success": True, "sim": mysim}
    return {"success": False, "message": 'Sim not found'}
示例#19
0
def launchSim(gameMapFile, brainClass, timeout, delay, follow, verbose,
              positionStr, orientationStr):
    brainName = brainClass.__name__.split(".")[-1]
    mapName = os.path.split(gameMapFile)[-1]

    msg_start = "%s %s ..." % (brainName, mapName)
    if (verbose):
        sys.stdout.write(msg_start)

    # transform position and orientation strings into objects

    position = positionStrToValue(positionStr)
    orientation = orientationStrToValue(orientationStr)

    # setup sim

    s = sim.Sim(gameMapFile, brainClass, timeout, delay, follow, position,
                orientation)
    s.run()
    rep = s.getReport()

    if rep['exitCode'] == sim.Sim.EXITCODE_TIMEOUT:
        exitCodeMsg = "timeout!"
    elif rep['exitCode'] == sim.Sim.EXITCODE_MAPMISSMATCH:
        exitCodeMsg = "map missmatch!"
    elif rep['exitCode'] == sim.Sim.EXITCODE_MAPMATCH:
        exitCodeMsg = "success!"
    else:
        exitCodeMsg = "failure (unknown code %d)" % rep['exitCode']

    msg_end = ""
    if not verbose and rep['exitCode'] != sim.Sim.EXITCODE_MAPMATCH:
        msg_end = msg_start

    if verbose or rep['exitCode'] != sim.Sim.EXITCODE_MAPMATCH:
        startPos = s.getStartPosition()
        startOri = s.getStartOrientation()
        msg_end += " %s" % exitCodeMsg
        msg_end += " (start at %s ori. %s)" % (
            startPos, baseBrain.BaseBrain.ORIENTATION_STR[startOri])
        sys.stdout.write(msg_end)

    if verbose or (not verbose
                   and rep['exitCode'] != sim.Sim.EXITCODE_MAPMATCH):
        sys.stdout.write("\n")

    return rep
示例#20
0
文件: region.py 项目: ssm2017/pyosm
    def is_alive(self):
        logging.main_logger.debug("[region] 'is_alive' called")

        import sim
        self.alive = False
        mysim = sim.Sim(self.sim_path, True)
        # check if the sim is responding to simstatus
        check_monitorstats = False
        from helpers import request
        url = 'http://127.0.0.1:' + mysim.port + '/monitorstats/' + self.region_uuid
        logging.main_logger.debug('Url called : %s' % (url))
        req = request(url)
        if req:
            # check if status is 200
            if req.getcode() == 200:
                check_monitorstats = True
                self.alive = True

        return self.alive
示例#21
0
def run_scripts(points):
    """
    Run all scripts that are present in the global list 'SCRIPTS'.
    """
    print("Running scripts ...")

    for script in SCRIPTS:
        print("Running " + script + " ...")

        # Run a single script
        s = sim.Sim(WAF_PATH,
                    NS3_PATH,
                    script,
                    verbose=VERBOSE,
                    nNodes=NODES,
                    debug=DEBUG,
                    pcap=PCAP,
                    energy=ENERGY)
        s.run(points)

        print("Finished running " + script)

    print("Completed running scripts")
示例#22
0
import sim

scene = sim.Sim()
示例#23
0
    # check if there is already a session
    if tmux_server.has_session(sim.name):
        tmux_server.kill_session(target_session=sim.name)

    logging.main_logger.debug("[run_sim] OpenSimulator killed from %s",
                              sim.name)
    return True


if __name__ == '__main__':
    args = sys.argv[1:]
    logging.main_logger.debug("[run_sim] run_sim called with args %s", args)

    # get the sim
    mysim = sim.Sim(args[1], load=True)
    if not (mysim.valid):
        msg = "[run_sim] Simulator is not valid"
        logging.main_logger.warning(msg)
        sys.exit(msg)

    logging.main_logger.debug("[run_sim] User is : %s", os.getuid())
    logging.main_logger.debug("[run_sim] username is : %s", mysim.username)
    logging.main_logger.debug("[run_sim] sim_name is : %s", mysim.name)

    if (args[0] == 'start'):
        start_sim(mysim)
    elif (args[0] == 'stop'):
        stop_sim(mysim)
    elif (args[0] == 'kill'):
        kill_sim(mysim)
示例#24
0
文件: simTest.py 项目: laurakah/clyde
 def testInit_timeOut_isUserSpecified(self):
     timeOut = 8888
     s = sim.Sim(self.gameMapFile, self.brainClass, timeOut)
     self.assertEqual(timeOut, s.getTimeOut())
示例#25
0
import sim
import simutilities

# Set goals/yaws to go to
# GOALS = simutilities.randomgoals(3)
GOALS = [(5, -5, 3), (-4, 4, 6)]

newSim = sim.Sim()
newSim.set_params(goals=GOALS, goal_time=15)
newSim.set_failure_mode(setting='defined', mode=['mf2', 'healthy', 'healthy', 'healthy'])
newSim.see_gui = False
newSim.see_motor_gui = False
newSim.time_scale = 0.5
newSim.use_lstm = False
newSim.ask_save_destination()
newSim.run_sim()
示例#26
0
文件: simTest.py 项目: laurakah/clyde
 def testInit_startOrientation_raisesExceptionWhenValueIsInvalid(self):
     with self.assertRaises(Exception) as ex:
         s1 = sim.Sim(self.gameMapFile,
                      self.brainClass, sim.Sim.DEFAULT_TIMEOUT, 0, False,
                      c.Coordinate(2, 2), 19)
     self.assertEqual("startOrientation is invalid!", ex.exception.message)
示例#27
0
文件: simTest.py 项目: laurakah/clyde
 def testInit_followIsUserSpecified(self):
     s = sim.Sim(self.gameMapFile, self.brainClass, 20, 20, True)
     self.assertEqual(True, s.followIsSet())
示例#28
0
文件: simTest.py 项目: laurakah/clyde
 def testInit_startOrientation_isUserSpecified(self):
     s1 = sim.Sim(self.gameMapFile,
                  self.brainClass, sim.Sim.DEFAULT_TIMEOUT, 0, False,
                  c.Coordinate(2, 2), bb.BaseBrain.ORIENTATION_LEFT)
     self.assertEqual(3, s1.getStartOrientation())
示例#29
0
class Root(controllers.RootController):
    user = srusers.User()
    fw = fw.FwServe()
    autosave = srautosave.Autosave()
    #feed = Feed()
    switchboard = switchboard.Switchboard()
    admin = admin.Admin()
    version = get_version()

    if config.get(
            "simulator.enabled"
    ):  # if simulation is enabled import the simulator controller
        import sim
        sim = sim.Sim()

    @expose()
    def index(self):
        """
        In case the apache rewrite rule isn't paying attention, serve up the
        index file from here.
        """
        loc = os.path.join(os.path.dirname(__file__), "static/index.html")
        return serveFile(loc)

    @expose("json")
    def info(self):
        info = dict(Version=self.version, User=str(srusers.get_curuser()))
        return dict(info=info)

    def get_project_path(self, path):
        """
        Get a project name and filepath from a path
        inputs:
            path - str to a file
        returns:
            tuple containing the project and path
        """
        root, project, file_path = path.split(os.path.sep, 2)
        return project, file_path

    def get_rev_id(self, team, project, revno=-1):
        """
        Get revision ID string from revision number.
        inputs:
            revno - revision number convertable with int().
                    if revno is -1 or not supplied, get latest revision id.
        returns:
            revision id string
        """

        b = open_branch(int(team), project)

        try:
            if revno == -1 or revno == "-1" or revno == "HEAD":  #TODO BZRPORT: stop anything calling "HEAD" string
                rev_id = b.last_revision()
            else:
                rev_id = b.get_rev_id(int(revno))

        except (TypeError):  # TODO BZRPORT: add bzr exception
            print "Getting ID for revno: %s failed, returning latest revision id." % revno
            rev_id = b.last_revision()

        return rev_id

    def get_file_revision(self, tree, fileid):
        """
        Get the id of the revision when the file was last modified.
        inputs: tree - a bzrlib tree of some kind
                fileid - file id of file in tree
        outputs: revid - revision id
        """
        return bzrlib.tree.Tree._file_revision(
            tree, fileid)  # static method for some reason

    @expose()
    @srusers.require(srusers.in_team())
    def checkout(self, team, project, simulator=False):
        """
        This function grabs a set of files and makes a zip available. Should be
        linked to directly.
        inputs:
            team & project - code to retrieve
            simulator - true if code is being delivered to a simulator.
        returns:
            A zip file as a downloadable file with appropriate HTTP headers
            sent.
        """
        b = open_branch(int(team), project)
        rev_tree = b.basis_tree()  # get latest revision tree for branch

        #Avoid using /tmp by writing into a memory based file
        zipData = StringIO.StringIO()
        zip = zipfile.ZipFile(zipData, "w", zipfile.ZIP_DEFLATED)
        #Need to lock_read before reading any file contents
        rev_tree.lock_read()
        try:
            #Get a list of files in the tree
            files = [
                f for f in rev_tree.iter_entries_by_dir()
                if f[1].kind == "file"
            ]
            for filename, file in files:
                #Set external_attr on a ZipInfo to make sure the files are
                #created with the right permissions
                info = zipfile.ZipInfo(filename.encode("ascii"))
                info.external_attr = 0666 << 16L
                #Read the file contents and add to zip
                zip.writestr(info, rev_tree.get_file(file.file_id).read())

            #Need a __init__ in the root of all code exports
            if not "__init__.py" in [f[0].encode("ascii") for f in files]:
                info = zipfile.ZipInfo("__init__.py")
                info.external_attr = 0666 << 16L
                zip.writestr(info, "")

        except:
            return "Error exporting project"
        finally:
            #Always unlock or get GC related errors
            rev_tree.unlock()
        zip.close()
        #Seek back to start of file so read() works later on
        zipData.seek(0)

        if not simulator:
            """
            The zipfile delivered to the robot is the contents of the
            repository as a zip inside another zip that contains firmware.
            """
            #Get a copy of the firmware zip, drop the code zip (in zipData)
            #in it and then put the resulting zip back into zipData
            sysZipData = open(config.get("robot.packagezip")).read()
            sysZipBuffer = StringIO.StringIO(sysZipData)

            sysZip = zipfile.ZipFile(sysZipBuffer, "a")
            info = zipfile.ZipInfo(ZIPNAME)
            info.external_attr = 0666 << 16L
            sysZip.writestr(info, zipData.read())
            sysZip.close()

            sysZipBuffer.seek(0)
            zipData = StringIO.StringIO(sysZipBuffer.read())

        #Set up headers for correctly serving a zipfile
        cherrypy.response.headers['Content-Type'] = \
                "application/x-download"
        cherrypy.response.headers['Content-Disposition'] = \
                'attachment; filename="' + ZIPNAME + '"'

        #Return the data
        return zipData.read()

    @expose("json")
    @srusers.require(srusers.in_team())
    def filesrc(self, team, file=None, revision=None):
        """
        Returns the contents of the file.
        """

        file_path = file  #save for later
        project, file = self.get_project_path(file_path)
        curtime = time.time()
        b = open_branch(int(team), project)

        #TODO: Need to security check here! No ../../ or /etc/passwd nautiness trac#208

        autosaved_code = self.autosave.getfilesrc(team, file_path, 1)

        if revision == None or revision == "HEAD":
            revno, revid = b.last_revision_info()
        else:
            revno = int(revision)
            revid = b.get_rev_id(revno)

        if file != None and file != "":  #TODO BZRPORT: URL checking
            #Load file from bzr
            # TODO BZRPORT: mime checking. Bzr doesn't have a mime property so the file will need to be checked with python
            try:
                branch_tree = b.repository.revision_tree(revid)
                file_id = branch_tree.path2id(file)
                b.lock_read()
                code = branch_tree.get_file_text(file_id)
                file_revid = self.get_file_revision(
                    branch_tree,
                    file_id)  # get revision the file was last modified
                file_revno = b.revision_id_to_revno(file_revid)
            except:
                code = "Error loading file '%s' at revision %s." % (file,
                                                                    revision)
                file_revno = 0
            # always unlock:
            finally:
                b.unlock()

        else:
            code = "Error loading file: No filename was supplied by the IDE.  Contact an SR admin!"
            revision = 0

        return dict(curtime=curtime,
                    code=code,
                    autosaved_code=autosaved_code,
                    file_rev=str(file_revno),
                    revision=revno,
                    path=file_path,
                    name=os.path.basename(file))

    @expose("json")
    @srusers.require(srusers.in_team())
    def gethistory(self, team, file, user=None, offset=0):
        """
        This function retrieves the bzr history for the given file(s)
        to restrict logs to particular user, supply a user parameter
        a maximum of 10 results are sent to the browser, if there are more than 10
        results available, overflow > 0.
        supply an offset to view older results: 0<offset < overflow; offset = 0 is the most recent logs
        """
        if file[:9] == 'New File ':
            return dict(path=file, history=[])

        file_path = file  #save for later
        project, file = self.get_project_path(file_path)
        b = open_branch(int(team), project)
        revisions = [
            b.repository.get_revision(r) for r in b.revision_history()
        ]

        #Get a list of authors
        authors = list(set([r.committer for r in revisions]))

        #If a user is passed, only show revisions committed by that user
        if user != None:
            revisions = [r for r in revisions if r.committer == user]

        #Only show revisions where the delta touches file
        fileid = b.basis_tree().path2id(file)
        if fileid == None:
            #File not found
            return dict()

        def revisionTouchesFile(revision):
            """
            Return true if the revision changed a the file referred to in fileid.
            """
            delta = b.get_revision_delta(
                b.revision_id_to_revno(revision.revision_id))
            return delta.touches_file_id(fileid)

        revisions = filter(revisionTouchesFile, revisions)

        #Calculate offsets for paging
        try:
            offset = int(offset)
        except ValueError:
            #Someone passed a string
            return dict()
        start = offset * 10
        end = start + 10
        maxval = len(revisions)
        if maxval % 10 > 0:
            overflow = maxval / 10 + 1
        else:
            overflow = maxval / 10

        revisions = revisions[start:end]
        revisions.reverse()

        return dict(path=file_path,
                    overflow=overflow,
                    offset=offset,
                    authors=authors,
                    history=[{
                        "author":
                        r.committer,
                        "date":
                        time.strftime("%H:%M:%S %d/%m/%Y",
                                      time.localtime(r.timestamp)),
                        "message":
                        r.message,
                        "rev":
                        b.revision_id_to_revno(r.revision_id)
                    } for r in revisions])

    @expose("json")
    @srusers.require(srusers.in_team())
    def polldata(self, team, files="", logrev=None):
        """Returns poll data:
            inputs: files - comma seperated list of files the client needs info
            on
            returns (json): A dictionary with an entry for each file (path is
            the key). Each value is a dictionary with information. The only key
            is revision, with a value of an integer of the current revision
            number in the repo"""
        pass  #TODO BZRPORT: Implement!

        #Default data
        r = {}
        l = {}
        client = Client(int(team))

        if files != "":
            files = files.split(",")
            rev = 0
            for file in files:
                r[file] = {}
                try:
                    info = client.info2(client.REPO + file)[0][1]
                    r[file]["rev"] = info["last_changed_rev"].number
                except pysvn.ClientError:
                    pass

        if logrev != None:
            try:
                newlogs = client.log(client.REPO,
                                     discover_changed_paths=True,
                                     revision_end=pysvn.Revision(
                                         pysvn.opt_revision_kind.number,
                                         int(logrev) + 1))

                l =[{"author":x["author"], \
                        "date":time.strftime("%H:%M:%S %d/%m/%Y", \
                        time.localtime(x["date"])), \
                        "message":x["message"], "rev":x["revision"].number,
                        "changed_paths":[(c.action, c.path) for c in \
                            x.changed_paths]} for x in newlogs]
            except pysvn.ClientError:
                #No commits recently, no data to return
                pass

        return dict(files=r, log=l)

    @expose("json")
    @srusers.require(srusers.in_team())
    def pollchanges(self, team, project, rev, date=0):
        """
        Used to determine if certain facets need updating.
        Currently this is only for the filelist, to remove the need to send the entire filelist just to see if its changed
        """
        b = open_branch(int(team), project)
        head_rev_id = self.get_rev_id(team, project, 'HEAD')
        target_rev_id = self.get_rev_id(team, project, rev)

        filelist = True
        if head_rev_id == target_rev_id:
            filelist = False

        return dict(filelist=filelist)

    @expose("json")
    @srusers.require(srusers.in_team())
    def delete(self, team, project, files, kind='SVN'):
        """
        Delete files from the repository, and prune empty directories.
        inputs: files - comma seperated list of paths
                kind - one of 'SVN' or 'AUTOSAVES'
        returns (json): Message - a message to show the user
        """

        if files != "":
            files = files.split(",")
            wt = WorkingTree(int(team), project)

            message = "Files deleted successfully: " + project + " >\n" + "\n".join(
                files)

            for f in files:
                self.autosave.delete(team, '/' + project + '/' + f)

            if kind == 'AUTOSAVES':
                return dict(Message="AutoSaves deleted successfully: \n" +
                            "\n".join(files))

            wt.remove(files)

            # find out current user
            ide_user = str(srusers.get_curuser())

            revproperties = {"authors": ide_user}
            wt.commit('Remove files: ' + ', '.join(files),
                      revprops=revproperties)
            wt.destroy()

            return dict(Message=message)

    @expose("json")
    @srusers.require(srusers.in_team())
    def savefile(self, team, filepath, rev, message, code):
        """
        Create/update contents of a file and attempt to commit.
        If file has been updated since submitted text was checked out,
            call update_merge to attempt to merge the changes.
        If file has not been updated since client checked it out,
            call commit_file_simple to commit the new version.

        inputs: path - path of file relative to project root.
                rev - revision of file when it was checked out by client.
        """

        project, filepath = self.get_project_path(filepath)

        projWrite = ProjectWrite(team, project, revno=rev)

        projWrite.update_file_contents(filepath, code)

        reloadfiles = "True"  # TODO: determine whether or not file list needs refreshing

        try:
            newrevno, newrevid = projWrite.commit(message)
            success = "True"
        except bzrlib.errors.OutOfDateTree:
            # a commit has occurred since code was opened.
            # A merge will need to take place
            code, newrevno, newrevid = projWrite.merge(filepath)
            if len(projWrite.conflicts) == 0:
                # TODO: when committing a merged transform preview affecting more than one file,
                #       the text changes do not commit despite the merge succeeding and returning correct text.
                #       solution for now is to open a new transform preview and pump the new code into it.
                pw2 = ProjectWrite(team, project)
                pw2.update_file_contents(filepath, code)
                newrevno, newrevid = pw2.commit(message)
                success = "AutoMerge"
                pw2.destroy()
            else:
                return dict(new_revision=newrevno,
                            code=code,
                            success="Merge",
                            file=filepath,
                            reloadfiles=reloadfiles)
        finally:
            projWrite.destroy()

        return dict(new_revision=str(newrevno),
                    code=code,
                    success=success,
                    file=filepath,
                    reloadfiles=reloadfiles)

    @expose("json")
    @srusers.require(srusers.in_team())
    def filelist(self, team, project, rootpath="/", rev=-1, date=0):
        """
        Returns a directory tree of the current repository.
        inputs: project - the bzr branch
                rootpath - to return file from a particular directory within the branch (recursive)
        returns: A tree as a list of files/directory objects:
            { tree : [{path : filepath
                       kind : FOLDER or FILE
                       children : [list as above]
                       name : name of file}, ...]}
        """

        b = open_branch(int(team), project)

        target_rev_id = self.get_rev_id(team, project, rev)
        self.user.set_setting('project.last', project)

        try:
            rev_tree = b.repository.revision_tree(target_rev_id)
        except:
            return {"error": "Error getting revision tree"}

        # Get id of root folder from which to list files. if it is not found it will return None
        rootid = rev_tree.path2id(rootpath)

        try:
            rev_tree.lock_read()
            # Get generator object containing file information from base rootid. If rootid=None, will return from root.
            files = rev_tree.inventory.iter_entries(rootid)
        except:  # TODO BZRPORT: Proper error handling
            return {"error": "Error getting file list"}
        # Always unlock tree:
        finally:
            rev_tree.unlock()

        #grab the autosave listings
        autosave_data = self.autosave.getfilesrc(team,
                                                 '/' + project + rootpath)

        def branch_recurse(project, path, entry, files, given_parent_id):
            """
            Travels recursively through a generator object provided by revision_tree.inventory.iter_items.
            Iter_items returns child items immediately after their parents, so by checking the parent_id field of the item with the actual id of the directory item that called it, we can check if we are still within that directory and therefore need to add the item as a child.
            This function will return a list of all children of a particular branch, along with the next items for analysis.
            Whenever it encounters a directory it will call itself to find the children.
            inputs: path - path of item to be analysed first
                    entry - InventoryEntry-derived object of item to be analysed first
                    files - generator object created by iter_items
                    given_parent_id - id (string) of calling directory
            returns: entry_list - list of children. if given_parent_id does not match entry.parent_id, this will be an empty list.
                     path - path of item that has not yet been added to the tree
                     entry - the entry object that has not yet been added to the tree.
                             if given_parent_id did not match entry.parent_id, then path and entry returned will be the same as path and entry called.
            """

            entry_list = []

            while entry.parent_id == given_parent_id:  # is a child of parent

                if entry.kind == "directory":
                    try:
                        next_path, next_entry = files.next()
                        children_list, next_path, next_entry = branch_recurse(
                            project, next_path, next_entry, files,
                            entry.file_id)
                    except StopIteration:  # No more files to iterate through after this one
                        next_entry = None  # break after adding this entry
                        children_list = [
                        ]  # no more items, so there can't be any children

                    entry_list.append({
                        "name": entry.name,
                        "path": project + path,
                        "kind": "FOLDER",
                        "autosave": 0,  # No autosave data for directories
                        "rev":
                        "-1",  #TODO BZRPORT: what's this show/for? yes, i know revision, i mean, current, or when it was created?
                        "children": children_list
                    })

                    if next_entry is None:
                        break  # there are no more iterations so break
                    else:
                        path = next_path
                        entry = next_entry  # now we'll use the returned entry

                else:
                    if project + path in autosave_data:
                        autosave_info = autosave_data[project + path]
                    else:
                        autosave_info = 0
                    entry_list.append({
                        "name": entry.name,
                        "path": project + path,
                        "kind": "FILE",
                        "autosave": autosave_info,
                        "rev":
                        "-1",  #TODO BZRPORT: what's this show/for? yes, i know revision, i mean, current, or when it was created?
                        "children": []
                    })

                    try:
                        path, entry = files.next()  # grab next entry
                    except StopIteration:  # No more files to iterate through
                        break

            return entry_list, path, entry

        # Determine tree_root string to pass to recursing function as a parent id
        if rootid == None:
            tree_root = "TREE_ROOT"
        else:
            tree_root = rootid

        try:
            first_path, first_entry = files.next()  # grab next entry
        except StopIteration:  # StopIteration caught on first pass: project tree must be empty
            return dict(tree=[])

        tree, last_path, last_entry = branch_recurse('/' + project + '/',
                                                     first_path, first_entry,
                                                     files, tree_root)

        return dict(tree=tree)

    #create a new directory
    @expose("json")
    @srusers.require(srusers.in_team())
    def newdir(self, team, path, msg):
        project, dirpath = self.get_project_path(path)
        projWrite = ProjectWrite(team, project)

        try:
            projWrite.new_directory(dirpath)
        except:  # TODO BZRPORT: replace with bzr error
            return dict( success=0, newdir = path,\
                        feedback="Error creating directory: " + path)

#TODO: try:
        revno, revid = projWrite.commit(msg)

        return dict( success=1, newdir = path,\
                feedback="Directory successfully created")

#        else: # directory wasn't created because it already existed
#            return dict( success=0, newdir = path,\
#                    feedback="Directory " + path + " already exists")

    @expose("json")
    @srusers.require(srusers.in_team())
    def projlist(self, team):
        """Returns a list of projects"""

        try:
            r = open_repo(int(team))
        except:
            #No repository present
            return dict(projects=[])

        self.user.set_setting('team.last', team)

        projects = []

        branches = r.find_branches()

        for branch in branches:
            projects.append(branch.nick)

        return dict(projects=projects)

    @expose("json")
    @srusers.require(srusers.in_team())
    def createproj(self, name, team):
        """Creates new project directory"""

        r = open_repo(int(team))

        if name.find(".") != -1:
            """No ../../ nastyness"""
            return nil

        url = srusers.get_svnrepo(team) + "/" + name

        r.bzrdir.create_branch_convenience(base=url, force_new_tree=False)

        return dict()

    @expose("json")
    @srusers.require(srusers.in_team())
    def revert(self, team, files, torev, message):

        file_list = files.split(',')
        if len(file_list) == 0:
            return dict(Message='Revert failed - no files specified', status=1)

        project, file = self.get_project_path(file_list[0])
        rev_spec = bzrlib.revisionspec.RevisionSpec.from_string(torev)
        file_list = [self.get_project_path(f)[1] for f in file_list]

        wt = WorkingTree(team, project)
        rev_tree = rev_spec.as_tree(wt.branch)

        wt.revert(file_list, rev_tree)

        # find out current user
        ide_user = str(srusers.get_curuser())

        revproperties = {"authors": ide_user}

        wt.commit(message, revprops=revproperties)
        newrev, id = wt.branch.last_revision_info()
        wt.destroy()

        return dict(new_revision=newrev,
                    code="",
                    success="Success !!!",
                    status=0)

        #from undelete
        return dict(fail=fail, success=','.join(success), status=status)

    @expose("json")
    @srusers.require(srusers.in_team())
    def calendar(self, mnth, yr, file, team):
        #returns data for calendar function

        if file == '/':  #no project selected
            return dict(path=file, history=[])

        month = int(mnth) + 1
        year = int(yr)
        b = open_branch(team, file)

        try:
            log = b.repository.get_revisions(b.revision_history())
        except:
            logging.debug("Log failed for %s" % file)
            print "failed to retrieve log"
            return dict(path=file, history=[])

        if len(log) == 0:  #if there's nothing there
            return dict(path=file, history=[])

        #get a list of users based on log authors
        start = datetime.datetime(year, month, 1, 0, 0, 0)

        if (month >= 12):
            end = datetime.datetime(year + 1, 1, 1, 0, 0,
                                    0)  #watchout for rollover
        else:
            end = datetime.datetime(year, month + 1, 1, 0, 0, 0)

        result = []

        for y in log:
            now = datetime.datetime(2000, 1, 1)
            #create a dummy datetime
            now = now.fromtimestamp(y.timestamp)
            if (start <= now < end):
                result.append(y)

        result.reverse()

        return dict(  path=file,\
                      history=[{"author":x.get_apparent_author(), \
                      "date":time.strftime("%Y/%m/%d/%H/%M/%S", \
                      time.localtime(x.timestamp)), \
                      "message":x.message, "rev":b.revision_id_to_revno(x.revision_id)} \
                      for x in result])

    @expose("json")
    @srusers.require(srusers.in_team())
    def move(self, team, src, dest, msg=""):
        #   the source and destination arguments may be directories or files
        #   directories rendered empty as a result of the move are automatically 'pruned'
        #   returns status = 0 on success

        src_proj, src_path = self.get_project_path(src)
        dest_proj, dest_path = self.get_project_path(dest)
        if src_proj != dest_proj:
            return dict(new_revision="0",
                        status="1",
                        message="Source and destination projects must match")

        wt = WorkingTree(int(team), src_proj)

        if not wt.has_filename(src_path):
            return dict(new_revision="0",
                        status="1",
                        message="Source file/folder doesn't exist: " + src)

        if not wt.has_filename(os.path.dirname(dest_path)):
            return dict(new_revision="0",
                        status="1",
                        message="Destination folder doesn't exist: " +
                        os.path.dirname(dest))

        if wt.has_filename(dest_path):
            return dict(new_revision="0",
                        status="1",
                        message="Destination already exists: " + dest)

        wt.rename_one(src_path, dest_path)

        # find out current user
        ide_user = str(srusers.get_curuser())

        revproperties = {"authors": ide_user}

        wt.commit('Move ' + src_path + ' to ' + dest_path,
                  revprops=revproperties)
        wt.destroy()

        self.autosave.move(team, src, dest)

        return dict(new_revision="0",
                    status="0",
                    message='Sucessfully moved file ' + src + ' to ' + dest)

    @expose("json")
    @srusers.require(srusers.in_team())
    def copyproj(self, team, src, dest):
        # Create a temporary directory
        tmpdir = tempfile.mkdtemp()
        #open the branch and sprout a new copy, in the temp dir
        b = open_branch(team, src)
        self.createproj(dest, team)
        nb = open_branch(team, dest)
        b.push(nb)
        return dict(status=0)

    @expose("json")
    @srusers.require(srusers.in_team())
    def copy(self, team, src="", dest="", msg="SVN Copy", rev="0"):
        return self.cp(team, src, dest, msg, rev)

    @srusers.require(srusers.in_team())
    def cp(self, team, src="", dest="", msg="Copy", rev="0"):

        project, src = self.get_project_path(src)
        dest_project, dest = self.get_project_path(dest)
        if dest_project != project:
            return dict(
                new_revision="0",
                status="1",
                message=
                "Copy Failed: Source and destination projects must match")

        if rev == "0":
            rev = None

        projWrite = ProjectWrite(team, project, revno=rev)

        if src == "":
            return dict(new_revision="0",
                        status="1",
                        msg="No Source file/folder specified")
        if dest == "":
            return dict(new_revision="0",
                        status="1",
                        msg="No Destination file/folder specified")

        try:
            projWrite.copy(src, dest)
            new_revno, new_rev_id = projWrite.commit(msg)

        except Exception, e:
            return dict(new_revision="0",
                        status="1",
                        message="Copy Failed: " + str(e))

        return dict(new_revision=str(new_revno),
                    status="0",
                    message="copy successful")
示例#30
0
    def run(self):
        g.init()
        if not self.journal: utils.load()
        self.sim=sim.Sim()
        load_save.retrieve()
        self.buttons_setup()
        if g.saved_n==0: buttons.off('cyan')
        self.slider=slider.Slider(g.sx(23.4),g.sy(20.2),5,utils.GREEN)
        if self.canvas<>None: self.canvas.grab_focus()
        ctrl=False
        pygame.key.set_repeat(600,120); key_ms=pygame.time.get_ticks()
        going=True
        while going:
            if self.journal:
                # Pump GTK messages.
                while gtk.events_pending(): gtk.main_iteration()

            # Pump PyGame messages.
            for event in pygame.event.get():
                if event.type==pygame.QUIT:
                    if not self.journal: utils.save()
                    going=False
                elif event.type == pygame.MOUSEMOTION:
                    g.pos=event.pos
                    g.redraw=True
                    if self.canvas<>None: self.canvas.grab_focus()
                elif event.type == pygame.MOUSEBUTTONDOWN:
                    g.redraw=True
                    if g.help_on: g.help_on=False 
                    elif event.button==1:
                        if self.do_click():
                            pass
                        elif self.slider.mouse():
                            pass # level changed
                        else:
                            bu=buttons.check()
                            if bu!='': self.do_button(bu); self.flush_queue()
                    elif event.button==3:
                        self.right_click()
                elif event.type == pygame.KEYDOWN:
                    # throttle keyboard repeat
                    if pygame.time.get_ticks()-key_ms>110:
                        key_ms=pygame.time.get_ticks()
                        if ctrl:
                            if event.key==pygame.K_q:
                                if not self.journal: utils.save()
                                going=False; break
                            else:
                                ctrl=False
                        if event.key in (pygame.K_LCTRL,pygame.K_RCTRL):
                            ctrl=True; break
                        self.do_key(event.key); g.redraw=True
                        self.flush_queue()
                elif event.type == pygame.KEYUP:
                    ctrl=False
            if not going: break
            self.update()
            if g.redraw:
                self.display()
                if g.version_display: utils.version_display()
                g.screen.blit(g.pointer,g.pos)
                pygame.display.flip()
                g.redraw=False
            g.clock.tick(40)