#! /usr/bin/env python

import os, re

def getvram(silent=True):
	"acquires the amount of video ram from the x-server log file and returns amount in MBytes; \
	returns 0 on error; fais silently if silent is true;"
	if silent:
		pipe=" 2>/dev/null"
	else:
		pipe=""
	xdpyinfo = os.popen("xdpyinfo"+pipe, "r")
	vendor = re.compile("vendor string\:.*").search(xdpyinfo.read(), 1)
	xdpyinfo.close()
	if vendor == None:
		if not silent:
			print "Error: getvram: could not get x-server vendor from xdpyinfo"
		return 0
	if re.compile("xfree86", re.I).search(vendor.group(), 1):
		log = "/var/log/XFree86.0.log"
	elif re.compile("x\.org", re.I).search(vendor.group(), 1):
		log = "/var/log/Xorg.0.log"
	else:
		if not silent:
			print "Error: getvram: unknown x-server vendor"
		return 0
	xlog = os.popen("grep -i videoram "+log+pipe, "r")
	vram = re.compile("videoram:\s*([0-9]*)\s*kbytes?", re.I).search(xlog.read(), 1)
	xlog.close()
	if vram == None:
		if not silent:
			print "Error: getvram: could not get videoRAM from x-server log file '"+log+"'"
		return 0
	return int(vram.groups()[0])//1024

print getvram(False)
