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
f81bc9a6
Commit
f81bc9a6
authored
5 years ago
by
Никита Ефремов
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Updated RedirectorWatch class and watch() function
parent
81010cad
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
48 additions
and
33 deletions
+48
-33
generators.py
core/generators.py
+1
-1
parser.py
core/parser.py
+8
-3
redirector.py
core/redirector.py
+39
-29
No files found.
core/generators.py
View file @
f81bc9a6
...
...
@@ -21,7 +21,7 @@ class MapGenerator:
class
ConfigGenerator
:
def
__init__
(
self
):
self
.
start_line
=
"if($
%
s-redirect) {
\n
"
self
.
rewrite_line
=
"
\t
rewrite ^/
%
s/(.^)$ $
%
s redirect;
\n
}"
self
.
rewrite_line
=
"
\t
rewrite ^/
%
s/(.^)$ $
%
s
-redirect
redirect;
\n
}"
def
generate_conf
(
self
,
project_name
):
return
(
self
.
start_line
%
project_name
)
+
(
self
.
rewrite_line
%
(
project_name
,
project_name
))
...
...
This diff is collapsed.
Click to expand it.
core/parser.py
View file @
f81bc9a6
import
yaml
import
os
from
yaml
import
load
,
dump
try
:
from
yaml
import
CLoader
as
Loader
,
CDumper
as
Dumper
except
ImportError
:
from
yaml
import
Loader
,
Dumper
import
re
...
...
@@ -83,7 +88,7 @@ class ConfigReader:
def
parse_yaml
(
file_dir
):
return_list
=
[]
with
open
(
file_dir
,
'r'
)
as
stream
:
data
=
yaml
.
safe_load
(
stream
)
data
=
load
(
stream
,
Loader
=
Loader
)
for
project
in
data
.
get
(
"projects"
):
map_path
=
project
.
get
(
"map"
)
project_prefix
=
project
.
get
(
"prefix"
)
...
...
@@ -95,7 +100,7 @@ class ConfigReader:
res_prefix
=
None
try
:
for
map_path
,
prefix
in
self
.
parse_yaml
(
yaml_file
):
if
map_path
==
map_file
:
if
map_path
==
map_file
or
os
.
getcwd
()
+
"/"
+
map_path
==
map_file
:
res_prefix
=
prefix
.
split
(
"/"
)[
-
1
]
# ???: Is the last directory of map_path a project's name?
with
open
(
map_file
,
"r"
)
as
file
:
for
i
,
line
in
enumerate
(
file
):
...
...
This diff is collapsed.
Click to expand it.
core/redirector.py
View file @
f81bc9a6
import
sys
import
os
import
pyinotify
import
asyncore
from
core
import
generators
from
core
import
logger
...
...
@@ -17,7 +19,8 @@ class Redirector:
project_name
=
"Error"
try
:
data
,
project_name
=
self
.
parser
.
parse_map
(
map_file
,
yaml_file
)
if
not
project_name
:
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
)
...
...
@@ -40,40 +43,44 @@ class Redirector:
def
__init__
(
self
,
message
,
errors
):
super
()
.
__init__
(
message
)
self
.
errors
=
errors
class
RedirectorInputDataError
(
Exception
):
def
__init__
(
self
,
message
,
errors
):
super
()
.
__init__
(
message
)
self
.
errors
=
errors
"""
Need to take care of unusual behavior of server - lagging, reboots, etc -> need some kind of saving file
"""
class
RedirectorWatch
:
def
__init__
(
self
):
self
.
logger
=
logger
.
Logger
(
"redirector-watch.log"
)
self
.
redirector
=
Redirector
()
self
.
mask
=
pyinotify
.
IN_MODIFY
watch_manager
=
pyinotify
.
WatchManager
()
self
.
notifier
=
pyinotify
.
AsyncioNotifier
(
watch_manager
,
self
.
Watch
())
@staticmethod
def
parse_many
(
filename
):
with
open
(
filename
,
"r"
)
as
file_
:
return
(
line
.
split
()
for
line
in
file_
.
readlines
())
def
add_files
(
self
,
map_file
,
yaml_file
,
filename
=
None
):
if
filename
:
for
map_
,
yaml_
in
filename
:
pass
else
:
pass
try
:
self
.
notifier
.
loop
(
daemonize
=
True
,
callback
=
self
.
watch
,
pid_file
=
'/tmp/pyinotify.pid'
,
stdout
=
'/tmp/pyinotify.log'
)
except
pyinotify
.
NotifierError
as
err
:
self
.
logger
.
log
(
err
)
@staticmethod
def
watch
(
*
args
):
print
(
args
)
self
.
parser
=
parser
.
ConfigReader
(
logger
=
self
.
logger
)
self
.
mask
=
pyinotify
.
IN_MODIFY
|
pyinotify
.
IN_DELETE
self
.
watch_manager
=
pyinotify
.
WatchManager
()
def
watch
(
self
,
yaml_file
):
class
Watch
(
pyinotify
.
ProcessEvent
):
def
__init__
(
self
,
*
args
,
**
kwargs
):
self
.
redirector
=
kwargs
.
pop
(
"redirector"
)
super
()
.
__init__
(
*
args
,
**
kwargs
)
def
process_IN_MODIFY
(
self
,
event
):
self
.
redirector
.
generate
(
yaml_file
,
event
.
pathname
)
self
.
notifier
=
pyinotify
.
AsyncNotifier
(
self
.
watch_manager
,
Watch
(
redirector
=
self
.
redirector
))
file_list
=
self
.
parser
.
parse_yaml
(
yaml_file
)
for
map_path
,
_
in
file_list
:
cwd
=
os
.
getcwd
()
wdd
=
self
.
watch_manager
.
add_watch
(
cwd
+
"/"
+
map_path
,
self
.
mask
,
rec
=
True
)
asyncore
.
loop
()
def
main
(
args
=
None
):
if
not
args
:
if
not
args
:
args
=
sys
.
argv
[
1
:]
redirector
=
Redirector
()
try
:
...
...
@@ -86,4 +93,7 @@ def watch(args=None):
if
not
args
:
args
=
sys
.
argv
[
1
:]
print
(
args
)
watch_
=
RedirectorWatch
()
watch_
.
watch
(
args
)
This diff is collapsed.
Click to expand it.
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