mirror of
https://github.com/lrnselfreliance/wrolpi.git
synced 2026-10-26 19:01:39 -04:00
Creating main.py argument which checks if the media directory is correct.
This commit is contained in:
parent
4a7f33b625
commit
f77ff75362
2 changed files with 31 additions and 3 deletions
15
main.py
15
main.py
|
|
@ -47,6 +47,9 @@ async def main(loop):
|
|||
parser = argparse.ArgumentParser()
|
||||
parser.add_argument('-v', '--verbose', action='count')
|
||||
parser.add_argument('--version', action='store_true', default=False)
|
||||
parser.add_argument('-c', '--check-media', action='store_true', default=False,
|
||||
help='Check that the media directory is mounted and has the correct permissions.'
|
||||
)
|
||||
|
||||
sub_commands = parser.add_subparsers(title='sub-commands', dest='sub_commands')
|
||||
|
||||
|
|
@ -65,6 +68,14 @@ async def main(loop):
|
|||
print(get_version_string())
|
||||
return 0
|
||||
|
||||
if args.check_media:
|
||||
# Run the media directory check. Exit with informative return code.
|
||||
result = check_media_directory()
|
||||
if result is False:
|
||||
return 1
|
||||
print('Media directory is correct.')
|
||||
return 0
|
||||
|
||||
logger.warning(f'Starting with: {sys.argv}')
|
||||
await set_log_level(args)
|
||||
logger.debug(get_version_string())
|
||||
|
|
@ -144,5 +155,5 @@ def periodic_downloads(app: Sanic, loop):
|
|||
|
||||
if __name__ == '__main__':
|
||||
loop_ = asyncio.get_event_loop()
|
||||
result = loop_.run_until_complete(main(loop_))
|
||||
sys.exit(result)
|
||||
result_ = loop_.run_until_complete(main(loop_))
|
||||
sys.exit(result_)
|
||||
|
|
|
|||
|
|
@ -406,14 +406,28 @@ def get_media_directory() -> Path:
|
|||
return MEDIA_DIRECTORY
|
||||
|
||||
|
||||
MEDIA_DIRECTORY_PERMISSIONS = 0o40755
|
||||
|
||||
|
||||
def check_media_directory():
|
||||
"""
|
||||
Check that the media directory exists and is readable/writeable. Log an error if the directory
|
||||
Check that the media directory exists and has the correct permissions. Log an error if the directory
|
||||
is unusable.
|
||||
"""
|
||||
result = True
|
||||
media_directory = get_media_directory()
|
||||
if not media_directory.is_dir():
|
||||
logger.error(f'Media directory does not exist: {media_directory}')
|
||||
result = False
|
||||
|
||||
permissions = media_directory.stat().st_mode
|
||||
if permissions != MEDIA_DIRECTORY_PERMISSIONS:
|
||||
logger.error(f'Media directory has the wrong permissions: {permissions}')
|
||||
result = False
|
||||
|
||||
if not media_directory.is_mount():
|
||||
logger.warning('Media directory is not a mount.')
|
||||
|
||||
try:
|
||||
# Write a file into the media directory, log an error if this is not possible. This file should be cleaned up
|
||||
# by tempfile.
|
||||
|
|
@ -421,6 +435,9 @@ def check_media_directory():
|
|||
tf.write(b'test')
|
||||
except Exception as e:
|
||||
logger.error(f'Could not write to media directory: {media_directory}', exc_info=e)
|
||||
result = False
|
||||
|
||||
return result
|
||||
|
||||
|
||||
def get_absolute_media_path(path: str) -> Path:
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue