Versuchen Sie, die cloudmonkey-CLI in python3 -1 zu konvertieren

Leider ist Python 2 auch im CLI-Tool CloudStack cloudmonkey CLI verfügbar, das AWS und Cloudn als APIs verfügbar macht. Lassen Sie es also mit Python3 zum Lernen funktionieren.

** Noch nicht erfolgreich, wird als Memo verwendet. ** ** **

Python 3-Konvertierung

Nach ein wenig Recherche scheint es im Allgemeinen zwei Muster zu geben: "Polyglot", das weiterhin mit Python2 funktioniert, und "Port", das Python2 danach nicht mehr unterstützt.

Richtung Ich benutze kein Python2, also benutze ich ** Port **.

Dokumentation für Python 3

diveintopython

Python 3-Konvertierungstool

Es scheint, dass der Übergang von 2 zu 3 üblich ist, und ich habe schnell einige Werkzeuge gefunden. 2to3 Futurize Six

Arbeitsgeschichte

Erstellen Sie ein Arbeitsverzeichnis und wechseln Sie dorthin

$ mkdir porting
$ cd porting

Bereiten Sie eine Arbeitsumgebung vor

$ pyvenv ven
$ source ven/bin/activate

Für die Dokumentation sind die folgenden Pakete erforderlich. Installieren Sie sie daher

readline
requests
Pygments
prettytable
 
argcomplete
$ pip install --upgrade readline requests Pygments prettytable argcomplete

$ pip freeze

argcomplete==1.4.1 prettytable==0.7.2 Pygments==2.1.3 readline==6.2.4.1 requests==2.11.1


Installieren Sie future, um sie und den Port hinzuzufügen
$ pip install future

##Klonen Sie den Code und bewegen Sie sich dorthin
$ git clone https://github.com/apache/cloudstack-cloudmonkey.git
cd cloudstack-cloudmonkey

Der Inhalt ist so

```sh
$ ls

CHANGES.md        LICENSE           NOTICE            cloudmonkey       docs              setup.cfg
Dockerfile        Makefile          README.md         config.docker     performrelease.sh setup.py

##Versuchen Sie sofort zu installieren

$ python setup.py develop
  File "setup.py", line 50
    print "If you're upgrading, run the following to enable parameter completion:"
                                                                                 ^
SyntaxError: Missing parentheses in call to 'print'

Nun, es wird scheitern. Ich werde das Dokument später noch einmal lesen, futurisieren--stage1 futurize --Es scheint, dass wenn Sie Stage2 ausführen, es in Python3-Code umgeschrieben wird. Richten Sie es so ein, dass es zuerst installiert werden kann.Konvertieren Sie py in pytho3-Code

$ futurize --stage1 -w setup.py

RefactoringTool: Skipping optional fixer: idioms
RefactoringTool: Skipping optional fixer: ws_comma
RefactoringTool: Refactored setup.py
--- setup.py   	(original)
+++ setup.py   	(refactored)
@@ -1,3 +1,4 @@
+from __future__ import print_function
 # Licensed to the Apache Software Foundation (ASF) under one
 # or more contributor license agreements.  See the NOTICE file
 # distributed with this work for additional information
@@ -47,10 +48,10 @@
         requires.append('readline')

 # Upgrade notes for 5.3.0
-print "If you're upgrading, run the following to enable parameter completion:"
-print "  cloudmonkey sync"
-print "  cloudmonkey set paramcompletion true"
-print "Parameter completion may fail, if the above is not run!"
+print("If you're upgrading, run the following to enable parameter completion:")
+print("  cloudmonkey sync")
+print("  cloudmonkey set paramcompletion true")
+print("Parameter completion may fail, if the above is not run!")

 setup(
     name = 'cloudmonkey',
RefactoringTool: Files that were modified:
RefactoringTool: setup.py

$ futurize --stage2 -w setup.py

RefactoringTool: Refactored setup.py
--- setup.py   	(original)
+++ setup.py   	(refactored)
@@ -1,4 +1,5 @@
 from __future__ import print_function
+from builtins import str
 # Licensed to the Apache Software Foundation (ASF) under one
 # or more contributor license agreements.  See the NOTICE file
 # distributed with this work for additional information
RefactoringTool: Files that were modified:
RefactoringTool: setup.py

Ich habe die Arbeit beendet und die Installation ausgeführt $ python setup.py develop

Traceback (most recent call last):
  File "setup.py", line 29, in <module>
    from cloudmonkey import __version__, __description__
  File "/Users/kentaro/porting/cloudstack-cloudmonkey/cloudmonkey/__init__.py", line 22
    except ImportError, e:
                      ^
SyntaxError: invalid syntax

init.py bekam einen Fehler und endete diesmal__init__.Versuchen Sie, py zu konvertieren. $ futurize --stage1 -w cloudmonkey/init.py

RefactoringTool: Skipping optional fixer: idioms
RefactoringTool: Skipping optional fixer: ws_comma
RefactoringTool: Refactored cloudmonkey/__init__.py
--- cloudmonkey/__init__.py    	(original)
+++ cloudmonkey/__init__.py    	(refactored)
@@ -1,3 +1,5 @@
+from __future__ import print_function
+from __future__ import absolute_import
 # Licensed to the Apache Software Foundation (ASF) under one
 # or more contributor license agreements.  See the NOTICE file
 # distributed with this work for additional information
@@ -16,8 +18,8 @@
 # under the License.

 try:
-    from config import __version__, __description__
-    from config import __maintainer__, __maintaineremail__
-    from config import __project__, __projecturl__, __projectemail__
-except ImportError, e:
-    print e
+    from .config import __version__, __description__
+    from .config import __maintainer__, __maintaineremail__
+    from .config import __project__, __projecturl__, __projectemail__
+except ImportError as e:
+    print(e)
RefactoringTool: Files that were modified:
RefactoringTool: cloudmonkey/__init__.py

$ futurize --stage2 -w cloudmonkey/init.py

RefactoringTool: No files need to be modified.

Wenn ich versuche, es erneut zu installieren. $ python setup.py develop

Traceback (most recent call last):
  File "setup.py", line 29, in <module>
    from cloudmonkey import __version__, __description__
  File "/Users/kentaro/porting/cloudstack-cloudmonkey/cloudmonkey/__init__.py", line 21, in <module>
    from .config import __version__, __description__
  File "/Users/kentaro/porting/cloudstack-cloudmonkey/cloudmonkey/config.py", line 33
    except ImportError, e:
                      ^
SyntaxError: invalid syntax

Diesmal config.py schlägt fehl, wird beendet, config.Versuchen Sie, py zu konvertieren. $ futurize --stage1 -w cloudmonkey/config.py

RefactoringTool: Skipping optional fixer: idioms
RefactoringTool: Skipping optional fixer: ws_comma
RefactoringTool: Refactored cloudmonkey/config.py
--- cloudmonkey/config.py      	(original)
+++ cloudmonkey/config.py      	(refactored)
@@ -16,6 +16,8 @@
 # specific language governing permissions and limitations
 # under the License.

+from __future__ import print_function
+from functools import reduce
 __version__ = "5.3.3"
 __description__ = "Command Line Interface for Apache CloudStack"
 __maintainer__ = "The Apache CloudStack Team"
@@ -30,8 +32,8 @@

     from ConfigParser import ConfigParser
     from os.path import expanduser
-except ImportError, e:
-    print "ImportError", e
+except ImportError as e:
+    print("ImportError", e)

 param_type = ['boolean', 'date', 'float', 'integer', 'short', 'list',
               'long', 'object', 'map', 'string', 'tzdate', 'uuid']
@@ -84,18 +86,18 @@
         try:
             with open(config_file, 'r') as cfg:
                 config.readfp(cfg)
-        except IOError, e:
-            print "Error: config_file not found", e
+        except IOError as e:
+            print("Error: config_file not found", e)

     profile = None
     try:
         profile = get_attr('profile')
-    except AttributeError, e:
+    except AttributeError as e:
         pass
     if profile is None or profile == '':
         profile = default_profile_name
     if profile in mandatory_sections:
-        print "Server profile name cannot be '%s'" % profile
+        print("Server profile name cannot be '%s'" % profile)
         sys.exit(1)

     has_profile_changed = False
@@ -117,8 +119,8 @@
                 else:
                     for key in config_fields[section].keys():
                         config.set(section, key, config_fields[section][key])
-            except ValueError, e:
-                print "Server profile name cannot be", profile
+            except ValueError as e:
+                print("Server profile name cannot be", profile)
                 sys.exit(1)
         if section in mandatory_sections:
             section_keys = config_fields[section].keys()
@@ -150,13 +152,13 @@
         try:
             with open(config_file, 'r') as cfg:
                 config.readfp(cfg)
-        except IOError, e:
-            print "Error: config_file not found", e
+        except IOError as e:
+            print("Error: config_file not found", e)
     else:
         config = write_config(get_attr, config_file)
-        print "Welcome! Use the `set` command to configure options"
-        print "Config file:", config_file
-        print "After setting up, run the `sync` command to sync apis\n"
+        print("Welcome! Use the `set` command to configure options")
+        print("Config file:", config_file)
+        print("After setting up, run the `sync` command to sync apis\n")

     missing_keys = []
     if config.has_option('core', 'profile'):
@@ -166,19 +168,19 @@
         profile = default_profile_name

     if profile is None or profile == '' or profile in mandatory_sections:
-        print "Server profile cannot be", profile
+        print("Server profile cannot be", profile)
         sys.exit(1)

     set_attr("profile_names", filter(lambda x: x != "core" and x != "ui",
                                      config.sections()))

     if not config.has_section(profile):
-        print ("Selected profile (%s) does not exist," +
-               " using default values") % profile
+        print(("Selected profile (%s) does not exist," +
+               " using default values") % profile)
         try:
             config.add_section(profile)
-        except ValueError, e:
-            print "Server profile name cannot be", profile
+        except ValueError as e:
+            print("Server profile name cannot be", profile)
             sys.exit(1)
         for key in default_profile.keys():
             config.set(profile, key, default_profile[key])
@@ -191,7 +193,7 @@
         for key in section_keys:
             try:
                 set_attr(key, config.get(section, key))
-            except Exception, e:
+            except Exception as e:
                 if section in mandatory_sections:
                     set_attr(key, config_fields[section][key])
                 else:
@@ -202,8 +204,8 @@
                 set_attr(key, get_attr('prompt').strip() + " ")

     if len(missing_keys) > 0:
-        print "Missing configuration was set using default values for keys:"
-        print "`%s` in %s" % (', '.join(missing_keys), config_file)
+        print("Missing configuration was set using default values for keys:")
+        print("`%s` in %s" % (', '.join(missing_keys), config_file))
         write_config(get_attr, config_file)

     return config_options
RefactoringTool: Files that were modified:
RefactoringTool: cloudmonkey/config.py

$ futurize --stage2 -w cloudmonkey/config.py

RefactoringTool: Refactored cloudmonkey/config.py
--- cloudmonkey/config.py      	(original)
+++ cloudmonkey/config.py      	(refactored)
@@ -17,6 +17,8 @@
 # under the License.

 from __future__ import print_function
+from future import standard_library
+standard_library.install_aliases()
 from functools import reduce
 __version__ = "5.3.3"
 __description__ = "Command Line Interface for Apache CloudStack"
@@ -30,7 +32,7 @@
     import os
     import sys

-    from ConfigParser import ConfigParser
+    from configparser import ConfigParser
     from os.path import expanduser
 except ImportError as e:
     print("ImportError", e)
@@ -114,18 +116,18 @@
             try:
                 config.add_section(section)
                 if section not in mandatory_sections:
-                    for key in default_profile.keys():
+                    for key in list(default_profile.keys()):
                         config.set(section, key, default_profile[key])
                 else:
-                    for key in config_fields[section].keys():
+                    for key in list(config_fields[section].keys()):
                         config.set(section, key, config_fields[section][key])
             except ValueError as e:
                 print("Server profile name cannot be", profile)
                 sys.exit(1)
         if section in mandatory_sections:
-            section_keys = config_fields[section].keys()
+            section_keys = list(config_fields[section].keys())
         else:
-            section_keys = default_profile.keys()
+            section_keys = list(default_profile.keys())
         for key in section_keys:
             try:
                 if not (has_profile_changed and section == profile):
@@ -143,9 +145,8 @@
     if not os.path.exists(config_dir):
         os.makedirs(config_dir)

-    config_options = reduce(lambda x, y: x + y, map(lambda x:
-                            config_fields[x].keys(), config_fields.keys()))
-    config_options += default_profile.keys()
+    config_options = reduce(lambda x, y: x + y, [list(config_fields[x].keys()) for x in list(config_fields.keys())])
+    config_options += list(default_profile.keys())

     config = ConfigParser()
     if os.path.exists(config_file):
@@ -171,8 +172,7 @@
         print("Server profile cannot be", profile)
         sys.exit(1)

-    set_attr("profile_names", filter(lambda x: x != "core" and x != "ui",
-                                     config.sections()))
+    set_attr("profile_names", [x for x in config.sections() if x != "core" and x != "ui"])

     if not config.has_section(profile):
         print(("Selected profile (%s) does not exist," +
@@ -182,14 +182,14 @@
         except ValueError as e:
             print("Server profile name cannot be", profile)
             sys.exit(1)
-        for key in default_profile.keys():
+        for key in list(default_profile.keys()):
             config.set(profile, key, default_profile[key])

     for section in (mandatory_sections + [profile]):
         if section in mandatory_sections:
-            section_keys = config_fields[section].keys()
+            section_keys = list(config_fields[section].keys())
         else:
-            section_keys = default_profile.keys()
+            section_keys = list(default_profile.keys())
         for key in section_keys:
             try:
                 set_attr(key, config.get(section, key))
RefactoringTool: Files that were modified:
RefactoringTool: cloudmonkey/config.py

Versuchen Sie erneut zu installieren $ python setup.py develop

If you're upgrading, run the following to enable parameter completion:
  cloudmonkey sync
  cloudmonkey set paramcompletion true
Parameter completion may fail, if the above is not run!
running develop
running egg_info
creating cloudmonkey.egg-info
writing dependency_links to cloudmonkey.egg-info/dependency_links.txt
writing cloudmonkey.egg-info/PKG-INFO
writing entry points to cloudmonkey.egg-info/entry_points.txt
writing top-level names to cloudmonkey.egg-info/top_level.txt
writing requirements to cloudmonkey.egg-info/requires.txt
writing manifest file 'cloudmonkey.egg-info/SOURCES.txt'
reading manifest file 'cloudmonkey.egg-info/SOURCES.txt'
writing manifest file 'cloudmonkey.egg-info/SOURCES.txt'
running build_ext
Creating /Users/kentaro/porting/ven/lib/python3.5/site-packages/cloudmonkey.egg-link (link to .)
Adding cloudmonkey 5.3.3 to easy-install.pth file
Installing cloudmonkey script to /Users/kentaro/porting/ven/bin

Installed /Users/kentaro/porting/cloudstack-cloudmonkey
Processing dependencies for cloudmonkey==5.3.3
Searching for requests-toolbelt
Reading https://pypi.python.org/simple/requests-toolbelt/
Best match: requests-toolbelt 0.7.0
Downloading https://pypi.python.org/packages/59/78/1d391d30ebf74079a8e4de6ab66fdca5362903ef2df64496f4697e9bb626/requests-toolbelt-0.7.0.tar.gz#md5=bfe2009905f460f4764c32cfbbf4205f
Processing requests-toolbelt-0.7.0.tar.gz
Writing /var/folders/vl/nj7qbspd7hlgll3chjfgvl400000gn/T/easy_install-l6qjdcgd/requests-toolbelt-0.7.0/setup.cfg
Running requests-toolbelt-0.7.0/setup.py -q bdist_egg --dist-dir /var/folders/vl/nj7qbspd7hlgll3chjfgvl400000gn/T/easy_install-l6qjdcgd/requests-toolbelt-0.7.0/egg-dist-tmp-xbz2yomj
no previously-included directories found matching 'docs/_build'
warning: no previously-included files matching '*.py[cdo]' found anywhere in distribution
warning: no previously-included files matching '__pycache__' found anywhere in distribution
warning: no previously-included files matching '*.so' found anywhere in distribution
warning: no previously-included files matching '*.pyd' found anywhere in distribution
zip_safe flag not set; analyzing archive contents...
Copying requests_toolbelt-0.7.0-py3.5.egg to /Users/kentaro/porting/ven/lib/python3.5/site-packages
Adding requests-toolbelt 0.7.0 to easy-install.pth file

Installed /Users/kentaro/porting/ven/lib/python3.5/site-packages/requests_toolbelt-0.7.0-py3.5.egg
Searching for dicttoxml
Reading https://pypi.python.org/simple/dicttoxml/
Best match: dicttoxml 1.7.4
Downloading https://pypi.python.org/packages/74/36/534db111db9e7610a41641a1f6669a964aacaf51858f466de264cc8dcdd9/dicttoxml-1.7.4.tar.gz#md5=ec5643a048cf32dad3c28db236b923e4
Processing dicttoxml-1.7.4.tar.gz
Writing /var/folders/vl/nj7qbspd7hlgll3chjfgvl400000gn/T/easy_install-3yyvt_ku/dicttoxml-1.7.4/setup.cfg
Running dicttoxml-1.7.4/setup.py -q bdist_egg --dist-dir /var/folders/vl/nj7qbspd7hlgll3chjfgvl400000gn/T/easy_install-3yyvt_ku/dicttoxml-1.7.4/egg-dist-tmp-1oj02owk
zip_safe flag not set; analyzing archive contents...
Copying dicttoxml-1.7.4-py3.5.egg to /Users/kentaro/porting/ven/lib/python3.5/site-packages
Adding dicttoxml 1.7.4 to easy-install.pth file

Installed /Users/kentaro/porting/ven/lib/python3.5/site-packages/dicttoxml-1.7.4-py3.5.egg
Searching for requests==2.11.1
Best match: requests 2.11.1
Adding requests 2.11.1 to easy-install.pth file

Using /Users/kentaro/porting/ven/lib/python3.5/site-packages
Searching for prettytable==0.7.2
Best match: prettytable 0.7.2
Adding prettytable 0.7.2 to easy-install.pth file

Using /Users/kentaro/porting/ven/lib/python3.5/site-packages
Searching for argcomplete==1.4.1
Best match: argcomplete 1.4.1
Adding argcomplete 1.4.1 to easy-install.pth file

Using /Users/kentaro/porting/ven/lib/python3.5/site-packages
Searching for Pygments==2.1.3
Best match: Pygments 2.1.3
Adding Pygments 2.1.3 to easy-install.pth file
Installing pygmentize script to /Users/kentaro/porting/ven/bin

Using /Users/kentaro/porting/ven/lib/python3.5/site-packages
Finished processing dependencies for cloudmonkey==5.3.3

Die Installation war erfolgreich.

$ cloudmonkey

Traceback (most recent call last):
  File "/Users/kentaro/porting/ven/bin/cloudmonkey", line 9, in <module>
    load_entry_point('cloudmonkey', 'console_scripts', 'cloudmonkey')()
  File "/Users/kentaro/porting/ven/lib/python3.5/site-packages/pkg_resources/__init__.py", line 542, in load_entry_point
    return get_distribution(dist).load_entry_point(group, name)
  File "/Users/kentaro/porting/ven/lib/python3.5/site-packages/pkg_resources/__init__.py", line 2569, in load_entry_point
    return ep.load()
  File "/Users/kentaro/porting/ven/lib/python3.5/site-packages/pkg_resources/__init__.py", line 2229, in load
    return self.resolve()
  File "/Users/kentaro/porting/ven/lib/python3.5/site-packages/pkg_resources/__init__.py", line 2235, in resolve
    module = __import__(self.module_name, fromlist=['__name__'], level=0)
  File "/Users/kentaro/porting/cloudstack-cloudmonkey/cloudmonkey/cloudmonkey.py", line 48
    except ImportError, e:
                      ^
SyntaxError: invalid syntax

Aber es endete mit einem Fehler.

Das ist alles für heute.

Recommended Posts

Versuchen Sie, die cloudmonkey-CLI in python3 -1 zu konvertieren
Versuchen Sie, Python selbst zu verstehen
Versuchen Sie, Facebook mit Python zu betreiben
Versuchen Sie Python
Versuchen Sie, Trace in Python zu berechnen
Versuchen Sie, Farbfilme mit Python zu reproduzieren
Versuchen Sie, sich mit Python bei qiita anzumelden
Versuchen Sie, Excel mit Python (Xlwings) zu betreiben.
Versuchen Sie, mit Pandas in ordentliche Daten umzuwandeln
Python Amateur versucht die Liste zusammenzufassen ①
Auf Python 2.7.9 aktualisiert
Versuchen Sie, Breiten- / Längen- und Weltkoordinaten mit Python ineinander umzuwandeln
Python> try: / außer:
"Backport" zu Python 2
Erste Schritte zum Testen von Google CloudVision in Python
Versuchen Sie, Oni Mai Tsuji Miserable mit Python zu implementieren
Versuchen Sie, Metriken über Python DogStatsD an datadog zu senden
Berechnen wir das statistische Problem mit Python
3,14 π Tag, versuchen Sie also, in Python auszugeben
Versuchen Sie automatisch, Enum in Python 3.6 automatisch zu bewerten
Versuchen Sie, das Problem der Python-Klassenvererbung zu lösen
Versuchen Sie, mit Python eine Lebenskurve zu zeichnen
Versuchen Sie, in Python einen "Entschlüsselungs" -Code zu erstellen
Versuchen Sie, Python-Dokumente automatisch mit Sphinx zu generieren
Versuchen Sie, RPN mit Python zu berechnen (für Anfänger)
Python-Mock, um AWS IoT Device Shadow auszuprobieren
Versuchen Sie, mit Python eine Diedergruppe zu bilden
Versuchen Sie, Fische mit Python + OpenCV2.4 (unvollendet) zu erkennen.
Versuchen Sie, das Programmier-Herausforderungsbuch mit Python3 zu lösen
Python-Anfänger versucht, dem Administrator von Django eine grundlegende Authentifizierung hinzuzufügen
So installieren Sie Python
[Python] Versuchen Sie, die coole Antwort auf das FizzBuzz-Problem zu lesen
Python versuchen ~ außer ~ sonst
Änderungen von Python 3.0 zu Python 3.5
Änderungen von Python 2 zu Python 3.0
Versuchen Sie, Ihr eigenes Intro-Quiz mit Python zu verbessern
(Python) Versuchen Sie, eine Webanwendung mit Django zu entwickeln
Versuchen Sie, das Problem der Zuweisung von Schulungsärzten mit Python zu lösen
Schreiben Sie Python2-Code in Python3 um (2to3)
Probieren Sie die DB-Operation mit Python aus und visualisieren Sie sie mit d3
So installieren Sie Python
Python - Hinweise beim Konvertieren vom Typ str in den Typ int
Einführung in OpenCV (Python) - (2)
Versuchen Sie Debian + Python 3.4 + django1.7 ……
Versuchen Sie, yolact zu implementieren
Versuchen Sie gRPC in Python
Beachten Sie, dass Python ein Daemon ist
Einführung von Python 2.7 in CentOS 6.6
Versuchen Sie, hochfrequente Wörter mit NLTK (Python) zu extrahieren.
Verbinden Sie Python mit MySQL
Probieren Sie 9 Slices in Python aus
Versuchen Sie es mit Tweepy [Python2.7]
Python versuchen / außer Memo
Versuchen Sie es mit GUI, PyQt in Python
Versuchen Sie, die Funktionsliste des Python> os-Pakets abzurufen
Versuchen Sie, verschiedene Informationen anzuzeigen, die für das Debuggen mit Python nützlich sind
Versuchen Sie, eine Excel-Datei mit Python (Pandas / XlsxWriter) zu betreiben