[Python-modules-commits] [pyfg] 01/05: New upstream release.

Vincent Bernat bernat at moszumanska.debian.org
Sun Nov 12 19:05:17 UTC 2017


This is an automated email from the git hooks/post-receive script.

bernat pushed a commit to annotated tag debian/0.50-1
in repository pyfg.

commit 938df4a55b19322e00f1bd5e519776b8a873380c
Author: Vincent Bernat <bernat at debian.org>
Date:   Sun Nov 12 19:58:01 2017 +0100

    New upstream release.
---
 PKG-INFO                  |   4 +-
 pyFG/__init__.py          |   6 ++-
 pyFG/ansible_helpers.py   |   2 +-
 pyFG/forticonfig.py       |  14 +++----
 pyFG/fortios.py           | 100 ++++++++++++++++++++++------------------------
 pyFG/py23_compat.py       |  15 +++++++
 pyfg.egg-info/PKG-INFO    |   4 +-
 pyfg.egg-info/SOURCES.txt |   3 +-
 pyfg.egg-info/pbr.json    |   1 -
 setup.cfg                 |   8 ++++
 setup.py                  |  16 ++++----
 11 files changed, 96 insertions(+), 77 deletions(-)

diff --git a/PKG-INFO b/PKG-INFO
index c18fd87..49df9eb 100644
--- a/PKG-INFO
+++ b/PKG-INFO
@@ -1,10 +1,10 @@
 Metadata-Version: 1.0
 Name: pyfg
-Version: 0.47
+Version: 0.50
 Summary: Python API for fortigate
 Home-page: https://github.com/spotify/pyfg
 Author: XNET
-Author-email: dbarroso at spotify.com
+Author-email: lindblom+pyfg at spotify.com
 License: UNKNOWN
 Description: UNKNOWN
 Platform: UNKNOWN
diff --git a/pyFG/__init__.py b/pyFG/__init__.py
index 8cc7c45..4b51a62 100644
--- a/pyFG/__init__.py
+++ b/pyFG/__init__.py
@@ -1,2 +1,4 @@
-from fortios import FortiOS
-from forticonfig import FortiConfig
+from pyFG.fortios import FortiOS
+from pyFG.forticonfig import FortiConfig
+
+__all__ = ['FortiOS', 'FortiConfig']
diff --git a/pyFG/ansible_helpers.py b/pyFG/ansible_helpers.py
index 6d6c7a5..71ea597 100644
--- a/pyFG/ansible_helpers.py
+++ b/pyFG/ansible_helpers.py
@@ -32,4 +32,4 @@ def set_logging(log_path, log_level):
 def save_text_generator_to_file(file_name, text):
     with open(file_name, "w") as text_file:
         for line in text:
-            text_file.write('%s\n' % line)
\ No newline at end of file
+            text_file.write('%s\n' % line)
diff --git a/pyFG/forticonfig.py b/pyFG/forticonfig.py
index 486e31e..b05c047 100644
--- a/pyFG/forticonfig.py
+++ b/pyFG/forticonfig.py
@@ -1,6 +1,10 @@
+from __future__ import unicode_literals
+
 import re
 from collections import OrderedDict
 
+from pyFG import py23_compat
+
 
 class FortiConfig(object):
     def __init__(self, name='', config_type='', parent=None, vdom=None):
@@ -183,7 +187,7 @@ class FortiConfig(object):
         Yields:
             parameter_name, parameter_value
         """
-        for key, value in self.parameters.iteritems():
+        for key, value in self.parameters.items():
             yield key, value
 
     def iterblocks(self):
@@ -201,7 +205,7 @@ class FortiConfig(object):
         Yields:
             sub_block_name, sub_block_value
         """
-        for key, data in self.sub_blocks.iteritems():
+        for key, data in self.sub_blocks.items():
             yield key, data
 
     def get_parameter_names(self):
@@ -347,7 +351,7 @@ class FortiConfig(object):
         regexp = re.compile('^(config |edit |set |end$|next$)(.*)')
         current_block = self
 
-        if output.__class__ is str or output.__class__ is unicode:
+        if isinstance(output, py23_compat.string_types):
             output = output.splitlines()
 
         for line in output:
@@ -362,17 +366,13 @@ class FortiConfig(object):
                 action = result.group(1).strip()
                 data = result.group(2).strip()
 
-
                 if action == 'config' or action == 'edit':
-
                     data = data.replace('"', '')
-
                     if data not in current_block.get_block_names():
                         config_block = FortiConfig(data, action, current_block)
                         current_block[data] = config_block
                     else:
                         config_block = current_block[data]
-
                     current_block = config_block
                 elif action == 'end' or action == 'next':
                     current_block = current_block.get_parent()
diff --git a/pyFG/fortios.py b/pyFG/fortios.py
index 8fe40be..f5ea250 100644
--- a/pyFG/fortios.py
+++ b/pyFG/fortios.py
@@ -1,12 +1,15 @@
 # coding=utf-8
+from __future__ import unicode_literals
+
+from pyFG.forticonfig import FortiConfig
+from pyFG import py23_compat
+from pyFG import exceptions
 
-from forticonfig import FortiConfig
 
-import exceptions
 import paramiko
-import StringIO
 import re
 import os
+import io
 from difflib import Differ
 
 import logging
@@ -54,7 +57,14 @@ class FortiOS(object):
         self.password = password
         self.keyfile = keyfile
         self.timeout = timeout
-
+        
+        # Set key exchange explcitly to address known fortinet issue
+        paramiko.Transport._preferred_kex = ('diffie-hellman-group14-sha1',
+                                             'diffie-hellman-group-exchange-sha1',
+                                             'diffie-hellman-group-exchange-sha256',
+                                             'diffie-hellman-group1-sha1',
+                                             )
+        
     def open(self):
         """
         Opens the ssh session with the device.
@@ -66,30 +76,29 @@ class FortiOS(object):
         self.ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
 
         cfg = {
-          'hostname': self.hostname, 
-          'timeout': self.timeout,
-          'username': self.username,
-          'password': self.password,
-          'key_filename': self.keyfile
+            'hostname': self.hostname,
+            'timeout': self.timeout,
+            'username': self.username,
+            'password': self.password,
+            'key_filename': self.keyfile
         }
 
         if os.path.exists(os.path.expanduser("~/.ssh/config")):
-          ssh_config = paramiko.SSHConfig()
-          user_config_file = os.path.expanduser("~/.ssh/config")
-          with open(user_config_file) as f:
-            ssh_config.parse(f)
-          f.close()
-
-          host_conf = ssh_config.lookup(self.hostname)
-          if host_conf:
-            if 'proxycommand' in host_conf:
-              cfg['sock'] = paramiko.ProxyCommand(host_conf['proxycommand'])
-            if 'user' in host_conf:
-              cfg['username'] = host_conf['user']
-            if 'identityfile' in host_conf:
-              cfg['key_filename'] = host_conf['identityfile']
-            if 'hostname' in host_conf:
-              cfg['hostname'] = host_conf['hostname']
+            ssh_config = paramiko.SSHConfig()
+            user_config_file = os.path.expanduser("~/.ssh/config")
+            with io.open(user_config_file, 'rt', encoding='utf-8') as f:
+                ssh_config.parse(f)
+
+            host_conf = ssh_config.lookup(self.hostname)
+            if host_conf:
+                if 'proxycommand' in host_conf:
+                    cfg['sock'] = paramiko.ProxyCommand(host_conf['proxycommand'])
+                if 'user' in host_conf:
+                    cfg['username'] = host_conf['user']
+                if 'identityfile' in host_conf:
+                    cfg['key_filename'] = host_conf['identityfile']
+                if 'hostname' in host_conf:
+                    cfg['hostname'] = host_conf['hostname']
 
         self.ssh.connect(**cfg)
 
@@ -101,6 +110,15 @@ class FortiOS(object):
         logger.debug('Closing connection to device %s' % self.hostname)
         self.ssh.close()
 
+    @staticmethod
+    def _read_wrapper(data):
+        """Ensure unicode always returned on read."""
+        # Paramiko (strangely) in PY3 returns an int here.
+        if isinstance(data, int):
+            data = chr(data)
+        # Ensure unicode
+        return py23_compat.text_type(data)
+
     def execute_command(self, command):
         """
         This method will execute the commands on the device without as if you were just connected to it (it will not
@@ -130,33 +148,10 @@ class FortiOS(object):
 
         error = ''
         output = ''
-
         for e in error_chan.read():
-            error += e
-
+            error = error + self._read_wrapper(e)
         for o in output_chan.read():
-            output += o
-
-        '''
-        output = StringIO.StringIO()
-        error = StringIO.StringIO()
-
-        while not chan.exit_status_ready():
-            if chan.recv_stderr_ready():
-                data_err = chan.recv_stderr(1024)
-                while data_err:
-                    error.write(data_err)
-                    data_err = chan.recv_stderr(1024)
-
-            if chan.recv_ready():
-                data = chan.recv(256)
-                while data:
-                    output.write(data)
-                    data = chan.recv(256)
-
-        output = output.getvalue()
-        error = error.getvalue()
-        '''
+            output = output + self._read_wrapper(o)
 
         if len(error) > 0:
             msg = '%s %s:\n%s\n%s' % (err_msg, self.ssh.get_host_keys().keys()[0], command, error)
@@ -165,7 +160,7 @@ class FortiOS(object):
 
         regex = re.compile('Command fail')
         if len(regex.findall(output)) > 0:
-            msg = '%s %s:\n%s\n%s'% (err_msg, self.ssh.get_host_keys().keys()[0], command, output)
+            msg = '%s %s:\n%s\n%s' % (err_msg, self.ssh.get_host_keys().keys()[0], command, output)
             logger.error(msg)
             raise exceptions.CommandExecutionException(msg)
 
@@ -330,7 +325,7 @@ class FortiOS(object):
                 exit_code = -1
                 self.rollback()
 
-            if exit_code < 0 :
+            if exit_code < 0:
                 raise exceptions.FailedCommit(wrong_commands)
 
     def rollback(self):
@@ -363,7 +358,6 @@ class FortiOS(object):
             if result is not None:
                 status_code = result.group(1)
                 command = result.group(2)
-
                 if int(status_code) < 0:
                     wrong_commands.append((status_code, command))
 
diff --git a/pyFG/py23_compat.py b/pyFG/py23_compat.py
new file mode 100644
index 0000000..1c1abea
--- /dev/null
+++ b/pyFG/py23_compat.py
@@ -0,0 +1,15 @@
+"""Simplify Python3 compatibility. Modeled after six behavior for small set of things"""
+from __future__ import print_function
+from __future__ import unicode_literals
+
+import sys
+
+PY2 = sys.version_info[0] == 2
+PY3 = sys.version_info[0] == 3
+
+if sys.version_info[0] == 3:
+    string_types = (str,)
+    text_type = str
+else:
+    string_types = (basestring,)  # noqa
+    text_type = unicode  # noqa
diff --git a/pyfg.egg-info/PKG-INFO b/pyfg.egg-info/PKG-INFO
index c18fd87..49df9eb 100644
--- a/pyfg.egg-info/PKG-INFO
+++ b/pyfg.egg-info/PKG-INFO
@@ -1,10 +1,10 @@
 Metadata-Version: 1.0
 Name: pyfg
-Version: 0.47
+Version: 0.50
 Summary: Python API for fortigate
 Home-page: https://github.com/spotify/pyfg
 Author: XNET
-Author-email: dbarroso at spotify.com
+Author-email: lindblom+pyfg at spotify.com
 License: UNKNOWN
 Description: UNKNOWN
 Platform: UNKNOWN
diff --git a/pyfg.egg-info/SOURCES.txt b/pyfg.egg-info/SOURCES.txt
index 3832d98..d9889b6 100644
--- a/pyfg.egg-info/SOURCES.txt
+++ b/pyfg.egg-info/SOURCES.txt
@@ -1,14 +1,15 @@
 MANIFEST.in
 requirements.txt
+setup.cfg
 setup.py
 pyFG/__init__.py
 pyFG/ansible_helpers.py
 pyFG/exceptions.py
 pyFG/forticonfig.py
 pyFG/fortios.py
+pyFG/py23_compat.py
 pyfg.egg-info/PKG-INFO
 pyfg.egg-info/SOURCES.txt
 pyfg.egg-info/dependency_links.txt
-pyfg.egg-info/pbr.json
 pyfg.egg-info/requires.txt
 pyfg.egg-info/top_level.txt
\ No newline at end of file
diff --git a/pyfg.egg-info/pbr.json b/pyfg.egg-info/pbr.json
deleted file mode 100644
index afeb5e5..0000000
--- a/pyfg.egg-info/pbr.json
+++ /dev/null
@@ -1 +0,0 @@
-{"is_release": true, "git_version": "c652adc"}
\ No newline at end of file
diff --git a/setup.cfg b/setup.cfg
index 861a9f5..b7adbbd 100644
--- a/setup.cfg
+++ b/setup.cfg
@@ -1,3 +1,11 @@
+[pylama]
+linters = mccabe,pep8,pyflakes
+ignore = D203,C901,E501,E265,E122
+skip = build/*,.tox/*,examples/*
+
+[pylama:pep8]
+max_line_length = 100
+
 [egg_info]
 tag_build = 
 tag_date = 0
diff --git a/setup.py b/setup.py
index 1b55700..64f99b7 100644
--- a/setup.py
+++ b/setup.py
@@ -11,13 +11,13 @@ install_reqs = parse_requirements('requirements.txt', session=uuid.uuid1())
 reqs = [str(ir.req) for ir in install_reqs]
 
 setup(
-    name = "pyfg",  
-    version = "0.47",
-    packages = find_packages(),
-    author = "XNET",
-    author_email = "dbarroso at spotify.com",
-    description = "Python API for fortigate",
-    url = "https://github.com/spotify/pyfg",
-    include_package_data = True,
+    name="pyfg",
+    version="0.50",
+    packages=find_packages(),
+    author="XNET",
+    author_email="lindblom+pyfg at spotify.com",
+    description="Python API for fortigate",
+    url="https://github.com/spotify/pyfg",
+    include_package_data=True,
     install_requires=reqs
 )

-- 
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/python-modules/packages/pyfg.git



More information about the Python-modules-commits mailing list