def find_install_type(): """ Determines how this copy of sr was installed. returns: type of installation. Possible values are: 'win': any compiled windows build 'git': running from source using git 'source': running from source without git """ if os.path.isdir( os.path.join(os.path.dirname(settings.PROG_DIR), '.git')): install_type = 'git' elif check_installed(): install_type = 'pip' else: install_type = 'source' return install_type
def start(self): """ Start SickChill """ # do some preliminary stuff settings.MY_FULLNAME = os.path.normpath(os.path.abspath(__file__)) settings.MY_NAME = os.path.basename(settings.MY_FULLNAME) settings.DATA_DIR = os.path.dirname(settings.PROG_DIR) profile_path = str(Path.home().joinpath("sickchill").absolute()) if check_installed(): settings.DATA_DIR = profile_path if settings.DATA_DIR != profile_path: checks = ["sickbeard.db", "sickchill.db", "config.ini"] if not any([ os.path.isfile(os.path.join(settings.DATA_DIR, check)) for check in checks ]): settings.DATA_DIR = profile_path settings.MY_ARGS = sys.argv[1:] # Rename the main thread threading.currentThread().name = "MAIN" args = SickChillArgumentParser(settings.DATA_DIR).parse_args() if args.force_update: result = self.force_update() sys.exit(int(not result)) # Ok -> 0 , Error -> 1 settings.NO_RESIZE = args.noresize self.console_logging = not (hasattr(sys, "frozen") or args.quiet or args.daemon) self.no_launch = args.nolaunch or args.daemon self.forced_port = args.port self.run_as_daemon = args.daemon and platform.system() != "Windows" # The pid file is only useful in daemon mode, make sure we can write the file properly if bool(args.pidfile) and not self.run_as_daemon: if self.console_logging: sys.stdout.write( "Not running in daemon mode. PID file creation disabled.\n" ) settings.DATA_DIR = os.path.abspath( args.datadir) if args.datadir else settings.DATA_DIR settings.CONFIG_FILE = os.path.abspath( args.config) if args.config else os.path.join( settings.DATA_DIR, "config.ini") # Make sure that we can create the data dir if not os.access(settings.DATA_DIR, os.F_OK): try: os.makedirs(settings.DATA_DIR, 0o744) except os.error: raise SystemExit("Unable to create data directory: {0}".format( settings.DATA_DIR)) # Make sure we can write to the data dir if not os.access(settings.DATA_DIR, os.W_OK): raise SystemExit("Data directory must be writeable: {0}".format( settings.DATA_DIR)) # Make sure we can write to the config file if not os.access(settings.CONFIG_FILE, os.W_OK): if os.path.isfile(settings.CONFIG_FILE): raise SystemExit("Config file must be writeable: {0}".format( settings.CONFIG_FILE)) elif not os.access(os.path.dirname(settings.CONFIG_FILE), os.W_OK): raise SystemExit( "Config file root dir must be writeable: {0}".format( os.path.dirname(settings.CONFIG_FILE))) os.chdir(settings.DATA_DIR) # Check if we need to perform a restore first restore_dir = os.path.join(settings.DATA_DIR, "restore") if os.path.exists(restore_dir): success = self.restore_db(restore_dir, settings.DATA_DIR) if self.console_logging: sys.stdout.write( "Restore: restoring DB and config.ini {0}!\n".format( ("FAILED", "SUCCESSFUL")[success])) # Load the config and publish it to the oldbeard package if self.console_logging and not os.path.isfile(settings.CONFIG_FILE): sys.stdout.write( "Unable to find {0}, all settings will be default!\n".format( settings.CONFIG_FILE)) settings.CFG = ConfigObj(settings.CONFIG_FILE, encoding="UTF-8", indent_type=" ") # Initialize the config and our threads sickchill.start.initialize(consoleLogging=self.console_logging) # Get PID settings.PID = os.getpid() # Build from the DB to start with self.load_shows_from_db() logger.info("Starting SickChill [{branch}] using '{config}'".format( branch=settings.BRANCH, config=settings.CONFIG_FILE)) self.clear_cache() if settings.DEVELOPER: settings.movie_list = MovieList() web_options = {} if self.forced_port: logger.info("Forcing web server to port {port}".format( port=self.forced_port)) self.start_port = self.forced_port web_options.update({ "port": int(self.start_port), }) else: self.start_port = settings.WEB_PORT # start web server self.web_server = SRWebServer(web_options) self.web_server.start() # Fire up all our threads sickchill.start.start() # Build internal name cache name_cache.build_name_cache() # Pre-populate network timezones, it isn't thread safe network_timezones.update_network_dict() # sure, why not? if settings.USE_FAILED_DOWNLOADS: failed_history.trimHistory() # Check for metadata indexer updates for shows (sets the next aired ep!) # oldbeard.showUpdateScheduler.forceRun() # Launch browser if settings.LAUNCH_BROWSER and not self.no_launch: sickchill.start.launchBrowser( "https" if settings.ENABLE_HTTPS else "http", self.start_port, settings.WEB_ROOT) # main loop while True: time.sleep(1)
def start(self): """ Start SickChill """ # do some preliminary stuff settings.MY_FULLNAME = os.path.normpath(os.path.abspath(__file__)) settings.MY_NAME = os.path.basename(settings.MY_FULLNAME) settings.DATA_DIR = os.path.dirname(settings.PROG_DIR) profile_path = str(Path.home().joinpath('sickchill').absolute()) if check_installed(): settings.DATA_DIR = profile_path if settings.DATA_DIR != profile_path: checks = [ 'sickbeard.db', 'sickchill.db', 'config.ini' ] if not any([os.path.isfile(os.path.join(settings.DATA_DIR, check)) for check in checks]): settings.DATA_DIR = profile_path settings.MY_ARGS = sys.argv[1:] # Rename the main thread threading.currentThread().name = 'MAIN' args = SickChillArgumentParser(settings.DATA_DIR).parse_args() if args.force_update: result = self.force_update() sys.exit(int(not result)) # Ok -> 0 , Error -> 1 # Need console logging for SickChill.py and SickChill-console.exe settings.NO_RESIZE = args.noresize self.console_logging = (not hasattr(sys, 'frozen')) or (settings.MY_NAME.lower().find('-console') > 0) and not args.quiet self.no_launch = args.nolaunch self.forced_port = args.port if args.daemon: self.run_as_daemon = platform.system() != 'Windows' self.console_logging = False self.no_launch = True self.create_pid = bool(args.pidfile) self.pid_file = args.pidfile if self.pid_file and os.path.exists(self.pid_file): # If the pid file already exists, SickChill may still be running, so exit raise SystemExit('PID file: {0} already exists. Exiting.'.format(self.pid_file)) settings.DATA_DIR = os.path.abspath(args.datadir) if args.datadir else settings.DATA_DIR settings.CONFIG_FILE = os.path.abspath(args.config) if args.config else os.path.join(settings.DATA_DIR, 'config.ini') # The pid file is only useful in daemon mode, make sure we can write the file properly if self.create_pid: if self.run_as_daemon: pid_dir = os.path.dirname(self.pid_file) if not os.access(pid_dir, os.F_OK): sys.exit('PID dir: {0} doesn\'t exist. Exiting.'.format(pid_dir)) if not os.access(pid_dir, os.W_OK): raise SystemExit('PID dir: {0} must be writable (write permissions). Exiting.'.format(pid_dir)) else: if self.console_logging: sys.stdout.write('Not running in daemon mode. PID file creation disabled.\n') self.create_pid = False # Make sure that we can create the data dir if not os.access(settings.DATA_DIR, os.F_OK): try: os.makedirs(settings.DATA_DIR, 0o744) except os.error: raise SystemExit('Unable to create data directory: {0}'.format(settings.DATA_DIR)) # Make sure we can write to the data dir if not os.access(settings.DATA_DIR, os.W_OK): raise SystemExit('Data directory must be writeable: {0}'.format(settings.DATA_DIR)) # Make sure we can write to the config file if not os.access(settings.CONFIG_FILE, os.W_OK): if os.path.isfile(settings.CONFIG_FILE): raise SystemExit('Config file must be writeable: {0}'.format(settings.CONFIG_FILE)) elif not os.access(os.path.dirname(settings.CONFIG_FILE), os.W_OK): raise SystemExit('Config file root dir must be writeable: {0}'.format(os.path.dirname(settings.CONFIG_FILE))) os.chdir(settings.DATA_DIR) # Check if we need to perform a restore first restore_dir = os.path.join(settings.DATA_DIR, 'restore') if os.path.exists(restore_dir): success = self.restore_db(restore_dir, settings.DATA_DIR) if self.console_logging: sys.stdout.write('Restore: restoring DB and config.ini {0}!\n'.format(('FAILED', 'SUCCESSFUL')[success])) # Load the config and publish it to the oldbeard package if self.console_logging and not os.path.isfile(settings.CONFIG_FILE): sys.stdout.write('Unable to find {0}, all settings will be default!\n'.format(settings.CONFIG_FILE)) settings.CFG = ConfigObj(settings.CONFIG_FILE, encoding='UTF-8', indent_type=' ') if self.run_as_daemon: self.daemonize() # Initialize the config and our threads sickchill.start.initialize(consoleLogging=self.console_logging) # Get PID settings.PID = os.getpid() # Build from the DB to start with self.load_shows_from_db() logger.info('Starting SickChill [{branch}] using \'{config}\''.format(branch=settings.BRANCH, config=settings.CONFIG_FILE)) self.clear_cache() if settings.DEVELOPER: settings.movie_list = MovieList() web_options = {} if self.forced_port: logger.info('Forcing web server to port {port}'.format(port=self.forced_port)) self.start_port = self.forced_port web_options.update({ 'port': int(self.start_port), }) else: self.start_port = settings.WEB_PORT # start web server self.web_server = SRWebServer(web_options) self.web_server.start() # Fire up all our threads sickchill.start.start() # Build internal name cache name_cache.buildNameCache() # Pre-populate network timezones, it isn't thread safe network_timezones.update_network_dict() # sure, why not? if settings.USE_FAILED_DOWNLOADS: failed_history.trimHistory() # Check for metadata indexer updates for shows (sets the next aired ep!) # oldbeard.showUpdateScheduler.forceRun() # Launch browser if settings.LAUNCH_BROWSER and not (self.no_launch or self.run_as_daemon): sickchill.start.launchBrowser('https' if settings.ENABLE_HTTPS else 'http', self.start_port, settings.WEB_ROOT) # main loop while True: time.sleep(1)