Coverage for src/prisma/binaries/platform.py: 84%

47 statements  

« prev     ^ index     » next       coverage.py v7.2.7, created at 2024-08-27 18:25 +0000

1import re 

2import sys 

3import subprocess 

4import platform as _platform 

5from functools import lru_cache 

6from typing import Tuple 

7 

8 

9def name() -> str: 

10 return _platform.system().lower() 

11 

12 

13def check_for_extension(file: str) -> str: 

14 if name() == 'windows' and '.exe' not in file: 

15 if '.gz' in file: 15 ↛ 16line 15 didn't jump to line 16, because the condition on line 15 was never true

16 return file.replace('.gz', '.exe.gz') 

17 return file + '.exe' 

18 return file 

19 

20 

21def linux_distro() -> str: 

22 # NOTE: this has only been tested on ubuntu 

23 distro_id, distro_id_like = _get_linux_distro_details() 

24 if distro_id == 'alpine': 24 ↛ 25line 24 didn't jump to line 25, because the condition on line 24 was never true

25 return 'alpine' 

26 

27 if any(distro in distro_id_like for distro in ['centos', 'fedora', 'rhel']): 27 ↛ 28line 27 didn't jump to line 28, because the condition on line 27 was never true

28 return 'rhel' 

29 

30 # default to debian 

31 return 'debian' 

32 

33 

34def _get_linux_distro_details() -> Tuple[str, str]: 

35 process = subprocess.run(['cat', '/etc/os-release'], stdout=subprocess.PIPE, check=True) 

36 output = str(process.stdout, sys.getdefaultencoding()) 

37 

38 match = re.search(r'ID="?([^"\n]*)"?', output) 

39 distro_id = match.group(1) if match else '' 

40 

41 match = re.search(r'ID_LIKE="?([^"\n]*)"?', output) 

42 distro_id_like = match.group(1) if match else '' 

43 return distro_id, distro_id_like 

44 

45 

46@lru_cache(maxsize=None) 

47def binary_platform() -> str: 

48 platform = name() 

49 if platform != 'linux': 

50 return platform 

51 

52 distro = linux_distro() 

53 if distro == 'alpine': 53 ↛ 54line 53 didn't jump to line 54, because the condition on line 53 was never true

54 return 'linux-musl' 

55 

56 ssl = get_openssl() 

57 return f'{distro}-openssl-{ssl}' 

58 

59 

60def get_openssl() -> str: 

61 process = subprocess.run(['openssl', 'version', '-v'], stdout=subprocess.PIPE, check=True) 

62 return parse_openssl_version(str(process.stdout, sys.getdefaultencoding())) 

63 

64 

65def parse_openssl_version(string: str) -> str: 

66 match = re.match(r'^OpenSSL\s(\d+\.\d+)\.\d+', string) 

67 if match is None: 67 ↛ 69line 67 didn't jump to line 69, because the condition on line 67 was never true

68 # default 

69 return '1.1.x' 

70 

71 return match.group(1) + '.x'