コード例 #1
0
ファイル: server.py プロジェクト: rsgalloway/grit
 def error_response(self, error, environ, start_response):
     headerbase = [('Content-Type', 'text/plain')]
     start_response(self.canned_collection['400'], headerbase)
     d = {}
     d['success'] = False
     d['failure'] = True
     d['data'] = {'msg': error}
     _ret = json.dumps(d)
     log.debug('ERROR: %s' % _ret)
     return _ret
コード例 #2
0
ファイル: version.py プロジェクト: rsgalloway/grit
    def save(self, msg=None):
        """
        Modify item data and commit to repo. 
        Git objects are immutable, to save means adding a new item

        :param msg: Commit message.
        """
        if msg is None:
            msg = 'Saving %s' % self.name
        log.debug(msg)
        self.repo.addItem(self, msg)
コード例 #3
0
ファイル: local.py プロジェクト: rsgalloway/grit
    def versions(self, version=None):
        """
        List of Versions of this repository.

        :param version: Version index.
        :param rev: Commit sha or ref.

        :return: List of Version objects matching params.
        """
        try:
            versions = [Version(self, c) for c in self._commits()]
        except Exception, e:
            log.debug('No versions exist')
            return []
コード例 #4
0
ファイル: cli.py プロジェクト: rsgalloway/grit
    def _write(item):
        log.debug('writing: %s' % item.name)
        if item.type != 'blob':
            return
        if r.type in ['repo', 'proxy', 'local']:
            path = os.path.join(r.name, item.path)
            pdir = os.path.dirname(path)
            if not os.path.isdir(pdir):
                os.makedirs(pdir)
        else:
            path = item.name

        f = open(path, 'w')
        f.write(item.data())
        f.close()
コード例 #5
0
ファイル: version.py プロジェクト: rsgalloway/grit
    def checkout(self, path):
        """
        Check out file data to path.

        :param path: Filesystem path to check out item to.

        :return: True if successful.
        """
        if os.path.isdir(path):
            path = os.path.join(path, self.name)
        try:
            log.debug('Checking out %s to %s' %(self.path, path))
            f = open(path, 'w')
            f.write(self.data())
            f.close()
            return True
        except Exception, e:
            raise ItemError(e)
コード例 #6
0
ファイル: version.py プロジェクト: rsgalloway/grit
    def from_string(self, repo, name, string):
        """
        Create a new Item from a data stream.

        :param repo: Repo object.
        :param name: Name of item.
        :param data: Data stream.

        :return: New Item class instance.
        """
        try:
            log.debug('Creating new item: %s' % name)
            blob = Blob.from_string(string)
            item = Item(parent=repo, sha=blob.sha, path=name)
            item.blob = blob
            return item
        except AssertionError, e:
            raise ItemError(e)
コード例 #7
0
ファイル: proxy.py プロジェクト: rsgalloway/grit
    def __request(self, url, params):
        """
        Make an HTTP POST request to the server and return JSON data. 

        :param url: HTTP URL to object.

        :returns: Response as dict. 
        """
        log.debug("request: %s %s" % (url, str(params)))
        try:
            response = urlopen(url, urlencode(params)).read()
            if params.get("action") != "data":
                log.debug("response: %s" % response)
            if params.get("action", None) == "data":
                return response
            else:
                return json.loads(response)
        except TypeError, e:
            log.exception("request error")
            raise ServerError(e)
コード例 #8
0
ファイル: server.py プロジェクト: rsgalloway/grit
    def __call__(self, environ, start_response):

        selector_matches = (environ.get('wsgiorg.routing_args') or ([],{}))[1]
        if 'working_path' in selector_matches:
            path_info = selector_matches['working_path'].decode('utf8')
        else:
            path_info = environ.get('PATH_INFO', '').decode('utf8')

        scheme = environ.get('wsgi.url_scheme', 'http')
        host = environ.get('HTTP_HOST', 'localhost').decode('utf8')
        self.url = '%s://%s/%s' %(scheme, host, path_info)

        full_path = os.path.abspath(os.path.join(self.content_path, path_info.strip('/')))
        _pp = os.path.abspath(self.content_path)

        cmd, kwargs = self.get_params(environ)

        if not full_path.startswith(_pp):
            log.error('forbidden: %s' % full_path)
            return self.canned_handlers(environ, start_response, 'forbidden')

        if os.path.exists(full_path):
            mtime = os.stat(full_path).st_mtime
            etag, last_modified =  str(mtime), email.utils.formatdate(mtime)
        else:
            mtime, etag, last_modified = (None, None, None)

        headers = [
            ('Content-type', 'text/plain'),
            ('Date', email.utils.formatdate(time.time())),
            ('Last-Modified', last_modified),
            ('ETag', etag)
        ]

        fmap = {
            'read': handle_read,
            'new': handle_branch,
            'branch': handle_branch,
            'repos': handle_repos,
            'items': handle_items,
            'versions': handle_versions,
            'submodules': handle_submodules,
            'addSubmodule': handle_addSubmodule,
            'addVersion': handle_addVersion,
            'parent': handle_parent,
            'upload': handle_upload,
        }

        repo = get_repo_parent(full_path)
        if repo is None:
            repo = full_path
        item_path = full_path.split(str(repo))[-1][1:]

        #HACK: get the item, swap with repo
        if item_path and cmd != 'submodules':
            log.debug('full_path: %s, item_path: %s' % (full_path, item_path))
            items = repo.items(path=item_path)
            if items:
                repo = item = items[0]

        if cmd == 'data':
            data = repo.file()
            return self.package_response(data, environ, start_response)

        else:
            func = fmap.get(cmd, None)
            if func:
                response = func(repo, **kwargs)
            else:
                response = getattr(repo, cmd)(**kwargs)
        return self.json_response(response, environ, start_response)