Permalink
Browse files

Add timer support and setup cleanup to tag a real release.

  • Loading branch information...
1 parent e6e8f81 commit 0bbb11409828031880cc8c5659e4ea542006ed25 @pandemicsyn committed Dec 29, 2011
View
@@ -0,0 +1,5 @@
+*.pyc
+build
+dist
+*.egg-info
+*.log
View
@@ -0,0 +1,5 @@
+#!/usr/bin/env python
+from statsdpy.statsd import run_server
+
+if __name__ == '__main__':
+ run_server()
View
@@ -1,180 +0,0 @@
-#!/usr/bin/env python
-
-import sys, os, time, atexit
-from signal import SIGTERM
-from ConfigParser import ConfigParser, NoSectionError, NoOptionError, \
- RawConfigParser
-
-#stolen from swfit.common.utils
-def readconf(conffile, section_name=None, log_name=None, defaults=None,
- raw=False):
- """
- Read config file and return config items as a dict
-
- :param conffile: path to config file, or a file-like object (hasattr
- readline)
- :param section_name: config section to read (will return all sections if
- not defined)
- :param log_name: name to be used with logging (will use section_name if
- not defined)
- :param defaults: dict of default values to pre-populate the config with
- :returns: dict of config items
- """
- if defaults is None:
- defaults = {}
- if raw:
- c = RawConfigParser(defaults)
- else:
- c = ConfigParser(defaults)
- if hasattr(conffile, 'readline'):
- c.readfp(conffile)
- else:
- if not c.read(conffile):
- print ("Unable to read config file %s") % conffile
- sys.exit(1)
- if section_name:
- if c.has_section(section_name):
- conf = dict(c.items(section_name))
- else:
- print ("Unable to find %s config section in %s") % \
- (section_name, conffile)
- sys.exit(1)
- if "log_name" not in conf:
- if log_name is not None:
- conf['log_name'] = log_name
- else:
- conf['log_name'] = section_name
- else:
- conf = {}
- for s in c.sections():
- conf.update({s: dict(c.items(s))})
- if 'log_name' not in conf:
- conf['log_name'] = log_name
- conf['__file__'] = conffile
- return conf
-
-#from http://www.jejik.com/articles/2007/02/a_simple_unix_linux_daemon_in_python/
-class Daemon:
- """
- A generic daemon class.
-
- Usage: subclass the Daemon class and override the run() method
- """
- def __init__(self, pidfile, stdin='/dev/null', stdout='/dev/null', stderr='/dev/null'):
- self.stdin = stdin
- self.stdout = stdout
- self.stderr = stderr
- self.pidfile = pidfile
-
- def daemonize(self):
- """
- do the UNIX double-fork magic, see Stevens' "Advanced
- Programming in the UNIX Environment" for details (ISBN 0201563177)
- http://www.erlenstar.demon.co.uk/unix/faq_2.html#SEC16
- """
- try:
- pid = os.fork()
- if pid > 0:
- # exit first parent
- sys.exit(0)
- except OSError, e:
- sys.stderr.write("fork #1 failed: %d (%s)\n" % (e.errno, e.strerror))
- sys.exit(1)
-
- # decouple from parent environment
- os.chdir("/")
- os.setsid()
- os.umask(0)
-
- # do second fork
- try:
- pid = os.fork()
- if pid > 0:
- # exit from second parent
- sys.exit(0)
- except OSError, e:
- sys.stderr.write("fork #2 failed: %d (%s)\n" % (e.errno, e.strerror))
- sys.exit(1)
-
- # redirect standard file descriptors
- sys.stdout.flush()
- sys.stderr.flush()
- si = file(self.stdin, 'r')
- so = file(self.stdout, 'a+')
- se = file(self.stderr, 'a+', 0)
- os.dup2(si.fileno(), sys.stdin.fileno())
- os.dup2(so.fileno(), sys.stdout.fileno())
- os.dup2(se.fileno(), sys.stderr.fileno())
-
- # write pidfile
- atexit.register(self.delpid)
- pid = str(os.getpid())
- file(self.pidfile,'w+').write("%s\n" % pid)
-
- def delpid(self):
- os.remove(self.pidfile)
-
- def start(self, *args, **kw):
- """
- Start the daemon
- """
- # Check for a pidfile to see if the daemon already runs
- try:
- pf = file(self.pidfile,'r')
- pid = int(pf.read().strip())
- pf.close()
- except IOError:
- pid = None
-
- if pid:
- message = "pidfile %s already exist. Daemon already running?\n"
- sys.stderr.write(message % self.pidfile)
- sys.exit(1)
-
- # Start the daemon
- self.daemonize()
- self.run(*args, **kw)
-
- def stop(self):
- """
- Stop the daemon
- """
- # Get the pid from the pidfile
- try:
- pf = file(self.pidfile,'r')
- pid = int(pf.read().strip())
- pf.close()
- except IOError:
- pid = None
-
- if not pid:
- message = "pidfile %s does not exist. Daemon not running?\n"
- sys.stderr.write(message % self.pidfile)
- return # not an error in a restart
-
- # Try killing the daemon process
- try:
- while 1:
- os.kill(pid, SIGTERM)
- time.sleep(0.1)
- except OSError, err:
- err = str(err)
- if err.find("No such process") > 0:
- if os.path.exists(self.pidfile):
- os.remove(self.pidfile)
- else:
- print str(err)
- sys.exit(1)
-
- def restart(self):
- """
- Restart the daemon
- """
- self.stop()
- self.start()
-
- def run(self):
- """
- You should override this method when you subclass Daemon. It will be called after the process has been
- daemonized by start() or restart().
- """
File renamed without changes.
View
@@ -0,0 +1,35 @@
+from setuptools import setup, find_packages
+from statsdpy import __version__ as version
+
+install_requires = []
+try:
+ import eventlet
+except ImportError:
+ install_requires.append("eventlet")
+try:
+ import json
+except ImportError:
+ install_requires.append("simplejson")
+
+name = "statsdlog"
+
+setup(
+ name = name,
+ version = version,
+ author = "Florian Hines",
+ author_email = "syn@ronin.io",
+ description = "statsdpy",
+ license = "Apache License, (2.0)",
+ keywords = "statsd",
+ url = "http://github.com/pandemicsyn/statsdpy",
+ packages=find_packages(),
+ classifiers=[
+ 'Development Status :: 4 - Beta',
+ 'License :: OSI Approved :: Apache Software License',
+ 'Operating System :: POSIX :: Linux',
+ 'Programming Language :: Python :: 2.6',
+ 'Environment :: No Input/Output (Daemon)',
+ ],
+ install_requires=install_requires,
+ scripts=['bin/statsdpy-server']
+ )
View
@@ -0,0 +1,8 @@
+import gettext
+
+
+#: Version information (major, minor, revision[, 'dev']).
+version_info = (0, 0, 1)
+#: Version string 'major.minor.revision'.
+version = __version__ = ".".join(map(str, version_info))
+gettext.install('statsdlog')
Oops, something went wrong.

0 comments on commit 0bbb114

Please sign in to comment.