commit 4a2cdadd4bb4907b1e51b3ded03c5ab1cf36d6be Author: Damian Johnson atagar@torproject.org Date: Sun Sep 8 16:35:41 2013 -0700
Changing getPathPrefix() to a function
The torTools' getPathPrefix() method only uses the self reference to prevent logging duplicate messages. Swapping this over to be a funciton instead. --- arm/graphing/bandwidthStats.py | 2 +- arm/logPanel.py | 2 +- arm/starter.py | 2 +- arm/util/torConfig.py | 2 +- arm/util/torTools.py | 69 +++++++++++++++++++++------------------- 5 files changed, 40 insertions(+), 37 deletions(-)
diff --git a/arm/graphing/bandwidthStats.py b/arm/graphing/bandwidthStats.py index 40117dc..b6f5d5a 100644 --- a/arm/graphing/bandwidthStats.py +++ b/arm/graphing/bandwidthStats.py @@ -151,7 +151,7 @@ class BandwidthStats(graphPanel.GraphStats): return False
# attempt to open the state file - try: stateFile = open("%s%s/state" % (conn.getPathPrefix(), dataDir), "r") + try: stateFile = open("%s%s/state" % (torTools.getPathPrefix(), dataDir), "r") except IOError: msg = PREPOPULATE_FAILURE_MSG % "unable to read the state file" log.notice(msg) diff --git a/arm/logPanel.py b/arm/logPanel.py index 98843c4..a1b0b87 100644 --- a/arm/logPanel.py +++ b/arm/logPanel.py @@ -218,7 +218,7 @@ def getLogFileEntries(runlevels, readLimit = None, addLimit = None): if not loggingLocation: return []
# includes the prefix for tor paths - loggingLocation = torTools.getConn().getPathPrefix() + loggingLocation + loggingLocation = torTools.getPathPrefix() + loggingLocation
# if the runlevels argument is a superset of the log file then we can # limit the read contents to the addLimit diff --git a/arm/starter.py b/arm/starter.py index 53b28ff..67fc5a6 100644 --- a/arm/starter.py +++ b/arm/starter.py @@ -223,7 +223,7 @@ def _getController(controlAddr="127.0.0.1", controlPort=9051, passphrase=None, i
controller = None try: - chroot = arm.util.torTools.getConn().getPathPrefix() + chroot = arm.util.torTools.getPathPrefix() controller = Controller.from_port(controlAddr, controlPort)
try: diff --git a/arm/util/torConfig.py b/arm/util/torConfig.py index b7289ad..beb88b1 100644 --- a/arm/util/torConfig.py +++ b/arm/util/torConfig.py @@ -343,7 +343,7 @@ def getConfigLocation():
conn = torTools.getConn() configLocation = conn.getInfo("config-file", None) - torPid, torPrefix = conn.controller.get_pid(None), conn.getPathPrefix() + torPid, torPrefix = conn.controller.get_pid(None), torTools.getPathPrefix() if not configLocation: raise IOError("unable to query the torrc location")
try: diff --git a/arm/util/torTools.py b/arm/util/torTools.py index dc95fd8..e7766e3 100644 --- a/arm/util/torTools.py +++ b/arm/util/torTools.py @@ -31,6 +31,12 @@ REQ_EVENTS = {"NEWDESC": "information related to descriptors will grow stale", "NS": "information related to the consensus will grow stale", "NEWCONSENSUS": "information related to the consensus will grow stale"}
+# Logs issues and notices when fetching the path prefix if true. This is +# only done once for the duration of the application to avoid pointless +# messages. + +PATH_PREFIX_LOGGING = True + def getConn(): """ Singleton constructor for a Controller. Be aware that this starts as being @@ -41,6 +47,36 @@ def getConn(): if CONTROLLER == None: CONTROLLER = Controller() return CONTROLLER
+def getPathPrefix(): + """ + Provides the path prefix that should be used for fetching tor resources. + If undefined and Tor is inside a jail under FreeBsd then this provides the + jail's path. + """ + + global PATH_PREFIX_LOGGING + + # make sure the path prefix is valid and exists (providing a notice if not) + prefixPath = CONFIG["features.pathPrefix"].strip() + + if not prefixPath and os.uname()[0] == "FreeBSD": + prefixPath = system.get_bsd_jail_path(getConn().controller.get_pid(0)) + + if prefixPath and PATH_PREFIX_LOGGING: + log.info("Adjusting paths to account for Tor running in a jail at: %s" % prefixPath) + + if prefixPath: + # strips off ending slash from the path + if prefixPath.endswith("/"): prefixPath = prefixPath[:-1] + + # avoid using paths that don't exist + if PATH_PREFIX_LOGGING and prefixPath and not os.path.exists(prefixPath): + log.notice("The prefix path set in your config (%s) doesn't exist." % prefixPath) + prefixPath = "" + + PATH_PREFIX_LOGGING = False # prevents logging if fetched again + return prefixPath + class Controller: """ Stem wrapper providing convenience functions (mostly from the days of using @@ -58,11 +94,6 @@ class Controller: self._consensusLookupCache = {} # lookup cache with network status entries self._descriptorLookupCache = {} # lookup cache with relay descriptors self._lastNewnym = 0 # time we last sent a NEWNYM signal - - # Logs issues and notices when fetching the path prefix if true. This is - # only done once for the duration of the application to avoid pointless - # messages. - self._pathPrefixLogging = True
def init(self, controller): """ @@ -558,34 +589,6 @@ class Controller:
return (None, None)
- def getPathPrefix(self): - """ - Provides the path prefix that should be used for fetching tor resources. - If undefined and Tor is inside a jail under FreeBsd then this provides the - jail's path. - """ - - # make sure the path prefix is valid and exists (providing a notice if not) - prefixPath = CONFIG["features.pathPrefix"].strip() - - if not prefixPath and os.uname()[0] == "FreeBSD": - prefixPath = system.get_bsd_jail_path(getConn().controller.get_pid(0)) - - if prefixPath and self._pathPrefixLogging: - log.info("Adjusting paths to account for Tor running in a jail at: %s" % prefixPath) - - if prefixPath: - # strips off ending slash from the path - if prefixPath.endswith("/"): prefixPath = prefixPath[:-1] - - # avoid using paths that don't exist - if self._pathPrefixLogging and prefixPath and not os.path.exists(prefixPath): - log.notice("The prefix path set in your config (%s) doesn't exist." % prefixPath) - prefixPath = "" - - self._pathPrefixLogging = False # prevents logging if fetched again - return prefixPath - def getStartTime(self): """ Provides the unix time for when the tor process first started. If this