Coverage for src / template / methods / directory.py: 94%

23 statements  

« prev     ^ index     » next       coverage.py v7.12.0, created at 2025-11-21 16:36 +0000

1import os 

2import sys 

3import urllib 

4 

5 

6def accept(tpl, dirpath): 

7 """ 

8 Uses dirpath and not tpl.path 

9 """ 

10 # Has to be a directory 

11 if not os.path.isdir(dirpath): 

12 return False 

13 

14 # filepath is the path of the scaffold.yml file, and 

15 # must exist 

16 filepath = os.path.join(dirpath, tpl.filename) 

17 if not os.path.isfile(filepath): 

18 return False 

19 

20 # Ensure that filepath is relative to the tpl.path 

21 real_dirpath = os.path.realpath(dirpath) 

22 real_filepath = os.path.realpath(filepath) 

23 if not real_filepath.startswith(real_dirpath): 23 ↛ 24line 23 didn't jump to line 24 because the condition on line 23 was never true

24 sys.exit(f'error: File "{tpl.filename}" is not relative to the template path "{dirpath}"') 

25 

26 return real_dirpath 

27 

28 

29def find(tpl): 

30 # A local directory can also be a git repository 

31 # So if a branch is specified, don't look for a local 

32 # directory, but return directly to force the git handler 

33 if tpl.branch: 

34 return False 

35 

36 # If a scheme is detected, it's also not a local directory 

37 urlpath = urllib.parse.urlparse(tpl.path) 

38 if urlpath.scheme in ["https", "http"]: 

39 return False 

40 

41 if os.path.isfile(tpl.path): 

42 sys.exit( 

43 "error: positional argument TEMPLATE should point to a directory, or remote git repository, and not a file", 

44 ) 

45 

46 return accept(tpl, tpl.path)