Skip to content

Commit

Permalink
start an aim.utils and move md5sum there
Browse files Browse the repository at this point in the history
  • Loading branch information
kteague committed Aug 6, 2019
1 parent 935b7c0 commit 6a6ed25
Show file tree
Hide file tree
Showing 9 changed files with 47 additions and 31 deletions.
3 changes: 2 additions & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,8 @@
'aim.stack_group',
'aim.tests',
'aim.doc',
'aim.application'
'aim.application',
'aim.utils'
],
include_package_data=True,
zip_safe=False,
Expand Down
6 changes: 3 additions & 3 deletions src/aim/application/ec2_launch_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
from aim.models import schemas
from aim.models.locations import get_parent_by_interface
from aim.core.yaml import YAML
from aim.config import aim_context
from aim.utils import md5sum
from aim.core.exception import StackException
from aim.core.exception import AimErrorCode

Expand Down Expand Up @@ -101,9 +101,9 @@ def build(self):
file_path = os.path.join(bundle_folder, bundle_file['name'])
with open(file_path, "w") as output_fd:
output_fd.write(bundle_file['contents'])
contents_md5 += aim_context.md5sum(str_data=bundle_file['contents'])
contents_md5 += md5sum(str_data=bundle_file['contents'])

self.cache_id = aim_context.md5sum(str_data=contents_md5)
self.cache_id = md5sum(str_data=contents_md5)

lb_tar_filename = str.join('.', [bundle_folder, 'tgz'])
lb_tar = tarfile.open(lb_tar_filename, "w:gz")
Expand Down
6 changes: 3 additions & 3 deletions src/aim/cftemplates/cftemplates.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
from aim.core.exception import StackException
from aim.core.exception import AimErrorCode
from aim.stack_group import Stack
from aim.config import aim_context
from aim.utils import md5sum
from aim.models import references
from aim.models.references import Reference
from botocore.exceptions import ClientError
Expand Down Expand Up @@ -327,13 +327,13 @@ def set_parameter( self,
self.parameters.append(param_entry)

def gen_cache_id(self):
template_md5 = aim_context.md5sum(self.get_yaml_path())
template_md5 = md5sum(self.get_yaml_path())
outputs_str = ""
for param_entry in self.parameters:
param_value = param_entry.gen_parameter_value()
outputs_str += param_value

outputs_md5 = aim_context.md5sum(str_data=outputs_str)
outputs_md5 = md5sum(str_data=outputs_str)

return template_md5+outputs_md5

Expand Down
4 changes: 2 additions & 2 deletions src/aim/cftemplates/iam_managed_policies.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
from aim.cftemplates.cftemplates import CFTemplate
from aim.cftemplates.cftemplates import Parameter
from aim.cftemplates.cftemplates import StackOutputParam
from aim.config import aim_context
from aim.utils import md5sum
from io import StringIO
from enum import Enum
import sys
Expand Down Expand Up @@ -142,7 +142,7 @@ def validate(self):

# Generate a name valid in CloudFormation
def gen_policy_name(self, policy_id):
policy_context_hash = aim_context.md5sum(str_data=self.policy_context['ref'])[:8].upper()
policy_context_hash = md5sum(str_data=self.policy_context['ref'])[:8].upper()
policy_name = '-'.join([policy_context_hash, policy_id])
policy_name = self.aim_ctx.normalize_name(policy_name, '-', False)
return policy_name
Expand Down
4 changes: 2 additions & 2 deletions src/aim/cftemplates/iam_roles.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
from io import StringIO
from enum import Enum
import sys
from aim.config import aim_context
from aim.utils import md5sum

class IAMRoles(CFTemplate):
def __init__(self,
Expand Down Expand Up @@ -173,7 +173,7 @@ def validate(self):

# Generate a name valid in CloudFormation
def gen_iam_role_name(self, role_type, role_id):
iam_context_hash = aim_context.md5sum(str_data=self.role_ref)[:8].upper()
iam_context_hash = md5sum(str_data=self.role_ref)[:8].upper()
role_name = '-'.join([iam_context_hash, role_type[0], role_id])
role_name = self.aim_ctx.normalize_name(role_name, '-', False)
return role_name
Expand Down
16 changes: 0 additions & 16 deletions src/aim/config/aim_context.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
import hashlib
import os
import aim.config.aws_credentials
import aim.core.log
import aim.controllers
import aim.models.services
import pkg_resources
from functools import partial
from aim.models import load_project_from_yaml
from aim.models import references
from copy import deepcopy
Expand Down Expand Up @@ -315,17 +313,3 @@ def input(self,

try_again = False
return value

def md5sum(filename=None, str_data=None):
d = hashlib.md5()
if filename != None:
with open(filename, mode='rb') as f:
for buf in iter(partial(f.read, 128), b''):
d.update(buf)
elif str_data != None:
d.update(bytearray(str_data, 'utf-8'))
else:
print("cli: md5sum: Filename or String data expected")
raise StackException(AimErrorCode.Unknown)

return d.hexdigest()
4 changes: 2 additions & 2 deletions src/aim/stack_group/grp_s3.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
from aim.stack_group import StackEnum, StackOrder, Stack, StackGroup, StackHooks
from aim.core.exception import StackException
from aim.core.exception import AimErrorCode
from aim.config import aim_context
from aim.utils import md5sum


class S3StackGroup(StackGroup):
Expand Down Expand Up @@ -36,7 +36,7 @@ def __init__(self,
self.buckets,
self.resource_ref,
None)
s3_template.set_template_file_id(aim_context.md5sum(str_data=resource_ref))
s3_template.set_template_file_id(md5sum(str_data=resource_ref))

# S3 Delete on Stack Delete hook
if self.stack_hooks == None:
Expand Down
4 changes: 2 additions & 2 deletions src/aim/stack_group/stack_group.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
from botocore.exceptions import ClientError
from enum import Enum
from aim.core.yaml import YAML
from aim.config import aim_context
from aim.utils import md5sum
from copy import deepcopy

yaml=YAML(typ="safe", pure=True)
Expand Down Expand Up @@ -46,7 +46,7 @@ def cf_list(self):
return tag_list

def gen_cache_id(self):
return aim_context.md5sum(str_data=yaml.dump(self.tags))
return md5sum(str_data=yaml.dump(self.tags))


class StackOrderItem():
Expand Down
31 changes: 31 additions & 0 deletions src/aim/utils/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
"""
Here be utils.
.%%..%%..%%%%%%..%%%%%%..%%.......%%%%..
.%%..%%....%%......%%....%%......%%.....
.%%..%%....%%......%%....%%.......%%%%..
.%%..%%....%%......%%....%%..........%%.
..%%%%.....%%....%%%%%%..%%%%%%...%%%%..
........................................
"""

import hashlib
from aim.core.exception import StackException
from functools import partial


def md5sum(filename=None, str_data=None):
"""Computes and returns an MD5 sum in hexdigest format on a file or string"""
d = hashlib.md5()
if filename != None:
with open(filename, mode='rb') as f:
for buf in iter(partial(f.read, 128), b''):
d.update(buf)
elif str_data != None:
d.update(bytearray(str_data, 'utf-8'))
else:
print("cli: md5sum: Filename or String data expected")
raise StackException(AimErrorCode.Unknown)

return d.hexdigest()

0 comments on commit 6a6ed25

Please sign in to comment.