def _installhg()

in pylib/mercurial-support/run-tests.py [0:0]


    def _installhg(self):
        """Install hg into the test environment.

        This will also configure hg with the appropriate testing settings.
        """
        vlog("# Performing temporary installation of HG")
        installerrs = os.path.join(self._hgtmp, b"install.err")
        compiler = ''
        if self.options.compiler:
            compiler = '--compiler ' + self.options.compiler
        if self.options.pure:
            pure = b"--pure"
        else:
            pure = b""

        # Run installer in hg root
        script = os.path.realpath(sys.argv[0])
        exe = sysexecutable
        if PYTHON3:
            compiler = _bytespath(compiler)
            script = _bytespath(script)
            exe = _bytespath(exe)
        hgroot = os.path.dirname(os.path.dirname(script))
        self._hgroot = hgroot
        os.chdir(hgroot)
        nohome = b'--home=""'
        if os.name == 'nt':
            # The --home="" trick works only on OS where os.sep == '/'
            # because of a distutils convert_path() fast-path. Avoid it at
            # least on Windows for now, deal with .pydistutils.cfg bugs
            # when they happen.
            nohome = b''
        cmd = (
            b'"%(exe)s" setup.py %(pure)s clean --all'
            b' build %(compiler)s --build-base="%(base)s"'
            b' install --force --prefix="%(prefix)s"'
            b' --install-lib="%(libdir)s"'
            b' --install-scripts="%(bindir)s" %(nohome)s >%(logfile)s 2>&1'
            % {
                b'exe': exe,
                b'pure': pure,
                b'compiler': compiler,
                b'base': os.path.join(self._hgtmp, b"build"),
                b'prefix': self._installdir,
                b'libdir': self._pythondir,
                b'bindir': self._bindir,
                b'nohome': nohome,
                b'logfile': installerrs,
            }
        )

        # setuptools requires install directories to exist.
        def makedirs(p):
            try:
                os.makedirs(p)
            except OSError as e:
                if e.errno != errno.EEXIST:
                    raise

        makedirs(self._pythondir)
        makedirs(self._bindir)

        vlog("# Running", cmd.decode("utf-8"))
        if subprocess.call(_strpath(cmd), shell=True) == 0:
            if not self.options.verbose:
                try:
                    os.remove(installerrs)
                except OSError as e:
                    if e.errno != errno.ENOENT:
                        raise
        else:
            with open(installerrs, 'rb') as f:
                for line in f:
                    if PYTHON3:
                        sys.stdout.buffer.write(line)
                    else:
                        sys.stdout.write(line)
            sys.exit(1)
        os.chdir(self._testdir)

        self._usecorrectpython()

        if self.options.py3_warnings and not self.options.anycoverage:
            vlog("# Updating hg command to enable Py3k Warnings switch")
            with open(os.path.join(self._bindir, 'hg'), 'rb') as f:
                lines = [line.rstrip() for line in f]
                lines[0] += ' -3'
            with open(os.path.join(self._bindir, 'hg'), 'wb') as f:
                for line in lines:
                    f.write(line + '\n')

        hgbat = os.path.join(self._bindir, b'hg.bat')
        if os.path.isfile(hgbat):
            # hg.bat expects to be put in bin/scripts while run-tests.py
            # installation layout put it in bin/ directly. Fix it
            with open(hgbat, 'rb') as f:
                data = f.read()
            if br'"%~dp0..\python" "%~dp0hg" %*' in data:
                data = data.replace(
                    br'"%~dp0..\python" "%~dp0hg" %*',
                    b'"%~dp0python" "%~dp0hg" %*',
                )
                with open(hgbat, 'wb') as f:
                    f.write(data)
            else:
                print('WARNING: cannot fix hg.bat reference to python.exe')

        if self.options.anycoverage:
            custom = os.path.join(
                osenvironb[b'RUNTESTDIR'], b'sitecustomize.py'
            )
            target = os.path.join(self._pythondir, b'sitecustomize.py')
            vlog('# Installing coverage trigger to %s' % target)
            shutil.copyfile(custom, target)
            rc = os.path.join(self._testdir, b'.coveragerc')
            vlog('# Installing coverage rc to %s' % rc)
            osenvironb[b'COVERAGE_PROCESS_START'] = rc
            covdir = os.path.join(self._installdir, b'..', b'coverage')
            try:
                os.mkdir(covdir)
            except OSError as e:
                if e.errno != errno.EEXIST:
                    raise

            osenvironb[b'COVERAGE_DIR'] = covdir