1#!/usr/bin/env python3

2# This file is part of Checkbox.

3#

4# Copyright 2024 Canonical Ltd.

5# Written by:

6# Hanhsuan Lee <hanhsuan.lee@canonical.com>

7#

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

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

10# as published by the Free Software Foundation.

11#

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

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

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

15# GNU General Public License for more details.

16#

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

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

19

20 import subprocess

21 import re

22 import os

23

24

25 class GLSupport:

26 """

27 This is a simple class to use unity_support_test to verify

28 OpenGL is supported or not

29 """

30

31 def remove_color_code(self, string: str) -> str:

32 """

33 Use to make the color code removing could be unit tested

34

35 :param string: the string that you would like to remove color code

36 """

37

38 return re.sub(r"\x1B(?:[@-Z\\-_]|\[[0-?]*[ -/]*[@-~])", "", string)

39

40 def is_support_opengl(self):

41 cr = os.getenv("CHECKBOX_RUNTIME", default="")

42 cmd = [

43 "{}/usr/lib/nux/unity_support_test".format(cr),

44 "-p",

45 ]

46 try:

47 rv = subprocess.run(

48 cmd,

49 universal_newlines=True,

50 stdout=subprocess.PIPE,

51 stderr=subprocess.STDOUT,

52 )

53 except (subprocess.CalledProcessError, FileNotFoundError) as e:

54 raise SystemExit("running cmd:[{}] fail:{}".format(cmd, repr(e)))

55 print(self.remove_color_code(rv.stdout))

56 if rv.returncode != 0:

57 raise SystemExit("Some OpenGL functions might not be supported")

58

59

60 if __name__ == "__main__":

61 GLSupport().is_support_opengl()