Re-flow indentation of bugfix items.
[tor-bridgedb.git] / bridgedb / runner.py
blobb6117e1f8ae5e29b226eb37411b76a839072785c
1 # -*- coding: utf-8 ; test-case-name: bridgedb.test.test_runner -*-
3 # This file is part of BridgeDB, a Tor bridge distribution system.
5 # :authors: Isis Lovecruft 0xA3ADB67A2CDB8B35 <isis@torproject.org>
6 # please also see AUTHORS file
7 # :copyright: (c) 2007-2017, The Tor Project, Inc.
8 # (c) 2007-2017, all entities within the AUTHORS file
9 # (c) 2012-2017, Isis Lovecruft
10 # :license: 3-clause BSD, see included LICENSE for information
12 """Classes for running components and servers, as well as daemonisation.
14 ** Module Overview: **
16 """
18 from __future__ import print_function
20 import glob
21 import logging
22 import sys
23 import os
25 from twisted.python import procutils
27 from bridgedb import util
30 def cleanupUnparseableDescriptors(directory, seconds):
31 """Delete any ``*.unparseable`` descriptor files in ``directory`` with
32 mtimes more than ``seconds`` ago.
34 The :func:`bridgedb.parsers._copyUnparseableDescriptors` function
35 will make copies of any files we attempt to parse which contain
36 unparseable descriptors. This function should run on a timer to
37 clean them up.
39 :param str directory: The directory in which to search for unparseable
40 descriptors.
41 :param int olderThan: If a file's mtime is more than this number
42 (in seconds), it will be deleted.
43 """
44 files = []
46 for pattern in ["*.unparseable", "*.unparseable.xz"]:
47 files.extend(glob.glob(os.sep.join([directory, pattern])))
49 if files:
50 logging.info("Deleting old unparseable descriptor files...")
51 logging.debug("Considered for deletion: %s" % "\n".join(files))
53 deleted = util.deleteFilesOlderThan(files, seconds)
54 logging.info("Deleted %d unparseable descriptor files." % len(deleted))
56 def find(filename):
57 """Find the executable ``filename``.
59 :param string filename: The executable to search for. Must be in the
60 effective user ID's $PATH.
61 :rtype: string
62 :returns: The location of the executable, if found. Otherwise, returns
63 None.
64 """
65 executable = None
67 logging.debug("Searching for installed '%s'..." % filename)
68 which = procutils.which(filename, os.X_OK)
70 if len(which) > 0:
71 for that in which:
72 if os.stat(that).st_uid == os.geteuid():
73 executable = that
74 break
75 if not executable:
76 return None
78 logging.debug("Found installed script at '%s'" % executable)
79 return executable
81 def generateDescriptors(count=None, rundir=None):
82 """Run a script which creates fake bridge descriptors for testing purposes.
84 :param integer count: Number of mocked bridges to generate descriptor
85 for. (default: 3)
86 :type rundir: string or None
87 :param rundir: If given, use this directory as the current working
88 directory for the bridge descriptor generator script to run in. The
89 directory MUST already exist, and the descriptor files will be created
90 in it. If None, use the whatever directory we are currently in.
91 """
92 from stem.descriptor.server_descriptor import RelayDescriptor
94 count = count if count else 3
95 rundir = rundir if rundir else os.getcwd()
97 for i in range(count):
98 with open(os.path.join(rundir, 'descriptor_%i' % i), 'w') as descriptor_file:
99 descriptor_file.write(RelayDescriptor.content(sign = True))