From 9431c014958dac2fdf256835aed0df85a880ab98 Mon Sep 17 00:00:00 2001 From: Valentin Deniaud Date: Tue, 21 Sep 2021 10:18:10 +0200 Subject: [PATCH 34/59] misc: fix consider-using-with pylint error (#56982) --- src/authentic2/csv_import.py | 2 ++ .../management/commands/export_site.py | 8 ++--- src/authentic2/nonce/utils.py | 1 + src/authentic2/saml/x509utils.py | 29 ++++++++----------- src/authentic2_auth_fc/utils.py | 6 ++-- 5 files changed, 22 insertions(+), 24 deletions(-) diff --git a/src/authentic2/csv_import.py b/src/authentic2/csv_import.py index 48467d43..6894333a 100644 --- a/src/authentic2/csv_import.py +++ b/src/authentic2/csv_import.py @@ -113,6 +113,7 @@ class CsvImporter: input_fd = io.StringIO(fd_or_str) elif not hasattr(fd_or_str, 'read1'): try: + # pylint: disable=consider-using-with input_fd = open(fd_or_str.fileno(), closefd=False, mode='rb') except Exception: try: @@ -151,6 +152,7 @@ class CsvImporter: input_fd.seek(0) if not hasattr(input_fd, 'readable'): + # pylint: disable=consider-using-with input_fd = open(input_fd.fileno(), 'rb', closefd=False) return io.TextIOWrapper(input_fd, encoding=encoding) diff --git a/src/authentic2/management/commands/export_site.py b/src/authentic2/management/commands/export_site.py index 2f075e07..8077e252 100644 --- a/src/authentic2/management/commands/export_site.py +++ b/src/authentic2/management/commands/export_site.py @@ -32,9 +32,7 @@ class Command(BaseCommand): def handle(self, *args, **options): if options['output']: - output, close = open(options['output'], 'w'), True + with open(options['output'], 'w') as f: + json.dump(export_site(), f, indent=4) else: - output, close = sys.stdout, False - json.dump(export_site(), output, indent=4) - if close: - output.close() + json.dump(export_site(), sys.stdout, indent=4) diff --git a/src/authentic2/nonce/utils.py b/src/authentic2/nonce/utils.py index 3e8f9373..0a1b4b82 100644 --- a/src/authentic2/nonce/utils.py +++ b/src/authentic2/nonce/utils.py @@ -79,6 +79,7 @@ def accept_nonce_file_storage(path, now, value, context=None, not_on_or_after=No # not too old, the nonce is unacceptable return False + # pylint: disable=consider-using-with temp_file = tempfile.NamedTemporaryFile(dir=path, delete=False) temp_file.close() diff --git a/src/authentic2/saml/x509utils.py b/src/authentic2/saml/x509utils.py index 8196e7d8..3fe5adf0 100644 --- a/src/authentic2/saml/x509utils.py +++ b/src/authentic2/saml/x509utils.py @@ -41,11 +41,11 @@ def _call_openssl(args): Return a tuple made of the return code and the stdout output """ try: - process = subprocess.Popen( + with subprocess.Popen( args=[_openssl] + args, stdout=subprocess.PIPE, stderr=subprocess.STDOUT, universal_newlines=True - ) - output = process.communicate()[0] - return process.returncode, output + ) as process: + output = process.communicate()[0] + return process.returncode, output except OSError: return 1, None @@ -57,9 +57,9 @@ def check_key_pair_consistency(publickey=None, privatekey=None): if not publickey or not privatekey: return None - privatekey_file = tempfile.NamedTemporaryFile(mode='w') - publickey_file = tempfile.NamedTemporaryFile(mode='w') - with privatekey_file, publickey_file: + with tempfile.NamedTemporaryFile(mode='w') as privatekey_file, tempfile.NamedTemporaryFile( + mode='w' + ) as publickey_file: privatekey_file.write(privatekey) privatekey_file.flush() @@ -87,10 +87,9 @@ def check_key_pair_consistency(publickey=None, privatekey=None): def generate_rsa_keypair(numbits=1024): """Generate simple RSA public and private key files""" - privatekey_file = tempfile.NamedTemporaryFile(mode='r') - publickey_file = tempfile.NamedTemporaryFile(mode='r') - - with privatekey_file, publickey_file: + with tempfile.NamedTemporaryFile(mode='r') as privatekey_file, tempfile.NamedTemporaryFile( + mode='r' + ) as publickey_file: rc1, _ = _call_openssl(['genrsa', '-out', privatekey_file.name, '-passout', 'pass:', str(numbits)]) if rc1 != 0: raise Exception('Failed to generate a key') @@ -101,9 +100,7 @@ def generate_rsa_keypair(numbits=1024): def get_rsa_public_key_modulus(publickey): - publickey_file = tempfile.NamedTemporaryFile(mode='w') - - with publickey_file: + with tempfile.NamedTemporaryFile(mode='w') as publickey_file: publickey_file.write(publickey) publickey_file.flush() @@ -124,9 +121,7 @@ def get_rsa_public_key_modulus(publickey): def get_rsa_public_key_exponent(publickey): - publickey_file = tempfile.NamedTemporaryFile(mode='w') - - with publickey_file: + with tempfile.NamedTemporaryFile(mode='w') as publickey_file: publickey_file.write(publickey) publickey_file.flush() diff --git a/src/authentic2_auth_fc/utils.py b/src/authentic2_auth_fc/utils.py index d73a6c58..e523748a 100644 --- a/src/authentic2_auth_fc/utils.py +++ b/src/authentic2_auth_fc/utils.py @@ -101,7 +101,8 @@ _insee_communes = None def resolve_insee_commune(insee_code): global _insee_communes if not _insee_communes: - _insee_communes = json.load(open(os.path.join(os.path.dirname(__file__), 'insee-communes.json'))) + with open(os.path.join(os.path.dirname(__file__), 'insee-communes.json')) as f: + _insee_communes = json.load(f) return _insee_communes.get(insee_code, _('Unknown INSEE code')) @@ -112,7 +113,8 @@ def resolve_insee_country(insee_code): global _insee_countries if not _insee_countries: - _insee_countries = json.load(open(os.path.join(os.path.dirname(__file__), 'insee-countries.json'))) + with open(os.path.join(os.path.dirname(__file__), 'insee-countries.json')) as f: + _insee_countries = json.load(f) return _insee_countries.get(insee_code, _('Unknown INSEE code')) -- 2.30.2