Coverage for kiertotie/tree.py: 0.00%

29 statements  

« prev     ^ index     » next       coverage.py v7.4.1, created at 2024-02-04 18:44:05 +00:00

1#! /usr/bin/env python 

2"""Creating the folders for the hierarchy if they do not exist from the proxy data.""" 

3import datetime as dti 

4import os 

5import pathlib 

6from typing import Union 

7 

8from kiertotie import DASH, TS_FORMAT, load, log 

9 

10 

11def span( 

12 proxy_data_path: Union[str, pathlib.Path], 

13 anchor_path: Union[str, pathlib.Path, None] = None, 

14 verbose: bool = False, 

15) -> int: 

16 """Span the folder tree per proxy folder data.""" 

17 anchor = pathlib.Path.cwd() if anchor_path is None else pathlib.Path(anchor_path) 

18 log.debug(f'assuming anchor as ({anchor}) in span tree') 

19 

20 store_path = pathlib.Path(proxy_data_path) 

21 log.debug(f'loading proxy data from ({store_path}) in span tree') 

22 repo = load(store_path) 

23 

24 root_folder_str = store_path.name.split(DASH)[1] 

25 if root_folder_str == 'development': 

26 root_folder_str += '_releases' 

27 root_folder = pathlib.Path(root_folder_str) 

28 log.debug(f'assuming root folder as ({root_folder}) below anchor ({anchor}) in span tree') 

29 

30 folder_count = repo['count_folders'] 

31 log.debug(f'creating {folder_count} folders below the root') 

32 for folder in repo['tree']['folders']: # type: ignore 

33 folder_path = folder['path'] 

34 if folder_path == '.': 

35 continue 

36 path = anchor / root_folder / folder_path # type: ignore 

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

38 ts = ( 

39 dti.datetime.strptime(folder['timestamp'], TS_FORMAT) # type: ignore 

40 .replace(tzinfo=dti.timezone.utc) 

41 .timestamp() 

42 ) 

43 os.utime(path, times=(ts, ts)) 

44 

45 log.debug('folder hiearchy is complete in span tree') 

46 return 0