Skip to content

Commit e6545b4

Browse files
authored
Various fixes & improvments in the modern distro validation script (#12387)
1 parent 719f865 commit e6545b4

File tree

1 file changed

+33
-10
lines changed

1 file changed

+33
-10
lines changed

distributions/validate-modern.py

Lines changed: 33 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,7 @@ def read_config_keys(config: configparser.ConfigParser) -> dict:
155155

156156
for section in config.sections():
157157
for key in config[section].keys():
158-
keys[f'{section}.{key}'] = config[section][key]
158+
keys[f'{section}.{key.lower()}'] = config[section][key]
159159

160160
return keys
161161

@@ -233,8 +233,31 @@ def list_directory(path: str):
233233

234234
return units
235235

236+
# Manually implemented because os.path.realpath tries to resolve local symlinks
237+
def linux_real_path(path: str):
238+
components = path.split('/')
236239

237-
def get_tar_file(tar, path: str, follow_symlink=False):
240+
result = []
241+
for e in components:
242+
if e == '.' or not e:
243+
continue
244+
elif e == '..':
245+
if result:
246+
del result[-1]
247+
continue
248+
249+
result.append(e)
250+
251+
real_path = '/'.join(result)
252+
if path and path[0] == '/':
253+
return '/' + real_path
254+
else:
255+
return real_path
256+
257+
def get_tar_file(tar, path: str, follow_symlink=False, symlink_depth=10):
258+
if symlink_depth < 0:
259+
print(f'Warning: Exceeded maximum symlink depth when reading: {path}')
260+
return None, None
238261

239262
# Tar members can be formated as /{path}, {path}, or ./{path}
240263
if path.startswith('/'):
@@ -247,9 +270,9 @@ def get_tar_file(tar, path: str, follow_symlink=False):
247270
def follow_if_symlink(info, path: str):
248271
if follow_symlink and info.issym():
249272
if info.linkpath.startswith('/'):
250-
return get_tar_file(tar, info.linkpath, follow_symlink=True)
273+
return get_tar_file(tar, info.linkpath, follow_symlink=True, symlink_depth=symlink_depth - 1)
251274
else:
252-
return get_tar_file(tar, f'{os.path.dirname(path)}/{info.linkpath}', follow_symlink=True)
275+
return get_tar_file(tar, linux_real_path(os.path.dirname(path) + '/' + info.linkpath), follow_symlink=True, symlink_depth=symlink_depth -1)
253276
else:
254277
return info, path
255278

@@ -268,9 +291,9 @@ def follow_if_symlink(info, path: str):
268291
parent_path = os.path.dirname(path)
269292
if parent_path != path:
270293
try:
271-
parent_info, real_parent_path = get_tar_file(tar, parent_path, follow_symlink=True)
272-
if real_parent_path != parent_path:
273-
return get_tar_file(tar, f'{real_parent_path}/{os.path.basename(path)}', follow_symlink=True)
294+
parent_info, real_parent_path = get_tar_file(tar, parent_path, follow_symlink=True, symlink_depth=symlink_depth - 1)
295+
if real_parent_path is not None and real_parent_path != parent_path:
296+
return get_tar_file(tar, f'{real_parent_path}/{os.path.basename(path)}', follow_symlink=True, symlink_depth=symlink_depth -1)
274297
except KeyError:
275298
pass
276299

@@ -327,7 +350,7 @@ def validate_config(path: str, valid_keys: list):
327350

328351
keys = read_config_keys(config)
329352

330-
unexpected_keys = [e for e in keys if e not in valid_keys]
353+
unexpected_keys = [e for e in keys if e.lower() not in valid_keys]
331354
if unexpected_keys:
332355
error(flavor, name, f'Found unexpected_keys in "{path}": {unexpected_keys}')
333356
else:
@@ -337,7 +360,7 @@ def validate_config(path: str, valid_keys: list):
337360

338361
defaultUid = None
339362
if validate_mode('/etc/wsl-distribution.conf', [oct(0o664), oct(0o644)], 0, 0):
340-
config = validate_config('/etc/wsl-distribution.conf', ['oobe.command', 'oobe.defaultuid', 'shortcut.icon', 'oobe.defaultname', 'windowsterminal.profileTemplate'])
363+
config = validate_config('/etc/wsl-distribution.conf', ['oobe.command', 'oobe.defaultuid', 'shortcut.icon', 'oobe.defaultname', 'windowsterminal.profiletemplate'])
341364

342365
if oobe_command := config.get('oobe.command', None):
343366
validate_mode(oobe_command, [oct(0o775), oct(0o755)], 0, 0)
@@ -432,7 +455,7 @@ def warning(flavor: str, distribution: str, message: str):
432455
global warnings
433456

434457
message = f'{flavor}/{distribution}: {message}'
435-
click.secho(f'Warning: {message}', fg='red')
458+
click.secho(f'Warning: {message}', fg='yellow')
436459

437460
warnings.append(message)
438461
if __name__ == "__main__":

0 commit comments

Comments
 (0)