Commit f92c8c99 authored by godog's avatar godog

run 2to3 and fix import ConfigParser

parent f10dc3c2
......@@ -16,7 +16,7 @@ log = logging.getLogger(__name__)
class _CAFiles(object):
def __init__(self, basedir, **attrs):
for key, value in attrs.items():
for key, value in list(attrs.items()):
setattr(self, key, os.path.join(basedir, value))
......@@ -51,7 +51,7 @@ class CA(object):
try:
fcntl.lockf(self._lockfd, fcntl.LOCK_EX | fcntl.LOCK_NB)
break
except IOError, e:
except IOError as e:
if e.errno in (errno.EACCES, errno.EAGAIN):
n -= 1
if n == 0:
......@@ -73,7 +73,7 @@ class CA(object):
self._unlock()
def create(self):
old_umask = os.umask(077)
old_umask = os.umask(0o77)
for pathext in ('', 'conf', 'public', 'public/certs',
'public/crl', 'private', 'newcerts'):
......@@ -133,9 +133,9 @@ class CA(object):
os.path.join(self.basedir, 'public/certs'),
self.files.public_key):
if os.path.isdir(path):
os.chmod(path, 0755)
os.chmod(path, 0o755)
else:
os.chmod(path, 0644)
os.chmod(path, 0o644)
def gencrl(self):
log.info('generating CRL')
......@@ -153,7 +153,7 @@ class CA(object):
'crl', '-inform', 'PEM', '-outform', 'DER',
'-in', tmpf, '-out', self.files.crl)
os.remove(tmpf)
os.chmod(self.files.crl, 0644)
os.chmod(self.files.crl, 0o644)
def revoke(self, cert):
log.info('revoking certificate %s', cert.name)
......@@ -205,7 +205,7 @@ class CA(object):
'req', '-new', '-keyout', cert.private_key_file,
'-' + self.config['signature_algorithm'],
'-nodes', '-out', csr_file)
os.chmod(cert.private_key_file, 0600)
os.chmod(cert.private_key_file, 0o600)
openssl_wrap.run_with_config(
self.basedir, conf_file, self._getpw(),
'ca', '-days', conf['days'],
......
import ConfigParser
try:
import ConfigParser as configparser
except ImportError:
# python 3 has ConfigParser renamed
import configparser
import os
from cam import cert
from cam import ca
......@@ -9,7 +13,7 @@ class ConfigError(Exception):
def read_config(filename):
parser = ConfigParser.ConfigParser()
parser = configparser.ConfigParser()
if not parser.read(filename):
raise ConfigError('File not found: %s' % filename)
root_dir = os.path.dirname(os.path.abspath(filename))
......
......@@ -64,7 +64,7 @@ def cmd_init(global_config, ca, certs, args):
def cmd_gen(global_config, ca, certs, args):
if len(args) < 1:
print 'Nothing to do.'
print('Nothing to do.')
for tag in args:
ca.generate(find_cert(certs, tag))
......@@ -75,11 +75,11 @@ def cmd_gencrl(global_config, ca, certs, args):
def cmd_files(global_config, ca, certs, args):
if len(args) < 1:
print 'Nothing to do.'
print('Nothing to do.')
for tag in args:
c = find_cert(certs, tag)
print c.public_key_file
print c.private_key_file
print(c.public_key_file)
print(c.private_key_file)
def cmd_list(global_config, ca, certs, args):
......@@ -94,19 +94,19 @@ def cmd_list(global_config, ca, certs, args):
if expiry < now:
state = 'EXPIRED'
expiry_str = time.strftime('%Y/%m/%d', time.gmtime(expiry))
print cert.name, cert.cn, state, expiry_str
print(cert.name, cert.cn, state, expiry_str)
def cmd_verify(global_config, ca, certs, args):
if len(args) < 1:
print 'Nothing to do.'
print('Nothing to do.')
failed = False
for path in args:
if not ca.verify(path):
print '%s: FAIL' % path
print('%s: FAIL' % path)
failed = True
else:
print '%s: OK' % path
print('%s: OK' % path)
return failed
......@@ -114,9 +114,9 @@ def cmd_fingerprint(global_config, ca, certs, args):
if len(args) > 0:
certs = [find_cert(certs, x) for x in args]
for cert in certs:
print cert.name, cert.cn
print ' SHA1:', cert.get_fingerprint('sha1')
print ' MD5:', cert.get_fingerprint('md5')
print(cert.name, cert.cn)
print(' SHA1:', cert.get_fingerprint('sha1'))
print(' MD5:', cert.get_fingerprint('md5'))
def cmd_check(global_config, ca, certs, args):
......@@ -126,7 +126,7 @@ def cmd_check(global_config, ca, certs, args):
for cert in certs:
exp = cert.get_expiration_date()
if exp and (exp - now) < warning_time:
print '%s (%s) is about to expire.' % (cert.name, cert.cn)
print('%s (%s) is about to expire.' % (cert.name, cert.cn))
retval = 1
return retval
......
......@@ -50,36 +50,36 @@ class CertTest(unittest.TestCase):
def test_get_fingerprint(self):
crt = cert.Cert(self.ca, 'test', {'cn': 'test.com'})
md5 = crt.get_fingerprint('md5')
self.assertEquals(TEST_MD5, md5)
self.assertEqual(TEST_MD5, md5)
sha1 = crt.get_fingerprint('sha1')
self.assertEquals(TEST_SHA1, sha1)
self.assertEqual(TEST_SHA1, sha1)
def test_get_fingerprint_nonexist(self):
crt = cert.Cert(self.ca, 'test-nonexist', {'cn': 'test.com'})
result = crt.get_fingerprint('md5')
self.assertEquals(None, result)
self.assertEqual(None, result)
def test_cn_in_alt_names(self):
crt = cert.Cert(self.ca, 'test', {'cn': 'test.com',
'alt_names': 'test2.com'})
self.assert_('test.com' in crt.alt_names)
self.assertTrue('test.com' in crt.alt_names)
def test_get_expiration_date(self):
crt = cert.Cert(self.ca, 'test', {'cn': 'test.com'})
exp = crt.get_expiration_date()
self.assertEquals(TEST_EXPIRY, exp)
self.assertEqual(TEST_EXPIRY, exp)
def test_get_expiration_date_nonexist(self):
crt = cert.Cert(self.ca, 'test-nonexist', {'cn': 'test.com'})
exp = crt.get_expiration_date()
self.assertEquals(None, exp)
self.assertEqual(None, exp)
def test_expired(self):
crt = cert.Cert(self.ca, 'test', {'cn': 'test.com'})
exp = crt.get_expiration_date()
now = time.time()
is_expired = (exp > now)
self.assertEquals(is_expired, crt.expired())
self.assertEqual(is_expired, crt.expired())
if __name__ == '__main__':
......
......@@ -38,8 +38,8 @@ cn = test.com
self.mox.ReplayAll()
global_config, ca_obj, certs = config.read_config(cf_file)
self.assertEquals('ca', ca_obj)
self.assertEquals(['cert1'], certs)
self.assertEqual('ca', ca_obj)
self.assertEqual(['cert1'], certs)
def test_read_config_nonexist(self):
def f():
......
......@@ -37,12 +37,12 @@ cn = www.test.org
sys.argv = ['cam', '--config=%s' % self.cfgfile] + list(args)
try:
return main.main()
except SystemExit, e:
except SystemExit as e:
return e.code
def test_init_and_sanity_check(self):
self.assertEquals(None, self._run('init'))
self.assertEquals(None, self._run('gen', 'web'))
self.assertEqual(None, self._run('init'))
self.assertEqual(None, self._run('gen', 'web'))
ca_file = os.path.join(self.tmpdir, 'public/ca.pem')
crt_file = os.path.join(self.tmpdir, 'public/certs/web.pem')
......@@ -59,8 +59,8 @@ cn = www.test.org
stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
output = pipe.communicate()[0]
result = pipe.returncode
self.assertEquals(0, result)
self.assertEqual(0, result)
print output
print(output)
self.assertTrue('error ' not in output)
......@@ -31,7 +31,7 @@ class OpensslWrapTest(unittest.TestCase):
).AndReturn(pipe_stub)
self.mox.ReplayAll()
result = openssl_wrap.run('test')
self.assertEquals('output', result)
self.assertEqual('output', result)
def test_run_fails(self):
self.mox.StubOutWithMock(subprocess, 'Popen', use_mock_anything=True)
......
......@@ -22,9 +22,9 @@ class UtilsTest(unittest.TestCase):
tfd.close()
of = os.path.join(self.tmpdir, 'test.out')
utils.render(of, 'test', {'sub':'TEST'})
self.assert_(os.path.exists(of))
self.assertTrue(os.path.exists(of))
output = open(of, 'r').read()
self.assertEquals('this is a TEST\n', output)
self.assertEqual('this is a TEST\n', output)
def test_parse_bool(self):
self.assertTrue(utils.parse_bool('y'))
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment