Exemplo n.º 1
0
    def _createObjectByType(self, name, body, content_type):

        if name.endswith('.py'):

            ob = PythonScript(name)
            ob.write(body)

        elif name.endswith('.dtml'):

            ob = DTMLDocument('', __name__=name)
            ob.munge(body)

        elif content_type in ('text/html', 'text/xml'):

            ob = ZopePageTemplate(name, str(body), content_type=content_type)

        elif content_type[:6] == 'image/':

            ob = Image(name, '', body, content_type=content_type)

        else:
            ob = File(name, '', body, content_type=content_type)

        return ob
Exemplo n.º 2
0
    def _createReport(self, name, steps, messages):

        """ Record the results of a run.
        """
        lines = []
        # Create report
        for step in steps:
            lines.append('=' * 65)
            lines.append('Step: %s' % step)
            lines.append('=' * 65)
            msg = messages[step]
            lines.extend(msg.split('\n'))
            lines.append('')

        report = '\n'.join(lines)
        if isinstance(report, unicode):
            report = report.encode('latin-1')

        file = File(id=name,
                    title='',
                    file=report,
                    content_type='text/plain'
                   )
        self._setObject(name, file)
Exemplo n.º 3
0
    def __call__(self):
        form = self.request.form
        context = aq_inner(self.context)
        if not self.memship.checkPermission('Poi: Add Response', context):
            raise Unauthorized

        response_text = form.get('response', u'')
        new_response = Response(response_text)
        new_response.mimetype = self.mimetype
        new_response.type = self.determine_response_type(new_response)

        issue_has_changed = False
        transition = form.get('transition', u'')
        if transition and transition in self.available_transitions:
            wftool = getToolByName(context, 'portal_workflow')
            before = wftool.getInfoFor(context, 'review_state')
            before = wftool.getTitleForStateOnType(before, 'PoiIssue')
            wftool.doActionFor(context, transition)
            after = wftool.getInfoFor(context, 'review_state')
            after = wftool.getTitleForStateOnType(after, 'PoiIssue')
            new_response.add_change('review_state', _(u'Issue state'),
                                    before, after)
            issue_has_changed = True

        options = [
            ('severity', _(u'Severity'), 'available_severities'),
            ('responsibleManager', _(u'Responsible manager'),
             'available_managers'),
            ]
        # Changes that need to be applied to the issue (apart from
        # workflow changes that need to be handled separately).
        changes = {}
        for option, title, vocab in options:
            new = form.get(option, u'')
            if new and new in self.__getattribute__(vocab):
                current = self.__getattribute__(option)
                if current != new:
                    changes[option] = new
                    new_response.add_change(option, title,
                                            current, new)
                    issue_has_changed = True

        #('targetRelease', 'Target release', 'available_releases'),
        new = form.get('targetRelease', u'')
        if new and new in self.available_releases:
            current = self.targetRelease
            if current != new:
                # from value (uid) to key (id)
                new_label = self.available_releases.getValue(new)
                current_label = self.available_releases.getValue(current)
                changes['targetRelease'] = new
                new_response.add_change('targetRelease', _(u'Target release'),
                                        current_label, new_label)
                issue_has_changed = True

        attachment = form.get('attachment')
        if attachment:
            # File(id, title, file)
            data = File(attachment.filename, attachment.filename, attachment)
            new_response.attachment = data
            issue_has_changed = True

        if len(response_text) == 0 and not issue_has_changed:
            status = IStatusMessage(self.request)
            msg = _(u"No response text added and no issue changes made.")
            msg = translate(msg, 'Poi', context=self.request)
            status.addStatusMessage(msg, type='error')
        else:
            # Apply changes to issue
            context.update(**changes)
            # Add response
            self.folder.add(new_response)
        self.request.response.redirect(context.absolute_url())
Exemplo n.º 4
0
 def _createZODBClone(self):
     return File(self.getId(), '', self._readFile(1))
Exemplo n.º 5
0
    def postResults(self, REQUEST):
        """ Record the results of a test run.

        o Create a folder with properties representing the summary results,
          and files containing the suite and the individual test runs.

        o REQUEST will have the following form fields:

          result -- one of "failed" or "passed"

          totalTime -- time in floating point seconds for the run

          numTestPasses -- count of test runs which passed

          numTestFailures -- count of test runs which failed

          numCommandPasses -- count of commands which passed

          numCommandFailures -- count of commands which failed

          numCommandErrors -- count of commands raising non-assert errors

          suite -- Colorized HTML of the suite table

          testTable.<n> -- Colorized HTML of each test run
        """
        completed = DateTime()
        result_id = 'result_%s' % completed.strftime('%Y%m%d_%H%M%S.%f')
        self._setObject(result_id, ZuiteResults(result_id))
        result = self._getOb(result_id)
        rfg = REQUEST.form.get
        reg = REQUEST.environ.get

        result._updateProperty('completed', completed)

        result._updateProperty('passed', rfg('result').lower() == 'passed')

        result._updateProperty('finished', rfg('finished').lower() == 'true')

        result._updateProperty('time_secs', float(rfg('totalTime', 0)))

        result._updateProperty('tests_passed', int(rfg('numTestPasses', 0)))

        result._updateProperty('tests_failed', int(rfg('numTestFailures', 0)))

        result._updateProperty('commands_passed',
                               int(rfg('numCommandPasses', 0)))

        result._updateProperty('commands_failed',
                               int(rfg('numCommandFailures', 0)))

        result._updateProperty('commands_with_errors',
                               int(rfg('numCommandErrors', 0)))

        result._updateProperty('user_agent', reg('HTTP_USER_AGENT', 'unknown'))

        result._updateProperty('remote_addr', reg('REMOTE_ADDR', 'unknown'))

        result._updateProperty('http_host', reg('HTTP_HOST', 'unknown'))

        result._updateProperty('server_software',
                               reg('SERVER_SOFTWARE', 'unknown'))

        result._updateProperty('product_info', self._listProductInfo())

        result._setObject(
            'suite.html',
            File('suite.html', 'Test Suite', unquote(rfg('suite')),
                 'text/html'))

        test_ids = [
            x for x in REQUEST.form.keys() if x.startswith('testTable')
        ]
        test_ids.sort()

        for test_id in test_ids:
            body = unquote(rfg(test_id))
            result._setObject(
                test_id,
                File(test_id, 'Test case: %s' % test_id, body, 'text/html'))
            testcase = result._getOb(test_id)

            # XXX:  this is silly, but we have no other metadata.
            testcase._setProperty('passed',
                                  _PINK_BACKGROUND.search(body) is None,
                                  'boolean')
Exemplo n.º 6
0
def load_file(filename):
    id = os.path.basename(filename)
    ob = File(id, '', '')
    ob.update_data(file(filename, 'rb').read())
    return ob
    def _discovering_dist_ids(project):
        # for each file in the project we
        # extract the distutils name
        project_path = '/'.join(project.getPhysicalPath())
        files = cat(**{
            'portal_type': ['PSCFile', 'PSCFileLink'],
            'path': project_path
        })
        ids = []
        for file_ in files:
            portal_type = file_.portal_type
            if portal_type == 'PSCFileLink':
                # the file is somewhere else, let's scan it
                file_ = file_.getObject()
                file_ = DistantFile(file_.getExternalURL())
            else:
                file_ = file_.getObject()
            # trying to get back from old
            # storage
            # ExternalStorage here
            #
            if WAS_EXTERNAL_STORAGE and portal_type != 'PSCFileLink':
                from Products.ExternalStorage.ExternalStorage import\
                     ExternalStorage
                storage = ExternalStorage(
                    prefix=EXTERNAL_STORAGE_PATHS[0],
                    archive=False,
                    rename=False,
                )
                # transferring old data to new AT container
                fs = file_.schema['downloadableFile']
                old = fs.storage
                fs.storage = storage
                portal_url = getToolByName(file_, 'portal_url')

                real_file = os.path.join(
                    *portal_url.getRelativeContentPath(file_))
                for path in EXTERNAL_STORAGE_PATHS:
                    final_file = os.path.join(path, real_file)
                    if os.path.exists(final_file):
                        break
                if not os.path.exists(final_file):
                    logging.info(
                        '******** could not get %s on the file system !!' %
                        real_file)
                    continue
                    #raise ValueError('File not found %s' % final_file)
                fs.storage = old
                dfile = file_.getDownloadableFile()
                filename = dfile.filename
                data = open(final_file).read()
                if data == '':
                    logging.info('empty file ! %s' % final_file)
                #f = File(filename, filename, open(final_file))
            elif portal_type != 'PSCFileLink':
                storage = AttributeStorage()
                fs = file_.schema['downloadableFile']
                old = fs.storage
                fs.storage = storage
                dfile = file_.getDownloadableFile()
                data = dfile.get_data()
                filename = dfile.filename
                fs.storage = old
                #file_.getDownloadableFile().data = data
                #f = File(filename, filename, StringIO(data))
            if portal_type != 'PSCFileLink':
                #file_.setDownloadableFile(f)
                file_.schema = PSCFileSchema
                if filename == '' and data == '':
                    logging.info('file empty for %s' % file_)
                else:
                    if filename is None:
                        filename = file_.getId()
                    file_.setDownloadableFile(
                        File(filename, filename, StringIO(data)))
            id_ = extract_distutils_id(file_)
            if id_ is not None and id_ not in ids:
                ids.append(id_)
        return ids
    def create_content(self, data, context):
        registry = getUtility(IRegistry)
        settings = registry.forInterface(IConfiguration, check=False)
        # check api key
        if not data["api_key"]['value'] == settings.api_key:
            return
        # get mappings
        self.type_mapping = self.create_mapping(settings.type_mapping)
        self.field_mapping = self.create_mapping(settings.field_mapping)
        self.view_mapping = self.create_mapping(settings.view_mapping)
        # Map Portal Types
        data["portal_type"]["value"] = self.type_mapping.get(
            data["portal_type"]["value"], data["portal_type"]["value"])
        # Check if Type is available
        portal_types = getToolByName(context, "portal_types")
        types = portal_types.listContentTypes()
        if data["portal_type"]["value"] not in types:
            return

        # TODO Topic to Collection / FormGen / PloneGlossary
        #      ContentProxy
        if data["portal_type"]["value"] in ["FormFolder", "ContentProxy"]:
            return

#        # ToDo PloneBoard
#        if "Ploneboard" in data["portal_type"]["value"]:
#            return

# ToDo check and Fix Collection
        if "Criterion" in data["portal_type"]["value"]:
            return
        if "Criteria" in data["portal_type"]["value"]:
            return
        #
        if "parent_path" in data.keys():
            if "neue-inhalte" in data["parent_path"]["value"]:
                return
            if "veranstaltungen-elsass" in data["parent_path"]["value"]:
                return

        # TODO FI ALIAS Contenttype
        # TODO FI GLOSSARY Contenttype
        # TODO FI FormGen Contenttype
        if data["portal_type"]["value"] in ["Alias"]:
            return
        #
        #    # TODO Image Sizes

        portal = getToolByName(context, 'portal_url').getPortalObject()

        try:
            if "parent_path" not in data.keys():
                parent = portal
            else:
                parent = portal.restrictedTraverse(
                    str(data["parent_path"]["value"]))
        except:
            print "################################"
            print "ERROR GETTING PARENT: %s" % (str(
                data["parent_path"]["value"]))
            return

        if data["portal_type"]["value"] == "Topic":
            print "HOTTOPIC"

        if data["portal_type"]["value"] == "PloneboardComment":
            creator = data["creators"]["value"][0]
            obj = parent.addComment(data["title"]["value"],
                                    data["text"]["value"],
                                    creator=creator)
            obj.setCreationDate(DateTime(data["creation_date"]["value"]))
            if "attachments" not in data.keys():
                return
            for at in data["attachments"]["value"]:
                at_file = File(str(at["id"]), safe_unicode(at["filename"]),
                               base64.b64decode(str(at["data"])),
                               str(at["contentType"]))
                obj.addAttachment(at_file)
            return
        else:
            if data['id']["value"] not in parent.objectIds():
                parent.invokeFactory(
                    type_name=data["portal_type"]["value"],
                    id=data['id']["value"],
                )
            obj = parent[data['id']['value']]

        if obj.portal_type == "Event":
            self.set_event_fields(obj, data)
#        elif obj.portal_type == "News Item":
#            import pdb; pdb.set_trace()
        else:
            self.set_fields(obj, data)

        #
        self.set_workflow_history(obj, data)
        self.set_local_roles(obj, data)
        self.set_exclude_from_nav(obj, data)
        self.set_default_view(obj, data)
        self.set_portlets(obj, data)
        self.set_portlet_blacklists(obj, data)
        self.set_creator_and_owner(obj, data)
        #
        obj.reindexObjectSecurity()
Exemplo n.º 9
0
    def __iter__(self):

        for item in self.previous:
            yield item

        metadata = {}

        if self.metadata:
            metadataFile = open(self.metadata, 'rb')
            reader = csv.DictReader(metadataFile, delimiter=self.delimiter)

            if reader.fieldnames is None:
                raise ValueError("Metadata CSV is empty.")
            if len(reader.fieldnames) is 1 and \
                self.delimiter not in reader.fieldnames[0]:
                msg = "Metadata CSV does not use the specified delimiter: %s"
                raise ValueError(msg % self.delimiter)
            if 'path' not in reader.fieldnames:
                msg = "Metadata CSV file does not have a 'path' column."
                raise ValueError(msg)

            if self.strict:
                field_count = len(reader.fieldnames)

            for row in reader:

                if self.strict and field_count != len(row):
                    msg = "Found a row in Metadata CSV that has a different \
                    count of fields compared to first row: %s"

                    raise ValueError(msg % row)

                path = row['path']
                data = row.copy()
                del data['path']
                metadata[path] = data

        if self.requireMetadata and not metadata:
            m = "Metadata is required, but metadata file %s not given or empty"
            raise ValueError(m % self.metadata)

        if not os.path.exists(self.directory):
            raise ValueError("Directory %s does not exist" % self.directory)

        for dirpath, dirnames, filenames in os.walk(self.directory):

            dirnames.sort()
            filenames.sort()

            wrapData = self.wrapData
            # Create folders first, if necessary
            for dirname in dirnames:
                dirPath = os.path.join(dirpath, dirname)
                zodbPath = self.getZODBPath(dirPath)

                if self.ignored(zodbPath)[1]:
                    continue

                _type = self.folderType

                item = {'_type': self.folderType, '_path': zodbPath}

                if zodbPath in metadata:
                    item.update(metadata[zodbPath])

                yield item

            # Then import files

            for filename in filenames:
                filePath = os.path.join(dirpath, filename)
                zodbPath = self.getZODBPath(filePath)

                if self.ignored(zodbPath)[1]:
                    continue

                if self.metadata and \
                   os.path.abspath(filePath) == os.path.abspath(self.metadata):
                    continue

                if self.requireMetadata and zodbPath not in metadata:
                    continue

                if zodbPath in metadata and \
                  'portal_type' in metadata[zodbPath] and \
                   metadata[zodbPath]['portal_type'] in ['News Item',
                                                         'Document',
                                                         'Event']:
                    # if portal_type is given in metadata.csv, use it!
                    _type = metadata[zodbPath]['portal_type']

                    mimeType = 'text/html'
                    fieldname = 'text'
                    wrapData = False
                    # if the file is an image: use the image field of the
                    # news item, otherwise use the text field
                    if _type == 'News Item':
                        basename, extension = os.path.splitext(filename)
                        if extension and \
                           extension.lower() in mimetypes.types_map and \
                           mimetypes.types_map[extension.lower()].startswith('image'):
                            mimeType = mimetypes.types_map[extension.lower()]
                            fieldname = self.imageField  # the same of news
                            wrapData = self.wrapData

                else:
                    # else make it File or Image
                    _type = self.fileType
                    fieldname = self.fileField
                    mimeType = self.defaultMimeType

                    # Try to guess mime type and content type
                    basename, extension = os.path.splitext(filename)
                    if extension and extension.lower() in mimetypes.types_map:
                        mimeType = mimetypes.types_map[extension.lower()]
                        if mimeType.startswith('image'):
                            _type = self.imageType
                            fieldname = self.imageField

                # read in main content of this item
                infile = open(filePath, 'rb')
                if wrapData:
                    fileData = File(filename, filename, infile, mimeType)
                    fileData.filename = filename
                else:
                    fileData = infile.read()
                infile.close()

                item = {
                    '_type': _type,
                    '_path': zodbPath,
                    '_mimetype': mimeType,
                    fieldname: fileData
                }

                if zodbPath in metadata:
                    item.update(metadata[zodbPath])

                yield item