# Path: scripts/pkgs/constants.py
# def uniq(vals):
#     def cpu_count():
# def build_dir():
# def set_build_dir(x):
# def current_source():
# def set_current_source(x):
# def set_tdir(x):
# def mkdtemp(prefix=''):
# def putenv(**kw):
# ROOT = 'C:\\' if iswindows else '/'
# SW = ROOT + 'sw'
# SOURCES = ROOT + 'sources'
# PATCHES = ROOT + 'patches'
# SCRIPTS = ROOT + 'scripts'
# CALIBRE_DIR = ROOT + 'calibre'
# PREFIX = os.path.join(SW, 'sw')
# BIN = os.path.join(PREFIX, 'bin')
# PYTHON = os.path.join(PREFIX, 'private', 'python', 'python.exe') if iswindows else os.path.join(BIN, 'python')
#     CFLAGS = CPPFLAGS = LIBDIR = LDFLAGS = ''
#     CFLAGS = worker_env['CFLAGS'] = '-I' + os.path.join(PREFIX, 'include')
#     CPPFLAGS = worker_env['CPPFLAGS'] = '-I' + os.path.join(PREFIX, 'include')
#     LIBDIR = os.path.join(PREFIX, 'lib')
#     LDFLAGS = worker_env['LDFLAGS'] = '-L{0} -Wl,-rpath-link,{0}'.format(LIBDIR)
#     LDFLAGS = worker_env['LDFLAGS'] = '-headerpad_max_install_names -L{}'.format(LIBDIR)
# MAKEOPTS = '-j%d' % cpu_count()
# PKG_CONFIG_PATH = worker_env['PKG_CONFIG_PATH'] = os.path.join(PREFIX, 'lib', 'pkgconfig')
# CMAKE = os.path.join(PREFIX, 'bin', 'cmake') if isosx else 'cmake'
# QT_PREFIX = os.path.join(PREFIX, 'qt')
# QT_DLLS = ['Qt5' + x for x in (
#     'Core', 'Gui', 'Network', 'PrintSupport', 'Positioning', 'Sensors', 'Sql',
#     'Svg', 'WebKit', 'WebKitWidgets', 'Widgets', 'Multimedia', 'OpenGL',
#     'MultimediaWidgets', 'Xml',  # 'XmlPatterns',
# )]
# QT_PLUGINS = [
#     'imageformats', 'iconengines', 'mediaservice', 'platforms',
#     'playlistformats', 'sqldrivers',
#     # 'audio', 'printsupport', 'bearer', 'position',
# ]
# PYQT_MODULES = ('Qt', 'QtCore', 'QtGui', 'QtNetwork',  # 'QtMultimedia', 'QtMultimediaWidgets',
#                 'QtPrintSupport', 'QtSensors', 'QtSvg', 'QtWebKit', 'QtWebKitWidgets', 'QtWidgets')
# CODESIGN_KEYCHAIN = '/Users/kovid/codesign.keychain'
#     class SYSTEM_INFO(ctypes.Structure):
# 
# Path: scripts/pkgs/utils.py
# @contextmanager
# def tempdir(prefix='tmp-'):
#     tdir = mkdtemp(prefix)
#     yield tdir
#     try:
#         shutil.rmtree(tdir)
#     except EnvironmentError:
#         time.sleep(10)  # idiotic windows mandatory file locking
#         shutil.rmtree(tdir)
# 
# def walk(path='.'):
#     ''' A nice interface to os.walk '''
#     for dirpath, dirnames, filenames in os.walk(path):
#         for f in filenames:
#             yield os.path.join(dirpath, f)
# 
# def run(*args, **kw):
#     if len(args) == 1 and isinstance(args[0], type('')):
#         cmd = split(args[0])
#     else:
#         cmd = args
#     print(' '.join(pipes.quote(x) for x in cmd))
#     sys.stdout.flush()
#     env = current_env(library_path=kw.get('library_path'))
#     try:
#         p = subprocess.Popen(cmd, env=env, cwd=kw.get('cwd'))
#     except EnvironmentError as err:
#         if err.errno == errno.ENOENT:
#             raise SystemExit('Could not find the program: %s' % cmd[0])
#         raise
#     rc = p.wait()
#     if kw.get('no_check'):
#         return rc
#     if rc != 0:
#         print('The following command failed, with return code:', rc)
#         print(' '.join(pipes.quote(x) for x in cmd))
#         print('Dropping you into a shell')
#         sys.stdout.flush()
#         run_shell(library_path=kw.get('library_path'))
#         raise SystemExit(1)


import os
import re
import hashlib
import json
import errno
import time
import sys
import urllib
import urlparse
import tarfile
import shutil
                import traceback
from .constants import SOURCES, iswindows
from .utils import tempdir, walk, run
        url = get_pypi_url(pkg)
    print('Downloading', filename, 'from', url)
    if pkg['hash'].startswith('git:'):
        get_git_clone(pkg, url, fname)
    else:
        urllib.urlretrieve(url, fname, reporthook)
    if not verify_hash(pkg):
        raise SystemExit('The hash of the downloaded file: %s does not match the saved hash' % filename)


def download_pkg(pkg):
    for try_count in range(3):
        for url in pkg['urls']:
            try:
                return try_once(pkg, url)
            except Exception as err:
                traceback.print_exc()
                sys.stderr.flush()
                print('Download failed, with error:', type('')(err))
                sys.stdout.flush()
            finally:
                print()
    raise SystemExit(
        'Downloading of %s failed after three tries, giving up.' % pkg['name'])


def cleanup_cache(sources):
    if os.path.exists(SOURCES):
        existing = {x.lower(): x for x in os.listdir(SOURCES)}
        for extra in set(existing) - all_filenames:

# Path: scripts/pkgs/constants.py
# def uniq(vals):
#     def cpu_count():
# def build_dir():
# def set_build_dir(x):
# def current_source():
# def set_current_source(x):
# def set_tdir(x):
# def mkdtemp(prefix=''):
# def putenv(**kw):
# ROOT = 'C:\\' if iswindows else '/'
# SW = ROOT + 'sw'
# SOURCES = ROOT + 'sources'
# PATCHES = ROOT + 'patches'
# SCRIPTS = ROOT + 'scripts'
# CALIBRE_DIR = ROOT + 'calibre'
# PREFIX = os.path.join(SW, 'sw')
# BIN = os.path.join(PREFIX, 'bin')
# PYTHON = os.path.join(PREFIX, 'private', 'python', 'python.exe') if iswindows else os.path.join(BIN, 'python')
#     CFLAGS = CPPFLAGS = LIBDIR = LDFLAGS = ''
#     CFLAGS = worker_env['CFLAGS'] = '-I' + os.path.join(PREFIX, 'include')
#     CPPFLAGS = worker_env['CPPFLAGS'] = '-I' + os.path.join(PREFIX, 'include')
#     LIBDIR = os.path.join(PREFIX, 'lib')
#     LDFLAGS = worker_env['LDFLAGS'] = '-L{0} -Wl,-rpath-link,{0}'.format(LIBDIR)
#     LDFLAGS = worker_env['LDFLAGS'] = '-headerpad_max_install_names -L{}'.format(LIBDIR)
# MAKEOPTS = '-j%d' % cpu_count()
# PKG_CONFIG_PATH = worker_env['PKG_CONFIG_PATH'] = os.path.join(PREFIX, 'lib', 'pkgconfig')
# CMAKE = os.path.join(PREFIX, 'bin', 'cmake') if isosx else 'cmake'
# QT_PREFIX = os.path.join(PREFIX, 'qt')
# QT_DLLS = ['Qt5' + x for x in (
#     'Core', 'Gui', 'Network', 'PrintSupport', 'Positioning', 'Sensors', 'Sql',
#     'Svg', 'WebKit', 'WebKitWidgets', 'Widgets', 'Multimedia', 'OpenGL',
#     'MultimediaWidgets', 'Xml',  # 'XmlPatterns',
# )]
# QT_PLUGINS = [
#     'imageformats', 'iconengines', 'mediaservice', 'platforms',
#     'playlistformats', 'sqldrivers',
#     # 'audio', 'printsupport', 'bearer', 'position',
# ]
# PYQT_MODULES = ('Qt', 'QtCore', 'QtGui', 'QtNetwork',  # 'QtMultimedia', 'QtMultimediaWidgets',
#                 'QtPrintSupport', 'QtSensors', 'QtSvg', 'QtWebKit', 'QtWebKitWidgets', 'QtWidgets')
# CODESIGN_KEYCHAIN = '/Users/kovid/codesign.keychain'
#     class SYSTEM_INFO(ctypes.Structure):
# 
# Path: scripts/pkgs/utils.py
# @contextmanager
# def tempdir(prefix='tmp-'):
#     tdir = mkdtemp(prefix)
#     yield tdir
#     try:
#         shutil.rmtree(tdir)
#     except EnvironmentError:
#         time.sleep(10)  # idiotic windows mandatory file locking
#         shutil.rmtree(tdir)
# 
# def walk(path='.'):
#     ''' A nice interface to os.walk '''
#     for dirpath, dirnames, filenames in os.walk(path):
#         for f in filenames:
#             yield os.path.join(dirpath, f)
# 
# def run(*args, **kw):
#     if len(args) == 1 and isinstance(args[0], type('')):
#         cmd = split(args[0])
#     else:
#         cmd = args
#     print(' '.join(pipes.quote(x) for x in cmd))
#     sys.stdout.flush()
#     env = current_env(library_path=kw.get('library_path'))
#     try:
#         p = subprocess.Popen(cmd, env=env, cwd=kw.get('cwd'))
#     except EnvironmentError as err:
#         if err.errno == errno.ENOENT:
#             raise SystemExit('Could not find the program: %s' % cmd[0])
#         raise
#     rc = p.wait()
#     if kw.get('no_check'):
#         return rc
#     if rc != 0:
#         print('The following command failed, with return code:', rc)
#         print(' '.join(pipes.quote(x) for x in cmd))
#         print('Dropping you into a shell')
#         sys.stdout.flush()
#         run_shell(library_path=kw.get('library_path'))
#         raise SystemExit(1)


# Path: scripts/pkgs/download_sources.py
import os
import re
import hashlib
import json
import errno
import time
import sys
import urllib
import urlparse
import tarfile
import shutil
                import traceback
from .constants import SOURCES, iswindows
from .utils import tempdir, walk, run

        url = get_pypi_url(pkg)
    print('Downloading', filename, 'from', url)
    if pkg['hash'].startswith('git:'):
        get_git_clone(pkg, url, fname)
    else:
        urllib.urlretrieve(url, fname, reporthook)
    if not verify_hash(pkg):
        raise SystemExit('The hash of the downloaded file: %s does not match the saved hash' % filename)


def download_pkg(pkg):
    for try_count in range(3):
        for url in pkg['urls']:
            try:
                return try_once(pkg, url)
            except Exception as err:
                traceback.print_exc()
                sys.stderr.flush()
                print('Download failed, with error:', type('')(err))
                sys.stdout.flush()
            finally:
                print()
    raise SystemExit(
        'Downloading of %s failed after three tries, giving up.' % pkg['name'])


def cleanup_cache(sources):
    if os.path.exists(SOURCES):
        existing = {x.lower(): x for x in os.listdir(SOURCES)}
        for extra in set(existing) - all_filenames:

            os.remove(os.path.join(SOURCES, existing[extra]))