author Tom Prince <mozilla@hocat.ca>
Thu, 12 Apr 2018 10:17:50 -0600
changeset 783688 b36fb5458ecbde976a48ec0644d3b88154325eb8
parent 756866 bf662dbf4bc7eb0e0ea23b1241809b19cee24ffe
child 818046 c018a0a933bcfe9fa6634692b834485422f6cc42
permissions -rw-r--r--
Bug 1453658 - Use pip options when install python3 modules. r=bhearsum, a=test-only Differential Revision: https://phabricator.services.mozilla.com/D924

#!/usr/bin/env python
# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this file,
# You can obtain one at http://mozilla.org/MPL/2.0/.
# ***** END LICENSE BLOCK *****
'''Python usage, esp. virtualenv.

import errno
import os
import subprocess
import sys
import json
import socket
import traceback
import urlparse

import mozharness
from mozharness.base.script import (
from mozharness.base.errors import VirtualenvErrorList
from mozharness.base.log import WARNING, FATAL

external_tools_path = os.path.join(

def get_tlsv1_post():
    # Monkeypatch to work around SSL errors in non-bleeding-edge Python.
    # Taken from https://lukasa.co.uk/2013/01/Choosing_SSL_Version_In_Requests/
    import requests
    from requests.packages.urllib3.poolmanager import PoolManager
    import ssl

    class TLSV1Adapter(requests.adapters.HTTPAdapter):
        def init_poolmanager(self, connections, maxsize, block=False):
            self.poolmanager = PoolManager(num_pools=connections,
    s = requests.Session()
    s.mount('https://', TLSV1Adapter())
    return s.post

# Virtualenv {{{1
virtualenv_config_options = [
    [["--virtualenv-path"], {
        "action": "store",
        "dest": "virtualenv_path",
        "default": "venv",
        "help": "Specify the path to the virtualenv top level directory"
    [["--find-links"], {
        "action": "extend",
        "dest": "find_links",
        "help": "URL to look for packages at"
    [["--pip-index"], {
        "action": "store_true",
        "default": True,
        "dest": "pip_index",
        "help": "Use pip indexes (default)"
    [["--no-pip-index"], {
        "action": "store_false",
        "dest": "pip_index",
        "help": "Don't use pip indexes"

class VirtualenvMixin(object):
    '''BaseScript mixin, designed to create and use virtualenvs.

    Config items:
     * virtualenv_path points to the virtualenv location on disk.
     * virtualenv_modules lists the module names.
     * MODULE_url list points to the module URLs (optional)
    Requires virtualenv to be in PATH.
    Depends on ScriptMixin
    python_paths = {}
    site_packages_path = None

    def __init__(self, *args, **kwargs):
        self._virtualenv_modules = []
        super(VirtualenvMixin, self).__init__(*args, **kwargs)

    def register_virtualenv_module(self, name=None, url=None, method=None,
                                   requirements=None, optional=False,
                                   two_pass=False, editable=False):
        """Register a module to be installed with the virtualenv.

        This method can be called up until create_virtualenv() to register
        modules that should be installed in the virtualenv.

        See the documentation for install_module for how the arguments are
        self._virtualenv_modules.append((name, url, method, requirements,
                                         optional, two_pass, editable))

    def query_virtualenv_path(self):
        """Determine the absolute path to the virtualenv."""
        dirs = self.query_abs_dirs()

        if 'abs_virtualenv_dir' in dirs:
            return dirs['abs_virtualenv_dir']

        p = self.config['virtualenv_path']
        if not p:
            self.fatal('virtualenv_path config option not set; '
                       'this should never happen')

        if os.path.isabs(p):
            return p
            return os.path.join(dirs['abs_work_dir'], p)

    def query_python_path(self, binary="python"):
        """Return the path of a binary inside the virtualenv, if
        c['virtualenv_path'] is set; otherwise return the binary name.
        Otherwise return None
        if binary not in self.python_paths:
            bin_dir = 'bin'
            if self._is_windows():
                bin_dir = 'Scripts'
            virtualenv_path = self.query_virtualenv_path()
            self.python_paths[binary] = os.path.abspath(os.path.join(virtualenv_path, bin_dir, binary))

        return self.python_paths[binary]

    def query_python_site_packages_path(self):
        if self.site_packages_path:
            return self.site_packages_path
        python = self.query_python_path()
        self.site_packages_path = self.get_output_from_command(
            [python, '-c',
             'from distutils.sysconfig import get_python_lib; ' +
        return self.site_packages_path

    def package_versions(self, pip_freeze_output=None, error_level=WARNING, log_output=False):
        reads packages from `pip freeze` output and returns a dict of
        {package_name: 'version'}
        packages = {}

        if pip_freeze_output is None:
            # get the output from `pip freeze`
            pip = self.query_python_path("pip")
            if not pip:
                self.log("package_versions: Program pip not in path", level=error_level)
                return {}
            pip_freeze_output = self.get_output_from_command([pip, "freeze"], silent=True, ignore_errors=True)
            if not isinstance(pip_freeze_output, basestring):
                self.fatal("package_versions: Error encountered running `pip freeze`: %s" % pip_freeze_output)

        for line in pip_freeze_output.splitlines():
            # parse the output into package, version
            line = line.strip()
            if not line:
                # whitespace
            if line.startswith('-'):
                # not a package, probably like '-e http://example.com/path#egg=package-dev'
            if '==' not in line:
                self.fatal("pip_freeze_packages: Unrecognized output line: %s" % line)
            package, version = line.split('==', 1)
            packages[package] = version

        if log_output:
            self.info("Current package versions:")
            for package in sorted(packages):
                self.info("  %s == %s" % (package, packages[package]))

        return packages

    def is_python_package_installed(self, package_name, error_level=WARNING):
        Return whether the package is installed
        packages = self.package_versions(error_level=error_level).keys()
        return package_name.lower() in [package.lower() for package in packages]

    def install_module(self, module=None, module_url=None, install_method=None,
                       requirements=(), optional=False, global_options=[],
                       no_deps=False, editable=False):
        Install module via pip.

        module_url can be a url to a python package tarball, a path to
        a directory containing a setup.py (absolute or relative to work_dir)
        or None, in which case it will default to the module name.

        requirements is a list of pip requirements files.  If specified, these
        will be combined with the module_url (if any), like so:

        pip install -r requirements1.txt -r requirements2.txt module_url
        c = self.config
        dirs = self.query_abs_dirs()
        env = self.query_env()
        venv_path = self.query_virtualenv_path()
        self.info("Installing %s into virtualenv %s" % (module, venv_path))
        if not module_url:
            module_url = module
        if install_method in (None, 'pip'):
            if not module_url and not requirements:
                self.fatal("Must specify module and/or requirements")
            pip = self.query_python_path("pip")
            if c.get("verbose_pip"):
                command = [pip, "-v", "install"]
                command = [pip, "install"]
            if no_deps:
                command += ["--no-deps"]
            # To avoid timeouts with our pypi server, increase default timeout:
            # https://bugzilla.mozilla.org/show_bug.cgi?id=1007230#c802
            command += ['--timeout', str(c.get('pip_timeout', 120))]
            for requirement in requirements:
                command += ["-r", requirement]
            if c.get('find_links') and not c["pip_index"]:
                command += ['--no-index']
            for opt in global_options:
                command += ["--global-option", opt]
        elif install_method == 'easy_install':
            if not module:
                self.fatal("module parameter required with install_method='easy_install'")
            if requirements:
                # Install pip requirements files separately, since they're
                # not understood by easy_install.
            # Allow easy_install to be overridden by
            # self.config['exes']['easy_install']
            default = 'easy_install'
            command = self.query_exe('easy_install', default=default, return_type="list")
            self.fatal("install_module() doesn't understand an install_method of %s!" % install_method)

        trusted_hosts = set()
        for link in c.get('find_links', []):
            parsed = urlparse.urlparse(link)

            except socket.gaierror as e:
                self.info('error resolving %s (ignoring): %s' %
                          (parsed.hostname, e.message))

            command.extend(["--find-links", link])
            if parsed.scheme != 'https':

        if install_method != 'easy_install':
            for host in sorted(trusted_hosts):
                command.extend(['--trusted-host', host])

        # module_url can be None if only specifying requirements files
        if module_url:
            if editable:
                if install_method in (None, 'pip'):
                    command += ['-e']
                    self.fatal("editable installs not supported for install_method %s" % install_method)
            command += [module_url]

        # If we're only installing a single requirements file, use
        # the file's directory as cwd, so relative paths work correctly.
        cwd = dirs['abs_work_dir']
        if not module and len(requirements) == 1:
            cwd = os.path.dirname(requirements[0])

        quoted_command = subprocess.list2cmdline(command)
        # Allow for errors while building modules, but require a
        # return status of 0.
            # None will cause default value to be used
            attempts=1 if optional else None,
            error_level=WARNING if optional else FATAL,
            error_message='Could not install python package: ' + quoted_command + ' failed after %(attempts)d tries!',
            args=[command, ],
                'error_list': VirtualenvErrorList,
                'cwd': cwd,
                'env': env,
                # WARNING only since retry will raise final FATAL if all
                # retry attempts are unsuccessful - and we only want
                # an ERROR of FATAL if *no* retry attempt works
                'error_level': WARNING,

    def create_virtualenv(self, modules=(), requirements=()):
        Create a python virtualenv.

        This uses the copy of virtualenv that is vendored in mozharness.

        virtualenv_modules can be a list of module names to install, e.g.

            virtualenv_modules = ['module1', 'module2']

        or it can be a heterogeneous list of modules names and dicts that
        define a module by its name, url-or-path, and a list of its global

            virtualenv_modules = [
                    'name': 'module1',
                    'url': None,
                    'global_options': ['--opt', '--without-gcc']
                    'name': 'module2',
                    'url': 'http://url/to/package',
                    'global_options': ['--use-clang']
                    'name': 'module3',
                    'url': os.path.join('path', 'to', 'setup_py', 'dir')
                    'global_options': []

        virtualenv_requirements is an optional list of pip requirements files to
        use when invoking pip, e.g.,

            virtualenv_requirements = [
        c = self.config
        dirs = self.query_abs_dirs()
        venv_path = self.query_virtualenv_path()
        self.info("Creating virtualenv %s" % venv_path)

        # Always use the virtualenv that is vendored since that is deterministic.
        # TODO Bug 1408051 - Use the copy of virtualenv under
        # third_party/python/virtualenv once everything is off buildbot
        virtualenv = [
            os.path.join(external_tools_path, 'virtualenv', 'virtualenv.py'),
        virtualenv_options = c.get('virtualenv_options', [])
        # Don't create symlinks. If we don't do this, permissions issues may
        # hinder virtualenv creation or operation.

        if os.path.exists(self.query_python_path()):
            self.info("Virtualenv %s appears to already exist; skipping virtualenv creation." % self.query_python_path())
            self.run_command(virtualenv + virtualenv_options + [venv_path],
                             partial_env={'VIRTUALENV_NO_DOWNLOAD': "1"},

        if not modules:
            modules = c.get('virtualenv_modules', [])
        if not requirements:
            requirements = c.get('virtualenv_requirements', [])
        if not modules and requirements:
        for module in modules:
            module_url = module
            global_options = []
            if isinstance(module, dict):
                if module.get('name', None):
                    module_name = module['name']
                    self.fatal("Can't install module without module name: %s" %
                module_url = module.get('url', None)
                global_options = module.get('global_options', [])
                module_url = self.config.get('%s_url' % module, module_url)
                module_name = module
            install_method = 'pip'
            if module_name in ('pywin32',):
                install_method = 'easy_install'

        for module, url, method, requirements, optional, two_pass, editable in \
            if two_pass:
                    module=module, module_url=url,
                    install_method=method, requirements=requirements or (),
                    optional=optional, no_deps=True, editable=editable
                module=module, module_url=url,
                install_method=method, requirements=requirements or (),
                optional=optional, editable=editable

        self.info("Done creating virtualenv %s." % venv_path)


    def activate_virtualenv(self):
        """Import the virtualenv's packages into this Python interpreter."""
        bin_dir = os.path.dirname(self.query_python_path())
        activate = os.path.join(bin_dir, 'activate_this.py')
        execfile(activate, dict(__file__=activate))

# This is (sadly) a mixin for logging methods.
class PerfherderResourceOptionsMixin(ScriptMixin):
    def perfherder_resource_options(self):
        """Obtain a list of extraOptions values to identify the env."""
        opts = []

        if 'TASKCLUSTER_INSTANCE_TYPE' in os.environ:
            # Include the instance type so results can be grouped.
            opts.append('taskcluster-%s' % os.environ['TASKCLUSTER_INSTANCE_TYPE'])
            # We assume !taskcluster => buildbot.
            instance = 'unknown'

            # Try to load EC2 instance type from metadata file. This file
            # may not exist in many scenarios (including when inside a chroot).
            # So treat it as optional.
                # This file should exist on Linux in EC2.
                with open('/etc/instance_metadata.json', 'rb') as fh:
                    im = json.load(fh)
                    instance = im.get('aws_instance_type', u'unknown').encode('ascii')
            except IOError as e:
                if e.errno != errno.ENOENT:
                self.info('instance_metadata.json not found; unable to '
                          'determine instance type')
            except Exception:
                self.warning('error reading instance_metadata: %s' %

            opts.append('buildbot-%s' % instance)

        # Allow configs to specify their own values.
        opts.extend(self.config.get('perfherder_extra_options', []))

        return opts

class ResourceMonitoringMixin(PerfherderResourceOptionsMixin):
    """Provides resource monitoring capabilities to scripts.

    When this class is in the inheritance chain, resource usage stats of the
    executing script will be recorded.

    This class requires the VirtualenvMixin in order to install a package used
    for recording resource usage.

    While we would like to record resource usage for the entirety of a script,
    since we require an external package, we can only record resource usage
    after that package is installed (as part of creating the virtualenv).
    That's just the way things have to be.
    def __init__(self, *args, **kwargs):
        super(ResourceMonitoringMixin, self).__init__(*args, **kwargs)

        self.register_virtualenv_module('psutil>=3.1.1', method='pip',
                                        method='pip', optional=True)
        # explicitly install functools32, because some slaves aren't using
        # a version of pip recent enough to install it automatically with
        # jsonschema (which depends on it)
        # https://github.com/Julian/jsonschema/issues/233
        self._resource_monitor = None

        # 2-tuple of (name, options) to assign Perfherder resource monitor
        # metrics to. This needs to be assigned by a script in order for
        # Perfherder metrics to be reported.
        self.resource_monitor_perfherder_id = None

    def _start_resource_monitoring(self, action, success=None):

        # Resource Monitor requires Python 2.7, however it's currently optional.
        # Remove when all machines have had their Python version updated (bug 711299).
        if sys.version_info[:2] < (2, 7):
            self.warning('Resource monitoring will not be enabled! Python 2.7+ required.')

            from mozsystemmonitor.resourcemonitor import SystemResourceMonitor

            self.info("Starting resource monitoring.")
            self._resource_monitor = SystemResourceMonitor(poll_interval=1.0)
        except Exception:
            self.warning("Unable to start resource monitor: %s" %

    def _resource_record_pre_action(self, action):
        # Resource monitor isn't available until after create-virtualenv.
        if not self._resource_monitor:


    def _resource_record_post_action(self, action, success=None):
        # Resource monitor isn't available until after create-virtualenv.
        if not self._resource_monitor:


    def _resource_record_post_run(self):
        if not self._resource_monitor:

        # This should never raise an exception. This is a workaround until
        # mozsystemmonitor is fixed. See bug 895388.

            # Upload a JSON file containing the raw resource data.
                upload_dir = self.query_abs_dirs()['abs_blob_upload_dir']
                if not os.path.exists(upload_dir):
                with open(os.path.join(upload_dir, 'resource-usage.json'), 'wb') as fh:
                    json.dump(self._resource_monitor.as_dict(), fh,
                              sort_keys=True, indent=4)
            except (AttributeError, KeyError):
                self.exception('could not upload resource usage JSON',

        except Exception:
            self.warning("Exception when reporting resource usage: %s" %

    def _log_resource_usage(self):
        # Delay import because not available until virtualenv is populated.
        import jsonschema

        rm = self._resource_monitor

        if rm.start_time is None:

        def resources(phase):
            cpu_percent = rm.aggregate_cpu_percent(phase=phase, per_cpu=False)
            cpu_times = rm.aggregate_cpu_times(phase=phase, per_cpu=False)
            io = rm.aggregate_io(phase=phase)

            swap_in = sum(m.swap.sin for m in rm.measurements)
            swap_out = sum(m.swap.sout for m in rm.measurements)

            return cpu_percent, cpu_times, io, (swap_in, swap_out)

        def log_usage(prefix, duration, cpu_percent, cpu_times, io):
            message = '{prefix} - Wall time: {duration:.0f}s; ' \
                'CPU: {cpu_percent}; ' \
                'Read bytes: {io_read_bytes}; Write bytes: {io_write_bytes}; ' \
                'Read time: {io_read_time}; Write time: {io_write_time}'

            # XXX Some test harnesses are complaining about a string being
            # being fed into a 'f' formatter. This will help diagnose the
            # issue.
            cpu_percent_str = str(round(cpu_percent)) + '%' if cpu_percent else "Can't collect data"

                        prefix=prefix, duration=duration,
                        cpu_percent=cpu_percent_str, io_read_bytes=io.read_bytes,
                        io_write_bytes=io.write_bytes, io_read_time=io.read_time,

            except ValueError:
                self.warning("Exception when formatting: %s" %

        cpu_percent, cpu_times, io, (swap_in, swap_out) = resources(None)
        duration = rm.end_time - rm.start_time

        # Write out Perfherder data if configured.
        if self.resource_monitor_perfherder_id:
            perfherder_name, perfherder_options = self.resource_monitor_perfherder_id

            suites = []
            overall = []

            if cpu_percent:
                    'name': 'cpu_percent',
                    'value': cpu_percent,

                {'name': 'io_write_bytes', 'value': io.write_bytes},
                {'name': 'io.read_bytes', 'value': io.read_bytes},
                {'name': 'io_write_time', 'value': io.write_time},
                {'name': 'io_read_time', 'value': io.read_time},

                'name': '%s.overall' % perfherder_name,
                'extraOptions': perfherder_options + self.perfherder_resource_options(),
                'subtests': overall,


            for phase in rm.phases.keys():
                phase_duration = rm.phases[phase][1] - rm.phases[phase][0]
                subtests = [
                        'name': 'time',
                        'value': phase_duration,
                cpu_percent = rm.aggregate_cpu_percent(phase=phase,
                if cpu_percent is not None:
                        'name': 'cpu_percent',
                        'value': rm.aggregate_cpu_percent(phase=phase,

                # We don't report I/O during each step because measured I/O
                # is system I/O and that I/O can be delayed (e.g. writes will
                # buffer before being flushed and recorded in our metrics).
                    'name': '%s.%s' % (perfherder_name, phase),
                    'subtests': subtests,

            data = {
                'framework': {'name': 'job_resource_usage'},
                'suites': suites,

            schema_path = os.path.join(external_tools_path,
            with open(schema_path, 'rb') as fh:
                schema = json.load(fh)

            # this will throw an exception that causes the job to fail if the
            # perfherder data is not valid -- please don't change this
            # behaviour, otherwise people will inadvertently break this
            # functionality
            self.info('Validating Perfherder data against %s' % schema_path)
            jsonschema.validate(data, schema)
            self.info('PERFHERDER_DATA: %s' % json.dumps(data))

        log_usage('Total resource usage', duration, cpu_percent, cpu_times, io)

        # Print special messages so usage shows up in Treeherder.
        if cpu_percent:
            self._tinderbox_print('CPU usage<br/>{:,.1f}%'.format(

        self._tinderbox_print('I/O read bytes / time<br/>{:,} / {:,}'.format(
                              io.read_bytes, io.read_time))
        self._tinderbox_print('I/O write bytes / time<br/>{:,} / {:,}'.format(
                              io.write_bytes, io.write_time))

        # Print CPU components having >1%. "cpu_times" is a data structure
        # whose attributes are measurements. Ideally we'd have an API that
        # returned just the measurements as a dict or something.
        cpu_attrs = []
        for attr in sorted(dir(cpu_times)):
            if attr.startswith('_'):
            if attr in ('count', 'index'):

        cpu_total = sum(getattr(cpu_times, attr) for attr in cpu_attrs)

        for attr in cpu_attrs:
            value = getattr(cpu_times, attr)
            # cpu_total can be 0.0. Guard against division by 0.
            percent = value / cpu_total * 100.0 if cpu_total else 0.0

            if percent > 1.00:
                self._tinderbox_print('CPU {}<br/>{:,.1f} ({:,.1f}%)'.format(
                                      attr, value, percent))

        # Swap on Windows isn't reported by psutil.
        if not self._is_windows():
            self._tinderbox_print('Swap in / out<br/>{:,} / {:,}'.format(
                                  swap_in, swap_out))

        for phase in rm.phases.keys():
            start_time, end_time = rm.phases[phase]
            cpu_percent, cpu_times, io, swap = resources(phase)
            log_usage(phase, end_time - start_time, cpu_percent, cpu_times, io)

    def _tinderbox_print(self, message):
        self.info('TinderboxPrint: %s' % message)

# This needs to be inherited only if you have already inherited ScriptMixin
class Python3Virtualenv(object):
    ''' Support Python3.5+ virtualenv creation.'''
    py3_initialized_venv = False

    def py3_venv_configuration(self, python_path, venv_path):
        '''We don't use __init__ to allow integrating with other mixins.

        python_path - Path to Python 3 binary.
        venv_path - Path to virtual environment to be created.
        self.py3_initialized_venv = True
        self.py3_python_path = os.path.abspath(python_path)
        version = self.get_output_from_command(
                    [self.py3_python_path, '--version'], env=self.query_env()).split()[-1]
        # Using -m venv is only used on 3.5+ versions
        assert version > '3.5.0'
        self.py3_venv_path = os.path.abspath(venv_path)
        self.py3_pip_path = os.path.join(self.py3_path_to_executables(), 'pip')

    def py3_path_to_executables(self):
        platform = self.platform_name()
        if platform.startswith('win'):
            return os.path.join(self.py3_venv_path, 'Scripts')
            return os.path.join(self.py3_venv_path, 'bin')

    def py3_venv_initialized(func):
        def call(self, *args, **kwargs):
            if not self.py3_initialized_venv:
                raise Exception('You need to call py3_venv_configuration() '
                                'before using this method.')
            func(self, *args, **kwargs)
        return call

    def py3_create_venv(self):
        '''Create Python environment with python3 -m venv /path/to/venv.'''
        if os.path.exists(self.py3_venv_path):
            self.info("Virtualenv %s appears to already exist; skipping "
                      "virtualenv creation." % self.py3_venv_path)
            self.info('Running command...')
                '%s -m venv %s' % (self.py3_python_path, self.py3_venv_path),

    def py3_install_modules(self, modules,
        if not os.path.exists(self.py3_venv_path):
            raise Exception('You need to call py3_create_venv() first.')

        for m in modules:
            cmd = [self.py3_pip_path, 'install']
            if use_mozharness_pip_config:
                cmd += self._mozharness_pip_args()
            cmd += [m]
            self.run_command(cmd, env=self.query_env())

    def _mozharness_pip_args(self):
        '''We have information in Mozharness configs that apply to pip'''
        c = self.config
        pip_args = []
        # To avoid timeouts with our pypi server, increase default timeout:
        # https://bugzilla.mozilla.org/show_bug.cgi?id=1007230#c802
        pip_args += ['--timeout', str(c.get('pip_timeout', 120))]

        if c.get('find_links') and not c["pip_index"]:
            pip_args += ['--no-index']

        # Add --find-links pages to look at. Add --trusted-host automatically if
        # the host isn't secure. This allows modern versions of pip to connect
        # without requiring an override.
        trusted_hosts = set()
        for link in c.get('find_links', []):
            parsed = urlparse.urlparse(link)

            except socket.gaierror as e:
                self.info('error resolving %s (ignoring): %s' %
                          (parsed.hostname, e.message))

            pip_args += ["--find-links", link]
            if parsed.scheme != 'https':

        for host in sorted(trusted_hosts):
            pip_args += ['--trusted-host', host]

        return pip_args

    def py3_install_requirement_files(self, requirements, pip_args=[],
        requirements - You can specify multiple requirements paths
        cmd = [self.py3_pip_path, 'install']
        cmd += pip_args

        if use_mozharness_pip_config:
            cmd += self._mozharness_pip_args()

        for requirement_path in requirements:
            cmd += ['-r', requirement_path]

        self.run_command(cmd, env=self.query_env())

# __main__ {{{1

if __name__ == '__main__':
    '''TODO: unit tests.