use proper data folders

This commit is contained in:
Kevin Froman 2019-07-14 22:01:56 -05:00
parent 650d5077ed
commit 837286a970
8 changed files with 63 additions and 25 deletions

View File

@ -19,11 +19,9 @@
''' '''
import os, json, logger import os, json, logger
from utils import identifyhome
# set data dir # set data dir
dataDir = os.environ.get('ONIONR_HOME', os.environ.get('DATA_DIR', 'data/')) dataDir = identifyhome.identify_home()
if not dataDir.endswith('/'):
dataDir += '/'
_configfile = os.path.abspath(dataDir + 'config.json') _configfile = os.path.abspath(dataDir + 'config.json')
_config = {} _config = {}

View File

@ -30,6 +30,7 @@ import dbcreator, onionrstorage, serializeddata, subprocesspow
from etc import onionrvalues, powchoice from etc import onionrvalues, powchoice
from onionrutils import localcommand, stringvalidators, bytesconverter, epoch from onionrutils import localcommand, stringvalidators, bytesconverter, epoch
from onionrutils import blockmetadata from onionrutils import blockmetadata
from utils import identifyhome
import storagecounter import storagecounter
class Core: class Core:
@ -38,9 +39,7 @@ class Core:
Initialize Core Onionr library Initialize Core Onionr library
''' '''
# set data dir # set data dir
self.dataDir = os.environ.get('ONIONR_HOME', os.environ.get('DATA_DIR', 'data/')) self.dataDir = identifyhome.identify_home()
if not self.dataDir.endswith('/'):
self.dataDir += '/'
try: try:
self.usageFile = self.dataDir + 'disk-usage.txt' self.usageFile = self.dataDir + 'disk-usage.txt'

View File

@ -29,7 +29,7 @@ def readline(message = ''):
color = colors.fg.green + colors.bold color = colors.fg.green + colors.bold
output = colors.reset + str(color) + '... ' + colors.reset + str(message) + colors.reset output = colors.reset + str(color) + '... ' + colors.reset + str(message) + colors.reset
if not settings.get_settings() & USE_ANSI: if not settings.get_settings() & settings.USE_ANSI:
output = colors.filter(output) output = colors.filter(output)
sys.stdout.write(output) sys.stdout.write(output)

View File

@ -18,7 +18,9 @@
along with this program. If not, see <https://www.gnu.org/licenses/>. along with this program. If not, see <https://www.gnu.org/licenses/>.
''' '''
import os import os
data_home = os.environ.get('DATA_DIR', os.environ.get('DATA_DIR', 'data')) from utils import identifyhome
data_home = os.environ.get('ONIONR_LOG_DIR', identifyhome.identify_home())
# Use the bitwise operators to merge these settings # Use the bitwise operators to merge these settings
USE_ANSI = 0b100 USE_ANSI = 0b100
if os.name == 'nt': if os.name == 'nt':

View File

@ -20,6 +20,7 @@
import os, sys, base64, subprocess, signal import os, sys, base64, subprocess, signal
import config, logger import config, logger
from . import getopenport from . import getopenport
from utils import identifyhome
config.reload() config.reload()
class NetController: class NetController:
''' '''
@ -28,9 +29,7 @@ class NetController:
def __init__(self, hsPort, apiServerIP='127.0.0.1'): def __init__(self, hsPort, apiServerIP='127.0.0.1'):
# set data dir # set data dir
self.dataDir = os.environ.get('ONIONR_HOME', os.environ.get('DATA_DIR', 'data/')) self.dataDir = identifyhome.identify_home()
if not self.dataDir.endswith('/'):
self.dataDir += '/'
self.torConfigLocation = self.dataDir + 'torrc' self.torConfigLocation = self.dataDir + 'torrc'
self.readyState = False self.readyState = False

View File

@ -43,11 +43,12 @@ from netcontroller import NetController
from onionrblockapi import Block from onionrblockapi import Block
import onionrproofs, onionrexceptions, communicator, setupconfig import onionrproofs, onionrexceptions, communicator, setupconfig
import onionrcommands as commands # Many command definitions are here import onionrcommands as commands # Many command definitions are here
from utils import identifyhome
try: try:
from urllib3.contrib.socks import SOCKSProxyManager from urllib3.contrib.socks import SOCKSProxyManager
except ImportError: except ImportError:
raise Exception("You need the PySocks module (for use with socks5 proxy to use Tor)") raise ImportError("You need the PySocks module (for use with socks5 proxy to use Tor)")
class Onionr: class Onionr:
def __init__(self): def __init__(self):
@ -67,7 +68,7 @@ class Onionr:
pass pass
# set data dir # set data dir
self.dataDir = os.environ.get('ONIONR_HOME', os.environ.get('DATA_DIR', 'data/')) self.dataDir = identifyhome.identify_home()
if not self.dataDir.endswith('/'): if not self.dataDir.endswith('/'):
self.dataDir += '/' self.dataDir += '/'
@ -97,7 +98,8 @@ class Onionr:
if not os.path.exists(plugins.get_plugin_data_folder(name)): if not os.path.exists(plugins.get_plugin_data_folder(name)):
try: try:
os.mkdir(plugins.get_plugin_data_folder(name)) os.mkdir(plugins.get_plugin_data_folder(name))
except: except Exception as e:
logger.warn('Error enabling plugin: ' + str(e))
plugins.disable(name, onionr = self, stop_event = False) plugins.disable(name, onionr = self, stop_event = False)
self.communicatorInst = None self.communicatorInst = None
@ -140,6 +142,7 @@ class Onionr:
finally: finally:
self.execute(command) self.execute(command)
os.chdir(self.userRunDir)
return return
def exitSigterm(self, signum, frame): def exitSigterm(self, signum, frame):
@ -175,7 +178,7 @@ class Onionr:
def get_hostname(self): def get_hostname(self):
try: try:
with open('./' + self.dataDir + 'hs/hostname', 'r') as hostname: with open(self.dataDir + 'hs/hostname', 'r') as hostname:
return hostname.read().strip() return hostname.read().strip()
except FileNotFoundError: except FileNotFoundError:
return "Not Generated" return "Not Generated"

View File

@ -19,11 +19,9 @@
''' '''
import os, re, importlib import os, re, importlib
import onionrevents as events, config, logger import onionrevents as events, config, logger
from utils import identifyhome
# set data dir # set data dir
dataDir = os.environ.get('ONIONR_HOME', os.environ.get('DATA_DIR', 'data/')) dataDir = identifyhome.identify_home()
if not dataDir.endswith('/'):
dataDir += '/'
_pluginsfolder = dataDir + 'plugins/' _pluginsfolder = dataDir + 'plugins/'
_instances = dict() _instances = dict()
@ -75,7 +73,7 @@ def enable(name, onionr = None, start_event = True):
return False return False
else: else:
enabled_plugins.append(name) enabled_plugins.append(name)
config.set('plugins.enabled', enabled_plugins, True) config.set('plugins.enabled', enabled_plugins, savefile=True)
if start_event is True: if start_event is True:
start(name) start(name)
@ -83,7 +81,7 @@ def enable(name, onionr = None, start_event = True):
else: else:
return False return False
else: else:
logger.error('Failed to enable plugin \"%s\", disabling plugin.' % name) logger.error('Failed to enable plugin \"%s\", disabling plugin.' % name, terminal=True)
disable(name) disable(name)
return False return False
@ -245,7 +243,7 @@ def get_plugin_data_folder(name, absolute = True):
Returns the location of a plugin's data folder Returns the location of a plugin's data folder
''' '''
return get_plugins_folder(name, absolute) + dataDir return get_plugins_folder(name, absolute)
def check(): def check():
''' '''

View File

@ -0,0 +1,39 @@
'''
Onionr - Private P2P Communication
Identify a data directory for Onionr
'''
'''
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
'''
import os, platform
def identify_home():
path = os.environ.get('ONIONR_HOME', None)
if path is None:
system = platform.system()
if system == 'Linux':
path = os.path.expanduser('~') + '/.local/share/onionr/'
elif system == 'Windows':
path = os.path.expanduser('~') + '\\AppData\\Local\\onionr\\'
elif system == 'Darwin':
path = os.path.expanduser('~' + '/Library/Application Support/onionr/')
else:
path = 'data/'
else:
path = os.path.abspath(path)
if not path.endswith('/'):
path += '/'
return path