Beispiel #1
0
def _format_file(path):
    import platform
    from yapf.yapflib.yapf_api import FormatFile
    config = """{
column_limit : 120
}"""

    try:
        # It might be tempting to use the "inplace" option to
        # FormatFile, but it doesn't do an atomic replace, which
        # is dangerous, so don't use it unless you submit a fix to
        # yapf.
        (contents, encoding, changed) = FormatFile(path, style_config=config)
        if platform.system() == 'Windows':
            # yapf screws up line endings on windows
            with codecs.open(path, 'r', encoding) as file:
                old_contents = file.read()
            contents = contents.replace("\r\n", "\n")
            if len(old_contents) == 0:
                # windows yapf seems to force a newline? I dunno
                contents = ""
            changed = (old_contents != contents)
    except Exception as e:
        error = "yapf crashed on {path}: {error}".format(path=path, error=e)
        print(error, file=sys.stderr)
        return False

    if changed:
        atomic_replace(path, contents, encoding)
        print("Reformatted:     " + path)
        return False
    else:
        return True
Beispiel #2
0
def _update_version_file():
    version_code = ('"""Version information."""\n\n' + '# Note: this is a generated file, edit setup.py not here.\n' +
                    ('version = "%s"\n' % VERSION))
    content = coding_utf8_header + copyright_header + version_code
    try:
        old_content = codecs.open(VERSION_PY, 'r', 'utf-8').read()
    except IOError as e:
        if e.errno == errno.ENOENT:
            old_content = ""
        else:
            raise e
    if old_content != content:
        print("Updating " + VERSION_PY + " with version " + VERSION)
        atomic_replace(VERSION_PY, content, 'utf-8')
Beispiel #3
0
def _update_version_file():
    version_code = ('"""Version information."""\n\n' + '# Note: this is a generated file, edit setup.py not here.\n' +
                    ('version = "%s"\n' % VERSION))
    content = coding_utf8_header + copyright_header + version_code
    try:
        old_content = codecs.open(VERSION_PY, 'r', 'utf-8').read()
    except IOError as e:
        if e.errno == errno.ENOENT:
            old_content = ""
        else:
            raise e
    if old_content != content:
        print("Updating " + VERSION_PY + " with version " + VERSION)
        atomic_replace(VERSION_PY, content, 'utf-8')
Beispiel #4
0
    def _headerize_file(self, path):
        with codecs.open(path, 'r', 'utf-8') as file:
            old_contents = file.read()
        have_coding = (coding_utf8_header in old_contents)
        have_copyright = (copyright_re.search(old_contents) is not None)
        if have_coding and have_copyright:
            return

        if not have_coding:
            print("No encoding header comment in " + path)
            if "encoding_header" not in self.failed:
                self.failed.append("encoding_header")
        if not have_copyright:
            print("No copyright header comment in " + path)
            if "copyright_header" not in self.failed:
                self.failed.append("copyright_header")

        # Note: do NOT automatically change the copyright owner or
        # date.  The copyright owner/date is a statement of legal
        # reality, not a way to create legal reality. All we do
        # here is add an owner/date if there is none; if it's
        # incorrect, the person creating/reviewing the pull
        # request will need to fix it. If there's already an
        # owner/date then we leave it as-is assuming someone
        # has manually chosen it.
        contents = old_contents

        if not have_copyright:
            print("Adding copyright header to: " + path)
            contents = copyright_header + contents

        if not have_coding:
            print("Adding encoding header to: " + path)
            contents = coding_utf8_header + contents

        atomic_replace(path, contents, 'utf-8')
Beispiel #5
0
    def _headerize_file(self, path):
        with codecs.open(path, 'r', 'utf-8') as file:
            old_contents = file.read()
        have_coding = (coding_utf8_header in old_contents)
        have_copyright = (copyright_re.search(old_contents) is not None)
        if have_coding and have_copyright:
            return

        if not have_coding:
            print("No encoding header comment in " + path)
            if "encoding_header" not in self.failed:
                self.failed.append("encoding_header")
        if not have_copyright:
            print("No copyright header comment in " + path)
            if "copyright_header" not in self.failed:
                self.failed.append("copyright_header")

        # Note: do NOT automatically change the copyright owner or
        # date.  The copyright owner/date is a statement of legal
        # reality, not a way to create legal reality. All we do
        # here is add an owner/date if there is none; if it's
        # incorrect, the person creating/reviewing the pull
        # request will need to fix it. If there's already an
        # owner/date then we leave it as-is assuming someone
        # has manually chosen it.
        contents = old_contents

        if not have_copyright:
            print("Adding copyright header to: " + path)
            contents = copyright_header + contents

        if not have_coding:
            print("Adding encoding header to: " + path)
            contents = coding_utf8_header + contents

        atomic_replace(path, contents, 'utf-8')