Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
N
nginx-redirector
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Registry
Registry
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
eterfund
nginx-redirector
Commits
36a4d1b2
Commit
36a4d1b2
authored
Apr 21, 2022
by
Soldatoff
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Changed the processing and formatting of project_name, prefix variables
parent
44f1a1a5
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
95 additions
and
85 deletions
+95
-85
redirector.py
redirector/redirector.py
+3
-3
generators.py
redirector/utils/generators.py
+47
-48
parser.py
redirector/utils/parser.py
+45
-34
No files found.
redirector/redirector.py
View file @
36a4d1b2
...
...
@@ -13,14 +13,14 @@ class Redirector:
def
generate
(
self
,
yaml_file
,
map_file
):
project_name
=
"Error"
try
:
data
,
project_name
=
self
.
parser
.
parse_map
(
map_file
,
yaml_file
)
data
=
self
.
parser
.
read_map
(
map_file
)
project_name
,
prefix
=
self
.
parser
.
parse_map
(
map_file
,
yaml_file
)
if
not
data
:
raise
self
.
RedirectorInputDataError
(
"Can
\'
t properly parse .map or .yaml files"
,
Exception
())
# FIXME: what is the better way to do so?
except
Exception
as
e
:
raise
self
.
RedirectorParserError
(
"Can
\'
t parse .map file
%
s"
%
map_file
,
e
)
self
.
generator
.
generate
(
data
,
project_name
,
map_file
)
self
.
generator
.
generate
(
data
,
project_name
,
prefix
)
class
RedirectorParserError
(
Exception
):
def
__init__
(
self
,
message
,
errors
):
...
...
redirector/utils/generators.py
View file @
36a4d1b2
from
redirector.utils.const
import
MAPS_DIR
,
CONFIG_DIR
,
CONFIG
from
redirector.utils.parser
import
ConfigReader
from
redirector.utils.const
import
MAPS_DIR
,
CONFIG_DIR
class
Generator
:
def
__init__
(
self
):
...
...
@@ -9,44 +8,40 @@ class Generator:
def
get_conf_dirs
(
self
):
return
MAPS_DIR
,
CONFIG_DIR
def
get_essential
(
self
,
arg_map
=
None
):
return_dict
=
{
"project_name"
:
''
,
"prefix"
:
''
,
"custom"
:
''
,
"dir_name"
:
''
}
if
arg_map
:
return_dict
[
"dir_name"
]
=
arg_map
.
split
(
"/"
)[
-
2
]
if
len
(
ConfigReader
.
parse_yaml
(
CONFIG
)[
0
][
1
])
>
len
(
"/"
):
#prefix != "/"
return_dict
[
"prefix"
]
=
(
ConfigReader
.
parse_yaml
(
CONFIG
)[
0
][
1
]
.
split
(
"/"
)[
-
1
])
return_dict
[
"project_name"
]
=
(
ConfigReader
.
parse_yaml
(
CONFIG
)[
0
][
1
]
.
split
(
"/"
)[
-
1
])
elif
len
(
ConfigReader
.
parse_yaml
(
CONFIG
)[
0
][
2
])
>
len
(
"/"
):
#name != "/" and prefix == "/"
return_dict
[
"custom"
]
=
(
ConfigReader
.
parse_yaml
(
CONFIG
)[
0
][
2
]
.
split
(
"/"
)[
-
1
])
return_dict
[
"project_name"
]
=
return_dict
[
"custom"
]
+
"-"
+
return_dict
[
"dir_name"
]
else
:
#name == "/" and prefix == "/"
return_dict
[
"project_name"
]
=
return_dict
[
"dir_name"
]
return
return_dict
def
generate
(
self
,
redirects_data
,
project_name
,
arg_map
=
None
):
arg
=
self
.
get_essential
(
arg_map
)
redirects_map
=
self
.
map_gen
.
generate_map
(
redirects_data
[
0
],
arg
)
redirects_with_options
=
self
.
map_gen
.
generate_opt_map
(
redirects_data
[
1
],
arg
)
conf_data
=
self
.
conf_gen
.
generate_config
(
arg
[
"project_name"
],
[
code
for
code
,
data
in
redirects_with_options
],
arg
[
"prefix"
])
maps_dir
,
config_dir
=
self
.
get_conf_dirs
()
def
write_map
(
self
,
maps_dir
,
project_name
,
redirects_map
,
redirects_with_options
):
try
:
with
open
(
"{}/{}.map"
.
format
(
maps_dir
,
arg
[
"project_name"
]),
"w
"
)
as
map_file
:
with
open
(
"{}/{}.map"
.
format
(
maps_dir
,
project_name
),
"w+
"
)
as
map_file
:
map_file
.
write
(
redirects_map
)
for
code
,
data
in
redirects_with_options
:
if
code
==
"301"
:
code
=
"permanent"
elif
code
==
"302"
:
code
=
"redirect"
with
open
(
maps_dir
+
"/
%
s_
%
s_options.map"
%
(
arg
[
"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
)
with
open
(
config_dir
+
"/
%
s.conf"
%
arg
[
"project_name"
],
"w"
)
as
conf_file
:
except
Exception
as
e
:
print
(
e
)
raise
self
.
GenerationError
(
"Can
\'
t generate new .map file
\
from
%
s .map file for
%
s project"
%
(
map_file
,
project_name
),
e
)
def
write_conf
(
self
,
config_dir
,
project_name
,
conf_data
):
try
:
with
open
(
config_dir
+
"/
%
s.conf"
%
project_name
,
"w+"
)
as
conf_file
:
conf_file
.
write
(
conf_data
)
except
Exception
as
e
:
print
(
e
)
raise
self
.
GenerationError
(
"Can
\'
t generate new .conf or new .map file
\
from
%
s .map file for
%
s project"
%
(
map_file
,
project_name
),
e
)
raise
self
.
GenerationError
(
"Can
\'
t generate new .conf
\
from
%
s .conf file for
%
s project"
%
(
conf_file
,
project_name
),
e
)
def
generate
(
self
,
redirects_data
,
project_name
,
prefix
):
redirects_map
=
self
.
map_gen
.
generate_map
(
redirects_data
[
0
],
project_name
,
prefix
)
redirects_with_options
=
self
.
map_gen
.
generate_opt_map
(
redirects_data
[
1
],
project_name
,
prefix
)
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
()
self
.
write_map
(
maps_dir
,
project_name
,
redirects_map
,
redirects_with_options
)
self
.
write_conf
(
config_dir
,
project_name
,
conf_data
)
class
GenerationError
(
Exception
):
def
__init__
(
self
,
message
,
errors
):
...
...
@@ -62,38 +57,40 @@ class MapGenerator:
self
.
endline
=
"
\n
}"
def
generate_map
(
self
,
redirects
,
arg
):
if
arg
[
"prefix"
]
:
data
=
self
.
start_line
%
arg
[
"prefix"
]
def
generate_map
(
self
,
redirects
,
project_name
,
prefix
):
if
prefix
:
data
=
self
.
start_line
%
prefix
else
:
data
=
self
.
start_line
%
arg
[
"project_name"
]
if
arg
[
"project_name"
]
==
arg
[
"prefix"
]
:
arg
[
"prefix"
]
=
"/{}"
.
format
(
arg
[
"prefix"
]
)
data
=
self
.
start_line
%
project_name
if
project_name
==
prefix
:
prefix
=
"/{}"
.
format
(
prefix
)
for
arg1
,
arg2
in
redirects
:
# print('redirect is', arg1, arg2)
data
+=
"
\n\t
%
s
\t
%
s;"
%
(
arg
[
"prefix"
]
+
arg1
,
arg
[
"prefix"
]
+
arg2
)
data
+=
"
\n\t
%
s
\t
%
s;"
%
(
prefix
+
arg1
,
prefix
+
arg2
)
data
+=
self
.
endline
return
data
def
generate_opt_map
(
self
,
redirect_dict
,
arg
):
def
generate_opt_map
(
self
,
redirect_dict
,
project_name
,
prefix
):
result
=
[]
for
code
,
redirects
in
redirect_dict
.
items
():
if
project_name
==
prefix
:
prefix
=
"/{}"
.
format
(
prefix
)
if
code
==
"301"
:
code
=
"permanent"
data
=
self
.
opt_start_line
%
(
arg
[
"project_name"
]
,
code
)
data
=
self
.
opt_start_line
%
(
project_name
,
code
)
for
arg1
,
arg2
in
redirects
:
data
+=
"
\n\t
%
s
\t
%
s;"
%
(
arg
[
"prefix"
]
+
arg1
,
arg2
)
data
+=
"
\n\t
%
s
\t
%
s;"
%
(
prefix
+
arg1
,
arg2
)
data
+=
self
.
endline
elif
code
==
"302"
:
code
=
"redirect"
data
=
self
.
opt_start_line
%
(
arg
[
"project_name"
]
,
code
)
data
=
self
.
opt_start_line
%
(
project_name
,
code
)
for
arg1
,
arg2
in
redirects
:
data
+=
"
\n\t
%
s
\t
%
s;"
%
(
arg
[
"prefix"
]
+
arg1
,
arg
[
"prefix"
]
+
arg2
)
data
+=
"
\n\t
%
s
\t
%
s;"
%
(
prefix
+
arg1
,
prefix
+
arg2
)
data
+=
self
.
endline
elif
code
==
"status=301"
:
data
=
self
.
opt_start_line
%
(
arg
[
"project_name"
]
,
code
)
data
=
self
.
opt_start_line
%
(
project_name
,
code
)
for
arg1
,
arg2
in
redirects
:
data
+=
"
\n\t
%
s
\t
%
s;"
%
(
arg
[
"prefix"
]
+
arg1
,
arg
[
"prefix"
]
+
arg2
)
data
+=
"
\n\t
%
s
\t
%
s;"
%
(
prefix
+
arg1
,
prefix
+
arg2
)
data
+=
self
.
endline
result
.
append
((
code
,
data
))
return
result
...
...
@@ -105,17 +102,19 @@ class ConfigGenerator:
self
.
start_line
=
"if ($
%
s_redirect) {
\n
"
self
.
opt_start_line
=
"if ($
%
s_
%
s_redirect) {
\n
"
self
.
rewrite_line
=
"
\t
rewrite ^
%
s/(.*)$ $
%
s_redirect
%
s;
\n
"
self
.
opt_rewrite_line
=
"
\t
rewrite ^
/
%
s/(.*)$ $
%
s_
%
s_redirect
%
s;
\n
"
self
.
opt_rewrite_line
=
"
\t
rewrite ^
%
s/(.*)$ $
%
s_
%
s_redirect
%
s;
\n
"
self
.
option_line
=
"return
%
s;
\n
}
\n
"
def
generate_config
(
self
,
project_name
,
options
,
prefix
):
data
=
(
self
.
start_line
%
project_name
)
+
(
self
.
rewrite_line
%
(
"{}"
.
format
(
prefix
),
project_name
,
"redirect"
))
+
"}
\n
"
if
project_name
==
prefix
:
prefix
=
"/{}"
.
format
(
prefix
)
data
=
(
self
.
start_line
%
project_name
)
+
(
self
.
rewrite_line
%
(
prefix
,
project_name
,
"redirect"
))
+
"}
\n
"
for
code
in
options
:
if
code
==
"permanent"
:
data
+=
(
self
.
opt_start_line
%
(
project_name
,
"permanent"
))
+
(
self
.
opt_rewrite_line
%
(
pr
oject_name
,
project_name
,
"permanent"
,
"permanent"
))
+
"}
\n
"
data
+=
(
self
.
opt_start_line
%
(
project_name
,
"permanent"
))
+
(
self
.
opt_rewrite_line
%
(
pr
efix
,
project_name
,
"permanent"
,
"permanent"
))
+
"}
\n
"
elif
code
==
"redirect"
:
data
+=
(
self
.
opt_start_line
%
(
project_name
,
"redirect"
))
+
(
self
.
opt_rewrite_line
%
(
pr
oject_name
,
project_name
,
"redirect"
,
"redirect"
))
+
"}
\n
"
data
+=
(
self
.
opt_start_line
%
(
project_name
,
"redirect"
))
+
(
self
.
opt_rewrite_line
%
(
pr
efix
,
project_name
,
"redirect"
,
"redirect"
))
+
"}
\n
"
else
:
data
+=
(
self
.
opt_start_line
%
(
project_name
,
code
))
+
(
self
.
opt_rewrite_line
%
(
pr
oject_name
,
project_name
,
code
,
"redirect"
))
+
(
self
.
option_line
%
code
)
data
+=
(
self
.
opt_start_line
%
(
project_name
,
code
))
+
(
self
.
opt_rewrite_line
%
(
pr
efix
,
project_name
,
code
,
"redirect"
))
+
(
self
.
option_line
%
code
)
return
data
redirector/utils/parser.py
View file @
36a4d1b2
...
...
@@ -15,7 +15,7 @@ class MapLineParser:
r"^(?:http(s)?:\/\/)?[\w.-]+(?:\.[\w\.-]+)+[\w\-\._~:/?#[\]@!\$&'\(\)\*\+,;=.]+$"
)
self
.
logger
=
logger
def
parse_line
(
self
,
line
,
prefix
,
i
):
def
parse_line
(
self
,
line
,
i
):
line
=
line
.
strip
()
return_code
,
options
=
0
,
[]
"""
...
...
@@ -88,6 +88,20 @@ class ConfigReader:
self
.
line_parser
=
MapLineParser
(
logger
=
logger
)
if
logger
:
self
.
logger
=
logger
@staticmethod
def
get_projectname_prefix
(
map_path
,
prefix
,
custom_name
):
if
map_path
:
#path to map file
dir_name
=
map_path
.
split
(
"/"
)[
-
2
]
if
len
(
prefix
)
>
len
(
"/"
):
#prefix != "/"
project_name
=
(
prefix
.
split
(
"/"
)[
-
1
])
elif
len
(
custom_name
)
>
len
(
"/"
):
#name != "/" and prefix == "/"
custom
=
(
custom_name
.
split
(
"/"
)[
-
1
])
project_name
=
custom
+
"_"
+
dir_name
else
:
#name == "/" and prefix == "/"
project_name
=
dir_name
prefix
=
prefix
.
split
(
"/"
)[
-
1
]
return
project_name
,
prefix
@staticmethod
def
parse_yaml
(
file_dir
):
...
...
@@ -111,9 +125,33 @@ class ConfigReader:
return
return_list
def
parse_map
(
self
,
map_file
,
yaml_file
):
def
read_map
(
self
,
map_path
):
res
=
[[],
{}]
res_prefix
=
None
f
=
open
(
map_path
,
"r"
)
for
i
,
line
in
enumerate
(
f
):
request_url
,
redirect_url
,
option
=
None
,
None
,
[]
try
:
return_code
,
request_url
,
redirect_url
,
*
option
=
\
self
.
line_parser
.
parse_line
(
line
,
i
)
except
MapLineParser
.
RegexpTestError
as
e
:
self
.
logger
.
log
(
e
.
message
%
map_path
)
except
MapLineParser
.
ParseLineError
as
e
:
self
.
logger
.
log
(
e
.
message
%
map_path
)
if
request_url
and
redirect_url
and
return_code
in
[
0
,
1
]:
if
return_code
==
0
:
res
[
0
]
.
append
((
request_url
,
redirect_url
))
elif
return_code
==
1
:
opt_
=
option
[
0
][
0
]
if
option
else
option
if
opt_
in
res
[
1
]
.
keys
():
res
[
1
][
opt_
]
.
append
([
request_url
,
redirect_url
])
else
:
res
[
1
][
opt_
]
=
[(
request_url
,
redirect_url
)]
f
.
close
()
return
res
def
parse_map
(
self
,
map_file
,
yaml_file
):
# normalize path
yaml_dir
=
os
.
path
.
dirname
(
os
.
path
.
abspath
(
yaml_file
))
# get yaml file directory for restore rel path to map file
...
...
@@ -123,39 +161,12 @@ class ConfigReader:
try
:
for
map_path
,
prefix
,
name
in
self
.
parse_yaml
(
yaml_file
):
abs_map_path
=
get_map_path
(
map_path
,
map_dir
)
if
map_file_name
not
in
abs_map_path
:
continue
#if map_file_name not in abs_map_path:
# continue
res_prefix
=
prefix
.
split
(
"/"
)[
-
1
]
# Last directory of map_path a project's name
f
=
open
(
map_path
,
"r"
)
for
i
,
line
in
enumerate
(
f
):
request_url
,
redirect_url
,
option
=
None
,
None
,
[]
try
:
return_code
,
request_url
,
redirect_url
,
*
option
=
\
self
.
line_parser
.
parse_line
(
line
,
prefix
,
i
)
except
MapLineParser
.
RegexpTestError
as
e
:
self
.
logger
.
log
(
e
.
message
%
map_file
)
except
MapLineParser
.
ParseLineError
as
e
:
self
.
logger
.
log
(
e
.
message
%
map_file
)
if
request_url
and
redirect_url
and
return_code
in
[
0
,
1
]:
if
return_code
==
0
:
res
[
0
]
.
append
((
request_url
,
redirect_url
))
elif
return_code
==
1
:
opt_
=
option
[
0
][
0
]
if
option
else
option
if
opt_
in
res
[
1
]
.
keys
():
res
[
1
][
opt_
]
.
append
([
request_url
,
redirect_url
])
else
:
res
[
1
][
opt_
]
=
[(
request_url
,
redirect_url
)]
f
.
close
()
return
res
,
res_prefix
project_name
,
prefix
=
self
.
get_projectname_prefix
(
map_path
,
prefix
,
name
)
return
project_name
,
prefix
except
YAMLError
as
e
:
self
.
logger
.
log
(
"Error occurred while reading
%
s"
%
yaml_file
+
str
(
e
))
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment