diff --git a/.gitattributes b/.gitattributes new file mode 100644 index 00000000..01c7be27 --- /dev/null +++ b/.gitattributes @@ -0,0 +1 @@ +web.py ident \ No newline at end of file diff --git a/cps/helper.py b/cps/helper.py index d861af38..63000609 100755 --- a/cps/helper.py +++ b/cps/helper.py @@ -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) \ No newline at end of file diff --git a/cps/templates/admin.html b/cps/templates/admin.html index 4f5c67ec..48343a76 100644 --- a/cps/templates/admin.html +++ b/cps/templates/admin.html @@ -78,6 +78,8 @@ {% if not development %}
{{_('Restart Calibre-web')}}
{{_('Stop Calibre-web')}}
+
{{_('Check for update')}}
+ {% endif %} @@ -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')}}'); + };} + }); + }); {% endblock %} diff --git a/cps/web.py b/cps/web.py index 735318e9..4d4612d2 100755 --- a/cps/web.py +++ b/cps/web.py @@ -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"))