# Copyright 2014 The Native Client Authors. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.

import fcntl
import hashlib
import os
import shutil
import subprocess
import sys

# Allow use of this module even if termcolor is missing.  There are many
# standalone python scripts in build_tools that can be run directly without
# PYTHONPATH set (i.e. not via build/python_wrapper that adds this path.
# TODO(sbc): we should probably just assume that all the module dependencies
# are present.
try:
  import termcolor
except ImportError:
  termcolor = None

from naclports import error, paths

GS_URL = 'http://storage.googleapis.com/'
GS_BUCKET = 'naclports'
GS_MIRROR_URL = '%s%s/mirror' % (GS_URL, GS_BUCKET)

# Require the latest version of the NaCl SDK. naclports is built
# and tested against the pepper_canary release. To build aginst older
# versions of the SDK use the one of the pepper_XX branches (or use
# --skip-sdk-version-check).
MIN_SDK_VERSION = 46

arch_to_pkgarch = {
    'x86_64': 'x86-64',
    'i686': 'i686',
    'arm': 'arm',
    'pnacl': 'pnacl',
    'emscripten': 'emscripten',
}

# Inverse of arch_to_pkgarch
pkgarch_to_arch = {v: k for k, v in arch_to_pkgarch.items()}

LOG_ERROR = 0
LOG_WARN = 1
LOG_INFO = 2
LOG_VERBOSE = 3
LOG_TRACE = 4

ELF_MAGIC = '\x7fELF'
PEXE_MAGIC = 'PEXE'

log_level = LOG_INFO
color_mode = 'auto'


def Color(message, color):
  if termcolor and Color.enabled:
    return termcolor.colored(message, color)
  else: