mirror of https://github.com/ansible/ansible.git
You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
83 lines
1.7 KiB
Python
83 lines
1.7 KiB
Python
7 years ago
|
#!/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
|
||
5 years ago
|
from __future__ import (absolute_import, division, print_function)
|
||
|
__metaclass__ = type
|
||
7 years ago
|
|
||
|
import os
|
||
|
import struct
|
||
5 years ago
|
import sys
|
||
7 years ago
|
|
||
7 years ago
|
from ansible.module_utils.basic import to_bytes
|
||
|
|
||
7 years ago
|
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 = [
|
||
|
'.',
|
||
|
' ',
|
||
|
]
|
||
|
|
||
|
|
||
6 years ago
|
def check_path(path, is_dir=False):
|
||
|
type_name = 'directory' if is_dir else 'file'
|
||
5 years ago
|
file_name = os.path.basename(path.rstrip(os.path.sep))
|
||
5 years ago
|
name = os.path.splitext(file_name)[0]
|
||
7 years ago
|
|
||
|
if name.upper() in ILLEGAL_NAMES:
|
||
7 years ago
|
print("%s: illegal %s name %s" % (path, type_name, name.upper()))
|
||
7 years ago
|
|
||
|
if file_name[-1] in ILLEGAL_END_CHARS:
|
||
7 years ago
|
print("%s: illegal %s name end-char '%s'" % (path, type_name, file_name[-1]))
|
||
7 years ago
|
|
||
7 years ago
|
bfile = to_bytes(file_name, encoding='utf-8')
|
||
7 years ago
|
for char in ILLEGAL_CHARS:
|
||
|
if char in bfile:
|
||
7 years ago
|
bpath = to_bytes(path, encoding='utf-8')
|
||
7 years ago
|
print("%s: illegal char '%s' in %s name" % (bpath, char, type_name))
|
||
7 years ago
|
|
||
|
|
||
|
def main():
|
||
5 years ago
|
for path in sys.argv[1:] or sys.stdin.read().splitlines():
|
||
|
check_path(path, is_dir=path.endswith(os.path.sep))
|
||
7 years ago
|
|
||
|
|
||
|
if __name__ == '__main__':
|
||
|
main()
|