Browse Source

added docstrings

bugfixes
harshadyeola 10 years ago
parent
commit
01296edc9f
  1. 21
      ee/core/apt_repo.py
  2. 24
      ee/core/aptget.py
  3. 3
      ee/core/database.py
  4. 4
      ee/core/domainvalidate.py
  5. 58
      ee/core/fileutils.py
  6. 7
      ee/core/git.py
  7. 16
      ee/core/logging.py
  8. 3
      ee/core/models.py
  9. 161
      ee/core/services.py
  10. 3
      ee/core/shellexec.py

21
ee/core/apt_repo.py

@ -12,6 +12,14 @@ class EERepo():
pass
def add(self, repo_url=None, ppa=None):
"""
This function used to add apt repositories and or ppa's
If repo_url is provided adds repo file to
/etc/apt/sources.list.d/
If ppa is provided add apt-repository using
add-apt-repository
command.
"""
if repo_url is not None:
repo_file_path = ("/etc/apt/sources.list.d/"
@ -43,12 +51,23 @@ class EERepo():
"'{ppa_name}'"
.format(ppa_name=ppa))
def remove(self, repo_url=None):
def remove(self, ppa=None):
"""
This function used to remove ppa's
If ppa is provided adds repo file to
/etc/apt/sources.list.d/
command.
"""
EEShellExec.cmd_exec(self, "add-apt-repository -y "
"--remove '{ppa_name}'"
.format(ppa_name=repo_url))
def add_key(self, keyids, keyserver=None):
"""
This function adds imports repository keys from keyserver.
default keyserver is hkp://keys.gnupg.net
user can provide other keyserver with keyserver="hkp://xyz"
"""
if keyserver is None:
EEShellExec.cmd_exec(self, "gpg --keyserver {serv}"
.format(serv=(keyserver or

24
ee/core/aptget.py

@ -10,6 +10,9 @@ class EEAptGet():
"""Generic apt-get intialisation"""
def update(self):
"""
Similar to `apt-get upgrade`
"""
try:
apt_cache = apt.cache.Cache()
apt_cache.update()
@ -22,6 +25,9 @@ class EEAptGet():
Log.error(self, 'AttributeError: ' + str(e))
def dist_upgrade():
"""
Similar to `apt-get upgrade`
"""
apt_cache = apt.cache.Cache()
apt_cache.update()
apt_cache.open(None)
@ -33,7 +39,9 @@ class EEAptGet():
return success
def install(self, packages):
"""Installation of packages"""
"""
Similar to `apt-get install`
"""
apt_pkg.init()
# #apt_pkg.PkgSystemLock()
global apt_cache
@ -79,6 +87,10 @@ class EEAptGet():
continue
def remove(self, packages, auto=False, purge=False):
"""
Similar to `apt-get remove/purge`
purge packages if purge=True
"""
apt_pkg.init()
# apt_pkg.PkgSystemLock()
global apt_cache
@ -122,6 +134,9 @@ class EEAptGet():
continue
def auto_clean(self):
"""
Similar to `apt-get autoclean`
"""
try:
apt_get.autoclean("-y")
except ErrorReturnCode as e:
@ -129,6 +144,9 @@ class EEAptGet():
Log.error(self, "Unable to apt-get autoclean")
def auto_remove(self):
"""
Similar to `apt-get autoremove`
"""
try:
Log.debug(self, "Running apt-get autoremove")
apt_get.autoremove("-y")
@ -137,6 +155,10 @@ class EEAptGet():
Log.error(self, "Unable to apt-get autoremove")
def is_installed(self, package_name):
"""
Checks if package is available in cache and is installed or not
returns True if installed otherwise returns False
"""
apt_cache = apt.cache.Cache()
apt_cache.open()
if (package_name.strip() in apt_cache and

3
ee/core/database.py

@ -13,6 +13,9 @@ Base.query = db_session.query_property()
def init_db():
"""
Initializes and creates all tables from models into the database
"""
# import all modules here that might define models so that
# they will be registered properly on the metadata. Otherwise
# you will have to import them first before calling init_db()

4
ee/core/domainvalidate.py

@ -3,6 +3,10 @@ from urllib.parse import urlparse
def ValidateDomain(url):
"""
This function returns domain name removing http:// and https://
returns domain name only with or without www as user provided.
"""
# Check if http:// or https:// present remove it if present
domain_name = url.split('/')

58
ee/core/fileutils.py

@ -10,11 +10,12 @@ from ee.core.logging import Log
class EEFileUtils():
"""Method to operate on files"""
"""Utilities to operate on files"""
def __init__():
pass
def remove(self, filelist):
"""remove files from given path"""
for file in filelist:
if os.path.isfile(file):
Log.info(self, "Removing {0:65}".format(file), end=' ')
@ -33,6 +34,10 @@ class EEFileUtils():
Log.error(self, 'Unable to Remove file ')
def create_symlink(self, paths, errormsg=''):
"""
Create symbolic links provided in list with first as source
and second as destination
"""
src = paths[0]
dst = paths[1]
if not os.path.islink(dst):
@ -45,6 +50,9 @@ class EEFileUtils():
Log.debug(self, "Destination: {0} exists".format(dst))
def remove_symlink(self, filepath):
"""
Removes symbolic link for the path provided with filepath
"""
try:
os.unlink(filepath)
except Exception as e:
@ -52,6 +60,11 @@ class EEFileUtils():
Log.error(self, "Unable to reomove symbolic link ...\n")
def copyfile(self, src, dest):
"""
Copies files:
src : source path
dest : destination path
"""
try:
shutil.copy2(src, dest)
except shutil.Error as e:
@ -64,6 +77,12 @@ class EEFileUtils():
.fromat(src, dest))
def searchreplace(self, fnm, sstr, rstr):
"""
Search replace strings in file
fnm : filename
sstr: search string
rstr: replace string
"""
try:
for line in fileinput.input(fnm, inplace=True):
print(line.replace(sstr, rstr), end='')
@ -74,6 +93,11 @@ class EEFileUtils():
.format(fnm, sstr, rstr))
def mvfile(self, src, dst):
"""
Moves file from source path to destination path
src : source path
dst : Destination path
"""
try:
Log.debug(self, "Moving file from {0} to {1}".format(src, dst))
shutil.move(src, dst)
@ -83,6 +107,10 @@ class EEFileUtils():
.format(src, dst))
def chdir(self, path):
"""
Change Directory to path specified
Path : path for destination directory
"""
try:
os.chdir(path)
except OSError as e:
@ -90,6 +118,14 @@ class EEFileUtils():
Log.error(self, 'Unable to Change Directory {0}'.format(path))
def chown(self, path, user, group, recursive=False):
"""
Change Owner for files
change owner for file with path specified
user: username of owner
group: group of owner
recursive: if recursive is True change owner for all
files in directory
"""
userid = pwd.getpwnam(user)[2]
groupid = pwd.getpwnam(user)[3]
try:
@ -111,6 +147,12 @@ class EEFileUtils():
Log.error(self, "Unable to change owner : {0} ".format(path))
def chmod(self, path, perm, recursive=False):
"""
Changes Permission for files
path : file path permission to be changed
perm : permissions to be given
recursive: change permission recursively for all files
"""
try:
if recursive:
for root, dirs, files in os.walk(path):
@ -125,6 +167,11 @@ class EEFileUtils():
Log.error(self, "Unable to change owner : {0}".format(path))
def mkdir(self, path):
"""
create directories.
path : path for directory to be created
Similar to `mkdir -p`
"""
try:
os.makedirs(path)
except OSError as e:
@ -132,6 +179,9 @@ class EEFileUtils():
Log.error(self, "Unable to create directory {0} ".format(path))
def isexist(self, path):
"""
Check if file exist on given path
"""
try:
if os.path.exists(path):
return (True)
@ -142,6 +192,9 @@ class EEFileUtils():
Log.error(self, "Unable to check path {0}".format(path))
def grep(self, fnm, sstr):
"""
Searches for string in file and returns the matched line.
"""
try:
for line in open(fnm):
if sstr in line:
@ -152,6 +205,9 @@ class EEFileUtils():
.format(sstr, fnm))
def rm(self, path):
"""
Remove files
"""
if EEFileUtils.isexist(self, path):
try:
if os.path.isdir(path):

7
ee/core/git.py

@ -11,6 +11,10 @@ class EEGit:
pass
def add(self, paths, msg="Intializating"):
"""
Initializes Directory as repository if not already git repo.
and adds uncommited changes automatically
"""
for path in paths:
global git
git = git.bake("--git-dir={0}/.git".format(path),
@ -40,6 +44,9 @@ class EEGit:
Log.debug(self, "EEGit: Path {0} not present".format(path))
def checkfilestatus(self, repo, filepath):
"""
Checks status of file, If its tracked or untracked.
"""
global git
git = git.bake("--git-dir={0}/.git".format(repo),
"--work-tree={0}".format(repo))

16
ee/core/logging.py

@ -2,6 +2,10 @@
class Log:
"""
Logs messages with colors for different messages
according to functions
"""
HEADER = '\033[95m'
OKBLUE = '\033[94m'
OKGREEN = '\033[92m'
@ -12,16 +16,28 @@ class Log:
UNDERLINE = '\033[4m'
def error(self, msg):
"""
Logs error into log file
"""
print(Log.FAIL + msg + Log.ENDC)
self.app.log.error(Log.FAIL + msg + Log.ENDC)
self.app.close(1)
def info(self, msg, end='\n'):
"""
Logs info messages into log file
"""
print(Log.OKBLUE + msg + Log.ENDC, end=end)
self.app.log.info(Log.OKBLUE + msg + Log.ENDC)
def warn(self, msg):
"""
Logs warning into log file
"""
self.app.log.warn(Log.BOLD + msg + Log.ENDC)
def debug(self, msg):
"""
Logs debug messages into log file
"""
self.app.log.debug(Log.HEADER + msg + Log.ENDC)

3
ee/core/models.py

@ -3,6 +3,9 @@ from ee.core.database import Base
class SiteDB(Base):
"""
Databse model for site table
"""
__tablename__ = 'sites'
id = Column(Integer, primary_key=True)
sitename = Column(String, unique=True)

161
ee/core/services.py

@ -13,91 +13,106 @@ class EEService():
pass
def start_service(self, service_name):
try:
Log.info(self, "Start : {0:10}" .format(service_name), end='')
retcode = subprocess.getstatusoutput('service {0} start'
.format(service_name))
if retcode[0] == 0:
Log.info(self, "[" + Log.ENDC + "OK" + Log.OKBLUE + "]")
return True
else:
Log.debug(self, "{0}".format(retcode[1]))
Log.info(self, "[" + Log.FAIL + "Failed" + Log.OKBLUE+"]")
return False
except OSError as e:
Log.debug(self, "{0}".format(e))
Log.error(self, "\nFailed to start service {0}"
.format(service_name))
"""
start service
Similar to `service xyz start`
"""
try:
Log.info(self, "Start : {0:10}" .format(service_name), end='')
retcode = subprocess.getstatusoutput('service {0} start'
.format(service_name))
if retcode[0] == 0:
Log.info(self, "[" + Log.ENDC + "OK" + Log.OKBLUE + "]")
return True
else:
Log.debug(self, "{0}".format(retcode[1]))
Log.info(self, "[" + Log.FAIL + "Failed" + Log.OKBLUE+"]")
return False
except OSError as e:
Log.debug(self, "{0}".format(e))
Log.error(self, "\nFailed to start service {0}"
.format(service_name))
def stop_service(self, service_name):
try:
Log.info(self, "Stop : {0:10}" .format(service_name), end='')
retcode = subprocess.getstatusoutput('service {0} stop'
.format(service_name))
if retcode[0] == 0:
Log.info(self, "[" + Log.ENDC + "OK" + Log.OKBLUE + "]")
return True
else:
Log.debug(self, "{0}".format(retcode[1]))
Log.info(self, "[" + Log.FAIL + "Failed" + Log.OKBLUE+"]")
return False
except OSError as e:
Log.debug(self, "{0}".format(e))
Log.error(self, "\nFailed to stop service : {0}"
.format(service_name))
"""
Stop service
Similar to `service xyz stop`
"""
try:
Log.info(self, "Stop : {0:10}" .format(service_name), end='')
retcode = subprocess.getstatusoutput('service {0} stop'
.format(service_name))
if retcode[0] == 0:
Log.info(self, "[" + Log.ENDC + "OK" + Log.OKBLUE + "]")
return True
else:
Log.debug(self, "{0}".format(retcode[1]))
Log.info(self, "[" + Log.FAIL + "Failed" + Log.OKBLUE+"]")
return False
except OSError as e:
Log.debug(self, "{0}".format(e))
Log.error(self, "\nFailed to stop service : {0}"
.format(service_name))
def restart_service(self, service_name):
try:
Log.info(self, "Restart : {0:10}".format(service_name), end='')
retcode = subprocess.getstatusoutput('service {0} restart'
.format(service_name))
if retcode[0] == 0:
Log.info(self, "[" + Log.ENDC + "OK" + Log.OKBLUE + "]")
return True
else:
Log.debug(self, "{0}".format(retcode[1]))
Log.info(self, "[" + Log.FAIL + "Failed" + Log.OKBLUE+"]")
return False
except OSError as e:
Log.debug(self, "{0} {1}".format(e.errno, e.strerror))
Log.error(self, "\nFailed to restart service : {0}"
.format(service_name))
"""
Restart service
Similar to `service xyz restart`
"""
try:
Log.info(self, "Restart : {0:10}".format(service_name), end='')
retcode = subprocess.getstatusoutput('service {0} restart'
.format(service_name))
if retcode[0] == 0:
Log.info(self, "[" + Log.ENDC + "OK" + Log.OKBLUE + "]")
return True
else:
Log.debug(self, "{0}".format(retcode[1]))
Log.info(self, "[" + Log.FAIL + "Failed" + Log.OKBLUE+"]")
return False
except OSError as e:
Log.debug(self, "{0} {1}".format(e.errno, e.strerror))
Log.error(self, "\nFailed to restart service : {0}"
.format(service_name))
def reload_service(self, service_name):
try:
if service_name in ['nginx', 'php5-fpm']:
Log.info(self, "Reload : {0:10}".format(service_name),
end='')
retcode = subprocess.getstatusoutput('{0} -t &&'
' service {0} reload'
.format(service_name))
if retcode[0] == 0:
# print(retcode[0])
# subprocess.getstatusoutput('service {0} reload'
# .format(service_name))
Log.info(self, "[" + Log.ENDC + "OK" + Log.OKBLUE +
"]")
return True
else:
Log.debug(self, "{0}".format(retcode[1]))
Log.info(self, "[" + Log.FAIL + "Failed" +
Log.OKBLUE+"]")
return False
Log.info(self, "Reload : {0:10}".format(service_name), end='')
retcode = subprocess.getstatusoutput('service {0} reload'
"""
Stop service
Similar to `service xyz stop`
"""
try:
if service_name in ['nginx', 'php5-fpm']:
Log.info(self, "Reload : {0:10}".format(service_name),
end='')
retcode = subprocess.getstatusoutput('{0} -t &&'
' service {0} reload'
.format(service_name))
if retcode[0] == 0:
Log.info(self, "[" + Log.ENDC + "OK" + Log.OKBLUE + "]")
# print(retcode[0])
# subprocess.getstatusoutput('service {0} reload'
# .format(service_name))
Log.info(self, "[" + Log.ENDC + "OK" + Log.OKBLUE +
"]")
return True
else:
Log.debug(self, "{0}".format(retcode[1]))
Log.info(self, "[" + Log.FAIL + "Failed" + Log.OKBLUE+"]")
Log.info(self, "[" + Log.FAIL + "Failed" +
Log.OKBLUE+"]")
return False
except OSError as e:
Log.debug(self, "{0}".format(e))
Log.error(self, "\nFailed to reload service {0}"
.format(service_name))
Log.info(self, "Reload : {0:10}".format(service_name), end='')
retcode = subprocess.getstatusoutput('service {0} reload'
.format(service_name))
if retcode[0] == 0:
Log.info(self, "[" + Log.ENDC + "OK" + Log.OKBLUE + "]")
return True
else:
Log.debug(self, "{0}".format(retcode[1]))
Log.info(self, "[" + Log.FAIL + "Failed" + Log.OKBLUE+"]")
return False
except OSError as e:
Log.debug(self, "{0}".format(e))
Log.error(self, "\nFailed to reload service {0}"
.format(service_name))
def get_service_status(self, service_name):
try:

3
ee/core/shellexec.py

@ -36,6 +36,9 @@ class EEShellExec():
.format(command))
def invoke_editor(self, filepath, errormsg=''):
"""
Open files using sensible editor
"""
try:
subprocess.call(['sensible-editor', filepath])
except OSError as e:

Loading…
Cancel
Save