[COMMIT osv master] scripts: migrate setup.py to python 3

4 views
Skip to first unread message

Commit Bot

unread,
Feb 21, 2020, 12:57:12 PM2/21/20
to osv...@googlegroups.com, Waldemar Kozaczuk
From: Waldemar Kozaczuk <jwkoz...@gmail.com>
Committer: Waldemar Kozaczuk <jwkoz...@gmail.com>
Branch: master

scripts: migrate setup.py to python 3

The setup.py relies on platform.linux_distribution() to determine
linux distribution of the host. Python 3 has unfortunately deprecated
aforementioned method so in order to migrate to Python 3 this patch
changes setup.py to parse the same information from some standard files
located under /etc.

More specifically new setup.py tries to parse name and version
from /etc/os-release. If not found it falls back to /etc/lsb-release.

Signed-off-by: Waldemar Kozaczuk <jwkoz...@gmail.com>

---
diff --git a/scripts/setup.py b/scripts/setup.py
--- a/scripts/setup.py
+++ b/scripts/setup.py
@@ -1,8 +1,8 @@
-#!/usr/bin/python2
+#!/usr/bin/python3

# set up a development environment for OSv. Run as root.

-import sys, platform, argparse
+import sys, argparse
import subprocess

standard_ec2_packages = ['python-pip', 'wget']
@@ -304,6 +304,27 @@ class LinuxMint_19(object):

versions = [LinuxMint_18_03, LinuxMint_19]

+def linux_distribution():
+ def parse_file(f):
+ res = {}
+ for line in f:
+ k, v = line.rstrip().split('=')
+ res[k] = v.strip('"')
+ return res
+
+ try:
+ with open('/etc/os-release') as f:
+ info = parse_file(f)
+ return (info['NAME'], info['VERSION_ID'])
+ except FileNotFoundError:
+ try:
+ with open('/etc/lsb-release') as f:
+ info = parse_file(f)
+ return (info['DISTRIB_ID'], info['DISTRIB_RELEASE'])
+ except FileNotFoundError:
+ print('Could not find linux distribution file!')
+ return ('Unknown', 'Unknown')
+
distros = [
Debian(),
Fedora(),
@@ -319,11 +340,11 @@ class LinuxMint_19(object):
help="install packages required by testing tools")
cmdargs = parser.parse_args()

-(name, version, id) = platform.linux_distribution()
+(name, version) = linux_distribution()

for distro in distros:
if type(distro.name) == type([]):
- dname = filter(lambda n: name.startswith(n), distro.name)
+ dname = [n for n in distro.name if name.startswith(n)]
if len(dname):
distro.name = dname[0]
else:
@@ -349,5 +370,5 @@ class LinuxMint_19(object):
print ('Your distribution %s version %s is not supported by this script' % (name, version))
sys.exit(1)

-print 'Your distribution is not supported by this script.'
+print('Your distribution is not supported by this script.')
sys.exit(2)
Reply all
Reply to author
Forward
0 new messages