def fixup()

in xar/py_util.py [0:0]


    def fixup(self, install_paths):
        """
        Deletes .pyc files if the .py file exists (to be recompiled).
        Compiles all Python sources.
        """
        root = os.path.dirname(self.distinfo_location(install_paths))
        # Read the RECORDS
        records = list(self.records())
        # Get a list of Python files (and a set for quick membership tests)
        py_files = [
            os.path.normpath(os.path.join(root, record))
            for record, _, _ in records
            if record.endswith(".py")
        ]
        py_set = set(py_files)
        # Write the new RECORDS file, excluding .pyc files
        new_records = []
        for record_line in records:
            record, _, _ = record_line
            # Add non-pyc files and continue
            is_py = record.endswith(".py")
            if is_py or not any(record.endswith(e) for e in PYTHON_EXTS):
                new_records.append(record_line)
                continue
            # We have a pyc file, delete it if the .py file exists.
            pyc_file = os.path.normpath(os.path.join(root, record))
            try:
                py_file = source_from_cache(pyc_file)
                if py_file in py_set:
                    xar_util.safe_remove(pyc_file)
                    continue
            except ValueError:
                pass
            # There is no .py file, add the .pyc file
            new_records.append(record_line)
        # Compile all the py files
        # Failures are okay since there might be for example Python 3 only code
        errors = compile_files(py_files)
        for _, msg in errors.items():
            logger.warning(msg)
        # Add the new .pyc files relative to root
        new_records += [
            [os.path.relpath(get_pyc_file(f), root), "", ""]
            for f in py_files
            if f not in errors
        ]
        # Write the new RECORDS file
        records_path = os.path.join(root, self.distinfo_name(), self.RECORD)
        with open(records_path, "wt") as f:
            writer = csv.writer(f)
            writer.writerows(new_records)