Skip to content

Instantly share code, notes, and snippets.

@anil3a
Last active October 17, 2019 00:55
Show Gist options
  • Save anil3a/ede8cd383e70e6764813c00ed1809557 to your computer and use it in GitHub Desktop.
Save anil3a/ede8cd383e70e6764813c00ed1809557 to your computer and use it in GitHub Desktop.
import os.path
from os import path
# This script is used for my personal project in codeigniter to check if file exist and line of text exist.
# It is running from applications folder in the project
def verify_if_file_content_text(filepath,content_to_find):
content_exist = False
if( path.exists( filepath ) and os.path.isfile( filepath ) ):
with open(filepath) as fp:
line = fp.readline()
cnt = 1
while line:
if( content_to_find in line.strip() ):
content_exist = True
line = fp.readline()
cnt += 1
if content_exist:
print( "\nYour content \""+ content_to_find +"\" exists in "+ filepath )
else:
print( "\nCannot find your content \""+ content_to_find +"\" in "+ filepath )
else:
print( "\nFile doesn't exists: "+ filepath )
# check if library name is included in the helper
verify_if_file_content_text( 'helpers/my_functions_helper.php', "google" )
# check if controller file exist and if library name is included in that file.
verify_if_file_content_text( '/controllers/module/Google.php', "google" )
import os.path
from os import path
# Used specifically for my codeigniter project
# Add content to helper and
# Create file if not exist and add content if not exist
def write_module_name_to_helpers_and_controllers_if_not_found(content_to_find,helper_content, controller_file, controller_content):
helperfile = '../helpers/my_functions_helper.php'
controllerPath = '../controllers/module/'
content_exist = False
# Check helper file if the content exists and if not, insert the helper content
if( path.exists( helperfile ) and os.path.isfile( helperfile ) ):
with open(helperfile, 'r') as fp:
line = fp.readline()
while line:
if( content_to_find in line.strip() ):
content_exist = True
line = fp.readline()
if not content_exist:
with open( helperfile, 'a') as wrh:
wrh.write( "\n"+ helper_content )
print( "\nHelper content updated with new content appended "+ helper_content )
else:
print( "\nHelper content exists in your "+ helperfile +" with content "+ content_to_find )
else:
print( "\nFile doesn't exists: "+ helperfile )
# Check if Controller file exists and if not create one with content
if( not path.exists( controllerPath + controller_file ) ):
with open( controllerPath + controller_file, 'w') as wrc:
wrc.write( controller_content )
print( "\nController file created successfully: "+ controllerPath + controller_file )
else:
print( "\nController file exists: "+ controllerPath + controller_file )
write_module_name_to_helpers_and_controllers_if_not_found(
"google",
"require_once(APPLICATION_PATH . '/modules/google/google.php' );",
"Google.php",
"<?php require_once(APPLICATION_PATH . '/modules/google/controllers/Google.php' );"
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment