Skip to content

Commit b8be73f

Browse files
committed
package.py - Added support for comments in patterns
1 parent dc2a274 commit b8be73f

File tree

1 file changed

+22
-8
lines changed

1 file changed

+22
-8
lines changed

package.py

+22-8
Original file line numberDiff line numberDiff line change
@@ -489,23 +489,33 @@ def str_int_to_timestamp(s):
489489
################################################################################
490490
# Building
491491

492-
def patterns_list(patterns):
492+
def patterns_list(args, patterns):
493+
_filter = str.strip
494+
if args.pattern_comments:
495+
def _filter(x):
496+
x = x.strip()
497+
p = re.search("^(.*?)[ \t]*(?:[ \t]{2}#.*)?$", x).group(1).rstrip()
498+
if p.startswith('#'):
499+
return
500+
if p:
501+
return p
493502
if isinstance(patterns, str):
494-
return list(map(str.strip, patterns.splitlines()))
503+
return list(filter(None, map(_filter, patterns.splitlines())))
495504
return patterns
496505

497506

498507
class ZipContentFilter:
499508
""""""
500509

501-
def __init__(self):
510+
def __init__(self, args):
511+
self._args = args
502512
self._rules = None
503513
self._excludes = set()
504514
self._log = logging.getLogger('zip')
505515

506516
def compile(self, patterns):
507517
rules = []
508-
for p in patterns_list(patterns):
518+
for p in patterns_list(self._args, patterns):
509519
self._log.debug("pattern '%s'", p)
510520
if p.startswith('!'):
511521
r = re.compile(p[1:])
@@ -571,7 +581,8 @@ def emit_file(fpath, opath):
571581
class BuildPlanManager:
572582
""""""
573583

574-
def __init__(self, log=None):
584+
def __init__(self, args, log=None):
585+
self._args = args
575586
self._source_paths = None
576587
self._log = log or logging.root
577588

@@ -655,7 +666,7 @@ def commands_step(path, commands):
655666
patterns = claim.get('patterns')
656667
commands = claim.get('commands')
657668
if patterns:
658-
step('set:filter', patterns_list(patterns))
669+
step('set:filter', patterns_list(self._args, patterns))
659670
if commands:
660671
commands_step(path, commands)
661672
else:
@@ -891,7 +902,7 @@ def prepare_command(args):
891902
recreate_missing_package = yesno_bool(args.recreate_missing_package)
892903
docker = query.docker
893904

894-
bpm = BuildPlanManager(log=log)
905+
bpm = BuildPlanManager(args, log=log)
895906
build_plan = bpm.plan(source_path, query)
896907

897908
if log.isEnabledFor(DEBUG2):
@@ -985,7 +996,7 @@ def build_command(args):
985996
# Zip up the build plan and write it to the target filename.
986997
# This will be used by the Lambda function as the source code package.
987998
with ZipWriteStream(filename) as zs:
988-
bpm = BuildPlanManager(log=log)
999+
bpm = BuildPlanManager(args, log=log)
9891000
bpm.execute(build_plan, zs, query)
9901001

9911002
os.utime(filename, ns=(timestamp, timestamp))
@@ -1074,10 +1085,13 @@ def args_parser():
10741085

10751086
def main():
10761087
ns = argparse.Namespace(
1088+
pattern_comments=yesno_bool(os.environ.get(
1089+
'TF_LAMBDA_PACKAGE_PATTERN_COMMENTS', False)),
10771090
recreate_missing_package=os.environ.get(
10781091
'TF_RECREATE_MISSING_LAMBDA_PACKAGE', True),
10791092
log_level=os.environ.get('TF_LAMBDA_PACKAGE_LOG_LEVEL', 'INFO'),
10801093
)
1094+
10811095
p = args_parser()
10821096
args = p.parse_args(namespace=ns)
10831097

0 commit comments

Comments
 (0)