Skip to content

Instantly share code, notes, and snippets.

@bentan2013
Created January 8, 2018 02:36
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bentan2013/1b43fb38da0ffc3bb4794a5db489d55e to your computer and use it in GitHub Desktop.
Save bentan2013/1b43fb38da0ffc3bb4794a5db489d55e to your computer and use it in GitHub Desktop.
# for python 3.5
import os, sys
import os.path
import shutil
import argparse
import argparse
import os
import sys
import glob
import tempfile
from stat import ST_ATIME, ST_MTIME
# Readme
# Source codes from here:
# https://jonlabelle.com/snippets/view/python/replace-lf-with-crlf-in-python
# #提交时转换为LF,检出时转换为CRLF
# git config --global core.autocrlf true
#
# #提交时转换为LF,检出时不转换
# git config --global core.autocrlf input
#
# #提交检出均不转换
# git config --global core.autocrlf false
#
#
# #拒绝提交包含混合换行符的文件
# git config --global core.safecrlf true
#
# # Git will always convert line endings to CRLF on checkout.
# # You should use this for files that must keep CRLF endings.
# text eol=crlf
EOL_UNIX = b'\n'
EOL_WINDOWS = b'\r\n'
EOL_MAC = b'\r'
def _read_file_data(filepath):
"""Read file data.
:param filepath: The file path.
:return: The file contents.
"""
data = open(filepath, 'rb').read()
return data
def _write_file_data(filepath, data):
"""Write file data.
:param filepath: The file path.
:param data: The data to write.
"""
f = open(filepath, 'wb')
f.write(data)
f.close()
def _normalize_line_endings(lines, line_ending='u'):
r"""Normalize line endings to unix (\n), windows (\r\n) or mac (\r).
:param lines: The lines to normalize.
:param line_ending: The line ending format.
Acceptable values are 'unix' (default), 'windows' and 'mac'.
:return: Line endings normalized.
"""
lines = lines.replace(EOL_WINDOWS, EOL_UNIX).replace(EOL_MAC, EOL_UNIX)
if line_ending == 'w':
lines = lines.replace(EOL_UNIX, EOL_WINDOWS)
elif line_ending == 'm':
lines = lines.replace(EOL_UNIX, EOL_MAC)
else:
lines = lines.replace(EOL_UNIX, EOL_UNIX)
return lines
def process_files(srcDir, extensions, line_ending):
extsCount = len(extensions)
cpyAll = False
if extsCount == 1 and extensions[0] == '*':
cpyAll = True
for file in os.listdir(srcDir):
srcFile = os.path.join(srcDir, file)
if os.path.isfile(srcFile):
sufix = os.path.splitext(srcFile)[1][1:]
copyfile = False
for index in range(extsCount):
if sufix == extensions[index]:
copyfile = True
break
if copyfile == True or(cpyAll == True):
data = _read_file_data(srcFile)
newdata = _normalize_line_endings(
#data.decode('utf-8')*/,
data,
line_ending=line_ending)
if data != newdata and len(newdata) != 0:
print(srcFile)
_write_file_data(
srcFile,
#newdata.encode('utf-8'))
newdata)
if os.path.isdir(srcFile):
process_files(srcFile, exts, line_ending)
if __name__ == "__main__":
# C:\Anaconda3\envs\py35\python.exe endings.py -i C:\Projects\arcgis-earth -ext cs,xaml - os u
parser = argparse.ArgumentParser()
parser.add_argument('-i', '--source_directory', type=str, help='the source directory')
parser.add_argument('-ext', '--extensions', type=str, help='the extension you want to copy, use , to split it. If you want to keep all files, please use *')
parser.add_argument('-os', '--operation_system', type=str, help='operation system. w: windows u: Unix m: Mac')
args = parser.parse_args()
params = vars(args)
sourceDir = params['source_directory']
exts_string= params['extensions']
exts = exts_string.split(',')
line_ending = params['operation_system']
process_files(sourceDir, exts, line_ending)
print ("ok!")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment