def _mktemp(self, contents=None, prefix='test-rutorrent-client-'): fd, name = mkstemp(prefix=prefix) write_fd(fd, contents) close_fd(fd) self._temp_files.append(name) return name
def _mktemp(self, contents=None, prefix='test-', dir=None): fd, name = mkstemp(prefix=prefix, dir=dir) write_fd(fd, contents) close_fd(fd) self._temp_files.append(name) return name
def _daemonize(self): # this code is inspire by the recipe described here: # http://code.activestate.com/recipes/278731-creating-a-daemon-the-python-way/ setsid() import resource MAXFD = 1024 # we close all open file descriptors != sys.stderr.fileno() maxfd = resource.getrlimit(resource.RLIMIT_NOFILE)[1] if (maxfd == resource.RLIM_INFINITY): maxfd = MAXFD if hasattr(sys.stderr, "fileno"): stderr_fileno = sys.stderr.fileno() else: # mod_wsgi replace sys.stderr with a fake file without a "fileno" # method stderr_fileno = -1 # close all fd > 4 for fd in xrange(3, maxfd): if fd != stderr_fileno: try: close_fd(fd) except OSError: pass # free up some memory global httplib del httplib del resource self.log.debug("cleanup done") child_pid = fork() if child_pid > 0: _exit(0) self.log.info("NTLMAuthHandler daemon spawned with pid %d", getpid()) # forked processes inherits lock created by flock, so we need to # unlock the file here # flock(lockf.fileno(), LOCK_UN) # lockf.close() # we are the daemon # Catch all exceptions here and print them to stderr before exiting # to avoid problems when running under paster try: self._run_as_daemon() except Exception as e: trace = traceback.format_exc() self.log.critical("Uncaught exception: %s\n%s", e, trace) self.log.critical("Exiting") _exit(1)
def _daemonize(self): # this code is inspire by the recipe described here: # http://code.activestate.com/recipes/278731-creating-a-daemon-the-python-way/ setsid() import resource MAXFD = 1024 # we close all open file descriptors != sys.stderr.fileno() maxfd = resource.getrlimit(resource.RLIMIT_NOFILE)[1] if (maxfd == resource.RLIM_INFINITY): maxfd = MAXFD if hasattr(sys.stderr, "fileno"): stderr_fileno = sys.stderr.fileno() else: # mod_wsgi replace sys.stderr with a fake file without a "fileno" # method stderr_fileno = -1 # close all fd > 4 for fd in xrange(3, maxfd): if fd != stderr_fileno: try: close_fd(fd) except OSError: pass # free up some memory global httplib del httplib del resource self.log.debug("cleanup done") child_pid = fork() if child_pid > 0: _exit(0) self.log.info("NTLMAuthHandler daemon spawned with pid %d", getpid()) # forked processes inherits lock created by flock, so we need to # unlock the file here # flock(lockf.fileno(), LOCK_UN) # lockf.close() # we are the daemon # Catch all exceptions here and print them to stderr before exiting # to avoid problems when running under paster try: self._run_as_daemon() except Exception as e: trace = traceback.format_exc() self.log.critical("Uncaught exception: %s\n%s", e,trace) self.log.critical("Exiting") _exit(1)
def _daemonize(self): # this code is inspire by the recipe described here: # http://code.activestate.com/recipes/278731-creating-a-daemon-the-python-way/ setsid() import resource MAXFD = 1024 # we close all open file descriptors != sys.stderr.fileno() maxfd = resource.getrlimit(resource.RLIMIT_NOFILE)[1] if maxfd == resource.RLIM_INFINITY: maxfd = MAXFD if hasattr(sys.stderr, "fileno"): stderr_fileno = sys.stderr.fileno() else: # mod_wsgi replace sys.stderr with a fake file without a "fileno" # method stderr_fileno = -1 # close all fd > 4 for fd in xrange(3, maxfd): if fd != stderr_fileno: try: close_fd(fd) except OSError: pass # free up some memory global httplib del httplib del resource # print >> sys.stderr, "cleanup done" child_pid = fork() if child_pid > 0: _exit(0) print >>sys.stderr, ("NTLMAuthHandler daemon spawned with pid %d" % getpid()) # forked processes inherits lock created by flock, so we need to # unlock the file here # flock(lockf.fileno(), LOCK_UN) # lockf.close() # we are the daemon self._run_as_daemon()
def _daemonize(self): # this code is inspire by the recipe described here: # http://code.activestate.com/recipes/278731-creating-a-daemon-the-python-way/ setsid() import resource MAXFD = 1024 # we close all open file descriptors != sys.stderr.fileno() maxfd = resource.getrlimit(resource.RLIMIT_NOFILE)[1] if (maxfd == resource.RLIM_INFINITY): maxfd = MAXFD if hasattr(sys.stderr, "fileno"): stderr_fileno = sys.stderr.fileno() else: # mod_wsgi replace sys.stderr with a fake file without a "fileno" # method stderr_fileno = -1 # close all fd > 4 for fd in xrange(3, maxfd): if fd != stderr_fileno: try: close_fd(fd) except OSError: pass # free up some memory global httplib del httplib del resource # print >> sys.stderr, "cleanup done" child_pid = fork() if child_pid > 0: _exit(0) print >> sys.stderr, ("NTLMAuthHandler daemon spawned with pid %d" % getpid()) # forked processes inherits lock created by flock, so we need to # unlock the file here # flock(lockf.fileno(), LOCK_UN) # lockf.close() # we are the daemon self._run_as_daemon()
def _epoll_fd_finalizer(epoll_fd, progress_tasks): assert epoll_fd >= 0 # Notice, progress_tasks must be cleared before we close # epoll_fd progress_tasks.clear() close_fd(epoll_fd)
def start_torrents(): """Uploads torrent files to the server.""" signal.signal(signal.SIGINT, ctrl_c_handler) cache_dir = realpath(expanduser('~/.cache/xirvik')) if not isdir(cache_dir): makedirs(cache_dir) log = logging.getLogger('xirvik-start-torrents') parser = argparse.ArgumentParser() parser.add_argument('-d', '--debug', action='store_true') parser.add_argument('-v', '--verbose', action='store_true') parser.add_argument('-c', '--netrc-path', default=expanduser('~/.netrc')) # parser.add_argument('-C', '--client', default='rutorrent3') parser.add_argument('-H', '--host', nargs=1, required=True) parser.add_argument('-p', '--port', nargs=1, default=[443]) parser.add_argument('--start-stopped', action='store_true') parser.add_argument('-s', '--syslog', action='store_true') parser.add_argument('directory', metavar='DIRECTORY', action=ReadableDirectoryListAction, nargs='*') args = parser.parse_args() verbose = args.debug or args.verbose log.setLevel(logging.INFO) if verbose: channel = logging.StreamHandler(sys.stdout if args.verbose else sys.stderr) channel.setFormatter(logging.Formatter('%(asctime)s - %(message)s')) channel.setLevel(logging.INFO if args.verbose else logging.DEBUG) log.addHandler(channel) if args.debug: log.setLevel(logging.DEBUG) if args.syslog: try: syslogh = SysLogHandler(address='/dev/log') except (OSError, socket.error): syslogh = SysLogHandler(address='/var/run/syslog', facility='local1') syslogh.ident = 'xirvik-start-torrents' logging.INFO = logging.WARNING syslogh.setFormatter( logging.Formatter('%(name)s[%(process)d]: %(message)s')) syslogh.setLevel(logging.DEBUG if args.debug else logging.INFO) log.addHandler(syslogh) try: user, _, password = netrc(args.netrc_path).authenticators(args.host[0]) except TypeError: print('Cannot find host {} in netrc. Specify user name and ' 'password'.format(args.host[0]), file=sys.stderr) sys.exit(1) post_url = ('https://{host:s}:{port:d}/rtorrent/php/' 'addtorrent.php?'.format(host=args.host[0], port=args.port[0])) form_data = {} exceptions_caught = [] # rtorrent2/3 params # dir_edit - ? # tadd_label - Label for the torrents, more param: label= # torrent_file - Torrent file blob data if args.start_stopped: form_data['torrents_start_stopped'] = 'on' for d in args.directory: for item in listdir(d): if not item.lower().endswith('.torrent'): continue item = path_join(d, item) # Workaround for surrogates not allowed error, rename the file prefix = '{n:s}-'.format(n=splitext(basename(item))[0]) fd, name = mkstemp(dir=cache_dir, prefix=prefix, suffix='.torrent') close_fd(fd) with open(name, 'wb') as w: with open(item, 'rb') as r: w.write(r.read()) old = item item = name with open(item, 'rb') as f: # Because the server does not understand filename*=UTF8 syntax # https://github.com/kennethreitz/requests/issues/2117 # This is not a huge concern, as the API's "Get .torrent" does # not return the file with its original name either try: filename = unidecode(f.name.decode('utf8')) except AttributeError: # decode filename = unidecode(f.name) files = dict(torrent_file=(filename, f,)) try: log.info('Uploading torrent {} (actual name: "{}")'.format( basename(item), basename(filename))) except OSError: pass r = requests.post(post_url, data=form_data, files=files) try: r.raise_for_status() except Exception as e: log.error('Caught exception: {}'.format(e)) # Delete original only after successful upload log.debug('Deleting {}'.format(old)) rm(old) if exceptions_caught: log.error('Exceptions caught, exiting with failure status') cleanup_and_exit(1)