Skip to content

Instantly share code, notes, and snippets.

@tianweidut
Created August 17, 2014 14:38
Show Gist options
  • Save tianweidut/365aeb31b878669dcc56 to your computer and use it in GitHub Desktop.
Save tianweidut/365aeb31b878669dcc56 to your computer and use it in GitHub Desktop.
#coding: utf-8
import os
from os.path import join, exists, splitext
import datetime
import uuid
import string
import random
import shutil
TEST_DIR = '/tmp/xinxin/'
FNAME_NOT_FOUND = 'filenotfound.txt'
TARGET_STR = 'abc'
REPLACE_STR = 'def'
def random_text(length=30):
alphabeta = list(string.ascii_letters)
alphabeta_len = len(alphabeta)
random.shuffle(alphabeta)
length = length if length <= alphabeta_len else alphabeta_len
content = ''.join(alphabeta[:length])
if random.random() > 0.5:
content = '-'.join([content, TARGET_STR, TARGET_STR])
return content
def build_test():
fpath_exists = []
fpath_not_exists = []
shutil.rmtree(TEST_DIR)
os.makedirs(TEST_DIR)
for i in range(10):
fname = join(TEST_DIR, str(uuid.uuid4()) + ".txt")
fname_null = join(TEST_DIR, str(uuid.uuid4()) + "_not_exists.txt")
fpath_exists.append(fname)
if exists(fname_null):
os.remove(fname_null)
fpath_not_exists.append(fname_null)
with open(fname, 'a') as f:
f.writelines(random_text())
fname_input = os.path.join('.', 'allfiles.txt')
with open(fname_input, 'w') as f:
f.write(' '.join(fpath_exists + fpath_not_exists))
return fname_input, fpath_exists, fpath_not_exists
def _replace_text(fname_src, fname_dest, target_str, replace_str):
with open(fname_src, 'r') as f:
content = f.read()
content = content.replace(target_str, replace_str)
with open(fname_dest, 'w') as ft:
ft.write(content)
def get_fix_format_fname(fname):
dpath, name = os.path.split(fname)
name_without_revision, _ = splitext(name)
timestamp = datetime.datetime.today().strftime('%Y%M%d')
fname_fix = join(dpath, "%s.%s.fix" % (name_without_revision, timestamp))
return fname_fix
def replace(fname_in):
files_not_found = set()
with open(fname_in) as f:
fnames = f.read().split()
for fname in fnames:
if not exists(fname):
files_not_found.add(fname)
continue
fname_fix = get_fix_format_fname(fname)
_replace_text(fname, fname_fix, TARGET_STR, REPLACE_STR)
with open(FNAME_NOT_FOUND, 'w') as f:
f.writelines('\n'.join(files_not_found))
return FNAME_NOT_FOUND
def validate_test(fname_out, fpath_exists, fpath_not_exists):
# validate not found files
with open(fname_out, 'r') as f:
fnames_not_exist = f.readlines()
fnames_not_exist = [n.strip('\n') for n in fnames_not_exist]
assert not set(fnames_not_exist) ^ set(fpath_not_exists), 'error: files not exists '
print '[ok]validate not found files'
# validate content replace
for fname in fpath_exists:
with open(fname, 'r') as f:
content = f.read()
if TARGET_STR in content:
fname_fix = get_fix_format_fname(fname)
assert exists(fname_fix), 'error: exists fixed files'
with open(fname_fix, 'r') as ft:
target_content = ft.read()
assert TARGET_STR not in target_content, "error: replace target str"
assert REPLACE_STR in target_content, "error: replace target str"
print '[ok]validate replace and fix format file name'
if __name__ == "__main__":
fname_in, fpath_exists, fpath_not_exists = build_test()
fname_out = replace(fname_in)
validate_test(fname_out, fpath_exists, fpath_not_exists)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment