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
179ade80
Commit
179ade80
authored
Jun 25, 2020
by
Давид Добряков
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Separate files for watcher and main script
parent
acd0d7e1
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
87 additions
and
73 deletions
+87
-73
redirector.py
redirector/redirector.py
+0
-73
watcher.py
redirector/watcher.py
+87
-0
No files found.
redirector/redirector.py
View file @
179ade80
...
...
@@ -9,8 +9,6 @@ import logger
import
parser
import
const
redirector_watch
=
None
class
Redirector
:
def
__init__
(
self
,
logger
=
None
):
self
.
parser
=
parser
.
ConfigReader
(
logger
=
logger
)
...
...
@@ -38,65 +36,6 @@ class Redirector:
self
.
errors
=
errors
class
RedirectorWatch
:
def
__init__
(
self
):
self
.
logger
=
logger
.
Logger
(
"redirector-watch.log"
)
self
.
redirector
=
Redirector
()
self
.
parser
=
parser
.
ConfigReader
(
logger
=
self
.
logger
)
self
.
mask
=
pyinotify
.
IN_MODIFY
self
.
pool
=
{}
def
watch
(
self
,
yaml_file
):
if
not
yaml_file
:
raise
self
.
RedirectorWatchException
(
"yaml_file list is empty"
,
Exception
)
Watch
=
self
.
create_watch_class
(
yaml_file
)
for
map_path
,
_
in
self
.
parser
.
parse_yaml
(
yaml_file
):
self
.
redirector
.
generate
(
yaml_file
,
map_path
)
try
:
self
.
set_inotify
(
yaml_file
,
Watch
)
except
pyinotify
.
PyinotifyError
as
e
:
raise
self
.
RedirectorWatchException
(
"Can
\'
t set inotify for yamll file
%
s"
%
yaml_file
,
e
)
# creating class Watch with custom static 'yaml_file' argument
def
create_watch_class
(
self
,
yaml_file
):
global
redirector_watch
class
Watch
(
pyinotify
.
ProcessEvent
):
def
__init__
(
self
,
*
args
,
**
kwargs
):
self
.
redirector
=
kwargs
.
pop
(
"redirector"
)
super
()
.
__init__
(
*
args
,
**
kwargs
)
def
process_IN_MODIFY
(
self
,
event
):
try
:
redirector_watch
.
pool
[
yaml_file
][
1
]
.
stop
()
except
RuntimeError
as
e
:
print
(
"hello there"
)
redirector_watch
.
pool
[
yaml_file
]
=
None
print
(
"Proccessing..."
)
self
.
redirector
.
generate
(
yaml_file
,
event
.
pathname
)
redirector_watch
.
watch
(
yaml_file
)
return
Watch
# we suggest that self.watch_manager already exists
def
set_inotify
(
self
,
yaml_file
,
Watch_class
):
wm
=
pyinotify
.
WatchManager
()
self
.
add_files
(
yaml_file
,
wm
)
notifier
=
pyinotify
.
ThreadedNotifier
(
wm
,
Watch_class
(
redirector
=
self
.
redirector
))
notifier
.
start
()
self
.
pool
[
yaml_file
]
=
[
wm
,
notifier
]
def
add_files
(
self
,
file_
,
wm
):
file_list
=
self
.
parser
.
parse_yaml
(
file_
)
for
map_path
,
_
in
file_list
:
cwd
=
os
.
getcwd
()
wdd
=
wm
.
add_watch
(
cwd
+
"/"
+
map_path
,
self
.
mask
,
rec
=
True
)
class
RedirectorWatchException
(
Exception
):
def
__init__
(
self
,
message
,
errors
):
super
()
.
__init__
(
message
)
self
.
errors
=
errors
def
main
(
args
=
None
):
parser
=
ap
.
ArgumentParser
(
description
=
'Redirector: nginx config generator'
)
parser
.
add_argument
(
'yaml_file'
,
metavar
=
'Y'
,
type
=
str
,
nargs
=
1
,
help
=
'.yaml file that defines project and it
\'
s content'
)
...
...
@@ -111,18 +50,6 @@ def main(args=None):
print
(
"CRITICAL:
\n
"
+
str
(
e
))
def
watch
(
args
=
None
):
parser
=
ap
.
ArgumentParser
(
description
=
'Redirector watchdog utility'
)
parser
.
add_argument
(
'filename'
,
metavar
=
'Y'
,
type
=
str
,
nargs
=
1
,
help
=
'Sets watch on the content of .yaml file'
)
if
not
args
:
args
=
parser
.
parse_args
()
global
redirector_watch
redirector_watch
=
RedirectorWatch
()
redirector_watch
.
watch
(
args
.
filename
[
0
])
if
__name__
==
"__main__"
:
main
()
redirector/watcher.py
0 → 100644
View file @
179ade80
import
sys
import
os
import
pyinotify
import
asyncore
import
argparse
as
ap
import
generators
import
logger
import
parser
import
const
redirector_watch
=
None
class
RedirectorWatch
:
def
__init__
(
self
):
self
.
logger
=
logger
.
Logger
(
"redirector-watch.log"
)
self
.
redirector
=
Redirector
()
self
.
parser
=
parser
.
ConfigReader
(
logger
=
self
.
logger
)
self
.
mask
=
pyinotify
.
IN_MODIFY
self
.
pool
=
{}
def
watch
(
self
,
yaml_file
):
if
not
yaml_file
:
raise
self
.
RedirectorWatchException
(
"yaml_file list is empty"
,
Exception
)
Watch
=
self
.
create_watch_class
(
yaml_file
)
for
map_path
,
_
in
self
.
parser
.
parse_yaml
(
yaml_file
):
self
.
redirector
.
generate
(
yaml_file
,
map_path
)
try
:
self
.
set_inotify
(
yaml_file
,
Watch
)
except
pyinotify
.
PyinotifyError
as
e
:
raise
self
.
RedirectorWatchException
(
"Can
\'
t set inotify for yamll file
%
s"
%
yaml_file
,
e
)
# creating class Watch with custom static 'yaml_file' argument
def
create_watch_class
(
self
,
yaml_file
):
global
redirector_watch
class
Watch
(
pyinotify
.
ProcessEvent
):
def
__init__
(
self
,
*
args
,
**
kwargs
):
self
.
redirector
=
kwargs
.
pop
(
"redirector"
)
super
()
.
__init__
(
*
args
,
**
kwargs
)
def
process_IN_MODIFY
(
self
,
event
):
try
:
redirector_watch
.
pool
[
yaml_file
][
1
]
.
stop
()
except
RuntimeError
as
e
:
print
(
"hello there"
)
redirector_watch
.
pool
[
yaml_file
]
=
None
print
(
"Proccessing..."
)
self
.
redirector
.
generate
(
yaml_file
,
event
.
pathname
)
redirector_watch
.
watch
(
yaml_file
)
return
Watch
# we suggest that self.watch_manager already exists
def
set_inotify
(
self
,
yaml_file
,
Watch_class
):
wm
=
pyinotify
.
WatchManager
()
self
.
add_files
(
yaml_file
,
wm
)
notifier
=
pyinotify
.
ThreadedNotifier
(
wm
,
Watch_class
(
redirector
=
self
.
redirector
))
notifier
.
start
()
self
.
pool
[
yaml_file
]
=
[
wm
,
notifier
]
def
add_files
(
self
,
file_
,
wm
):
file_list
=
self
.
parser
.
parse_yaml
(
file_
)
for
map_path
,
_
in
file_list
:
cwd
=
os
.
getcwd
()
wdd
=
wm
.
add_watch
(
cwd
+
"/"
+
map_path
,
self
.
mask
,
rec
=
True
)
class
RedirectorWatchException
(
Exception
):
def
__init__
(
self
,
message
,
errors
):
super
()
.
__init__
(
message
)
self
.
errors
=
errors
def
watch
(
args
=
None
):
parser
=
ap
.
ArgumentParser
(
description
=
'Redirector watchdog utility'
)
parser
.
add_argument
(
'filename'
,
metavar
=
'Y'
,
type
=
str
,
nargs
=
1
,
help
=
'Sets watch on the content of .yaml file'
)
if
not
args
:
args
=
parser
.
parse_args
()
global
redirector_watch
redirector_watch
=
RedirectorWatch
()
redirector_watch
.
watch
(
args
.
filename
[
0
])
if
__name__
==
"__main__"
:
watch
()
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