Skip to content

Instantly share code, notes, and snippets.

@tajulasri
Created July 17, 2017 16:24
Show Gist options
  • Save tajulasri/f731494a8d716e3b58e23fe5558146db to your computer and use it in GitHub Desktop.
Save tajulasri/f731494a8d716e3b58e23fe5558146db to your computer and use it in GitHub Desktop.
replace content to all files by supply from input source text
#!/usr/local/bin/python
import os
import sys
import errno
from datetime import datetime
from time import time
rootdir = os.getcwd()
commands = (
'--setup',
'--scan-dir',
'--text-source',
'--log'
)
def parse_option(str):
if '=' in str:
return str.split('=')[1]
def run_setup():
folders = ('runtime','sample','testing')
for i in folders:
try:
os.mkdir(rootdir+'/'+i)
print 'Create directory {dir}'.format(dir=rootdir+'/'+i)
except OSError:
print 'Directory already exists.'
pass
def create_file():
for d in os.listdir(rootdir):
if os.path.isdir(d):
f = open(os.path.join(os.getcwd(),d,'sample.cpp'),'w+')
f.write('<replace_here>')
f.close()
def usage():
print 'Usage: python main.py'
print 'example usage: python main.py --scan-dir=testing/dir --text-source=source-file.txt'
print '--setup \t\t Run initial setup for simulation.\n'
print '--scan-dir \t\t scan directory on current root. Set option --scan-dir=directory (unix style).\n'
print '--text-source \t\t Replace all files with text inside source.\n'
print '--log\t\t enable logging std output.'
if(len(sys.argv) <= 1 ):
usage()
else:
options = sys.argv[1::]
log = open(os.path.join(rootdir,'output.log'),'w+')
source = parse_option(options[2])
directory = parse_option(options[1])
if os.path.isdir(source) == False or directory is None:
real_dir = os.path.realpath(directory)
if os.path.exists(source) == False:
print 'file {file} source not found'.format(file=source)
else:
files = [x for x in os.listdir(real_dir) if os.path.isdir(x) == False]
f = open(source,'r+')
for file in files:
content = ''.join([x for x in open(os.path.join(real_dir,file),'r+')])
if '<replace_here>' in content:
bak = open(os.path.join(real_dir,file+'.bak'),'w+')
content = content.replace('<replace_here>',f.read())
bak.write(content)
print 'Replacing content {file}'.format(file=os.path.join(real_dir,file))
log.write('Replacing content {file} \n'.format(file=os.path.join(real_dir,file)))
original = file #dump to memory original name
print 'swap {backup} to original file {original_file}.'.format(backup=os.path.join(real_dir,original+'.bak'),original_file=os.path.join(real_dir,file))
log.write('swap {backup} to original file {original_file}. \n'.format(backup=os.path.join(real_dir,original+'.bak'),original_file=os.path.join(real_dir,file)))
os.remove(os.path.join(real_dir,original))
os.rename(os.path.join(real_dir,original+'.bak'),os.path.join(real_dir,original))
bak.close()
else:
print 'marker are not found inside this {file}'.format(file=os.path.join(real_dir,file))
log.write('marker are not found inside this {file} \n'.format(file=os.path.join(real_dir,file)))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment