Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Tajul Asri
#!/usr/bin/python
# -*- coding: utf-8 -*-
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.'
def read_content(text, files, path):
for fp in files:
content = ''.join([x for x in open(os.path.join(path, fp),'r+')])
if '<replace_here>' in content:
bak = open(os.path.join(path, fp + '.bak'),'w+')
content = content.replace('<replace_here>',text.read())
bak.write(content)
print 'Replacing content {file}'.format(file=os.path.join(path,fp))
log.write('Replacing content {file} \n'.format(file=os.path.join(path,fp)))
original = fp # dump to memory original name
print 'swap {backup} to original file {original_file}.'.format(backup=os.path.join(path,original + '.bak'),original_file=os.path.join(path, fp))
log.write('swap {backup} to original file {original_file}. \n'.format(backup=os.path.join(path,original + '.bak'),original_file=os.path.join(path,fp)))
os.remove(os.path.join(path, original))
os.rename(os.path.join(path, original + '.bak'), os.path.join(path, original))
bak.close()
else:
print 'marker are not found inside this {file}'.format(file=os.path.join(path,fp))
log.write('marker are not found inside this {file} \n'.format(file=os.path.join(path,fp)))
if len(sys.argv) <= 1:
usage()
else:
options = sys.argv[1::]
log = open(os.path.join(rootdir, 'output.log'), 'w+')
source = parse_option(options[1])
directory = parse_option(options[0])
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:
path = []
for r, d, f in os.walk(real_dir):
path.append(r)
if path:
for p in path:
f = open(source, 'r+')
files = []
for x in os.listdir(p):
if not os.path.isdir(os.path.join(p, x)):
files.append(x)
read_content(f, files, p)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.