@@ -21,8 +21,9 @@ def parse_args():
21
21
formatter_class = argparse .RawDescriptionHelpFormatter ,
22
22
allow_abbrev = False )
23
23
24
- parser .add_argument ('-d' , '--directory' , required = True ,
25
- help = 'Directory to walk for sub-directory discovery' )
24
+ parser .add_argument ('-d' , '--directory' , required = True , action = 'append' ,
25
+ help = 'Directory to walk for sub-directory discovery \
26
+ (can be specified multiple times)' )
26
27
parser .add_argument ('-c' , '--create-links' , required = False ,
27
28
help = 'Create links for each directory found in \
28
29
directory given' )
@@ -37,34 +38,55 @@ def parse_args():
37
38
return args
38
39
39
40
40
- def get_subfolder_list (directory , create_links = None ):
41
- """Return subfolder list of a directory """
41
+ def get_subfolder_list (directories , create_links = None ):
42
+ """Return subfolder list of directories """
42
43
dirlist = []
43
-
44
- if create_links is not None :
45
- if not os .path .exists (create_links ):
46
- os .makedirs (create_links )
47
- symbase = os .path .basename (directory )
48
- symlink = create_links + os .path .sep + symbase
49
- if not os .path .exists (symlink ):
50
- os .symlink (directory , symlink )
51
- dirlist .append (symlink )
52
- else :
53
- dirlist .append (directory )
54
-
55
- for root , dirs , _ in os .walk (directory , topdown = True ):
56
- dirs .sort ()
57
- for subdir in dirs :
58
- if create_links is not None :
59
- targetdirectory = os .path .join (root , subdir )
60
- reldir = os .path .relpath (targetdirectory , directory )
61
- linkname = symbase + '_' + reldir .replace (os .path .sep , '_' )
62
- symlink = create_links + os .path .sep + linkname
63
- if not os .path .exists (symlink ):
64
- os .symlink (targetdirectory , symlink )
65
- dirlist .append (symlink )
66
- else :
67
- dirlist .append (os .path .join (root , subdir ))
44
+ used_link_names = set ()
45
+
46
+ for directory in directories :
47
+ if create_links is not None :
48
+ if not os .path .exists (create_links ):
49
+ os .makedirs (create_links )
50
+ symbase = os .path .basename (directory )
51
+
52
+ # Ensure unique link name for the base directory
53
+ linkname = symbase
54
+ counter = 1
55
+ while linkname in used_link_names :
56
+ linkname = f"{ symbase } _{ counter } "
57
+ counter += 1
58
+ used_link_names .add (linkname )
59
+
60
+ symlink = create_links + os .path .sep + linkname
61
+ if not os .path .exists (symlink ):
62
+ os .symlink (directory , symlink )
63
+ dirlist .append (symlink )
64
+ else :
65
+ dirlist .append (directory )
66
+
67
+ for root , dirs , _ in os .walk (directory , topdown = True ):
68
+ dirs .sort ()
69
+ for subdir in dirs :
70
+ if create_links is not None :
71
+ targetdirectory = os .path .join (root , subdir )
72
+ reldir = os .path .relpath (targetdirectory , directory )
73
+ base_linkname = (os .path .basename (directory ) +
74
+ '_' + reldir .replace (os .path .sep , '_' ))
75
+
76
+ # Ensure unique link name for subdirectories
77
+ linkname = base_linkname
78
+ counter = 1
79
+ while linkname in used_link_names :
80
+ linkname = f"{ base_linkname } _{ counter } "
81
+ counter += 1
82
+ used_link_names .add (linkname )
83
+
84
+ symlink = create_links + os .path .sep + linkname
85
+ if not os .path .exists (symlink ):
86
+ os .symlink (targetdirectory , symlink )
87
+ dirlist .append (symlink )
88
+ else :
89
+ dirlist .append (os .path .join (root , subdir ))
68
90
69
91
return dirlist
70
92
@@ -108,7 +130,8 @@ def main():
108
130
"""Parse command line arguments and take respective actions"""
109
131
args = parse_args ()
110
132
111
- dirs = get_subfolder_list (args .directory , args .create_links )
133
+ directories = sorted (set (args .directory ))
134
+ dirs = get_subfolder_list (directories , args .create_links )
112
135
gen_out_file (args .out_file , dirs )
113
136
114
137
# Always touch trigger file to ensure json files are updated
0 commit comments