diff --git a/Dockerfile b/Dockerfile index 95c26a2..e78be40 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,13 +1,19 @@ -FROM alpine:latest +FROM alpine:3.12 +# fetching cheat sheets +## installing dependencies +RUN apk add --update --no-cache git py3-six py3-pygments py3-yaml py3-gevent \ + libstdc++ py3-colorama py3-requests py3-icu py3-redis +## building missing python packages +RUN apk add --no-cache --virtual build-deps py3-pip g++ python3-dev \ + && pip3 install --no-cache-dir rapidfuzz colored polyglot pycld2 \ + && apk del build-deps +## copying WORKDIR /app COPY . /app -RUN apk add --update --no-cache python2 py2-pip py2-gevent \ - py2-flask py2-requests py2-pygments py2-redis \ - py2-cffi py2-icu bash vim gawk sed \ - && apk add --no-cache --virtual build-deps python2-dev \ - build-base git \ - && pip install -r requirements.txt \ - && sh share/scripts/get-sheets.sh \ - && apk del build-deps -ENTRYPOINT ["python2"] +RUN mkdir -p /root/.cheat.sh/log/ \ + && python3 lib/fetch.py fetch-all + +# installing server dependencies +RUN apk add --update --no-cache py3-jinja2 py3-flask bash gawk +ENTRYPOINT ["python3"] CMD ["bin/srv.py"] diff --git a/bin/srv.py b/bin/srv.py index 8f623a8..52aaff1 100644 --- a/bin/srv.py +++ b/bin/srv.py @@ -96,9 +96,9 @@ def log_query(ip_addr, found, topic, user_agent): """ Log processed query and some internal data """ - log_entry = "%s %s %s %s" % (ip_addr, found, topic, user_agent) - with open(CONFIG["path.log.queries"], 'a') as my_file: - my_file.write(log_entry.encode('utf-8')+"\n") + log_entry = "%s %s %s %s\n" % (ip_addr, found, topic, user_agent) + with open(CONFIG["path.log.queries"], 'ab') as my_file: + my_file.write(log_entry.encode('utf-8')) def get_request_ip(req): """ @@ -274,9 +274,13 @@ def answer(topic=None): return result return Response(result, mimetype='text/plain') + +if '--debug' in sys.argv: + app.debug = True if 'CHEATSH_PORT' in os.environ: - SRV = WSGIServer((CONFIG['server.bind'], int(os.environ.get('CHEATSH_PORT'))), app) # log=None) - SRV.serve_forever() + PORT = int(os.environ.get('CHEATSH_PORT')) else: - SRV = WSGIServer((CONFIG['server.bind'], CONFIG['server.port']), app) # log=None) - SRV.serve_forever() + PORT = CONFIG['server.port'] +SRV = WSGIServer((CONFIG['server.bind'], PORT), app) # log=None) +print("Starting server on {}:{}".format(SRV.address[0], SRV.address[1])) +SRV.serve_forever() diff --git a/lib/adapter/git_adapter.py b/lib/adapter/git_adapter.py index e91a0d7..b8a8d29 100644 --- a/lib/adapter/git_adapter.py +++ b/lib/adapter/git_adapter.py @@ -79,7 +79,7 @@ class GitRepositoryAdapter(RepositoryAdapter): #pylint: disable=abstract-meth if not local_repository_dir: return None - return ['git', 'clone', cls._repository_url, local_repository_dir] + return ['git', 'clone', '--depth=1', cls._repository_url, local_repository_dir] @classmethod def update_command(cls): diff --git a/lib/fetch.py b/lib/fetch.py index fb8c951..75a0304 100644 --- a/lib/fetch.py +++ b/lib/fetch.py @@ -56,7 +56,11 @@ def fetch_all(skip_existing=True): sys.stdout.write("Fetching %s..." % (adptr)) sys.stdout.flush() - process = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.STDOUT) + try: + process = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.STDOUT) + except OSError: + print("\nERROR: %s" % cmd) + raise output = process.communicate()[0] if process.returncode != 0: sys.stdout.write("\nERROR:\n---\n" + output) diff --git a/lib/frontend/html.py b/lib/frontend/html.py index c639e04..c73e96c 100644 --- a/lib/frontend/html.py +++ b/lib/frontend/html.py @@ -75,13 +75,16 @@ def _render_html(query, result, editable, repository_button, topics_list, reques """ Convert ANSI text `data` to HTML """ - proc = Popen( - ["bash", CONFIG['path.internal.ansi2html'], "--palette=solarized", "--bg=dark"], - stdin=PIPE, stdout=PIPE, stderr=PIPE) + cmd = ["bash", CONFIG['path.internal.ansi2html'], "--palette=solarized", "--bg=dark"] + try: + proc = Popen(cmd, stdin=PIPE, stdout=PIPE, stderr=PIPE) + except FileNotFoundError: + print("ERROR: %s" % cmd) + raise data = data.encode('utf-8') stdout, stderr = proc.communicate(data) if proc.returncode != 0: - error(stdout + stderr) + error((stdout + stderr).decode('utf-8')) return stdout.decode('utf-8')