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 @@ @@ -1,19 +1,19 @@
#!/usr/bin/env python
# -*- 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
#
#Requirements
#
# CentOS: yum -y install python-pip
# Debian: apt-get install python-pip
#
# pip install configparser
# 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
# more details.
#
# 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 os
@ -28,6 +28,7 @@ import configparser @@ -28,6 +28,7 @@ import configparser
from string import Template
from contextlib import contextmanager
@contextmanager
def if_needed():
'''Execute with error ignoring'''
@ -36,6 +37,7 @@ def if_needed(): @@ -36,6 +37,7 @@ def if_needed():
except:
pass
class Dir:
'''Execute procedures in directory'''
def __init__(self, directory):
@ -48,7 +50,8 @@ class Dir: @@ -48,7 +50,8 @@ class Dir:
def __exit__(self, type, value, traceback):
os.chdir(self.previous_dir)
#-----------------------------------------------------------------------
# ----------------------------------------------------------------------
def readConfig(config_file):
'''Loading configuration file'''
@ -58,6 +61,7 @@ def readConfig(config_file): @@ -58,6 +61,7 @@ def readConfig(config_file):
settings.read(config_file)
return settings
def opt(parameter):
'''Getting parameters from file'''
setting = parameter.split(":")
@ -69,13 +73,15 @@ def opt(parameter): @@ -69,13 +73,15 @@ def opt(parameter):
return Template(value).safe_substitute(**substr)
def saveTimestamp(outfile, mode="a"):
'''Saving timestamt to file'''
with open(outfile, mode) as log:
now = datetime.datetime.now()
log.write(now.strftime("%Y-%m-%d %H:%M:%S\n"))
#-----------------------------------------------------------------------
# ----------------------------------------------------------------------
def git(command, stand="develop"):
'''Executing git command'''
@ -84,6 +90,7 @@ def git(command, stand="develop"): @@ -84,6 +90,7 @@ def git(command, stand="develop"):
return git_output
def stop(stand="develop"):
'''Stop service'''
if os.path.isfile(opt(stand + ':pid')):
@ -98,6 +105,7 @@ def stop(stand="develop"): @@ -98,6 +105,7 @@ def stop(stand="develop"):
os.remove(pid)
print "#### Application stopped"
def start(stand):
'''Start service'''
service_dir = opt(stand + ':path')
@ -118,16 +126,19 @@ def start(stand): @@ -118,16 +126,19 @@ def start(stand):
print return_code
exit(1)
def restart(stand="develop"):
'''Restart service'''
stop(stand)
start(stand)
saveTimestamp(opt(stand + ':restarts_log'))
def clean_all():
'''Remove artefacts'''
os.system("rm -rfv " + opt('develop:targets'))
def build(target="stand", modules=None):
'''Build service. [stand|modules|all]'''
play_cmd = opt(SERVICE + ':play') + " " + opt(SERVICE + ':sbt_config')
@ -136,7 +147,7 @@ def build(target="stand", modules=None): @@ -136,7 +147,7 @@ def build(target="stand", modules=None):
if target == "modules" or target == "all":
with Dir(opt('develop:modules_dir')):
if modules == None:
if modules is None:
modules = os.walk(".").next()[1]
modules = filter(None, set(modules))
@ -164,6 +175,7 @@ def build(target="stand", modules=None): @@ -164,6 +175,7 @@ def build(target="stand", modules=None):
if return_code != 0:
exit(1)
def update_from_git():
'''Update app with modules'''
git('checkout ' + opt('build:git_branch'))
@ -180,20 +192,23 @@ def update_from_git(): @@ -180,20 +192,23 @@ def update_from_git():
build("all", modules)
def copy_libs():
'''Copy libraries'''
with if_needed():
shutil.rmtree(opt(SERVICE + ':class_path'))
shutil.copytree(opt('develop:class_path'), opt(SERVICE + ':class_path'))
def update():
'''Update current service from repository'''
saveTimestamp(opt('develop:restarts_log'))
stop()
update_from_git()
start()
start(SERVICE)
saveTimestamp(opt('develop:update_log'), 'w')
def upgrade():
'''Update developing service from repository'''
update_from_git()
@ -204,7 +219,8 @@ def upgrade(): @@ -204,7 +219,8 @@ def upgrade():
start(SERVICE)
saveTimestamp(opt(SERVICE + ':update_log'), 'w')
#-----------------------------------------------------------------------
# ----------------------------------------------------------------------
def show_help():
exe = os.path.basename(sys.argv[0])
@ -220,6 +236,7 @@ def show_help(): @@ -220,6 +236,7 @@ def show_help():
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"
settings = []
readConfig('config.ini')
@ -262,4 +279,3 @@ if task: @@ -262,4 +279,3 @@ if task:
else:
print "CMD: " + sys.argv[arg_index]
task()
Loading…
Cancel
Save