2018-01-29 22:46:02 +01:00
|
|
|
#!/usr/bin/env python
|
|
|
|
|
|
|
|
# a script to check for illegal filenames on various Operating Systems. The
|
|
|
|
# main rules are derived from restrictions on Windows
|
|
|
|
# https://msdn.microsoft.com/en-us/library/aa365247#naming_conventions
|
2019-07-12 08:46:20 +02:00
|
|
|
from __future__ import (absolute_import, division, print_function)
|
|
|
|
__metaclass__ = type
|
2018-01-29 22:46:02 +01:00
|
|
|
|
|
|
|
import os
|
2018-01-30 23:58:03 +01:00
|
|
|
import re
|
2018-01-29 22:46:02 +01:00
|
|
|
import struct
|
|
|
|
|
2018-01-30 23:58:03 +01:00
|
|
|
from ansible.module_utils.basic import to_bytes
|
|
|
|
|
2018-01-29 22:46:02 +01:00
|
|
|
ILLEGAL_CHARS = [
|
|
|
|
b'<',
|
|
|
|
b'>',
|
|
|
|
b':',
|
|
|
|
b'"',
|
|
|
|
b'/',
|
|
|
|
b'\\',
|
|
|
|
b'|',
|
|
|
|
b'?',
|
|
|
|
b'*'
|
|
|
|
] + [struct.pack("b", i) for i in range(32)]
|
|
|
|
|
|
|
|
ILLEGAL_NAMES = [
|
|
|
|
"CON",
|
|
|
|
"PRN",
|
|
|
|
"AUX",
|
|
|
|
"NUL",
|
|
|
|
"COM1",
|
|
|
|
"COM2",
|
|
|
|
"COM3",
|
|
|
|
"COM4",
|
|
|
|
"COM5",
|
|
|
|
"COM6",
|
|
|
|
"COM7",
|
|
|
|
"COM8",
|
|
|
|
"COM9",
|
|
|
|
"LPT1",
|
|
|
|
"LPT2",
|
|
|
|
"LPT3",
|
|
|
|
"LPT4",
|
|
|
|
"LPT5",
|
|
|
|
"LPT6",
|
|
|
|
"LPT7",
|
|
|
|
"LPT8",
|
|
|
|
"LPT9",
|
|
|
|
]
|
|
|
|
|
|
|
|
ILLEGAL_END_CHARS = [
|
|
|
|
'.',
|
|
|
|
' ',
|
|
|
|
]
|
|
|
|
|
|
|
|
|
2018-09-14 20:12:06 +02:00
|
|
|
def check_path(path, is_dir=False):
|
|
|
|
type_name = 'directory' if is_dir else 'file'
|
2019-07-12 22:17:20 +02:00
|
|
|
file_name = os.path.basename(path)
|
|
|
|
name = os.path.splitext(file_name)[0]
|
2018-01-29 22:46:02 +01:00
|
|
|
|
|
|
|
if name.upper() in ILLEGAL_NAMES:
|
2018-02-28 00:05:39 +01:00
|
|
|
print("%s: illegal %s name %s" % (path, type_name, name.upper()))
|
2018-01-29 22:46:02 +01:00
|
|
|
|
|
|
|
if file_name[-1] in ILLEGAL_END_CHARS:
|
2018-02-28 00:05:39 +01:00
|
|
|
print("%s: illegal %s name end-char '%s'" % (path, type_name, file_name[-1]))
|
2018-01-29 22:46:02 +01:00
|
|
|
|
2018-01-30 23:58:03 +01:00
|
|
|
bfile = to_bytes(file_name, encoding='utf-8')
|
2018-01-29 22:46:02 +01:00
|
|
|
for char in ILLEGAL_CHARS:
|
|
|
|
if char in bfile:
|
2018-01-30 23:58:03 +01:00
|
|
|
bpath = to_bytes(path, encoding='utf-8')
|
2018-02-28 00:05:39 +01:00
|
|
|
print("%s: illegal char '%s' in %s name" % (bpath, char, type_name))
|
2018-01-29 22:46:02 +01:00
|
|
|
|
|
|
|
|
|
|
|
def main():
|
2018-02-28 00:05:39 +01:00
|
|
|
pattern = re.compile("^test/integration/targets/.*/backup")
|
2018-01-30 23:58:03 +01:00
|
|
|
|
2018-01-29 22:46:02 +01:00
|
|
|
for root, dirs, files in os.walk('.'):
|
2018-02-28 00:05:39 +01:00
|
|
|
if root == '.':
|
|
|
|
root = ''
|
|
|
|
elif root.startswith('./'):
|
|
|
|
root = root[2:]
|
|
|
|
|
2018-01-30 23:58:03 +01:00
|
|
|
# ignore test/integration/targets/*/backup
|
|
|
|
if pattern.match(root):
|
|
|
|
continue
|
|
|
|
|
2018-01-29 22:46:02 +01:00
|
|
|
for dir_name in dirs:
|
2018-09-14 20:12:06 +02:00
|
|
|
check_path(os.path.join(root, dir_name), is_dir=True)
|
2018-01-29 22:46:02 +01:00
|
|
|
|
|
|
|
for file_name in files:
|
2018-09-14 20:12:06 +02:00
|
|
|
check_path(os.path.join(root, file_name), is_dir=False)
|
2018-01-29 22:46:02 +01:00
|
|
|
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
main()
|