def test_known_licenses(self): """Test the KNOWN_LICENSES""" total_licenses = len(KNOWN_LICENSES) self.assertEqual(total_licenses, 3, msg='shared_setup has %s licenses' % total_licenses); md5sums = [] for short, data in KNOWN_LICENSES.items(): # the known text must be in known_licenses dir with the short name fn = os.path.join(REPO_BASE_DIR, 'known_licenses', short) self.assertTrue(os.path.isfile(fn), msg='license %s is in known_licenses directory'% short) md5sum = get_md5sum(fn) self.assertEqual(data[0], md5sum, msg='md5sum from KNOWN_LICENSES %s matches the one in known_licenses dir %s for %s' % (data[0], md5sum, short) ) self.assertFalse(md5sum in md5sums, msg='md5sum for license %s is unique' % md5sum) lic_name, classifier = get_license(license=fn) self.assertEqual(lic_name, os.path.basename(fn), msg='file %s is license %s' % (fn, lic_name)) self.assertTrue(classifier.startswith('License :: OSI Approved :: ') or classifier == 'License :: Other/Proprietary License', msg='classifier as expected for %s' % short)
def check_header(filename, script=False, write=False): """ Given filename, extract the header, verify it if script: treat first line as shebang if write: adapt file to new header If the header contains line '### External compatible license', one assumes the license is correct and should not be controlled by check_header Return if header is different from expected or not """ header, shebang = get_header(filename, script=script) header_end_pos = len(header) changed = False if shebang is not None: # original position header_end_pos += 1 + len(shebang) # 1 is from splitted newline if 'python' in shebang and shebang != SHEBANG_ENV_PYTHON: log.info('python in shebang, forcing env python (header modified)') changed = True shebang = SHEBANG_ENV_PYTHON if re.search(r'^### External compatible license\s*$', header, re.M): log.info('Header is an external compatible license. Leaving the header as-is.') return changed # genheader # version is irrelevant name_url = get_name_url(version='ALL_VERSIONS') license, _ = get_license() # begin and endyear from copyright rule beginyear, endyear = begin_end_from_header(header) data = { 'beginyear': beginyear, 'endyear': endyear, 'name': name_url['name'], 'url': name_url['url'], } gen_header = gen_license_header(license, **data) # force encoding? reg_enc = ENCODING_REGEXP.search(header) if reg_enc: enc_line = reg_enc.group(1) + "\n" # matches full line, but not newline gen_header = enc_line + gen_header if header != gen_header: log.info("Diff header vs gen_header\n" + "".join(nicediff(header, gen_header))) changed = True if write and changed: log.info('write enabled and different header. Going to modify file %s' % filename) wholetext = open(filename).read() newtext = '' if shebang is not None: newtext += shebang + "\n" newtext += gen_header newtext += wholetext[header_end_pos:] _write(filename, newtext) # return different or not return changed