Add "cleanold" subcommand closes #11
This commit is contained in:
parent
73a47f4f2b
commit
062f547b58
4 changed files with 61 additions and 15 deletions
|
@ -7,6 +7,7 @@ logger = logging.getLogger('cli')
|
||||||
CWD = os.getcwd()
|
CWD = os.getcwd()
|
||||||
|
|
||||||
import forge
|
import forge
|
||||||
|
import maint
|
||||||
from config_manager import get_config
|
from config_manager import get_config
|
||||||
import server
|
import server
|
||||||
|
|
||||||
|
@ -25,6 +26,7 @@ def pre_check_permissions():
|
||||||
yield "Code writable"
|
yield "Code writable"
|
||||||
if not is_writable(get_config()['AUDIO_OUTPUT']):
|
if not is_writable(get_config()['AUDIO_OUTPUT']):
|
||||||
yield "Audio output '%s' not writable" % get_config()['AUDIO_OUTPUT']
|
yield "Audio output '%s' not writable" % get_config()['AUDIO_OUTPUT']
|
||||||
|
logger.critical("Aborting")
|
||||||
sys.exit(10)
|
sys.exit(10)
|
||||||
|
|
||||||
|
|
||||||
|
@ -69,18 +71,27 @@ if __name__ == "__main__":
|
||||||
parser = ArgumentParser(description='creates mp3 from live recordings')
|
parser = ArgumentParser(description='creates mp3 from live recordings')
|
||||||
parser.add_argument('--verbose', '-v', action='count',
|
parser.add_argument('--verbose', '-v', action='count',
|
||||||
help='Increase verbosity; can be used multiple times')
|
help='Increase verbosity; can be used multiple times')
|
||||||
sub = parser.add_subparsers(title='subcommands',
|
parser.add_argument('--pretend', '-p', action='store_true', default=False,
|
||||||
description='valid subcommands',
|
help='Only pretend; no real action will be done')
|
||||||
help='additional help')
|
sub = parser.add_subparsers(title='main subcommands',
|
||||||
serve_p = sub.add_parser('serve')
|
description='valid subcommands')
|
||||||
|
serve_p = sub.add_parser('serve', help="Start an HTTP server")
|
||||||
serve_p.set_defaults(func=server.main_cmd)
|
serve_p.set_defaults(func=server.main_cmd)
|
||||||
forge_p = sub.add_parser('forge')
|
forge_p = sub.add_parser('forge', help="Create an audio file")
|
||||||
forge_p.add_argument('starttime', metavar='FROM', action=DateTimeAction)
|
forge_p.add_argument('starttime', metavar='FROM', action=DateTimeAction)
|
||||||
forge_p.add_argument('endtime', metavar='TO', action=DateTimeAction)
|
forge_p.add_argument('endtime', metavar='TO', action=DateTimeAction)
|
||||||
forge_p.add_argument('-o', metavar='OUTFILE', dest='outfile',
|
forge_p.add_argument('-o', metavar='OUTFILE', dest='outfile',
|
||||||
default='out.mp3', help='Path of the output mp3')
|
default='out.mp3', help='Path of the output mp3')
|
||||||
forge_p.set_defaults(func=forge.main_cmd)
|
forge_p.set_defaults(func=forge.main_cmd)
|
||||||
|
|
||||||
|
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)
|
||||||
|
|
||||||
|
|
||||||
options = parser.parse_args()
|
options = parser.parse_args()
|
||||||
options.cwd = CWD
|
options.cwd = CWD
|
||||||
if options.verbose < 1:
|
if options.verbose < 1:
|
||||||
|
@ -89,5 +100,7 @@ if __name__ == "__main__":
|
||||||
logging.basicConfig(level=logging.INFO)
|
logging.basicConfig(level=logging.INFO)
|
||||||
elif options.verbose >= 2:
|
elif options.verbose >= 2:
|
||||||
logging.basicConfig(level=logging.DEBUG)
|
logging.basicConfig(level=logging.DEBUG)
|
||||||
|
if options.verbose > 2:
|
||||||
|
logging.info("giving verbose flag >2 times is useless")
|
||||||
common_pre()
|
common_pre()
|
||||||
options.func(options)
|
options.func(options)
|
||||||
|
|
22
server/maint.py
Normal file
22
server/maint.py
Normal file
|
@ -0,0 +1,22 @@
|
||||||
|
import sys
|
||||||
|
import logging
|
||||||
|
|
||||||
|
from config_manager import get_config
|
||||||
|
from techrec import RecDB
|
||||||
|
|
||||||
|
def cleanold_cmd(options):
|
||||||
|
log = logging.getLogger('cleanold')
|
||||||
|
log.debug("starting cleanold[%d]" % options.minage)
|
||||||
|
db = RecDB(get_config()['DB_URI'])
|
||||||
|
res = db.get_not_completed(options.minage*3600*24)
|
||||||
|
count = len(res)
|
||||||
|
if options.pretend:
|
||||||
|
for rec in res:
|
||||||
|
print rec
|
||||||
|
else:
|
||||||
|
for rec in res:
|
||||||
|
logging.info("Deleting " + str(rec))
|
||||||
|
db.session.delete(rec)
|
||||||
|
db.commit()
|
||||||
|
logging.info("Cleanold complete: %d deleted" % count)
|
||||||
|
sys.exit(0)
|
|
@ -148,7 +148,9 @@ class RecAPI(Bottle):
|
||||||
get_config()['FORGE_MAX_DURATION']:
|
get_config()['FORGE_MAX_DURATION']:
|
||||||
response.status = 400
|
response.status = 400
|
||||||
return {'status': 'error',
|
return {'status': 'error',
|
||||||
'message': 'The requested recording is too long'
|
'message': 'The requested recording is too long' +
|
||||||
|
' (%d seconds)' %
|
||||||
|
(rec.endtime - rec.starttime).total_seconds()
|
||||||
}
|
}
|
||||||
rec.filename = 'ror-%s-%s.mp3' % \
|
rec.filename = 'ror-%s-%s.mp3' % \
|
||||||
(rec.starttime.strftime('%y%m%d_%H%M'),
|
(rec.starttime.strftime('%y%m%d_%H%M'),
|
||||||
|
|
|
@ -1,14 +1,12 @@
|
||||||
import logging
|
import logging
|
||||||
|
from datetime import datetime, timedelta
|
||||||
|
|
||||||
|
|
||||||
import sys
|
import sys
|
||||||
|
|
||||||
try:
|
from sqlalchemy import create_engine, Column, Integer, String, DateTime
|
||||||
from sqlalchemy import create_engine, Column, Integer, String, DateTime
|
from sqlalchemy.orm import sessionmaker
|
||||||
from sqlalchemy.orm import sessionmaker
|
from sqlalchemy.ext.declarative import declarative_base
|
||||||
from sqlalchemy.ext.declarative import declarative_base
|
|
||||||
except:
|
|
||||||
sys.exit("No SQLAlchemy.")
|
|
||||||
|
|
||||||
|
|
||||||
PAGESIZE = 10
|
PAGESIZE = 10
|
||||||
|
@ -110,7 +108,7 @@ class RecDB:
|
||||||
return False
|
return False
|
||||||
|
|
||||||
if len(_rlist) > 1:
|
if len(_rlist) > 1:
|
||||||
self.log.warning("DB: Delete: multilpe records found!")
|
self.log.warning("DB: Delete: multiple records found!")
|
||||||
self.err = "multiple ID Found %s" % (_rlist)
|
self.err = "multiple ID Found %s" % (_rlist)
|
||||||
return False
|
return False
|
||||||
|
|
||||||
|
@ -130,11 +128,24 @@ class RecDB:
|
||||||
query = self._query_page(self._query_ongoing(), page, page_size)
|
query = self._query_page(self._query_ongoing(), page, page_size)
|
||||||
return query.all()
|
return query.all()
|
||||||
|
|
||||||
|
def get_not_completed(self, minseconds=36000):
|
||||||
|
query = self._query_ongoing()
|
||||||
|
query = self._query_older(timedelta(seconds=minseconds), query)
|
||||||
|
return query.all()
|
||||||
|
|
||||||
def _query_ongoing(self, query=None):
|
def _query_ongoing(self, query=None):
|
||||||
if query is None:
|
if query is None:
|
||||||
query = self.session.query(Rec)
|
query = self.session.query(Rec)
|
||||||
return query.filter(Rec.filename == None)
|
return query.filter(Rec.filename == None)
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
def _query_older(self, delta, query=None):
|
||||||
|
if query is None:
|
||||||
|
query = self.session.query(Rec)
|
||||||
|
return query.filter(Rec.endtime < datetime.now() - delta)
|
||||||
|
|
||||||
|
|
||||||
def _query_page(self, query, page=0, page_size=PAGESIZE):
|
def _query_page(self, query, page=0, page_size=PAGESIZE):
|
||||||
if page_size:
|
if page_size:
|
||||||
page_size = int(page_size)
|
page_size = int(page_size)
|
||||||
|
@ -179,8 +190,6 @@ class RecDB:
|
||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
from datetime import datetime
|
|
||||||
|
|
||||||
def printall(queryres):
|
def printall(queryres):
|
||||||
for record in queryres:
|
for record in queryres:
|
||||||
print "Record: %s" % record
|
print "Record: %s" % record
|
||||||
|
|
Loading…
Reference in a new issue