Example #1
0
    def upload_code_version(self, kwargs):
        if 'code' not in kwargs:
            return HttpErrorResponse(
                ManagerException(ManagerException.E_ARGS_MISSING,
                                 'code').message)
        code = kwargs.pop('code')
        if 'description' in kwargs: description = kwargs.pop('description')
        else: description = ''

        if len(kwargs) != 0:
            return HttpErrorResponse(
                ManagerException(ManagerException.E_ARGS_UNEXPECTED,
                                 kwargs.keys()).message)
        if not isinstance(code, FileUploadField):
            return HttpErrorResponse(
                ManagerException(
                    ManagerException.E_ARGS_INVALID,
                    detail='codeVersionId should be a file').message)

        config = self._configuration_get()
        fd, name = tempfile.mkstemp(prefix='code-', dir=self.code_repo)
        fd = os.fdopen(fd, 'w')
        upload = code.file
        codeVersionId = os.path.basename(name)

        bytes = upload.read(2048)
        while len(bytes) != 0:
            fd.write(bytes)
            bytes = upload.read(2048)
        fd.close()

        arch = archive_open(name)
        if arch == None:
            os.remove(name)
            return HttpErrorResponse(
                ManagerException(ManagerException.E_ARGS_INVALID,
                                 detail='Invalid archive format').message)

        for fname in archive_get_members(arch):
            if fname.startswith('/') or fname.startswith('..'):
                archive_close(arch)
                os.remove(name)
                return HttpErrorResponse(
                    ManagerException(
                        ManagerException.E_ARGS_INVALID,
                        detail=
                        'Absolute file names are not allowed in archive members'
                    ).message)
        archive_close(arch)
        config.codeVersions[codeVersionId] = CodeVersion(
            codeVersionId,
            os.path.basename(code.filename),
            archive_get_type(name),
            description=description)
        self._configuration_set(config)
        return HttpJsonResponse(
            {'codeVersionId': os.path.basename(codeVersionId)})
Example #2
0
    def _get_servlet_urls(self, codeVersionId):
        ret = []
        archname = os.path.join(self.code_repo, codeVersionId)

        if os.path.isfile(archname):
            # File-based code upload
            arch = archive_open(archname)
            filelist = archive_get_members(arch)
            if 'WEB-INF/web.xml' in filelist:
                tmp_dir = mkdtemp()
                arch.extract('WEB-INF/web.xml', path=tmp_dir)
                ret = self._get_servlet_urls_from_webxml(os.path.join(tmp_dir, 'WEB-INF', 'web.xml'))
                rmtree(tmp_dir, ignore_errors=True)

            return ret

        # git-based code upload
        webxml_filename = os.path.join(archname, 'WEB-INF', 'web.xml')
        if os.path.isfile(webxml_filename):
            ret = self._get_servlet_urls_from_webxml(webxml_filename)

        return ret
Example #3
0
 def upload_code_version(self, kwargs):
   if 'code' not in kwargs:
     return HttpErrorResponse(ManagerException(ManagerException.E_ARGS_MISSING, 'code').message)
   code = kwargs.pop('code')
   if 'description' in kwargs: description = kwargs.pop('description')
   else: description = ''
   
   if len(kwargs) != 0:
     return HttpErrorResponse(ManagerException(ManagerException.E_ARGS_UNEXPECTED, kwargs.keys()).message)
   if not isinstance(code, FileUploadField):
     return HttpErrorResponse(ManagerException(ManagerException.E_ARGS_INVALID, detail='codeVersionId should be a file').message)
   
   config = self._configuration_get()
   fd, name = tempfile.mkstemp(prefix='code-', dir=self.code_repo)
   fd = os.fdopen(fd, 'w')
   upload = code.file
   codeVersionId = os.path.basename(name)
   
   bytes = upload.read(2048)
   while len(bytes) != 0:
     fd.write(bytes)
     bytes = upload.read(2048)
   fd.close()
   
   arch = archive_open(name)
   if arch == None:
     os.remove(name)
     return HttpErrorResponse(ManagerException(ManagerException.E_ARGS_INVALID, detail='Invalid archive format').message)
   
   for fname in archive_get_members(arch):
     if fname.startswith('/') or fname.startswith('..'):
       archive_close(arch)
       os.remove(name)
       return HttpErrorResponse(ManagerException(ManagerException.E_ARGS_INVALID, detail='Absolute file names are not allowed in archive members').message)
   archive_close(arch)
   config.codeVersions[codeVersionId] = CodeVersion(codeVersionId, os.path.basename(code.filename), archive_get_type(name), description=description)
   self._configuration_set(config)
   return HttpJsonResponse({'codeVersionId': os.path.basename(codeVersionId)})
Example #4
0
    def _get_servlet_urls(self, codeVersionId):
        ret = []
        archname = os.path.join(self.code_repo, codeVersionId)

        if os.path.isfile(archname):
            # File-based code upload
            arch = archive_open(archname)
            filelist = archive_get_members(arch)
            if 'WEB-INF/web.xml' in filelist:
                tmp_dir = mkdtemp()
                arch.extract('WEB-INF/web.xml', path=tmp_dir)
                ret = self._get_servlet_urls_from_webxml(
                    os.path.join(tmp_dir, 'WEB-INF', 'web.xml'))
                rmtree(tmp_dir, ignore_errors=True)

            return ret

        # git-based code upload
        webxml_filename = os.path.join(archname, 'WEB-INF', 'web.xml')
        if os.path.isfile(webxml_filename):
            ret = self._get_servlet_urls_from_webxml(webxml_filename)

        return ret