-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path__main__.py
More file actions
executable file
·77 lines (65 loc) · 2.79 KB
/
__main__.py
File metadata and controls
executable file
·77 lines (65 loc) · 2.79 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
'''
# Project: PySync
# Developed By: Mahesh Patel
# Org: Emtyops Technologies
# Objective:
# - Backup folders and databases on servers and local systems
# autamation to save time for system admins.
# - Sync backup archive to online backup drives.
# - Easy configurable
'''
import os
# /////////////////////////////////////////////////////////////////////////////#
# Imports
# /////////////////////////////////////////////////////////////////////////////#
import platform
import subprocess
import sys
# ------------------------------------------------------------------------------#
# Operational Variables
# ------------------------------------------------------------------------------#
__PLATFORM__ = platform.system()
__BASE_DIR__ = os.path.dirname(__file__)
# ------------------------------------------------------------------------------#
# ------------------------------------------------------------------------------#
class PySync:
def __init__(self):
pass
def daemonize(self, daemon_file):
'''
@Doc
Create daemon process independent to this execution
which will keep running in background and backup
until interrupted implecitely/explecitely.
:__file__: File path which will be daemonized to independent process.
:return: PID - Process ID of independent process or False if not a file.
'''
if os.path.isfile(daemon_file):
# process creation flag status
_FLAG = 0
# pre-executioning function before creation
_PFN = None
if (__PLATFORM__ == 'Windows'):
'''Check if subprocess has FLAGS if system is WIN'''
if hasattr(subprocess, 'CREATE_NEW_PROCESS_GROUP'):
_FLAG = subprocess.CREATE_NEW_PROCESS_GROUP
elif hasattr(subprocess, 'DETACHED_PROCESS'):
_FLAG = subprocess.DETACHED_PROCESS
else:
'''SET Pre-Executional function to create daemon on UNIX'''
_PFN = os.setpgrp
independent_process = subprocess.Popen([sys.executable, daemon_file],
creationflags=_FLAG,
preexec_fn=_PFN)
_PID = independent_process.pid
if _PID:
return _PID
else:
return False
else:
return False
# ------------------------------------------------------------------------------ #
# ------------------------------------------------------------------------------ #
if __name__ == '__main__':
PySync = PySync()
PySync.daemonize('pysync_daemon.py')