11from django .contrib .staticfiles .storage import staticfiles_storage
2- from django .contrib .staticfiles .finders import find
2+ from django .contrib .staticfiles .finders import get_finders , find
33from django .core .files .base import ContentFile
44from django .utils .encoding import smart_bytes
55
@@ -98,11 +98,25 @@ def pack_stylesheets(self, package, **kwargs):
9898 variant = package .variant , ** kwargs )
9999
100100 def compile (self , paths , compiler_options = {}, force = False ):
101- return self .compiler .compile (
101+ paths = self .compiler .compile (
102102 paths ,
103103 compiler_options = compiler_options ,
104104 force = force ,
105105 )
106+ for path in paths :
107+ if not self .storage .exists (path ):
108+ if self .verbose :
109+ print ("Compiled file '%s' cannot be found with packager's storage. Locating it." % path )
110+
111+ source_storage = self .find_source_storage (path )
112+ if source_storage is not None :
113+ with source_storage .open (path ) as source_file :
114+ if self .verbose :
115+ print ("Saving: %s" % path )
116+ self .storage .save (path , source_file )
117+ else :
118+ raise IOError ("File does not exist: %s" % path )
119+ return paths
106120
107121 def pack (self , package , compress , signal , ** kwargs ):
108122 output_filename = package .output_filename
@@ -127,6 +141,15 @@ def pack_templates(self, package):
127141 def save_file (self , path , content ):
128142 return self .storage .save (path , ContentFile (smart_bytes (content )))
129143
144+ def find_source_storage (self , path ):
145+ for finder in get_finders ():
146+ for short_path , storage in finder .list ('' ):
147+ if short_path == path :
148+ if self .verbose :
149+ print ("Found storage: %s" % str (self .storage ))
150+ return storage
151+ return None
152+
130153 def create_packages (self , config ):
131154 packages = {}
132155 if not config :
0 commit comments