Share Pastebin
Guest
Public paste!

Untitled

By: a guest | May 4th, 2010 | Syntax: Python | Size: 7.04 KB | Hits: 91 | Expires: Never
This paste has a previous version, view the difference. Copy text to clipboard
  1. #!/usr/bin/env python
  2.  
  3.  
  4. # NOTE: this script requires MythTV 0.23+fixes r24420 or newer.
  5. # For older versions, you can try applying this patch to the python bindings:
  6. #   http://svn.mythtv.org/trac/changeset/24420
  7.  
  8.  
  9. # schema version that this code expects
  10. schema_version = 1000
  11.  
  12.  
  13. # default number of timeout days
  14. timeout = 30
  15.  
  16.  
  17. # if any of these are not None, the given value(s) will be used to create a
  18. # database connection
  19. dbhost = None
  20. dbname = None
  21. dbuser = None
  22. dbpass = None
  23.  
  24.  
  25. # includes
  26. from sys import exit
  27. from optparse import OptionParser
  28. from MythTV import OldRecorded
  29. from MythTV import MythDB, MythLog
  30.  
  31.  
  32. # utility funciton: connect and return a MythDB object
  33. def __db_connect(schemacheck=True):
  34.  
  35.   # connect
  36.   args = []
  37.   if dbhost: args.append( ('DBHostName', dbhost) )
  38.   if dbname: args.append( ('DBName', dbname) )
  39.   if dbuser: args.append( ('DBUserName', dbuser) )
  40.   if dbpass: args.append( ('DBPassword', dbpass) )
  41.   db = MythDB(args=args)
  42.   log = MythLog(module='RerecordLater', lstr='important')
  43.  
  44.   # schema version check
  45.   if schemacheck:
  46.  
  47.     if db.settings.NULL.RerecordLaterDBSchemaVer == None:
  48.  
  49.       # first time run, auto install
  50.       log(
  51.         MythLog.IMPORTANT,
  52.         'RerecordLater database does not exist, creating it.'
  53.       )
  54.       action_install(db)
  55.  
  56.     else:
  57.  
  58.       dbversion = int(db.settings.NULL.RerecordLaterDBSchemaVer)
  59.  
  60.       # db schema too new ?
  61.       if dbversion > schema_version:
  62.         log(
  63.           MythLog.IMPORTANT,
  64.           'The schema version in the database (%d) is newer than the RerecordLater program (%d). Please update to the appropriate version of RerecordLater.' %
  65.           ( dbversion, schema_version )
  66.         )
  67.         exit(3)
  68.  
  69.       # db schema too old?
  70.       elif dbversion < schema_version:
  71.         log(
  72.           MythLog.IMPORTANT,
  73.           'The schema version in the database (%d) is older than the RerecordLater program (%d). Updating it.' %
  74.           ( dbversion, schema_version )
  75.         )
  76.         action_database_upgrade(db)
  77.  
  78.       # correct db schema version?
  79.       else:
  80.         pass
  81.  
  82.   return db
  83.  
  84.  
  85. # --rerecord handler
  86. def action_rerecord(chanid, starttime):
  87.  
  88.   db = __db_connect()
  89.  
  90.   # search for old recordings with chanid/starttime
  91.   q = db.searchOldRecorded(chanid=chanid, starttime=starttime)
  92.   if q:
  93.     for oldrec in q:
  94.       if not oldrec.duplicate:
  95.         # This recording was set to re-record. this is where we take action.
  96.         # It is told *not* to rerecord now and will be flipped back on after
  97.         # the timeout period.
  98.         db.cursor().execute("""
  99.          INSERT INTO rerecordlater
  100.          SET
  101.            chanid = %d,
  102.             starttime = "%s",
  103.             timeout = NOW() + INTERVAL %d DAY
  104.        """ % ( chanid, starttime, timeout ) )
  105.         oldrec.setDuplicate(True)
  106.  
  107.  
  108. # --reschedule handler
  109. def action_reschedule():
  110.  
  111.   # connect
  112.   db = __db_connect()
  113.   c = db.cursor()
  114.  
  115.   # get rerecordlater records where the timeout has expired
  116.   c.execute("""
  117.    SELECT chanid, starttime
  118.    FROM rerecordlater
  119.    WHERE timeout <= NOW()
  120.  """)
  121.   for rec in c.fetchall():
  122.     # find the oldrecorded record and re-allow dupes.
  123.     q = db.searchOldRecorded(chanid=rec[0], starttime=rec[1])
  124.     for oldrec in q: oldrec.setDuplicate(False)
  125.     c.execute("""
  126.      DELETE FROM rerecordlater
  127.      WHERE chanid = %d AND starttime = "%s"
  128.    """ % ( rec[0], rec[1] ) )
  129.  
  130.  
  131. # --legacy handler
  132. def action_legacy():
  133.   # Find recordings that were set to duplicate
  134.   q = db.searchOldRecorded(duplicate=0)
  135.   for oldrec in q: action_rerecord(oldrec.chanid, oldrec.starttime)
  136.  
  137.  
  138. # --install handler
  139. def action_install(db=None):
  140.   if not db: db = __db_connect(schemacheck=False)
  141.   db.cursor().execute("""
  142.    CREATE TABLE IF NOT EXISTS rerecordlater(
  143.      chanid INT(10) UNSIGNED,
  144.      starttime datetime,
  145.      timeout datetime
  146.    )
  147.  """)
  148.   db.settings.NULL.RerecordLaterDBSchemaVer = schema_version
  149.  
  150.  
  151. # --database-upgrade handler
  152. def action_database_upgrade(db=None):
  153.   if not db: db = __db_connect()
  154.   # TODO: update system when needed. The idea is to iterate
  155.   # from db_schema_version+1 through the current version and
  156.   # execute a function called database_upgrade_[n] where [n]
  157.   # is a particular schema version. Each of these functions
  158.   # should have the ability to update the database from the
  159.   # previous version, so executing them in a row will upgrade
  160.   # from whatever the current db version is to current schema.
  161.  
  162.  
  163. # main
  164. if __name__ == '__main__':
  165.  
  166.   parser = OptionParser()
  167.   parser.add_option(
  168.     '-a', '--legacy', dest='legacy', action='store_true', default=False,
  169.     help='Add programs that are marked re-recordable and are not already handled by this script. Use this to seed the database at installation time.'
  170.   )
  171.   parser.add_option(
  172.     '-r', '--reschedule', dest='reschedule', action='store_true', default=False,
  173.     help='Mark programs where the timeout has expired as OK to re-record. Run daily. Recommended: put --reschedule on the "mythfilldatabase ran" system event.'
  174.   )
  175.   parser.add_option(
  176.     '-i', '--install', dest='install', action='store_true', default=False,
  177.     help='Initialize the database. If it already exists, does nothing. Happens automatically if the program is run without an existing database.'
  178.   )
  179.   parser.add_option(
  180.     '-g', '--database-upgrade', dest='upgrade', action='store_true', default=False,
  181.     help='upgrade the database to the current schema version (NOTE: currently not implemented)'
  182.   )
  183.   parser.add_option(
  184.     '-t', '--timeout', dest='timeout', type='int', metavar='LEVEL',
  185.     help='number of days before marked programs will time out'
  186.   )
  187.   parser.add_option(
  188.     '-o', '--host', dest='host', metavar='HOST',
  189.     help='database host'
  190.   )
  191.   parser.add_option(
  192.     '-d', '--database', dest='database', metavar='NAME',
  193.     help='database name'
  194.   )
  195.   parser.add_option(
  196.     '-u', '--user', dest='user', metavar='USER',
  197.     help='database user'
  198.   )
  199.   parser.add_option(
  200.     '-p', '--password', dest='password', metavar='PASSWORD',
  201.     help='database password'
  202.   )
  203.   parser.add_option(
  204.     '-l', '--log-level', dest='loglevel', metavar='LEVEL',
  205.     help='set the log level'
  206.   )
  207.   (options, args) = parser.parse_args()
  208.  
  209.   # run configuration
  210.   if options.timeout: timeout = options.timeout
  211.   if options.host: dbhost = options.host
  212.   if options.database: dbname = options.database
  213.   if options.user: dbuser = options.user
  214.   if options.password: dbpass = options.password
  215.   if options.loglevel: MythLog._setlevel(options.loglevel)
  216.  
  217.   # actions
  218.   if options.legacy: action_legacy()
  219.   if options.reschedule: action_reschedule()
  220.   if options.install: action_install()
  221.   if options.upgrade: action_database_upgrade()
  222.  
  223.   if len(args) % 2 != 0:
  224.     usage(
  225.       'There must be an even number of positional arguments. Specify one or more sets of {chanid starttime} after other options are specified.'
  226.     )
  227.  
  228.   for i in range(0, len(args), 2):
  229.     chanid = int(args[i])
  230.     starttime = args[i+1]
  231.     action_rerecord(chanid, starttime)