Browse Source

pep8

master
Maxim Likhachev 5 years ago
parent
commit
f550c726b2
  1. 48
      java-app-updater.py

48
service.py → java-app-updater.py

@ -1,19 +1,19 @@
#!/usr/bin/env python #!/usr/bin/env python
# -*- coding: utf-8 -*- # -*- coding: utf-8 -*-
# Copyright (C) 2017, Maxim Lihachev, <envrm@yandex.ru>
# #
# Script for upgrading java/play application from git # This program is free software: you can redistribute it and/or modify it
# under the terms of the GNU General Public License as published by the Free
# Software Foundation, version 3.
# #
# (c) envrm # This program is distributed in the hope that it will be useful, but WITHOUT
# # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
# FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
#Requirements # more details.
#
# CentOS: yum -y install python-pip
# Debian: apt-get install python-pip
#
# pip install configparser
# #
# You should have received a copy of the GNU General Public License along with
# this program. If not, see <https://www.gnu.org/licenses/>.
import re import re
import os import os
@ -28,6 +28,7 @@ import configparser
from string import Template from string import Template
from contextlib import contextmanager from contextlib import contextmanager
@contextmanager @contextmanager
def if_needed(): def if_needed():
'''Execute with error ignoring''' '''Execute with error ignoring'''
@ -36,6 +37,7 @@ def if_needed():
except: except:
pass pass
class Dir: class Dir:
'''Execute procedures in directory''' '''Execute procedures in directory'''
def __init__(self, directory): def __init__(self, directory):
@ -48,7 +50,8 @@ class Dir:
def __exit__(self, type, value, traceback): def __exit__(self, type, value, traceback):
os.chdir(self.previous_dir) os.chdir(self.previous_dir)
#-----------------------------------------------------------------------
# ----------------------------------------------------------------------
def readConfig(config_file): def readConfig(config_file):
'''Loading configuration file''' '''Loading configuration file'''
@ -58,6 +61,7 @@ def readConfig(config_file):
settings.read(config_file) settings.read(config_file)
return settings return settings
def opt(parameter): def opt(parameter):
'''Getting parameters from file''' '''Getting parameters from file'''
setting = parameter.split(":") setting = parameter.split(":")
@ -69,13 +73,15 @@ def opt(parameter):
return Template(value).safe_substitute(**substr) return Template(value).safe_substitute(**substr)
def saveTimestamp(outfile, mode="a"): def saveTimestamp(outfile, mode="a"):
'''Saving timestamt to file''' '''Saving timestamt to file'''
with open(outfile, mode) as log: with open(outfile, mode) as log:
now = datetime.datetime.now() now = datetime.datetime.now()
log.write(now.strftime("%Y-%m-%d %H:%M:%S\n")) log.write(now.strftime("%Y-%m-%d %H:%M:%S\n"))
#-----------------------------------------------------------------------
# ----------------------------------------------------------------------
def git(command, stand="develop"): def git(command, stand="develop"):
'''Executing git command''' '''Executing git command'''
@ -84,6 +90,7 @@ def git(command, stand="develop"):
return git_output return git_output
def stop(stand="develop"): def stop(stand="develop"):
'''Stop service''' '''Stop service'''
if os.path.isfile(opt(stand + ':pid')): if os.path.isfile(opt(stand + ':pid')):
@ -98,6 +105,7 @@ def stop(stand="develop"):
os.remove(pid) os.remove(pid)
print "#### Application stopped" print "#### Application stopped"
def start(stand): def start(stand):
'''Start service''' '''Start service'''
service_dir = opt(stand + ':path') service_dir = opt(stand + ':path')
@ -118,16 +126,19 @@ def start(stand):
print return_code print return_code
exit(1) exit(1)
def restart(stand="develop"): def restart(stand="develop"):
'''Restart service''' '''Restart service'''
stop(stand) stop(stand)
start(stand) start(stand)
saveTimestamp(opt(stand + ':restarts_log')) saveTimestamp(opt(stand + ':restarts_log'))
def clean_all(): def clean_all():
'''Remove artefacts''' '''Remove artefacts'''
os.system("rm -rfv " + opt('develop:targets')) os.system("rm -rfv " + opt('develop:targets'))
def build(target="stand", modules=None): def build(target="stand", modules=None):
'''Build service. [stand|modules|all]''' '''Build service. [stand|modules|all]'''
play_cmd = opt(SERVICE + ':play') + " " + opt(SERVICE + ':sbt_config') play_cmd = opt(SERVICE + ':play') + " " + opt(SERVICE + ':sbt_config')
@ -136,7 +147,7 @@ def build(target="stand", modules=None):
if target == "modules" or target == "all": if target == "modules" or target == "all":
with Dir(opt('develop:modules_dir')): with Dir(opt('develop:modules_dir')):
if modules == None: if modules is None:
modules = os.walk(".").next()[1] modules = os.walk(".").next()[1]
modules = filter(None, set(modules)) modules = filter(None, set(modules))
@ -164,6 +175,7 @@ def build(target="stand", modules=None):
if return_code != 0: if return_code != 0:
exit(1) exit(1)
def update_from_git(): def update_from_git():
'''Update app with modules''' '''Update app with modules'''
git('checkout ' + opt('build:git_branch')) git('checkout ' + opt('build:git_branch'))
@ -180,20 +192,23 @@ def update_from_git():
build("all", modules) build("all", modules)
def copy_libs(): def copy_libs():
'''Copy libraries''' '''Copy libraries'''
with if_needed(): with if_needed():
shutil.rmtree(opt(SERVICE + ':class_path')) shutil.rmtree(opt(SERVICE + ':class_path'))
shutil.copytree(opt('develop:class_path'), opt(SERVICE + ':class_path')) shutil.copytree(opt('develop:class_path'), opt(SERVICE + ':class_path'))
def update(): def update():
'''Update current service from repository''' '''Update current service from repository'''
saveTimestamp(opt('develop:restarts_log')) saveTimestamp(opt('develop:restarts_log'))
stop() stop()
update_from_git() update_from_git()
start() start(SERVICE)
saveTimestamp(opt('develop:update_log'), 'w') saveTimestamp(opt('develop:update_log'), 'w')
def upgrade(): def upgrade():
'''Update developing service from repository''' '''Update developing service from repository'''
update_from_git() update_from_git()
@ -204,7 +219,8 @@ def upgrade():
start(SERVICE) start(SERVICE)
saveTimestamp(opt(SERVICE + ':update_log'), 'w') saveTimestamp(opt(SERVICE + ':update_log'), 'w')
#-----------------------------------------------------------------------
# ----------------------------------------------------------------------
def show_help(): def show_help():
exe = os.path.basename(sys.argv[0]) exe = os.path.basename(sys.argv[0])
@ -220,6 +236,7 @@ def show_help():
print "\n[service] can be one of «" + "», «".join(ast.literal_eval(opt('service:projects')).keys()) + "»." print "\n[service] can be one of «" + "», «".join(ast.literal_eval(opt('service:projects')).keys()) + "»."
print "\nDefault value of [service] is", opt('service:default'), "\n" print "\nDefault value of [service] is", opt('service:default'), "\n"
settings = [] settings = []
readConfig('config.ini') readConfig('config.ini')
@ -262,4 +279,3 @@ if task:
else: else:
print "CMD: " + sys.argv[arg_index] print "CMD: " + sys.argv[arg_index]
task() task()
Loading…
Cancel
Save