Example #1
0
    def check(self, filename, regions=all_regions):
        f = self.open(filename, 'r')

        lines = [ l.rstrip('\n') for l in f.xreadlines() ]
        old = ''.join(line + '\n' for line in lines)
        f.close()

        if len(lines) == 0:
            return 0

        language = lang_type(filename, lines[0])
        sort_lines = list(self.sort_includes(lines, filename, language))
        new = ''.join(line + '\n' for line in sort_lines)

        mod = modified_regions(old, new)
        modified = mod & regions

        if modified:
            self.write("invalid sorting of includes in %s\n" % (filename))
            if self.ui.verbose:
                for start, end in modified.regions:
                    self.write("bad region [%d, %d)\n" % (start, end))
            return 1

        return 0
Example #2
0
    def check(self, filename, regions=all_regions, fobj=None, silent=False):
        close = False
        if fobj is None:
            fobj = self.open(filename, 'r')
            close = True
        norm_fname = self.normalize_filename(filename)

        old = [ l.rstrip('\n') for l in fobj.xreadlines() ]
        if close:
            fobj.close()

        if len(old) == 0:
            return 0

        language = lang_type(filename, old[0])
        new = list(self.sort_includes(old, norm_fname, language))

        modified = _modified_regions(old, new) & regions

        if modified:
            if not silent:
                self.ui.write("invalid sorting of includes in %s\n"
                                % (filename))
                if self.ui.verbose:
                    for start, end in modified.regions:
                        self.ui.write("bad region [%d, %d)\n" % (start, end))
            return 1

        return 0
Example #3
0
    def check(self, filename, regions=all_regions, fobj=None, silent=False):
        close = False
        if fobj is None:
            fobj = self.open(filename, 'r')
            close = True
        norm_fname = self.normalize_filename(filename)

        old = [ l.rstrip('\n') for l in fobj.xreadlines() ]
        if close:
            fobj.close()

        if len(old) == 0:
            return 0

        language = lang_type(filename, old[0])
        new = list(self.sort_includes(old, norm_fname, language))

        modified = _modified_regions(old, new) & regions

        if modified:
            if not silent:
                self.ui.write("invalid sorting of includes in %s\n"
                                % (filename))
                if self.ui.verbose:
                    for start, end in modified.regions:
                        self.ui.write("bad region [%d, %d)\n" % (start, end))
            return 1

        return 0
Example #4
0
    def check(self, filename, regions=all_regions):
        f = self.open(filename, 'r')

        lines = [l.rstrip('\n') for l in f.xreadlines()]
        old = ''.join(line + '\n' for line in lines)
        f.close()

        if len(lines) == 0:
            return 0

        language = lang_type(filename, lines[0])
        sort_lines = list(self.sort_includes(lines, filename, language))
        new = ''.join(line + '\n' for line in sort_lines)

        mod = modified_regions(old, new)
        modified = mod & regions

        if modified:
            self.write("invalid sorting of includes\n")
            if self.ui.verbose:
                for start, end in modified.regions:
                    self.write("bad region [%d, %d)\n" % (start, end))
            return 1

        return 0
Example #5
0
 def skip(self, filename):
     # We never want to handle symlinks, so always skip them: If the location
     # pointed to is a directory, skip it. If the location is a file inside
     # the gem5 directory, it will be checked as a file, so symlink can be
     # skipped. If the location is a file outside gem5, we don't want to
     # check it anyway.
     if os.path.islink(filename):
         return True
     return lang_type(filename) not in self.languages
Example #6
0
 def skip(self, filename):
     # We never want to handle symlinks, so always skip them: If the
     # location pointed to is a directory, skip it. If the location is a
     # file inside the gem5 directory, it will be checked as a file, so
     # symlink can be skipped. If the location is a file outside gem5, we
     # don't want to check it anyway.
     if os.path.islink(filename):
         return True
     return lang_type(filename) not in self.languages
Example #7
0
    def fix(self, filename, regions=all_regions):
        f = self.open(filename, 'r+')

        old = f.readlines()
        lines = [ l.rstrip('\n') for l in old ]
        language = lang_type(filename, lines[0])
        sort_lines = list(self.sort_includes(lines, filename, language))
        new = ''.join(line + '\n' for line in sort_lines)

        f.seek(0)
        f.truncate()

        for i,line in enumerate(sort_lines):
            f.write(line)
            f.write('\n')
        f.close()
Example #8
0
    def fix(self, filename, regions=all_regions):
        f = self.open(filename, 'r+')

        old = f.readlines()
        lines = [l.rstrip('\n') for l in old]
        language = lang_type(filename, lines[0])
        sort_lines = list(self.sort_includes(lines, filename, language))
        new = ''.join(line + '\n' for line in sort_lines)

        f.seek(0)
        f.truncate()

        for i, line in enumerate(sort_lines):
            f.write(line)
            f.write('\n')
        f.close()
Example #9
0
    def check(self, filename, regions=all_regions):
        f = self.open(filename, 'r')

        lang = lang_type(filename)
        assert lang in self.languages

        errors = 0
        for num, line in enumerate(f):
            if num not in regions:
                continue
            line = line.rstrip('\n')
            if not self.check_line(line, language=lang):
                self.ui.write("invalid %s in %s:%d\n" % \
                              (self.test_name, filename, num + 1))
                if self.ui.verbose:
                    self.ui.write(">>%s<<\n" % line[:-1])
                errors += 1
        f.close()
        return errors
Example #10
0
    def check(self, filename, regions=all_regions):
        f = self.open(filename, 'r')

        lang = lang_type(filename)
        assert lang in self.languages

        errors = 0
        for num,line in enumerate(f):
            if num not in regions:
                continue
            line = line.rstrip('\n')
            if not self.check_line(line, language=lang):
                self.ui.write("invalid %s in %s:%d\n" % \
                              (self.test_name, filename, num + 1))
                if self.ui.verbose:
                    self.ui.write(">>%s<<\n" % line[:-1])
                errors += 1
        f.close()
        return errors
Example #11
0
    def fix(self, filename, regions=all_regions):
        f = self.open(filename, 'r+')

        lang = lang_type(filename)
        assert lang in self.languages

        lines = list(f)

        f.seek(0)
        f.truncate()

        for i,line in enumerate(lines):
            line = line.rstrip('\n')
            if i in regions:
                line = self.fix_line(line, language=lang)

            f.write(line)
            f.write("\n")
        f.close()
        self.current_language = None
Example #12
0
    def fix(self, filename, regions=all_regions):
        f = self.open(filename, 'r+')

        lang = lang_type(filename)
        assert lang in self.languages

        lines = list(f)

        f.seek(0)
        f.truncate()

        for i, line in enumerate(lines):
            line = line.rstrip('\n')
            if i in regions:
                line = self.fix_line(line, language=lang)

            f.write(line)
            f.write("\n")
        f.close()
        self.current_language = None
Example #13
0
    def check(self, filename, regions=all_regions):
        f = self.open(filename, 'r')
        norm_fname = self.normalize_filename(filename)

        old = [l.rstrip('\n') for l in f.xreadlines()]
        f.close()

        if len(old) == 0:
            return 0

        language = lang_type(filename, old[0])
        new = list(self.sort_includes(old, norm_fname, language))

        modified = _modified_regions(old, new) & regions

        if modified:
            self.ui.write("invalid sorting of includes in %s\n" % (filename))
            if self.ui.verbose:
                for start, end in modified.regions:
                    self.ui.write("bad region [%d, %d)\n" % (start, end))
            return 1

        return 0
Example #14
0
    def check(self, filename, regions=all_regions):
        f = self.open(filename, 'r')
        norm_fname = self.normalize_filename(filename)

        old = [ l.rstrip('\n') for l in f.xreadlines() ]
        f.close()

        if len(old) == 0:
            return 0

        language = lang_type(filename, old[0])
        new = list(self.sort_includes(old, norm_fname, language))

        modified = _modified_regions(old, new) & regions

        if modified:
            self.ui.write("invalid sorting of includes in %s\n" % (filename))
            if self.ui.verbose:
                for start, end in modified.regions:
                    self.ui.write("bad region [%d, %d)\n" % (start, end))
            return 1

        return 0
Example #15
0
    def check(self, filename, regions=all_regions, fobj=None, silent=False):
        close = False
        if fobj is None:
            fobj = self.open(filename, 'r')
            close = True

        lang = lang_type(filename)
        assert lang in self.languages

        errors = 0
        for num,line in enumerate(fobj):
            if num not in regions:
                continue
            line = line.rstrip('\n')
            if not self.check_line(line, language=lang):
                if not silent:
                    self.ui.write("invalid %s in %s:%d\n" % \
                                  (self.test_name, filename, num + 1))
                    if self.ui.verbose:
                        self.ui.write(">>%s<<\n" % line[:-1])
                errors += 1
        if close:
            fobj.close()
        return errors
Example #16
0
    def check(self, filename, regions=all_regions, fobj=None, silent=False):
        close = False
        if fobj is None:
            fobj = self.open(filename, 'r')
            close = True

        lang = lang_type(filename)
        assert lang in self.languages

        errors = 0
        for num,line in enumerate(fobj):
            if num not in regions:
                continue
            line = line.rstrip('\n')
            if not self.check_line(line, language=lang):
                if not silent:
                    self.ui.write("invalid %s in %s:%d\n" % \
                                  (self.test_name, filename, num + 1))
                    if self.ui.verbose:
                        self.ui.write(">>%s<<\n" % line[:-1])
                errors += 1
        if close:
            fobj.close()
        return errors
Example #17
0
def validate(filename, stats, verbose, exit_code):
    if lang_type(filename) not in format_types:
        return

    def msg(lineno, line, message):
        print '%s:%d>' % (filename, lineno + 1), message
        if verbose > 2:
            print line

    def bad():
        if exit_code is not None:
            sys.exit(exit_code)

    try:
        f = file(filename, 'r')
    except OSError:
        if verbose > 0:
            print 'could not open file %s' % filename
        bad()
        return

    for i, line in enumerate(f):
        line = line.rstrip('\n')

        # no carriage returns
        if line.find('\r') != -1:
            self.cret += 1
            if verbose > 1:
                msg(i, line, 'carriage return found')
            bad()

        # lines max out at 79 chars
        llen = linelen(line)
        if llen > 79:
            stats.toolong += 1
            if llen == 80:
                stats.toolong80 += 1
            if verbose > 1:
                msg(i, line, 'line too long (%d chars)' % llen)
            bad()

        # no tabs used to indent
        match = lead.search(line)
        if match and match.group(1).find('\t') != -1:
            stats.leadtabs += 1
            if verbose > 1:
                msg(i, line, 'using tabs to indent')
            bad()

        # no trailing whitespace
        if trail.search(line):
            stats.trailwhite += 1
            if verbose > 1:
                msg(i, line, 'trailing whitespace')
            bad()

        # for c++, exactly one space betwen if/while/for and (
        if cpp:
            match = any_control.search(line)
            if match and not good_control.search(line):
                stats.badcontrol += 1
                if verbose > 1:
                    msg(i, line, 'improper spacing after %s' % match.group(1))
                bad()
Example #18
0
 def skip(self, filename):
     return lang_type(filename) not in self.languages
Example #19
0
 def skip(self, filename):
     return lang_type(filename) not in self.languages
Example #20
0
    except getopt.GetoptError:
        usage(1)

    for o,a in opts:
        if o == '-c':
            show_counts = True
        if o == '-i':
            ignore.add(a)
        if o == '-v':
            verbose = True

    files = []

    for base in args:
        if os.path.isfile(base):
            files += [ (base, lang_type(base)) ]
        elif os.path.isdir(base):
            files += find_files(base)
        else:
            raise AttributeError, "can't access '%s'" %  base

    copyrights = {}
    counts = {}

    for filename, lang in files:
        f = file(filename, 'r')
        lines = f.readlines()
        if not lines:
            continue

        lines = [ line.rstrip('\r\n') for line in lines ]
Example #21
0
    except getopt.GetoptError:
        usage(1)

    for o,a in opts:
        if o == '-c':
            show_counts = True
        if o == '-i':
            ignore.add(a)
        if o == '-v':
            verbose = True

    files = []

    for base in args:
        if os.path.isfile(base):
            files += [ (base, lang_type(base)) ]
        elif os.path.isdir(base):
            files += find_files(base)
        else:
            raise AttributeError("can't access '%s'" %  base)

    copyrights = {}
    counts = {}

    for filename, lang in files:
        f = file(filename, 'r')
        lines = f.readlines()
        if not lines:
            continue

        lines = [ line.rstrip('\r\n') for line in lines ]
Example #22
0
def validate(filename, stats, verbose, exit_code):
    lang = lang_type(filename)
    if lang not in format_types:
        return

    def msg(lineno, line, message):
        print '%s:%d>' % (filename, lineno + 1), message
        if verbose > 2:
            print line

    def bad():
        if exit_code is not None:
            sys.exit(exit_code)

    try:
        f = file(filename, 'r')
    except OSError:
        if verbose > 0:
            print 'could not open file %s' % filename
        bad()
        return

    for i,line in enumerate(f):
        line = line.rstrip('\n')

        # no carriage returns
        if line.find('\r') != -1:
            self.cret += 1
            if verbose > 1:
                msg(i, line, 'carriage return found')
            bad()

        # lines max out at 79 chars
        llen = linelen(line)
        if llen > 79:
            stats.toolong += 1
            if llen == 80:
                stats.toolong80 += 1
            if verbose > 1:
                msg(i, line, 'line too long (%d chars)' % llen)
            bad()

        # no tabs used to indent
        match = lead.search(line)
        if match and match.group(1).find('\t') != -1:
            stats.leadtabs += 1
            if verbose > 1:
                msg(i, line, 'using tabs to indent')
            bad()

        # no trailing whitespace
        if trail.search(line):
            stats.trailwhite +=1
            if verbose > 1:
                msg(i, line, 'trailing whitespace')
            bad()

        # for c++, exactly one space betwen if/while/for and (
        if lang == 'C++':
            match = any_control.search(line)
            if match and match.group(2) != " ":
                stats.badcontrol += 1
                if verbose > 1:
                    msg(i, line, 'improper spacing after %s' % match.group(1))
                bad()
Example #23
0
def validate(filename, stats, verbose, exit_code):
    if lang_type(filename) not in format_types:
        return

    def msg(lineno, line, message):
        print "%s:%d>" % (filename, lineno + 1), message
        if verbose > 2:
            print line

    def bad():
        if exit_code is not None:
            sys.exit(exit_code)

    try:
        f = file(filename, "r")
    except OSError:
        if verbose > 0:
            print "could not open file %s" % filename
        bad()
        return

    for i, line in enumerate(f):
        line = line.rstrip("\n")

        # no carriage returns
        if line.find("\r") != -1:
            self.cret += 1
            if verbose > 1:
                msg(i, line, "carriage return found")
            bad()

        # lines max out at 79 chars
        llen = linelen(line)
        if llen > 79:
            stats.toolong += 1
            if llen == 80:
                stats.toolong80 += 1
            if verbose > 1:
                msg(i, line, "line too long (%d chars)" % llen)
            bad()

        # no tabs used to indent
        match = lead.search(line)
        if match and match.group(1).find("\t") != -1:
            stats.leadtabs += 1
            if verbose > 1:
                msg(i, line, "using tabs to indent")
            bad()

        # no trailing whitespace
        if trail.search(line):
            stats.trailwhite += 1
            if verbose > 1:
                msg(i, line, "trailing whitespace")
            bad()

        # for c++, exactly one space betwen if/while/for and (
        if cpp:
            match = any_control.search(line)
            if match and not good_control.search(line):
                stats.badcontrol += 1
                if verbose > 1:
                    msg(i, line, "improper spacing after %s" % match.group(1))
                bad()