Skip to content

Commit

Permalink
More error tolerant cleaning + remove unused flags
Browse files Browse the repository at this point in the history
  • Loading branch information
allejok96 committed Sep 8, 2017
1 parent 1b74b46 commit 97e9c76
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 18 deletions.
12 changes: 9 additions & 3 deletions jwb-index
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,15 @@ parser = argparse.ArgumentParser(prog='jwb-index',
usage='%(prog)s [options] [DIR]',
description='Index or download media from tv.jw.org')

# The commented out options would only be valid with a --config file
add_arguments(parser, ['--quiet',
'--mode',
'--lang',
'--quality',
'--subtitles',
'--no-subtitles',
#'--no-subtitles',
'--download',
'--checksum',
#'--checksum',
'--no-checksum',
'--no-warning',
'--free',
Expand Down Expand Up @@ -51,12 +52,17 @@ parser.add_argument('--curl-path',
default='curl',
metavar='PATH',
help='path to the curl binary')
parser.add_argument('--clean-symlinks',
dest='clean',
action='store_true',
help='remove all old symlinks (only valid with --mode=filesystem)')

jwb = JWBroadcasting()
# Default values, not set by JWBroadcasting
jwb.work_dir = '.'
jwb.mode = None
jwb.warn = False
jwb.clean = False
jwb.exclude_category = 'VODSJJMeetings'
parser.parse_args(namespace=jwb)

Expand Down Expand Up @@ -87,7 +93,7 @@ elif mode == 'm3u':
elif mode == 'm3ucompat':
jo.output_m3u(r, wd, subdir, flat=True)
elif mode == 'filesystem':
jo.clean_symlinks(os.path.join(wd, subdir), quiet=jwb.quiet)
jo.clean_symlinks(os.path.join(wd, subdir), quiet=jwb.quiet, clean_all=jwb.clean)
jo.output_filesystem(r, wd, subdir)
elif mode == 'html':
jo.output_html(r, wd, subdir)
3 changes: 2 additions & 1 deletion jwb-stream
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ parser = argparse.ArgumentParser(prog='jwb-stream',
add_arguments(parser, ['--lang',
'--quality',
'--subtitles',
'--no-subtitles'])
#'--no-subtitles'
])
# TODO
# parser.add_argument('--config')
parser.add_argument('--channel',
Expand Down
9 changes: 6 additions & 3 deletions jwlib/arguments.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,16 @@
'help': 'prefer subtitled videos'},
'--no-subtitles': {
'action': 'store_false',
'dest': 'subtitles'},
'dest': 'subtitles',
'help': 'prefer un-subtitled videos'},
'--checksum': {
'action': 'store_true',
'dest': 'checksums',
'help': 'check md5 checksum'},
'--no-checksum': {
'action': 'store_false'},
'action': 'store_false',
'dest': 'checksums',
'help': 'don\'t check md5 checksum'},
'--free': {
'default': 0,
'type': int,
Expand Down Expand Up @@ -88,7 +91,7 @@ def disk_usage_info(wd, keep_free: int, warn=True, quiet=0):
makedirs(wd,exist_ok=True)
free = disk_usage(wd).free

if quiet == 0:
if quiet < 1:
print('free space: {:} MiB, minimum limit: {:} MiB'.format(free//1024**2, keep_free//1024**2), file=stderr)

if warn and free < keep_free:
Expand Down
23 changes: 12 additions & 11 deletions jwlib/output.py
Original file line number Diff line number Diff line change
Expand Up @@ -169,23 +169,24 @@ def clean_symlinks(d, clean_all=False, quiet=0):
:param d: Path to directory to clean
:param clean_all: Remove non-broken symlinks too
:param quiet: Log level (int)
"""
try:
for subdir in os.listdir(d):
if not os.path.isdir(d):
return

for subdir in os.listdir(d):
subdir = pj(d, subdir)
if not os.path.isdir(subdir):
continue

for file in os.listdir(subdir):
file = pj(subdir, file)
# If file is not a symlink it will raise OSError
try:
source = pj(subdir, os.readlink(file))
except OSError:
if not os.path.islink(file):
continue

# Remove broken links
source = pj(subdir, os.readlink(file))

if clean_all or not os.path.exists(source):
if quiet < 2:
print('removing broken link: ' + os.path.basename(file), file=stderr)
print('removing link: ' + os.path.basename(file), file=stderr)
os.remove(file)

except (NotADirectoryError, FileNotFoundError):
pass

0 comments on commit 97e9c76

Please sign in to comment.