def run_pipeline(self, config_file, name, lines): linesep = super().get_linesep(lines) license_template = Config.read_file( os.path.dirname(os.path.abspath(name)), ".styleguide-license") # Get year when file was most recently modified in Git history # # Committer date is used instead of author date (the one shown by "git # log" because the year the file was last modified in the history should # be used. Author dates can be older than this or even out of order in # the log. cmd = ["git", "log", "-n", "1", "--format=%ci", "--", name] last_year = subprocess.run(cmd, stdout=subprocess.PIPE).stdout.decode()[:4] # Check if file has uncomitted changes in the working directory cmd = ["git", "diff-index", "--quiet", "HEAD", "--", name] has_uncommitted_changes = subprocess.run(cmd).returncode # If file hasn't been committed yet or has changes in the working # directory, use current calendar year as end of copyright year range if last_year == "" or has_uncommitted_changes: last_year = str(date.today().year) success, first_year, appendix = self.__try_regex( lines, last_year, license_template) if not success: success, first_year, appendix = self.__try_string_search( lines, last_year, license_template) output = "" # Determine copyright range and trailing padding if first_year != last_year: year_range = first_year + "-" + last_year else: year_range = first_year for line in license_template: # Insert copyright year range line = line.replace("{year}", year_range) # Insert padding which expands to the 80th column. If there is more # than one padding token, the line may contain fewer than 80 # characters due to rounding during the padding width calculation. PADDING_TOKEN = "{padding}" padding_count = line.count(PADDING_TOKEN) if padding_count: padding = 80 - len(line) + len(PADDING_TOKEN) * padding_count padding_width = int(padding / padding_count) line = line.replace(PADDING_TOKEN, " " * padding_width) output += line + linesep # Copy rest of original file into new one output += appendix return output, True
def run_pipeline(self, config_file, name, lines): linesep = Task.get_linesep(lines) license_template = Config.read_file( os.path.dirname(os.path.abspath(name)), ".styleguide-license") success, year, appendix = self.__try_regex(lines, license_template) if not success: success, year, appendix = self.__try_string_search( lines, license_template) output = "" # Determine copyright range and trailing padding if year != self.__current_year: year = year + "-" + self.__current_year for line in license_template: # Insert copyright year range line = line.replace("{year}", year) # Insert padding which expands to the 80th column. If there is more # than one padding token, the line may contain fewer than 80 # characters due to rounding during the padding width calculation. PADDING_TOKEN = "{padding}" padding_count = line.count(PADDING_TOKEN) if padding_count: padding = 80 - len(line) + len(PADDING_TOKEN) * padding_count padding_width = int(padding / padding_count) line = line.replace(PADDING_TOKEN, " " * padding_width) output += line + linesep # Copy rest of original file into new one output += appendix return (output, lines != output, True)
def run_pipeline(self, config_file, name, lines): linesep = Task.get_linesep(lines) license_template = Config.read_file( os.path.dirname(os.path.abspath(name)), ".styleguide-license") # Strip newlines at top of file stripped_lines = lines.lstrip().split(linesep) # If a comment at the beginning of the file is considered a license, it # is replaced with an updated license. Otherwise, a license header is # inserted before it. first_comment_is_license = False license_end = 0 # Regex for tokenizing on comment boundaries token_regex = re.compile("^/\*|\*/|^//") in_multiline_comment = False for line in stripped_lines: # If part of comment contains "Copyright (c)", comment is license. if "Copyright (c)" in line: first_comment_is_license = True line_has_comment = False for match in token_regex.finditer(line): # If any comment token was matched, the line has a comment line_has_comment = True token = match.group() if token == "/*": in_multiline_comment = True elif token == "*/": in_multiline_comment = False if not in_multiline_comment and not line_has_comment: break else: license_end += 1 # If comment at beginning of file is non-empty license, update it if first_comment_is_license and license_end > 0: file_parts = \ [linesep.join(stripped_lines[0:license_end]), linesep + linesep.join(stripped_lines[license_end:]).lstrip()] else: file_parts = ["", linesep + lines.lstrip()] # Default year when none is found is current one year = self.__current_year year_regex = re.compile("Copyright \(c\).*\s(20..)") modify_copyright = False for line in file_parts[0].split(linesep): match = year_regex.search(line) # If license contains copyright pattern, extract the first year if match: year = match.group(1) modify_copyright = True break output = "" # Determine copyright range and trailing padding if modify_copyright and year != self.__current_year: year = year + "-" + self.__current_year for line in license_template: # Insert copyright year range line = line.replace("{year}", year) # Insert padding which expands to the 80th column. If there is more # than one padding token, the line may contain fewer than 80 # characters due to rounding during the padding width calculation. PADDING_TOKEN = "{padding}" padding_count = line.count(PADDING_TOKEN) if padding_count: padding = 80 - len(line) + len(PADDING_TOKEN) * padding_count padding_width = int(padding / padding_count) line = line.replace(PADDING_TOKEN, " " * padding_width) output += line + linesep # Copy rest of original file into new one if len(file_parts) > 1: output += file_parts[1] return (output, lines != output, True)