-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
192 additions
and
48 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
""" | ||
Usage: FixPadding [-h | --help] (INPUT ...) | ||
Options: | ||
-h --help Show this | ||
INPUT FILES to edit or DIRECTORIES to search recursively for files to edit | ||
""" | ||
import os | ||
import itertools | ||
from docopt import docopt | ||
|
||
def files_by_dir(dirs): | ||
for dir in dirs: | ||
for _dir,subdir,files in os.walk(dir): | ||
for file in files: | ||
curr_file_path = os.path.join(_dir,file) | ||
yield os.path.abspath(curr_file_path) | ||
|
||
|
||
def files_from_input(files): | ||
for item in files: | ||
if os.path.isfile(item): | ||
yield os.path.abspath(item) | ||
|
||
|
||
def combine_iterators(*args): | ||
yield from itertools.chain(args) | ||
|
||
|
||
def _replace_in_file(file_to_edit,replace_this,with_this): | ||
with open(file_to_edit,'r') as curr_file: | ||
contents = curr_file.read() | ||
contents = contents.replace(replace_this,with_this) | ||
|
||
with open(file_to_edit,'w+') as curr_file: | ||
curr_file.write(contents) | ||
|
||
def _get_file_hash(file): | ||
with open(file,'r') as curr_file: | ||
return hash(curr_file.read()) | ||
|
||
def replace_in_files(file_to_edit,replace_this,with_this): | ||
before_hash = _get_file_hash(file_to_edit) | ||
_replace_in_file(file_to_edit,replace_this,with_this) | ||
after_hash = _get_file_hash(file_to_edit) | ||
if before_hash == after_hash: | ||
return False | ||
else: | ||
return True | ||
|
||
|
||
if __name__ == '__main__': | ||
args = docopt(__doc__) | ||
user_input = args['INPUT'] | ||
|
||
dirs = files_by_dir(user_input) | ||
files_from_cli = files_from_input(user_input) | ||
|
||
|
||
files = itertools.chain(dirs,files_from_cli) | ||
replication_files = (file for file in files if os.path.splitext(file)[1] == '.sch') | ||
|
||
num_files_modified = 0 | ||
for file in replication_files: | ||
if (replace_in_files(file,"SET ANSI PADDING OFF","SET ANSI PADDING ON")): | ||
num_files_modified +=1 | ||
|
||
print(str(num_files_modified) + " file(s) fixed.") | ||
|
||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,63 +1,72 @@ | ||
"""Usage: FixAnsiPadding [-h | --help] [INPUT ...] | ||
""" | ||
Usage: FixPadding [-h | --help] (INPUT ...) | ||
-h --help show this | ||
INPUT FILES to edit or DIRECTORIES to search recursively for files to edit | ||
Options: | ||
-h --help Show this | ||
INPUT FILES to edit or DIRECTORIES to search recursively for files to edit | ||
""" | ||
import os | ||
from docopt import docopt | ||
import itertools | ||
from docopt import docopt | ||
|
||
def replace_in_file(input_file,replace_text,with_this_text): #having fun with argument names | ||
with open(input_file,"r") as curr_file: | ||
contents = curr_file.read().replace(replace_text,with_this_text) | ||
|
||
with open(input_file,"w") as curr_file: | ||
curr_file.write(contents) | ||
|
||
""" | ||
more or less equiv to: | ||
$configFiles = get-childitem <UNC PATH> *.txt -rec | ||
foreach ($file in $configFiles) | ||
{ | ||
(get-content $file.PSPath) | | ||
foreach-object { $_ -replace "SET ANSI PADDING OFF", "SET ANSI PADDING ON" } | | ||
set-content $file.PSPath | ||
} | ||
""" | ||
if __name__ == '__main__': | ||
args = docopt(__doc__) | ||
user_input = args['INPUT'] | ||
extension = '.sch' | ||
|
||
files_to_edit = [] | ||
#for each element in user_input check for if its a path or a full file | ||
for elem in user_input: | ||
if os.path.isdir(elem): | ||
for dir,subdir,files in os.walk(os.path.abspath(elem)): | ||
for file in files: | ||
rel_path = os.path.join(dir,file) | ||
files_to_edit.append(os.path.abspath(rel_path)) | ||
|
||
elif os.path.isfile(elem): | ||
if os.path.splitext(elem)[1] == extension: | ||
files_to_edit.append(os.path.abspath(elem)) | ||
|
||
#actually edit the files | ||
for file in files_to_edit: | ||
replace_in_file(file,"SET ANSI PADDING OFF","SET ANSI PADDING ON") | ||
|
||
print(str(len(files_to_edit)) + ' file(s) updated.') | ||
def files_by_dir(dirs): | ||
for dir in dirs: | ||
for _dir,subdir,files in os.walk(dir): | ||
for file in files: | ||
curr_file_path = os.path.join(_dir,file) | ||
yield os.path.abspath(curr_file_path) | ||
|
||
|
||
|
||
def files_from_input(files): | ||
for item in files: | ||
if os.path.isfile(item): | ||
yield os.path.abspath(item) | ||
|
||
|
||
def combine_iterators(*args): | ||
yield from itertools.chain(args) | ||
|
||
|
||
def _replace_in_file(file_to_edit,replace_this,with_this): | ||
with open(file_to_edit,'r') as curr_file: | ||
contents = curr_file.read() | ||
contents = contents.replace(replace_this,with_this) | ||
|
||
with open(file_to_edit,'w+') as curr_file: | ||
curr_file.write(contents) | ||
|
||
def _get_file_hash(file): | ||
with open(file,'r') as curr_file: | ||
return hash(curr_file.read()) | ||
|
||
|
||
|
||
def replace_in_files(file_to_edit,replace_this,with_this): | ||
before_hash = _get_file_hash(file_to_edit) | ||
_replace_in_file(file_to_edit,replace_this,with_this) | ||
after_hash = _get_file_hash(file_to_edit) | ||
if before_hash == after_hash: | ||
return False | ||
else: | ||
return True | ||
|
||
|
||
if __name__ == '__main__': | ||
args = docopt(__doc__) | ||
user_input = args['INPUT'] | ||
|
||
dirs = files_by_dir(user_input) | ||
files_from_cli = files_from_input(user_input) | ||
|
||
|
||
files = itertools.chain(dirs,files_from_cli) | ||
replication_files = (file for file in files if os.path.splitext(file)[1] == '.sch') | ||
|
||
num_files_modified = 0 | ||
for file in replication_files: | ||
if (replace_in_files(file,"SET ANSI PADDING OFF","SET ANSI PADDING ON")): | ||
num_files_modified += 1 | ||
|
||
print(str(num_files_modified) + " file(s) fixed.") | ||
|
||
|
||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
"""Usage: FixAnsiPadding [-h | --help] [INPUT ...] | ||
-h --help show this | ||
INPUT FILES to edit or DIRECTORIES to search recursively for files to edit | ||
""" | ||
import os | ||
from docopt import docopt | ||
|
||
def replace_in_file(input_file,replace_text,with_this_text): #having fun with argument names | ||
with open(input_file,"r") as curr_file: | ||
contents = curr_file.read().replace(replace_text,with_this_text) | ||
|
||
with open(input_file,"w") as curr_file: | ||
curr_file.write(contents) | ||
|
||
""" | ||
more or less equiv to: | ||
$configFiles = get-childitem <UNC PATH> *.txt -rec | ||
foreach ($file in $configFiles) | ||
{ | ||
(get-content $file.PSPath) | | ||
foreach-object { $_ -replace "SET ANSI PADDING OFF", "SET ANSI PADDING ON" } | | ||
set-content $file.PSPath | ||
} | ||
""" | ||
if __name__ == '__main__': | ||
args = docopt(__doc__) | ||
user_input = args['INPUT'] | ||
extension = '.sch' | ||
|
||
files_to_edit = [] | ||
#for each element in user_input check for if its a path or a full file | ||
for elem in user_input: | ||
if os.path.isdir(elem): | ||
for dir,subdir,files in os.walk(os.path.abspath(elem)): | ||
for file in files: | ||
rel_path = os.path.join(dir,file) | ||
files_to_edit.append(os.path.abspath(rel_path)) | ||
|
||
elif os.path.isfile(elem): | ||
if os.path.splitext(elem)[1] == extension: | ||
files_to_edit.append(os.path.abspath(elem)) | ||
|
||
#actually edit the files | ||
for file in files_to_edit: | ||
replace_in_file(file,"SET ANSI PADDING OFF","SET ANSI PADDING ON") | ||
|
||
print(str(len(files_to_edit)) + ' file(s) updated.') | ||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|