Beispiel #1
0
 def handle_login(self, request):
     "Log this client in."
     request = request.cast_to(LoginMessage)
     self.account = Account.create(request['username'], request['password'])
     if self.auth.verify(self.account):
         # TODO: verify machine & type values
         self.machine, self.type = request['machine'], request['type']
         self.namespace = request['username']
         print self.address_str(), "=>", self.id
         if not self.tracker.assign_namespace(self):
             yield gen.Task(
                 self.stream.write,
                 ResponseMessage.error(
                     request.id,
                     reason="Machine of name %r already connected." %
                     self.machine,
                     code=ErrorCodes.MACHINE_CONFLICT))
             self.close()
             raise StopIteration
         msg = ResponseMessage.success(request.id)
         yield gen.Task(self.stream.write, msg)
         print self.id, '<-', msg
         self.process_request()
     else:
         self.fail(request.id,
                   reason='Invalid username or password',
                   code=ErrorCodes.BAD_AUTH,
                   close_stream=False)
Beispiel #2
0
 def do_clients(self, request):
     machines = {}
     for name, client in self.tracker.get_namespace(self.namespace).items():
         machines[name] = client.type
     msg = ResponseMessage.success(request.id, clients=machines)
     print self.id, '<-', msg
     yield gen.Task(self.stream.write, msg)
Beispiel #3
0
 def do_clients(self, request):
     machines = {}
     for name, client in self.tracker.get_namespace(self.namespace).items():
         machines[name] = client.type
     msg = ResponseMessage.success(request.id, clients=machines)
     print self.id, '<-', msg
     yield gen.Task(self.stream.write, msg)
Beispiel #4
0
    def do_download(self, mclient, request):
        if self._invalid_project(mclient, request):
            raise StopIteration

        if not self.builder.has_file(request['project'], request['filepath']):
            yield gen.Task(mclient.write_response, ResponseMessage.error(
                request.id,
                reason="Invalid filepath",
                code=ErrorCodes.MISSING_FILEPATH,
            ))
            raise StopIteration
        contents = self.builder.read_file(request['project'], request['filepath'])
        if contents is None:
            yield gen.Task(mclient.write_response, ResponseMessage.error(
                request.id,
                reason="Invalid filepath",
                code=ErrorCodes.MISSING_FILEPATH,
            ))
            raise StopIteration
        yield gen.Task(mclient.write_response, ResponseMessage.success(
            request.id,
            project=request['project'],
            filepath=request['filepath'],
            contents=contents,
        ))
Beispiel #5
0
 def handle_login(self, request):
     "Log this client in."
     request = request.cast_to(LoginMessage)
     self.account = Account.create(request['username'], request['password'])
     if self.auth.verify(self.account):
         # TODO: verify machine & type values
         self.machine, self.type = request['machine'], request['type']
         self.namespace = request['username']
         print self.address_str(), "=>", self.id
         if not self.tracker.assign_namespace(self):
             yield gen.Task(self.stream.write, ResponseMessage.error(
                 request.id,
                 reason="Machine of name %r already connected." % self.machine,
                 code=ErrorCodes.MACHINE_CONFLICT
             ))
             self.close()
             raise StopIteration
         msg = ResponseMessage.success(request.id)
         yield gen.Task(self.stream.write, msg)
         print self.id, '<-', msg
         self.process_request()
     else:
         self.fail(request.id,
             reason='Invalid username or password',
             code=ErrorCodes.BAD_AUTH,
             close_stream=False)
Beispiel #6
0
    def do_commit(self, mclient, request):
        if self._invalid_project(mclient, request):
            raise StopIteration

        self.builder.commit(request['project'], request['message'])
        yield gen.Task(mclient.write_response, ResponseMessage.success(
            request.id,
        ))
Beispiel #7
0
    def do_stage_file(self, mclient, request):
        if self._invalid_project(mclient, request):
            raise StopIteration

        self.builder.add_to_index(request['project'], request['filepath'])
        yield gen.Task(mclient.write_response, ResponseMessage.success(
            request.id,
        ))
Beispiel #8
0
    def do_diff_stats(self, mclient, request):
        if self._invalid_project(mclient, request):
            raise StopIteration

        yield gen.Task(
            mclient.write_response,
            ResponseMessage.success(request.id, stats=self.builder.diff_stats(request["project"])),
        )
Beispiel #9
0
    def do_diff_stats(self, mclient, request):
        if self._invalid_project(mclient, request):
            raise StopIteration

        yield gen.Task(mclient.write_response, ResponseMessage.success(
            request.id,
            stats=self.builder.diff_stats(request['project'])
        ))
Beispiel #10
0
    def do_branches(self, mclient, request):
        if self._invalid_project(mclient, request):
            raise StopIteration

        yield gen.Task(mclient.write_response, ResponseMessage.success(
            request.id,
            branches=self.builder.get_branches(request['project']),
            head=self.builder.get_current_branch(request['project']),
        ))
Beispiel #11
0
 def handle_register(self, request):
     "Register a new user account fo this client."
     error = self.auth.create(request['username'], request['password'])
     if not error:
         msg = ResponseMessage.success(request.id)
         print self.id, '<-', msg
         yield gen.Task(self.stream.write, msg)
     else:
         msg = ResponseMessage.error(request.id, **error)
         print self.id, '<-', msg
         yield gen.Task(self.stream.write, msg)
Beispiel #12
0
 def handle_register(self, request):
     "Register a new user account fo this client."
     error = self.auth.create(request['username'], request['password'])
     if not error:
         msg = ResponseMessage.success(request.id)
         print self.id, '<-', msg
         yield gen.Task(self.stream.write, msg)
     else:
         msg = ResponseMessage.error(request.id, **error)
         print self.id, '<-', msg
         yield gen.Task(self.stream.write, msg)
Beispiel #13
0
    def do_branches(self, mclient, request):
        if self._invalid_project(mclient, request):
            raise StopIteration

        yield gen.Task(
            mclient.write_response,
            ResponseMessage.success(
                request.id,
                branches=self.builder.get_branches(request["project"]),
                head=self.builder.get_current_branch(request["project"]),
            ),
        )
Beispiel #14
0
    def do_perform(self, mclient, request):
        if self._invalid_project(mclient, request):
            raise StopIteration
        project = self.builder.projects[request['project']]

        if project.is_busy:
            yield gen.Task(mclient.write_response, ResponseMessage.error(
                request.id,
                reason="Project is busy. Use cancel to stop existing command.",
                code=ErrorCodes.ACTION_CONFLICT,
            ))
            raise StopIteration

        if request['action'] not in project.actions:
            yield gen.Task(mclient.write_response, ResponseMessage.error(
                request.id,
                reason="Invalid Action",
                code=ErrorCodes.MISSING_ACTION,
            ))
            raise StopIteration

        self.process_query = project.perform_action(request['action'])

        target = request.sender
        yield gen.Task(mclient.write_response, ResponseMessage.success(request.id))

        # TODO: handle stdin
        while not self.process_query.has_terminated or self.process_query.can_read:
            if self.process_query.can_read:
                yield gen.Task(mclient.send, target, StreamNotification(
                    project=project.name,
                    contents=self.process_query.read(),
                ))
            else:
               # wait, to allow processing of other IO
               yield gen.Task(self.wait)

        yield gen.Task(mclient.send, target, StreamEOFNotification(
            project=project.name,
        ))
        yield gen.Task(mclient.send, target, ReturnCodeNotification(
            project=project.name,
            code=self.process_query.return_code,
        ))
        self.process_query = None
Beispiel #15
0
    def do_cancel(self, mclient, request):
        if self._invalid_project(mclient, request):
            raise StopIteration

        if not self.process_query:
            yield gen.Task(mclient.write_response, ResponseMessage.error(
                request.id,
                reason="No process running for %r." % request['project']
            ))
            raise StopIteration

        self.process_query.terminate()
        yield gen.Task(mclient.write_response, ResponseMessage.success(request.id))

        # really kill it if we need to
        yield gen.Task(mclient.wait_for, seconds=1)
        self.process_query.kill()
        self.process_query = None
Beispiel #16
0
    def do_cancel(self, mclient, request):
        if self._invalid_project(mclient, request):
            raise StopIteration

        if not self.process_query:
            yield gen.Task(
                mclient.write_response,
                ResponseMessage.error(request.id, reason="No process running for %r." % request["project"]),
            )
            raise StopIteration

        self.process_query.terminate()
        yield gen.Task(mclient.write_response, ResponseMessage.success(request.id))

        # really kill it if we need to
        yield gen.Task(mclient.wait_for, seconds=1)
        self.process_query.kill()
        self.process_query = None
Beispiel #17
0
    def do_perform(self, mclient, request):
        if self._invalid_project(mclient, request):
            raise StopIteration
        project = self.builder.projects[request["project"]]

        if project.is_busy:
            yield gen.Task(
                mclient.write_response,
                ResponseMessage.error(
                    request.id,
                    reason="Project is busy. Use cancel to stop existing command.",
                    code=ErrorCodes.ACTION_CONFLICT,
                ),
            )
            raise StopIteration

        if request["action"] not in project.actions:
            yield gen.Task(
                mclient.write_response,
                ResponseMessage.error(request.id, reason="Invalid Action", code=ErrorCodes.MISSING_ACTION),
            )
            raise StopIteration

        self.process_query = project.perform_action(request["action"])

        target = request.sender
        yield gen.Task(mclient.write_response, ResponseMessage.success(request.id))

        # TODO: handle stdin
        while not self.process_query.has_terminated or self.process_query.can_read:
            if self.process_query.can_read:
                yield gen.Task(
                    mclient.send, target, StreamNotification(project=project.name, contents=self.process_query.read())
                )
            else:
                # wait, to allow processing of other IO
                yield gen.Task(self.wait)

        yield gen.Task(mclient.send, target, StreamEOFNotification(project=project.name))
        yield gen.Task(
            mclient.send, target, ReturnCodeNotification(project=project.name, code=self.process_query.return_code)
        )
        self.process_query = None
Beispiel #18
0
    def do_files(self, mclient, request):
        if self._invalid_project(mclient, request):
            raise StopIteration

        files = self.builder.get_files(request["project"], request["branch"])

        # files is None when switching branches fails.
        # TODO: perhaps we should stash & apply instead?
        if files is None:
            yield gen.Task(
                mclient.write_response,
                ResponseMessage.error(request.id, reason="Failed to checkout files", code=ErrorCodes.INTERNAL_ERROR),
            )
        else:
            yield gen.Task(
                mclient.write_response,
                ResponseMessage.success(
                    request.id, files=files, branch=self.builder.get_current_branch(request["project"])
                ),
            )
Beispiel #19
0
    def do_upload(self, mclient, request):
        if self._invalid_project(mclient, request):
            raise StopIteration

        if request['contents'] is None:
            yield gen.Task(mclient.write_response, ResponseMessage.error(
                request.id,
                reason="Bad Request",
                code=ErrorCodes.BAD_REQUEST,
            ))
            raise StopIteration
        # no op for now
        try:
            self.builder.write_file(request['project'], request['filepath'], request['contents'] or '')
        except IOError:
            yield gen.Task(mclient.write_response, ResponseMessage.error(
                request.id, reason="Failed to write", code=ErrorCodes.INTERNAL_ERROR))
            raise StopIteration

        yield gen.Task(mclient.write_response, ResponseMessage.success(request.id))
Beispiel #20
0
    def do_upload(self, mclient, request):
        if self._invalid_project(mclient, request):
            raise StopIteration

        if request["contents"] is None:
            yield gen.Task(
                mclient.write_response,
                ResponseMessage.error(request.id, reason="Bad Request", code=ErrorCodes.BAD_REQUEST),
            )
            raise StopIteration
        # no op for now
        try:
            self.builder.write_file(request["project"], request["filepath"], request["contents"] or "")
        except IOError:
            yield gen.Task(
                mclient.write_response,
                ResponseMessage.error(request.id, reason="Failed to write", code=ErrorCodes.INTERNAL_ERROR),
            )
            raise StopIteration

        yield gen.Task(mclient.write_response, ResponseMessage.success(request.id))
Beispiel #21
0
    def do_input(self, mclient, request):
        if self._invalid_project(mclient, request):
            raise StopIteration

        if request["contents"] is None:
            yield gen.Task(
                mclient.write_response,
                ResponseMessage.error(request.id, reason="Bad Request", code=ErrorCodes.BAD_REQUEST),
            )
            raise StopIteration

        if not self.process_query:
            yield gen.Task(
                mclient.write_response,
                ResponseMessage.error(
                    request.id, reason="No process running for %r." % request["project"], code=ErrorCodes.NO_ACTIVITY
                ),
            )
            raise StopIteration

        self.process_query.write(str(request["contents"]))
        yield gen.Task(mclient.write_response, ResponseMessage.success(request.id))
Beispiel #22
0
    def do_input(self, mclient, request):
        if self._invalid_project(mclient, request):
            raise StopIteration

        if request['contents'] is None:
            yield gen.Task(mclient.write_response, ResponseMessage.error(
                request.id,
                reason="Bad Request",
                code=ErrorCodes.BAD_REQUEST,
            ))
            raise StopIteration

        if not self.process_query:
            yield gen.Task(mclient.write_response, ResponseMessage.error(
                request.id,
                reason="No process running for %r." % request['project'],
                code=ErrorCodes.NO_ACTIVITY,
            ))
            raise StopIteration

        self.process_query.write(str(request['contents']))
        yield gen.Task(mclient.write_response, ResponseMessage.success(request.id))
Beispiel #23
0
    def do_files(self, mclient, request):
        if self._invalid_project(mclient, request):
            raise StopIteration

        files = self.builder.get_files(
            request['project'],
            request['branch']
        )

        # files is None when switching branches fails.
        # TODO: perhaps we should stash & apply instead?
        if files is None:
            yield gen.Task(mclient.write_response, ResponseMessage.error(
                request.id,
                reason="Failed to checkout files",
                code=ErrorCodes.INTERNAL_ERROR,
            ))
        else:
            yield gen.Task(mclient.write_response, ResponseMessage.success(
                request.id,
                files=files,
                branch=self.builder.get_current_branch(request['project']),
            ))
Beispiel #24
0
    def do_download(self, mclient, request):
        if self._invalid_project(mclient, request):
            raise StopIteration

        if not self.builder.has_file(request["project"], request["filepath"]):
            yield gen.Task(
                mclient.write_response,
                ResponseMessage.error(request.id, reason="Invalid filepath", code=ErrorCodes.MISSING_FILEPATH),
            )
            raise StopIteration
        contents = self.builder.read_file(request["project"], request["filepath"])
        if contents is None:
            yield gen.Task(
                mclient.write_response,
                ResponseMessage.error(request.id, reason="Invalid filepath", code=ErrorCodes.MISSING_FILEPATH),
            )
            raise StopIteration
        yield gen.Task(
            mclient.write_response,
            ResponseMessage.success(
                request.id, project=request["project"], filepath=request["filepath"], contents=contents
            ),
        )
Beispiel #25
0
 def do_projects(self, mclient, request):
     yield gen.Task(mclient.write_response,
             ResponseMessage.success(request.id,
                 projects=self.builder.project_names))
Beispiel #26
0
    def do_commit(self, mclient, request):
        if self._invalid_project(mclient, request):
            raise StopIteration

        self.builder.commit(request["project"], request["message"])
        yield gen.Task(mclient.write_response, ResponseMessage.success(request.id))
Beispiel #27
0
    def do_stage_file(self, mclient, request):
        if self._invalid_project(mclient, request):
            raise StopIteration

        self.builder.add_to_index(request["project"], request["filepath"])
        yield gen.Task(mclient.write_response, ResponseMessage.success(request.id))
Beispiel #28
0
 def do_stats(self, mclient, request):
     yield gen.Task(mclient.write_response, ResponseMessage.success(request.id, activity=self.builder.activities))
Beispiel #29
0
 def do_stats(self, mclient, request):
     yield gen.Task(mclient.write_response, ResponseMessage.success(
         request.id,
         activity=self.builder.activities,
     ))
Beispiel #30
0
 def do_projects(self, mclient, request):
     yield gen.Task(mclient.write_response, ResponseMessage.success(request.id, projects=self.builder.project_names))