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