@@ -359,37 +359,46 @@ def maybe_patch_library(app_configs, **kwargs):
359359 django_path = os .path .dirname (django .__file__ )
360360 library_path = os .path .dirname (django_path )
361361 saved_cwd = os .getcwd ()
362- os .chdir (library_path )
363362 # All patches in settings.CHECKS_LIBRARY_PATCHES_TO_APPLY must have a
364363 # relative file path rooted in the django dir, for instance
365364 # 'django/db/models/fields/__init__.py'
366365 for patch_file in settings .CHECKS_LIBRARY_PATCHES_TO_APPLY :
367- patch_path = os .path .join (saved_cwd , patch_file )
368- patch_set = patch .fromfile (patch_path )
369- if not hasattr (patch_set , 'already_patched' ):
370- patch_set .already_patched = False
371- if patch_set :
372- applied = patch_set .apply ()
373- if not applied :
366+ try :
367+ patch_path = os .path .join (saved_cwd , patch_file )
368+ patch_set = patch .fromfile (patch_path )
369+ if not hasattr (patch_set , 'already_patched' ):
370+ patch_set .already_patched = False
371+ if patch_set :
372+ os .chdir (library_path )
373+ applied = patch_set .apply ()
374+ os .chdir (saved_cwd )
375+ if not applied :
376+ errors .append (checks .Warning (
377+ "Could not apply patch from file '%s'" % patch_file ,
378+ hint = "Make sure that the patch file contains a unified diff and has valid file paths" ,
379+ id = "datatracker.W0002" ,
380+ ))
381+ elif patch_set .already_patched :
382+ #for p in patch_set:
383+ # sys.stderr.write(" Already patched: %s\n" % p.target)
384+ pass
385+ else :
386+ for p in patch_set :
387+ sys .stderr .write (" Patched %s\n " % p .target )
388+ else :
374389 errors .append (checks .Warning (
375- "Could not apply patch from file '%s'" % patch_file ,
376- hint = "Make sure that the patch file contains a unified diff and has valid file paths " ,
377- id = "datatracker.W0002 " ,
390+ "Could not parse patch file '%s'" % patch_file ,
391+ hint = "Make sure that the patch file contains a unified diff" ,
392+ id = "datatracker.W0001 " ,
378393 ))
379- elif patch_set .already_patched :
380- #for p in patch_set:
381- # sys.stderr.write(" Already patched: %s\n" % p.target)
382- pass
383- else :
384- for p in patch_set :
385- sys .stderr .write (" Patched %s\n " % p .target )
386- else :
387- errors .append (checks .Warning (
388- "Could not parse patch file '%s'" % patch_file ,
389- hint = "Make sure that the patch file contains a unified diff" ,
390- id = "datatracker.W0001" ,
391- ))
392- os .chdir (saved_cwd )
394+ except IOError as e :
395+ errors .append (
396+ checks .Warning ("Could not apply patch from %s: %s" % (patch_file , e ),
397+ hint = "Check file permissions and locations" ,
398+ id = "datatracker.W0003" ,
399+ )
400+ )
401+ pass
393402 return errors
394403
395404@checks .register ('security' )
0 commit comments