def __init__(self, cw=None):
        CallbackType.__init__(self, cw)

        self.pattern = Pattern()
        self.cw = cw

        try:
            opts, args = getopt.getopt(cw.module_options, "hl",
                                       ["help", "license-on-top"])
        except getopt.GetoptError:
            self.usage()
            raise ValueError("*** Parameter Error")

        self.licenseOnTop = False

        for option, value in opts:
            if option in ("-h", "--help"):
                self.usage()
                raise ValueError("*** Parameter Error")
            elif option in ("-l", "--license-on-top"):
                self.licenseOnTop = True
                continue
            else:
                self.usage()
                raise ValueError("*** Parameter Error")
Exemple #2
0
    def __init__(self, callback=None, mod_opts='', opts=''):
        self.pattern = Pattern()

        if callback is None:
            raise ValueError("callback must be defined!")

        self.callback = callback
Exemple #3
0
class ListLicense(CallbackType):
    """checks whether a file contains a copyright or license message and reports the type of license"""

    def __init__(self, cw=None):
        CallbackType.__init__(self, cw)

        self.cw = cw
        self.pattern = Pattern()

    def callback(self, root=None, file=None, type=None):

        definition = self.pattern.getDefinition(type)
        # types that have no openComment have no comment
        # syntax at all. Therefore it is useless to check for
        # a license.
        if definition.openComment is None:
            return

        fullfile = os.path.join(root, file)

        if self.cw.verbose:
            print "Checking License for %s (%s)" % (file, type)

        lines = load(fullfile)

        if self.cw.verbose:
            print "%s has %d lines" % (file, len(lines))

        commentSplitter = CommentSplitter(self.pattern.getDefinition(type))
        elementList = commentSplitter.parse(lines)

        if self.cw.verbose:
            print "%s has %d blocks" % (file, len(elementList))

        commentBlock = None
        for block in elementList:
            if isinstance(block, CommentPart):
                licenseChecker = LicenseType(block)

                if licenseChecker.isLicense:
                    commentBlock = block
                    break
        else:
            print "%s: No comment block found! (No copyright notice either!)" % fullfile
            return

        if not licenseChecker.isCopyright:
            print "%s: Has no copyright" % fullfile

        if not licenseChecker.isLicense:
            print "%s: Has no license" % fullfile
        else:
            if licenseChecker.license is None:
                print "%s: License Type is unknown" % fullfile
            else:
                print "%s: Licensed under %s" % (fullfile, licenseChecker.license)
Exemple #4
0
class ListLicense(CallbackType):
    """checks whether a file contains a copyright or license message and reports the type of license"""
    def __init__(self, cw=None):
        CallbackType.__init__(self, cw)

        self.cw = cw
        self.pattern = Pattern()

    def callback(self, root=None, file=None, type=None):

        definition = self.pattern.getDefinition(type)
        # types that have no openComment have no comment
        # syntax at all. Therefore it is useless to check for
        # a license.
        if definition.openComment is None:
            return

        fullfile = os.path.join(root, file)

        if self.cw.verbose:
            print "Checking License for %s (%s)" % (file, type)

        lines = load(fullfile)

        if self.cw.verbose:
            print "%s has %d lines" % (file, len(lines))

        commentSplitter = CommentSplitter(self.pattern.getDefinition(type))
        elementList = commentSplitter.parse(lines)

        if self.cw.verbose:
            print "%s has %d blocks" % (file, len(elementList))

        commentBlock = None
        for block in elementList:
            if isinstance(block, CommentPart):
                licenseChecker = LicenseType(block)

                if licenseChecker.isLicense:
                    commentBlock = block
                    break
        else:
            print "%s: No comment block found! (No copyright notice either!)" % fullfile
            return

        if not licenseChecker.isCopyright:
            print "%s: Has no copyright" % fullfile

        if not licenseChecker.isLicense:
            print "%s: Has no license" % fullfile
        else:
            if licenseChecker.license is None:
                print "%s: License Type is unknown" % fullfile
            else:
                print "%s: Licensed under %s" % (fullfile,
                                                 licenseChecker.license)
Exemple #5
0
    def __init__(self, cw=None):
        CallbackType.__init__(self, cw)

        self.pattern = Pattern()
        self.cw = cw

        try:
            opts, args = getopt.getopt(cw.module_options, "ef:hnt:", ["existing-only", "file=", "help", "new-only", "type="])
        except getopt.GetoptError:
            self.usage()
            raise ValueError("*** Parameter Error")

        filename = None
        type = "text"

        self.existingOnly = False
        self.newOnly = False

        for option, value in opts:
            if option in ("-h", "--help"):
                self.usage()
                raise ValueError("*** Parameter Error")
            elif option in ("-e", "--existing-only"):
                self.existingOnly = True
                continue
            elif option in ("-f", "--file"):
                filename = value
                continue
            elif option in ("-n", "--new-only"):
                self.newOnly = True
                continue
            elif option in ("-t", "--type"):
                type = value
                continue

        if filename is None:
            self.usage()
            raise ValueError("*** You must supply a license file name")

        if not self.existingOnly and not self.newOnly:
            self.usage()
            raise ValueError("*** No Action has been selected!")

        if not os.path.isabs(filename):
            filename = os.path.join(self.cw.dirtree, filename)

        if self.cw.verbose:
            print "License file is at %s" % filename

        self.license = CommentPart(self.pattern.getDefinition(type), load(filename))

        if len(self.license) == 0:
            raise ValueError("License file is empty or could not be read")
Exemple #6
0
    def __init__(self, callback=None, mod_opts="", opts=""):
        self.pattern = Pattern()

        if callback is None:
            raise ValueError("callback must be defined!")

        self.callback = callback
Exemple #7
0
class StripBlank(CallbackType):
    """ removes spaces from the line endings """
    def __init__(self, cw=None):
        CallbackType.__init__(self, cw)

        self.pattern = Pattern()
        self.cw = cw

    def callback(self, root=None, file=None, type=None):

        definition = self.pattern.getDefinition(type)
        # types that have no openComment have no comment
        # syntax at all. Therefore it is useless to check for
        # a license.
        if definition.openComment is None:
            return

        fullfile = os.path.join(root, file)

        lines = load(fullfile)

        result = []

        for line in lines:
            newline = line.rstrip('\n')
            if len(result) == 0 and (len(newline) == 0 or newline.isspace()):
                continue
            result.append(newline.rstrip())

        result = "\n".join(result) + "\n"

        if result != "".join(lines):
            print "%s: File reformatted" % fullfile
            save(fullfile, result)
Exemple #8
0
class StripBlank(CallbackType):
    """ removes spaces from the line endings """
    def __init__(self, cw=None):
        CallbackType.__init__(self, cw)

        self.pattern = Pattern()
        self.cw = cw

    def callback(self, root=None, file=None, type=None):

        definition = self.pattern.getDefinition(type)
        # types that have no openComment have no comment
        # syntax at all. Therefore it is useless to check for
        # a license.
        if definition.openComment is None:
            return

        fullfile = os.path.join(root, file)

        lines = load(fullfile)

        result = []

        for line in lines:
            newline = line.rstrip('\n')
            if len(result) == 0 and (len(newline) == 0 or newline.isspace()):
                continue
            result.append(newline.rstrip())

        result = "\n".join(result) + "\n"

        if result != "".join(lines):
            print "%s: File reformatted" % fullfile
            save(fullfile, result)
Exemple #9
0
    def __init__(self, cw=None):
        CallbackType.__init__(self, cw)

        self.pattern = Pattern()
        self.cw = cw

        try:
            opts, args = getopt.getopt(
                cw.module_options, "ef:hnt:", ["existing-only", "file=", "help", "new-only", "type="]
            )
        except getopt.GetoptError:
            self.usage()
            raise ValueError("*** Parameter Error")

        filename = None
        type = "text"

        self.existingOnly = False
        self.newOnly = False

        for option, value in opts:
            if option in ("-h", "--help"):
                self.usage()
                raise ValueError("*** Parameter Error")
            elif option in ("-e", "--existing-only"):
                self.existingOnly = True
                continue
            elif option in ("-f", "--file"):
                filename = value
                continue
            elif option in ("-n", "--new-only"):
                self.newOnly = True
                continue
            elif option in ("-t", "--type"):
                type = value
                continue

        if filename is None:
            self.usage()
            raise ValueError("*** You must supply a license file name")

        if not self.existingOnly and not self.newOnly:
            self.usage()
            raise ValueError("*** No Action has been selected!")

        if not os.path.isabs(filename):
            filename = os.path.join(self.cw.dirtree, filename)

        if self.cw.verbose:
            print "License file is at %s" % filename

        self.license = CommentPart(self.pattern.getDefinition(type), load(filename))

        if len(self.license) == 0:
            raise ValueError("License file is empty or could not be read")
Exemple #10
0
class Traverse:
    """ Method called by the TreeBuilder to process the various files"""
    def __init__(self, callback=None, mod_opts='', opts=''):
        self.pattern = Pattern()

        if callback is None:
            raise ValueError("callback must be defined!")

        self.callback = callback

    def traverse(self, root=None, file=None):
        if root is None or file is None:
            raise ValueError("traverse with illegal root or file!")

        type = self.pattern.getType(file)

        self.callback.callback(root, file, type)
Exemple #11
0
class Traverse:
    """ Method called by the TreeBuilder to process the various files"""

    def __init__(self, callback=None, mod_opts="", opts=""):
        self.pattern = Pattern()

        if callback is None:
            raise ValueError("callback must be defined!")

        self.callback = callback

    def traverse(self, root=None, file=None):
        if root is None or file is None:
            raise ValueError("traverse with illegal root or file!")

        type = self.pattern.getType(file)

        self.callback.callback(root, file, type)
Exemple #12
0
    def __init__(self, cw=None):
        CallbackType.__init__(self, cw)

        self.pattern = Pattern()
        self.cw = cw

        try:
            opts, args = getopt.getopt(cw.module_options, "hl", ["help", "license-on-top"])
        except getopt.GetoptError:
            self.usage()
            raise ValueError("*** Parameter Error")

        self.licenseOnTop = False

        for option, value in opts:
            if option in ("-h", "--help"):
                self.usage()
                raise ValueError("*** Parameter Error")
            elif option in ("-l", "--license-on-top"):
                self.licenseOnTop = True
                continue
            else:
                self.usage()
                raise ValueError("*** Parameter Error")
Exemple #13
0
class CommentFormat(CallbackType):
    """ formats existing comment blocks to match the global comment style """
    def __init__(self, cw=None):
        CallbackType.__init__(self, cw)

        self.pattern = Pattern()
        self.cw = cw

    def callback(self, root=None, file=None, type=None):

        definition = self.pattern.getDefinition(type)
        # types that have no openComment have no comment
        # syntax at all. Therefore it is useless to check for
        # a license.
        if definition.openComment is None:
            return

        fullfile = os.path.join(root, file)

        if self.cw.verbose:
            print "Formatting comments in %s (%s)..." % (file, type)

        lines = load(fullfile)

        if self.cw.verbose:
            print "%s has %d lines" % (file, len(lines))

        commentSplitter = CommentSplitter(definition)
        elementList = commentSplitter.parse(lines)

        if self.cw.verbose:
            print "%s has %d blocks" % (file, len(elementList))

        if elementList.toString() != "".join(lines):
            print "%s: File reformatted" % fullfile
            save(fullfile, elementList.toString())
class CommentFormat(CallbackType):
    """ formats existing comment blocks to match the global comment style """
    def __init__(self, cw=None):
        CallbackType.__init__(self, cw)

        self.pattern = Pattern()
        self.cw = cw

    def callback(self, root=None, file=None, type=None):

        definition = self.pattern.getDefinition(type)
        # types that have no openComment have no comment
        # syntax at all. Therefore it is useless to check for
        # a license.
        if definition.openComment is None:
            return

        fullfile = os.path.join(root, file)

        if self.cw.verbose:
            print "Formatting comments in %s (%s)..." % (file, type)

        lines = load(fullfile)

        if self.cw.verbose:
            print "%s has %d lines" % (file, len(lines))

        commentSplitter = CommentSplitter(definition)
        elementList = commentSplitter.parse(lines)

        if self.cw.verbose:
            print "%s has %d blocks" % (file, len(elementList))

        if elementList.toString() != "".join(lines):
            print "%s: File reformatted" % fullfile
            save(fullfile, elementList.toString())
Exemple #15
0
class CheckLicense(CallbackType):
    def __init__(self, cw=None):
        CallbackType.__init__(self, cw)

        self.pattern = Pattern()
        self.cw = cw

        try:
            opts, args = getopt.getopt(cw.module_options, "f:ht:",
                                       ["file=", "help", "type="])
        except getopt.GetoptError:
            self.usage()
            raise ValueError("*** Parameter Error")

        filename = None
        type = "text"

        for option, value in opts:
            if option in ("-h", "--help"):
                self.usage()
                raise ValueError("*** Parameter Error")
            elif option in ("-f", "--file"):
                filename = value
                continue
            elif option in ("-t", "--type"):
                type = value
                continue

        if filename is None:
            self.usage()
            raise ValueError("*** You must supply a license file name")

        if not os.path.isabs(filename):
            filename = os.path.join(self.cw.dirtree, filename)

        if self.cw.verbose:
            print "License file is at %s" % filename

        self.license = CommentPart(self.pattern.getDefinition(type),
                                   load(filename))

        if len(self.license) == 0:
            raise ValueError("*** License file is empty or could not be read")

    def usage(self):
        print "Usage: license.CheckLicense"
        print ""
        print "The following parameters must be supplied using the \"--modopts\" option"
        print "of the main program:"
        print ""
        print "-h, --help:          Show this help"
        print ""
        print "-f, --file:          The license file to check. If name is not absolute,"
        print "                     check relative to the working directory"
        print "-t, --type:          Type of the license file. Default is text. If you want"
        print "                     to use e.g. a checkstyle-license file, you may need 'java'"
        print ""

    def callback(self, root=None, file=None, type=None):

        definition = self.pattern.getDefinition(type)
        # types that have no openComment have no comment
        # syntax at all. Therefore it is useless to check for
        # a license.
        if definition.openComment is None:
            return

        fullfile = os.path.join(root, file)

        if self.cw.verbose:
            print "Checking License for %s (%s)" % (file, type)

        lines = load(fullfile)

        if self.cw.verbose:
            print "%s has %d lines" % (file, len(lines))

        commentSplitter = CommentSplitter(self.pattern.getDefinition(type))
        elementList = commentSplitter.parse(lines)

        if self.cw.verbose:
            print "%s has %d blocks" % (file, len(elementList))

        commentBlock = None
        for block in elementList:
            if isinstance(block, CommentPart):
                licenseChecker = LicenseType(block)
                if licenseChecker.isLicense or licenseChecker.isCopyright:
                    commentBlock = block
                    break
        else:
            print "%s: No comment block found! (No copyright notice either!)" % fullfile
            return

        if len(commentBlock) != len(self.license):
            print "%s: line count of license block does not match: %d vs. %d lines" % (
                fullfile, len(commentBlock), len(self.license))
            return

        space = re.compile("^\s+$")
        for i in range(0, len(commentBlock)):
            patt = re.compile("^(.*?)" + re.escape(self.license[i]) + "(.*?)$")
            match = patt.search(commentBlock[i])
            if match:
                if match.group(1) and not space.search(match.group(1)):
                    print "%s: line %d has leading text: %s" % (
                        fullfile, i + 1, match.group(1))
                if match.group(2) and not space.search(match.group(2)):
                    print "%s: line %d has trailing text: %s" % (
                        fullfile, i + 1, match.group(2))
            else:
                print "%s: line %d does not match" % (fullfile, i + 1)
Exemple #16
0
class ReLicense(CallbackType):
    """ Replaces existing licenses with a given license file and/or adds a license to files"""

    def __init__(self, cw=None):
        CallbackType.__init__(self, cw)

        self.pattern = Pattern()
        self.cw = cw

        try:
            opts, args = getopt.getopt(
                cw.module_options, "ef:hnt:", ["existing-only", "file=", "help", "new-only", "type="]
            )
        except getopt.GetoptError:
            self.usage()
            raise ValueError("*** Parameter Error")

        filename = None
        type = "text"

        self.existingOnly = False
        self.newOnly = False

        for option, value in opts:
            if option in ("-h", "--help"):
                self.usage()
                raise ValueError("*** Parameter Error")
            elif option in ("-e", "--existing-only"):
                self.existingOnly = True
                continue
            elif option in ("-f", "--file"):
                filename = value
                continue
            elif option in ("-n", "--new-only"):
                self.newOnly = True
                continue
            elif option in ("-t", "--type"):
                type = value
                continue

        if filename is None:
            self.usage()
            raise ValueError("*** You must supply a license file name")

        if not self.existingOnly and not self.newOnly:
            self.usage()
            raise ValueError("*** No Action has been selected!")

        if not os.path.isabs(filename):
            filename = os.path.join(self.cw.dirtree, filename)

        if self.cw.verbose:
            print "License file is at %s" % filename

        self.license = CommentPart(self.pattern.getDefinition(type), load(filename))

        if len(self.license) == 0:
            raise ValueError("License file is empty or could not be read")

    def usage(self):
        print "Usage: license.ReLicense"
        print ""
        print 'The following parameters must be supplied using the "--modopts" option'
        print "of the main program:"
        print ""
        print "-h, --help:          Show this help"
        print ""
        print "-e, --existing-only: Replace only existing licenses. If a file has no"
        print "                     license, don't touch it"
        print "-n, --new-only:      Only add licenses to a file which does not already"
        print "                     has one"
        print "-f, --file:          The license file to check. If name is not absolute,"
        print "                     check relative to the working directory"
        print "-t, --type:          Type of the license file. Default is text. If you want"
        print "                     to use e.g. a checkstyle-license file, you may need 'java'"
        print ""

    def callback(self, root=None, file=None, type=None):

        definition = self.pattern.getDefinition(type)
        # types that have no openComment have no comment
        # syntax at all. Therefore it is useless to check for
        # a license.
        if definition.openComment is None:
            return

        fullfile = os.path.join(root, file)

        if self.cw.verbose:
            print "Checking License for %s (%s)" % (file, type)

        lines = load(fullfile)

        if self.cw.verbose:
            print "%s has %d lines" % (file, len(lines))

        commentSplitter = CommentSplitter(definition)
        elementList = commentSplitter.parse(lines)

        if self.cw.verbose:
            print "%s has %d blocks" % (file, len(elementList))

        commentBlock = None
        commentIndex = -1
        firstIndex = -1
        for i in range(0, len(elementList)):
            if isinstance(elementList[i], CommentPart):
                firstIndex = i
                licenseChecker = LicenseType(elementList[i])
                if licenseChecker.isLicense or licenseChecker.isCopyright:
                    commentBlock = elementList[i]
                    commentIndex = i
                    break
        else:
            if firstIndex == -1:
                # if the file has no comment block, then we squeeze a new copyright block on
                # top of it
                if self.newOnly:
                    elementList[:0] = [self.license.copy(definition)]
                    print "%s: Added License" % fullfile
                    save(fullfile, elementList.toString())
                return
            else:
                # we did find a comment block but none of the comment blocks was actually a license
                # block
                commentIndex = firstIndex
                commentBlock = elementList[firstIndex]

        licenseChecker = LicenseType(commentBlock)
        if not licenseChecker.isLicense and not licenseChecker.isCopyright:
            if self.newOnly:
                elementList[:0] = [self.license.copy(definition)]
                print "%s: Added License" % fullfile
                save(fullfile, elementList.toString())
        else:
            if self.existingOnly:
                elementList[commentIndex] = self.license.copy(definition)

                if commentBlock.toString() != elementList[commentIndex].toString():
                    print "%s: Replaced License" % fullfile
                    save(fullfile, elementList.toString())
Exemple #17
0
class TopFormatter(CallbackType):
    """Reformats the header of a java file to match the package, license, imports sequence"""

    def __init__(self, cw=None):
        CallbackType.__init__(self, cw)

    def __init__(self, cw=None):
        CallbackType.__init__(self, cw)

        self.pattern = Pattern()
        self.cw = cw

        try:
            opts, args = getopt.getopt(cw.module_options, "hl", ["help", "license-on-top"])
        except getopt.GetoptError:
            self.usage()
            raise ValueError("*** Parameter Error")

        self.licenseOnTop = False

        for option, value in opts:
            if option in ("-h", "--help"):
                self.usage()
                raise ValueError("*** Parameter Error")
            elif option in ("-l", "--license-on-top"):
                self.licenseOnTop = True
                continue
            else:
                self.usage()
                raise ValueError("*** Parameter Error")

    def usage(self):
        print "Usage: java.TopFormatter"
        print ""
        print 'The following parameters can be supplied using the "--modopts" option'
        print "of the main program:"
        print ""
        print "-h, --help:           Show this help"
        print ""
        print "-l, --license-on-top: Put the license block on the very top of the file."
        print "                      Else it is put right after the package statement."
        print ""

    def callback(self, root=None, file=None, type=None):
        if type != "java":
            return

        if self.cw.verbose:
            print "Processing %s" % file

        definition = self.pattern.getDefinition(type)

        fullfile = os.path.join(root, file)
        lines = load(fullfile)

        commentSplitter = CommentSplitter(definition)
        elementList = commentSplitter.parse(lines)

        commentBlock = None
        for block in elementList:
            if isinstance(block, CommentPart):
                commentBlock = block
                break
        else:
            raise ValueError("%s: No comment block found" % fullfile)

        licenseChecker = LicenseType(commentBlock)

        if not licenseChecker.isLicense and not licenseChecker.isCopyright:
            raise ValueError("%s: First comment block is not the license/copyright block!" % fullfile)

        newElements = BlockList()

        # Search the "package" line
        packagePattern = re.compile("^package\s+.*$")
        packageLine = None

        for packageBlock in elementList:
            if isinstance(packageBlock, CommentPart):
                continue

            for line in packageBlock:
                if packagePattern.search(line):
                    packageLine = line
                    break

            if packageLine is not None:
                break  # python sucks

        newPackageBlock = DataPart()

        if self.licenseOnTop:
            newPackageBlock.append("\n")

        if packageLine is not None:
            newPackageBlock.append(packageLine)
            newPackageBlock.append("\n")

        dataBlock = DataPart()

        if self.licenseOnTop:
            newElements.append(commentBlock)
            newElements.append(newPackageBlock)
        else:
            newElements.append(newPackageBlock)
            newElements.append(commentBlock)
            dataBlock.append("\n")

        importStart = False
        importEnd = False
        importPattern = re.compile("^import\s+.*$")
        commentPattern = re.compile("^\s*\/\/.*$")

        for block in elementList:
            if block is commentBlock:
                continue  # We already have this block on our new list

            # if it is a comment block and we still have an open dataBlock, add the
            # data block on the list, add the comment block and reopen the data block
            # else just add the comment block and go on
            if isinstance(block, CommentPart):
                if dataBlock is None:
                    newElements.append(block)
                else:
                    newElements.append(dataBlock)
                    newElements.append(block)
                    dataBlock = DataPart()
                continue

            # If we found a line after "import" that is neither comment nor blank line, consider
            # the import list done. Just add this block, because it is somewhere deep inside the
            # source code file
            if importEnd:  # We already have copied the last block that contained "import" statements
                newElements.append(block)
                continue

            # We have not yet found the "import end". This might be a block 'before' the license (then it
            # contains the package statement or a block part that contains import lines and maybe the
            # class/interface (non-importe) line. Loop over it.
            for line in block:
                # We already have the "package" line copied
                if packagePattern.search(line):
                    continue

                # If we already fell off the list of imports, don't test any longer. Just add all the
                # lines from the block
                if not importEnd:
                    # We have not yet found an import line. Skip if it is whitespace
                    if not importStart:
                        if len(line) == 0 or line.isspace():
                            continue

                        # This is an import line. Open the import block and go on
                        if importPattern.search(line):
                            importStart = True
                        # This is neither whitespace, nor import nor comment. So we have
                        # a class without imports. Set both flags and go on
                        elif not commentPattern.search(line):
                            importStart = True
                            importEnd = True
                        else:
                            raise ValueError("%s: Strange line found between imports: %s" % (fullfile, line))
                    else:
                        if not (
                            line.isspace()
                            or len(line) == 0
                            or commentPattern.search(line)
                            or importPattern.search(line)
                        ):
                            importEnd = True

                dataBlock.append(line)

            if importEnd:
                newElements.append(dataBlock)
                dataBlock = None

        if newElements.toString() != "".join(lines):
            print "%s: File reformatted" % fullfile
            save(fullfile, newElements.toString())
Exemple #18
0
class ReLicense(CallbackType):
    """ Replaces existing licenses with a given license file and/or adds a license to files"""
    def __init__(self, cw=None):
        CallbackType.__init__(self, cw)

        self.pattern = Pattern()
        self.cw = cw

        try:
            opts, args = getopt.getopt(cw.module_options, "ef:hnt:", ["existing-only", "file=", "help", "new-only", "type="])
        except getopt.GetoptError:
            self.usage()
            raise ValueError("*** Parameter Error")

        filename = None
        type = "text"

        self.existingOnly = False
        self.newOnly = False

        for option, value in opts:
            if option in ("-h", "--help"):
                self.usage()
                raise ValueError("*** Parameter Error")
            elif option in ("-e", "--existing-only"):
                self.existingOnly = True
                continue
            elif option in ("-f", "--file"):
                filename = value
                continue
            elif option in ("-n", "--new-only"):
                self.newOnly = True
                continue
            elif option in ("-t", "--type"):
                type = value
                continue

        if filename is None:
            self.usage()
            raise ValueError("*** You must supply a license file name")

        if not self.existingOnly and not self.newOnly:
            self.usage()
            raise ValueError("*** No Action has been selected!")

        if not os.path.isabs(filename):
            filename = os.path.join(self.cw.dirtree, filename)

        if self.cw.verbose:
            print "License file is at %s" % filename

        self.license = CommentPart(self.pattern.getDefinition(type), load(filename))

        if len(self.license) == 0:
            raise ValueError("License file is empty or could not be read")


    def usage(self):
        print "Usage: license.ReLicense"
        print ""
        print "The following parameters must be supplied using the \"--modopts\" option"
        print "of the main program:"
        print ""
        print "-h, --help:          Show this help"
        print ""
        print "-e, --existing-only: Replace only existing licenses. If a file has no"
        print "                     license, don't touch it"
        print "-n, --new-only:      Only add licenses to a file which does not already"
        print "                     has one"
        print "-f, --file:          The license file to check. If name is not absolute,"
        print "                     check relative to the working directory"
        print "-t, --type:          Type of the license file. Default is text. If you want"
        print "                     to use e.g. a checkstyle-license file, you may need 'java'"
        print ""

    def callback(self, root=None, file=None, type=None):

        definition = self.pattern.getDefinition(type)
        # types that have no openComment have no comment
        # syntax at all. Therefore it is useless to check for
        # a license.
        if definition.openComment is None:
            return

        fullfile = os.path.join(root, file)

        if self.cw.verbose:
            print "Checking License for %s (%s)" % (file, type)

        lines = load(fullfile)

        if self.cw.verbose:
            print "%s has %d lines" % (file, len(lines))

        commentSplitter = CommentSplitter(definition)
        elementList = commentSplitter.parse(lines)

        if self.cw.verbose:
            print "%s has %d blocks" % (file, len(elementList))

        commentBlock = None
        commentIndex = -1
        firstIndex = -1
        for i in range(0, len(elementList)):
            if isinstance(elementList[i], CommentPart):
                firstIndex = i
                licenseChecker = LicenseType(elementList[i])
                if licenseChecker.isLicense or licenseChecker.isCopyright:
                    commentBlock = elementList[i]
                    commentIndex = i
                    break
        else:
            if firstIndex == -1:
                # if the file has no comment block, then we squeeze a new copyright block on
                # top of it
                if self.newOnly:
                    elementList[:0] = [ self.license.copy(definition) ]
                    print "%s: Added License" % fullfile
                    save(fullfile, elementList.toString())
                return
            else:
                # we did find a comment block but none of the comment blocks was actually a license
                # block
                commentIndex = firstIndex
                commentBlock = elementList[firstIndex]

        licenseChecker = LicenseType(commentBlock)
        if not licenseChecker.isLicense and not licenseChecker.isCopyright:
            if self.newOnly:
                elementList[:0] = [ self.license.copy(definition) ]
                print "%s: Added License" % fullfile
                save(fullfile, elementList.toString())
        else:
            if self.existingOnly:
                elementList[commentIndex] = self.license.copy(definition)

                if commentBlock.toString() != elementList[commentIndex].toString():
                    print "%s: Replaced License" % fullfile
                    save(fullfile, elementList.toString())
Exemple #19
0
    def __init__(self, cw=None):
        CallbackType.__init__(self, cw)

        self.cw = cw
        self.pattern = Pattern()
Exemple #20
0
class CheckLicense(CallbackType):
    def __init__(self, cw=None):
        CallbackType.__init__(self, cw)

        self.pattern = Pattern()
        self.cw = cw

        try:
            opts, args = getopt.getopt(cw.module_options, "f:ht:", ["file=", "help", "type="])
        except getopt.GetoptError:
            self.usage()
            raise ValueError("*** Parameter Error")

        filename = None
        type = "text"

        for option, value in opts:
            if option in ("-h", "--help"):
                self.usage()
                raise ValueError("*** Parameter Error")
            elif option in ("-f", "--file"):
                filename = value
                continue
            elif option in ("-t", "--type"):
                type = value
                continue

        if filename is None:
            self.usage()
            raise ValueError("*** You must supply a license file name")

        if not os.path.isabs(filename):
            filename = os.path.join(self.cw.dirtree, filename)

        if self.cw.verbose:
            print "License file is at %s" % filename

        self.license = CommentPart(self.pattern.getDefinition(type), load(filename))

        if len(self.license) == 0:
            raise ValueError("*** License file is empty or could not be read")


    def usage(self):
        print "Usage: license.CheckLicense"
        print ""
        print "The following parameters must be supplied using the \"--modopts\" option"
        print "of the main program:"
        print ""
        print "-h, --help:          Show this help"
        print ""
        print "-f, --file:          The license file to check. If name is not absolute,"
        print "                     check relative to the working directory"
        print "-t, --type:          Type of the license file. Default is text. If you want"
        print "                     to use e.g. a checkstyle-license file, you may need 'java'"
        print ""


    def callback(self, root=None, file=None, type=None):

        definition = self.pattern.getDefinition(type)
        # types that have no openComment have no comment
        # syntax at all. Therefore it is useless to check for
        # a license.
        if definition.openComment is None:
            return

        fullfile = os.path.join(root, file)

        if self.cw.verbose:
            print "Checking License for %s (%s)" % (file, type)

        lines = load(fullfile)

        if self.cw.verbose:
            print "%s has %d lines" % (file, len(lines))

        commentSplitter = CommentSplitter(self.pattern.getDefinition(type))
        elementList = commentSplitter.parse(lines)

        if self.cw.verbose:
            print "%s has %d blocks" % (file, len(elementList))

        commentBlock = None
        for block in elementList:
            if isinstance(block, CommentPart):
                licenseChecker = LicenseType(block)
                if licenseChecker.isLicense or licenseChecker.isCopyright:
                    commentBlock = block
                    break
        else:
            print "%s: No comment block found! (No copyright notice either!)" % fullfile
            return


        if len(commentBlock) != len(self.license):
            print "%s: line count of license block does not match: %d vs. %d lines" % (fullfile, len(commentBlock), len(self.license))
            return

        space = re.compile("^\s+$")
        for i in range(0, len(commentBlock)):
            patt = re.compile("^(.*?)" + re.escape(self.license[i]) + "(.*?)$")
            match = patt.search(commentBlock[i])
            if match:
                if match.group(1) and not space.search(match.group(1)):
                    print "%s: line %d has leading text: %s" % (fullfile, i + 1, match.group(1))
                if match.group(2) and not space.search(match.group(2)):
                    print "%s: line %d has trailing text: %s" % (fullfile, i + 1, match.group(2))
            else:
                print "%s: line %d does not match" % (fullfile, i + 1)
Exemple #21
0
class TopFormatter(CallbackType):
    """Reformats the header of a xml file to make sure that the ?xml line is on top"""
    def __init__(self, cw=None):
        CallbackType.__init__(self, cw)

    def __init__(self, cw=None):
        CallbackType.__init__(self, cw)

        self.pattern = Pattern()
        self.cw = cw

    def callback(self, root=None, file=None, type=None):
        if type != 'xml' and type != 'jelly':
            return

        if self.cw.verbose:
            print "Processing %s" % file

        definition = self.pattern.getDefinition(type)

        fullfile = os.path.join(root, file)
        lines = load(fullfile)

        commentSplitter = CommentSplitter(definition)
        elementList = commentSplitter.parse(lines)

        # Search the "?xml" line
        xmlDefPattern = re.compile("^\s*\<\?xml\s")

        testLine = False
        foundLine = None

        for block in elementList:
            if isinstance(block, DataPart):
                for line in block:
                    if len(line) == 0 or line.isspace():
                        continue
                    testLine = True
                    if xmlDefPattern.search(line):
                        foundLine = line
                        break
            if testLine or foundLine is not None:
                break # python sucks


        newXmlDefBlock = DataPart()

        if foundLine is None:
            newXmlDefBlock.append("<?xml version=\"1.0\" encoding=\"UTF-8\"?>")
        else:

            if self.cw.verbose:
                print "Found line: %s" % foundLine
            newXmlDefBlock.append(foundLine)

        newElements = BlockList()
        newElements.append(newXmlDefBlock)

        for block in elementList:
            if isinstance(block, DataPart):
                dataBlock = DataPart()
                for line in block:
                    # We already have the "?xml" line copied
                    if xmlDefPattern.search(line):
                        continue

                    dataBlock.append(line)
                block = dataBlock

            newElements.append(block)

        if newElements.toString() != "".join(lines):
            print "%s: File reformatted" % fullfile
            save(fullfile, newElements.toString())
Exemple #22
0
    def __init__(self, cw=None):
        CallbackType.__init__(self, cw)

        self.pattern = Pattern()
        self.cw = cw
Exemple #23
0
class TopFormatter(CallbackType):
    """Reformats the header of a java file to match the package, license, imports sequence"""
    def __init__(self, cw=None):
        CallbackType.__init__(self, cw)

    def __init__(self, cw=None):
        CallbackType.__init__(self, cw)

        self.pattern = Pattern()
        self.cw = cw

        try:
            opts, args = getopt.getopt(cw.module_options, "hl",
                                       ["help", "license-on-top"])
        except getopt.GetoptError:
            self.usage()
            raise ValueError("*** Parameter Error")

        self.licenseOnTop = False

        for option, value in opts:
            if option in ("-h", "--help"):
                self.usage()
                raise ValueError("*** Parameter Error")
            elif option in ("-l", "--license-on-top"):
                self.licenseOnTop = True
                continue
            else:
                self.usage()
                raise ValueError("*** Parameter Error")

    def usage(self):
        print "Usage: java.TopFormatter"
        print ""
        print "The following parameters can be supplied using the \"--modopts\" option"
        print "of the main program:"
        print ""
        print "-h, --help:           Show this help"
        print ""
        print "-l, --license-on-top: Put the license block on the very top of the file."
        print "                      Else it is put right after the package statement."
        print ""

    def callback(self, root=None, file=None, type=None):
        if type != 'java':
            return

        if self.cw.verbose:
            print "Processing %s" % file

        definition = self.pattern.getDefinition(type)

        fullfile = os.path.join(root, file)
        lines = load(fullfile)

        commentSplitter = CommentSplitter(definition)
        elementList = commentSplitter.parse(lines)

        commentBlock = None
        for block in elementList:
            if isinstance(block, CommentPart):
                commentBlock = block
                break
        else:
            raise ValueError("%s: No comment block found" % fullfile)

        licenseChecker = LicenseType(commentBlock)

        if not licenseChecker.isLicense and not licenseChecker.isCopyright:
            raise ValueError(
                "%s: First comment block is not the license/copyright block!" %
                fullfile)

        newElements = BlockList()

        # Search the "package" line
        packagePattern = re.compile("^package\s+.*$")
        packageLine = None

        for packageBlock in elementList:
            if isinstance(packageBlock, CommentPart):
                continue

            for line in packageBlock:
                if packagePattern.search(line):
                    packageLine = line
                    break

            if packageLine is not None:
                break  # python sucks

        newPackageBlock = DataPart()

        if self.licenseOnTop:
            newPackageBlock.append("\n")

        if packageLine is not None:
            newPackageBlock.append(packageLine)
            newPackageBlock.append("\n")

        dataBlock = DataPart()

        if self.licenseOnTop:
            newElements.append(commentBlock)
            newElements.append(newPackageBlock)
        else:
            newElements.append(newPackageBlock)
            newElements.append(commentBlock)
            dataBlock.append("\n")

        importStart = False
        importEnd = False
        importPattern = re.compile("^import\s+.*$")
        commentPattern = re.compile("^\s*\/\/.*$")

        for block in elementList:
            if block is commentBlock:
                continue  # We already have this block on our new list

            # if it is a comment block and we still have an open dataBlock, add the
            # data block on the list, add the comment block and reopen the data block
            # else just add the comment block and go on
            if isinstance(block, CommentPart):
                if dataBlock is None:
                    newElements.append(block)
                else:
                    newElements.append(dataBlock)
                    newElements.append(block)
                    dataBlock = DataPart()
                continue

            # If we found a line after "import" that is neither comment nor blank line, consider
            # the import list done. Just add this block, because it is somewhere deep inside the
            # source code file
            if importEnd:  # We already have copied the last block that contained "import" statements
                newElements.append(block)
                continue

            # We have not yet found the "import end". This might be a block 'before' the license (then it
            # contains the package statement or a block part that contains import lines and maybe the
            # class/interface (non-importe) line. Loop over it.
            for line in block:
                # We already have the "package" line copied
                if packagePattern.search(line):
                    continue

                # If we already fell off the list of imports, don't test any longer. Just add all the
                # lines from the block
                if not importEnd:
                    # We have not yet found an import line. Skip if it is whitespace
                    if not importStart:
                        if len(line) == 0 or line.isspace():
                            continue

                        # This is an import line. Open the import block and go on
                        if importPattern.search(line):
                            importStart = True
                        # This is neither whitespace, nor import nor comment. So we have
                        # a class without imports. Set both flags and go on
                        elif not commentPattern.search(line):
                            importStart = True
                            importEnd = True
                        else:
                            raise ValueError(
                                "%s: Strange line found between imports: %s" %
                                (fullfile, line))
                    else:
                        if not (line.isspace() or len(line) == 0
                                or commentPattern.search(line)
                                or importPattern.search(line)):
                            importEnd = True

                dataBlock.append(line)

            if importEnd:
                newElements.append(dataBlock)
                dataBlock = None

        if newElements.toString() != "".join(lines):
            print "%s: File reformatted" % fullfile
            save(fullfile, newElements.toString())
Exemple #24
0
class TopFormatter(CallbackType):
    """Reformats the header of a xml file to make sure that the ?xml line is on top"""
    def __init__(self, cw=None):
        CallbackType.__init__(self, cw)

    def __init__(self, cw=None):
        CallbackType.__init__(self, cw)

        self.pattern = Pattern()
        self.cw = cw

    def callback(self, root=None, file=None, type=None):
        if type != 'xml' and type != 'jelly':
            return

        if self.cw.verbose:
            print "Processing %s" % file

        definition = self.pattern.getDefinition(type)

        fullfile = os.path.join(root, file)
        lines = load(fullfile)

        commentSplitter = CommentSplitter(definition)
        elementList = commentSplitter.parse(lines)

        # Search the "?xml" line
        xmlDefPattern = re.compile("^\s*\<\?xml\s")

        testLine = False
        foundLine = None

        for block in elementList:
            if isinstance(block, DataPart):
                for line in block:
                    if len(line) == 0 or line.isspace():
                        continue
                    testLine = True
                    if xmlDefPattern.search(line):
                        foundLine = line
                        break
            if testLine or foundLine is not None:
                break  # python sucks

        newXmlDefBlock = DataPart()

        if foundLine is None:
            newXmlDefBlock.append("<?xml version=\"1.0\" encoding=\"UTF-8\"?>")
        else:

            if self.cw.verbose:
                print "Found line: %s" % foundLine
            newXmlDefBlock.append(foundLine)

        newElements = BlockList()
        newElements.append(newXmlDefBlock)

        for block in elementList:
            if isinstance(block, DataPart):
                dataBlock = DataPart()
                for line in block:
                    # We already have the "?xml" line copied
                    if xmlDefPattern.search(line):
                        continue

                    dataBlock.append(line)
                block = dataBlock

            newElements.append(block)

        if newElements.toString() != "".join(lines):
            print "%s: File reformatted" % fullfile
            save(fullfile, newElements.toString())