2013-12-23 19:58:44 +01:00
|
|
|
import os
|
|
|
|
import sys
|
|
|
|
from argparse import ArgumentParser, Action
|
|
|
|
from datetime import datetime
|
|
|
|
import logging
|
2014-04-15 01:20:24 +02:00
|
|
|
logging.basicConfig(stream=sys.stdout)
|
2013-12-23 19:58:44 +01:00
|
|
|
logger = logging.getLogger('cli')
|
2014-04-15 01:20:24 +02:00
|
|
|
|
|
|
|
|
2013-12-27 00:56:46 +01:00
|
|
|
CWD = os.getcwd()
|
2013-12-23 19:58:44 +01:00
|
|
|
|
|
|
|
import forge
|
2014-02-18 19:09:10 +01:00
|
|
|
import maint
|
2013-12-23 19:58:44 +01:00
|
|
|
from config_manager import get_config
|
|
|
|
import server
|
|
|
|
|
2013-12-27 00:56:46 +01:00
|
|
|
|
2013-12-23 19:58:44 +01:00
|
|
|
def pre_check_permissions():
|
|
|
|
|
|
|
|
def is_writable(d):
|
|
|
|
return os.access(d, os.W_OK)
|
|
|
|
|
|
|
|
if is_writable(get_config()['AUDIO_INPUT']):
|
|
|
|
yield "Audio input '%s' writable" % get_config()['AUDIO_INPUT']
|
|
|
|
if not os.access(get_config()['AUDIO_INPUT'], os.R_OK):
|
|
|
|
yield "Audio input '%s' unreadable" % get_config()['AUDIO_INPUT']
|
|
|
|
sys.exit(10)
|
|
|
|
if is_writable(os.getcwd()):
|
|
|
|
yield "Code writable"
|
|
|
|
if not is_writable(get_config()['AUDIO_OUTPUT']):
|
|
|
|
yield "Audio output '%s' not writable" % get_config()['AUDIO_OUTPUT']
|
2014-02-18 19:09:10 +01:00
|
|
|
logger.critical("Aborting")
|
2013-12-23 19:58:44 +01:00
|
|
|
sys.exit(10)
|
|
|
|
|
2013-12-27 00:56:46 +01:00
|
|
|
|
2013-12-23 19:58:44 +01:00
|
|
|
def pre_check_user():
|
|
|
|
if os.geteuid() == 0:
|
|
|
|
yield "You're running as root; this is dangerous"
|
|
|
|
|
2014-04-14 18:51:04 +02:00
|
|
|
def pre_check_ffmpeg():
|
|
|
|
path = get_config()['FFMPEG_PATH']
|
|
|
|
if not path.startswith('/'):
|
|
|
|
yield "FFMPEG_PATH is not absolute: %s" % path
|
|
|
|
from subprocess import check_output
|
|
|
|
try:
|
|
|
|
check_output([path, '-version'])
|
|
|
|
except OSError:
|
|
|
|
yield "FFMPEG not found as " + path
|
|
|
|
else:
|
|
|
|
if not os.path.exists(path):
|
|
|
|
yield "FFMPEG not found in " + path
|
|
|
|
|
2013-12-23 19:58:44 +01:00
|
|
|
|
|
|
|
class DateTimeAction(Action):
|
|
|
|
def __call__(self, parser, namespace, values, option_string=None):
|
2014-02-27 11:59:10 +01:00
|
|
|
if len(values) == 15 or len(values) == 13:
|
2013-12-23 19:58:44 +01:00
|
|
|
parsed_val = datetime.strptime(values, '%Y%m%d-%H%M%S')
|
|
|
|
else:
|
|
|
|
raise ValueError("'%s' is not a valid datetime" % values)
|
|
|
|
setattr(namespace, self.dest, parsed_val)
|
|
|
|
|
2013-12-27 00:56:46 +01:00
|
|
|
|
2013-12-23 20:08:15 +01:00
|
|
|
def common_pre():
|
2014-04-14 18:51:04 +02:00
|
|
|
prechecks = [pre_check_user, pre_check_permissions, pre_check_ffmpeg]
|
2013-12-23 19:58:44 +01:00
|
|
|
configs = ['default_config.py']
|
|
|
|
if 'TECHREC_CONFIG' in os.environ:
|
|
|
|
for conf in os.environ['TECHREC_CONFIG'].split(':'):
|
|
|
|
if not conf:
|
|
|
|
continue
|
|
|
|
path = os.path.realpath(conf)
|
|
|
|
if not os.path.exists(path):
|
|
|
|
logger.warn("Configuration file '%s' does not exist; skipping"
|
|
|
|
% path)
|
|
|
|
continue
|
|
|
|
configs.append(path)
|
|
|
|
os.chdir(os.path.dirname(os.path.realpath(__file__)))
|
|
|
|
for conf in configs:
|
|
|
|
get_config().from_pyfile(conf)
|
|
|
|
|
|
|
|
for check in prechecks:
|
|
|
|
for warn in check():
|
2014-04-15 01:20:24 +02:00
|
|
|
logger.warn(warn)
|
2013-12-23 19:58:44 +01:00
|
|
|
|
2013-12-23 20:08:15 +01:00
|
|
|
if __name__ == "__main__":
|
2013-12-27 00:56:46 +01:00
|
|
|
parser = ArgumentParser(description='creates mp3 from live recordings')
|
|
|
|
parser.add_argument('--verbose', '-v', action='count',
|
|
|
|
help='Increase verbosity; can be used multiple times')
|
2014-02-18 19:09:10 +01:00
|
|
|
parser.add_argument('--pretend', '-p', action='store_true', default=False,
|
|
|
|
help='Only pretend; no real action will be done')
|
|
|
|
sub = parser.add_subparsers(title='main subcommands',
|
|
|
|
description='valid subcommands')
|
|
|
|
serve_p = sub.add_parser('serve', help="Start an HTTP server")
|
2013-12-23 19:58:44 +01:00
|
|
|
serve_p.set_defaults(func=server.main_cmd)
|
2014-02-18 19:09:10 +01:00
|
|
|
forge_p = sub.add_parser('forge', help="Create an audio file")
|
2014-02-27 11:59:10 +01:00
|
|
|
forge_p.add_argument('starttime', metavar='START',
|
|
|
|
help='Start time, espressed as 19450425_1200 (%%Y%%m%%d-%%H%%M%%S)',
|
|
|
|
action=DateTimeAction)
|
|
|
|
forge_p.add_argument('endtime', metavar='END',
|
|
|
|
help='End time, espressed as 19450425_1200 (%%Y%%m%%d-%%H%%M%%S)',
|
|
|
|
action=DateTimeAction)
|
2013-12-23 19:58:44 +01:00
|
|
|
forge_p.add_argument('-o', metavar='OUTFILE', dest='outfile',
|
|
|
|
default='out.mp3', help='Path of the output mp3')
|
|
|
|
forge_p.set_defaults(func=forge.main_cmd)
|
|
|
|
|
2014-02-18 19:09:10 +01:00
|
|
|
cleanold_p = sub.add_parser('cleanold', help="Remove old files from DB",
|
|
|
|
description="Will remove oldfiles with no filename from DB")
|
|
|
|
cleanold_p.add_argument('-t', metavar='MINAGE', dest='minage',
|
|
|
|
default='14', type=int,
|
|
|
|
help='Minimum age (in days) for removal')
|
|
|
|
cleanold_p.set_defaults(func=maint.cleanold_cmd)
|
|
|
|
|
|
|
|
|
2013-12-23 19:58:44 +01:00
|
|
|
options = parser.parse_args()
|
2013-12-27 00:56:46 +01:00
|
|
|
options.cwd = CWD
|
|
|
|
if options.verbose < 1:
|
|
|
|
logging.basicConfig(level=logging.WARNING)
|
|
|
|
elif options.verbose == 1:
|
|
|
|
logging.basicConfig(level=logging.INFO)
|
|
|
|
elif options.verbose >= 2:
|
|
|
|
logging.basicConfig(level=logging.DEBUG)
|
2014-02-18 19:09:10 +01:00
|
|
|
if options.verbose > 2:
|
|
|
|
logging.info("giving verbose flag >2 times is useless")
|
2013-12-27 00:56:46 +01:00
|
|
|
common_pre()
|
2013-12-23 19:58:44 +01:00
|
|
|
options.func(options)
|