def _sort_on_date(self, patches): ''' Sort patches on date field. The format is like: Fri, 28 Dec 2012 21:00:31 +0200 ''' records = [] for patch in patches: strings = ut.read_strings(ut.join_path(self.patchdir, patch)) for string in strings: if ("Date:" in string): # e.g. 'Date: Wed, 16 Jan 2013 19:09:47 +0000' fields = ut.string_to_words(string[6:].lstrip(' \t')) _, s_day, s_mon, s_year, s_hms, _ = fields s_hour, s_minute, s_second = s_hms.split(':') day, year = int(s_day), int(s_year) hour, minute, second = int(s_hour), int(s_minute), int(s_second) month = self.month_numbers[s_mon] # ignoring microseconds dt = datetime(year, month, day, hour, minute, second) records += [(dt, patch)] break records = sorted(records, key = lambda r: r[0]) patches = [patch for (_, patch) in records] return patches
def list_files(patchpath): """ List the files referenced by a patch, without duplicates Args: patchpath (string) path to patch file Returns: list of filenames Notes: A "filename" is the portion of the file's path after the kernel root, e.g. "drivers/iio/...". """ #-- strings = ut.read_strings(patchpath) files = {} for string in strings: if (string.startswith('diff --git ')): filename = ut.get_string_filename(string) files[filename] = True filenames = sorted(files) # 2to3 return filenames
def list_files(patchpath): """ List the files referenced by a patch, without duplicates Args: patchpath (string) path to patch file Returns: list of filenames Notes: A "filename" is the portion of the file's path after the kernel root, e.g. "drivers/iio/...". """ #-- strings = ut.read_strings(patchpath) files = {} for string in strings: if (string.startswith('diff --git ')): filename = ut.get_string_filename(string) files[filename] = True filenames = sorted(files) # 2to3 return filenames
def _sort_on_date(self, patches): ''' Sort patches on date field. The format is like: Fri, 28 Dec 2012 21:00:31 +0200 ''' records = [] for patch in patches: strings = ut.read_strings(ut.join_path(self.patchdir, patch)) for string in strings: if ("Date:" in string ): # e.g. 'Date: Wed, 16 Jan 2013 19:09:47 +0000' fields = ut.string_to_words(string[6:].lstrip(' \t')) _, s_day, s_mon, s_year, s_hms, _ = fields s_hour, s_minute, s_second = s_hms.split(':') day, year = int(s_day), int(s_year) hour, minute, second = int(s_hour), int(s_minute), int( s_second) month = self.month_numbers[s_mon] # ignoring microseconds dt = datetime(year, month, day, hour, minute, second) records += [(dt, patch)] break records = sorted(records, key=lambda r: r[0]) patches = [patch for (_, patch) in records] return patches
def _iterate(): r = c['sourcedir'] t = ut.join_path(c['tempdir'], 'encoding.dat') w = h.read('walk.txt') for file_ in w: path = ut.join_path(r, file_) s = ut.read_strings(path) ut.write_strings(s, t)
def _iterate(): r = c['sourcedir'] t = ut.join_path(c['tempdir'], 'encoding.dat') w = h.read('walk.txt') for file_ in w: path = ut.join_path(r, file_) s = ut.read_strings(path) ut.write_strings(s, t)
def read(self, filepath): """ Read data from path as a sequence of '\\n' terminated lines. Convert the lines to a list of strings Args: filepath (string): file path Notes: This function is used to input data output by the Checker, etc. """ return ut.read_strings(filepath)
def read(self, filepath): """ Read data from path as a sequence of '\\n' terminated lines. Convert the lines to a list of strings Args: filepath (string): file path Notes: This function is used to input data output by the Checker, etc. """ return ut.read_strings(filepath)
def _segments(self): ''' Use generator to split the archive file into diff segments ''' index = begin = 0 curr_seg = [] for string in ut.read_strings(self._path): if (string.startswith('diff ')): if (len(curr_seg) > 0): yield (begin, curr_seg) begin = index curr_seg = [string] else: curr_seg += [string] index += 1
def convert_api_data(config): srcroot = config['srcroot'] sources = config['sources'] data = [] for source in sources: print('extract: ' + source) path = ut.join_path(srcroot, source) strings = Strings(ut.read_strings(path)) sections = strings.extract('#++','#--') if (len(sections) > 0): sections = [conv_section(section) for section in sections] data += Strings.join(sections) else: pass ut.write_strings(data, './_rest/api.txt')
def _match_patterns(self, path): ''' Search files in self.paths for match to self.matcher. ''' filepath = ut.join_path(self.root_path, path) strings = ut.read_strings(filepath) for index in range(len(strings)): text = strings[index] if (self.debug > 1): print(' "%s"' % text) pattern = self.matcher(text) if (pattern is not None): if (self.trim_paths): path_ = path else: path_ = filepath if (pattern not in self.matches): self.matches[pattern] = [] self.matches[pattern] += [(path_, index + 1, text.lstrip())]
def _match_patterns(self, path): ''' Search files in self.paths for match to self.matcher. ''' filepath = ut.join_path(self.root_path, path) strings = ut.read_strings(filepath) for index in range(len(strings)): text = strings[index] if (self.debug > 1): print(' "%s"' % text) pattern = self.matcher(text) if (pattern is not None): if (self.trim_paths): path_ = path else: path_ = filepath if (pattern not in self.matches): self.matches[pattern] = [] self.matches[pattern] += [(path_, index + 1, text.lstrip())]
def __init__(self, path): """ Constructor Args: path (string): path to patch file Raises: PT_ParameterError PT_NotFoundError Notes: Commented out diff and hunk sections are omitted. """ #-- self.name = 'Patch' if (not ut.is_string_type(path)): raise PT_ParameterError(self.name, path) if (not ut.is_file(path)): raise PT_NotFoundError(self.name, path) strings = Strings(ut.read_strings(path)) strings = strings.discard('"""', '"""') # Split any email header from the patch data (_, body) = strings.partition('diff -') if (body is None): #all diffs commented out? self.diffs = [] self.patch_type = 'text' self.patch_mode = 'git' print "body is none" elif (body[0].startswith('diff --git ')): # Split any email footer from the patch data (body, _) = body.rpartition('-- ') self._parse_body(body, 'diff --git ') self.patch_mode = 'git' print "git diff" else: self._parse_body(body, 'diff -') self.patch_mode = 'urn' print "urn"
def _check(self, patchpath): self._misc_msg('\nPATCH: "%s"' % patchpath) patchpath = ut.join_path(self.patchdir, patchpath) pdata = Patch(patchpath) if (len(pdata.diffs) == 0): self._info_msg('skipping empty/commented patch', 1) return 0 if (pdata.patch_type == 'binary'): self._info_msg('skipping binary patch', 1) return -1 errors = 0 for diff in pdata.diffs: self._misc_msg('DIFF: "%s"' % diff.spec, 1) if (not self._check_paths(diff)): errors += 1 continue if (diff.old_path == '/dev/null'): # Can't fail on adding lines to a new file continue old_lines = ut.read_strings(ut.join_path(self.sourcedir, diff.old_path)) for hunk in diff.hunks: self._misc_msg('HUNK: "%s"' % hunk.spec, 2) edits = hunk.edits start = hunk.old_start count = hunk.old_count tag = 'old' note = hunk.note if (not self._check_hunk_format(start, count, len(old_lines), tag)): errors += 1 continue errors += self._check_hunk_edits(diff.old_path, edits, start, count, note, old_lines) self._info_msg("%d patch errors" % errors, 1) return errors
def _get_diffs(self, archive, filenames): ''' Generate list of diff sections that refer to files in filenames ''' strings = ut.read_strings(archive) diff = [] for index in range(len(strings)): string = strings[index] if (string.lstrip().startswith('diff --git ')): if (len(diff) > 0): yield diff filename = ut.get_string_filename(string) if (filename in filenames): diff = ["%04d: %s" % (index + 1, string)] elif (len(diff) > 0): diff += ["%04d: %s" % (index + 1, string)] if (len(diff) > 0): yield diff
def _get_diffs(self, archive, filenames): ''' Generate list of diff sections that refer to files in filenames ''' strings = ut.read_strings(archive) diff = [] for index in range(len(strings)): string = strings[index] if (string.lstrip().startswith('diff --git ')): if (len(diff) > 0): yield diff filename = ut.get_string_filename(string) if (filename in filenames): diff = ["%04d: %s" % (index + 1, string)] elif (len(diff) > 0): diff += ["%04d: %s" % (index + 1, string)] if (len(diff) > 0): yield diff
def __init__(self, path): """ Constructor Args: path (string): path to patch file Raises: PT_ParameterError PT_NotFoundError Notes: Commented out diff and hunk sections are omitted. """ #-- self.name = 'Patch' if (not ut.is_string_type(path)): raise PT_ParameterError(self.name, path) if (not ut.is_file(path)): raise PT_NotFoundError(self.name, path) strings = Strings(ut.read_strings(path)) strings = strings.discard('"""','"""') # Split any email header from the patch data (_, body) = strings.partition('diff -') if (body is None): #all diffs commented out? self.diffs = [] self.patch_type = 'text' self.patch_mode = 'git' elif (body[0].startswith('diff --git ')): # Split any email footer from the patch data (body, _) = body.rpartition('-- ') self._parse_body(body, 'diff --git ') self.patch_mode = 'git' else: self._parse_body(body, 'diff -') self.patch_mode = 'urn'
def _get_data(self): ''' Scan patchset to get two mappings: filedata maps filenames to patches patchdata maps patches to filenames ''' filedata = {} patchdata = {} for patchname in self.get_patch_names(): filelist = [] strings = ut.read_strings(ut.join_path(self.patchdir, patchname)) for index in range(len(strings)): if (strings[index].startswith('diff --git ')): filename = ut.get_string_filename(strings[index]) if (filename in filedata): filedata[filename] += [(patchname, index)] else: filedata[filename] = [(patchname, index)] filelist += [(filename, index)] patchdata[patchname] = filelist self.filedata = filedata self.patchdata = patchdata
def _get_data(self): ''' Scan patchset to get two mappings: filedata maps filenames to patches patchdata maps patches to filenames ''' filedata = {} patchdata = {} for patchname in self.get_patch_names(): filelist = [] strings = ut.read_strings(ut.join_path(self.patchdir, patchname)) for index in range(len(strings)): if (strings[index].startswith('diff --git ')): filename = ut.get_string_filename(strings[index]) if (filename in filedata): filedata[filename] += [(patchname, index)] else: filedata[filename] = [(patchname, index)] filelist += [(filename, index)] patchdata[patchname] = filelist self.filedata = filedata self.patchdata = patchdata
def _check(self, patchpath): self._misc_msg('\nPATCH: "%s"' % patchpath) patchpath = ut.join_path(self.patchdir, patchpath) # print patchpath pdata = Patch(patchpath) if (len(pdata.diffs) == 0): self._info_msg('skipping empty/commented patch', 1) return 0 if (pdata.patch_type == 'binary'): self._info_msg('skipping binary patch', 1) return -1 errors = 0 context = [] logs = "" for diff in pdata.diffs: # print "a path :" + diff.a_path # print "old path :" + diff.old_path # print "new path :" + diff.new_path self._misc_msg('DIFF: "%s"' % diff.spec, 1) if (not self._check_paths(diff)): errors += 1 continue if (diff.old_path == '/dev/null' ): # Can't fail on adding lines to a new file continue # print "old file path :" + ut.join_path(self.sourcedir, diff.old_path) old_lines = ut.read_strings( ut.join_path(self.sourcedir, diff.old_path)) new_hunks = [] new_diff = [] for hunk in diff.hunks: self._misc_msg('HUNK: "%s"' % hunk.spec, 2) edits = hunk.edits start = hunk.old_start count = hunk.old_count tag = 'old' note = hunk.note if (not self._check_hunk_format(start, count, len(old_lines), tag)): errors += 1 continue # print "begin to change hunk edits" # errors += self._check_hunk_edits(diff.old_path, edits, start, count, note, old_lines) #获取并加入新的hunk信息 new_hunks.append( self._patch_hunk_edits(diff.old_path, edits, start, count, note, old_lines)) # print "end for this hunk" #写入当前hunk new_diff.append(diff.spec + "\n") # for (new_edits,new_start,context_start,new_count,context_count) in new_hunks: # new_diff.append("@@ " + str(new_start) + "," + str(context_count) + " " + str(context_start) + "," + str(new_count) + "@@\n") # for edit in new_edits: # new_diff.append(edit + "\n") # # context.append("".join(new_diff)) for (new_place, before_start, before_count, after_start, after_count, log) in new_hunks: new_diff.append("@@ -" + str(before_start) + "," + str(before_count) + " +" + str(after_start) + "," + str(after_count) + "@@\n") for (edit, new_line_current, new_line_edited) in new_place: new_diff.append(edit + "\n") logs += log context.append("".join(new_diff)) # print new_diff #将新的diffs信息写入文件 f = file("new.patch", "w") # print context f.write("".join(context)) f.close() logfile = file("log.txt", "w") logfile.write("".join(logs)) logfile.close() self._info_msg("%d patch errors" % errors, 1) return errors