[node-patches] Change in ovirt-node[master]: Simplify if-else statement to make the code more compact and...
pzhukov at redhat.com
pzhukov at redhat.com
Mon Jan 25 10:41:00 UTC 2016
Pavel Zhukov has uploaded a new change for review.
Change subject: Simplify if-else statement to make the code more compact and readable
......................................................................
Simplify if-else statement to make the code more compact and readable
Change-Id: I8c446035431ed8748e50a9f2d130d139ccac155c
Signed-off-by: Pavel Zhukov <pzhukov at redhat.com>
---
M src/ovirtnode/ovirtfunctions.py
1 file changed, 39 insertions(+), 112 deletions(-)
git pull ssh://gerrit.ovirt.org:29418/ovirt-node refs/changes/78/52678/1
diff --git a/src/ovirtnode/ovirtfunctions.py b/src/ovirtnode/ovirtfunctions.py
index 1194dde..8fb7551 100644
--- a/src/ovirtnode/ovirtfunctions.py
+++ b/src/ovirtnode/ovirtfunctions.py
@@ -60,14 +60,14 @@
# read product / version info
PRODUCT_SHORT = aug.get("/files/etc/default/version/PRODUCT_SHORT")
-if PRODUCT_SHORT == None:
+if PRODUCT_SHORT is None:
PRODUCT_SHORT = "oVirt"
else:
PRODUCT_SHORT = PRODUCT_SHORT.strip("'\"")
PRODUCT_VERSION = aug.get("/files/etc/default/version/VERSION")
PRODUCT_RELEASE = aug.get("/files/etc/default/version/RELEASE")
-OVIRT_CONFIG_FILES = [ "/etc/rsyslog.conf",
+OVIRT_CONFIG_FILES = ["/etc/rsyslog.conf",
"/etc/libvirt/libvirtd.conf",
"/etc/libvirt/passwd.db",
"/etc/passwd",
@@ -90,6 +90,7 @@
OVIRT_VARS = {}
# Parse all OVIRT_* variables
+
def parse_defaults():
global OVIRT_VARS
@@ -105,6 +106,7 @@
pass
f.close()
return OVIRT_VARS
+
def get_dev_live():
return "/dev/{live}".format(live=process.check_output("/usr/libexec/ovirt-functions get_live_disk", shell=True).strip())
@@ -253,25 +255,16 @@
# return 0 if this is an upgrade
# return 1 otherwise
def is_upgrade():
- if OVIRT_VARS.has_key("OVIRT_UPGRADE") and OVIRT_VARS["OVIRT_UPGRADE"] == "1":
- return True
- else:
- return False
+ return OVIRT_VARS.has_key("OVIRT_UPGRADE") and OVIRT_VARS["OVIRT_UPGRADE"] == "1"
def is_install():
- if OVIRT_VARS.has_key("OVIRT_INSTALL") and OVIRT_VARS["OVIRT_INSTALL"] == "1":
- return True
- else:
- return False
+ return OVIRT_VARS.has_key("OVIRT_INSTALL") and OVIRT_VARS["OVIRT_INSTALL"] == "1"
+
# return 0 if booted from local disk
# return 1 if booted from other media
def is_booted_from_local_disk():
- ret = system_closefds("grep -q LABEL=Root /proc/cmdline")
- if ret == 0:
- return True
- else:
- return False
+ return system_closefds("grep -q LABEL=Root /proc/cmdline") == 0
def is_rescue_mode():
ret = system_closefds("grep -q rescue /proc/cmdline")
@@ -279,10 +272,7 @@
return True
# check for runlevel 1/single
else:
- ret = system_closefds("runlevel|grep -q '1\|S'")
- if ret == 0:
- return True
- return False
+ return system_closefds("runlevel|grep -q '1\|S'") == 0
def get_ttyname():
for f in sys.stdin, sys.stdout, sys.stderr:
@@ -310,10 +300,8 @@
def manual_setup():
logger.info("Checking For Setup Lockfile")
tty = get_ttyname()
- if os.path.exists("/tmp/ovirt-setup.%s" % tty):
- return True
- else:
- return False
+ return os.path.exists("/tmp/ovirt-setup.%s" % tty)
+e
def manual_teardown():
logger.info("Removing Setup Lockfile")
@@ -330,10 +318,7 @@
elif OVIRT_VARS["OVIRT_FIRSTBOOT"] == "0":
return False
# in case there's no key, default to True unless booted from disk
- if is_booted_from_local_disk():
- return False
- else:
- return True
+ return not is_booted_from_local_disk()
def is_cim_enabled():
# check if theres a key first
@@ -356,12 +341,7 @@
def is_stateless():
# check if theres a key first
- if OVIRT_VARS.has_key("OVIRT_STATELESS"):
- if OVIRT_VARS["OVIRT_STATELESS"] == "1":
- return True
- elif OVIRT_VARS["OVIRT_STATELESS"] == "0":
- return False
- return False
+ return OVIRT_VARS.has_key("OVIRT_STATELESS") and OVIRT_VARS["OVIRT_STATELESS"] == "1"
def disable_firstboot():
if os.path.ismount("/config"):
@@ -511,10 +491,7 @@
elif os.path.ismount("/run/initramfs/live"):
system_closefds("mount -o bind /run/initramfs/live /live")
- if os.path.ismount("/live"):
- return True
- else:
- return False
+ return os.path.ismount("/live")
# mount root partition
# boot loader + kernel + initrd + LiveOS
@@ -999,10 +976,7 @@
def is_persisted(filename):
abspath = os.path.abspath(filename)
- if os.path.exists("/config" + abspath):
- return True
- else:
- return False
+ return os.path.exists("/config" + abspath)
# unmount bindmounted config files
# unmount_config /etc/config /etc/config2 ...
@@ -1014,10 +988,7 @@
def check_bind_mount(config_file):
bind_mount_cmd = 'grep -q "%s ext4" /proc/mounts' % config_file
- if system_closefds(bind_mount_cmd) == 0:
- return True
- else:
- return False
+ return system_closefds(bind_mount_cmd) == 0
def unmount_config(files):
if os.path.ismount("/config"):
@@ -1240,13 +1211,7 @@
def is_valid_hostname(hostname):
regex_1 = "([a-zA-Z]|[0-9])(([a-zA-Z]|[0-9]|-)*([a-zA-Z]|[0-9]))?$"
regex_2 = "^(([a-zA-Z0-9]|[a-zA-Z0-9][a-zA-Z0-9\-]*[a-zA-Z0-9])\.)*([A-Za-z]|[A-Za-z][A-Za-z0-9\-]*[A-Za-z0-9])$"
- if re.match(regex_1, hostname):
- return True
- else:
- if re.match(regex_2, hostname):
- return True
- else:
- return False
+ return re.match(regex_1, hostname) or re.match(regex_2, hostname)
def is_valid_nfs(nfs_entry):
regex = "^([a-zA-Z0-9_\-]+)([\.][a-zA-Z0-9_\-]+)+([:][/][a-zA-Z0-9\~\(\)_\-]*)+([\.][a-zA-Z0-9\(\)_\-]+)*"
@@ -1254,10 +1219,7 @@
ip = re.findall(r'[0-9]+(?:\.[0-9]+){3}', nfs_entry)
try:
if ip[0]:
- if is_valid_ipv4(ip[0]):
- return True
- else:
- return False
+ return is_valid_ipv4(ip[0])
except:
# hostname will fail on purpose
return True
@@ -1266,51 +1228,33 @@
def is_valid_host_port(host):
regex = "^([a-zA-Z0-9_\-]+)([\.][a-zA-Z0-9_\-]+)+([:][0-9\~\(\)_\-]*)+([\.][0-9]+)*$"
- if re.match(regex, host):
- return True
- else:
- return False
+ return re.match(regex, host)
def is_valid_url(host):
regex = "(((http|https)://)|(www\.))+(([a-zA-Z0-9\._-]+\.[a-zA-Z]{2,6})|([0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}))(/[a-zA-Z0-9\&%_\./-~-]*)?"
- if re.match(regex, host):
- return True
- else:
- return False
+ return re.match(regex, host)
def is_valid_host_or_ip(host_or_ip):
- if host_or_ip != "":
- if is_valid_ipv4(host_or_ip):
- return True
- if is_valid_ipv6(host_or_ip):
- return True
- if is_valid_hostname(host_or_ip):
- return True
- else:
- return False
- else:
- return True
+ ## ?? blank host_or_ip is valid ip ??
+ ## Seems like this function is not used anymore
+ return host_or_ip != "" and (is_valid_ipv4(host_or_ip) or
+ is_valid_ipv6(host_or_ip) or
+ is_valid_hostname(host_or_ip))
+
def is_valid_user_host(user):
regex = "^[\w-]+(\.[\w-]+)*@([a-z0-9-]+(\.[a-z0-9-]+)*?\.[a-z]{2,6}|(\d{1,3}\.){3}\d{1,3})(:\d{4})?$"
- if re.match(regex, user):
- return True
- else:
- return False
+ return re.match(regex, user)
+
def is_valid_iqn(iqn):
regex="^iqn\.(\d{4}-\d{2})\.([^:]+):"
- if re.match(regex, iqn):
- return True
- else:
- return False
+ return re.match(regex, iqn)
# Check if networking is already up
def network_up():
ret = system_closefds("ip addr show | grep -q 'inet.*scope global'")
- if ret == 0:
- return True
- return False
+ return ret == 0
def get_ip_address(ifname):
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
@@ -1368,10 +1312,7 @@
def is_valid_port(port_number):
regex = "^(6553[0-5]|655[0-2]\d|65[0-4]\d\d|6[0-4]\d{3}|[1-5]\d{4}|[1-9]\d{0,3}|0)$"
- if re.match(regex, port_number):
- return True
- else:
- return False
+ return re.match(regex, port_number)
# Cleans partition tables
def wipe_partitions(_drive):
@@ -1482,19 +1423,14 @@
passwd_cmd = "passwd -S %s" % user
passwd = subprocess_closefds(passwd_cmd, shell=True, stdout=PIPE, stderr=STDOUT)
passwd, err = passwd.communicate()
- if "locked" in passwd:
- return True
- else:
- return False
+ return "locked" in passwd
+
def pwd_set_check(user):
passwd_cmd = "passwd -S %s" % user
passwd = subprocess_closefds(passwd_cmd, shell=True, stdout=PIPE, stderr=STDOUT)
passwd, err = passwd.communicate()
- if "set" in passwd:
- return True
- else:
- return False
+ return "set" in passwd
# Check if a user exists on the system
def check_user_exists(name):
@@ -1701,10 +1637,7 @@
return string
def is_efi_boot():
- if os.path.exists("/sys/firmware/efi"):
- return True
- else:
- return False
+ return os.path.exists("/sys/firmware/efi")
def manage_firewall_port(port, action="open", proto="tcp"):
if action == "open":
@@ -1719,8 +1652,7 @@
ovirt_store_config("/etc/sysconfig/iptables")
def is_iscsi_install():
- if OVIRT_VARS.has_key("OVIRT_ISCSI_INSTALL") and OVIRT_VARS["OVIRT_ISCSI_INSTALL"].upper() == "Y":
- return True
+ return OVIRT_VARS.has_key("OVIRT_ISCSI_INSTALL") and OVIRT_VARS["OVIRT_ISCSI_INSTALL"].upper() == "Y"
def load_keyboard_config():
kbd = osystem.Keyboard()
@@ -1739,10 +1671,7 @@
True
'''
ip_cmd = "ip --details --oneline link | egrep -iq 'ovirtmgmt|rhevm'"
- if system_closefds(ip_cmd) is 0:
- return True
- else:
- return False
+ return system_closefds(ip_cmd) is 0
def create_minimal_etc_hosts_file():
filename = "/etc/hosts"
@@ -1822,10 +1751,8 @@
return efi.add_entry(label, loader_filename, disk)
def grub2_available():
- if os.path.exists("/sbin/grub2-install"):
- return True
- else:
- return False
+ return os.path.exists("/sbin/grub2-install")
+
class PluginBase(object):
"""Base class for pluggable Hypervisor configuration options.
--
To view, visit https://gerrit.ovirt.org/52678
To unsubscribe, visit https://gerrit.ovirt.org/settings
Gerrit-MessageType: newchange
Gerrit-Change-Id: I8c446035431ed8748e50a9f2d130d139ccac155c
Gerrit-PatchSet: 1
Gerrit-Project: ovirt-node
Gerrit-Branch: master
Gerrit-Owner: Pavel Zhukov <pzhukov at redhat.com>
More information about the node-patches
mailing list