Skip to content

Commit dc2a274

Browse files
committed
package.py - Renamed: logger -> log
1 parent 90f7b49 commit dc2a274

File tree

1 file changed

+62
-62
lines changed

1 file changed

+62
-62
lines changed

package.py

+62-62
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,8 @@
3434
DEBUG3 = 8
3535

3636
log_handler = None
37-
logger = logging.getLogger()
38-
cmd_logger = logging.getLogger('cmd')
37+
log = logging.getLogger()
38+
cmd_log = logging.getLogger('cmd')
3939

4040

4141
def configure_logging(use_tf_stderr=False):
@@ -71,8 +71,8 @@ def formatMessage(self, record):
7171
log_handler = logging.StreamHandler(stream=log_stream)
7272
log_handler.setFormatter(LogFormatter())
7373

74-
logger.addHandler(log_handler)
75-
logger.setLevel(logging.INFO)
74+
log.addHandler(log_handler)
75+
log.setLevel(logging.INFO)
7676

7777

7878
################################################################################
@@ -88,7 +88,7 @@ def shlex_join(split_command):
8888

8989
def abort(message):
9090
"""Exits with an error message."""
91-
logger.error(message)
91+
log.error(message)
9292
sys.exit(1)
9393

9494

@@ -97,7 +97,7 @@ def cd(path, silent=False):
9797
"""Changes the working directory."""
9898
cwd = os.getcwd()
9999
if not silent:
100-
cmd_logger.info('cd %s', shlex.quote(path))
100+
cmd_log.info('cd %s', shlex.quote(path))
101101
try:
102102
os.chdir(path)
103103
yield
@@ -110,20 +110,20 @@ def tempdir():
110110
"""Creates a temporary directory and then deletes it afterwards."""
111111
prefix = 'terraform-aws-lambda-'
112112
path = tempfile.mkdtemp(prefix=prefix)
113-
cmd_logger.info('mktemp -d %sXXXXXXXX # %s', prefix, shlex.quote(path))
113+
cmd_log.info('mktemp -d %sXXXXXXXX # %s', prefix, shlex.quote(path))
114114
try:
115115
yield path
116116
finally:
117117
shutil.rmtree(path)
118118

119119

120-
def list_files(top_path, logger=None):
120+
def list_files(top_path, log=None):
121121
"""
122122
Returns a sorted list of all files in a directory.
123123
"""
124124

125-
if logger:
126-
logger = logger.getChild('ls')
125+
if log:
126+
log = log.getChild('ls')
127127

128128
results = []
129129

@@ -132,8 +132,8 @@ def list_files(top_path, logger=None):
132132
file_path = os.path.join(root, file_name)
133133
relative_path = os.path.relpath(file_path, top_path)
134134
results.append(relative_path)
135-
if logger:
136-
logger.debug(relative_path)
135+
if log:
136+
log.debug(relative_path)
137137

138138
results.sort()
139139
return results
@@ -205,30 +205,30 @@ def emit_dir_content(base_dir):
205205

206206

207207
def generate_content_hash(source_paths,
208-
hash_func=hashlib.sha256, logger=None):
208+
hash_func=hashlib.sha256, log=None):
209209
"""
210210
Generate a content hash of the source paths.
211211
"""
212212

213-
if logger:
214-
logger = logger.getChild('hash')
213+
if log:
214+
log = log.getChild('hash')
215215

216216
hash_obj = hash_func()
217217

218218
for source_path in source_paths:
219219
if os.path.isdir(source_path):
220220
source_dir = source_path
221-
_logger = logger if logger.isEnabledFor(DEBUG3) else None
222-
for source_file in list_files(source_dir, logger=_logger):
221+
_log = log if log.isEnabledFor(DEBUG3) else None
222+
for source_file in list_files(source_dir, log=_log):
223223
update_hash(hash_obj, source_dir, source_file)
224-
if logger:
225-
logger.debug(os.path.join(source_dir, source_file))
224+
if log:
225+
log.debug(os.path.join(source_dir, source_file))
226226
else:
227227
source_dir = os.path.dirname(source_path)
228228
source_file = os.path.relpath(source_path, source_dir)
229229
update_hash(hash_obj, source_dir, source_file)
230-
if logger:
231-
logger.debug(source_path)
230+
if log:
231+
log.debug(source_path)
232232

233233
return hash_obj
234234

@@ -268,14 +268,14 @@ def __init__(self, zip_filename,
268268
self._compresslevel = compresslevel
269269
self._zip = None
270270

271-
self._logger = logging.getLogger('zip')
271+
self._log = logging.getLogger('zip')
272272

273273
def open(self):
274274
if self._tmp_filename:
275275
raise zipfile.BadZipFile("ZipStream object can't be reused")
276276
self._ensure_base_path(self.filename)
277277
self._tmp_filename = '{}.tmp'.format(self.filename)
278-
self._logger.info("creating '%s' archive", self.filename)
278+
self._log.info("creating '%s' archive", self.filename)
279279
self._zip = zipfile.ZipFile(self._tmp_filename, "w",
280280
self._compress_type)
281281
return self
@@ -304,7 +304,7 @@ def _ensure_base_path(self, zip_filename):
304304
archive_dir = os.path.dirname(zip_filename)
305305

306306
if archive_dir and not os.path.exists(archive_dir):
307-
self._logger.info("creating %s", archive_dir)
307+
self._log.info("creating %s", archive_dir)
308308
os.makedirs(archive_dir)
309309

310310
def write_dirs(self, *base_dirs, prefix=None, timestamp=None):
@@ -313,7 +313,7 @@ def write_dirs(self, *base_dirs, prefix=None, timestamp=None):
313313
"""
314314
self._ensure_open()
315315
for base_dir in base_dirs:
316-
self._logger.info("adding content of directory '%s'", base_dir)
316+
self._log.info("adding content of directory '%s'", base_dir)
317317
for path in emit_dir_content(base_dir):
318318
arcname = os.path.relpath(path, base_dir)
319319
self._write_file(path, prefix, arcname, timestamp)
@@ -338,7 +338,7 @@ def _write_file(self, file_path, prefix=None, name=None, timestamp=None):
338338
arcname = name if name else os.path.basename(file_path)
339339
if prefix:
340340
arcname = os.path.join(prefix, arcname)
341-
self._logger.info("adding '%s'", arcname)
341+
self._log.info("adding '%s'", arcname)
342342
zinfo = self._make_zinfo_from_file(file_path, arcname)
343343
if timestamp is None:
344344
timestamp = self.timestamp
@@ -501,12 +501,12 @@ class ZipContentFilter:
501501
def __init__(self):
502502
self._rules = None
503503
self._excludes = set()
504-
self._logger = logging.getLogger('zip')
504+
self._log = logging.getLogger('zip')
505505

506506
def compile(self, patterns):
507507
rules = []
508508
for p in patterns_list(patterns):
509-
self._logger.debug("pattern '%s'", p)
509+
self._log.debug("pattern '%s'", p)
510510
if p.startswith('!'):
511511
r = re.compile(p[1:])
512512
rules.append((operator.not_, r))
@@ -559,21 +559,21 @@ def emit_file(fpath, opath):
559559
else:
560560
for root, dirs, files in os.walk(path):
561561
o, d = norm_path(path, root)
562-
logger.info('od: %s %s', o, d)
562+
log.info('od: %s %s', o, d)
563563
if root != path:
564564
yield from emit_dir(d, o)
565565
for name in files:
566566
o, f = norm_path(path, root, name)
567-
logger.info('of: %s %s', o, f)
567+
log.info('of: %s %s', o, f)
568568
yield from emit_file(f, o)
569569

570570

571571
class BuildPlanManager:
572572
""""""
573573

574-
def __init__(self, logger=None):
574+
def __init__(self, log=None):
575575
self._source_paths = None
576-
self._logger = logger or logging.root
576+
self._log = log or logging.root
577577

578578
def hash(self, extra_paths):
579579
if not self._source_paths:
@@ -584,9 +584,9 @@ def hash(self, extra_paths):
584584
# Generate a hash based on file names and content. Also use the
585585
# runtime value, build command, and content of the build paths
586586
# because they can have an effect on the resulting archive.
587-
self._logger.debug("Computing content hash on files...")
587+
self._log.debug("Computing content hash on files...")
588588
content_hash = generate_content_hash(content_hash_paths,
589-
logger=self._logger)
589+
log=self._log)
590590
return content_hash
591591

592592
def plan(self, source_path, query):
@@ -723,7 +723,7 @@ def execute(self, build_plan, zip_stream, query):
723723
os.close(w)
724724
sh_work_dir = side_ch.read().strip()
725725
p.wait()
726-
logger.info('WD: %s', sh_work_dir)
726+
log.info('WD: %s', sh_work_dir)
727727
side_ch.close()
728728
elif cmd == 'set:filter':
729729
patterns = action[1]
@@ -745,7 +745,7 @@ def install_pip_requirements(query, zip_stream, requirements_file):
745745

746746
working_dir = os.getcwd()
747747

748-
logger.info('Installing python requirements: %s', requirements_file)
748+
log.info('Installing python requirements: %s', requirements_file)
749749
with tempdir() as temp_dir:
750750
requirements_filename = os.path.basename(requirements_file)
751751
target_file = os.path.join(temp_dir, requirements_filename)
@@ -779,7 +779,7 @@ def install_pip_requirements(query, zip_stream, requirements_file):
779779
pip_cache_dir=pip_cache_dir
780780
))
781781
else:
782-
cmd_logger.info(shlex_join(pip_command))
782+
cmd_log.info(shlex_join(pip_command))
783783
log_handler and log_handler.flush()
784784
check_call(pip_command)
785785

@@ -796,7 +796,7 @@ def docker_build_command(build_root, docker_file=None, tag=None):
796796
docker_cmd.extend(['--tag', tag])
797797
docker_cmd.append(build_root)
798798

799-
cmd_logger.info(shlex_join(docker_cmd))
799+
cmd_log.info(shlex_join(docker_cmd))
800800
log_handler and log_handler.flush()
801801
return docker_cmd
802802

@@ -853,7 +853,7 @@ def docker_run_command(build_root, command, runtime,
853853
docker_cmd.extend([shell, '-c'])
854854
docker_cmd.extend(command)
855855

856-
cmd_logger.info(shlex_join(docker_cmd))
856+
cmd_log.info(shlex_join(docker_cmd))
857857
log_handler and log_handler.flush()
858858
return docker_cmd
859859

@@ -869,15 +869,15 @@ def prepare_command(args):
869869
Outputs a filename and a command to run if the archive needs to be built.
870870
"""
871871

872-
logger = logging.getLogger('prepare')
872+
log = logging.getLogger('prepare')
873873

874874
# Load the query.
875875
query_data = json.load(sys.stdin)
876876

877-
if logger.isEnabledFor(DEBUG3):
878-
logger.debug('ENV: %s', json.dumps(dict(os.environ), indent=2))
879-
if logger.isEnabledFor(DEBUG2):
880-
logger.debug('QUERY: %s', json.dumps(query_data, indent=2))
877+
if log.isEnabledFor(DEBUG3):
878+
log.debug('ENV: %s', json.dumps(dict(os.environ), indent=2))
879+
if log.isEnabledFor(DEBUG2):
880+
log.debug('QUERY: %s', json.dumps(query_data, indent=2))
881881

882882
query = datatree('prepare_query', **query_data)
883883

@@ -891,11 +891,11 @@ def prepare_command(args):
891891
recreate_missing_package = yesno_bool(args.recreate_missing_package)
892892
docker = query.docker
893893

894-
bpm = BuildPlanManager(logger=logger)
894+
bpm = BuildPlanManager(log=log)
895895
build_plan = bpm.plan(source_path, query)
896896

897-
if logger.isEnabledFor(DEBUG2):
898-
logger.debug('BUILD_PLAN: %s', json.dumps(build_plan, indent=2))
897+
if log.isEnabledFor(DEBUG2):
898+
log.debug('BUILD_PLAN: %s', json.dumps(build_plan, indent=2))
899899

900900
# Expand a Terraform path.<cwd|root|module> references
901901
hash_extra_paths = [p.format(path=tf_paths) for p in hash_extra_paths]
@@ -958,12 +958,12 @@ def build_command(args):
958958
Installs dependencies with pip automatically.
959959
"""
960960

961-
logger = logging.getLogger('build')
961+
log = logging.getLogger('build')
962962

963-
if logger.isEnabledFor(DEBUG3):
964-
logger.debug('ENV: %s', json.dumps(dict(os.environ), indent=2))
965-
if logger.isEnabledFor(DEBUG2):
966-
logger.debug('CMD: python3 %s', shlex_join(sys.argv))
963+
if log.isEnabledFor(DEBUG3):
964+
log.debug('ENV: %s', json.dumps(dict(os.environ), indent=2))
965+
if log.isEnabledFor(DEBUG2):
966+
log.debug('CMD: python3 %s', shlex_join(sys.argv))
967967

968968
with open(args.build_plan_file) as f:
969969
query_data = json.load(f)
@@ -979,20 +979,20 @@ def build_command(args):
979979
timestamp = int(_timestamp)
980980

981981
if os.path.exists(filename) and not args.force:
982-
logger.info('Reused: %s', shlex.quote(filename))
982+
log.info('Reused: %s', shlex.quote(filename))
983983
return
984984

985985
# Zip up the build plan and write it to the target filename.
986986
# This will be used by the Lambda function as the source code package.
987987
with ZipWriteStream(filename) as zs:
988-
bpm = BuildPlanManager(logger=logger)
988+
bpm = BuildPlanManager(log=log)
989989
bpm.execute(build_plan, zs, query)
990990

991991
os.utime(filename, ns=(timestamp, timestamp))
992-
logger.info('Created: %s', shlex.quote(filename))
993-
if logger.isEnabledFor(logging.DEBUG):
992+
log.info('Created: %s', shlex.quote(filename))
993+
if log.isEnabledFor(logging.DEBUG):
994994
with open(filename, 'rb') as f:
995-
logger.info('Base64sha256: %s', source_code_hash(f.read()))
995+
log.info('Base64sha256: %s', source_code_hash(f.read()))
996996

997997

998998
def add_hidden_commands(sub_parsers):
@@ -1022,16 +1022,16 @@ def hidden_parser(name, **kwargs):
10221022

10231023
def zip_cmd(args):
10241024
if args.verbose:
1025-
logger.setLevel(logging.DEBUG)
1025+
log.setLevel(logging.DEBUG)
10261026
with ZipWriteStream(args.zipfile) as zs:
10271027
zs.write_dirs(*args.dir, timestamp=args.timestamp)
1028-
if logger.isEnabledFor(logging.DEBUG):
1028+
if log.isEnabledFor(logging.DEBUG):
10291029
zipinfo = shutil.which('zipinfo')
10301030
if zipinfo:
1031-
logger.debug('-' * 80)
1031+
log.debug('-' * 80)
10321032
subprocess.call([zipinfo, args.zipfile])
1033-
logger.debug('-' * 80)
1034-
logger.debug('Source code hash: %s',
1033+
log.debug('-' * 80)
1034+
log.debug('Source code hash: %s',
10351035
source_code_hash(open(args.zipfile, 'rb').read()))
10361036

10371037
p = hidden_parser('zip', help='Zip folder with provided files timestamp')

0 commit comments

Comments
 (0)