def _smtp(ui): '''build an smtp connection and return a function to send mail''' local_hostname = ui.config('smtp', 'local_hostname') tls = ui.config('smtp', 'tls', 'none') # backward compatible: when tls = true, we use starttls. starttls = tls == 'starttls' or util.parsebool(tls) smtps = tls == 'smtps' if (starttls or smtps) and not util.safehasattr(socket, 'ssl'): raise util.Abort(_("can't use TLS: Python SSL support not installed")) mailhost = ui.config('smtp', 'host') if not mailhost: raise util.Abort(_('smtp.host not configured - cannot send mail')) verifycert = ui.config('smtp', 'verifycert', 'strict') if verifycert not in ['strict', 'loose']: if util.parsebool(verifycert) is not False: raise util.Abort(_('invalid smtp.verifycert configuration: %s') % (verifycert)) verifycert = False if (starttls or smtps) and verifycert: sslkwargs = sslutil.sslkwargs(ui, mailhost) else: sslkwargs = {} if smtps: ui.note(_('(using smtps)\n')) s = SMTPS(sslkwargs, local_hostname=local_hostname) elif starttls: s = STARTTLS(sslkwargs, local_hostname=local_hostname) else: s = smtplib.SMTP(local_hostname=local_hostname) if smtps: defaultport = 465 else: defaultport = 25 mailport = util.getport(ui.config('smtp', 'port', defaultport)) ui.note(_('sending mail: smtp host %s, port %s\n') % (mailhost, mailport)) s.connect(host=mailhost, port=mailport) if starttls: ui.note(_('(using starttls)\n')) s.ehlo() s.starttls() s.ehlo() if (starttls or smtps) and verifycert: ui.note(_('(verifying remote certificate)\n')) sslutil.validator(ui, mailhost)(s.sock, verifycert == 'strict') username = ui.config('smtp', 'username') password = ui.config('smtp', 'password') if username and not password: password = ui.getpass() if username and password: ui.note(_('(authenticating to mail server as %s)\n') % (username)) try: s.login(username, password) except smtplib.SMTPException, inst: raise util.Abort(inst)
def _smtp(ui): '''build an smtp connection and return a function to send mail''' local_hostname = ui.config('smtp', 'local_hostname') tls = ui.config('smtp', 'tls', 'none') # backward compatible: when tls = true, we use starttls. starttls = tls == 'starttls' or util.parsebool(tls) smtps = tls == 'smtps' if (starttls or smtps) and not util.safehasattr(socket, 'ssl'): raise util.Abort(_("can't use TLS: Python SSL support not installed")) mailhost = ui.config('smtp', 'host') if not mailhost: raise util.Abort(_('smtp.host not configured - cannot send mail')) verifycert = ui.config('smtp', 'verifycert', 'strict') if verifycert not in ['strict', 'loose']: if util.parsebool(verifycert) is not False: raise util.Abort( _('invalid smtp.verifycert configuration: %s') % (verifycert)) verifycert = False if (starttls or smtps) and verifycert: sslkwargs = sslutil.sslkwargs(ui, mailhost) else: sslkwargs = {} if smtps: ui.note(_('(using smtps)\n')) s = SMTPS(sslkwargs, local_hostname=local_hostname) elif starttls: s = STARTTLS(sslkwargs, local_hostname=local_hostname) else: s = smtplib.SMTP(local_hostname=local_hostname) if smtps: defaultport = 465 else: defaultport = 25 mailport = util.getport(ui.config('smtp', 'port', defaultport)) ui.note(_('sending mail: smtp host %s, port %s\n') % (mailhost, mailport)) s.connect(host=mailhost, port=mailport) if starttls: ui.note(_('(using starttls)\n')) s.ehlo() s.starttls() s.ehlo() if (starttls or smtps) and verifycert: ui.note(_('(verifying remote certificate)\n')) sslutil.validator(ui, mailhost)(s.sock, verifycert == 'strict') username = ui.config('smtp', 'username') password = ui.config('smtp', 'password') if username and not password: password = ui.getpass() if username and password: ui.note(_('(authenticating to mail server as %s)\n') % (username)) try: s.login(username, password) except smtplib.SMTPException, inst: raise util.Abort(inst)
def _smtp(ui): '''build an smtp connection and return a function to send mail''' local_hostname = ui.config('smtp', 'local_hostname') tls = ui.config('smtp', 'tls', 'none') # backward compatible: when tls = true, we use starttls. starttls = tls == 'starttls' or util.parsebool(tls) smtps = tls == 'smtps' if (starttls or smtps) and not util.safehasattr(socket, 'ssl'): raise util.Abort(_("can't use TLS: Python SSL support not installed")) if smtps: ui.note(_('(using smtps)\n')) s = smtplib.SMTP_SSL(local_hostname=local_hostname) else: s = smtplib.SMTP(local_hostname=local_hostname) mailhost = ui.config('smtp', 'host') if not mailhost: raise util.Abort(_('smtp.host not configured - cannot send mail')) mailport = util.getport(ui.config('smtp', 'port', 25)) ui.note(_('sending mail: smtp host %s, port %s\n') % (mailhost, mailport)) s.connect(host=mailhost, port=mailport) if starttls: ui.note(_('(using starttls)\n')) s.ehlo() s.starttls() s.ehlo() username = ui.config('smtp', 'username') password = ui.config('smtp', 'password') if username and not password: password = ui.getpass() if username and password: ui.note(_('(authenticating to mail server as %s)\n') % (username)) try: s.login(username, password) except smtplib.SMTPException, inst: raise util.Abort(inst)
def configbool(self, section, name, default=False, untrusted=False): """parse a configuration element as a boolean >>> u = ui(); s = 'foo' >>> u.setconfig(s, 'true', 'yes') >>> u.configbool(s, 'true') True >>> u.setconfig(s, 'false', 'no') >>> u.configbool(s, 'false') False >>> u.configbool(s, 'unknown') False >>> u.configbool(s, 'unknown', True) True >>> u.setconfig(s, 'invalid', 'somevalue') >>> u.configbool(s, 'invalid') Traceback (most recent call last): ... ConfigError: foo.invalid is not a boolean ('somevalue') """ v = self.config(section, name, None, untrusted) if v is None: return default if isinstance(v, bool): return v b = util.parsebool(v) if b is None: raise error.ConfigError(_("%s.%s is not a boolean ('%s')") % (section, name, v)) return b
def configbool(self, section, name, default=False, untrusted=False): """parse a configuration element as a boolean >>> u = ui(); s = 'foo' >>> u.setconfig(s, 'true', 'yes') >>> u.configbool(s, 'true') True >>> u.setconfig(s, 'false', 'no') >>> u.configbool(s, 'false') False >>> u.configbool(s, 'unknown') False >>> u.configbool(s, 'unknown', True) True >>> u.setconfig(s, 'invalid', 'somevalue') >>> u.configbool(s, 'invalid') Traceback (most recent call last): ... ConfigError: foo.invalid is not a boolean ('somevalue') """ v = self.config(section, name, None, untrusted) if v is None: return default if isinstance(v, bool): return v b = util.parsebool(v) if b is None: raise error.ConfigError( _("%s.%s is not a boolean ('%s')") % (section, name, v)) return b
def pad(context, mapping, args): """:pad(text, width[, fillchar=' '[, right=False]]): Pad text with a fill character.""" if not (2 <= len(args) <= 4): # i18n: "pad" is a keyword raise error.ParseError(_("pad() expects two to four arguments")) width = int(args[1][1]) text = stringify(args[0][0](context, mapping, args[0][1])) if args[0][0] == runstring: text = stringify(runtemplate(context, mapping, compiletemplate(text, context))) right = False fillchar = ' ' if len(args) > 2: fillchar = stringify(args[2][0](context, mapping, args[2][1])) if len(args) > 3: right = util.parsebool(args[3][1]) if right: return text.rjust(width, fillchar) else: return text.ljust(width, fillchar)
def pad(context, mapping, args): """usage: pad(text, width, fillchar=' ', right=False) """ if not (2 <= len(args) <= 4): # i18n: "pad" is a keyword raise error.ParseError(_("pad() expects two to four arguments")) width = int(args[1][1]) text = stringify(args[0][0](context, mapping, args[0][1])) if args[0][0] == runstring: text = stringify( runtemplate(context, mapping, compiletemplate(text, context))) right = False fillchar = ' ' if len(args) > 2: fillchar = stringify(args[2][0](context, mapping, args[2][1])) if len(args) > 3: right = util.parsebool(args[3][1]) if right: return text.rjust(width, fillchar) else: return text.ljust(width, fillchar)
def configbool(self, section, name, default=False, untrusted=False): v = self.config(section, name, None, untrusted) if v is None: return default if isinstance(v, bool): return v b = util.parsebool(v) if b is None: raise error.ConfigError(_("%s.%s not a boolean ('%s')") % (section, name, v)) return b
def configbool(self, section, name, default=False, untrusted=False): v = self.config(section, name, None, untrusted) if v is None: return default if isinstance(v, bool): return v b = util.parsebool(v) if b is None: raise error.ConfigError( _("%s.%s not a boolean ('%s')") % (section, name, v)) return b
def checkportabilityalert(ui): """check if the user's config requests nothing, a warning, or abort for non-portable filenames""" val = ui.config("ui", "portablefilenames", "warn") lval = val.lower() bval = util.parsebool(val) abort = os.name == "nt" or lval == "abort" warn = bval or lval == "warn" if bval is None and not (warn or abort or lval == "ignore"): raise error.ConfigError(_("ui.portablefilenames value is invalid ('%s')") % val) return abort, warn
def checkportabilityalert(ui): '''check if the user's config requests nothing, a warning, or abort for non-portable filenames''' val = ui.config('ui', 'portablefilenames', 'warn') lval = val.lower() bval = util.parsebool(val) abort = os.name == 'nt' or lval == 'abort' warn = bval or lval == 'warn' if bval is None and not (warn or abort or lval == 'ignore'): raise error.ConfigError( _("ui.portablefilenames value is invalid ('%s')") % val) return abort, warn
def __init__(self, repo, bundlecaps=None): """Given a source repo, construct a bundler. bundlecaps is optional and can be used to specify the set of capabilities which can be used to build the bundle. """ # Set of capabilities we can use to build the bundle. if bundlecaps is None: bundlecaps = set() self._bundlecaps = bundlecaps self._changelog = repo.changelog self._manifest = repo.manifest reorder = repo.ui.config('bundle', 'reorder', 'auto') if reorder == 'auto': reorder = None else: reorder = util.parsebool(reorder) self._repo = repo self._reorder = reorder self._progress = repo.ui.progress
def pad(context, mapping, args): """:pad(text, width[, fillchar=' '[, right=False]]): Pad text with a fill character.""" if not (2 <= len(args) <= 4): # i18n: "pad" is a keyword raise error.ParseError(_("pad() expects two to four arguments")) width = int(args[1][1]) text = stringify(args[0][0](context, mapping, args[0][1])) right = False fillchar = ' ' if len(args) > 2: fillchar = stringify(args[2][0](context, mapping, args[2][1])) if len(args) > 3: right = util.parsebool(args[3][1]) if right: return text.rjust(width, fillchar) else: return text.ljust(width, fillchar)
def __init__(self, repo, bundlecaps=None): """Given a source repo, construct a bundler. bundlecaps is optional and can be used to specify the set of capabilities which can be used to build the bundle. """ # Set of capabilities we can use to build the bundle. if bundlecaps is None: bundlecaps = set() self._bundlecaps = bundlecaps reorder = repo.ui.config('bundle', 'reorder', 'auto') if reorder == 'auto': reorder = None else: reorder = util.parsebool(reorder) self._repo = repo self._reorder = reorder self._progress = repo.ui.progress if self._repo.ui.verbose and not self._repo.ui.debugflag: self._verbosenote = self._repo.ui.note else: self._verbosenote = lambda s: None
def _smtp(ui): """build an smtp connection and return a function to send mail""" local_hostname = ui.config("smtp", "local_hostname") tls = ui.config("smtp", "tls", "none") # backward compatible: when tls = true, we use starttls. starttls = tls == "starttls" or util.parsebool(tls) smtps = tls == "smtps" if (starttls or smtps) and not util.safehasattr(socket, "ssl"): raise util.Abort(_("can't use TLS: Python SSL support not installed")) mailhost = ui.config("smtp", "host") if not mailhost: raise util.Abort(_("smtp.host not configured - cannot send mail")) verifycert = ui.config("smtp", "verifycert", "strict") if verifycert not in ["strict", "loose"]: if util.parsebool(verifycert) is not False: raise util.Abort(_("invalid smtp.verifycert configuration: %s") % (verifycert)) verifycert = False if (starttls or smtps) and verifycert: sslkwargs = sslutil.sslkwargs(ui, mailhost) else: # 'ui' is required by sslutil.wrapsocket() and set by sslkwargs() sslkwargs = {"ui": ui} if smtps: ui.note(_("(using smtps)\n")) s = SMTPS(sslkwargs, local_hostname=local_hostname) elif starttls: s = STARTTLS(sslkwargs, local_hostname=local_hostname) else: s = smtplib.SMTP(local_hostname=local_hostname) if smtps: defaultport = 465 else: defaultport = 25 mailport = util.getport(ui.config("smtp", "port", defaultport)) ui.note(_("sending mail: smtp host %s, port %s\n") % (mailhost, mailport)) s.connect(host=mailhost, port=mailport) if starttls: ui.note(_("(using starttls)\n")) s.ehlo() s.starttls() s.ehlo() if (starttls or smtps) and verifycert: ui.note(_("(verifying remote certificate)\n")) sslutil.validator(ui, mailhost)(s.sock, verifycert == "strict") username = ui.config("smtp", "username") password = ui.config("smtp", "password") if username and not password: password = ui.getpass() if username and password: ui.note(_("(authenticating to mail server as %s)\n") % (username)) try: s.login(username, password) except smtplib.SMTPException as inst: raise util.Abort(inst) def send(sender, recipients, msg): try: return s.sendmail(sender, recipients, msg) except smtplib.SMTPRecipientsRefused as inst: recipients = [r[1] for r in inst.recipients.values()] raise util.Abort("\n" + "\n".join(recipients)) except smtplib.SMTPException as inst: raise util.Abort(inst) return send