1
0
mirror of https://github.com/janeczku/calibre-web synced 2024-12-24 09:00:29 +00:00

- Implemented autoupdater for testing

- failed logins are logged
- no of backups for log-file increased to 2
This commit is contained in:
OzzieIsaacs 2017-01-30 18:58:36 +01:00
parent 935b6e3143
commit b1293c84bc
4 changed files with 159 additions and 2 deletions

1
.gitattributes vendored Normal file
View File

@ -0,0 +1 @@
web.py ident

View File

@ -21,7 +21,7 @@ from email.MIMEText import MIMEText
from email.generator import Generator
from flask_babel import gettext as _
import subprocess
import shutil
def update_download(book_id, user_id):
check = ub.session.query(ub.Downloads).filter(ub.Downloads.user_id == user_id).filter(ub.Downloads.book_id ==
@ -254,3 +254,105 @@ def update_dir_stucture(book_id, calibrepath):
os.renames(path, new_author_path)
book.path = new_authordir + os.sep + book.path.split(os.sep)[1]
db.session.commit()
def file_to_list(file):
return [x.strip() for x in open(file, 'r') if not x.startswith('#EXT')]
def one_minus_two(one, two):
return [x for x in one if x not in set(two)]
def reduce_dirs(delete_files, new_list):
new_delete = []
for file in delete_files:
parts = file.split(os.sep)
sub = ''
for i in range(len(parts)):
sub = os.path.join(sub, parts[i])
if sub == '':
sub = os.sep
count = 0
for song in new_list:
if song.startswith(sub):
count += 1
break
if count == 0:
if sub != '\\':
new_delete.append(sub)
break
return list(set(new_delete))
def reduce_files(remove_items, exclude_items):
rf = []
for item in remove_items:
if not item in exclude_items:
rf.append(item)
return rf
def moveallfiles(root_src_dir, root_dst_dir):
change_permissions = False
if sys.platform == "win32" or sys.platform == "darwin":
change_permissions=True
else:
app.logger.debug('OS-System : '+sys.platform )
new_permissions=os.stat(root_dst_dir)
for src_dir, dirs, files in os.walk(root_src_dir):
dst_dir = src_dir.replace(root_src_dir, root_dst_dir, 1)
if not os.path.exists(dst_dir):
os.makedirs(dst_dir)
if change_permissions:
os.chown(dst_dir,new_permissions.ST_UID,new_permissions.ST_GID)
for file_ in files:
src_file = os.path.join(src_dir, file_)
dst_file = os.path.join(dst_dir, file_)
if change_permissions:
permission=os.stat(dst_file)
if os.path.exists(dst_file):
os.remove(dst_file)
shutil.move(src_file, dst_dir)
if change_permissions:
os.chown(dst_file, permission.ST_UID, permission.ST_GID)
return
def update_source(source,destination):
# destination files
old_list=list()
exclude = (['vendor' + os.sep + 'kindlegen.exe','vendor' + os.sep + 'kindlegen','/app.db','vendor','/update.py'])
for root, dirs, files in os.walk(destination, topdown=True):
for name in files:
old_list.append(os.path.join(root, name).replace(destination, ''))
for name in dirs:
old_list.append(os.path.join(root, name).replace(destination, ''))
# source files
new_list = list()
for root, dirs, files in os.walk(source, topdown=True):
for name in files:
new_list.append(os.path.join(root, name).replace(source, ''))
for name in dirs:
new_list.append(os.path.join(root, name).replace(source, ''))
delete_files = one_minus_two(old_list, new_list)
print('raw delete list', delete_files)
rf= reduce_files(delete_files, exclude)
print('reduced delete list', rf)
remove_items = reduce_dirs(rf, new_list)
print('delete files', remove_items)
moveallfiles(source, destination)
for item in remove_items:
item_path = os.path.join(destination, item[1:])
if os.path.isdir(item_path):
# app.logger.info("Delete dir "+ item_path)
print("Delete dir "+ item_path)
#shutil.rmtree(item_path)
else:
try:
print("Delete file "+ item_path)
#os.remove(item_path)
except:
print("Could not remove:"+item_path)
shutil.rmtree(source, ignore_errors=True)

View File

@ -78,6 +78,8 @@
{% if not development %}
<div class="btn btn-default" data-toggle="modal" data-target="#RestartDialog">{{_('Restart Calibre-web')}}</a></div>
<div class="btn btn-default" data-toggle="modal" data-target="#ShutdownDialog">{{_('Stop Calibre-web')}}</a></div>
<div class="btn btn-default" id="check_for_update">{{_('Check for update')}}</a></div>
<a href="{{url_for('update')}}" class="btn btn-default hidden" id="perform_update">{{_('Perform Update')}}</a>
{% endif %}
</div>
<!-- Modal -->
@ -134,5 +136,19 @@
return alert(data.text);}
});
});
$("#check_for_update").click(function() {
$("#check_for_update").html('Checking...');
$.ajax({
dataType: 'json',
url: "{{url_for('get_update_status')}}",
success: function(data) {
if (data.status == true) {
$("#check_for_update").addClass('hidden');
$("#perform_update").removeClass('hidden');
}else{
$("#check_for_update").html('{{_('Check for update')}}');
};}
});
});
</script>
{% endblock %}

View File

@ -3,6 +3,7 @@
import mimetypes
import logging
from logging.handlers import RotatingFileHandler
from tempfile import gettempdir
import textwrap
from flask import Flask, render_template, session, request, Response, redirect, url_for, send_from_directory, \
make_response, g, flash, abort
@ -39,6 +40,7 @@ import re
import db
from shutil import move, copyfile
from tornado.ioloop import IOLoop
import StringIO
try:
from wand.image import Image
@ -108,7 +110,7 @@ app.wsgi_app = ReverseProxied(app.wsgi_app)
formatter = logging.Formatter(
"[%(asctime)s] {%(pathname)s:%(lineno)d} %(levelname)s - %(message)s")
file_handler = RotatingFileHandler(os.path.join(config.get_main_dir, "calibre-web.log"), maxBytes=50000, backupCount=1)
file_handler = RotatingFileHandler(os.path.join(config.get_main_dir, "calibre-web.log"), maxBytes=50000, backupCount=2)
file_handler.setFormatter(formatter)
app.logger.addHandler(file_handler)
app.logger.setLevel(config.config_log_level)
@ -707,6 +709,20 @@ def get_tags_json():
json_dumps = json.dumps([dict(r) for r in entries])
return json_dumps
@app.route("/get_update_status", methods=['GET'])
@login_required_if_no_ano
def get_update_status():
status = {}
if request.method == "GET":
commit_id = '$Id$'
commit = requests.get('https://api.github.com/repos/janeczku/calibre-web/git/refs/heads/master').json()
if "object" in commit and commit['object']['sha'] != commit_id[5:-2]:
status['status'] = True
else:
status['status'] = False
return json.dumps(status)
@app.route("/get_languages_json", methods=['GET', 'POST'])
@login_required_if_no_ano
@ -1019,6 +1035,27 @@ def shutdown():
else:
abort(404)
@app.route("/update")
@login_required
@admin_required
def update():
global global_task
r = requests.get('https://api.github.com/repos/janeczku/calibre-web/zipball/master', stream=True)
fname = re.findall("filename=(.+)", r.headers['content-disposition'])[0]
z = zipfile.ZipFile(StringIO.StringIO(r.content))
tmp_dir = gettempdir()
z.extractall(tmp_dir)
helper.update_source(os.path.join(tmp_dir,os.path.splitext(fname)[0]),config.get_main_dir)
global_task = 0
db.session.close()
db.engine.dispose()
ub.session.close()
ub.engine.dispose()
# stop tornado server
server = IOLoop.instance()
server.add_callback(server.stop)
flash(_(u"Update done"), category="info")
return logout()
@app.route("/search", methods=["GET"])
@login_required_if_no_ano
@ -1269,6 +1306,7 @@ def login():
# test=
return redirect(url_for("index"))
else:
app.logger.info('Login failed for user "'+form['username']+'"')
flash(_(u"Wrong Username or Password"), category="error")
return render_title_template('login.html', title=_(u"login"))