Esempio n. 1
0
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, orig_shebang = get_header(filename, script=script)
    header_end_pos = len(header)

    changed = False

    shebang = None
    if script:
        # scripts must have an appropriate shebang
        shebang = orig_shebang
        file_ext = os.path.splitext(filename)[1]
        log.info("Shebang found in %s (ext: %s): %s", filename, file_ext,
                 shebang)

        if file_ext == '.py':
            if shebang != SHEBANG_ENV_PYTHON:
                log.info(
                    "Wrong shebang for Python script %s: found '%s', should be '%s'",
                    filename, shebang, SHEBANG_ENV_PYTHON)
                shebang = SHEBANG_ENV_PYTHON

        elif file_ext in ['.sh', '']:
            if shebang != SHEBANG_BIN_BASH:
                log.info(
                    "Wrong shebang for shell script %s: found '%s', should be '%s'",
                    filename, shebang, SHEBANG_BIN_BASH)
                shebang = SHEBANG_BIN_BASH

        else:
            log.warn(
                "Don't know expected shebang based on extension '%s' for script '%s', assuming it's OK...",
                file_ext, filename)

        changed = shebang != orig_shebang

    if orig_shebang is not None:
        # original position
        header_end_pos += 1 + len(orig_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
    setup = vsc_setup()
    name_url = setup.get_name_url(version='ALL_VERSIONS')
    license_name, _ = setup.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'],
    }

    # reconstruct original header, incl. shebang (if any)
    if orig_shebang:
        orig_header = orig_shebang + '\n' + header
    else:
        orig_header = header

    # generate header like it should be
    gen_header = gen_license_header(license_name, **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

    # compose header like it should be, incl. shebang
    if shebang:
        new_header = shebang + '\n' + gen_header
    else:
        new_header = gen_header

    if orig_header != new_header:
        log.info("Diff orig_header vs new_header for %s\n" % filename +
                 ''.join(nicediff(orig_header, new_header)))
        changed = True

    if write and changed:
        log.info(
            'write enabled and different header. Going to modify file %s' %
            filename)
        with open(filename) as fh:
            wholetext = fh.read()
        _write(filename, new_header + wholetext[header_end_pos:])

    # return different or not
    return changed
Esempio n. 2
0
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, orig_shebang = get_header(filename, script=script)
    header_end_pos = len(header)

    changed = False

    shebang = None
    if script:
        # scripts must have an appropriate shebang
        shebang = orig_shebang
        file_ext = os.path.splitext(filename)[1]
        log.info("Shebang found in %s (ext: %s): %s", filename, file_ext, shebang)

        if file_ext == '.py':
            if shebang != SHEBANG_ENV_PYTHON:
                log.info("Wrong shebang for Python script %s: found '%s', should be '%s'",
                        filename, shebang, SHEBANG_ENV_PYTHON)
                shebang = SHEBANG_ENV_PYTHON

        elif file_ext in ['.sh', '']:
            if shebang != SHEBANG_BIN_BASH:
                log.info("Wrong shebang for shell script %s: found '%s', should be '%s'",
                         filename, shebang, SHEBANG_BIN_BASH)
                shebang = SHEBANG_BIN_BASH

        else:
            log.warn("Don't know expected shebang based on extension '%s' for script '%s', assuming it's OK...",
                     file_ext, filename)

        changed = shebang != orig_shebang

    if orig_shebang is not None:
        # original position
        header_end_pos += 1 + len(orig_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
    setup = vsc_setup()
    name_url = setup.get_name_url(version='ALL_VERSIONS')
    license_name, _ = setup.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'],
    }

    # reconstruct original header, incl. shebang (if any)
    if orig_shebang:
        orig_header = orig_shebang + '\n' + header
    else:
        orig_header = header

    # generate header like it should be
    gen_header = gen_license_header(license_name, **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

    # compose header like it should be, incl. shebang
    if shebang:
        new_header = shebang + '\n' + gen_header
    else:
        new_header = gen_header

    if orig_header != new_header:
        log.info("Diff orig_header vs new_header for %s\n" % filename + ''.join(nicediff(orig_header, new_header)))
        changed = True

    if write and changed:
        log.info('write enabled and different header. Going to modify file %s' % filename)
        with open(filename) as fh:
            wholetext = fh.read()
        _write(filename, new_header + wholetext[header_end_pos:])

    # return different or not
    return changed