Skip to content

Instantly share code, notes, and snippets.

@boseji
Created February 24, 2014 05:17
Show Gist options
  • Save boseji/9182333 to your computer and use it in GitHub Desktop.
Save boseji/9182333 to your computer and use it in GitHub Desktop.
Execution Program for the Runner Scripts to execute python scripts by parsing directories through a single python file.
#!/usr/bin/python
###########################################################################
### @file main.py
### Execution Engine for the Site Generator
### @Author Abhijit Bose <info@adharlabs.in>
### @License
### Copyright 2014 Abhijit Bose <info@adharlabs.in>
### Licensed under the Apache License, Version 2.0 (the "License");
### you may not use this file except in compliance with the License.
### You may obtain a copy of the License at
###
### http://www.apache.org/licenses/LICENSE-2.0
###
### Unless required by applicable law or agreed to in writing, software
### distributed under the License is distributed on an "AS IS" BASIS,
### WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
### See the License for the specific language governing permissions and
### limitations under the License.
###########################################################################
### History
### @version v0.1 First Implementation of the Runner for scripts
###########################################################################
### Imports
###########################################################################
import os,sys,importlib
from os import path
###########################################################################
### Globals
###########################################################################
###########################################################################
### Functions
###########################################################################
#####
def RunScript(sPath,sFile,bDirect=2):
''' Run a Python Script via differenct Methods
@param sPath Sting containing Only the
Absolute Path of the File to Execute
@param sFile String containing Only the File Name to Execute
@param bDirect Flag indicates how to run the Script
0 = Run using Exec Technique
1 = Run directly in Shell
2 = Import Module and call run() function
@exception In case of any error both in the script or the execution, it
shall be reported
@return None
'''
try:
if bDirect == 0:
sRunPath = sPath
with open(path.join(sPath,sFile)) as f:
exec(compile(f.read(), "temp.p", 'exec'), globals())
elif bDirect == 1:
tmp = os.getcwd()
os.chdir(sPath)
os.system("python " + sFile)
os.chdir(tmp)
elif bDirect == 2:
sys.path.insert(0,sPath)
tmp = importlib.import_module(sFile.split(".py")[0])
tmp.run()
sys.path.remove(sPath)
else:
pass
except BaseException as p:
print("\nProblem in Running",sFile,"at",sPath,"Script :",p)
#####
def Runner(sRootPath):
''' Runs all the scripts in lower directories
@param sRootPath Specifies the Path from which Script Running should start
@return None
'''
try:
for root, dirs, files in os.walk(sRootPath):
#bypass dir with _ and __ , Process only if some files
if (root.endswith('_') == False) and (len(files) > 0 ) and \
(root != sRootPath) and (root.find("__")==-1):
print("-" * 20)
print(" Root: ",root)
print("-" * 20)
for name in files:
#check for scripts but avoid packages
if name.endswith(".py") and (root.find("__")==-1):
print(" Running Script : ",name)
RunScript(root,name)
print()
except Exception as p:
print("\nProblem in Runner :",p)
#####
###########################################################################
### Main Program
###########################################################################
#####
## Run through the Path
if __name__ == "__main__":
print(slIntro)
Runner(path.dirname(__file__))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment