예제 #1
0
파일: pgp.py 프로젝트: cadithealth/genemail
  def modify(self, mailfrom, recipients, data, *other):

    rcptlist = list(set(recipients))

    if self.kprune:
      keys = self.gpg.list_keys()
      for rcpt in rcptlist[:]:
        for key in keys:
          if rcpt in ','.join(key.get('uids', [])):
            break
        else:
          rcptlist.remove(rcpt)
          log.warning('recipient %s removed (not found in keys)', rcpt)

    if self.rprune:
      recipients = rcptlist[:]
      if asset.isstr(data):
        data = email.message_from_string(data)
      if 'to' in data:
        del data['to']
        data.add_header('to', ', '.join(recipients))

    if not asset.isstr(data):
      hdritems = data.items()
      data = data.as_string()
      if not data.endswith('\n'):
        data += '\n'
    else:
      hdritems = email.message_from_string(data).items()

    if self.addkeys:
      rcptlist = list(set(rcptlist + self.addkeys))

    data = self.gpg.encrypt(data, rcptlist, sign=self.sign, always_trust=True)
    if not data.ok:
      raise ValueError('Encryption failed: ' + data.status)

    edata = email.MIMEMultipart.MIMEMultipart(
      'encrypted', protocol='application/pgp-encrypted')

    params = email.MIMENonMultipart.MIMENonMultipart('application', 'pgp-encrypted')
    params.set_payload('Version: 1\n')
    edata.attach(params)

    payload = email.MIMENonMultipart.MIMENonMultipart('application', 'octet-stream')
    payload.set_payload(str(data))
    edata.attach(payload)

    for key, value in hdritems:
      if key.lower() in ('content-type', 'mime-version'):
        continue
      edata.add_header(key, value)

    return (mailfrom, recipients, edata) + other
예제 #2
0
def autoresolve(obj):
  if isstr(obj):
    return obj
  if isinstance(obj, (list, tuple)):
    return [autoresolve(e) for e in obj]
  if not isinstance(obj, dict) or 'class' not in obj:
    return obj
  params = {k: autoresolve(v) for k, v in obj.items() if k != 'class'}
  return symbol(obj['class'])(**params)
예제 #3
0
 def __init__(self, settings=None):
   if settings is None:
     settings = dict()
   self.threshold = float(settings.get('threshold', DEFAULT_INFLECT))
   factors = settings.get('factors') or DEFAULT_FACTORS
   if asset.isstr(factors):
     factors = [s.strip() for s in factors.split(',')]
   for key in settings.keys():
     key = key.split('.')
     if len(key) >= 3 \
         and key[0] == 'factor' and key[2] == 'class' \
         and key[1] not in factors:
       factors.append(key[1])
   self.factors   = [self._load(factor, settings) for factor in factors]
   self.logger    = asset.symbol(settings.get('logger'))
   self.pessimism = 1.0 / float(settings.get('pessimism', 10))
예제 #4
0
 def __init__(self, settings=None):
     if settings is None:
         settings = dict()
     self.threshold = float(settings.get('threshold', DEFAULT_INFLECT))
     factors = settings.get('factors') or DEFAULT_FACTORS
     if asset.isstr(factors):
         factors = [s.strip() for s in factors.split(',')]
     for key in settings.keys():
         key = key.split('.')
         if len(key) >= 3 \
             and key[0] == 'factor' and key[2] == 'class' \
             and key[1] not in factors:
             factors.append(key[1])
     self.factors = [self._load(factor, settings) for factor in factors]
     self.logger = asset.symbol(settings.get('logger'))
     self.pessimism = 1.0 / float(settings.get('pessimism', 10))
예제 #5
0
파일: pgp.py 프로젝트: cadithealth/genemail
 def __init__(self, prune_keys=True, prune_recipients=False,
              sign=None, add_key='sign-key', gpg_options=None):
   import gnupg
   self.kprune  = prune_keys
   self.rprune  = prune_recipients
   self.sign    = sign
   self.addkeys = add_key
   if self.addkeys is not None:
     if asset.isstr(self.addkeys):
       self.addkeys = [self.addkeys]
     self.addkeys = list(set(self.addkeys))
     if 'sign-key' in self.addkeys:
       self.addkeys.remove('sign-key')
       if self.sign is not None:
         self.addkeys.append(self.sign)
   self.gpg = gnupg.GPG(**(gpg_options or dict()))
예제 #6
0
 def _load(self, factor, settings):
   predef = {
     'length'   : LengthFactor,
     'charmix'  : CharmixFactor,
     'casemix'  : CasemixFactor,
     'variety'  : VarietyFactor,
     'notword'  : NotWordFactor,
     'phrase'   : PhraseFactor,
     }
   params = dict()
   if asset.isstr(factor):
     for key, value in settings.items():
       if key.startswith('factor.' + factor + '.'):
         params[key[len(factor) + 8:]] = value
   factor = params.pop('class', factor)
   if factor in predef:
     return predef[factor](**params)
   return asset.symbol(factor)(**params)
예제 #7
0
 def _load(self, factor, settings):
     predef = {
         'length': LengthFactor,
         'charmix': CharmixFactor,
         'casemix': CasemixFactor,
         'variety': VarietyFactor,
         'notword': NotWordFactor,
         'phrase': PhraseFactor,
     }
     params = dict()
     if asset.isstr(factor):
         for key, value in settings.items():
             if key.startswith('factor.' + factor + '.'):
                 params[key[len(factor) + 8:]] = value
     factor = params.pop('class', factor)
     if factor in predef:
         return predef[factor](**params)
     return asset.symbol(factor)(**params)
예제 #8
0
 def loadPublishingPoints(self):
   pubpnts = {}
   cfgsrcs = [sources.get(src)(self)
              for src in asList(self.options.configOrder)]
   def parentDirWalk(path):
     while True:
       yield path
       next = os.path.dirname(path)
       if next == path:
         return
       path = next
   overconf = None
   if self.options.overrideConfig is not None:
     overconf = self.options.overrideConfig
     if isstr(overconf):
       overconf = yaml.load(util.getUriContent(overconf, aadict(svnrev=self.svnrev, svndir='')))
     overconf = aadict.d2ar(overconf)
   for change in self.svnrev.changes:
     path = change.path
     if not change.isdir:
       path = os.path.dirname(path)
     self.log.drivel('checking change "%s" for publishing point config', change)
     for root in parentDirWalk(path):
       root = root or '/'
       self.log.noise('checking publishing point "%s" for config', root)
       if len(asList(self.options.publishOnly)) > 0:
         # TODO: add RE flag control...
         # todo: only add the '$' if it does not already end in a '$'...
         if len([e for e in asList(self.options.publishOnly) if re.match(e + '$', root) is not None]) <= 0:
           self.log.debug('publishing point "%s" not in options.publishOnly - skipping', root)
           continue
       conf = None
       for cs in cfgsrcs:
         conf = cs.getConfig(root)
         if conf is not None:
           conf = aadict.d2ar(conf)
           break
       if conf is not None \
          or ( overconf is not None and len(asList(self.options.publishOnly)) > 0 ):
         pubpnts[root] = PublishingPoint(self, root, overconf or conf)
       else:
         self.log.noise('no publishing point config for "%s"', change)
   return sorted(pubpnts.values(), key=operator.attrgetter('root'))
예제 #9
0
def run_engines(params, paramkey, engtype, args=None,
                prefix=None, evals=None, env=None):
  if params.get(paramkey) is None:
    return

  if args is None:
    args = []

  engines = asList(params.get(paramkey))

  engine_env = evalEnv(params, prefix,
                       params.get(paramkey + '-env'), params.get(paramkey + '-env+'),
                       evals, env)

  for engcfg in engines:
    if isstr(engcfg):
      engcfg = aadict(engine='shell', command=engcfg)
    engine   = engcfg.engine
    engsafe  = str2sym(engine)
    funcname = engcfg.get('engine-handler', 'svnpublish.engine.%s.%s_%s' % (engsafe, engtype, engsafe))
    params.logger.info('loading %s engine "%s" (%s)', engtype, engine, funcname)
    handler = symbol(funcname)
    if params.dryrun:
      funcname += '_dryrun'
      try:
        handler = symbol(funcname + '_dryrun')
      except ImportError:
        params.logger.debug('engine "%s" does not support dry-run mode - skipping', engine)
        continue
    engine_params = aadict(params).update(engcfg).update({
      'options' : engcfg,
      'logger'  : logging.getLogger('.'.join((params.logger.name, engtype, engine))),
      'prefix'  : prefix,
      'env'     : engine_env,
      'evals'   : evals,
      })
    handler(engine_params, *args)