buildframework/helium/sf/python/pythoncore/lib/comments.py
author wbernard
Fri, 13 Aug 2010 14:59:05 +0300
changeset 628 7c4a911dc066
parent 587 85df38eb4012
permissions -rw-r--r--
helium_11.0.0-e00f171ca185
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
587
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
     1
#============================================================================ 
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
     2
#Name        : comments.py 
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
     3
#Part of     : Helium 
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
     4
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
     5
#Copyright (c) 2009 Nokia Corporation and/or its subsidiary(-ies).
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
     6
#All rights reserved.
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
     7
#This component and the accompanying materials are made available
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
     8
#under the terms of the License "Eclipse Public License v1.0"
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
     9
#which accompanies this distribution, and is available
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    10
#at the URL "http://www.eclipse.org/legal/epl-v10.html".
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    11
#
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    12
#Initial Contributors:
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    13
#Nokia Corporation - initial contribution.
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    14
#
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    15
#Contributors:
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    16
#
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    17
#Description:
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    18
#===============================================================================
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    19
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    20
""" Helper to parse branch information.
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    21
"""
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    22
import amara
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    23
import logging
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    24
import os
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    25
import re
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    26
import string
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    27
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    28
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    29
COMMENT_SYMBOLS = {
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    30
    '.java': ['//'],
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    31
    '.hrh': ['//'],
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    32
    '.cpp': ['//'],
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    33
    '.h': ['//'],
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    34
    '.inf': ['//'],
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    35
    '.mmp': ['//'],
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    36
    '.iby': ['//'],
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    37
    '.pl':['#'],
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    38
    '.py':['#'],
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    39
    '.mk':['#'],
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    40
    '.bat':['REM'],
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    41
    '.xml':['<!--'],
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    42
    '.txt':['//'],
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    43
    '.cmd':['#','REM']
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    44
    }
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    45
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    46
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    47
# Uncomment this line to enable logging in this module, or configure logging elsewhere   
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    48
#logging.basicConfig(level=logging.DEBUG)
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    49
_logger = logging.getLogger("comments")
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    50
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    51
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    52
class CommentParser(object):
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    53
    """ Parse branch information. """
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    54
    def __init__(self, files, element_name):
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    55
        self.files = files
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    56
        self.element_name = element_name
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    57
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    58
    def scan(self):
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    59
        """ This method goes processes the input files.
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    60
        
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    61
        It returns an xml document.  """
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    62
        doc = amara.create_document(u"commentLog")
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    63
        for path in self.files:
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    64
            open_file = open(path)
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    65
            CommentParser.scan_content(path, open_file.read(), self.element_name, doc)
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    66
            open_file.close()
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    67
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    68
        #print doc.xml()
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    69
        return doc
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    70
    
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    71
    @staticmethod
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    72
    def scan_content(filename, content, element_name, doc=None):
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    73
        """ This method scan the defined content to find any custom comment tags.
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    74
        
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    75
        It returns an xml document.
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    76
        """
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    77
        # Creating a doc if not defined
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    78
        if not doc:
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    79
            doc = amara.create_document(u"commentLog")
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    80
            
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    81
        # Search the file for any XML elements matching the given element name
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    82
        regex = string.Template(r"<${element_name}.*</${element_name}>").substitute(element_name=element_name)
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    83
        comment_elements = re.findall(regex, content, re.DOTALL)
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    84
        for comment in comment_elements:
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    85
            (_, file_type) = os.path.splitext(filename)
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    86
            file_type = file_type.lower()
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    87
            if COMMENT_SYMBOLS.has_key(file_type):
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    88
                for i in range(len(COMMENT_SYMBOLS[file_type])): 
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    89
                    comment = comment.replace(COMMENT_SYMBOLS[file_type][i], "")
628
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 587
diff changeset
    90
                    
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 587
diff changeset
    91
            doc.commentLog.xml_append_fragment(comment)
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 587
diff changeset
    92
            # Add a generic file attribute to the comment to label which file it comes from
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 587
diff changeset
    93
            doc.commentLog.xml_children[-1].xml_set_attribute(u'file', unicode(filename))
587
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    94
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    95
        #print doc.xml()
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    96
        return doc
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    97