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
You need to sign in or sign up before continuing.
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
Show whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
85 additions
and
75 deletions
+85
-75
redirector.py
redirector/redirector.py
+3
-3
generators.py
redirector/utils/generators.py
+47
-48
parser.py
redirector/utils/parser.py
+35
-24
No files found.
redirector/redirector.py
View file @
36a4d1b2
...
@@ -13,14 +13,14 @@ class Redirector:
...
@@ -13,14 +13,14 @@ class Redirector:
def
generate
(
self
,
yaml_file
,
map_file
):
def
generate
(
self
,
yaml_file
,
map_file
):
project_name
=
"Error"
project_name
=
"Error"
try
:
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
:
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
,
prefix
)
self
.
generator
.
generate
(
data
,
project_name
,
map_file
)
class
RedirectorParserError
(
Exception
):
class
RedirectorParserError
(
Exception
):
def
__init__
(
self
,
message
,
errors
):
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.const
import
MAPS_DIR
,
CONFIG_DIR
from
redirector.utils.parser
import
ConfigReader
class
Generator
:
class
Generator
:
def
__init__
(
self
):
def
__init__
(
self
):
...
@@ -9,45 +8,41 @@ class Generator:
...
@@ -9,45 +8,41 @@ class Generator:
def
get_conf_dirs
(
self
):
def
get_conf_dirs
(
self
):
return
MAPS_DIR
,
CONFIG_DIR
return
MAPS_DIR
,
CONFIG_DIR
def
get_essential
(
self
,
arg_map
=
None
):
def
write_map
(
self
,
maps_dir
,
project_name
,
redirects_map
,
redirects_with_options
):
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
()
try
:
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
)
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"
:
code
=
"permanent"
code
=
"permanent"
elif
code
==
"302"
:
elif
code
==
"302"
:
code
=
"redirect"
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
)
map_file
.
write
(
data
)
with
open
(
config_dir
+
"/
%
s.conf"
%
arg
[
"project_name"
],
"w"
)
as
conf_file
:
conf_file
.
write
(
conf_data
)
except
Exception
as
e
:
except
Exception
as
e
:
print
(
e
)
print
(
e
)
raise
self
.
GenerationError
(
"Can
\'
t generate new .
conf or new .
map file
\
raise
self
.
GenerationError
(
"Can
\'
t generate new .map file
\
from
%
s .map file for
%
s project"
%
(
map_file
,
project_name
),
e
)
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
\
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
):
class
GenerationError
(
Exception
):
def
__init__
(
self
,
message
,
errors
):
def
__init__
(
self
,
message
,
errors
):
super
()
.
__init__
(
message
)
super
()
.
__init__
(
message
)
...
@@ -62,38 +57,40 @@ class MapGenerator:
...
@@ -62,38 +57,40 @@ class MapGenerator:
self
.
endline
=
"
\n
}"
self
.
endline
=
"
\n
}"
def
generate_map
(
self
,
redirects
,
arg
):
def
generate_map
(
self
,
redirects
,
project_name
,
prefix
):
if
arg
[
"prefix"
]
:
if
prefix
:
data
=
self
.
start_line
%
arg
[
"prefix"
]
data
=
self
.
start_line
%
prefix
else
:
else
:
data
=
self
.
start_line
%
arg
[
"project_name"
]
data
=
self
.
start_line
%
project_name
if
arg
[
"project_name"
]
==
arg
[
"prefix"
]
:
if
project_name
==
prefix
:
arg
[
"prefix"
]
=
"/{}"
.
format
(
arg
[
"prefix"
]
)
prefix
=
"/{}"
.
format
(
prefix
)
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;"
%
(
arg
[
"prefix"
]
+
arg1
,
arg
[
"prefix"
]
+
arg2
)
data
+=
"
\n\t
%
s
\t
%
s;"
%
(
prefix
+
arg1
,
prefix
+
arg2
)
data
+=
self
.
endline
data
+=
self
.
endline
return
data
return
data
def
generate_opt_map
(
self
,
redirect_dict
,
arg
):
def
generate_opt_map
(
self
,
redirect_dict
,
project_name
,
prefix
):
result
=
[]
result
=
[]
for
code
,
redirects
in
redirect_dict
.
items
():
for
code
,
redirects
in
redirect_dict
.
items
():
if
project_name
==
prefix
:
prefix
=
"/{}"
.
format
(
prefix
)
if
code
==
"301"
:
if
code
==
"301"
:
code
=
"permanent"
code
=
"permanent"
data
=
self
.
opt_start_line
%
(
arg
[
"project_name"
]
,
code
)
data
=
self
.
opt_start_line
%
(
project_name
,
code
)
for
arg1
,
arg2
in
redirects
:
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
data
+=
self
.
endline
elif
code
==
"302"
:
elif
code
==
"302"
:
code
=
"redirect"
code
=
"redirect"
data
=
self
.
opt_start_line
%
(
arg
[
"project_name"
]
,
code
)
data
=
self
.
opt_start_line
%
(
project_name
,
code
)
for
arg1
,
arg2
in
redirects
:
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
data
+=
self
.
endline
elif
code
==
"status=301"
:
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
:
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
data
+=
self
.
endline
result
.
append
((
code
,
data
))
result
.
append
((
code
,
data
))
return
result
return
result
...
@@ -105,17 +102,19 @@ class ConfigGenerator:
...
@@ -105,17 +102,19 @@ class ConfigGenerator:
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
=
"
\t
rewrite ^
%
s/(.*)$ $
%
s_redirect
%
s;
\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
"
self
.
option_line
=
"return
%
s;
\n
}
\n
"
def
generate_config
(
self
,
project_name
,
options
,
prefix
):
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
:
for
code
in
options
:
if
code
==
"permanent"
:
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"
:
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
:
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
return
data
redirector/utils/parser.py
View file @
36a4d1b2
...
@@ -15,7 +15,7 @@ class MapLineParser:
...
@@ -15,7 +15,7 @@ class MapLineParser:
r"^(?:http(s)?:\/\/)?[\w.-]+(?:\.[\w\.-]+)+[\w\-\._~:/?#[\]@!\$&'\(\)\*\+,;=.]+$"
)
r"^(?:http(s)?:\/\/)?[\w.-]+(?:\.[\w\.-]+)+[\w\-\._~:/?#[\]@!\$&'\(\)\*\+,;=.]+$"
)
self
.
logger
=
logger
self
.
logger
=
logger
def
parse_line
(
self
,
line
,
prefix
,
i
):
def
parse_line
(
self
,
line
,
i
):
line
=
line
.
strip
()
line
=
line
.
strip
()
return_code
,
options
=
0
,
[]
return_code
,
options
=
0
,
[]
"""
"""
...
@@ -90,6 +90,20 @@ class ConfigReader:
...
@@ -90,6 +90,20 @@ class ConfigReader:
self
.
logger
=
logger
self
.
logger
=
logger
@staticmethod
@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
):
def
parse_yaml
(
file_dir
):
return_list
=
[]
return_list
=
[]
yaml_dir
=
os
.
path
.
dirname
(
os
.
path
.
abspath
(
file_dir
))
yaml_dir
=
os
.
path
.
dirname
(
os
.
path
.
abspath
(
file_dir
))
...
@@ -111,36 +125,18 @@ class ConfigReader:
...
@@ -111,36 +125,18 @@ class ConfigReader:
return
return_list
return
return_list
def
parse_map
(
self
,
map_file
,
yaml_file
):
def
read_map
(
self
,
map_path
):
res
=
[[],
{}]
res
=
[[],
{}]
res_prefix
=
None
# normalize path
yaml_dir
=
os
.
path
.
dirname
(
os
.
path
.
abspath
(
yaml_file
))
# get yaml file directory for restore rel path to map file
map_dir
=
os
.
path
.
dirname
(
os
.
path
.
abspath
(
map_file
))
map_file_name
=
os
.
path
.
basename
(
map_file
)
# get filename from arg
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
res_prefix
=
prefix
.
split
(
"/"
)[
-
1
]
# Last directory of map_path a project's name
f
=
open
(
map_path
,
"r"
)
f
=
open
(
map_path
,
"r"
)
for
i
,
line
in
enumerate
(
f
):
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
=
\
self
.
line_parser
.
parse_line
(
line
,
prefix
,
i
)
self
.
line_parser
.
parse_line
(
line
,
i
)
except
MapLineParser
.
RegexpTestError
as
e
:
except
MapLineParser
.
RegexpTestError
as
e
:
self
.
logger
.
log
(
e
.
message
%
map_file
)
self
.
logger
.
log
(
e
.
message
%
map_path
)
except
MapLineParser
.
ParseLineError
as
e
:
except
MapLineParser
.
ParseLineError
as
e
:
self
.
logger
.
log
(
e
.
message
%
map_file
)
self
.
logger
.
log
(
e
.
message
%
map_path
)
if
request_url
and
redirect_url
and
return_code
in
[
0
,
1
]:
if
request_url
and
redirect_url
and
return_code
in
[
0
,
1
]:
if
return_code
==
0
:
if
return_code
==
0
:
res
[
0
]
.
append
((
request_url
,
redirect_url
))
res
[
0
]
.
append
((
request_url
,
redirect_url
))
...
@@ -153,9 +149,24 @@ class ConfigReader:
...
@@ -153,9 +149,24 @@ class ConfigReader:
res
[
1
][
opt_
]
=
[(
request_url
,
redirect_url
)]
res
[
1
][
opt_
]
=
[(
request_url
,
redirect_url
)]
f
.
close
()
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
map_dir
=
os
.
path
.
dirname
(
os
.
path
.
abspath
(
map_file
))
map_file_name
=
os
.
path
.
basename
(
map_file
)
# get filename from arg
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
return
res
,
res_prefix
project_name
,
prefix
=
self
.
get_projectname_prefix
(
map_path
,
prefix
,
name
)
return
project_name
,
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
))
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