| OLD | NEW |
| 1 # Copyright 2016 The Chromium Authors. All rights reserved. | 1 # Copyright 2016 The Chromium Authors. All rights reserved. |
| 2 # Use of this source code is governed by a BSD-style license that can be | 2 # Use of this source code is governed by a BSD-style license that can be |
| 3 # found in the LICENSE file. | 3 # found in the LICENSE file. |
| 4 | 4 |
| 5 """Util functions for git repository processing.""" | 5 """Util functions for git repository processing.""" |
| 6 | 6 |
| 7 import base64 | 7 import base64 |
| 8 import hashlib | 8 import hashlib |
| 9 import json | 9 import json |
| 10 import logging | 10 import logging |
| 11 import os | 11 import os |
| 12 import pickle | 12 import pickle |
| 13 import re | 13 import re |
| 14 import subprocess | 14 import subprocess |
| 15 import traceback | 15 import traceback |
| 16 import urllib2 | 16 import urllib2 |
| 17 | 17 |
| 18 import dev_appserver | 18 import dev_appserver |
| 19 dev_appserver.fix_sys_path() | 19 dev_appserver.fix_sys_path() |
| 20 | 20 |
| 21 from common import appengine_util | 21 from common import appengine_util |
| 22 | 22 |
| 23 # TODO(katesonia): move host to azalea host after migration. | 23 # TODO(katesonia): move host to azalea host after migration. |
| 24 _FEEDBACK_URL_TEMPLATE = 'host/crash/fracas-result-feedback?key=%s' | 24 _FEEDBACK_URL_TEMPLATE = 'host/crash/fracas-result-feedback?key=%s' |
| 25 GIT_HASH_PATTERN = re.compile(r'^[0-9a-fA-F]{40}$') | 25 GIT_HASH_PATTERN = re.compile(r'^[0-9a-fA-F]{40}$') |
| 26 | 26 |
| 27 | 27 |
| 28 def GenerateFileName(*args): | 28 # TODO(crbug.com/662540): Add unittests. |
| 29 def GenerateFileName(*args): # pragma: no cover |
| 29 """Encodes args and returns the generated result file.""" | 30 """Encodes args and returns the generated result file.""" |
| 30 return hashlib.md5(pickle.dumps(args)).hexdigest() | 31 return hashlib.md5(pickle.dumps(args)).hexdigest() |
| 31 | 32 |
| 32 | 33 |
| 33 def IsGitHash(revision): | 34 # TODO(crbug.com/662540): Add unittests. |
| 35 def IsGitHash(revision): # pragma: no cover |
| 34 return GIT_HASH_PATTERN.match(str(revision)) or revision.lower() == 'master' | 36 return GIT_HASH_PATTERN.match(str(revision)) or revision.lower() == 'master' |
| 35 | 37 |
| 36 | 38 |
| 37 def ParseGitHash(revision): | 39 # TODO(crbug.com/662540): Add unittests. |
| 40 def ParseGitHash(revision): # pragma: no cover |
| 38 """Gets git hash of a revision.""" | 41 """Gets git hash of a revision.""" |
| 39 if IsGitHash(revision): | 42 if IsGitHash(revision): |
| 40 return revision | 43 return revision |
| 41 | 44 |
| 42 try: | 45 try: |
| 43 # Can parse revision like 'HEAD', 'HEAD~3'. | 46 # Can parse revision like 'HEAD', 'HEAD~3'. |
| 44 return subprocess.check_output( | 47 return subprocess.check_output( |
| 45 ['git', 'rev-parse', revision]).replace('\n', '') | 48 ['git', 'rev-parse', revision]).replace('\n', '') |
| 46 except: # pylint: disable=W | 49 except: # pylint: disable=W |
| 47 logging.error('Failed to parse git hash for %s\nStacktrace:\n%s', | 50 logging.error('Failed to parse git hash for %s\nStacktrace:\n%s', |
| 48 revision, traceback.format_exc()) | 51 revision, traceback.format_exc()) |
| 49 return None | 52 return None |
| 50 | 53 |
| 51 | 54 |
| 52 def EnsureDirExists(path): | 55 # TODO(crbug.com/662540): Add unittests. |
| 56 def EnsureDirExists(path): # pragma: no cover |
| 53 directory = os.path.dirname(path) | 57 directory = os.path.dirname(path) |
| 54 if os.path.exists(directory): | 58 if os.path.exists(directory): |
| 55 return | 59 return |
| 56 | 60 |
| 57 os.makedirs(directory) | 61 os.makedirs(directory) |
| 58 | 62 |
| 59 | 63 |
| 60 def FlushResult(result, result_path): | 64 # TODO(crbug.com/662540): Add unittests. |
| 65 def FlushResult(result, result_path): # pragma: no cover |
| 61 logging.info('\nFlushing results to %s', result_path) | 66 logging.info('\nFlushing results to %s', result_path) |
| 62 EnsureDirExists(result_path) | 67 EnsureDirExists(result_path) |
| 63 with open(result_path, 'wb') as f: | 68 with open(result_path, 'wb') as f: |
| 64 pickle.dump(result, f) | 69 pickle.dump(result, f) |
| 65 | 70 |
| 66 | 71 |
| 67 def PrintDelta(deltas, crash_num): | 72 # TODO(crbug.com/662540): Add unittests. |
| 73 def PrintDelta(deltas, crash_num): # pragma: no cover |
| 68 logging.info(('\n+++++++++++++++++++++' | 74 logging.info(('\n+++++++++++++++++++++' |
| 69 '\nDelta on %d crashes ' | 75 '\nDelta on %d crashes ' |
| 70 '\n+++++++++++++++++++++'), crash_num) | 76 '\n+++++++++++++++++++++'), crash_num) |
| 71 | 77 |
| 72 if not deltas: | 78 if not deltas: |
| 73 logging.info('Two sets of results are the same.') | 79 logging.info('Two sets of results are the same.') |
| 74 return | 80 return |
| 75 | 81 |
| 76 for crash_id, delta in deltas.iteritems(): | 82 for crash_id, delta in deltas.iteritems(): |
| 77 logging.info('\nCrash: %s\n%s\n', | 83 logging.info('\nCrash: %s\n%s\n', |
| 78 _FEEDBACK_URL_TEMPLATE % crash_id, | 84 _FEEDBACK_URL_TEMPLATE % crash_id, |
| 79 str(delta)) | 85 str(delta)) |
| 80 | 86 |
| 81 | 87 |
| 82 def WriteDeltaToCSV(deltas, crash_num, git_hash1, git_hash2, file_path): | 88 # TODO(crbug.com/662540): Add unittests. |
| 89 def WriteDeltaToCSV(deltas, crash_num, |
| 90 git_hash1, git_hash2, file_path): # pragma: no cover |
| 83 EnsureDirExists(file_path) | 91 EnsureDirExists(file_path) |
| 84 def _EncodeStr(string): | 92 def _EncodeStr(string): |
| 85 return string.replace('\"', '\'') if string else '' | 93 return string.replace('\"', '\'') if string else '' |
| 86 | 94 |
| 87 logging.info('Writing delta diff to %s\n', file_path) | 95 logging.info('Writing delta diff to %s\n', file_path) |
| 88 with open(file_path, 'wb') as f: | 96 with open(file_path, 'wb') as f: |
| 89 f.write('Delta between githash1 %s and githash2 %s on %d crashes\n' % ( | 97 f.write('Delta between githash1 %s and githash2 %s on %d crashes\n' % ( |
| 90 git_hash1, git_hash2, crash_num)) | 98 git_hash1, git_hash2, crash_num)) |
| 91 f.write('project, components, cls, regression_range\n') | 99 f.write('project, components, cls, regression_range\n') |
| 92 for crash_id, delta in deltas.iteritems(): | 100 for crash_id, delta in deltas.iteritems(): |
| 93 delta_str_dict = delta.delta_str_dict | 101 delta_str_dict = delta.delta_str_dict |
| 94 feedback_url = _FEEDBACK_URL_TEMPLATE % crash_id | 102 feedback_url = _FEEDBACK_URL_TEMPLATE % crash_id |
| 95 f.write('%s, "%s", "%s", "%s", "%s"\n' % ( | 103 f.write('%s, "%s", "%s", "%s", "%s"\n' % ( |
| 96 feedback_url, | 104 feedback_url, |
| 97 _EncodeStr(delta_str_dict.get('project', '')), | 105 _EncodeStr(delta_str_dict.get('project', '')), |
| 98 _EncodeStr(delta_str_dict.get('components', '')), | 106 _EncodeStr(delta_str_dict.get('components', '')), |
| 99 _EncodeStr(delta_str_dict.get('cls', '')), | 107 _EncodeStr(delta_str_dict.get('cls', '')), |
| 100 _EncodeStr(delta_str_dict.get('regression_range', '')) | 108 _EncodeStr(delta_str_dict.get('regression_range', '')) |
| 101 )) | 109 )) |
| OLD | NEW |