1# Copyright 2017 The Distro Tracker Developers 

2# See the COPYRIGHT file at the top-level directory of this distribution and 

3# at https://deb.li/DTAuthors 

4# 

5# This file is part of Distro Tracker. It is subject to the license terms 

6# in the LICENSE file found in the top-level directory of this 

7# distribution and at https://deb.li/DTLicense. No part of Distro Tracker, 

8# including this file, may be copied, modified, propagated, or distributed 

9# except according to the terms contained in the LICENSE file. 

10""" 

11Miscellaneous utilities that don't require their own python module. 

12""" 

13 

14import hashlib 

15import json 

16 

17 

18def get_data_checksum(data): 

19 """Checksums a dict, without its prospective 'checksum' key/value.""" 

20 

21 if 'checksum' in data: 

22 to_hash = dict(data) 

23 to_hash.pop('checksum', None) 

24 else: 

25 to_hash = data 

26 

27 json_dump = json.dumps(to_hash, sort_keys=True) 

28 return hashlib.md5(json_dump.encode('utf-8', 'ignore')).hexdigest() 

29 

30 

31def call_methods_with_prefix(obj, prefix, *args, **kwargs): 

32 """ 

33 Identify all the object's methods that start with the given prefix and calls 

34 them in the alphabetical order while passing the remaining arguments as 

35 positional and keywords arguments. 

36 

37 :param object obj: The object instance to inspect 

38 :param str prefix: The prefix used to identify the methods to call 

39 """ 

40 attributes = sorted(filter(lambda x: x.startswith(prefix), dir(obj))) 

41 for name in attributes: 

42 method = getattr(obj, name) 

43 if callable(method): 

44 method(*args, **kwargs)