2019-10-22 16:23:38 +02:00
|
|
|
"""
|
|
|
|
Path related functions
|
|
|
|
"""
|
2019-09-27 16:09:29 +02:00
|
|
|
import logging
|
|
|
|
import os
|
2017-11-07 12:46:23 +01:00
|
|
|
import re
|
2019-09-27 16:09:29 +02:00
|
|
|
import sys
|
2017-11-07 12:46:23 +01:00
|
|
|
from datetime import datetime
|
2019-09-27 16:09:29 +02:00
|
|
|
from shutil import move
|
|
|
|
|
|
|
|
|
|
|
|
logger = logging.getLogger('default')
|
2017-01-11 17:00:00 +01:00
|
|
|
|
|
|
|
# When using py2exe or py2app, the variable frozen is added to the sys
|
2019-09-27 16:09: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-27 16:09:29 +02:00
|
|
|
frozen = getattr(sys, 'frozen', None)
|
|
|
|
|
2017-01-11 17:00:00 +01:00
|
|
|
|
|
|
|
def lookupExeFolder():
|
2019-09-27 16:09:29 +02:00
|
|
|
"""Returns executable folder path"""
|
2017-01-11 17:00:00 +01:00
|
|
|
if frozen:
|
2019-09-27 16:09:29 +02:00
|
|
|
exeFolder = (
|
2017-01-11 17:00:00 +01:00
|
|
|
# targetdir/Bitmessage.app/Contents/MacOS/Bitmessage
|
2019-09-27 16:09:29 +02:00
|
|
|
os.path.dirname(sys.executable).split(os.path.sep)[0] + os.path.sep
|
|
|
|
if frozen == "macosx_app" else
|
|
|
|
os.path.dirname(sys.executable) + os.path.sep)
|
2017-01-11 17:00:00 +01:00
|
|
|
elif __file__:
|
2019-09-27 16:09:29 +02:00
|
|
|
exeFolder = os.path.dirname(__file__) + os.path.sep
|
2017-01-11 17:00:00 +01:00
|
|
|
else:
|
|
|
|
exeFolder = ''
|
|
|
|
return exeFolder
|
|
|
|
|
2019-09-27 16:09:29 +02:00
|
|
|
|
2017-01-11 17:00:00 +01:00
|
|
|
def lookupAppdataFolder():
|
2019-09-27 16:09:29 +02:00
|
|
|
"""Returns path of the folder where application data is stored"""
|
2017-01-11 17:00:00 +01:00
|
|
|
APPNAME = "PyBitmessage"
|
2019-09-27 16:09:29 +02:00
|
|
|
dataFolder = os.environ.get('BITMESSAGE_HOME')
|
|
|
|
if dataFolder:
|
|
|
|
if dataFolder[-1] not in (os.path.sep, os.path.altsep):
|
|
|
|
dataFolder += os.path.sep
|
2017-01-11 17:00:00 +01:00
|
|
|
elif sys.platform == 'darwin':
|
2019-09-27 16:09:29 +02:00
|
|
|
try:
|
|
|
|
dataFolder = os.path.join(
|
|
|
|
os.environ['HOME'],
|
|
|
|
'Library/Application Support/', APPNAME
|
2019-10-22 16:23:38 +02:00
|
|
|
) + '/'
|
|
|
|
|
2019-09-27 16:09:29 +02:00
|
|
|
except KeyError:
|
|
|
|
sys.exit(
|
|
|
|
'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
|
|
|
elif 'win32' in sys.platform or 'win64' in sys.platform:
|
2019-09-27 16:09:29 +02:00
|
|
|
dataFolder = os.path.join(
|
|
|
|
os.environ['APPDATA'].decode(
|
|
|
|
sys.getfilesystemencoding(), 'ignore'), APPNAME
|
|
|
|
) + os.path.sep
|
2017-01-11 17:00:00 +01:00
|
|
|
else:
|
|
|
|
try:
|
2019-09-27 16:09:29 +02:00
|
|
|
dataFolder = os.path.join(os.environ['XDG_CONFIG_HOME'], APPNAME)
|
2017-01-11 17:00:00 +01:00
|
|
|
except KeyError:
|
2019-09-27 16:09:29 +02:00
|
|
|
dataFolder = os.path.join(os.environ['HOME'], '.config', APPNAME)
|
2017-01-11 17:00:00 +01:00
|
|
|
|
2019-09-27 16:09:29 +02:00
|
|
|
# Migrate existing data to the proper location
|
|
|
|
# if this is an existing install
|
2017-01-11 17:00:00 +01:00
|
|
|
try:
|
2019-09-27 16:09:29 +02:00
|
|
|
move(os.path.join(os.environ['HOME'], '.%s' % APPNAME), dataFolder)
|
|
|
|
logger.info('Moving data folder to %s', dataFolder)
|
2017-01-11 17:00:00 +01:00
|
|
|
except IOError:
|
|
|
|
# Old directory may not exist.
|
|
|
|
pass
|
2019-09-27 16:09:29 +02:00
|
|
|
dataFolder = dataFolder + os.path.sep
|
2017-01-11 17:00:00 +01:00
|
|
|
return dataFolder
|
2019-09-27 16:09:29 +02:00
|
|
|
|
|
|
|
|
2017-01-11 17:00:00 +01:00
|
|
|
def codePath():
|
2019-09-27 16:09:29 +02:00
|
|
|
"""Returns path to the program sources"""
|
|
|
|
if not frozen:
|
|
|
|
return os.path.dirname(__file__)
|
|
|
|
return (
|
|
|
|
os.environ.get('RESOURCEPATH')
|
2019-10-22 16:23:38 +02:00
|
|
|
# pylint: disable=protected-access
|
2019-09-27 16:09:29 +02:00
|
|
|
if frozen == "macosx_app" else sys._MEIPASS)
|
|
|
|
|
2017-01-11 17:00:00 +01:00
|
|
|
|
2017-02-07 20:46:30 +01:00
|
|
|
def tail(f, lines=20):
|
2019-09-27 16:09:29 +02:00
|
|
|
"""Returns last lines in the f file object"""
|
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-27 16:09:29 +02:00
|
|
|
# blocks of size BLOCK_SIZE, in reverse order starting
|
|
|
|
# from the end of the file
|
|
|
|
blocks = []
|
2017-02-07 20:46:30 +01:00
|
|
|
while lines_to_go > 0 and block_end_byte > 0:
|
2019-10-22 16:23:38 +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-27 16:09:29 +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-09-27 16:09: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-27 16:09:29 +02:00
|
|
|
"""
|
|
|
|
Returns last commit information as dict with 'commit' and 'time' keys
|
|
|
|
"""
|
|
|
|
githeadfile = os.path.join(codePath(), '..', '.git', 'logs', 'HEAD')
|
2017-11-07 12:46:23 +01:00
|
|
|
result = {}
|
2019-09-27 16:09:29 +02:00
|
|
|
if os.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
|
2017-11-07 12:46:23 +01:00
|
|
|
return result
|