install_tor

This commit is contained in:
j 2016-01-10 19:13:10 +05:30
parent bc593b65fd
commit cda61c5560

View file

@ -1,4 +1,5 @@
import os
import sys
import subprocess
from threading import Thread
import distutils
@ -43,32 +44,9 @@ DirReqStatistics 0
'''.strip().format(base=settings.config_path))
return defaults, torrc
def get_tor(self):
def cmd_exists(cmd):
return subprocess.call("type " + cmd, shell=True,
stdout=subprocess.PIPE, stderr=subprocess.PIPE) == 0
for path in (
'/Applications/TorBrowser.app/TorBrowser/Tor/tor',
):
if os.path.isfile(path) and os.access(path, os.X_OK):
return path
start = os.path.expanduser('~/.local/share/applications/start-tor-browser.desktop')
if os.path.exists(start):
with open(start) as fd:
e = [line for line in fd.read().split('\n') if line.startswith('Exec')]
if e:
try:
base = os.path.dirname(e[0].split('"')[1])
path = os.path.join(base, 'TorBrowser', 'Tor', 'tor')
if os.path.isfile(path) and os.access(path, os.X_OK):
return path
except:
pass
return distutils.spawn.find_executable('tor')
def run(self):
defaults, torrc = self.create_torrc()
tor = self.get_tor()
tor = get_tor()
if not tor:
self._status.append('No tor binary found. Please install TorBrowser or tor')
else:
@ -203,7 +181,6 @@ class Tor(object):
def torbrowser_url():
import re
import sys
from ox.cache import read_url
base_url = 'https://dist.torproject.org/torbrowser/'
@ -211,7 +188,11 @@ def torbrowser_url():
current = sorted(r.findall(read_url(base_url).decode()))[-1]
url = base_url + current
if sys.platform.startswith('linux'):
import platform
if platform.architecture()[0] == '64bit':
osname = 'linux64'
else:
osname = 'linux32'
ext = 'xz'
elif sys.platform == 'darwin':
osname = 'osx64'
@ -226,3 +207,65 @@ def torbrowser_url():
torbrowser = sorted(r.findall(read_url(url).decode()))[-1]
url += torbrowser
return url
def get_tor():
if sys.platform == 'darwin':
for path in (
'/Applications/TorBrowser.app/TorBrowser/Tor/tor',
os.path.join(settings.base_dir, 'tor', 'TorBrowser.app/TorBrowser/Tor/tor')
):
if os.path.isfile(path) and os.access(path, os.X_OK):
return path
start = os.path.expanduser('~/.local/share/applications/start-tor-browser.desktop')
if os.path.exists(start):
with open(start) as fd:
e = [line for line in fd.read().split('\n') if line.startswith('Exec')]
if e:
try:
base = os.path.dirname(e[0].split('"')[1])
path = os.path.join(base, 'TorBrowser', 'Tor', 'tor')
if os.path.isfile(path) and os.access(path, os.X_OK):
return path
except:
pass
return distutils.spawn.find_executable('tor')
def install_tor():
import tarfile
import update
import shutil
# only install if tor can not be found
if get_tor():
print('found existing tor installation')
url = torbrowser_url()
target = os.path.join(settings.base_dir, 'tor')
if url:
print('downloading and installing tor')
if sys.platform.startswith('linux'):
os.makedirs(target)
tar_file = os.path.join(target, os.path.basename(url))
update.get(url, tar_file)
tar = tarfile.open(tar_file)
tar.extractall(target)
desktop = os.path.join(target, tar.members[0].path, 'start-tor-browser.desktop')
tar.close()
subprocess.call([desktop, '--register-app'], cwd=target)
os.unlink(tar_file)
elif sys.platform == 'darwin':
os.makedirs(target)
dmg = os.path.join(target, os.path.basename(url))
update.get(url, dmg)
subprocess.call(['hdiutil', 'mount', dmg])
volume = '/Volumes/Tor Browser'
name = 'TorBrowser.app'
app = os.path.join(volume, name)
if os.access('/Applications', os.W_OK):
target = os.path.join('/Applications', name)
else:
os.makedirs(target)
target = os.path.join(target, name)
if not os.path.exists(target):
shutil.copy(app, target)
subprocess.call(['hdiutil', 'unmount', volume])
else:
logger.debug('no way to install TorBrowser on %s so far', sys.platform)