Refactoring, add test_suite to setup.py

parent ffa0e145
include redirector/conf/config.ini include redirector/conf/config.ini
include redirector/tests/*.ini
include redirector/tests/*.map
include redirector/tests/*.yaml
include LICENSE include LICENSE
include README.md include README.md
import sys
import os
import pyinotify
import asyncore
import argparse as ap import argparse as ap
from redirector.utils import generators from redirector.utils import generators
......
NAME = "nginx-redirector" NAME = "nginx-redirector"
VERSION = "1.0" VERSION = "1.0.0"
AUTHOR = "Nikita Yefremov" AUTHOR = "Nikita Yefremov, David Dobryakov"
EMAIL = "enk@etersoft.ru" EMAIL = "kantegory@etersoft.ru"
PYTHON_VERSION = "3.6" PYTHON_VERSION = "3.8.1"
VERSION_STATUS = "alpha" VERSION_STATUS = "alpha"
...@@ -19,8 +19,6 @@ class Generator: ...@@ -19,8 +19,6 @@ class Generator:
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])
#config = Config()
#config.read_config()
maps_dir, config_dir = self.get_conf_dirs() maps_dir, config_dir = self.get_conf_dirs()
......
...@@ -5,6 +5,7 @@ try: ...@@ -5,6 +5,7 @@ try:
except ImportError: except ImportError:
from yaml import Loader, Dumper from yaml import Loader, Dumper
import re import re
from redirector.utils.utils import get_map_path
class MapLineParser: class MapLineParser:
...@@ -96,16 +97,20 @@ class ConfigReader: ...@@ -96,16 +97,20 @@ class ConfigReader:
return_list = [] return_list = []
yaml_dir = os.path.dirname(os.path.abspath(file_dir)) yaml_dir = os.path.dirname(os.path.abspath(file_dir))
with open(file_dir, 'r') as stream: stream = open(file_dir, 'r')
data = load(stream, Loader=Loader) data = load(stream, Loader=Loader)
for project in data.get("projects"): for project in data.get("projects"):
map_path = project.get("map") map_path = project.get("map")
rel_map_path = os.path.relpath(map_path, start=yaml_dir)
abs_map_path = os.path.join(rel_map_path, yaml_dir, os.path.basename(map_path)) abs_map_path = get_map_path(map_path, yaml_dir)
abs_map_path = os.path.normpath(abs_map_path)
project_prefix = project.get("prefix") project_prefix = project.get("prefix")
return_list.append((abs_map_path, project_prefix)) return_list.append((abs_map_path, project_prefix))
stream.close()
return return_list return return_list
def parse_map(self, map_file, yaml_file): def parse_map(self, map_file, yaml_file):
...@@ -118,14 +123,16 @@ class ConfigReader: ...@@ -118,14 +123,16 @@ class ConfigReader:
try: try:
for map_path, prefix in self.parse_yaml(yaml_file): for map_path, prefix in self.parse_yaml(yaml_file):
rel_map_path = os.path.relpath(map_path, start=yaml_dir) # restore rel path for map file abs_map_path = get_map_path(map_path, yaml_dir)
abs_map_path = os.path.join(rel_map_path, yaml_dir, os.path.basename(map_path)) # join rel path with yaml dir and map filename
abs_map_path = os.path.normpath(abs_map_path) # normalize path for removing "../", "./" and etc. if map_file_name not in abs_map_path:
continue
if map_file_name in abs_map_path:
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
with open(abs_map_path, "r") as file:
for i, line in enumerate(file): f = open(abs_map_path, "r")
for i, line in enumerate(f):
request_url, redirect_url, option = None, None, [] request_url, redirect_url, option = None, None, []
try: try:
return_code, request_url, redirect_url, *option = \ return_code, request_url, redirect_url, *option = \
...@@ -135,19 +142,21 @@ class ConfigReader: ...@@ -135,19 +142,21 @@ class ConfigReader:
except MapLineParser.ParseLineError as e: except MapLineParser.ParseLineError as e:
self.logger.log(e.message % map_file) self.logger.log(e.message % map_file)
if not request_url or not redirect_url or return_code == -1: if request_url and redirect_url and return_code in [0, 1]:
continue
else:
if return_code == 0: if return_code == 0:
res[0].append((request_url, redirect_url)) res[0].append((request_url, redirect_url))
elif return_code == 1: elif return_code == 1:
opt_ = option[0][0] if option else option opt_ = option[0][0] if option else option
if opt_ in res[1].keys(): if opt_ in res[1].keys():
res[1][opt_].append([request_url, redirect_url]) res[1][opt_].append([request_url, redirect_url])
else: else:
res[1][opt_] = [(request_url, redirect_url)] res[1][opt_] = [(request_url, redirect_url)]
f.close()
return res, res_prefix return res, res_prefix
except YAMLError as e: except YAMLError as e:
self.logger.log("Error occurred while reading %s" % yaml_file + str(e)) self.logger.log("Error occurred while reading %s" % yaml_file + str(e))
import os
def get_map_path(filename, dirname):
rel_map_path = os.path.relpath(filename, start=dirname)
abs_map_path = os.path.join(rel_map_path, dirname, os.path.basename(filename))
abs_map_path = os.path.normpath(abs_map_path)
return abs_map_path
import sys
import os import os
import pyinotify import pyinotify
import asyncore
import argparse as ap import argparse as ap
from redirector.utils import generators from redirector.utils import generators
...@@ -104,6 +102,6 @@ def watch(args=None): ...@@ -104,6 +102,6 @@ def watch(args=None):
redirector_watch.watch(args.filename[0]) redirector_watch.watch(args.filename[0])
#if __name__ == "__main__": if __name__ == "__main__":
# watch() watch()
...@@ -25,7 +25,7 @@ setup( ...@@ -25,7 +25,7 @@ setup(
long_description=open('README.md').read(), long_description=open('README.md').read(),
author="Nikita Efremov, Dobryakov David", author="Nikita Efremov, Dobryakov David",
author_email="kantegory@etersoft.ru", author_email="kantegory@etersoft.ru",
url="https://gitlab.eterfund.ru/eterfund/nginx-redirector/tree/dev-enhancement", url="https://gitlab.eterfund.ru/eterfund/nginx-redirector/",
license="ISC", license="ISC",
packages=['redirector', 'redirector.utils', 'redirector.tests', 'redirector.conf'], packages=['redirector', 'redirector.utils', 'redirector.tests', 'redirector.conf'],
entry_points={ entry_points={
...@@ -41,6 +41,7 @@ setup( ...@@ -41,6 +41,7 @@ setup(
'pyinotify', 'pyinotify',
'pyyaml' 'pyyaml'
], ],
include_package_data=True include_package_data=True,
test_suite="redirector.tests"
) )
projects: projects:
- map: ./test2.map
prefix: /test3
- map: ./test.map - map: ./test.map
prefix: /test prefix: /test
- map: /home/kantegory/pvt/nginx-redirector/tests/test2.map
prefix: /test2
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