Exemple #1
0
func extractCopyrightNotices(path string, raw []byte, verbose bool, showNotice bool, copyrightTagger *tagger.Tagger) ([]byte, error) {
	if showNotice {
		log.Printf("[LIC %s]: found copyright outside of comments\n", path)
	}

	cindex := copyrightTagger.FindAllIndex(raw)
	if cindex == nil {
		return nil, fmt.Errorf("%s: matched a copyright but couldn't find it", path)
	}

	var ltext []byte
	for i := 0; i < len(cindex); i++ {
		start := cindex[i][0]
		end := cindex[i][1]

		if showNotice {
			log.Printf("[COPYRIGHT %s] %s\n", path, string(raw[start:end]))
		}

		ltext = append(ltext, raw[start:end]...)
		ltext = append(ltext, '\n')
	}

	return ltext, nil
}
Exemple #2
0
//
// Strategy / Heuristics:
//
// 1. If this is an unsupported filetype, return a notice to that effect, including identifying the type of file
//
// 2. Look for a copyright notice, if none was found, return a canonical "unknown copyright" notice.
//
// 3. If a copyright notice was found, but no comments were found, just include the copyright notice.
//
// 4. If a copyright notice was found, create the notice text by including all of the
//    text from all comment blocks which have copyright notices.
//
// 5. As a last resort, if a copyright notice was found, and comments were found, but the copyright
//    notice wasn't found in a comment, just include the copyright notice.
//
func NewNoticeFromFile(path string, verbose bool, showNotice bool, copyrightTagger *tagger.Tagger) (*Notice, error) {

	if verbose {
		log.Printf("[LIC] Process %s\n", path)
	}

	m, ltype, err := skipFile(path)
	if err != nil {
		if m == nil {
			return nil, err
		}
		return mkNotice(path, ltype, append([]byte("Unsupported Filetype: "), m.Magic...), showNotice)
	}

	raw, err := ioutil.ReadFile(path)
	if err != nil {
		return nil, err
	}

	// Check to see if any copyright notice exists in this file within or not inside of comments
	if !copyrightTagger.Match(raw) {
		if showNotice {
			log.Printf("[LIC %s] %s\n", path, noNotice)
		}
		return mkNotice(path, ltype, nil, showNotice)
	}

	cindex := rcomment.FindAllIndex(raw, -1)
	var ltext []byte

	if cindex == nil {
		ltext, err = extractCopyrightNotices(path, raw, verbose, showNotice, copyrightTagger)
		if err != nil {
			return nil, err
		}
		return mkNotice(path, ltype, ltext, showNotice)
	}

	for i := 0; i < len(cindex); i++ {
		start := cindex[i][0]
		end := cindex[i][1]

		// If the comment does contain something I want I take it if not I skip and go back to the top
		if !copyrightTagger.Match(raw[start:end]) {
			continue
		}

		if showNotice {
			log.Printf("[LICENSE %s] %s\n", path, string(raw[start:end]))
		}

		ltext = append(ltext, raw[start:end]...)
		ltext = append(ltext, '\n')
	}

	if ltext == nil {
		ltext, err = extractCopyrightNotices(path, raw, verbose, showNotice, copyrightTagger)
		if err != nil {
			return nil, err
		}
		return mkNotice(path, ltype, ltext, showNotice)
	}

	return mkNotice(path, ltype, ltext, showNotice)
}