From 8c71086242fd5f5cb958351fc89a0878cdfac78f Mon Sep 17 00:00:00 2001 From: Adam Gausmann Date: Tue, 29 Jan 2019 19:45:45 -0600 Subject: [PATCH] Finish template engine --- Pipfile | 1 + Pipfile.lock | 10 +++++++++- hosts.toml | 4 ++++ install.py | 39 ++++++++++++++++++++++++++++++++++++--- 4 files changed, 50 insertions(+), 4 deletions(-) create mode 100644 hosts.toml diff --git a/Pipfile b/Pipfile index dafab2b..43c9b86 100644 --- a/Pipfile +++ b/Pipfile @@ -8,6 +8,7 @@ name = "pypi" [packages] mako = "*" +toml = "*" [dev-packages] diff --git a/Pipfile.lock b/Pipfile.lock index 7345608..98bfd21 100644 --- a/Pipfile.lock +++ b/Pipfile.lock @@ -1,7 +1,7 @@ { "_meta": { "hash": { - "sha256": "5f2a777077fa545ffa7e39a4b62c35aae42497d7e4786940f3e4b79fac2c5737" + "sha256": "aa3152fd503bd5d4cad9dccb32c96b17ad9ba583118340e3a8c4d7ef86a5acf0" }, "host-environment-markers": { "implementation_name": "cpython", @@ -67,6 +67,14 @@ "sha256:4e97332c9ce444b0c2c38dd22ddc61c743eb208d916e4265a2a3b575bdccb1d3" ], "version": "==1.1.0" + }, + "toml": { + "hashes": [ + "sha256:f1db651f9657708513243e61e6cc67d101a39bad662eaa9b5546f789338e07a3", + "sha256:235682dd292d5899d361a811df37e04a8828a5b1da3115886b73cf81ebc9100e", + "sha256:229f81c57791a41d65e399fc06bf0848bab550a9dfd5ed66df18ce5f05e73d5c" + ], + "version": "==0.10.0" } }, "develop": {} diff --git a/hosts.toml b/hosts.toml new file mode 100644 index 0000000..28be823 --- /dev/null +++ b/hosts.toml @@ -0,0 +1,4 @@ +[ayypad.i3status] +wireless = ["wlp4s0"] +ethernet = ["enp2s0"] +disks = ["/", "/home"] diff --git a/install.py b/install.py index e9b0159..109c510 100755 --- a/install.py +++ b/install.py @@ -2,10 +2,14 @@ import argparse import os +import socket import sys from pathlib import Path -from mako.lookup import TemplateLookup +import mako.lookup +import mako.template +import toml + def main(): parser = argparse.ArgumentParser( @@ -20,16 +24,45 @@ def main(): ) parser.add_argument( '-n', '--hostname', - help='The hostname or other identifying name of this system.', - default=os.environ.get('HOSTNAME'), + help='The hostname or other identifying name of this system that will' + ' be used to retrieve the host-specific configuration.', + default=os.environ.get('HOSTNAME') or socket.gethostname(), ) parser.add_argument( '-o', '--home', help='The home directory where generated dotfiles will be installed.', + type=Path, default=os.environ.get('HOME') or Path.home(), ) args = parser.parse_args() + templates_dir = args.dotfiles / 'templates' + include_dir = args.dotfiles / 'include' + + with open(args.dotfiles / 'hosts.toml') as hosts_file: + hosts_config = toml.load(hosts_file) + + lookup = mako.lookup.TemplateLookup( + directories=[ + str(templates_dir), + str(include_dir), + ], + ) + + for template_path in templates_dir.glob('**/*'): + if not template_path.is_file(): + continue + template = mako.template.Template( + filename=str(template_path), + strict_undefined=True, + lookup=lookup, + ) + output = template.render( + host=hosts_config[args.hostname] + ) + output_path = args.home / template_path.relative_to(templates_dir) + with open(output_path, 'w+') as output_file: + output_file.write(output) main()