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())
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)
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())
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())
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())
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())