aboutsummaryrefslogtreecommitdiff
path: root/source/pyAggr3g470r
blob: 3ea4893309015dcb3e89178918ab9275dac260dd (plain)
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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
#! /usr/bin/env python
#-*- coding: utf-8 -*-

# pyAggr3g470r - A Web based news aggregator.
# Copyright (C) 2010-2012  Cédric Bonhomme - http://cedricbonhomme.org/
#
# For more information : http://bitbucket.org/cedricbonhomme/pyaggr3g470r/
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>

__author__ = "Cedric Bonhomme"
__version__ = "$Revision: 0.1 $"
__date__ = "$Date: 2011/06/20 $"
__copyright__ = "Copyright (c) Cedric Bonhomme"
__license__ = "GPLv3"

# This control file is inspired from Forban: http://www.foo.be/forban.

import os
import sys
import time
import subprocess
import platform
import signal

PATH = os.path.abspath(".")
SERVICE = "pyAggr3g470r"

def service_start(python_command, servicename = None):
    """
    Starts a new service with Popen and returns the processus id.
    """
    if servicename is not None:
        service = servicename + ".py"
        proc =  subprocess.Popen([python_command, "-tt", service], stderr=subprocess.STDOUT, stdout=subprocess.PIPE)
        time.sleep(0.15)
        return proc.pid
    return False

def writepid(processname = None, pid = None):
    """
    Writes the pid of processname in a file.
    """
    pidpath = os.path.join(PATH, "var", processname + ".pid")
    if processname is not None and pid is not None:
        f = open (pidpath, "w")
        f.write(str(pid))
        f.close()
        return True
    else:
        return False

def checkpid (servicename = None):
    pidpath = os.path.join(PATH,"var", servicename + ".pid")
    if os.path.exists(pidpath):
        return True
    else:
        return False

def pidof(processname = None):
    pidpath = os.path.join(PATH, "var", processname + ".pid")
    if processname is not None and os.path.exists(pidpath):
        f = open (pidpath)
        pid = f.read()
        f.close()
        return pid
    else:
        return False

def rmpid (processname = None):
    pidpath = os.path.join(PATH, "var", processname + ".pid")
    if os.path.exists(pidpath):
        os.unlink(pidpath)
        return True
    else:
        return False

def start(python_command):
    if not checkpid(servicename = SERVICE):
        pid = service_start(python_command, servicename = SERVICE)
        writepid(processname = SERVICE, pid = pid)
        print(SERVICE + " is starting with pid: " + pidof(processname=SERVICE))
    else:
        print(SERVICE + " could not be started (pid exists)")
        retval=False 

def stop():
    print("Stopping pyAggr3g470r...")
    retval=True
    pid = pidof(processname=SERVICE)
    if pid:
        if platform.system() == "Windows":
            import win32api
            import win32con
            phandle = win32api.OpenProcess(win32con.PROCESS_TERMINATE, 0, int(pid))
            win32api.TerminateProcess(phandle, 0)
            win32api.CloseHandle(phandle)
            rmpid(processname=SERVICE)
        else:
            try:
                os.kill(int(pid), signal.SIGKILL)
            except OSError as e:
                print(SERVICE + " unsuccessfully stopped")
                retval = False
            print(SERVICE)
            rmpid(processname=SERVICE)
    return retval

def usage():
    print("pyAggr3g470r (start|stop|restart)")
    exit (1)

if __name__ == "__main__":
    # Point of entry in execution mode.
    python_command = "python"
    if sys.version_info.major == 2:
        # Ensures that code doesn't illegally mixed tabs and spaces
        python_command = "python3.2"
    if len(sys.argv) == 1:
        usage()
    elif sys.argv[1] == "start":
        start(python_command)
    elif sys.argv[1] == "stop":
        stop()
    elif sys.argv[1] == "restart":
        stop()
        start(python_command)
    else:
        usage()
bgstack15