Skip to contentSkip to conversation

scripts: Convert qemu-version.sh to qemu-version.py

PATCH Superseded Yonggang Luo 4 participants 6 messages Oct 5, 2020 – Oct 6, 2020 Arm/Amlogic Meson SoC support

v2 of 11 Superseded Latest is v11 →

lore
Share & tools
lore
#0 lore raw

PATCH v2 1/1 scripts: Convert qemu-version.sh to qemu-version.py

The sh script are harder to maintain for compatible different xsh environment

---
 meson.build             |  2 +-
 scripts/qemu-version.py | 30 ++++++++++++++++++++++++++++++
 scripts/qemu-version.sh | 25 -------------------------
 3 files changed, 31 insertions(+), 26 deletions(-)
 create mode 100644 scripts/qemu-version.py
 delete mode 100755 scripts/qemu-version.sh
meson.build +1 -1
index 95a532bd29..20f653b6eb 100644
@@ -1072,7 +1072,7 @@ tracetool = [
    '--backend=' + config_host['TRACE_BACKENDS']
 ]
 
-qemu_version_cmd = [find_program('scripts/qemu-version.sh'),
+qemu_version_cmd = [find_program('scripts/qemu-version.py'),
                     meson.current_source_dir(),
                     config_host['PKGVERSION'], meson.project_version()]
 qemu_version = custom_target('qemu-version.h',
scripts/qemu-version.py new file +30 -0
new file mode 100644
index 0000000000..384c54027d
@@ -0,0 +1,30 @@
+#!/usr/bin/env python3
+
+# Script for retrieve qemu git version information
+# and output to stdout as QEMU_PKGVERSION and QEMU_FULL_VERSION header
+# Author: Yonggang Luo <[email protected]>
+
+import sys
+import subprocess
+
+def main(args):
+    if len(args) <= 3:
+        sys.exit(0)
+
+    dir = args[1]
+    pkgversion = args[2]
+    version = args[3]
+    pc = subprocess.run(['git', 'describe', '--match', "'v*'", '--dirty', '--always'],
+        stdout=subprocess.PIPE, stderr=subprocess.DEVNULL, cwd=dir)
+    if pc.returncode == 0:
+        pkgversion = pc.stdout.decode('utf8').strip()
+    fullversion = version
+    if len(pkgversion) > 0:
+        fullversion = "{} ({})".format(version, pkgversion)
+
+    version_header = '''#define QEMU_PKGVERSION "{}"
+#define QEMU_FULL_VERSION "{}"'''.format(pkgversion, fullversion)
+    sys.stdout.buffer.write(version_header.encode('utf8'))
+
+if __name__ == "__main__":
+    main(sys.argv)
scripts/qemu-version.sh deleted +0 -25
deleted file mode 100755
index 03128c56a2..0000000000
@@ -1,25 +0,0 @@
-#!/bin/sh
-
-set -eu
-
-dir="$1"
-pkgversion="$2"
-version="$3"
-
-if [ -z "$pkgversion" ]; then
-    cd "$dir"
-    if [ -e .git ]; then
-        pkgversion=$(git describe --match 'v*' --dirty | echo "")
-    fi
-fi
-
-if [ -n "$pkgversion" ]; then
-    fullversion="$version ($pkgversion)"
-else
-    fullversion="$version"
-fi
-
-cat <<EOF
-#define QEMU_PKGVERSION "$pkgversion"
-#define QEMU_FULL_VERSION "$fullversion"
-EOF
Signature · 1 line
2.28.0.windows.1
#1 lore raw
Quoted 5 lines · Yonggang Luo

The sh script are harder to maintain for compatible different xsh environment

---

What are the changes since v1?

Quoted 88 lines
 meson.build             |  2 +-
 scripts/qemu-version.py | 30 ++++++++++++++++++++++++++++++
 scripts/qemu-version.sh | 25 -------------------------
 3 files changed, 31 insertions(+), 26 deletions(-)
 create mode 100644 scripts/qemu-version.py
 delete mode 100755 scripts/qemu-version.sh
meson.build +1 -1
index 95a532bd29..20f653b6eb 100644
@@ -1072,7 +1072,7 @@ tracetool = [
    '--backend=' + config_host['TRACE_BACKENDS']
 ]
 
-qemu_version_cmd = [find_program('scripts/qemu-version.sh'),
+qemu_version_cmd = [find_program('scripts/qemu-version.py'),
                     meson.current_source_dir(),
                     config_host['PKGVERSION'], meson.project_version()]
 qemu_version = custom_target('qemu-version.h',
scripts/qemu-version.py new file +30 -0
new file mode 100644
index 0000000000..384c54027d
@@ -0,0 +1,30 @@
+#!/usr/bin/env python3
+
+# Script for retrieve qemu git version information
+# and output to stdout as QEMU_PKGVERSION and QEMU_FULL_VERSION header
+# Author: Yonggang Luo <[email protected]>
+
+import sys
+import subprocess
+
+def main(args):
+    if len(args) <= 3:
+        sys.exit(0)
+
+    dir = args[1]
+    pkgversion = args[2]
+    version = args[3]
+    pc = subprocess.run(['git', 'describe', '--match', "'v*'", '--dirty', '--always'],
+        stdout=subprocess.PIPE, stderr=subprocess.DEVNULL, cwd=dir)
+    if pc.returncode == 0:
+        pkgversion = pc.stdout.decode('utf8').strip()
+    fullversion = version
+    if len(pkgversion) > 0:
+        fullversion = "{} ({})".format(version, pkgversion)
+
+    version_header = '''#define QEMU_PKGVERSION "{}"
+#define QEMU_FULL_VERSION "{}"'''.format(pkgversion, fullversion)
+    sys.stdout.buffer.write(version_header.encode('utf8'))
+
+if __name__ == "__main__":
+    main(sys.argv)
scripts/qemu-version.sh deleted +0 -25
deleted file mode 100755
index 03128c56a2..0000000000
@@ -1,25 +0,0 @@
-#!/bin/sh
-
-set -eu
-
-dir="$1"
-pkgversion="$2"
-version="$3"
-
-if [ -z "$pkgversion" ]; then
-    cd "$dir"
-    if [ -e .git ]; then
-        pkgversion=$(git describe --match 'v*' --dirty | echo "")
-    fi
-fi
-
-if [ -n "$pkgversion" ]; then
-    fullversion="$version ($pkgversion)"
-else
-    fullversion="$version"
-fi
-
-cat <<EOF
-#define QEMU_PKGVERSION "$pkgversion"
-#define QEMU_FULL_VERSION "$fullversion"
-EOF
#2 lore raw
Quoted 9 lines · Philippe Mathieu-Daudé
Quoted 5 lines · Yonggang Luo

The sh script are harder to maintain for compatible different xsh environment

---

What are the changes since v1?

+ pc = subprocess.run(['git', 'describe', '--match', "'v*'", '--dirty',
'--always'],
+ stdout=subprocess.PIPE, stderr=subprocess.DEVNULL, cwd=dir)

I output the stderr to devnull so that doesn't pollute the console.

Quoted 36 lines
Quoted 36 lines
 meson.build             |  2 +-
 scripts/qemu-version.py | 30 ++++++++++++++++++++++++++++++
 scripts/qemu-version.sh | 25 -------------------------
 3 files changed, 31 insertions(+), 26 deletions(-)
 create mode 100644 scripts/qemu-version.py
 delete mode 100755 scripts/qemu-version.sh
meson.build +1 -1
index 95a532bd29..20f653b6eb 100644
@@ -1072,7 +1072,7 @@ tracetool = [
    '--backend=' + config_host['TRACE_BACKENDS']
 ]
 
-qemu_version_cmd = [find_program('scripts/qemu-version.sh'),
+qemu_version_cmd = [find_program('scripts/qemu-version.py'),
                     meson.current_source_dir(),
                     config_host['PKGVERSION'], meson.project_version()]
 qemu_version = custom_target('qemu-version.h',
scripts/qemu-version.py new file +9 -0
new file mode 100644
index 0000000000..384c54027d
@@ -0,0 +1,30 @@
+#!/usr/bin/env python3
+
+# Script for retrieve qemu git version information
+# and output to stdout as QEMU_PKGVERSION and QEMU_FULL_VERSION header
+# Author: Yonggang Luo <[email protected]>
+
+import sys
+import subprocess
+
Quoted 8 lines

scripts/qemu-version.py

Quoted 8 lines
scripts/qemu-version.py new file +8 -0
+def main(args):
+    if len(args) <= 3:
+        sys.exit(0)
+
+    dir = args[1]
+    pkgversion = args[2]
+    version = args[3]
+    pc = subprocess.run(['git', 'describe', '--match', "'v*'",

'--dirty', '--always'],

Quoted 37 lines
Quoted 37 lines
scripts/qemu-version.py new file +13 -0
+        stdout=subprocess.PIPE, stderr=subprocess.DEVNULL, cwd=dir)
+    if pc.returncode == 0:
+        pkgversion = pc.stdout.decode('utf8').strip()
+    fullversion = version
+    if len(pkgversion) > 0:
+        fullversion = "{} ({})".format(version, pkgversion)
+
+    version_header = '''#define QEMU_PKGVERSION "{}"
+#define QEMU_FULL_VERSION "{}"'''.format(pkgversion, fullversion)
+    sys.stdout.buffer.write(version_header.encode('utf8'))
+
+if __name__ == "__main__":
+    main(sys.argv)
scripts/qemu-version.sh deleted +0 -18
deleted file mode 100755
index 03128c56a2..0000000000
@@ -1,25 +0,0 @@
-#!/bin/sh
-
-set -eu
-
-dir="$1"
-pkgversion="$2"
-version="$3"
-
-if [ -z "$pkgversion" ]; then
-    cd "$dir"
-    if [ -e .git ]; then
-        pkgversion=$(git describe --match 'v*' --dirty | echo "")
-    fi
-fi
-
-if [ -n "$pkgversion" ]; then
-    fullversion="$version ($pkgversion)"
-else
Quoted 8 lines

scripts/qemu-version.sh line 25

Quoted 8 lines
scripts/qemu-version.sh deleted +0 -7
-    fullversion="$version"
-fi
-
-cat <<EOF
-#define QEMU_PKGVERSION "$pkgversion"
-#define QEMU_FULL_VERSION "$fullversion"
-EOF

--
此致
礼
罗勇刚
Yours
sincerely,
Yonggang Luo

#3 lore raw
In reply to Yonggang Luo
Quoted 32 lines · Yonggang Luo

The sh script are harder to maintain for compatible different xsh environment

---
 meson.build             |  2 +-
 scripts/qemu-version.py | 30 ++++++++++++++++++++++++++++++
 scripts/qemu-version.sh | 25 -------------------------
 3 files changed, 31 insertions(+), 26 deletions(-)
 create mode 100644 scripts/qemu-version.py
 delete mode 100755 scripts/qemu-version.sh
meson.build +1 -1
index 95a532bd29..20f653b6eb 100644
@@ -1072,7 +1072,7 @@ tracetool = [
    '--backend=' + config_host['TRACE_BACKENDS']
 ]
 
-qemu_version_cmd = [find_program('scripts/qemu-version.sh'),
+qemu_version_cmd = [find_program('scripts/qemu-version.py'),
                     meson.current_source_dir(),
                     config_host['PKGVERSION'], meson.project_version()]
 qemu_version = custom_target('qemu-version.h',
scripts/qemu-version.py new file +0 -0
new file mode 100644
index 0000000000..384c54027d

scripts/qemu-version.py line 5

Quoted 5 lines
scripts/qemu-version.py new file +5 -0
@@ -0,0 +1,30 @@
+#!/usr/bin/env python3
+
+# Script for retrieve qemu git version information
+# and output to stdout as QEMU_PKGVERSION and QEMU_FULL_VERSION header
+# Author: Yonggang Luo <[email protected]>

Can we have a license statement in all new files, please?

Quoted 15 lines
scripts/qemu-version.py new file +15 -0
+
+import sys
+import subprocess
+
+def main(args):
+    if len(args) <= 3:
+        sys.exit(0)
+
+    dir = args[1]
+    pkgversion = args[2]
+    version = args[3]
+    pc = subprocess.run(['git', 'describe', '--match', "'v*'", '--dirty', '--always'],
+        stdout=subprocess.PIPE, stderr=subprocess.DEVNULL, cwd=dir)
+    if pc.returncode == 0:
+        pkgversion = pc.stdout.decode('utf8').strip()

scripts/qemu-version.py line 30

Quoted 10 lines
scripts/qemu-version.py new file +10 -0
+    fullversion = version
+    if len(pkgversion) > 0:
+        fullversion = "{} ({})".format(version, pkgversion)
+
+    version_header = '''#define QEMU_PKGVERSION "{}"
+#define QEMU_FULL_VERSION "{}"'''.format(pkgversion, fullversion)
+    sys.stdout.buffer.write(version_header.encode('utf8'))
+
+if __name__ == "__main__":
+    main(sys.argv)

Wow, python really makes this kind of task clunky compared to shell...

This doesn't seem to be the same logic as the original shell. I'm not too familiar with python, but:
* the shell script doesn't run git if pkgversion is not the empty string
* the shell script doesn't run git unless the .git directory exists
If these are intentional behaviour changes you should mention them in the commit message.

Also worth mentioning that you're fixing the bug in the shell script where we ignore the output from git (the intention was to ignore a failure from git but otherwise to keep its output; this patch:
https://patchew.org/QEMU/[email protected]/
is the fix for that in the existing shell script).

Quoted 21 lines
scripts/qemu-version.sh deleted +0 -15
deleted file mode 100755
index 03128c56a2..0000000000
@@ -1,25 +0,0 @@
-#!/bin/sh
-
-set -eu
-
-dir="$1"
-pkgversion="$2"
-version="$3"
-
-if [ -z "$pkgversion" ]; then
-    cd "$dir"
-    if [ -e .git ]; then
-        pkgversion=$(git describe --match 'v*' --dirty | echo "")
-    fi
-fi
-

scripts/qemu-version.sh line 25

Quoted 10 lines
scripts/qemu-version.sh deleted +0 -10
-if [ -n "$pkgversion" ]; then
-    fullversion="$version ($pkgversion)"
-else
-    fullversion="$version"
-fi
-
-cat <<EOF
-#define QEMU_PKGVERSION "$pkgversion"
-#define QEMU_FULL_VERSION "$fullversion"
-EOF

thanks
-- PMM

#4 lore raw
Quoted 42 lines · Peter Maydell
Quoted 32 lines · Yonggang Luo

The sh script are harder to maintain for compatible different xsh environment

---
 meson.build             |  2 +-
 scripts/qemu-version.py | 30 ++++++++++++++++++++++++++++++
 scripts/qemu-version.sh | 25 -------------------------
 3 files changed, 31 insertions(+), 26 deletions(-)
 create mode 100644 scripts/qemu-version.py
 delete mode 100755 scripts/qemu-version.sh
meson.build +1 -1
index 95a532bd29..20f653b6eb 100644
@@ -1072,7 +1072,7 @@ tracetool = [
    '--backend=' + config_host['TRACE_BACKENDS']
 ]
 
-qemu_version_cmd = [find_program('scripts/qemu-version.sh'),
+qemu_version_cmd = [find_program('scripts/qemu-version.py'),
                     meson.current_source_dir(),
                     config_host['PKGVERSION'], meson.project_version()]
 qemu_version = custom_target('qemu-version.h',
scripts/qemu-version.py new file +0 -0
new file mode 100644
index 0000000000..384c54027d

scripts/qemu-version.py line 5

Quoted 5 lines
scripts/qemu-version.py new file +5 -0
@@ -0,0 +1,30 @@
+#!/usr/bin/env python3
+
+# Script for retrieve qemu git version information
+# and output to stdout as QEMU_PKGVERSION and QEMU_FULL_VERSION header
+# Author: Yonggang Luo <[email protected]>

Can we have a license statement in all new files, please?

Quoted 12 lines

scripts/qemu-version.py

Quoted 12 lines
scripts/qemu-version.py new file +12 -0
+
+import sys
+import subprocess
+
+def main(args):
+    if len(args) <= 3:
+        sys.exit(0)
+
+    dir = args[1]
+    pkgversion = args[2]
+    version = args[3]
+    pc = subprocess.run(['git', 'describe', '--match', "'v*'",

'--dirty', '--always'],

Quoted 7 lines
Quoted 7 lines
scripts/qemu-version.py new file +7 -0
+        stdout=subprocess.PIPE, stderr=subprocess.DEVNULL, cwd=dir)
+    if pc.returncode == 0:
+        pkgversion = pc.stdout.decode('utf8').strip()
+    fullversion = version
+    if len(pkgversion) > 0:
+        fullversion = "{} ({})".format(version, pkgversion)
+
Quoted 8 lines

scripts/qemu-version.py line 30

Quoted 6 lines
scripts/qemu-version.py new file +6 -0
+    version_header = '''#define QEMU_PKGVERSION "{}"
+#define QEMU_FULL_VERSION "{}"'''.format(pkgversion, fullversion)
+    sys.stdout.buffer.write(version_header.encode('utf8'))
+
+if __name__ == "__main__":
+    main(sys.argv)

Wow, python really makes this kind of task clunky compared to shell...

That's right, the size are bloated, but for me, easier to understand

Quoted 8 lines

This doesn't seem to be the same logic as the original shell. I'm not too familiar with python, but:
* the shell script doesn't run git if pkgversion is not the empty string
* the shell script doesn't run git unless the .git directory exists
If these are intentional behaviour changes you should mention them in the commit message.

Quoted 5 lines

Also worth mentioning that you're fixing the bug in the shell script where we ignore the output from git (the intention was to ignore a failure from git but otherwise to keep its output; this patch:

https://patchew.org/QEMU/[email protected]/

Quoted 28 lines

is the fix for that in the existing shell script).

Quoted 26 lines
scripts/qemu-version.sh deleted +0 -20
deleted file mode 100755
index 03128c56a2..0000000000
@@ -1,25 +0,0 @@
-#!/bin/sh
-
-set -eu
-
-dir="$1"
-pkgversion="$2"
-version="$3"
-
-if [ -z "$pkgversion" ]; then
-    cd "$dir"
-    if [ -e .git ]; then
-        pkgversion=$(git describe --match 'v*' --dirty | echo "")
-    fi
-fi
-
-if [ -n "$pkgversion" ]; then
-    fullversion="$version ($pkgversion)"
-else
-    fullversion="$version"
-fi
Quoted 8 lines

scripts/qemu-version.sh line 25

Quoted 5 lines
scripts/qemu-version.sh deleted +0 -5
-
-cat <<EOF
-#define QEMU_PKGVERSION "$pkgversion"
-#define QEMU_FULL_VERSION "$fullversion"
-EOF

thanks
-- PMM

--
此致
礼
罗勇刚
Yours
sincerely,
Yonggang Luo

#5 lore raw
In reply to Peter Maydell
Quoted 7 lines · Peter Maydell

scripts/qemu-version.py line 16

Quoted 7 lines
scripts/qemu-version.py new file +7 -0
+def main(args):
+    if len(args) <= 3:
+        sys.exit(0)
+
+    dir = args[1]
+    pkgversion = args[2]
+    version = args[3]

In addition to what Peter pointed out, all these lines can be changed to

def main(dir, pkgversion, version, *unused):

and below

    if __name__ == "__main__":
        main(*sys.argv)
Quoted 6 lines

scripts/qemu-version.py line 22

Quoted 6 lines
scripts/qemu-version.py new file +6 -0
+    pc = subprocess.run(['git', 'describe', '--match', "'v*'", '--dirty', '--always'],
+        stdout=subprocess.PIPE, stderr=subprocess.DEVNULL, cwd=dir)
+    if pc.returncode == 0:
+        pkgversion = pc.stdout.decode('utf8').strip()
+    fullversion = version
+    if len(pkgversion) > 0:

Just "if pkgversion:" please.

Quoted 5 lines

scripts/qemu-version.py line 27

Quoted 5 lines
scripts/qemu-version.py new file +5 -0
+        fullversion = "{} ({})".format(version, pkgversion)
+
+    version_header = '''#define QEMU_PKGVERSION "{}"
+#define QEMU_FULL_VERSION "{}"'''.format(pkgversion, fullversion)
+    sys.stdout.buffer.write(version_header.encode('utf8'))

No need to use buffer and encode, just

print('#define QEMU_PKGVERSION "%s"' % pkgversion) print('#define QEMU_FULLVERSION "%s"' % fullversion)

It is still a bit more clunky than a shell script, but at least not as much.

Paolo