Esempio n. 1
0
def disk_space_avail(dn, timeout=5):
    def _disk_space_avail():
        if os.path.exists(dn):
            try:
                stat_info = os.statvfs(dn)
                return stat_info.f_bavail * stat_info.f_bsize / 1024**2
            except Exception:
                import ctypes
                free_bytes = ctypes.c_ulonglong(0)
                ctypes.windll.kernel32.GetDiskFreeSpaceExW(
                    ctypes.c_wchar_p(dn), None, None,
                    ctypes.pointer(free_bytes))
                return free_bytes.value / 1024**2
        return -1

    try:
        return hang_protection(_disk_space_avail, timeout)
    except TimeoutException:
        logging.getLogger('console').critical(
            'Unable to get free disk space for directory %s after waiting for %d sec!\n'
            % (dn, timeout) +
            'The file system is probably hanging or corrupted' +
            ' - try to check the free disk space manually. ' +
            'Refer to the documentation to disable checking the free disk space - at your own risk'
        )
        exit_without_cleanup(os.EX_OSERR)
Esempio n. 2
0
def run_command(cmd, args, fd_map, env):  # run command by replacing the current process
	def _safe_close(file_descriptor):
		try:
			os.close(file_descriptor)
		except Exception:
			pass

	for fd_target, fd_source in fd_map.items():
		os.dup2(fd_source, fd_target)  # set stdin/stdout/stderr
	try:
		fd_max = os.sysconf('SC_OPEN_MAX')
	except Exception:
		fd_max = 256
	for fd_open in irange(3, fd_max):  # close inherited file descriptors except for std{in/out/err}
		_safe_close(fd_open)
	try:
		os.execve(cmd, args, env)  # replace process - this command DOES NOT RETURN if successful!
	except Exception:
		pass
	error_msg_list = [
		'== grid-control process error ==',
		'        pid: %s' % os.getpid(),
		'     fd map: %s' % repr(fd_map),
		'environment: %s' % repr(env),
		'    command: %s' % repr(cmd),
		'  arguments: %s' % repr(args),
		'  exception: %s' % repr(sys.exc_info()[1]),
	]
	sys.stderr.write(str.join('\n', error_msg_list))
	for fd_std in [0, 1, 2]:
		_safe_close(fd_std)
	exit_without_cleanup(os.EX_OSERR)  # exit forked process with OS error
Esempio n. 3
0
def run_process(cmd, args, fd_child_stdin, fd_child_stdout, fd_child_stderr, env):
	for fd_target, fd_source in enumerate([fd_child_stdin, fd_child_stdout, fd_child_stderr]):
		os.dup2(fd_source, fd_target) # set stdin/stdout/stderr
	for fd in irange(3, FD_MAX):
		close_safe(fd)
	try:
		os.execve(cmd, args, env)
	except Exception:
		err_msg = 'Error while calling os.execv(%s, %s):' % (repr(cmd), repr(args))
		sys.stderr.write(err_msg + repr(sys.exc_info()[1]))
		for fd in [0, 1, 2]:
			close_safe(fd)
		exit_without_cleanup(os.EX_OSERR)
	exit_without_cleanup(os.EX_OK)
Esempio n. 4
0
def freeSpace(dn, timeout = 5):
	def freeSpace_int():
		if os.path.exists(dn):
			try:
				stat_info = os.statvfs(dn)
				return stat_info.f_bavail * stat_info.f_bsize / 1024**2
			except Exception:
				import ctypes
				free_bytes = ctypes.c_ulonglong(0)
				ctypes.windll.kernel32.GetDiskFreeSpaceExW(ctypes.c_wchar_p(dn), None, None, ctypes.pointer(free_bytes))
				return free_bytes.value / 1024**2
		return -1

	try:
		return hang_protection(freeSpace_int, timeout)
	except TimeoutException:
		sys.stderr.write(
			'Unable to get free disk space for directory %s after waiting for %d sec!\n' % (dn, timeout) +
			'The file system is probably hanging or corrupted - try to check the free disk space manually. ' +
			'Refer to the documentation to disable checking the free disk space - at your own risk')
		exit_without_cleanup(os.EX_OSERR)
Esempio n. 5
0
def run_command(cmd, args, fd_map,
                env):  # run command by replacing the current process
    def _safe_close(file_descriptor):
        try:
            os.close(file_descriptor)
        except Exception:
            pass

    for fd_target, fd_source in fd_map.items():
        os.dup2(fd_source, fd_target)  # set stdin/stdout/stderr
    try:
        fd_max = os.sysconf('SC_OPEN_MAX')
    except Exception:
        fd_max = 256
    for fd_open in irange(
            3, fd_max
    ):  # close inherited file descriptors except for std{in/out/err}
        _safe_close(fd_open)
    try:
        os.execve(
            cmd, args, env
        )  # replace process - this command DOES NOT RETURN if successful!
    except Exception:
        pass
    error_msg_list = [
        '== grid-control process error ==',
        '        pid: %s' % os.getpid(),
        '     fd map: %s' % repr(fd_map),
        'environment: %s' % repr(env),
        '    command: %s' % repr(cmd),
        '  arguments: %s' % repr(args),
        '  exception: %s' % repr(sys.exc_info()[1]),
    ]
    sys.stderr.write(str.join('\n', error_msg_list))
    for fd_std in [0, 1, 2]:
        _safe_close(fd_std)
    exit_without_cleanup(os.EX_OSERR)  # exit forked process with OS error
Esempio n. 6
0
def disk_space_avail(dn, timeout=5):
	def _disk_space_avail():
		if os.path.exists(dn):
			try:
				stat_info = os.statvfs(dn)
				return stat_info.f_bavail * stat_info.f_bsize / 1024 ** 2
			except Exception:
				import ctypes
				free_bytes = ctypes.c_ulonglong(0)
				ctypes.windll.kernel32.GetDiskFreeSpaceExW(
					ctypes.c_wchar_p(dn), None, None, ctypes.pointer(free_bytes))
				return free_bytes.value / 1024 ** 2
		return -1

	try:
		return hang_protection(_disk_space_avail, timeout)
	except TimeoutException:
		logging.getLogger('console').critical(
			'Unable to get free disk space for directory %s after waiting for %d sec!\n' % (dn, timeout) +
			'The file system is probably hanging or corrupted' +
			' - try to check the free disk space manually. ' +
			'Refer to the documentation to disable checking the free disk space - at your own risk')
		time.sleep(1)  # give GUI report the possibility to log its output
		exit_without_cleanup(os.EX_OSERR)