-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcheck_newline.py
96 lines (66 loc) · 2.8 KB
/
check_newline.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
#!/usr/bin/env python
# The MIT License (MIT)
#
# Copyright (c) 2013, 2014 Bartosz Zaczynski
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.
"""
Check if Python source files use Unix-style newline character.
Otherwise running the scripts on Linux will end up with this error:
: No such file or directory
"""
import os
from os.path import abspath, join
def get_python_source_files():
self = __file__.replace('\\', '/')
for root, folders, files in os.walk(r'.'):
if '.git' in root or 'build' in root:
continue
for file_name in files:
file_name = abspath(join(root, file_name)).replace('\\', '/')
if file_name == self:
continue
if file_name.endswith(('.py', '.pyw')):
yield file_name
def get_broken_files():
broken_files = {}
for file_name in get_python_source_files():
with open(file_name, 'U') as file_handle:
file_handle.read()
if file_handle.newlines is not None:
def make_list(obj):
if isinstance(obj, str):
return [obj]
else:
return list(obj)
newlines = make_list(file_handle.newlines)
if '\r' in newlines or '\r\n' in newlines:
broken_files[file_name] = newlines
return broken_files
def main():
broken_files = get_broken_files()
if len(broken_files) > 0:
print r'The following files contain invalid newline characters:'
for i, file_name in enumerate(sorted(broken_files)):
newlines = ', '.join([repr(x) for x in broken_files[file_name]])
print '%2d. %s (%s)' % (i + 1, file_name, newlines)
else:
print 'Ready to commit!'
if __name__ == '__main__':
main()