1# Template for all Python Scripts in this repository
2parameters:
3  SourceDirectory: ''
4  BasePathLength: 49
5
6steps:
7  - task: PythonScript@0
8    displayName: Analyze Path Lengths
9    inputs:
10      scriptSource: inline
11      script: |
12        # Verifies Length of file path for all files in the SourceDirectory.
13        # File paths and directory paths must be less than 260 and 248 characters respectively on windows OS
14        # Repo users get a limited number of characters for the repo clone path. As Specified by the BasePathLength parameter.
15        # Script makes sure that paths in the repo are less than 260 and 248 for files and directories respectively after adding the BasePathLength.
16        import os
17        import sys
18
19        source_directory = r'${{ parameters.SourceDirectory }}'
20        break_switch = False
21        long_file_paths = []
22        long_dir_paths = []
23
24        def pluralize(string, plural_string, count):
25            return plural_string if count > 1 else string
26
27        print('Analyzing length of paths...')
28        for root, dirs, files in os.walk('{0}'.format(source_directory)):
29            for file in files:
30                file_path = os.path.relpath(os.path.join(root, file), source_directory)
31                if ((len(file_path) + ${{ parameters.BasePathLength }}) > 260):
32                    long_file_paths.append(file_path)
33
34                dir_path = os.path.relpath(root, source_directory)
35                if ((len(dir_path) + ${{ parameters.BasePathLength }}) > 248):
36                    long_dir_paths.append(dir_path)
37
38        if (len(long_file_paths) > 0):
39            print('With a base path length of {0} the following file path{1} exceed the allow path length of 260 characters'.format(${{ parameters.BasePathLength }}, pluralize('', 's', len(long_file_paths))))
40            print(*long_file_paths, sep = "\n")
41            break_switch = True
42
43        if (len(long_dir_paths) > 0):
44            print('With a base path length of {0} the following directory path{1} exceed the allow path length of 248 characters'.format(${{ parameters.BasePathLength }}, pluralize('', 's', len(long_dir_paths))))
45            print(*long_dir_paths, sep = "\n")
46            break_switch = True
47
48        if break_switch == True:
49            print("Some file paths are too long. Please reduce path lengths")
50            exit(1)
51