Exemplo n.º 1
0
def _selftest(targethost, tport, username='******', realname=b'? ? ?',
      ssl=None):
   import pprint
   from gonium.service_aggregation import ServiceAggregate
   from gonium._debugging import streamlogger_setup
   
   logging.getLogger('IRCClientConnection').setLevel(20)
   
   us = IRCUserSpec(username=username, realname=realname)
   us.add_nick(b'NickServ')
   us.add_nick(b'ChanServ')
   
   servers = (
      IRCServerSpec('nonexistent.nowhere', 1),
      IRCServerSpec('0.0.0.0', 1),
      IRCServerSpec(targethost, tport, ssl=ssl),
   )
   
   def link():
      print('All done. Shutting down.')
      sa.ed.shutdown()
   
   streamlogger_setup()
   sa = ServiceAggregate()
   sa.add_dnslm()
   
   irccnl = IRCClientNetworkLink(sa, None, us, servers, conn_delay_is=5)
   irccnl.em_link_finish.new_prio_listener(link)
   
   irccnl.conn_init()
   
   sa.ed.event_loop()
Exemplo n.º 2
0
def _main():
   import optparse
   import sys
   from gonium import _debugging; _debugging.streamlogger_setup()
   
   op = optparse.OptionParser(usage='%prog [options] <lnfs_volume> <torrent meta file>...')
   op.add_option('-b', '--basepath', dest='basepath', default='.', metavar='PATH', help='Basepath to use for reading BT data')
   op.add_option('-r', '--reverse', default=False, action='store_true', help='Copy from LNFS to FS instead of the opposite direction')
   
   (options, args) = op.parse_args()
   basepath = options.basepath.encode()
   
   fn_vol = args[0]
   fns_bt = args[1:]
   
   sa = ServiceAggregate()
   sa.aio = None
   
   _log(20, 'Opening LNFS volume {0!a}.'.format(fn_vol))
   vol = LNFSVolume(open(fn_vol,'r+b'))
   
   for fn_bt in fns_bt:
      _log(20, 'Reading metainfo from {0!a} and opening data files.'.format(fn_bt))
      mi = BTMetaInfo.build_from_benc_stream(open(fn_bt,'rb'))
      btdiskio_v = btdiskio_v_build(sa, mi, basepath, mkdirs=options.reverse, mkfiles=options.reverse)
      btdiskio_lnfs = vol.btdiskio_build(sa, mi, basepath)
      
      _log(20, 'Copying data.')
      if (options.reverse):
         btdata_copy_b(sa, btdiskio_v, btdiskio_lnfs, mi.length_total)
      else:
         btdata_copy_b(sa, btdiskio_lnfs, btdiskio_v, mi.length_total)
      _log(20, 'Data copy finished.')
   
   _log(20, 'All done.')
Exemplo n.º 3
0
def _selftest(target, nick='Zanaffar', username='******', realname=b'? ? ?',
      channels=()):
   import pprint
   from gonium.fdm import ED_get
   from gonium._debugging import streamlogger_setup
   
   logging.getLogger('IRCClientConnection').setLevel(20)
   
   def link():
      for chan in channels:
         irccc._send_msg(b'JOIN', chan)
   
   streamlogger_setup()
   ed = ED_get()()
   irccc = IRCClientConnection.irc_build_sock_connect(ed, target, nick=nick,
      username=username, realname=realname)
   irccc.em_shutdown.new_listener(ccd(1)(ed.shutdown))
   irccc.em_link_finish.new_listener(ccd(1)(link))
   
   query_specs = (
      (2, b'LINKS'),
      (3, b'LIST'),
      (4, b'MAP')
   )
   
   queries_answered = 0
   def cb_print(query):
      nonlocal queries_answered
      print(query)
      pprint.pprint(query.rv[:20])
      queries_answered += 1
      if (queries_answered == len(query_specs)):
         print('All done. Shutting down.')
         ed.shutdown()
   
   for (d, cmd) in query_specs:
      ed.set_timer(d, irccc.put_msg, args=(IRCMessage(None, cmd,()), cb_print))
   
   if (channels):
      #__ChanEcho(irccc, channels[0])
      pass
   
   ed.event_loop()
Exemplo n.º 4
0
def _selftest():
   import struct
   from gonium.service_aggregation import ServiceAggregate
   from gonium._debugging import streamlogger_setup; streamlogger_setup()
   from collections import namedtuple
   from .benc_structures import BTTargetFile
   CHUNKLEN = 4
   
   flen_s = [1, 11, 1025, 23456]
   flen_s[-1] += (CHUNKLEN - (sum(flen_s) % CHUNKLEN))
   
   chunkcount = sum(flen_s)//CHUNKLEN
   
   fn_data = []
   for i in range(len(flen_s)):
      fn_data.append((flen_s[i], '__liasis_dio.test.{0}.tmp'.format(i).encode('ascii')))
   
   DMI = namedtuple('DummyMetaInfo', ('files','basename'))
   dmi = DMI([BTTargetFile(fn,fs) for (fs,fn) in fn_data], b'.')
   sa = ServiceAggregate()
   
   for btdio_cls in (BTDiskSyncIO,BTDiskAIO):
      print('== Testing {0} =='.format(btdio_cls))
      btdio = btdio_cls(sa, dmi, b'.')
      
      def sd(*args, **kwargs):
         sa.ed.shutdown()
      
      print('=== Test: Writing data ===')
      chunks_write = [(i*CHUNKLEN, struct.pack('>L', i) + b'\x00'*(CHUNKLEN-4)) for i in range(chunkcount)]
      btdio.async_write(chunks_write, sd).callback
      sa.ed.event_loop()
      
      for f in (btf.file for btf in dmi.files):
         f.seek(0)
      buf = b''
      i = 0
      
      for f in (btf.file for btf in dmi.files):
         while (True):
            buf += f.read(CHUNKLEN-len(buf))
            if (len(buf) < CHUNKLEN):
               break
            (val,) = struct.unpack('>L',buf[:4])
            if (val != i):
               raise ValueError('Chunk {0} has data {1}.'.format(i, buf))
            i += 1
            buf = b''
      
      print('=== Verifying written data ===')
      print('...passed.')
      
      chunks_read = [(i*CHUNKLEN, bytearray(CHUNKLEN)) for i in range(chunkcount)]
      print('=== Test: Reading data ===')
      btdio.async_readinto(chunks_read, sd).callback
      sa.ed.event_loop()
      for i in range(chunkcount):
          (val,) = struct.unpack('>L', chunks_read[i][1][:4])
          if (val != i):
             raise ValueError('BTDiskIO delivered data {0} for chunk {1}.'.format(chunks_read[i][1], i))
      
      print('...passed')
      print('== cleaning up ==')
      for f in dmi.files:
         f.file_close()
      
      for fnd in fn_data:
         os.remove(fnd[1])
Exemplo n.º 5
0
Arquivo: main.py Projeto: sh01/luteus
def main():
   import optparse
   import os.path
   import signal
   import sys
   
   from .config import LuteusConfig
   from gonium._debugging import streamlogger_setup
   from gonium.pid_filing import PidFile
   from gonium.daemon import daemon_fork
   from gonium.posix.signal import SA_RESTART
   
   logger = logging.getLogger()
   log = logger.log
   
   conf_fn = 'luteus.conf'
   
   op = optparse.OptionParser()
   op.add_option('--dir', default='~/.luteus', help='Directory to chdir to', metavar='DIR')
   op.add_option('--config', default=None, help='Config file to use', metavar='FILE')
   op.add_option('--debug', default=False, action='store_true', help="Don't fork, and log to stderr.")
   
   og_cc = optparse.OptionGroup(op, "Cert-retrieval mode")
   og_cc.add_option('--check-certs', dest='check_certs', action='store_true',
      default=False, help="Check whether SSL CA config works with current remotes, and exit.")
   og_cc.add_option('--write-new-certs', dest='cc_wnc', action='store_true',
      default=False, help="When checking CA config, retrieve currently used certs from peers and write them to disk.")
   og_cc.add_option('--cc-clobber', dest='cc_clobber', action='store_false',
      default=True, help="When writing retrieved CA certs to disk, overwrite cert files. [DANGEROUS]")
   
   op.add_option_group(og_cc)
   
   (opts, args) = op.parse_args()
   
   if (opts.debug or opts.check_certs):
      streamlogger_setup()
   
   if (opts.config):
      conf_fn = os.path.abspath(opts.config)
   
   tpath = os.path.expanduser(opts.dir)
   log(20, 'CDing to {0!a}.'.format(tpath))
   os.chdir(tpath)
   
   # Best to be paranoid for logs, etc.
   os.umask(0o77)
   
   conf = LuteusConfig()
   log(20, 'Loading config from {0!a}.'.format(conf_fn))
   conf.load_config_by_fn(conf_fn)

   if (opts.check_certs):
      log(20, 'Checking certs of SSL targets.')
      ca_certs_update(conf, opts.cc_wnc, opts.cc_clobber)
      log(20, 'All done.')
      return
   
   pid_file = PidFile()
   pid_file.lock(True)
   
   sa = conf._sa
   
   sa.sc.sighandler_install(signal.SIGTERM, SA_RESTART)
   sa.sc.sighandler_install(signal.SIGINT, SA_RESTART)
   def handle_signals(si_l):
      for si in si_l:
         if ((si.signo == signal.SIGTERM) or (si.signo == signal.SIGINT)):
            sa.ed.shutdown()
            quit_msg = 'Shutting down on signal {0!a}.'.format(si.signo)
            log(50, quit_msg)
            for nc in conf._icncs:
               nc.send_quit(quit_msg.encode('ascii'))
            break

   sa.sc.handle_signals.new_listener(handle_signals)
   
   if not (opts.debug):
      daemon_fork(pidfile=pid_file)
   
   log(50, 'Initialization done; starting normal operation.')
   conf._start_connections()
   conf._event_loop()