Example #1
0
    def test_get_header(self):
        """Test get_header function from .get_header files"""

        self.assertTrue(vsc.install.headers.HEADER_REGEXP.pattern.startswith('\A'),
                        msg='header regexp patterns starts with start of string: %s' %
                        vsc.install.headers.HEADER_REGEXP.pattern)

        # tuple, number of characters in header and shebang (-1 is None)
        # prefix _script_ to run with script=True
        expected = {
            'f1': (20, -1),  # split with special comment
            '_script_f1': (20, -1),  # because it's not a script
            'f2': (41, -1),  # split with docstring
            '_script_f2': (25, 15),  # fake shebang
            'f3': (0, -1),  # no header
        }

        for filename in glob.glob(os.path.join(self.setup.REPO_TEST_DIR, 'headers', "*.get_header")):
            log.info('test_get_header filename %s' % filename)

            found = False
            name = os.path.basename(filename[:-len('.get_header')])
            if name in expected:
                found = True
                self._get_header(filename, name, False, expected)

            name = '_script_%s' % name
            if name in expected:
                found = True
                self._get_header(filename, name, True, expected)

            self.assertTrue(found, msg='filename %s is expected')
Example #2
0
    def test_prospecrtorfail(self):
        """Test that whitelisted warnings actually fails"""

        base_dir = os.path.join(os.path.dirname(os.path.realpath(__file__)),
                                'prospectortest')
        test_files = glob.glob(
            os.path.join(base_dir, 'lib', 'vsc', 'mockinstall', "*.py"))
        test_files = [x for x in test_files if '__init__.py' not in x]
        log.debug("test_files = %s" % test_files)
        log.debug("base_dir = %s" % base_dir)

        failures = commontest.run_prospector(base_dir,
                                             clear_ignore_patterns=True)
        log.debug("Failures = %s" % failures)

        detected_tests = []
        all_tests = []
        for testfile in test_files:
            testfile_base = os.path.splitext(
                os.path.basename(testfile))[0].replace("_", "-")
            all_tests.append(testfile_base)
            for failure in failures:
                if failure['location'][
                        'path'] == testfile and testfile_base in [
                            failure['code'], failure['message']
                        ]:
                    detected_tests.append(testfile_base)

        log.debug("All tests = %s" % all_tests)
        log.info("Detected prospector tests = %s" % detected_tests)
        undetected_tests = [x for x in all_tests if x not in detected_tests]

        self.assertFalse(undetected_tests,
                         "\nprospector did not detect %s\n" % undetected_tests)
Example #3
0
    def test_get_header(self):
        """Test get_header function from .get_header files"""

        self.assertTrue(
            vsc.install.headers.HEADER_REGEXP.pattern.startswith('\A'),
            msg='header regexp patterns starts with start of string: %s' %
            vsc.install.headers.HEADER_REGEXP.pattern)

        # tuple, number of characters in header and shebang (-1 is None)
        # prefix _script_ to run with script=True
        expected = {
            'f1': (20, -1),  # split with special comment
            '_script_f1': (20, -1),  # because it's not a script
            'f2': (41, -1),  # split with docstring
            '_script_f2': (25, 15),  # fake shebang
            'f3': (0, -1),  # no header
        }

        for filename in glob.glob(
                os.path.join(self.setup.REPO_TEST_DIR, 'headers',
                             "*.get_header")):
            log.info('test_get_header filename %s' % filename)

            found = False
            name = os.path.basename(filename[:-len('.get_header')])
            if name in expected:
                found = True
                self._get_header(filename, name, False, expected)

            name = '_script_%s' % name
            if name in expected:
                found = True
                self._get_header(filename, name, True, expected)

            self.assertTrue(found, msg='filename %s is expected')
Example #4
0
 def compare(filename, content):
     log.info('mocked write does compare for %s ' % filename)
     name = filename[:-len('.check')]
     compares.append(name)
     new_filename = '%s.fixed' % name
     self.assertEqual(content, open(new_filename).read(),
                      msg='new content is as expected for %s' % filename)
Example #5
0
 def compare(filename, content):
     log.info('mocked write does compare for %s ' % filename)
     name = filename[:-len('.check')]
     compares.append(name)
     new_filename = '%s.fixed' % name
     self.assertEqual(content, open(new_filename).read(),
                      msg='new content is as expected for %s' % filename)
Example #6
0
 def compare(filename, content):
     log.info('mocked write does compare for %s ' % filename)
     name = filename.replace('.check', '')
     compares.append(name)
     new_filename = filename.replace('.check', '.fixed')
     self.assertEqual(content, open(new_filename).read(),
                      msg='new content is as expected for %s' % filename)
Example #7
0
    def test_prospecrtorfail(self):
        """Test that whitelisted warnings actually fails"""

        base_dir = os.path.join(os.path.dirname(os.path.realpath(__file__)), 'prospectortest')
        test_files = glob.glob(os.path.join(base_dir, 'lib', 'vsc', 'mockinstall', "*.py"))
        test_files = [x for x in test_files if '__init__.py' not in x]
        log.debug("test_files = %s" % test_files)
        log.debug("base_dir = %s" % base_dir)

        failures = commontest.run_prospector(base_dir, clear_ignore_patterns=True)
        log.debug("Failures = %s" % failures)

        detected_tests = []
        all_tests = []
        for testfile in test_files:
            testfile_base = os.path.splitext(os.path.basename(testfile))[0].replace("_", "-")
            all_tests.append(testfile_base)
            for failure in failures:
                if failure['location']['path'] == testfile and testfile_base in [failure['code'], failure['message']]:
                    detected_tests.append(testfile_base)

        log.debug("All tests = %s" % all_tests)
        log.info("Detected prospector tests = %s" % detected_tests)
        undetected_tests = [x for x in all_tests if x not in detected_tests]

        self.assertFalse(undetected_tests, "\nprospector did not detect %s\n" % undetected_tests)
Example #8
0
 def compare(filename, content):
     log.info('mocked write does compare for %s ' % filename)
     name = filename.replace('.check', '')
     compares.append(name)
     new_filename = filename.replace('.check', '.fixed')
     self.assertEqual(content,
                      open(new_filename).read(),
                      msg='new content is as expected for %s' %
                      filename)
Example #9
0
    def run(self):
        log.info("mympirun_vsc_install_scripts")
        # old-style class
        vsc_setup.vsc_install_scripts.run(self)

        for script in self.original_outfiles:
            if script.endswith(".py") or script.endswith(".sh"):
                script = script[:-3]

            if script.endswith('/mympirun'):
                # make the fake dir, create all symlinks

                # make all links
                # they are created with relative paths !

                rel_script = os.path.basename(script)
                rel_script_dir = os.path.dirname(script)

                # abspath: all_syms = [os.path.join(self.install_dir, x) for x in MYMPIRUN_ALIASES]
                # abspath: all_syms.append(os.path.join(abs_fakepath, 'mpirun'))
                # with relative paths, we also need to chdir for the fake/mpirun and ref to ../mympirun
                previous_pwd = os.getcwd()

                os.chdir(rel_script_dir)
                for sym_name in MYMPIRUN_ALIASES:
                    if os.path.exists(sym_name):
                        os.remove(sym_name)
                    os.symlink(rel_script, sym_name)
                    newoutfile = os.path.join(rel_script_dir, sym_name)
                    self.outfiles.append(newoutfile)
                    log.info("symlink %s to %s newoutfile %s", rel_script,
                             sym_name, newoutfile)

                # fake mpirun
                os.chdir(previous_pwd)
                abs_fakepath = os.path.join(self.install_dir,
                                            FAKE_SUBDIRECTORY_NAME)
                if not os.path.isdir(abs_fakepath):
                    log.info("creating abs_fakepath %s", abs_fakepath)
                    os.mkdir(abs_fakepath)
                else:
                    log.info("not creating abs_fakepath %s (already exists)",
                             abs_fakepath)

                os.chdir(abs_fakepath)  # abs_fakepath si not always absolute
                fake_mpirun = os.path.join(abs_fakepath, 'mpirun')
                if os.path.exists(fake_mpirun):
                    os.remove(fake_mpirun)

                mympirun_src = '../%s' % rel_script
                os.symlink(mympirun_src, 'mpirun')
                self.outfiles.append(fake_mpirun)
                log.info("symlink %s to %s newoutfile %s", mympirun_src,
                         'mpirun', fake_mpirun)

                os.chdir(previous_pwd)
Example #10
0
    def run(self):
        log.info("mympirun_vsc_install_scripts")
        # old-style class
        vsc_setup.vsc_install_scripts.run(self)

        for script in self.original_outfiles:
            if script.endswith(".py") or script.endswith(".sh"):
                script = script[:-3]

            if script.endswith('/mympirun'):
                # make the fake dir, create all symlinks

                # make all links
                # they are created with relative paths !

                rel_script = os.path.basename(script)
                rel_script_dir = os.path.dirname(script)

                # abspath: all_syms = [os.path.join(self.install_dir, x) for x in MYMPIRUN_ALIASES]
                # abspath: all_syms.append(os.path.join(abs_fakepath, 'mpirun'))
                # with relative paths, we also need to chdir for the fake/mpirun and ref to ../mympirun
                previous_pwd = os.getcwd()

                os.chdir(rel_script_dir)
                for sym_name in MYMPIRUN_ALIASES:
                    if os.path.exists(sym_name):
                        os.remove(sym_name)
                    os.symlink(rel_script, sym_name)
                    newoutfile = os.path.join(rel_script_dir, sym_name)
                    self.outfiles.append(newoutfile)
                    log.info("symlink %s to %s newoutfile %s" % (rel_script, sym_name, newoutfile))

                # fake mpirun
                os.chdir(previous_pwd)
                abs_fakepath = os.path.join(self.install_dir, FAKE_SUBDIRECTORY_NAME)
                if not os.path.isdir(abs_fakepath):
                    log.info("creating abs_fakepath %s" % abs_fakepath)
                    os.mkdir(abs_fakepath)
                else:
                    log.info("not creating abs_fakepath %s (already exists)" % abs_fakepath)

                os.chdir(abs_fakepath)  # abs_fakepath si not always absolute
                fake_mpirun = os.path.join(abs_fakepath, 'mpirun')
                if os.path.exists(fake_mpirun):
                    os.remove(fake_mpirun)

                mympirun_src = '../%s' % rel_script
                os.symlink(mympirun_src, 'mpirun')
                self.outfiles.append(fake_mpirun)
                log.info("symlink %s to %s newoutfile %s" % (mympirun_src, 'mpirun', fake_mpirun))

                os.chdir(previous_pwd)
Example #11
0
    def run(self):
        """
        make a fake mpirun, that replaces the symlinks of all the mpirun aliases.
        This way any mpirun call will be passed to the fake mpirun.

        Next, symlink the fake mpirun to the mympirun executable.
        This way any mpirun call will be passed to the mympirun executable, but we can see that it passed through
        the fake mpirun
        """

        # old-style class
        vsc_setup.vsc_install_scripts.run(self)

        for script in self.original_outfiles:
            if script.endswith(".py") or script.endswith(".sh"):
                script = script[:-3]

            if script.endswith('/mympirun'):

                # store current working dir so we can get back to it
                previous_pwd = os.getcwd()

                # get script basename and dirname
                rel_script = os.path.basename(script)
                rel_script_dir = os.path.dirname(script)
                os.chdir(rel_script_dir)

                # create symlinks that point to mympirun for all mpirun aliases
                for sym_name in MYMPIRUN_ALIASES:
                    if os.path.exists(sym_name):
                        os.remove(sym_name)
                    os.symlink(rel_script, sym_name)
                    newoutfile = os.path.join(rel_script_dir, sym_name)
                    self.outfiles.append(newoutfile)
                    log.info("symlink %s to %s newoutfile %s", rel_script, sym_name, newoutfile)

                # create a directory for faking mpirun
                os.chdir(previous_pwd)
                abs_fakepath = os.path.join(self.install_dir, FAKE_SUBDIRECTORY_NAME)
                if not os.path.isdir(abs_fakepath):
                    log.info("creating abs_fakepath %s", abs_fakepath)
                    os.mkdir(abs_fakepath)
                else:
                    log.info("not creating abs_fakepath %s (already exists)", abs_fakepath)

                # create a fake mpirin and symlink the real mpirun to it
                os.chdir(abs_fakepath)
                fake_mpirun = os.path.join(abs_fakepath, 'mpirun')
                if os.path.exists(fake_mpirun):
                    os.remove(fake_mpirun)

                # create another symlink that links mpirun to mympirun
                mympirun_src = '../%s' % rel_script
                os.symlink(mympirun_src, 'mpirun')
                self.outfiles.append(fake_mpirun)
                log.info("symlink %s to %s newoutfile %s", mympirun_src, 'mpirun', fake_mpirun)

                os.chdir(previous_pwd)
Example #12
0
    def test_gen_license_header(self):
        """Test the generation of headers for all supported/known licenses"""

        data = {
            'name': 'projectname',
            'beginyear': 1234,
            'endyear': 5678,
            'url': 'https://example.com/projectname',
        }
        for license in KNOWN_LICENSES.keys():
            res_fn = os.path.join(self.setup.REPO_TEST_DIR, 'headers', license)
            result = open(res_fn).read()

            gen_txt = gen_license_header(license, **data)
            self.assertEqual(gen_txt, result, msg='generated header for license %s as expected' % license)
            log.info('generated license header %s' % license)
Example #13
0
    def test_gen_license_header(self):
        """Test the generation of headers for all supported/known licenses"""

        data = {
            'name': 'projectname',
            'beginyear': 1234,
            'endyear': 5678,
            'url': 'https://example.com/projectname',
        }
        for license in KNOWN_LICENSES.keys():
            res_fn = os.path.join(self.setup.REPO_TEST_DIR, 'headers', license)
            result = open(res_fn).read()

            gen_txt = gen_license_header(license, **data)
            self.assertEqual(gen_txt, result, msg='generated header for license %s as expected' % license)
            log.info('generated license header %s' % license)
Example #14
0
    def test_prospectorfail(self):
        """Test that whitelisted warnings actually fails"""

        base_dir = os.path.join(os.path.dirname(os.path.realpath(__file__)),
                                'prospectortest')
        test_files = glob.glob(
            os.path.join(base_dir, 'lib', 'vsc', 'mockinstall', "*.py"))
        test_files = [x for x in test_files if '__init__.py' not in x]
        log.debug("test_files = %s" % test_files)
        log.debug("base_dir = %s" % base_dir)

        failures = commontest.run_prospector(base_dir,
                                             clear_ignore_patterns=True)
        log.debug("Failures = %s" % failures)

        detected_tests = []
        all_tests = []
        for testfile in test_files:
            testfile_base = os.path.splitext(
                os.path.basename(testfile))[0].replace("_", "-")
            all_tests.append(testfile_base)
            for failure in failures:
                if failure['location'][
                        'path'] == testfile and testfile_base in [
                            failure['code'], failure['message']
                        ]:
                    detected_tests.append(testfile_base)

        log.debug("All tests = %s" % all_tests)
        log.info("Detected prospector tests = %s" % detected_tests)
        undetected_tests = [x for x in all_tests if x not in detected_tests]

        if sys.version_info[0] >= 3:
            # some of the prospector test cases don't make sense in Python 3 because they yield syntax errors,
            # or are no longer a problem in Python 3
            py3_invalid_tests = [
                'backtick', 'old-octal-literal', 'import-star-module-level',
                'redefine-in-handler', 'old-raise-syntax', 'print-statement',
                'unpacking-in-except', 'old-ne-operator'
            ]
            undetected_tests = [
                x for x in undetected_tests if x not in py3_invalid_tests
            ]

        self.assertFalse(undetected_tests,
                         "\nprospector did not detect %s\n" % undetected_tests)
Example #15
0
    def test_gen_license_header(self):
        """Test the generation of headers for all supported/known licenses"""

        data = {
            'name': 'projectname',
            'beginyear': 1234,
            'endyear': 5678,
            'url': 'https://github.com/hpcugent/projectname',
        }
        data_brussel = {
            'name': 'projectname',
            'beginyear': 1234,
            'endyear': 5678,
            'url': 'https://github.com/vub-hpc/projectname',
        }
        for license in KNOWN_LICENSES.keys():
            res_fn = os.path.join(self.setup.REPO_TEST_DIR, 'headers', license)
            with open(res_fn) as fh:
                result = fh.read()
            gen_txt = gen_license_header(license, **data)
            self.assertEqual(
                gen_txt,
                result,
                msg='generated header for license %s as expected' % license)
            log.info('generated license header %s' % license)

            gen_txt_bru = gen_license_header(license, **data_brussel)
            self.assertNotRegexpMatches(
                gen_txt_bru,
                'Ghent University',
                msg='No reference to Ghent University in header')
            self.assertNotRegexpMatches(
                gen_txt_bru,
                r'ugent\.be',
                msg='No reference to ugent.be University in header')
            self.assertRegexpMatches(
                gen_txt_bru,
                r'the HPC team of Vrije Universiteit Brussel \(https://hpc.vub.be\)',
                msg='generted header for Brussel is correct for %s' % license)
            self.assertRegexpMatches(
                gen_txt_bru,
                r'support of Vrije Universiteit Brussel \(https://www.vub.be\)',
                msg='generted header for Brussel is correct for %s' % license)
Example #16
0
def get_header(filename, script=False):
    """
    Given filename, retrieve header.
    If script is true, retrieve the shebang

    Header is start of file up to (not incl)
        docstring (first 3 ' or " at begin of line)
        magic comment '### END OF HEADER' at begin of line
    Anything can be part of the header, does not require starting # or something like that

    Return tuple: first element is header, 2nd element shebang if script
    """

    if not os.path.isfile(filename):
        raise Exception('get_header filename %s not found' % filename)

    with open(filename) as fh:
        txt = fh.read()

    blocks = HEADER_REGEXP.split(txt)
    if len(blocks) == 1:
        # no headers, fake blocks with empty header
        blocks = ['', '']
    elif blocks[0] != '':
        # the block before the begin of text is always empty
        raise Exception('unexpected non-empty block with get_header %s: %s' %
                        (filename, blocks))

    header = blocks[1]

    shebang = None
    if script:
        log.info('get_header for script %s', filename)
        lines = header.split("\n")
        if lines[0].startswith('#!/'):
            shebang = lines[0]
            header = "\n".join(lines[1:])

    return header, shebang
Example #17
0
def get_header(filename, script=False):
    """
    Given filename, retrieve header.
    If script is true, retrieve the shebang

    Header is start of file up to (not incl)
        docstring (first 3 ' or " at begin of line)
        magic comment '### END OF HEADER' at begin of line
    Anything can be part of the header, does not require starting # or something like that

    Return tuple: first element is header, 2nd element shebang if script
    """

    if not os.path.isfile(filename):
        raise Exception('get_header filename %s not found' % filename)

    with open(filename) as fh:
        txt = fh.read()

    blocks = HEADER_REGEXP.split(txt)
    if len(blocks) == 1:
        # no headers, fake blocks with empty header
        blocks = ['', '']
    elif blocks[0] != '':
        # the block before the begin of text is always empty
        raise Exception('unexpected non-emtpy block with get_header %s: %s' % (filename, blocks))

    header = blocks[1]

    shebang = None
    if script:
        log.info('get_header for script %s', filename)
        lines = header.split("\n")
        if lines[0].startswith('#!/'):
            shebang = lines[0]
            header = "\n".join(lines[1:])

    return header, shebang
Example #18
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
Example #19
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
Example #20
0
 def lgpl():
     log.info('mocked get_license returns LGPLv2+')
     return 'LGPLv2+', ''
Example #21
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, 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
    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'],
    }

    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

    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
Example #22
0
ARR_TEMPLATE = """#
# Copyright {beginyear}-{endyear} Ghent University
#
# This file is part of {name},
# originally created by the HPC team of Ghent University (http://ugent.be/hpc/en),
# with support of Ghent University (http://ugent.be/hpc),
# the Flemish Supercomputer Centre (VSC) (https://www.vscentrum.be),
# the Flemish Research Foundation (FWO) (http://www.fwo.be/en)
# and the Department of Economy, Science and Innovation (EWI) (http://www.ewi-vlaanderen.be/en).
#
# {url}
#
# All rights reserved.
#
"""

if __name__ == '__main__':
    args = sys.argv[1:]
    try:
        is_script = int(args[-1]) == 1
    except (IndexError, ValueError):
        is_script = False

    if is_script:
        args.pop(-1)

    for fn in args:
        log.info('Going to check_header for file %s (is_script=%s)' % (fn, is_script))
        check_header(fn, script=is_script, write=True)
Example #23
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, 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
    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'],
    }

    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

    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
Example #24
0
ARR_TEMPLATE = """#
# Copyright {beginyear}-{endyear} {university_name}
#
# This file is part of {name},
# originally created by the HPC team of {university_name} ({university_team_url}),
# with support of {university_name} ({university_url}),
# the Flemish Supercomputer Centre (VSC) (https://www.vscentrum.be),
# the Flemish Research Foundation (FWO) (http://www.fwo.be/en)
# and the Department of Economy, Science and Innovation (EWI) (http://www.ewi-vlaanderen.be/en).
#
# {url}
#
# All rights reserved.
#
"""

if __name__ == '__main__':
    args = sys.argv[1:]
    try:
        is_script = int(args[-1]) == 1
    except (IndexError, ValueError):
        is_script = False

    if is_script:
        args.pop(-1)

    for fn in args:
        log.info('Going to check_header for file %s (is_script=%s)' %
                 (fn, is_script))
        check_header(fn, script=is_script, write=True)
Example #25
0
 def lgpl():
     log.info('mocked get_license returns LGPLv2+')
     return 'LGPLv2+', ''