Coverage for portality/lib/paths.py: 91%

22 statements  

« prev     ^ index     » next       coverage.py v6.4.2, created at 2022-08-30 11:09 +0100

1import os 

2import tempfile 

3from pathlib import Path 

4 

5 

6def rel2abs(file, *args): 

7 file = os.path.realpath(file) 

8 if os.path.isfile(file): 

9 file = os.path.dirname(file) 

10 return os.path.abspath(os.path.join(file, *args)) 

11 

12 

13def list_subdirs(path): 

14 return [x for x in os.listdir(path) if os.path.isdir(os.path.join(path, x))] 

15 

16 

17def get_project_root(): 

18 """ Should return folder path of `doaj` """ 

19 return Path(os.path.dirname(os.path.dirname(__file__))).parent 

20 

21 

22def create_tmp_dir(is_auto_mkdir=False) -> Path: 

23 num_retry = 20 

24 for _ in range(num_retry): 

25 path = Path(tempfile.NamedTemporaryFile().name) 

26 if not path.exists(): 

27 break 

28 else: 

29 raise EnvironmentError(f'create tmp dir retry [{num_retry}] failed') 

30 

31 if is_auto_mkdir: 

32 path.mkdir(parents=True, exist_ok=True) 

33 return path