Samuel Soubeyran

add dependencies and check for existing key in zip before writing new file

...@@ -57,7 +57,7 @@ class Context(object): ...@@ -57,7 +57,7 @@ class Context(object):
57 self.function = kappa.function.Function( 57 self.function = kappa.function.Function(
58 self, self.config['lambda']) 58 self, self.config['lambda'])
59 if 'restapi' in self.config: 59 if 'restapi' in self.config:
60 - self.restapi = kappa.restapi.RestApi( 60 + self.restapi = RestApi(
61 self, self.config['restapi']) 61 self, self.config['restapi'])
62 else: 62 else:
63 self.restapi = None 63 self.restapi = None
......
...@@ -55,6 +55,10 @@ class Function(object): ...@@ -55,6 +55,10 @@ class Function(object):
55 return self._config['handler'] 55 return self._config['handler']
56 56
57 @property 57 @property
58 + def dependencies(self):
59 + return self._config.get('dependencies', list())
60 +
61 + @property
58 def description(self): 62 def description(self):
59 return self._config['description'] 63 return self._config['description']
60 64
...@@ -148,7 +152,8 @@ class Function(object): ...@@ -148,7 +152,8 @@ class Function(object):
148 # changed and needs to be updated so return True. 152 # changed and needs to be updated so return True.
149 changed = True 153 changed = True
150 self._copy_config_file() 154 self._copy_config_file()
151 - self.zip_lambda_function(self.zipfile_name, self._context.source_dir) 155 + files = [] + self.dependencies + [self._context.source_dir]
156 + self.zip_lambda_function(self.zipfile_name, files)
152 m = hashlib.md5() 157 m = hashlib.md5()
153 with open(self.zipfile_name, 'rb') as fp: 158 with open(self.zipfile_name, 'rb') as fp:
154 m.update(fp.read()) 159 m.update(fp.read())
...@@ -196,7 +201,7 @@ class Function(object): ...@@ -196,7 +201,7 @@ class Function(object):
196 LOG.debug('_zip_lambda_dir: lambda_dir=%s', lambda_dir) 201 LOG.debug('_zip_lambda_dir: lambda_dir=%s', lambda_dir)
197 LOG.debug('zipfile_name=%s', zipfile_name) 202 LOG.debug('zipfile_name=%s', zipfile_name)
198 relroot = os.path.abspath(lambda_dir) 203 relroot = os.path.abspath(lambda_dir)
199 - with zipfile.ZipFile(zipfile_name, 'w', 204 + with zipfile.ZipFile(zipfile_name, 'a',
200 compression=zipfile.ZIP_DEFLATED) as zf: 205 compression=zipfile.ZIP_DEFLATED) as zf:
201 for root, subdirs, files in os.walk(lambda_dir): 206 for root, subdirs, files in os.walk(lambda_dir):
202 excluded_dirs = [] 207 excluded_dirs = []
...@@ -206,27 +211,49 @@ class Function(object): ...@@ -206,27 +211,49 @@ class Function(object):
206 excluded_dirs.append(subdir) 211 excluded_dirs.append(subdir)
207 for excluded in excluded_dirs: 212 for excluded in excluded_dirs:
208 subdirs.remove(excluded) 213 subdirs.remove(excluded)
209 - zf.write(root, os.path.relpath(root, relroot)) 214 +
215 + try:
216 + dir_path = os.path.relpath(root, relroot)
217 + dir_path = os.path.normpath(os.path.splitdrive(dir_path)[1])
218 + while dir_path[0] in (os.sep, os.altsep):
219 + dir_path = dir_path[1:]
220 + dir_path += '/'
221 + zf.getinfo(dir_path)
222 + except KeyError:
223 + zf.write(root, dir_path)
224 +
210 for filename in files: 225 for filename in files:
211 if filename not in self.excluded_files: 226 if filename not in self.excluded_files:
212 filepath = os.path.join(root, filename) 227 filepath = os.path.join(root, filename)
213 if os.path.isfile(filepath): 228 if os.path.isfile(filepath):
214 arcname = os.path.join( 229 arcname = os.path.join(
215 os.path.relpath(root, relroot), filename) 230 os.path.relpath(root, relroot), filename)
216 - zf.write(filepath, arcname) 231 + try:
232 + zf.getinfo(arcname)
233 + except KeyError:
234 + zf.write(filepath, arcname)
217 235
218 def _zip_lambda_file(self, zipfile_name, lambda_file): 236 def _zip_lambda_file(self, zipfile_name, lambda_file):
219 LOG.debug('_zip_lambda_file: lambda_file=%s', lambda_file) 237 LOG.debug('_zip_lambda_file: lambda_file=%s', lambda_file)
220 LOG.debug('zipfile_name=%s', zipfile_name) 238 LOG.debug('zipfile_name=%s', zipfile_name)
221 - with zipfile.ZipFile(zipfile_name, 'w', 239 + with zipfile.ZipFile(zipfile_name, 'a',
222 compression=zipfile.ZIP_DEFLATED) as zf: 240 compression=zipfile.ZIP_DEFLATED) as zf:
223 - zf.write(lambda_file) 241 + try:
242 + zf.getinfo(lambda_file)
243 + except KeyError:
244 + zf.write(lambda_file)
224 245
225 - def zip_lambda_function(self, zipfile_name, lambda_fn): 246 + def zip_lambda_function(self, zipfile_name, files):
226 - if os.path.isdir(lambda_fn): 247 + try:
227 - self._zip_lambda_dir(zipfile_name, lambda_fn) 248 + os.remove(zipfile_name)
228 - else: 249 + except OSError:
229 - self._zip_lambda_file(zipfile_name, lambda_fn) 250 + pass
251 + for f in files:
252 + LOG.debug('adding file %s', f)
253 + if os.path.isdir(f):
254 + self._zip_lambda_dir(zipfile_name, f)
255 + else:
256 + self._zip_lambda_file(zipfile_name, f)
230 257
231 def exists(self): 258 def exists(self):
232 return self._get_response() 259 return self._get_response()
......