GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.
Completed
Pull Request — master (#47)
by Matic
53s
created

Daemon.delpid()   A

Complexity

Conditions 1

Size

Total Lines 2

Duplication

Lines 0
Ratio 0 %
Metric Value
cc 1
dl 0
loc 2
rs 10
1
#!/usr/bin/env python
2
3
import sys
4
import os
5
import time
6
import atexit
7
from signal import SIGTERM
8
9
10
class Daemon(object):
11
12
    """
13
    Generic daemon class.
14
15
    Usage: subclass the Daemon class and override the run() method
16
    """
17
18
    def __init__(self, pidfile, stdin='/dev/null', stdout='/dev/null',
19
                 stderr='/dev/null'):
20
        self.stdin = stdin
21
        self.stdout = stdout
22
        self.stderr = stderr
23
        self.pidfile = pidfile
24
25
    def daemonize(self):
26
        """
27
        Do the UNIX double-fork magic.
28
29
        See Stevens' "Advanced Programming in the UNIX Environment"
30
        http://www.erlenstar.demon.co.uk/unix/faq_2.html#SEC16
31
        (ISBN 0201563177)
32
        """
33
        try:
34
            pid = os.fork()
35
            if pid > 0:
36
                # exit first parent
37
                sys.exit(0)
38
        except OSError, e:
39
            sys.stderr.write("fork #1 failed: {0:d} ({1!s})\n".format(e.errno, e.strerror))
40
            sys.exit(1)
41
42
        # decouple from parent environment
43
        os.chdir("/")
44
        os.setsid()
45
        os.umask(0)
46
47
        # do second fork
48
        try:
49
            pid = os.fork()
50
            if pid > 0:
51
                # exit from second parent
52
                sys.exit(0)
53
        except OSError, e:
54
            sys.stderr.write("fork #2 failed: {0:d} ({1!s})\n".format(e.errno, e.strerror))
55
            sys.exit(1)
56
57
        # redirect standard file descriptors
58
        sys.stdout.flush()
59
        sys.stderr.flush()
60
        si = file(self.stdin, 'r')
61
        so = file(self.stdout, 'a+')
62
        se = file(self.stderr, 'a+', 0)
63
        os.dup2(si.fileno(), sys.stdin.fileno())
64
        os.dup2(so.fileno(), sys.stdout.fileno())
65
        os.dup2(se.fileno(), sys.stderr.fileno())
66
67
        # write pidfile
68
        atexit.register(self.delpid)
69
        pid = str(os.getpid())
70
        file(self.pidfile, 'w+').write("{0!s}\n".format(pid))
71
72
    def delpid(self):
73
        os.remove(self.pidfile)
74
75
    def start(self):
76
        """Start the daemon."""
77
        # Check for a pidfile to see if the daemon already runs
78
        try:
79
            pf = file(self.pidfile, 'r')
80
            pid = int(pf.read().strip())
81
            pf.close()
82
        except IOError:
83
            pid = None
84
85
        if pid:
86
            message = "pidfile %s already exists. Daemon already running?\n"
87
            sys.stderr.write(message % self.pidfile)
88
            sys.exit(1)
89
90
        # Start the daemon
91
        self.daemonize()
92
        self.run()
93
94
    def stop(self):
95
        """Stop the daemon."""
96
        # Get the pid from the pidfile
97
        try:
98
            pf = file(self.pidfile, 'r')
99
            pid = int(pf.read().strip())
100
            pf.close()
101
        except IOError:
102
            pid = None
103
104
        if not pid:
105
            message = "pidfile %s does not exist. Daemon not running?\n"
106
            sys.stderr.write(message % self.pidfile)
107
            return  # not an error in a restart
108
109
        # Try killing the daemon process
110
        try:
111
            while 1:
112
                os.kill(pid, SIGTERM)
113
                time.sleep(0.1)
114
        except OSError, err:
115
            err = str(err)
116
            if err.find("No such process") > 0:
117
                if os.path.exists(self.pidfile):
118
                    os.remove(self.pidfile)
119
            else:
120
                print str(err)
121
                sys.exit(1)
122
123
    def restart(self):
124
        """Restart the daemon."""
125
        self.stop()
126
        self.start()
127
128
    def run(self):
129
        """
130
        You should override this method when you subclass Daemon.
131
        It will be called after the process has been
132
        daemonized by start() or restart().
133
        """
134