Do a first naive pass for py3 compatibility

Check that open() calls for reading and writing use binary modes
Use alpine version with py3-pip package
Use python3 in Dockerfile CMD
Remove sys.setdefaultencoding() "hack"
Simplify ensure_directory() in common.common package
pull/224/head
HubbeKing 3 years ago committed by Mike Lang
parent f0546e2ee3
commit 6d790a1b36

@ -1,7 +1,7 @@
FROM alpine:3.7 FROM alpine:3.14
# dependencies needed for compiling c extensions # dependencies needed for compiling c extensions
# also busybox-extras for telnet for easier use of backdoor # also busybox-extras for telnet for easier use of backdoor
RUN apk --update add py2-pip gcc python-dev musl-dev file make busybox-extras RUN apk --update add py3-pip gcc python-dev musl-dev file make busybox-extras
# Install gevent so that we don't need to re-install it when common changes # Install gevent so that we don't need to re-install it when common changes
RUN pip install gevent RUN pip install gevent
@ -14,4 +14,4 @@ RUN pip install /tmp/common && rm -r /tmp/common
COPY api_ping /tmp/api_ping COPY api_ping /tmp/api_ping
RUN pip install /tmp/api_ping && rm -r /tmp/api_ping RUN pip install /tmp/api_ping && rm -r /tmp/api_ping
ENTRYPOINT ["python2", "-m", "api_ping"] ENTRYPOINT ["python3", "-m", "api_ping"]

@ -1,7 +1,7 @@
FROM alpine:3.7 FROM alpine:3.14
# dependencies needed for compiling c extensions # dependencies needed for compiling c extensions
# also busybox-extras for telnet for easier use of backdoor # also busybox-extras for telnet for easier use of backdoor
RUN apk --update add py2-pip gcc python-dev musl-dev postgresql-dev file make busybox-extras RUN apk --update add py3-pip gcc python-dev musl-dev postgresql-dev file make busybox-extras
# Install gevent so that we don't need to re-install it when common changes # Install gevent so that we don't need to re-install it when common changes
RUN pip install gevent RUN pip install gevent
@ -15,4 +15,4 @@ RUN apk add postgresql-dev postgresql-libs
COPY backfiller /tmp/backfiller COPY backfiller /tmp/backfiller
RUN pip install /tmp/backfiller && rm -r /tmp/backfiller RUN pip install /tmp/backfiller && rm -r /tmp/backfiller
ENTRYPOINT ["python2", "-m", "backfiller", "--base-dir", "/mnt"] ENTRYPOINT ["python3", "-m", "backfiller", "--base-dir", "/mnt"]

@ -147,7 +147,7 @@ def get_remote_segment(base_dir, node, channel, quality, hour, missing_segment,
resp.raise_for_status() resp.raise_for_status()
with open(temp_path, 'w') as f: with open(temp_path, 'wb') as f:
for chunk in resp.iter_content(8192): for chunk in resp.iter_content(8192):
f.write(chunk) f.write(chunk)
hash.update(chunk) hash.update(chunk)

@ -1,14 +1,5 @@
"""A place for common utilities between wubloader components""" """A place for common utilities between wubloader components"""
# HACK: This sets the default encoding for the entire process.
# It is possible this may break (badly-written) third party libs.
import sys
reload(sys)
sys.setdefaultencoding('utf-8')
import datetime import datetime
import errno import errno
import os import os
@ -104,15 +95,7 @@ def ensure_directory(path):
"""Create directory that contains path, as well as any parent directories, """Create directory that contains path, as well as any parent directories,
if they don't already exist.""" if they don't already exist."""
dir_path = os.path.dirname(path) dir_path = os.path.dirname(path)
if os.path.exists(dir_path): os.mkdirs(dir_path, exist_ok=True)
return
ensure_directory(dir_path)
try:
os.mkdir(dir_path)
except OSError as e:
# Ignore if EEXISTS. This is needed to avoid a race if two getters run at once.
if e.errno != errno.EEXIST:
raise
def jitter(interval): def jitter(interval):

@ -364,7 +364,7 @@ def rough_cut_segments(segments, start, end):
This method works by simply concatenating all the segments, without any re-encoding. This method works by simply concatenating all the segments, without any re-encoding.
""" """
for segment in segments: for segment in segments:
with open(segment.path) as f: with open(segment.path, 'rb') as f:
for chunk in read_chunks(f): for chunk in read_chunks(f):
yield chunk yield chunk
@ -437,7 +437,7 @@ def fast_cut_segments(segments, start, end):
) )
else: else:
# no cutting needed, just serve the file # no cutting needed, just serve the file
with open(segment.path) as f: with open(segment.path, 'rb') as f:
for chunk in read_chunks(f): for chunk in read_chunks(f):
yield chunk yield chunk

@ -1,7 +1,7 @@
FROM alpine:3.7 FROM alpine:3.14
# dependencies needed for compiling c extensions # dependencies needed for compiling c extensions
# also busybox-extras for telnet for easier use of backdoor # also busybox-extras for telnet for easier use of backdoor
RUN apk --update add py2-pip gcc python-dev musl-dev postgresql-dev file make busybox-extras RUN apk --update add py3-pip gcc python-dev musl-dev postgresql-dev file make busybox-extras
# Install gevent so that we don't need to re-install it when common changes # Install gevent so that we don't need to re-install it when common changes
RUN pip install gevent RUN pip install gevent
@ -15,4 +15,4 @@ RUN apk add postgresql-dev postgresql-client ffmpeg
COPY cutter /tmp/cutter COPY cutter /tmp/cutter
RUN pip install /tmp/cutter && rm -r /tmp/cutter RUN pip install /tmp/cutter && rm -r /tmp/cutter
ENTRYPOINT ["python2", "-m", "cutter", "--base-dir", "/mnt"] ENTRYPOINT ["python3", "-m", "cutter", "--base-dir", "/mnt"]

@ -241,13 +241,13 @@ class Local(UploadBackend):
filepath = os.path.join(self.path, filename) filepath = os.path.join(self.path, filename)
try: try:
if self.write_info: if self.write_info:
with open(os.path.join(self.path, '{}-{}.json'.format(safe_title, video_id)), 'w') as f: with open(os.path.join(self.path, '{}-{}.json'.format(safe_title, video_id)), 'wb') as f:
f.write(json.dumps({ f.write(json.dumps({
'title': title, 'title': title,
'description': description, 'description': description,
'tags': tags, 'tags': tags,
}) + '\n') }) + '\n')
with open(filepath, 'w') as f: with open(filepath, 'wb') as f:
for chunk in data: for chunk in data:
f.write(chunk) f.write(chunk)
except (OSError, IOError) as e: except (OSError, IOError) as e:

@ -1,7 +1,7 @@
FROM alpine:3.7 FROM alpine:3.14
# dependencies needed for compiling c extensions # dependencies needed for compiling c extensions
# also busybox-extras for telnet for easier use of backdoor # also busybox-extras for telnet for easier use of backdoor
RUN apk --update add py2-pip gcc python-dev musl-dev file make busybox-extras RUN apk --update add py3-pip gcc python-dev musl-dev file make busybox-extras
# Install gevent so that we don't need to re-install it when common changes # Install gevent so that we don't need to re-install it when common changes
RUN pip install gevent RUN pip install gevent
@ -14,4 +14,4 @@ RUN pip install /tmp/common && rm -r /tmp/common
COPY downloader /tmp/downloader COPY downloader /tmp/downloader
RUN pip install /tmp/downloader && rm -r /tmp/downloader RUN pip install /tmp/downloader && rm -r /tmp/downloader
ENTRYPOINT ["python2", "-m", "downloader", "--base-dir", "/mnt"] ENTRYPOINT ["python3", "-m", "downloader", "--base-dir", "/mnt"]

@ -555,7 +555,7 @@ class SegmentGetter(object):
return return
resp.raise_for_status() resp.raise_for_status()
common.ensure_directory(temp_path) common.ensure_directory(temp_path)
with open(temp_path, 'w') as f: with open(temp_path, 'wb') as f:
file_created = True file_created = True
# We read chunk-wise in 8KiB chunks. Note that if the connection cuts halfway, # We read chunk-wise in 8KiB chunks. Note that if the connection cuts halfway,
# we may lose part of the last chunk even though we did receive it. # we may lose part of the last chunk even though we did receive it.

@ -1,7 +1,7 @@
FROM alpine:3.7 FROM alpine:3.14
# dependencies needed for compiling c extensions # dependencies needed for compiling c extensions
# also busybox-extras for telnet for easier use of backdoor # also busybox-extras for telnet for easier use of backdoor
RUN apk --update add py2-pip gcc python-dev musl-dev postgresql-dev file make busybox-extras RUN apk --update add py3-pip gcc python-dev musl-dev postgresql-dev file make busybox-extras
# Install gevent so that we don't need to re-install it when common changes # Install gevent so that we don't need to re-install it when common changes
RUN pip install gevent RUN pip install gevent
@ -14,4 +14,4 @@ RUN pip install /tmp/common && rm -r /tmp/common
COPY playlist_manager /tmp/playlist_manager COPY playlist_manager /tmp/playlist_manager
RUN pip install /tmp/playlist_manager && rm -r /tmp/playlist_manager RUN pip install /tmp/playlist_manager && rm -r /tmp/playlist_manager
ENTRYPOINT ["python2", "-m", "playlist_manager"] ENTRYPOINT ["python3", "-m", "playlist_manager"]

@ -1,7 +1,7 @@
FROM alpine:3.7 FROM alpine:3.14
# dependencies needed for compiling c extensions # dependencies needed for compiling c extensions
# also busybox-extras for telnet for easier use of backdoor # also busybox-extras for telnet for easier use of backdoor
RUN apk --update add py2-pip gcc python-dev musl-dev file make busybox-extras RUN apk --update add py3-pip gcc python-dev musl-dev file make busybox-extras
# Install gevent so that we don't need to re-install it when common changes # Install gevent so that we don't need to re-install it when common changes
RUN pip install gevent RUN pip install gevent
@ -15,4 +15,4 @@ RUN apk add ffmpeg
COPY restreamer /tmp/restreamer COPY restreamer /tmp/restreamer
RUN pip install /tmp/restreamer && rm -r /tmp/restreamer RUN pip install /tmp/restreamer && rm -r /tmp/restreamer
ENTRYPOINT ["python2", "-m", "restreamer", "--base-dir", "/mnt"] ENTRYPOINT ["python3", "-m", "restreamer", "--base-dir", "/mnt"]

@ -306,9 +306,7 @@ def cut(channel, quality):
@request_stats @request_stats
@has_path_args @has_path_args
def generate_videos(channel, quality): def generate_videos(channel, quality):
"""Generate one video for each contiguous range of segments (ie. split at holes), """
and save them as CHANNEL_QUALITY_N.ts in the segments directory.
Takes a JSON body {name: [start, end]} where start and end are timestamps. Takes a JSON body {name: [start, end]} where start and end are timestamps.
Creates files CHANNEL_QUALITY_NAME_N.mkv for each contiguous range of segments Creates files CHANNEL_QUALITY_NAME_N.mkv for each contiguous range of segments
in that hour range (ie. split at holes) and saves them in the segments directory. in that hour range (ie. split at holes) and saves them in the segments directory.
@ -319,6 +317,10 @@ def generate_videos(channel, quality):
start = dateutil.parse_utc_only(start) start = dateutil.parse_utc_only(start)
end = dateutil.parse_utc_only(end) end = dateutil.parse_utc_only(end)
# protect against directory traversal
if "/" in name:
return "Name cannot contain /", 400
if end <= start: if end <= start:
return "End must be after start", 400 return "End must be after start", 400

@ -1,7 +1,7 @@
FROM alpine:3.7 FROM alpine:3.14
# dependencies needed for compiling c extensions # dependencies needed for compiling c extensions
# also busybox-extras for telnet for easier use of backdoor # also busybox-extras for telnet for easier use of backdoor
RUN apk --update add py2-pip gcc python-dev musl-dev file make busybox-extras RUN apk --update add py3-pip gcc python-dev musl-dev file make busybox-extras
# Install gevent so that we don't need to re-install it when common changes # Install gevent so that we don't need to re-install it when common changes
RUN pip install gevent RUN pip install gevent
@ -21,4 +21,4 @@ RUN ln -s /usr/include/locale.h /usr/include/xlocale.h \
COPY segment_coverage /tmp/segment_coverage COPY segment_coverage /tmp/segment_coverage
RUN pip install /tmp/segment_coverage && rm -r /tmp/segment_coverage RUN pip install /tmp/segment_coverage && rm -r /tmp/segment_coverage
ENTRYPOINT ["python2", "-m", "segment_coverage"] ENTRYPOINT ["python3", "-m", "segment_coverage"]

@ -231,7 +231,7 @@ class CoverageChecker(object):
temp_path = '{}_{}.html'.format(path_prefix, uuid.uuid4()) temp_path = '{}_{}.html'.format(path_prefix, uuid.uuid4())
final_path = '{}_coverage.html'.format(path_prefix) final_path = '{}_coverage.html'.format(path_prefix)
common.ensure_directory(temp_path) common.ensure_directory(temp_path)
with open(temp_path, 'w') as f: with open(temp_path, 'wb') as f:
f.write(html) f.write(html)
os.rename(temp_path, final_path) os.rename(temp_path, final_path)
self.logger.info('Coverage page for {} created'.format(quality)) self.logger.info('Coverage page for {} created'.format(quality))

@ -1,7 +1,7 @@
FROM alpine:3.7 FROM alpine:3.14
# dependencies needed for compiling c extensions # dependencies needed for compiling c extensions
# also busybox-extras for telnet for easier use of backdoor # also busybox-extras for telnet for easier use of backdoor
RUN apk --update add py2-pip gcc python-dev musl-dev postgresql-dev file make busybox-extras RUN apk --update add py3-pip gcc python-dev musl-dev postgresql-dev file make busybox-extras
# Install gevent so that we don't need to re-install it when common changes # Install gevent so that we don't need to re-install it when common changes
RUN pip install gevent RUN pip install gevent
@ -15,4 +15,4 @@ RUN apk add postgresql-dev postgresql-client
COPY sheetsync /tmp/sheetsync COPY sheetsync /tmp/sheetsync
RUN pip install /tmp/sheetsync && rm -r /tmp/sheetsync RUN pip install /tmp/sheetsync && rm -r /tmp/sheetsync
ENTRYPOINT ["python2", "-m", "sheetsync"] ENTRYPOINT ["python3", "-m", "sheetsync"]

@ -1,7 +1,7 @@
FROM alpine:3.7 FROM alpine:3.14
# dependencies needed for compiling c extensions # dependencies needed for compiling c extensions
# also busybox-extras for telnet for easier use of backdoor # also busybox-extras for telnet for easier use of backdoor
RUN apk --update add py2-pip gcc python-dev musl-dev postgresql-dev file make busybox-extras RUN apk --update add py3-pip gcc python-dev musl-dev postgresql-dev file make busybox-extras
# Install gevent so that we don't need to re-install it when common changes # Install gevent so that we don't need to re-install it when common changes
RUN pip install gevent RUN pip install gevent
@ -15,4 +15,4 @@ RUN apk add postgresql-dev postgresql-libs
COPY thrimshim /tmp/thrimshim COPY thrimshim /tmp/thrimshim
RUN pip install /tmp/thrimshim && rm -r /tmp/thrimshim RUN pip install /tmp/thrimshim && rm -r /tmp/thrimshim
ENTRYPOINT ["python2", "-m", "thrimshim"] ENTRYPOINT ["python3", "-m", "thrimshim"]

Loading…
Cancel
Save