1#!/usr/bin/env python3

2# -*- coding: utf-8 -*-

3#

4# graphic_memory_info.py

5#

6# This file is part of Checkbox.

7#

8# Copyright 2012 Canonical Ltd.

9#

10# Authors: Shawn Wang <shawn.wang@canonical.com>

11#

12# Checkbox is free software: you can redistribute it and/or modify

13# it under the terms of the GNU General Public License version 3,

14# as published by the Free Software Foundation.

15

16#

17# Checkbox is distributed in the hope that it will be useful,

18# but WITHOUT ANY WARRANTY; without even the implied warranty of

19# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the

20# GNU General Public License for more details.

21#

22# You should have received a copy of the GNU General Public License

23# along with Checkbox. If not, see <http://www.gnu.org/licenses/>.

24

25"""

26 The graphic_memory_info.py got information from lspci

27"""

28

29 import re

30 import sys

31 import subprocess

32

33# 00:01.0 VGA compatible controller: \

34# Advanced Micro Devices [AMD] nee ATI Wrestler [Radeon HD 6320] \

35# (prog-if 00 [VGA controller])

36

37

38 def vgamem_paser(data=None):

39 """Parsing type vga and find memory information"""

40

41 device = None

42 vgamems = list()

43 for line in data.split("\n"):

44 for match in re.finditer(r"(\d\d:\d\d\.\d) VGA(.+): (.+)", line):

45 device = match.group(1)

46 name = match.group(3)

47 if device is None:

48 continue

49 # Memory at e0000000 (32-bit, prefetchable) [size=256M]

50 for match in re.finditer(

51 r"Memory(.+) prefetchable\) \[size=(\d+)M\]", line

52 ):

53 vgamem_size = match.group(2)

54 vgamem = {

55 "device": device,

56 "name": name,

57 "vgamem_size": vgamem_size,

58 }

59 vgamems.append(vgamem)

60 return vgamems

61

62

63 def main():

64 """main function

65 lspci -v -s 00:01.0 | grep ' prefetchable'

66 """

67

68 try:

69 data = subprocess.check_output(

70 ["lspci", "-v"], universal_newlines=True

71 )

72 except subprocess.CalledProcessError as exc:

73 return exc.returncode

74

75 vgamems = vgamem_paser(data)

76 for vgamem in vgamems:

77 output_str = "Device({0})\t Name: {1}\tVGA Memory Size: {2}M"

78 print(

79 output_str.format(

80 vgamem["device"], vgamem["name"], vgamem["vgamem_size"]

81 )

82 )

83

84

85 if __name__ == "__main__":

86 sys.exit(main())