From a247e4a0ed017e94ac5101391956f11f77eb8a2c Mon Sep 17 00:00:00 2001
From: Attila Farkas <x394097@fi.muni.cz>
Date: Fri, 16 Aug 2019 12:24:36 +0200
Subject: [PATCH] Vagrantfile is created directly

---
 README.md                 |  2 +-
 generate.py               |  4 ++--
 modules/device_creator.py | 13 +++++++++++++
 3 files changed, 16 insertions(+), 3 deletions(-)

diff --git a/README.md b/README.md
index 5901368..0863232 100644
--- a/README.md
+++ b/README.md
@@ -8,7 +8,7 @@ generate.py is a python program that generates a vagrant source file from a defi
 ### Usage:
 1. Clone the project.
 2. Navigate to the project folder.
-3. Type `$ python generate.py yaml_file.yaml > Vagrantfile`. There is a test yaml file in the repository called test.yaml.
+3. Type `$ python generate.py yaml_file.yaml`. There is a test yaml file in the repository called test.yaml.
 4. Run `$ vagrant up`
 
 ### Implemented attribute types:
diff --git a/generate.py b/generate.py
index 58c5be9..99a0a6a 100644
--- a/generate.py
+++ b/generate.py
@@ -4,7 +4,7 @@
 import sys
 import jinja2
 
-from modules.device_creator import create_devices
+from modules.device_creator import create_devices, create_vagrantfile
 
 if len(sys.argv) != 2:
     print("Error: Expecting 1 argument (yml file).")
@@ -19,4 +19,4 @@ TEMPLATE_ENV = jinja2.Environment(loader=TEMPLATE_LOADER, trim_blocks=True)
 BASE_TEMPLATE = TEMPLATE_ENV.get_template("base")
 OUTPUT = BASE_TEMPLATE.render(devices=DEVICES)
 
-print(OUTPUT)
+create_vagrantfile(OUTPUT)
diff --git a/modules/device_creator.py b/modules/device_creator.py
index ae7e936..c34937d 100644
--- a/modules/device_creator.py
+++ b/modules/device_creator.py
@@ -57,3 +57,16 @@ def create_devices(yml_file_name):
     return {
         **_create_hosts(yml, mappings, flavors),
         **_create_routers(yml)}
+
+
+def create_vagrantfile(output):
+    """ Writes the prepared output to a Vagrantfile. """
+    filename = "Vagrantfile"
+
+    try:
+        vagrantfile = open(filename, "w")
+        vagrantfile.write(output)
+    except IOError:
+        print("Error: cannot write to this location.")
+
+    print("Vagrantfile successfully created.")
-- 
GitLab