def validate_patch(cls, patchdata): the_patch = patch.fromstring(patchdata) # Strip one level of directories from the left of the filenames. for i, filename in enumerate(the_patch.source): if not '/' in filename: raise IncorrectPatch, 'Attempting to strip one level of slashes from header line "--- %s" left nothing.' % filename the_patch.source[i] = re.sub('^[^/]*/+', '', filename) for i, filename in enumerate(the_patch.target): if not '/' in filename: raise IncorrectPatch, 'Attempting to strip one level of slashes from header line "+++ %s" left nothing.' % filename the_patch.target[i] = re.sub('^[^/]*/+', '', filename) # Go through the files and check that ones that should be mentioned in the patch are so mentioned. path_to_mission_files = os.path.join(get_mission_data_path('diffpatch'), cls.ORIG_DIR) for filename in os.listdir(path_to_mission_files): old_style_filename = filename full_filename = os.path.join(path_to_mission_files, filename) if not os.path.isfile(full_filename): continue old_contents = open(full_filename).read() new_contents = old_contents for old, new in cls.SUBSTITUTIONS: new_contents = new_contents.replace(old, new) if old_contents == new_contents: continue # So it's a file that the patch should modify. try: index = the_patch.source.index(filename) except ValueError: # A file the user was supposed to modify was not included in # the patch. # # We did rename a bunch of the files a little while ago. # Maybe we can process their submission anyway by looking for # the old filename in the patch header. old_style_filename = DiffRecursiveMission.name_new2old(filename) try: index = the_patch.source.index(old_style_filename) except ValueError: raise IncorrectPatch, 'Patch does not modify file "%s", which it should modify.' % filename if (the_patch.target[index] != filename and the_patch.target[index] != old_style_filename): raise IncorrectPatch, 'Patch headers for file "%s" have inconsistent filenames.' % filename hunks = the_patch.hunks[index] del the_patch.source[index] del the_patch.target[index] del the_patch.hunks[index] del the_patch.hunkends[index] # Check that it will apply correctly to the file. if not the_patch._match_file_hunks(full_filename, hunks): # Check for reverse patch by seeing if there is "-firstSubstitute" and "+firstOriginal" in the diff. # (If they did everything perfectly and reversed the patch, there will be two lines following these conditions) if patchdata.find('-'+ cls.SUBSTITUTIONS[1][1]) != -1 and patchdata.find('+'+ cls.SUBSTITUTIONS[1][0]) != -1: raise IncorrectPatch, 'You submitted a patch that would revert the correct changes back to the originals. You may have mixed the parameters for diff, or performed a reverse patch.' else: raise IncorrectPatch, 'The modifications to "%s" will not apply correctly to the original file.' % filename # Check for BOM issues. Likely a Windows-only issue, and only when using a text editor that # includes UTF-8 BOM markers when saving files. if '\xef\xbb\xbf' in ''.join(the_patch.patch_stream(StringIO(old_contents),hunks)): raise IncorrectPatch, 'It appears the text editor you used to modify "%s" leaves UTF-8 BOM characters. Try an editor like Notepad++ or something similar.' % filename # Check that the resulting file matches what is expected. if ''.join(the_patch.patch_stream(StringIO(old_contents), hunks)) != new_contents: raise IncorrectPatch, 'The modifications to "%s" do not result in the correct contents. Make sure you replaced "Aubergine", too!' % filename if len(the_patch.source) != 0: raise IncorrectPatch, 'The patch modifies files that it should not modify: %s' % ', '.join(the_patch.source)
def synthesize_tarball(cls): tdata = StringIO() tfile = tarfile.open(fileobj=tdata, mode='w:gz') tfile.add(os.path.join(get_mission_data_path('diffpatch'), cls.ORIG_DIR), cls.ORIG_DIR) tfile.close() return tdata.getvalue()