OLD | NEW |
---|---|
1 # Copyright 2014 The Native Client Authors. All rights reserved. | 1 # Copyright 2014 The Native Client 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 import fcntl | 5 import fcntl |
6 import hashlib | 6 import hashlib |
7 import os | 7 import os |
8 import shutil | 8 import shutil |
9 import subprocess | 9 import subprocess |
10 import sys | 10 import sys |
(...skipping 23 matching lines...) Expand all Loading... | |
34 arch_to_pkgarch = { | 34 arch_to_pkgarch = { |
35 'x86_64': 'x86-64', | 35 'x86_64': 'x86-64', |
36 'i686': 'i686', | 36 'i686': 'i686', |
37 'arm': 'arm', | 37 'arm': 'arm', |
38 'pnacl': 'pnacl', | 38 'pnacl': 'pnacl', |
39 } | 39 } |
40 | 40 |
41 # Inverse of arch_to_pkgarch | 41 # Inverse of arch_to_pkgarch |
42 pkgarch_to_arch = {v:k for k, v in arch_to_pkgarch.items()} | 42 pkgarch_to_arch = {v:k for k, v in arch_to_pkgarch.items()} |
43 | 43 |
44 verbose = False | 44 LOG_ERROR = 0 |
binji
2015/03/17 19:28:12
The logging module uses the names debug, info, war
| |
45 LOG_WARN = 1 | |
46 LOG_MESSAGE = 2 | |
47 LOG_VERBOSE = 3 | |
48 LOG_TRACE = 4 | |
49 | |
50 log_level = LOG_MESSAGE | |
45 color_mode = 'auto' | 51 color_mode = 'auto' |
46 | 52 |
47 def Color(message, color): | 53 def Color(message, color): |
48 if termcolor and Color.enabled: | 54 if termcolor and Color.enabled: |
49 return termcolor.colored(message, color) | 55 return termcolor.colored(message, color) |
50 else: | 56 else: |
51 return message | 57 return message |
52 | 58 |
53 | 59 |
54 def CheckStdoutForColorSupport(): | 60 def CheckStdoutForColorSupport(): |
(...skipping 11 matching lines...) Expand all Loading... | |
66 def __call__(self, *args): | 72 def __call__(self, *args): |
67 return self[args] | 73 return self[args] |
68 | 74 |
69 def __missing__(self, key): | 75 def __missing__(self, key): |
70 ret = self[key] = self.f(*key) | 76 ret = self[key] = self.f(*key) |
71 return ret | 77 return ret |
72 | 78 |
73 return Memo(f) | 79 return Memo(f) |
74 | 80 |
75 | 81 |
76 def SetVerbose(verbosity): | 82 def SetVerbose(enabled): |
77 global verbose | 83 if enabled: |
78 verbose = verbosity | 84 SetLogLevel(LOG_VERBOSE) |
85 else: | |
86 SetLogLevel(LOG_MESSAGE) | |
79 | 87 |
80 | 88 |
81 def Log(message): | 89 def SetLogLevel(verbosity): |
90 global log_level | |
91 log_level = verbosity | |
92 | |
93 | |
94 def Log(message, verbosity=LOG_MESSAGE): | |
82 """Log a message to the console (stdout).""" | 95 """Log a message to the console (stdout).""" |
96 if log_level < verbosity: | |
97 return | |
83 sys.stdout.write(str(message) + '\n') | 98 sys.stdout.write(str(message) + '\n') |
84 sys.stdout.flush() | 99 sys.stdout.flush() |
85 | 100 |
86 | 101 |
87 def LogHeading(message, suffix=''): | 102 def LogHeading(message, suffix=''): |
88 """Log a colored/highlighted message with optional suffix.""" | 103 """Log a colored/highlighted message with optional suffix.""" |
89 if Color.enabled: | 104 if Color.enabled: |
90 Log(Color(message, 'green') + suffix) | 105 Log(Color(message, 'green') + suffix) |
91 else: | 106 else: |
92 if verbose: | 107 if log_level > LOG_WARN: |
93 # When running in verbose mode make sure heading standout | 108 # When running in verbose mode make sure heading standout |
94 Log('###################################################################') | 109 Log('###################################################################') |
95 Log(message + suffix) | 110 Log(message + suffix) |
96 Log('###################################################################') | 111 Log('###################################################################') |
97 else: | 112 else: |
98 Log(message + suffix) | 113 Log(message + suffix) |
99 | 114 |
100 | 115 |
101 def Warn(message): | 116 def Warn(message): |
102 Log('warning: ' + message) | 117 Log('warning: ' + message, LOG_WARN) |
103 | 118 |
104 | 119 |
105 def Trace(message): | 120 def Trace(message): |
106 """Log a message to the console if running in verbose mode (-v).""" | 121 Log(message, LOG_TRACE) |
107 if verbose: | 122 |
108 Log(message) | 123 |
124 def LogVerbose(message): | |
125 Log(message, LOG_VERBOSE) | |
109 | 126 |
110 | 127 |
111 def FindInPath(command_name): | 128 def FindInPath(command_name): |
112 """Search user's PATH for a given executable. | 129 """Search user's PATH for a given executable. |
113 | 130 |
114 Returns: | 131 Returns: |
115 Full path to executable. | 132 Full path to executable. |
116 """ | 133 """ |
117 if os.name == 'nt': | 134 if os.name == 'nt': |
118 extensions = ('.bat', '.com', '.exe') | 135 extensions = ('.bat', '.com', '.exe') |
(...skipping 24 matching lines...) Expand all Loading... | |
143 if hasattr(sys.stdout, 'fileno') and os.isatty(sys.stdout.fileno()): | 160 if hasattr(sys.stdout, 'fileno') and os.isatty(sys.stdout.fileno()): |
144 # Add --progress-bar but only if stdout is a TTY device. | 161 # Add --progress-bar but only if stdout is a TTY device. |
145 curl_cmd.append('--progress-bar') | 162 curl_cmd.append('--progress-bar') |
146 else: | 163 else: |
147 # otherwise suppress status output, since curl always assumes its | 164 # otherwise suppress status output, since curl always assumes its |
148 # talking to a TTY and writes \r and \b characters. But add | 165 # talking to a TTY and writes \r and \b characters. But add |
149 # --show-error so that when curl fails it at least prints something. | 166 # --show-error so that when curl fails it at least prints something. |
150 curl_cmd += ['--silent', '--show-error'] | 167 curl_cmd += ['--silent', '--show-error'] |
151 curl_cmd.append(url) | 168 curl_cmd.append(url) |
152 | 169 |
153 if verbose: | 170 if log_level > LOG_WARN: |
154 Log('Downloading: %s [%s]' % (url, filename)) | 171 Log('Downloading: %s [%s]' % (url, filename)) |
155 else: | 172 else: |
156 Log('Downloading: %s' % url.replace(GS_URL, '')) | 173 Log('Downloading: %s' % url.replace(GS_URL, '')) |
157 try: | 174 try: |
158 subprocess.check_call(curl_cmd) | 175 subprocess.check_call(curl_cmd) |
159 except subprocess.CalledProcessError as e: | 176 except subprocess.CalledProcessError as e: |
160 raise error.Error('Error downloading file: %s' % str(e)) | 177 raise error.Error('Error downloading file: %s' % str(e)) |
161 | 178 |
162 os.rename(temp_filename, filename) | 179 os.rename(temp_filename, filename) |
163 | 180 |
(...skipping 245 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
409 | 426 |
410 | 427 |
411 class InstallLock(Lock): | 428 class InstallLock(Lock): |
412 """Lock used when installing/uninstalling package""" | 429 """Lock used when installing/uninstalling package""" |
413 def __init__(self, config): | 430 def __init__(self, config): |
414 root = GetInstallRoot(config) | 431 root = GetInstallRoot(config) |
415 super(InstallLock, self).__init__(root) | 432 super(InstallLock, self).__init__(root) |
416 | 433 |
417 | 434 |
418 CheckStdoutForColorSupport() | 435 CheckStdoutForColorSupport() |
OLD | NEW |