diff --git a/naxalnet/__init__.py b/naxalnet/__init__.py index 7d33246..efc9152 100644 --- a/naxalnet/__init__.py +++ b/naxalnet/__init__.py @@ -16,4 +16,17 @@ # along with this program. If not, see . -__version__ = "0.2.0a3" +# GUIDE FOR CHANGING __version__ +# +# All commits in master should have the version as +# {last published version tag}.a{1,2,3,...} +# example: 0.2.0a3 should mean 3 commits after tag v0.2.0 +# +# All commits in other branches should +# have {version in master}.dev{1,2,...} +# example: 0.2.0a3.dev1 should mean 1 commit in the new +# branch after the commit in master. +# +# In case you forgot to add a version, put the next number +# in the next commit +__version__ = "0.2.0a3.dev1" diff --git a/naxalnet/__main__.py b/naxalnet/__main__.py index f2ebcf9..624a524 100644 --- a/naxalnet/__main__.py +++ b/naxalnet/__main__.py @@ -15,6 +15,13 @@ # You should have received a copy of the GNU General Public License # along with this program. If not, see . +""" +naxalnet +======== + +If called as python -m naxalnet, this file makes naxalnet run like +it was called from the commandline. +""" from naxalnet.scripts import here_be_dragons diff --git a/naxalnet/config.py b/naxalnet/config.py new file mode 100644 index 0000000..3d472d5 --- /dev/null +++ b/naxalnet/config.py @@ -0,0 +1,107 @@ +#!/usr/bin/env python3 + +""" +config.py +--------- + +This file contains functions to parse configuration files +and arguments. Most of these functions are meant to be used +by parse_args() internally, so only parse_args() should +be imported outside this file. +""" + +from pathlib import Path +from configparser import ConfigParser +from argparse import ArgumentParser +from naxalnet.default import CONFIG, CONFIG_FILES, CONFIG_DIRS + + +def get_config_files(): + """returns list of configuration files as Path objects to parse""" + config_files = [] + for directory in CONFIG_DIRS: + path = Path(directory) + if path.exists(): + for i in CONFIG_FILES: + glob = path.glob(i) + config_files.extend(glob) + return config_files + + +def parse_config(): + """ + Parse all configuration files, with the values in + default.py as fallback + """ + parser = ConfigParser() + # encoded defaults + parser.read_dict(CONFIG) + # read config files + files = get_config_files() + for i in files: + parser.read_file(i.open()) + return parser + + +def parse_args(): + """ + Parse all arguments and return ArgumentParser.parse_args(), + with values in config files as fallback. Ideally, only this + function should be used by naxalnet to get arguments and + configuration. + """ + config = parse_config() + parser = ArgumentParser( + description="setup batman-adv networks with systemd and iwd" + ) + parser.add_argument( + "--ap-ssid", + "-n", + type=str, + help="SSID of the WiFi AP", + default=config["ap"]["ssid"], + ) + parser.add_argument( + "--ap-passwd", + "-p", + "--ap-password", + type=str, + help="password of the WiFi AP", + default=config["ap"]["passwd"], + ) + parser.add_argument( + "--adhoc-name", + "-a", + type=str, + default=config["adhoc"]["name"], + help="name of adhoc network", + ) + # TODO: print info about wifi network from config and args and exit + parser.add_argument( + "--print-wifi", + action="store_true", + default=False, + help="prints the ssid and password of the WiFi network and exit", + ) + parser.add_argument( + "--networkd-config-dir", + type=str, + default=config["networkd"]["confdir"], + help="the directory where systemd-networkd configuration files are stored", + ) + parser.add_argument( + "--networkd-runtime-dir", + type=str, + default=config["networkd"]["runtimedir"], + help="the directory where configuration files of systemd-networkd should be copied", + ) + # TODO: implement --verbose + # parser.add_argument( + # "-v", + # "--verbose", + # help="increase output verbosity; can be used multiple times", + # action="count", + # default=0, + # ) + + return parser.parse_args() diff --git a/naxalnet/default.py b/naxalnet/default.py new file mode 100644 index 0000000..6e82375 --- /dev/null +++ b/naxalnet/default.py @@ -0,0 +1,19 @@ +#!/usr/bin/env python3 + +""" +This file contains default values for configuration. +The values are likely to be replaced by other configuration files. +""" + +CONFIG = { + "networkd": { + "confdir": "/usr/share/naxalnet/networkd", + "runtimedir": "/run/systemd/network", + }, + "adhoc": {"name": "NxMesh"}, + "ap": {"ssid": "MeshWiFi", "passwd": "naxalnet256"}, +} + +# glob +CONFIG_FILES = ["naxalnet.conf", "naxalnet.d/*.conf"] +CONFIG_DIRS = ["/etc"] diff --git a/naxalnet/scripts.py b/naxalnet/scripts.py index 244703f..dd5384d 100644 --- a/naxalnet/scripts.py +++ b/naxalnet/scripts.py @@ -23,9 +23,9 @@ with systemd-networkd and iwd import sys from pathlib import Path from shutil import copy -from argparse import ArgumentParser from dasbus.error import DBusError from naxalnet.iwd import IWD, Device, Adapter +from naxalnet.config import parse_args NETWORKD_CONFIGS = "/usr/share/naxalnet/networkd" NETWORKD_VOLATILE_DIR = "/run/systemd/network" @@ -35,7 +35,7 @@ AP_SSID = "NaxalNet" AP_PASSWD = "naxalnet256" -def copy_files(): +def copy_files(args): """ Copy networkd configs to volatile dir. The D-Bus API does not support creating new interfaces @@ -44,8 +44,8 @@ def copy_files(): """ print("Copying network config files") - dest = Path(NETWORKD_VOLATILE_DIR) - src = Path(NETWORKD_CONFIGS) + dest = Path(args.networkd_runtime_dir) + src = Path(args.networkd_config_dir) # Create the volatile directory if it doesn't exist dest.mkdir(parents=True, exist_ok=True) @@ -111,15 +111,13 @@ def here_be_dragons(): execute naxalnet from commandline """ args = parse_args() + try: - copy_files() + copy_files(args) except PermissionError as error: print(error) sys.exit("Make sure you are root") - # TODO: implement the daemon here so that it will call setup_devices - # every time a device is connected or removed. - # Now, the iwd part try: setup_devices(args) except DBusError as error: @@ -128,45 +126,3 @@ def here_be_dragons(): # naxalnet will print Bye if no errors occured print("Bye") - - -def parse_args(): - """parse all arguments and return ArgumentParser.parse_args()""" - parser = ArgumentParser( - description="setup batman-adv networks with systemd and iwd" - ) - parser.add_argument( - "--ap-ssid", - "-n", - type=str, - help="SSID of the WiFi AP", - default=AP_SSID, - ) - parser.add_argument( - "--ap-passwd", - "-p", - "--ap-password", - type=str, - help="password of the WiFi AP", - default=AP_PASSWD, - ) - parser.add_argument( - "--adhoc-name", "-a", type=str, default=ADHOC_NAME, help="name of adhoc network" - ) - # TODO: print info about wifi network from config and args - parser.add_argument( - "--print-wifi", - action="store_true", - default=False, - help="prints the ssid and password of the WiFi network and exit", - ) - # TODO: implement --verbose - parser.add_argument( - "-v", - "--verbose", - help="increase output verbosity; can be used multiple times", - action="count", - default=0, - ) - - return parser.parse_args() diff --git a/setup.cfg b/setup.cfg index 4ab3cc1..5d0a632 100644 --- a/setup.cfg +++ b/setup.cfg @@ -18,6 +18,9 @@ packages = find: python_requires = >=3.6 install_requires = dasbus + configparser + pathlib + argparse [options.entry_points] console_scripts =