2019-09-20 13:19:04 +02:00
|
|
|
"""
|
|
|
|
src/paths.py
|
|
|
|
============
|
|
|
|
"""
|
|
|
|
# pylint: disable=import-error
|
2017-01-11 17:00:00 +01:00
|
|
|
from os import environ, path
|
|
|
|
import sys
|
2017-11-07 12:46:23 +01:00
|
|
|
import re
|
2019-05-09 14:48:29 +02:00
|
|
|
import os
|
2017-11-07 12:46:23 +01:00
|
|
|
from datetime import datetime
|
2019-05-09 14:48:29 +02:00
|
|
|
from kivy.utils import platform
|
2017-01-11 17:00:00 +01:00
|
|
|
# When using py2exe or py2app, the variable frozen is added to the sys
|
2019-05-09 14:48:29 +02:00
|
|
|
# namespace. This can be used to setup a different code path for
|
2017-01-11 17:00:00 +01:00
|
|
|
# binary distributions vs source distributions.
|
2019-09-20 13:19:04 +02:00
|
|
|
frozen = getattr(sys, 'frozen', None)
|
|
|
|
|
2017-01-11 17:00:00 +01:00
|
|
|
|
|
|
|
def lookupExeFolder():
|
2019-09-20 13:19:04 +02:00
|
|
|
"""
|
|
|
|
Folder with PyBitmessage binary (.exe, .app, ...). If it is run from source, it returns the source root
|
|
|
|
directory
|
|
|
|
"""
|
2017-01-11 17:00:00 +01:00
|
|
|
if frozen:
|
|
|
|
if frozen == "macosx_app":
|
|
|
|
# targetdir/Bitmessage.app/Contents/MacOS/Bitmessage
|
|
|
|
exeFolder = path.dirname(path.dirname(path.dirname(path.dirname(sys.executable)))) + path.sep
|
|
|
|
else:
|
|
|
|
exeFolder = path.dirname(sys.executable) + path.sep
|
|
|
|
elif __file__:
|
|
|
|
exeFolder = path.dirname(__file__) + path.sep
|
|
|
|
else:
|
|
|
|
exeFolder = ''
|
|
|
|
return exeFolder
|
|
|
|
|
2019-05-09 14:48:29 +02:00
|
|
|
|
2019-09-20 13:19:04 +02:00
|
|
|
def lookupAppdataFolder(): # pylint: disable=too-many-branches
|
|
|
|
"""Folder with runtime data (like configuration, database, ...)"""
|
|
|
|
# flake8: noqa=F821
|
2019-05-09 14:48:29 +02:00
|
|
|
import traceback
|
2019-09-20 13:19:04 +02:00
|
|
|
print traceback.print_tb
|
2017-01-11 17:00:00 +01:00
|
|
|
APPNAME = "PyBitmessage"
|
|
|
|
if "BITMESSAGE_HOME" in environ:
|
|
|
|
dataFolder = environ["BITMESSAGE_HOME"]
|
2017-01-11 17:26:25 +01:00
|
|
|
if dataFolder[-1] not in [path.sep, path.altsep]:
|
|
|
|
dataFolder += path.sep
|
2017-01-11 17:00:00 +01:00
|
|
|
elif sys.platform == 'darwin':
|
|
|
|
if "HOME" in environ:
|
2017-01-11 17:26:25 +01:00
|
|
|
dataFolder = path.join(environ["HOME"], "Library/Application Support/", APPNAME) + '/'
|
2017-01-11 17:00:00 +01:00
|
|
|
else:
|
2019-09-20 13:19:04 +02:00
|
|
|
stringToLog = (
|
|
|
|
'Could not find home folder, please report this message'
|
|
|
|
' and your OS X version to the BitMessage Github.')
|
2017-01-11 17:00:00 +01:00
|
|
|
if 'logger' in globals():
|
2019-09-20 13:19:04 +02:00
|
|
|
logger.critical(stringToLog) # pylint: disable=undefined-variable
|
2017-01-11 17:00:00 +01:00
|
|
|
else:
|
|
|
|
print stringToLog
|
|
|
|
sys.exit()
|
2019-05-09 14:48:29 +02:00
|
|
|
elif platform == 'android':
|
2019-08-06 18:13:34 +02:00
|
|
|
dataFolder = path.join(os.environ['ANDROID_PRIVATE'] + '/', APPNAME) + '/'
|
2017-01-11 17:00:00 +01:00
|
|
|
|
|
|
|
elif 'win32' in sys.platform or 'win64' in sys.platform:
|
|
|
|
dataFolder = path.join(environ['APPDATA'].decode(sys.getfilesystemencoding(), 'ignore'), APPNAME) + path.sep
|
|
|
|
else:
|
|
|
|
from shutil import move
|
|
|
|
try:
|
|
|
|
dataFolder = path.join(environ["XDG_CONFIG_HOME"], APPNAME)
|
|
|
|
except KeyError:
|
|
|
|
dataFolder = path.join(environ["HOME"], ".config", APPNAME)
|
|
|
|
|
|
|
|
# Migrate existing data to the proper location if this is an existing install
|
|
|
|
try:
|
|
|
|
move(path.join(environ["HOME"], ".%s" % APPNAME), dataFolder)
|
|
|
|
stringToLog = "Moving data folder to %s" % (dataFolder)
|
|
|
|
if 'logger' in globals():
|
2019-09-20 13:19:04 +02:00
|
|
|
logger.info(stringToLog) # pylint: disable=undefined-variable
|
2017-01-11 17:00:00 +01:00
|
|
|
else:
|
|
|
|
print stringToLog
|
|
|
|
except IOError:
|
|
|
|
# Old directory may not exist.
|
|
|
|
pass
|
|
|
|
dataFolder = dataFolder + '/'
|
|
|
|
return dataFolder
|
2019-05-09 14:48:29 +02:00
|
|
|
|
2019-09-20 13:19:04 +02:00
|
|
|
|
2017-01-11 17:00:00 +01:00
|
|
|
def codePath():
|
2019-09-20 13:19:04 +02:00
|
|
|
"""Return the code path of the running instance"""
|
|
|
|
# pylint: disable=redefined-outer-name
|
2017-01-11 17:00:00 +01:00
|
|
|
if frozen == "macosx_app":
|
|
|
|
codePath = environ.get("RESOURCEPATH")
|
2019-09-20 13:19:04 +02:00
|
|
|
elif frozen: # windows
|
|
|
|
codePath = sys._MEIPASS # pylint: disable=no-member,protected-access
|
2019-05-09 14:48:29 +02:00
|
|
|
else:
|
2017-01-11 17:00:00 +01:00
|
|
|
codePath = path.dirname(__file__)
|
|
|
|
return codePath
|
|
|
|
|
2019-09-20 13:19:04 +02:00
|
|
|
|
2017-02-07 20:46:30 +01:00
|
|
|
def tail(f, lines=20):
|
2019-09-20 13:19:04 +02:00
|
|
|
"""Read last lines of a file. Like tail(1)"""
|
2017-02-07 20:46:30 +01:00
|
|
|
total_lines_wanted = lines
|
|
|
|
|
|
|
|
BLOCK_SIZE = 1024
|
|
|
|
f.seek(0, 2)
|
|
|
|
block_end_byte = f.tell()
|
|
|
|
lines_to_go = total_lines_wanted
|
|
|
|
block_number = -1
|
2019-09-20 13:19:04 +02:00
|
|
|
blocks = []
|
|
|
|
# blocks of size BLOCK_SIZE, in reverse order starting
|
|
|
|
# from the end of the file
|
2017-02-07 20:46:30 +01:00
|
|
|
while lines_to_go > 0 and block_end_byte > 0:
|
2019-09-20 13:19:04 +02:00
|
|
|
if block_end_byte - BLOCK_SIZE > 0:
|
2017-02-07 20:46:30 +01:00
|
|
|
# read the last block we haven't yet read
|
2019-09-20 13:19:04 +02:00
|
|
|
f.seek(block_number * BLOCK_SIZE, 2)
|
2017-02-07 20:46:30 +01:00
|
|
|
blocks.append(f.read(BLOCK_SIZE))
|
|
|
|
else:
|
|
|
|
# file too small, start from begining
|
2019-05-09 14:48:29 +02:00
|
|
|
f.seek(0, 0)
|
2017-02-07 20:46:30 +01:00
|
|
|
# only read what was not read
|
|
|
|
blocks.append(f.read(block_end_byte))
|
|
|
|
lines_found = blocks[-1].count('\n')
|
|
|
|
lines_to_go -= lines_found
|
|
|
|
block_end_byte -= BLOCK_SIZE
|
|
|
|
block_number -= 1
|
|
|
|
all_read_text = ''.join(reversed(blocks))
|
|
|
|
return '\n'.join(all_read_text.splitlines()[-total_lines_wanted:])
|
|
|
|
|
2017-11-07 12:46:23 +01:00
|
|
|
|
2017-02-07 20:46:30 +01:00
|
|
|
def lastCommit():
|
2019-09-20 13:19:04 +02:00
|
|
|
"""Git commitish of the currently checked out repository"""
|
2017-02-07 20:46:30 +01:00
|
|
|
githeadfile = path.join(codePath(), '..', '.git', 'logs', 'HEAD')
|
2017-11-07 12:46:23 +01:00
|
|
|
result = {}
|
|
|
|
if path.isfile(githeadfile):
|
2017-02-07 20:46:30 +01:00
|
|
|
try:
|
|
|
|
with open(githeadfile, 'rt') as githead:
|
2017-11-07 12:46:23 +01:00
|
|
|
line = tail(githead, 1)
|
|
|
|
result['commit'] = line.split()[1]
|
|
|
|
result['time'] = datetime.fromtimestamp(
|
|
|
|
float(re.search(r'>\s*(.*?)\s', line).group(1))
|
|
|
|
)
|
|
|
|
except (IOError, AttributeError, TypeError):
|
2017-02-07 20:46:30 +01:00
|
|
|
pass
|
2019-09-20 13:19:04 +02:00
|
|
|
return result
|