From 70326da86da332d19ba5d21725f1d6ee13b41d98 Mon Sep 17 00:00:00 2001 From: "Robin H. Johnson" Date: Sat, 15 Dec 2007 05:36:07 -0800 Subject: Pylint cleanup. --- gitosis/ssh.py | 39 +++++++++++++++++++++++++++------------ 1 file changed, 27 insertions(+), 12 deletions(-) diff --git a/gitosis/ssh.py b/gitosis/ssh.py index a315a5c..f280b8b 100644 --- a/gitosis/ssh.py +++ b/gitosis/ssh.py @@ -1,11 +1,19 @@ +""" +Gitosis code to handle SSH public keys. +""" import os, errno, re import logging +# C0103 - 'log' is a special name +# pylint: disable-msg=C0103 log = logging.getLogger('gitosis.ssh') _ACCEPTABLE_USER_RE = re.compile(r'^[a-zA-Z][a-zA-Z0-9_.-]*(@[a-zA-Z][a-zA-Z0-9.-]*)?$') def isSafeUsername(user): + """ + Is the username safe to use a a filename? + """ match = _ACCEPTABLE_USER_RE.match(user) return (match is not None) @@ -25,25 +33,29 @@ def readKeys(keydir): continue path = os.path.join(keydir, filename) - f = file(path) - for line in f: + fp = file(path) + for line in fp: line = line.rstrip('\n') yield (basename, line) - f.close() + fp.close() COMMENT = '### autogenerated by gitosis, DO NOT EDIT' def generateAuthorizedKeys(keys): - TEMPLATE=('command="gitosis-serve %(user)s",no-port-forwarding,' - +'no-X11-forwarding,no-agent-forwarding,no-pty %(key)s') + """ + Genarate the lines for the Gitosis ~/.ssh/authorized_keys. + """ + TEMPLATE = ('command="gitosis-serve %(user)s",no-port-forwarding,' + +'no-X11-forwarding,no-agent-forwarding,no-pty %(key)s') yield COMMENT for (user, key) in keys: yield TEMPLATE % dict(user=user, key=key) -_COMMAND_RE = re.compile('^command="(/[^ "]+/)?gitosis-serve [^"]+",no-port-forw' - +'arding,no-X11-forwarding,no-agent-forwardi' - +'ng,no-pty .*') +_COMMAND_RE = re.compile('^command="(/[^ "]+/)?gitosis-serve [^"]+",' + +'no-port-forwarding,no-X11-forwarding,' + +'no-agent-forwarding,no-pty' + +' .*') def filterAuthorizedKeys(fp): """ @@ -61,11 +73,14 @@ def filterAuthorizedKeys(fp): yield line def writeAuthorizedKeys(path, keydir): + """ + Update the Gitosis ~/.ssh/authorized_keys for the new Gitosis SSH key data. + """ tmp = '%s.%d.tmp' % (path, os.getpid()) try: in_ = file(path) - except IOError, e: - if e.errno == errno.ENOENT: + except IOError, ex: + if ex.errno == errno.ENOENT: in_ = None else: raise @@ -75,11 +90,11 @@ def writeAuthorizedKeys(path, keydir): try: if in_ is not None: for line in filterAuthorizedKeys(in_): - print >>out, line + print >> out, line keygen = readKeys(keydir) for line in generateAuthorizedKeys(keygen): - print >>out, line + print >> out, line os.fsync(out) finally: -- cgit v1.2.3