Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/python3
- ''' See if the HD Homerun box is accessable and running '''
- import subprocess
- import sys
- import logging
- from time import sleep
- from datetime import datetime
- ATTEMPTS = 21
- DELAY = 2
- def get_elapsed_time(start):
- ''' Calculate the time spent waiting for the HDHR to come up '''
- delta = datetime.utcnow() - start
- rounded_delta = '{:.3f}'.format(delta.seconds +
- (delta.microseconds / 1000000))
- return rounded_delta
- def main():
- ''' Try to discover the HDHR '''
- attempt = 0 # Shut up pylint.
- command = 'hdhomerun_config discover'
- logger = logging.getLogger(command)
- logging.basicConfig(filename='/var/log/mythtv/hdhr_discovery.log',
- filemode='a',
- format='%(asctime)s %(levelname)s\t%(message)s',
- datefmt='%Y-%m-%d %H:%M:%S',
- level=logging.INFO)
- logger.info('Starting HD Homerun discovery')
- start = datetime.utcnow()
- for attempt in range(1, ATTEMPTS):
- try:
- sproc = subprocess.Popen(command, stdout=subprocess.PIPE,
- shell=True)
- sproc.communicate()
- except KeyboardInterrupt:
- sys.exit(2)
- if sproc.returncode != 0:
- logger.warning('%s failed, return code = %d.', command,
- sproc.returncode)
- sleep(DELAY)
- else:
- logger.info('Found HD Homerun. Seconds=%s, attempts=%d.',
- get_elapsed_time(start), attempt)
- sys.exit(0)
- logger.error('Couldn\'t find HD Homerun. Seconds=%s, attempts=%d.',
- get_elapsed_time(start), attempt)
- if __name__ == '__main__':
- main()
- # vim: set expandtab tabstop=4 shiftwidth=4 smartindent colorcolumn=80:
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement