Coverage for yaptide/admin/git_submodules.py: 50%

32 statements  

« prev     ^ index     » next       coverage.py v7.4.4, created at 2024-07-01 12:55 +0000

1import subprocess 

2import shutil 

3import logging 

4from pathlib import Path 

5 

6 

7def find_git_executable(): 

8 """Function returns git executable path or None""" 

9 git_path = shutil.which('git') 

10 return git_path 

11 

12 

13def check_submodules_folders_exist(): 

14 """Function checking if submodules folders exists on system""" 

15 with open('.gitmodules') as f: 

16 logging.info("Found .gitmodules file") 

17 for line in f: 

18 if 'path' in line: 

19 logging.info("Found submodule line in .gitmodules: %s", line) 

20 submodule_path = line.split('=')[-1].strip() 

21 

22 # Check if folder exists and is not empty 

23 if not Path(submodule_path).exists() or not list(Path(submodule_path).iterdir()): 

24 submodule_name = submodule_path.split("/")[-1] 

25 raise RuntimeError( 

26 f"Submodule: '{submodule_name}' is missing! Please use: git submodule update --init --recursive" 

27 ) 

28 

29 

30def check_submodules(): 

31 """Function checking the status of Git submodules""" 

32 try: 

33 git_path = find_git_executable() 

34 

35 if git_path is None: 

36 logging.info("Git executable not found") 

37 check_submodules_folders_exist() 

38 return 

39 

40 logging.info("Found git executable path: %s", git_path) 

41 result = subprocess.run([git_path, "submodule", "status"], capture_output=True, text=True, check=True) 

42 for line in result.stdout.splitlines(): 

43 if line.startswith('-') or line.startswith('+'): 

44 submodule_name = line.split()[-1] 

45 raise RuntimeError( 

46 f"Submodule: '{submodule_name}' is missing! Please use: git submodule update --init --recursive") 

47 

48 except subprocess.CalledProcessError as e: 

49 logging.error("Error running 'git submodule status': %s", e)