def test_parse_human_size(self): from deluge.common import parse_human_size sizes = [('1', 1), ('10 bytes', 10), ('2048 bytes', 2048), ('1MiB', 2**(10 * 2)), ('1 MiB', 2**(10 * 2)), ('1 GiB', 2**(10 * 3)), ('1 GiB', 2**(10 * 3)), ('1M', 10**6), ('1MB', 10**6), ('1 GB', 10**9), ('1 TB', 10**12)] for human_size, byte_size in sizes: parsed = parse_human_size(human_size) self.assertEqual(parsed, byte_size, 'Mismatch when converting: %s' % human_size)
def _handle_ui_options(self, options): """Handle UI common and process group options. Args: options (argparse.Namespace): The parsed options. Returns: argparse.Namespace: The parsed options. """ if not self.common_setup: self.common_setup = True # Setup the logger if options.quiet: options.loglevel = 'none' if options.loglevel: options.loglevel = options.loglevel.lower() logfile_mode = 'w' logrotate = options.logrotate if options.logrotate: logfile_mode = 'a' logrotate = common.parse_human_size(options.logrotate) # Setup the logger deluge.log.setup_logger( level=options.loglevel, filename=options.logfile, filemode=logfile_mode, logrotate=logrotate, output_stream=self.log_stream, ) if options.config: if not set_config_dir(options.config): log = logging.getLogger(__name__) log.error( 'There was an error setting the config dir! Exiting..') sys.exit(1) else: if not os.path.exists(common.get_default_config_dir()): os.makedirs(common.get_default_config_dir()) if self.process_arg_group: self.process_arg_group = False # If donotdaemonize is set, skip process forking. if not (common.windows_check() or options.donotdaemonize): if os.fork(): os._exit(0) os.setsid() # Do second fork if os.fork(): os._exit(0) # Ensure process doesn't keep any directory in use that may prevent a filesystem unmount. os.chdir(get_config_dir()) # Write pid file before chuid if options.pidfile: with open(options.pidfile, 'w') as _file: _file.write('%d\n' % os.getpid()) if not common.windows_check(): if options.group: if not options.group.isdigit(): import grp options.group = grp.getgrnam(options.group)[2] os.setgid(options.group) if options.user: if not options.user.isdigit(): import pwd options.user = pwd.getpwnam(options.user)[2] os.setuid(options.user) return options
def _handle_ui_options(self, options): """Handle UI common and process group options. Args: options (argparse.Namespace): The parsed options. Returns: argparse.Namespace: The parsed options. """ if not self.common_setup: self.common_setup = True # Setup the logger if options.quiet: options.loglevel = 'none' if options.loglevel: options.loglevel = options.loglevel.lower() logfile_mode = 'w' logrotate = options.logrotate if options.logrotate: logfile_mode = 'a' logrotate = common.parse_human_size(options.logrotate) # Setup the logger deluge.log.setup_logger(level=options.loglevel, filename=options.logfile, filemode=logfile_mode, logrotate=logrotate, output_stream=self.log_stream) if options.config: if not set_config_dir(options.config): log = logging.getLogger(__name__) log.error('There was an error setting the config dir! Exiting..') sys.exit(1) else: if not os.path.exists(common.get_default_config_dir()): os.makedirs(common.get_default_config_dir()) if self.process_arg_group: self.process_arg_group = False # If donotdaemonize is set, skip process forking. if not (common.windows_check() or options.donotdaemonize): if os.fork(): os._exit(0) os.setsid() # Do second fork if os.fork(): os._exit(0) # Ensure process doesn't keep any directory in use that may prevent a filesystem unmount. os.chdir(get_config_dir()) # Write pid file before chuid if options.pidfile: with open(options.pidfile, 'wb') as _file: _file.write('%d\n' % os.getpid()) if not common.windows_check(): if options.user: if not options.user.isdigit(): import pwd options.user = pwd.getpwnam(options.user)[2] os.setuid(options.user) if options.group: if not options.group.isdigit(): import grp options.group = grp.getgrnam(options.group)[2] os.setuid(options.group) return options