Commit e9910f24 authored by Soldatoff's avatar Soldatoff

remove the extra slash, which works without a prefix. Fixed .conf generation without prefix

parent 24a459b0
...@@ -3,7 +3,7 @@ ...@@ -3,7 +3,7 @@
%define biname nginx-redirector %define biname nginx-redirector
Name: nginx-redirector Name: nginx-redirector
Version: 0.2.0 Version: 0.2.0.1
Release: alt1 Release: alt1
Summary: CLI-utility for building nginx redirects Summary: CLI-utility for building nginx redirects
......
...@@ -14,13 +14,13 @@ class Redirector: ...@@ -14,13 +14,13 @@ class Redirector:
project_name = "Error" project_name = "Error"
try: try:
data, project_name = self.parser.parse_map(map_file, yaml_file) data, project_name = self.parser.parse_map(map_file, yaml_file)
if not project_name: if not data:
raise self.RedirectorInputDataError("Can\'t properly parse .map or .yaml files", Exception()) raise self.RedirectorInputDataError("Can\'t properly parse .map or .yaml files", Exception())
# FIXME: what is the better way to do so? # FIXME: what is the better way to do so?
except Exception as e: except Exception as e:
raise self.RedirectorParserError("Can\'t parse .map file %s" % map_file, e) raise self.RedirectorParserError("Can\'t parse .map file %s" % map_file, e)
self.generator.generate(data, project_name) self.generator.generate(data, project_name, map_file)
class RedirectorParserError(Exception): class RedirectorParserError(Exception):
def __init__(self, message, errors): def __init__(self, message, errors):
......
from redirector.utils.const import MAPS_DIR, CONFIG_DIR from redirector.utils.const import MAPS_DIR, CONFIG_DIR, CONFIG
from redirector.utils.parser import ConfigReader
class Generator: class Generator:
def __init__(self): def __init__(self):
...@@ -9,15 +10,20 @@ class Generator: ...@@ -9,15 +10,20 @@ class Generator:
return MAPS_DIR, CONFIG_DIR return MAPS_DIR, CONFIG_DIR
def generate(self, redirects_data, project_name): def generate(self, redirects_data, project_name, arg_map = None):
if len(ConfigReader.parse_yaml(CONFIG)[0][1]) > len("/"):
prefix = ConfigReader.parse_yaml(CONFIG)[0][1].split("/")[-1]
redirects_map = self.map_gen.generate_map(redirects_data[0], "/{}".format(project_name))
else:
prefix = arg_map.split("/")[-2]
redirects_map = self.map_gen.generate_map(redirects_data[0], project_name) redirects_map = self.map_gen.generate_map(redirects_data[0], project_name)
redirects_with_options = self.map_gen.generate_opt_map(redirects_data[1], project_name) redirects_with_options = self.map_gen.generate_opt_map(redirects_data[1], project_name)
conf_data = self.conf_gen.generate_config(project_name, [code for code, data in redirects_with_options]) conf_data = self.conf_gen.generate_config(project_name, [code for code, data in redirects_with_options],prefix)
maps_dir, config_dir = self.get_conf_dirs() maps_dir, config_dir = self.get_conf_dirs()
try: try:
with open("{}/{}.map".format(maps_dir,project_name), "w") as map_file: with open("{}/{}.map".format(maps_dir,prefix), "w") as map_file:
map_file.write(redirects_map) map_file.write(redirects_map)
for code, data in redirects_with_options: for code, data in redirects_with_options:
if code == "301": if code == "301":
...@@ -26,7 +32,7 @@ class Generator: ...@@ -26,7 +32,7 @@ class Generator:
code = "redirect" code = "redirect"
with open(maps_dir + "/%s_%s_options.map" % (project_name, code), "w") as map_file: with open(maps_dir + "/%s_%s_options.map" % (project_name, code), "w") as map_file:
map_file.write(data) map_file.write(data)
with open(config_dir + "/%s.conf" % project_name, "w") as conf_file: with open(config_dir + "/%s.conf" % prefix, "w") as conf_file:
conf_file.write(conf_data) conf_file.write(conf_data)
except Exception as e: except Exception as e:
print(e) print(e)
...@@ -51,7 +57,7 @@ class MapGenerator: ...@@ -51,7 +57,7 @@ class MapGenerator:
data = self.start_line % project_name data = self.start_line % project_name
for arg1, arg2 in redirects: for arg1, arg2 in redirects:
# print('redirect is', arg1, arg2) # print('redirect is', arg1, arg2)
data += "\n\t/%s\t/%s;" % (project_name+arg1, project_name+arg2) data += "\n\t%s\t%s;" % (project_name+arg1, project_name+arg2)
data += self.endline data += self.endline
return data return data
...@@ -75,12 +81,15 @@ class ConfigGenerator: ...@@ -75,12 +81,15 @@ class ConfigGenerator:
def __init__(self): def __init__(self):
self.start_line = "if ($%s_redirect) {\n" self.start_line = "if ($%s_redirect) {\n"
self.opt_start_line = "if ($%s_%s_redirect) {\n" self.opt_start_line = "if ($%s_%s_redirect) {\n"
self.rewrite_line = "\trewrite ^/%s/(.*)$ $%s_redirect %s;\n" self.rewrite_line = "\trewrite ^%s/(.*)$ $%s_redirect %s;\n"
self.opt_rewrite_line = "\trewrite ^/%s/(.*)$ $%s_%s_redirect %s;\n" self.opt_rewrite_line = "\trewrite ^/%s/(.*)$ $%s_%s_redirect %s;\n"
self.option_line = "return %s;\n}\n" self.option_line = "return %s;\n}\n"
def generate_config(self, project_name, options): def generate_config(self, project_name, options, prefix):
data = (self.start_line % project_name) + (self.rewrite_line % (project_name, project_name, "redirect")) + "}\n" if len(ConfigReader.parse_yaml(CONFIG)[0][1]) > len("/"):
data = (self.start_line % project_name) + (self.rewrite_line % ("/{}".format(prefix), project_name, "redirect")) + "}\n"
else:
data = (self.start_line % project_name) + (self.rewrite_line % ("", project_name, "redirect")) + "}\n"
for code in options: for code in options:
if code == "permanent": if code == "permanent":
......
...@@ -123,8 +123,8 @@ class ConfigReader: ...@@ -123,8 +123,8 @@ class ConfigReader:
for map_path, prefix in self.parse_yaml(yaml_file): for map_path, prefix in self.parse_yaml(yaml_file):
abs_map_path = get_map_path(map_path, map_dir) abs_map_path = get_map_path(map_path, map_dir)
if map_file_name not in abs_map_path: #if map_file_name not in abs_map_path:
continue # continue
res_prefix = prefix.split("/")[-1] # Last directory of map_path a project's name res_prefix = prefix.split("/")[-1] # Last directory of map_path a project's name
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment