Skip to content

Commit

Permalink
configure-scripts: Fixes PEP8 errors
Browse files Browse the repository at this point in the history
  • Loading branch information
psaavedra committed Jan 25, 2025
1 parent 70f109f commit 902a518
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 21 deletions.
34 changes: 24 additions & 10 deletions recipes-extended/configure-scripts/files/configure-network
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import subprocess
import sys


## vars ########################################################################
# vars ###################################################################
iface = "wlan0"
net_conf_dir = "/etc/systemd/network/"
net_conf_file = net_conf_dir + "/20-net.network"
Expand All @@ -28,16 +28,20 @@ network={
}
'''

## functions ###################################################################
# functions ##############################################################


def pretty_print(line, prefix=""):
print(prefix + line.decode('utf-8'))


def print_ssid(line, prefix=""):
if line.find(b'SSID:') >= 0:
_ = line.split(b'SSID:')
if len(_) > 1 and len(_[1].strip()) > 0:
pretty_print(_[1], prefix)


def call(command, interrupt=False, pp=pretty_print):
print("\033[0;32m>>> %s\033[0m" % command)
proc = subprocess.Popen(command.split(), stdout=subprocess.PIPE)
Expand All @@ -50,12 +54,18 @@ def call(command, interrupt=False, pp=pretty_print):
print("!!! Failure")
sys.exit(1)


def wpa_psk(ssid, password):
dk = hashlib.pbkdf2_hmac('sha1', str.encode(password), str.encode(ssid), 4096, 32)
dk = hashlib.pbkdf2_hmac(
'sha1',
str.encode(password),
str.encode(ssid),
4096,
32)
return (binascii.hexlify(dk)).decode('utf8')


## command line options parser #################################################
# command line options parser ############################################
parser = argparse.ArgumentParser()
parser.add_argument("-i", "--interface", dest="iface", default=iface,
help=" (default: %s)" % iface)
Expand All @@ -67,13 +77,13 @@ iface = args.iface
disable = args.disable
wait = args.wait

## main ####################################################################
# main #####################################################################
if __name__ == "__main__":
if os.geteuid() != 0:
print("\033[0;31mYou need root permissions to run this command.\033")
sys.exit(2)

if False: # Deprecated code
if False: # Deprecated code
call("rm -rf %s/*" % net_conf_dir)
config = configparser.ConfigParser()
config['Match'] = {}
Expand All @@ -90,8 +100,11 @@ if __name__ == "__main__":
call("rm %s" % wpa_suplicant_conf_file % iface)
else:
call("iw dev %s scan" % iface, pp=print_ssid)
ssid = input("\033[0;37mEnter the WiFi SSID (in iface %s):\033[0m " % iface)
passphrase = input("\033[0;37mEnter the WiFi passphrase (Min. 8 chars):\033[0m ")
ssid = input(
"\033[0;37mEnter the WiFi SSID (in iface %s):\033[0m " %
iface)
passphrase = input(
"\033[0;37mEnter the WiFi passphrase (Min. 8 chars):\033[0m ")

if not os.path.exists(wpa_suplicant_conf_dir):
call("mkdir %s" % wpa_suplicant_conf_dir)
Expand All @@ -103,10 +116,11 @@ if __name__ == "__main__":
call("systemctl restart -l wpa_supplicant@%s.service" % iface)
call("systemctl status -l wpa_supplicant@%s.service" % iface)

ssid = print("\033[1;32mWaiting %s seconds to get IP in %s ...\033[0m " % (wait_seconds, iface))
ssid = print(
"\033[1;32mWaiting %s seconds to get IP in %s ...\033[0m " %
(wait_seconds, iface))
time.sleep(wait_seconds)
call("ip address show %s" % iface)
if wait:
print("\033[1;32mPress any key to exit ...\033[0m ")
input()

30 changes: 19 additions & 11 deletions recipes-extended/configure-scripts/files/configure-weston
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,15 @@ import os
import subprocess
import sys

## vars ########################################################################
# vars ###################################################################
configfile = '/etc/xdg/weston/weston.ini'


## functions ###################################################################
# functions ##############################################################
def pretty_print(line, prefix=""):
print(prefix + line.decode('utf-8'))


def call(command, interrupt=False, pp=pretty_print):
print("\033[0;32m>>> %s\033[0m" % command)
proc = subprocess.Popen(command.split(), stdout=subprocess.PIPE)
Expand All @@ -29,6 +30,7 @@ def call(command, interrupt=False, pp=pretty_print):
print("!!! Failure")
sys.exit(1)


def set_option(config, section, option, defaults={}):
if not config.has_section(section):
config[section] = {}
Expand All @@ -37,30 +39,37 @@ def set_option(config, section, option, defaults={}):
value = config.get(section, option, fallback=None)
if not value:
status = "default"
value = defaults.get(section, {}).get(option,"")
new_value = input("\033[1;33m>>> \033[0;37m[%s][%s] (%s: '%s'):\033[0m " % (section, option, status, value))
value = defaults.get(section, {}).get(option, "")
new_value = input(
"\033[1;33m>>> \033[0;37m[%s][%s] (%s: '%s'):\033[0m " %
(section, option, status, value))
config[section][option] = new_value if new_value else value


## command line options parser #################################################
# command line options parser ############################################
parser = argparse.ArgumentParser()
parser.add_argument("-c", "--configfile", dest="configfile", default=configfile,
help=" (default: %s)" % configfile)
parser.add_argument(
"-c",
"--configfile",
dest="configfile",
default=configfile,
help=" (default: %s)" %
configfile)

args = parser.parse_args()
configfile = args.configfile


## main ####################################################################
# main #####################################################################
if __name__ == "__main__":
if os.geteuid() != 0:
print("\033[0;31mYou need root permissions to run this command.\033")
sys.exit(2)

config = configparser.ConfigParser()
config.read(configfile)
config.get("core","idle-time", fallback=None)
config.get("core","", fallback=None)
config.get("core", "idle-time", fallback=None)
config.get("core", "", fallback=None)

defaults = {}
defaults['core'] = {}
Expand Down Expand Up @@ -92,4 +101,3 @@ if __name__ == "__main__":

call("diff -u %s.prev %s" % (configfile, configfile))
call("systemctl restart [email protected]")

0 comments on commit 902a518

Please sign in to comment.