Skip to content

Commit

Permalink
RF: misc pylint-based minor changes
Browse files Browse the repository at this point in the history
  • Loading branch information
jeremygray committed Apr 27, 2014
1 parent 590287d commit 0e69ccd
Show file tree
Hide file tree
Showing 5 changed files with 72 additions and 49 deletions.
110 changes: 63 additions & 47 deletions pyfilesec/__init__.py
Expand Up @@ -249,40 +249,46 @@ def __init__(self, defaults=None, test_keys=None):
def keys(self):
return list(self._functions.keys())

@staticmethod
def _test(new_functions, test_keys=None):
"""Perform self.test: decrypt(encrypt())"""
enc_kwargs, dec_kwargs = test_keys
test_co = PFSCodecRegistry({})
# hack: splice in to bypass .register:
test_co._functions = new_functions
test_dir = mkdtemp()
test_file = os.path.join(test_dir, 'codec_enc_dec.txt')
test_datum = printable_pwd(64)
recovered = None # i.e., = test will fail unless proven otherwise
with open(test_file, write_mode) as fd:
fd.write(test_datum)
try:
sf = SecFile(test_file, codec=test_co)
sf.encrypt(keep=True, **enc_kwargs)
sf.decrypt(**dec_kwargs)
except:
import traceback
traceback.print_exc()
finally:
if sf.file:
recovered = open(sf.file, read_mode).read()
shutil.rmtree(test_dir, ignore_errors=True)
return recovered == test_datum

def register(self, new_functions, test_keys=None):
"""Validate and add a new codec functions to the registry.
Typically one registers encrypt and decrypt functions in pairs. Its
Typically one registers encrypt and decrypt functions in pairs. It is
possible to register only a decrypt function, to support "read only"
(decrypt) use of a codec.
If ``test_keys`` is provided, an
encrypt-decrypt self-test validation must passbefore registration can
proceed. ``test_keys`` should be a tuple of (enc_kwargs, dec_kwargs)
encrypt-decrypt self-test validation must pass before registration can
proceed. ``test_keys`` should be a tuple of (enc_kwargs, dec_kwargs)
that will be passed to the respective functions being registered.
"""
if test_keys:
enc_kwargs, dec_kwargs = test_keys
test_co = PFSCodecRegistry({})
# hack: splice in to bypass .register:
test_co._functions = new_functions
test_dir = mkdtemp()
test_file = os.path.join(test_dir, 'codec_enc_dec.txt')
test_datum = printable_pwd(64)
with open(test_file, write_mode) as fd:
fd.write(test_datum)
try:
sf = SecFile(test_file, codec=test_co)
sf.encrypt(keep=True, **enc_kwargs)
sf.decrypt(**dec_kwargs)
except:
import traceback
traceback.print_exc()
finally:
if sf.file:
recovered = open(sf.file, read_mode).read()
shutil.rmtree(test_dir, ignore_errors=True)
assert recovered == test_datum # 'Codec reg: enc-dec failed'
assert self._test(new_functions, test_keys) # self-test failed

for key, fxn in list(new_functions.items()):
try:
Expand Down Expand Up @@ -411,10 +417,10 @@ def set_file(self, infile):
'PUBLIC KEY' in open(infile, read_mode).read()):
logging.warning('infile looks like a public key')

def set_file_time(self, new_time=None):
"""Obscure the time-stamp on the underlying file system.
"""
fatal('file time-stamp removal not supported yet', NotImplementedError)
#def set_file_time(self, new_time=None):
# """Obscure the time-stamp on the underlying file system.
# """
# fatal('file time-stamp removal not supported yet', NotImplementedError)

@property
def file(self):
Expand Down Expand Up @@ -497,7 +503,7 @@ def is_in_writeable_dir(self):
finally:
try:
os.unlink(test_name)
except:
except OSError:
pass
return writeable

Expand Down Expand Up @@ -618,7 +624,8 @@ def is_tracked(self):
self._get_git_info(self.file),
self._get_hg_info(self.file)])

def _get_git_info(self, path, git='git'):
@staticmethod
def _get_git_info(path, git='git'):
"""Report whether a directory or file is tracked in a git repo.
Can test any generic filename, not just current file::
Expand All @@ -642,7 +649,8 @@ def _get_git_info(self, path, git='git'):
logging.debug('path %s tracked in git repo: %s' % (path, is_tracked))
return is_tracked

def _get_svn_info(self, path):
@staticmethod
def _get_svn_info(path):
"""Tries to discover if a file is tracked under svn.
"""
if not path or not exists(path):
Expand All @@ -653,7 +661,8 @@ def _get_svn_info(self, path):
logging.debug('path %s tracked in svn repo: %s' % (path, has_svn_dir))
return has_svn_dir

def _get_hg_info(self, path):
@staticmethod
def _get_hg_info(path):
"""Tries to discover if a file is tracked under mercurial.
"""
if not path or not exists(path):
Expand Down Expand Up @@ -983,7 +992,7 @@ def encrypt(self, pub=None, meta=True, date=True, keep=False,
set_umask()
name = 'encrypt'
self.result = {'method': name, 'status': 'started'}
self._require_file(self.is_in_writeable_dir) # enc file ok, warn
self._require_file(self.is_in_writeable_dir) # enc file ok, warn below
self.rsakeys.update(pub=pub, req=NEED_PUBK)
logging.debug(name + 'start')
if self.is_encrypted:
Expand All @@ -994,6 +1003,7 @@ def encrypt(self, pub=None, meta=True, date=True, keep=False,
fatal(name + ': meta must be True, False, or dict', AttributeError)
if not keep in [True, False]:
fatal(name + ": bad value for 'keep' parameter")

# Do the encryption, using a registered `encMethod`:
ENCRYPT_FXN = self.codec.get_function(enc_method)
set_umask() # redundant
Expand Down Expand Up @@ -1049,14 +1059,15 @@ def encrypt(self, pub=None, meta=True, date=True, keep=False,
': retaining original file, encryption did not succeed')

unset_umask()
self.set_file(arch_enc.name) # likely off in some situations
self.set_file(arch_enc.name) # likely off in some situations (??)
self.result.update({'status': 'good', 'cipher_text': self.file,
'meta': meta})
if hmac_key:
self.result.update({'hmac': True})
return self

def _make_metadata(self, datafile, data_enc, pub, enc_method,
@staticmethod
def _make_metadata(datafile, data_enc, pub, enc_method,
date=True, hmac=None, note=None):
"""Return info about an encryption context, as {date-now: {info}} dict.
Expand Down Expand Up @@ -1423,7 +1434,7 @@ def rename(self, new_name):


class SecFileArchive(_SecFileBase):
"""Class for working with a cipher_text archive file (= \*.enc).
"""Class for working with a cipher_text archive file (= ``.enc``).
Used transparently by SecFile as needed; typically there's no need to work
directly with a SecFileArchive.
Expand Down Expand Up @@ -1642,7 +1653,8 @@ def test(self):
)
return self

def sniff(self, key):
@staticmethod
def sniff(key):
"""Inspects the file ``key``, returns information.
Example return values:
Expand Down Expand Up @@ -1701,7 +1713,8 @@ def update(self, pub=None, priv=None, pphr=None, req=0):
self._update_pphr(pphr)
if self.priv_requires_pphr:
req |= NEED_PPHR
req and self.require(req)
if req:
self.require(req)

def _update_pub(self, pub=None):
"""Get pub from self or from param, set as needed
Expand Down Expand Up @@ -1782,7 +1795,8 @@ class GenRSA(object):
def __init__(self):
pass

def demo_rsa_keys(self, folder=''):
@staticmethod
def demo_rsa_keys(folder=''):
pub = os.path.join(folder, 'pubkey_demo_only')
pubkey = """ !!! DEMO public key do not use; for testing only!!!
Expand Down Expand Up @@ -1830,7 +1844,8 @@ def demo_rsa_keys(self, folder=''):
fd.write(pp)
return _abspath(pub), _abspath(priv), _abspath(pphr)

def check_entropy(self):
@staticmethod
def check_entropy():
"""Basic query for some indication that entropy is available.
"""
e = '(unknown)'
Expand Down Expand Up @@ -1880,26 +1895,27 @@ def generate(self, pub='pub.pem', priv='priv.pem', pphr=None, bits=4096):
fatal('new keys failed to validate; removing them', RuntimeError)
return _abspath(pub), _abspath(priv)

def _cleanup(self, msg, pub='', priv='', pphr=None):
@staticmethod
def _cleanup(msg, pub='', priv='', pphr=None):
print(msg)
try:
SecFile(priv).destroy()
except:
try:
os.unlink(priv)
except:
except OSError:
pass
if pphr:
try:
SecFile(pphr).destroy()
except:
try:
os.unlink(pphr)
except:
except OSError:
pass
try:
os.unlink(pub)
except:
except OSError:
pass
return None, None, None

Expand Down Expand Up @@ -2369,14 +2385,14 @@ def hmac_sha256(key, filename):


def isinstance_basestring23(duck):
# placeholder for 2to3
#return isinstance(duck, basestring)
# idea is py2/py3 compatible: return isinstance(duck, basestring)
try:
duck + 'quack'
duck.endswith('quack')
return True
except:
return False
else:
return True


def printable_pwd(nbits=256, prefix='#'):
Expand Down
2 changes: 2 additions & 0 deletions pyfilesec/lib/_getpass.py
Expand Up @@ -13,6 +13,8 @@
# Guido van Rossum (Windows support and cleanup)
# Gregory P. Smith (tty support & GetPassWarning)

# pylint: skip-file

import os, sys, warnings
__all__ = ["getpass","getuser","GetPassWarning"]

Expand Down
3 changes: 3 additions & 0 deletions pyfilesec/lib/_pyperclip.py
@@ -1,4 +1,7 @@
#!/usr/bin/env python

# pylint: skip-file

from __future__ import print_function

# JRG: from https://github.com/gfxmonk/pyperclip/ Aug 2013
Expand Down
2 changes: 2 additions & 0 deletions pyfilesec/lib/which.py
@@ -1,5 +1,7 @@
#!/usr/bin/env python

# pylint: skip-file

# Copyright (c) 2002-2007 ActiveState Software Inc.
# See LICENSE.txt for license details.
# Author:
Expand Down
4 changes: 2 additions & 2 deletions tests/test_other.py
Expand Up @@ -151,8 +151,8 @@ def test_SecFileBase(self):
sf.basename
with pytest.raises(AttributeError):
sf.set_file(1)
with pytest.raises(NotImplementedError):
sf.set_file_time(0)
#with pytest.raises(NotImplementedError):
# sf.set_file_time(0)
sf.snippet
with pytest.raises(FileNotEncryptedError):
sf._require_enc_file()
Expand Down

0 comments on commit 0e69ccd

Please sign in to comment.