<commit_before>#!/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/.

from __future__ import with_statement

import ctypes
import os
import platform
import re
import subprocess
import time

verbose = False

isLinux = (platform.system() == 'Linux')
isMac = (platform.system() == 'Darwin')
# In Vista, Python 2.5.1 reports "Microsoft" - see http://bugs.python.org/issue1082
isWin = (platform.system() in ('Microsoft', 'Windows'))

########################
#  Platform Detection  #
########################

def macVer():
    '''
    If system is a Mac, return the mac type.
    '''
    assert platform.system() == 'Darwin'
    return [int(x) for x in platform.mac_ver()[0].split('.')]

def isVM():
    '''
    Returns the OS of the system, if system is a VM.
    '''
    vm = False
    # In VMware, shared folders are in z:, and we copy from the shared folders to avoid having
    # another copy of the repository in the VM.
    if (platform.uname()[2] == 'XP' \
            and os.path.exists(os.path.join('z:', os.sep, 'fuzzing'))) or \
        platform.uname()[0] == 'Linux' \
            and os.path.exists(os.path.join('/', 'mnt', 'hgfs', 'fuzzing')):
        assert not os.path.exists(normExpUserPath(os.path.join('~', 'fuzzing')))
        assert not os.path.exists(normExpUserPath(os.path.join('~', 'trees')))
        vm = True
    return ('Windows' if isWin else platform.system(), vm)

def getFreeSpace(folder, mulVar):
    '''
    Return folder/drive free space in bytes if mulVar is 0.
    Adapted from http://stackoverflow.com/a/2372171
    '''
    assert mulVar >= 0
    if platform.system() == 'Windows':
        free_bytes = ctypes.c_ulonglong(0)
        ctypes.windll.kernel32.GetDiskFreeSpaceExW(ctypes.c_wchar_p(folder), None, None, ctypes.pointer(free_bytes))
        retVal = float(free_bytes.value)
    else:
        retVal = float(os.statvfs(folder).f_bfree * os.statvfs(folder).f_frsize)

    return retVal / (1024 ** mulVar)

#####################
#  Shell Functions  #
#####################

def captureStdout(inputCmd, ignoreStderr=False, combineStderr=False, ignoreExitCode=False,
                  currWorkingDir=os.getcwdu(), env=os.environ, verbosity=False):
    '''
    Captures standard output, returns the output as a string, along with the return value.
    '''
    vdump(shellify(inputCmd))
    cmd = []
    for el in inputCmd:
        if (el.startswith('"') and el.endswith('"')):
            cmd.append(str(el[1:-1]))
        else:
            cmd.append(str(el))
    assert cmd != []
    p = subprocess.Popen(cmd,
        stdin = subprocess.PIPE,
        stdout = subprocess.PIPE,
        stderr = subprocess.STDOUT if combineStderr else subprocess.PIPE,
        cwd=currWorkingDir, env=env)
    (stdout, stderr) = p.communicate()
    if not ignoreExitCode and p.returncode != 0:
        # Potential problem area: Note that having a non-zero exit code does not mean that the
        # operation did not succeed, for example when compiling a shell. A non-zero exit code can
        # appear even though a shell compiled successfully. This issue has been bypassed in the
        # makeShell function in autoBisect.
        # Pymake in builds earlier than revision 232553f741a0 did not support the '-s' option.
        if 'no such option: -s' not in stdout:
            print 'Nonzero exit code from: '
            print '  ' + shellify(cmd)
            print stdout
        if stderr is not None:
            print stderr
        # Pymake in builds earlier than revision 232553f741a0 did not support the '-s' option.
        if 'hg pull: option --rebase not recognized' not in stdout and \
          'no such option: -s' not in stdout:
            if isWin and stderr and 'Permission denied' in stderr and \
                    'configure: error: installation or configuration problem: ' + \
                    'C++ compiler cannot create executables.' in stderr:
                raise Exception('Windows conftest.exe configuration permission problem')<commit_msg>Remove obsolete comment in subprocesses.py.<commit_after>#!/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/.

from __future__ import with_statement

import ctypes
import os
import platform
import re
import subprocess
import time

verbose = False

isLinux = (platform.system() == 'Linux')
isMac = (platform.system() == 'Darwin')
# In Vista, Python 2.5.1 reports "Microsoft" - see http://bugs.python.org/issue1082
isWin = (platform.system() in ('Microsoft', 'Windows'))

########################
#  Platform Detection  #
########################

def macVer():
    '''
    If system is a Mac, return the mac type.
    '''
    assert platform.system() == 'Darwin'
    return [int(x) for x in platform.mac_ver()[0].split('.')]

def isVM():
    '''
    Returns the OS of the system, if system is a VM.
    '''
    vm = False
    # In VMware, shared folders are in z:, and we copy from the shared folders to avoid having
    # another copy of the repository in the VM.
    if (platform.uname()[2] == 'XP' \
            and os.path.exists(os.path.join('z:', os.sep, 'fuzzing'))) or \
        platform.uname()[0] == 'Linux' \
            and os.path.exists(os.path.join('/', 'mnt', 'hgfs', 'fuzzing')):
        assert not os.path.exists(normExpUserPath(os.path.join('~', 'fuzzing')))
        assert not os.path.exists(normExpUserPath(os.path.join('~', 'trees')))
        vm = True
    return ('Windows' if isWin else platform.system(), vm)

def getFreeSpace(folder, mulVar):
    '''
    Return folder/drive free space in bytes if mulVar is 0.
    Adapted from http://stackoverflow.com/a/2372171
    '''
    assert mulVar >= 0
    if platform.system() == 'Windows':
        free_bytes = ctypes.c_ulonglong(0)
        ctypes.windll.kernel32.GetDiskFreeSpaceExW(ctypes.c_wchar_p(folder), None, None, ctypes.pointer(free_bytes))
        retVal = float(free_bytes.value)
    else:
        retVal = float(os.statvfs(folder).f_bfree * os.statvfs(folder).f_frsize)

    return retVal / (1024 ** mulVar)

#####################
#  Shell Functions  #
#####################

def captureStdout(inputCmd, ignoreStderr=False, combineStderr=False, ignoreExitCode=False,
                  currWorkingDir=os.getcwdu(), env=os.environ, verbosity=False):
    '''
    Captures standard output, returns the output as a string, along with the return value.
    '''
    vdump(shellify(inputCmd))
    cmd = []
    for el in inputCmd:
        if (el.startswith('"') and el.endswith('"')):
            cmd.append(str(el[1:-1]))
        else:
            cmd.append(str(el))
    assert cmd != []
    p = subprocess.Popen(cmd,
        stdin = subprocess.PIPE,
        stdout = subprocess.PIPE,
        stderr = subprocess.STDOUT if combineStderr else subprocess.PIPE,
        cwd=currWorkingDir, env=env)
    (stdout, stderr) = p.communicate()
    if not ignoreExitCode and p.returncode != 0:
        # Potential problem area: Note that having a non-zero exit code does not mean that the
        # operation did not succeed, for example when compiling a shell. A non-zero exit code can
        # appear even though a shell compiled successfully.
        # Pymake in builds earlier than revision 232553f741a0 did not support the '-s' option.
        if 'no such option: -s' not in stdout:
            print 'Nonzero exit code from: '
            print '  ' + shellify(cmd)
            print stdout
        if stderr is not None:
            print stderr
        # Pymake in builds earlier than revision 232553f741a0 did not support the '-s' option.
        if 'hg pull: option --rebase not recognized' not in stdout and \
          'no such option: -s' not in stdout:
            if isWin and stderr and 'Permission denied' in stderr and \
                    'configure: error: installation or configuration problem: ' + \
                    'C++ compiler cannot create executables.' in stderr:
                raise Exception('Windows conftest.exe configuration permission problem')
{'fraction_non_alphanumeric': 0.13347815592565773, 'fraction_numerical': 0.012792662321988897, 'mean_word_length': 2.7668181818181816, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6919736', 'n_tokens_mistral': 2585, 'n_tokens_neox': 2468, 'n_words': 818}