OLD | NEW |
---|---|
(Empty) | |
1 # Copyright 2015 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 """Utility functions for the stack tool.""" | |
6 | |
7 import glob | |
8 import os | |
9 import os.path | |
10 import re | |
11 | |
ppi
2015/08/21 14:53:07
two blank lines please
etiennej
2015/08/21 15:07:18
Done.
| |
12 def UnzipSymbols(symbolfile, symdir=None): | |
13 """Unzips a file to _DEFAULT_SYMROOT and returns the unzipped location. | |
14 | |
15 Args: | |
16 symbolfile: The .zip file to unzip | |
17 symdir: Optional temporary directory to use for extraction | |
18 | |
19 Returns: | |
20 A tuple containing (the directory into which the zip file was unzipped, | |
21 the path to the "symbols" directory in the unzipped file). To clean | |
22 up, the caller can delete the first element of the tuple. | |
23 | |
24 Raises: | |
25 SymbolDownloadException: When the unzip fails. | |
26 """ | |
27 if not symdir: | |
28 symdir = "%s/%s" % (_DEFAULT_SYMROOT, hash(symbolfile)) | |
29 if not os.path.exists(symdir): | |
30 os.makedirs(symdir) | |
31 | |
32 print "extracting %s..." % symbolfile | |
33 saveddir = os.getcwd() | |
34 os.chdir(symdir) | |
35 try: | |
36 unzipcode = subprocess.call(["unzip", "-qq", "-o", symbolfile]) | |
37 if unzipcode > 0: | |
38 os.remove(symbolfile) | |
39 raise SymbolDownloadException("failed to extract symbol files (%s)." | |
40 % symbolfile) | |
41 finally: | |
42 os.chdir(saveddir) | |
43 | |
44 android_symbols = glob.glob("%s/out/target/product/*/symbols" % symdir) | |
45 if android_symbols: | |
46 return (symdir, android_symbols[0]) | |
47 else: | |
48 # This is a zip of Chrome symbols, so symbol.CHROME_SYMBOLS_DIR needs to be | |
49 # updated to point here. | |
50 symbol.CHROME_SYMBOLS_DIR = symdir | |
51 return (symdir, symdir) | |
52 | |
53 | |
54 def GetBasenameFromMojoApp(url): | |
55 """Used by GetSymbolMapping() to extract the basename from the location the | |
56 mojo app was downloaded from. The location is a URL, e.g. | |
57 http://foo/bar/x.so.""" | |
58 index = url.rfind('/') | |
59 return url[(index + 1):] if index != -1 else url | |
60 | |
61 | |
62 def GetSymboledNameForMojoApp(path): | |
63 """Used by GetSymbolMapping to get the non-stripped library name for an | |
64 installed mojo app.""" | |
65 # e.g. tracing.mojo -> libtracing_library.so | |
66 name, ext = os.path.splitext(path) | |
67 if ext != '.mojo': | |
68 return path | |
69 return 'lib%s_library.so' % name | |
70 | |
71 | |
72 def GetSymbolMapping(lines): | |
73 """Returns a mapping (dictionary) from download file to .so.""" | |
74 regex = re.compile('Caching mojo app (\S+?)(?:\?\S+)? at (\S+)') | |
75 mappings = {} | |
76 for line in lines: | |
77 result = regex.search(line) | |
78 if result: | |
79 url = GetBasenameFromMojoApp(result.group(1)) | |
80 mappings[os.path.normpath(result.group(2))] = GetSymboledNameForMojoApp( | |
81 url) | |
82 return mappings | |
83 | |
84 | |
85 def _LowestAncestorContainingRelpath(dir_path, relpath): | |
86 """Returns the lowest ancestor dir of |dir_path| that contains |relpath|. | |
87 """ | |
88 cur_dir_path = os.path.abspath(dir_path) | |
89 while True: | |
90 if os.path.exists(os.path.join(cur_dir_path, relpath)): | |
91 return cur_dir_path | |
92 | |
93 next_dir_path = os.path.dirname(cur_dir_path) | |
94 if next_dir_path != cur_dir_path: | |
95 cur_dir_path = next_dir_path | |
96 else: | |
97 return None | |
98 | |
99 | |
100 def GuessDir(relpath): | |
101 """Returns absolute path to location |relpath| in the lowest ancestor of this | |
102 file that contains it.""" | |
103 lowest_ancestor = _LowestAncestorContainingRelpath( | |
104 os.path.dirname(__file__), relpath) | |
105 if not lowest_ancestor: | |
106 return None | |
107 return os.path.join(lowest_ancestor, relpath) | |
OLD | NEW |