Skip to contentSkip to conversation

scripts: More Python fixes

PATCH Applied Philippe Mathieu-Daudé 4 participants 16 messages May 12, 2020 – May 29, 2020 KVM

lore
Share & tools
lore
#0 lore raw

v4 cover scripts: More Python fixes

Trivial Python3 fixes, again...

Since v3:
- Fixed missing scripts/qemugdb/timers.py (kwolf)
- Cover more scripts
- Check for __main__ in few scripts

Since v2:
- Remove patch updating MAINTAINERS

Since v1:
- Added Alex Bennée A-b tags
- Addressed John Snow review comments
- Use /usr/bin/env
- Do not modify os.path (dropped last patch)

Philippe Mathieu-Daudé (6):
scripts/qemugdb: Remove shebang header scripts/qemu-gdb: Use Python 3 interpreter
scripts/qmp: Use Python 3 interpreter scripts/kvm/vmxcap: Use Python 3 interpreter and add pseudo-main() scripts/modules/module_block: Use Python 3 interpreter & add
pseudo-main
tests/migration/guestperf: Use Python 3 interpreter

 scripts/kvm/vmxcap                 |  7 ++++---
 scripts/modules/module_block.py    | 31 +++++++++++++++---------------
 scripts/qemu-gdb.py                |  4 ++--
 scripts/qemugdb/__init__.py        |  3 +--
 scripts/qemugdb/aio.py             |  3 +--
 scripts/qemugdb/coroutine.py       |  3 +--
 scripts/qemugdb/mtree.py           |  4 +---
 scripts/qemugdb/tcg.py             |  1 -
 scripts/qemugdb/timers.py          |  1 -
 scripts/qmp/qom-get                |  2 +-
 scripts/qmp/qom-list               |  2 +-
 scripts/qmp/qom-set                |  2 +-
 scripts/qmp/qom-tree               |  2 +-
 tests/migration/guestperf-batch.py |  2 +-
 tests/migration/guestperf-plot.py  |  2 +-
 tests/migration/guestperf.py       |  2 +-
 16 files changed, 33 insertions(+), 38 deletions(-)
Signature · 1 line
2.21.3
#1 lore raw

PATCH v4 1/6 scripts/qemugdb: Remove shebang header

From: Philippe Mathieu-Daudé <[email protected]>

These scripts are loaded as plugin by GDB (and they don't have any __main__ entry point). Remove the shebang header.

---
 scripts/qemugdb/__init__.py  | 3 +--
 scripts/qemugdb/aio.py       | 3 +--
 scripts/qemugdb/coroutine.py | 3 +--
 scripts/qemugdb/mtree.py     | 4 +---
 scripts/qemugdb/tcg.py       | 1 -
 scripts/qemugdb/timers.py    | 1 -
 6 files changed, 4 insertions(+), 11 deletions(-)
scripts/qemugdb/__init__.py +1 -2
index 969f552b26..da8ff612e5 100644
@@ -1,5 +1,4 @@
-#!/usr/bin/python
-
+#
 # GDB debugging support
 #
 # Copyright (c) 2015 Linaro Ltd
scripts/qemugdb/aio.py +1 -2
index 2ba00c4444..d7c1ba0c28 100644
@@ -1,5 +1,4 @@
-#!/usr/bin/python
-
+#
 # GDB debugging support: aio/iohandler debug
 #
 # Copyright (c) 2015 Red Hat, Inc.
scripts/qemugdb/coroutine.py +1 -2
index 41e079d0e2..db61389022 100644
@@ -1,5 +1,4 @@
-#!/usr/bin/python
-
+#
 # GDB debugging support
 #
 # Copyright 2012 Red Hat, Inc. and/or its affiliates
scripts/qemugdb/mtree.py +1 -3
index 3030a60d3f..8fe42c3c12 100644
@@ -1,5 +1,4 @@
-#!/usr/bin/python
-
+#
 # GDB debugging support
 #
 # Copyright 2012 Red Hat, Inc. and/or its affiliates
@@ -84,4 +83,3 @@ def print_item(self, ptr, offset = gdb.Value(0), level = 0):
         while not isnull(subregion):
             self.print_item(subregion, addr, level)
             subregion = subregion['subregions_link']['tqe_next']
-
scripts/qemugdb/tcg.py +0 -1
index 18880fc9a7..16c03c06a9 100644
@@ -1,4 +1,3 @@
-#!/usr/bin/python
 # -*- coding: utf-8 -*-
 #
 # GDB debugging support, TCG status
scripts/qemugdb/timers.py +0 -1
index f0e132d27a..46537b27cf 100644
@@ -1,4 +1,3 @@
-#!/usr/bin/python
 # -*- coding: utf-8 -*-
 # GDB debugging support
 #
Signature · 1 line
2.21.3
Reviewed-by on v4 1/6
#2 lore raw

Re: [PATCH v4 1/6] scripts/qemugdb: Remove shebang header

Quoted 7 lines · Philippe Mathieu-Daudé

From: Philippe Mathieu-Daudé <[email protected]>

These scripts are loaded as plugin by GDB (and they don't have any __main__ entry point). Remove the shebang header.

(The A-B should come below the S-O-B unless you do mean to indicate you've since made changes that AB didn't acknowledge, I think.)

#3 lore raw

PATCH v4 2/6 scripts/qemu-gdb: Use Python 3 interpreter

In reply to Philippe Mathieu-Daudé

From: Philippe Mathieu-Daudé <[email protected]>

---
 scripts/qemu-gdb.py | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
scripts/qemu-gdb.py +2 -2
index f2a305c42e..e0bfa7b5a4 100644
@@ -1,5 +1,5 @@
-#!/usr/bin/python
-
+#!/usr/bin/env python3
+#
 # GDB debugging support
 #
 # Copyright 2012 Red Hat, Inc. and/or its affiliates
Signature · 1 line
2.21.3
#5 lore raw

PATCH v4 3/6 scripts/qmp: Use Python 3 interpreter

In reply to Philippe Mathieu-Daudé

From: Philippe Mathieu-Daudé <[email protected]>

---
 scripts/qmp/qom-get  | 2 +-
 scripts/qmp/qom-list | 2 +-
 scripts/qmp/qom-set  | 2 +-
 scripts/qmp/qom-tree | 2 +-
 4 files changed, 4 insertions(+), 4 deletions(-)
scripts/qmp/qom-get +1 -1
index 007b4cd442..7c5ede91bb 100755
@@ -1,4 +1,4 @@
-#!/usr/bin/python
+#!/usr/bin/env python3
 ##
 # QEMU Object Model test tools
 #
scripts/qmp/qom-list +1 -1
index 03bda3446b..bb68fd65d4 100755
@@ -1,4 +1,4 @@
-#!/usr/bin/python
+#!/usr/bin/env python3
 ##
 # QEMU Object Model test tools
 #
scripts/qmp/qom-set +1 -1
index c37fe78b00..19881d85e9 100755
@@ -1,4 +1,4 @@
-#!/usr/bin/python
+#!/usr/bin/env python3
 ##
 # QEMU Object Model test tools
 #
scripts/qmp/qom-tree +1 -1
index 1c8acf61e7..fa91147a03 100755
@@ -1,4 +1,4 @@
-#!/usr/bin/python
+#!/usr/bin/env python3
 ##
 # QEMU Object Model test tools
 #
Signature · 1 line
2.21.3
Reviewed-by on v4 3/6
#6 lore raw

Re: [PATCH v4 3/6] scripts/qmp: Use Python 3 interpreter

Quoted 3 lines · Philippe Mathieu-Daudé

From: Philippe Mathieu-Daudé <[email protected]>

Whoops, I address this in a patch series I'm working on, too. I'll keep my patch in there for now until this one makes it in, or vice-versa.

Quoted 48 lines
---
 scripts/qmp/qom-get  | 2 +-
 scripts/qmp/qom-list | 2 +-
 scripts/qmp/qom-set  | 2 +-
 scripts/qmp/qom-tree | 2 +-
 4 files changed, 4 insertions(+), 4 deletions(-)
scripts/qmp/qom-get +1 -1
index 007b4cd442..7c5ede91bb 100755
@@ -1,4 +1,4 @@
-#!/usr/bin/python
+#!/usr/bin/env python3
 ##
 # QEMU Object Model test tools
 #
scripts/qmp/qom-list +1 -1
index 03bda3446b..bb68fd65d4 100755
@@ -1,4 +1,4 @@
-#!/usr/bin/python
+#!/usr/bin/env python3
 ##
 # QEMU Object Model test tools
 #
scripts/qmp/qom-set +1 -1
index c37fe78b00..19881d85e9 100755
@@ -1,4 +1,4 @@
-#!/usr/bin/python
+#!/usr/bin/env python3
 ##
 # QEMU Object Model test tools
 #
scripts/qmp/qom-tree +1 -1
index 1c8acf61e7..fa91147a03 100755
@@ -1,4 +1,4 @@
-#!/usr/bin/python
+#!/usr/bin/env python3
 ##
 # QEMU Object Model test tools
 #
Signature · 1 line
—js
#7 lore raw

PATCH v4 4/6 scripts/kvm/vmxcap: Use Python 3 interpreter and add pseudo-main()

In reply to Philippe Mathieu-Daudé
---
 scripts/kvm/vmxcap | 7 ++++---
 1 file changed, 4 insertions(+), 3 deletions(-)
scripts/kvm/vmxcap +4 -3
index 971ed0e721..6fe66d5f57 100755
@@ -1,4 +1,4 @@
-#!/usr/bin/python
+#!/usr/bin/env python3
 #
 # tool for querying VMX capabilities
 #
@@ -275,5 +275,6 @@ controls = [
         ),
     ]
 
-for c in controls:
-    c.show()
+if __name__ == '__main__':
+    for c in controls:
+        c.show()
Signature · 1 line
2.21.3
maintainer Acked-by on v4 4/6
#8 lore raw

Re: [PATCH v4 4/6] scripts/kvm/vmxcap: Use Python 3 interpreter and add pseudo-main()

Quoted 17 lines · Philippe Mathieu-Daudé
---
 scripts/kvm/vmxcap | 7 ++++---
 1 file changed, 4 insertions(+), 3 deletions(-)
scripts/kvm/vmxcap +1 -1
index 971ed0e721..6fe66d5f57 100755
@@ -1,4 +1,4 @@
-#!/usr/bin/python
+#!/usr/bin/env python3
 #
 # tool for querying VMX capabilities
 #

scripts/kvm/vmxcap controls = [ · line 280

Quoted 8 lines
scripts/kvm/vmxcap +3 -2
@@ -275,5 +275,6 @@ controls = [
         ),
     ]
 
-for c in controls:
-    c.show()
+if __name__ == '__main__':
+    for c in controls:
+        c.show()
Reviewed-by on v4 4/6
#9 lore raw

Re: [PATCH v4 4/6] scripts/kvm/vmxcap: Use Python 3 interpreter and add pseudo-main()

In reply to Philippe Mathieu-Daudé
Quoted 17 lines · Philippe Mathieu-Daudé
---
 scripts/kvm/vmxcap | 7 ++++---
 1 file changed, 4 insertions(+), 3 deletions(-)
scripts/kvm/vmxcap +1 -1
index 971ed0e721..6fe66d5f57 100755
@@ -1,4 +1,4 @@
-#!/usr/bin/python
+#!/usr/bin/env python3
 #
 # tool for querying VMX capabilities
 #

scripts/kvm/vmxcap controls = [ · line 280

Quoted 8 lines
scripts/kvm/vmxcap +3 -2
@@ -275,5 +275,6 @@ controls = [
         ),
     ]
 
-for c in controls:
-    c.show()
+if __name__ == '__main__':
+    for c in controls:
+        c.show()

yay

#10 lore raw

PATCH v4 5/6 scripts/modules/module_block: Use Python 3 interpreter & add pseudo-main

In reply to Philippe Mathieu-Daudé
---
 scripts/modules/module_block.py | 31 ++++++++++++++++---------------
 1 file changed, 16 insertions(+), 15 deletions(-)
scripts/modules/module_block.py +16 -15
index f23191fac1..2e7021b952 100644
@@ -1,4 +1,4 @@
-#!/usr/bin/python
+#!/usr/bin/env python3
 #
 # Module information generator
 #
@@ -10,7 +10,6 @@
 # This work is licensed under the terms of the GNU GPL, version 2.
 # See the COPYING file in the top-level directory.
 
-import sys
 import os
 
 def get_string_struct(line):
@@ -80,19 +79,21 @@ def print_bottom(fheader):
 #endif
 ''')
 
-# First argument: output file
-# All other arguments: modules source files (.c)
-output_file = sys.argv[1]
-with open(output_file, 'w') as fheader:
-    print_top(fheader)
+if __name__ == '__main__':
+    import sys
+    # First argument: output file
+    # All other arguments: modules source files (.c)
+    output_file = sys.argv[1]
+    with open(output_file, 'w') as fheader:
+        print_top(fheader)
 
-    for filename in sys.argv[2:]:
-        if os.path.isfile(filename):
-            process_file(fheader, filename)
-        else:
-            print("File " + filename + " does not exist.", file=sys.stderr)
-            sys.exit(1)
+        for filename in sys.argv[2:]:
+            if os.path.isfile(filename):
+                process_file(fheader, filename)
+            else:
+                print("File " + filename + " does not exist.", file=sys.stderr)
+                sys.exit(1)
 
-    print_bottom(fheader)
+        print_bottom(fheader)
 
-sys.exit(0)
+    sys.exit(0)
Signature · 1 line
2.21.3
Reviewed-by on v4 5/6
#11 lore raw

Re: [PATCH v4 5/6] scripts/modules/module_block: Use Python 3 interpreter & add pseudo-main

Quoted 24 lines · Philippe Mathieu-Daudé
---
 scripts/modules/module_block.py | 31 ++++++++++++++++---------------
 1 file changed, 16 insertions(+), 15 deletions(-)
scripts/modules/module_block.py +1 -2
index f23191fac1..2e7021b952 100644
@@ -1,4 +1,4 @@
-#!/usr/bin/python
+#!/usr/bin/env python3
 #
 # Module information generator
 #
@@ -10,7 +10,6 @@
 # This work is licensed under the terms of the GNU GPL, version 2.
 # See the COPYING file in the top-level directory.
 
-import sys
 import os
 
 def get_string_struct(line):

scripts/modules/module_block.py def print_bottom(fheader): · line 83

Quoted 10 lines
scripts/modules/module_block.py +2 -5
@@ -80,19 +79,21 @@ def print_bottom(fheader):
 #endif
 ''')
 
-# First argument: output file
-# All other arguments: modules source files (.c)
-output_file = sys.argv[1]
-with open(output_file, 'w') as fheader:
-    print_top(fheader)
+if __name__ == '__main__':
+    import sys

You can keep the imports at the top of the file.

If you want to split apart the code such that the core import doesn't import it, you can create a "core module" containing the classes and routines, and a separate script entrypoint, which imports arg parsers, sys.argv, etc.

For this, for now, it's okay to just leave it at the top of the file.

Quoted 15 lines
scripts/modules/module_block.py +7 -6
+    # First argument: output file
+    # All other arguments: modules source files (.c)
+    output_file = sys.argv[1]
+    with open(output_file, 'w') as fheader:
+        print_top(fheader)
 
-    for filename in sys.argv[2:]:
-        if os.path.isfile(filename):
-            process_file(fheader, filename)
-        else:
-            print("File " + filename + " does not exist.", file=sys.stderr)
-            sys.exit(1)
+        for filename in sys.argv[2:]:
+            if os.path.isfile(filename):

scripts/modules/module_block.py def print_bottom(fheader): · line 99

Quoted 10 lines
scripts/modules/module_block.py +6 -2
+                process_file(fheader, filename)
+            else:
+                print("File " + filename + " does not exist.", file=sys.stderr)
+                sys.exit(1)
 
-    print_bottom(fheader)
+        print_bottom(fheader)
 
-sys.exit(0)
+    sys.exit(0)

But, well. It's nitpicky and I'm not sure it matters just yet. It might, as we start to expand pylint to more places, but we're not being rigorous about that just yet. So either way, I know this works:

#12 lore raw

PATCH v4 6/6 tests/migration/guestperf: Use Python 3 interpreter

In reply to Philippe Mathieu-Daudé
---
 tests/migration/guestperf-batch.py | 2 +-
 tests/migration/guestperf-plot.py  | 2 +-
 tests/migration/guestperf.py       | 2 +-
 3 files changed, 3 insertions(+), 3 deletions(-)
tests/migration/guestperf-batch.py +1 -1
index cb150ce804..f1e900908d 100755
@@ -1,4 +1,4 @@
-#!/usr/bin/python
+#!/usr/bin/env python3
 #
 # Migration test batch comparison invokation
 #
tests/migration/guestperf-plot.py +1 -1
index d70bb7a557..907151011a 100755
@@ -1,4 +1,4 @@
-#!/usr/bin/python
+#!/usr/bin/env python3
 #
 # Migration test graph plotting command
 #
tests/migration/guestperf.py +1 -1
index 99b027e8ba..ba1c4bc4ca 100755
@@ -1,4 +1,4 @@
-#!/usr/bin/python
+#!/usr/bin/env python3
 #
 # Migration test direct invokation command
 #
Signature · 1 line
2.21.3
Reviewed-by on the v4 series
#14 lore raw
In reply to Philippe Mathieu-Daudé

Am 12.05.2020 um 12:32 hat Philippe Mathieu-Daudé geschrieben:

Quoted 6 lines

Trivial Python3 fixes, again...

Since v3:
- Fixed missing scripts/qemugdb/timers.py (kwolf)
- Cover more scripts
- Check for __main__ in few scripts

I'm not sure if the __main__ check actually provides anything useful in source files of standalone tools that aren't supposed to be imported from somewhere else. But of course, it's not wrong either.

#15 lore raw
In reply to Philippe Mathieu-Daudé
Quoted 34 lines · Philippe Mathieu-Daudé

Trivial Python3 fixes, again...

Since v3:
- Fixed missing scripts/qemugdb/timers.py (kwolf)
- Cover more scripts
- Check for __main__ in few scripts

Since v2:
- Remove patch updating MAINTAINERS

Since v1:
- Added Alex Bennée A-b tags
- Addressed John Snow review comments
- Use /usr/bin/env
- Do not modify os.path (dropped last patch)

Philippe Mathieu-Daudé (6):
scripts/qemugdb: Remove shebang header scripts/qemu-gdb: Use Python 3 interpreter
scripts/qmp: Use Python 3 interpreter scripts/kvm/vmxcap: Use Python 3 interpreter and add pseudo-main() scripts/modules/module_block: Use Python 3 interpreter & add
pseudo-main
tests/migration/guestperf: Use Python 3 interpreter

 scripts/kvm/vmxcap                 |  7 ++++---
 scripts/modules/module_block.py    | 31 +++++++++++++++---------------
 scripts/qemu-gdb.py                |  4 ++--
 scripts/qemugdb/__init__.py        |  3 +--
 scripts/qemugdb/aio.py             |  3 +--
 scripts/qemugdb/coroutine.py       |  3 +--
 scripts/qemugdb/mtree.py           |  4 +---
 scripts/qemugdb/tcg.py             |  1 -
 scripts/qemugdb/timers.py          |  1 -
Quoted 8 lines
 scripts/qmp/qom-get                |  2 +-
 scripts/qmp/qom-list               |  2 +-
 scripts/qmp/qom-set                |  2 +-
 scripts/qmp/qom-tree               |  2 +-
 tests/migration/guestperf-batch.py |  2 +-
 tests/migration/guestperf-plot.py  |  2 +-
 tests/migration/guestperf.py       |  2 +-
 16 files changed, 33 insertions(+), 38 deletions(-)

Thanks, applied to my python-next tree:
https://gitlab.com/philmd/qemu/commits/python-next