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
c2bffb25
Commit
c2bffb25
authored
Jul 16, 2020
by
Давид Добряков
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Refactoring, add test_suite to setup.py
parent
ffa0e145
Hide whitespace changes
Inline
Side-by-side
Showing
9 changed files
with
70 additions
and
56 deletions
+70
-56
MANIFEST.in
MANIFEST.in
+5
-0
redirector.py
redirector/redirector.py
+0
-4
const.py
redirector/utils/const.py
+4
-4
generators.py
redirector/utils/generators.py
+0
-2
parser.py
redirector/utils/parser.py
+45
-36
utils.py
redirector/utils/utils.py
+9
-0
watcher.py
redirector/watcher.py
+2
-4
setup.py
setup.py
+3
-2
test.yaml
tests/test.yaml
+2
-4
No files found.
MANIFEST.in
View file @
c2bffb25
include redirector/conf/config.ini
include redirector/tests/*.ini
include redirector/tests/*.map
include redirector/tests/*.yaml
include LICENSE
include README.md
redirector/redirector.py
View file @
c2bffb25
import
sys
import
os
import
pyinotify
import
asyncore
import
argparse
as
ap
from
redirector.utils
import
generators
...
...
redirector/utils/const.py
View file @
c2bffb25
NAME
=
"nginx-redirector"
VERSION
=
"1.0"
AUTHOR
=
"Nikita Yefremov"
EMAIL
=
"
enk
@etersoft.ru"
PYTHON_VERSION
=
"3.
6
"
VERSION
=
"1.0
.0
"
AUTHOR
=
"Nikita Yefremov
, David Dobryakov
"
EMAIL
=
"
kantegory
@etersoft.ru"
PYTHON_VERSION
=
"3.
8.1
"
VERSION_STATUS
=
"alpha"
redirector/utils/generators.py
View file @
c2bffb25
...
...
@@ -19,8 +19,6 @@ class Generator:
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
)
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
()
...
...
redirector/utils/parser.py
View file @
c2bffb25
...
...
@@ -5,6 +5,7 @@ try:
except
ImportError
:
from
yaml
import
Loader
,
Dumper
import
re
from
redirector.utils.utils
import
get_map_path
class
MapLineParser
:
...
...
@@ -96,16 +97,20 @@ class ConfigReader:
return_list
=
[]
yaml_dir
=
os
.
path
.
dirname
(
os
.
path
.
abspath
(
file_dir
))
with
open
(
file_dir
,
'r'
)
as
stream
:
data
=
load
(
stream
,
Loader
=
Loader
)
for
project
in
data
.
get
(
"projects"
):
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
=
os
.
path
.
normpath
(
abs_map_path
)
stream
=
open
(
file_dir
,
'r'
)
data
=
load
(
stream
,
Loader
=
Loader
)
for
project
in
data
.
get
(
"projects"
):
map_path
=
project
.
get
(
"map"
)
abs_map_path
=
get_map_path
(
map_path
,
yaml_dir
)
project_prefix
=
project
.
get
(
"prefix"
)
return_list
.
append
((
abs_map_path
,
project_prefix
))
stream
.
close
()
project_prefix
=
project
.
get
(
"prefix"
)
return_list
.
append
((
abs_map_path
,
project_prefix
))
return
return_list
def
parse_map
(
self
,
map_file
,
yaml_file
):
...
...
@@ -118,36 +123,40 @@ class ConfigReader:
try
:
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
=
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
in
abs_map_path
:
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
):
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
not
request_url
or
not
redirect_url
or
return_code
==
-
1
:
continue
abs_map_path
=
get_map_path
(
map_path
,
yaml_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
(
abs_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
:
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
)]
res
[
1
][
opt_
]
=
[(
request_url
,
redirect_url
)]
f
.
close
()
return
res
,
res_prefix
except
YAMLError
as
e
:
self
.
logger
.
log
(
"Error occurred while reading
%
s"
%
yaml_file
+
str
(
e
))
redirector/utils/utils.py
0 → 100644
View file @
c2bffb25
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
redirector/watcher.py
View file @
c2bffb25
import
sys
import
os
import
pyinotify
import
asyncore
import
argparse
as
ap
from
redirector.utils
import
generators
...
...
@@ -104,6 +102,6 @@ def watch(args=None):
redirector_watch
.
watch
(
args
.
filename
[
0
])
#
if __name__ == "__main__":
#
watch()
if
__name__
==
"__main__"
:
watch
()
setup.py
View file @
c2bffb25
...
...
@@ -25,7 +25,7 @@ setup(
long_description
=
open
(
'README.md'
)
.
read
(),
author
=
"Nikita Efremov, Dobryakov David"
,
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"
,
packages
=
[
'redirector'
,
'redirector.utils'
,
'redirector.tests'
,
'redirector.conf'
],
entry_points
=
{
...
...
@@ -41,6 +41,7 @@ setup(
'pyinotify'
,
'pyyaml'
],
include_package_data
=
True
include_package_data
=
True
,
test_suite
=
"redirector.tests"
)
tests/test.yaml
View file @
c2bffb25
projects
:
-
map
:
./test2.map
prefix
:
/test3
-
map
:
./test.map
prefix
:
/test
-
map
:
/home/kantegory/pvt/nginx-redirector/tests/test2.map
prefix
:
/test2
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