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: **
18 from __future__
import print_function
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
39 :param str directory: The directory in which to search for unparseable
41 :param int olderThan: If a file's mtime is more than this number
42 (in seconds), it will be deleted.
46 for pattern
in ["*.unparseable", "*.unparseable.xz"]:
47 files
.extend(glob
.glob(os
.sep
.join([directory
, pattern
])))
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
))
57 """Find the executable ``filename``.
59 :param string filename: The executable to search for. Must be in the
60 effective user ID's $PATH.
62 :returns: The location of the executable, if found. Otherwise, returns
67 logging
.debug("Searching for installed '%s'..." % filename
)
68 which
= procutils
.which(filename
, os
.X_OK
)
72 if os
.stat(that
).st_uid
== os
.geteuid():
78 logging
.debug("Found installed script at '%s'" % 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
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.
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))