flake8: paths
This commit is contained in:
parent
e5b92e29a2
commit
9a438c1a1a
111
src/paths.py
111
src/paths.py
|
@ -1,76 +1,85 @@
|
||||||
from os import environ, path
|
import logging
|
||||||
import sys
|
import os
|
||||||
import re
|
import re
|
||||||
|
import sys
|
||||||
from datetime import datetime
|
from datetime import datetime
|
||||||
|
from shutil import move
|
||||||
|
|
||||||
|
|
||||||
|
logger = logging.getLogger('default')
|
||||||
|
|
||||||
# When using py2exe or py2app, the variable frozen is added to the sys
|
# When using py2exe or py2app, the variable frozen is added to the sys
|
||||||
# namespace. This can be used to setup a different code path for
|
# namespace. This can be used to setup a different code path for
|
||||||
# binary distributions vs source distributions.
|
# binary distributions vs source distributions.
|
||||||
frozen = getattr(sys,'frozen', None)
|
frozen = getattr(sys, 'frozen', None)
|
||||||
|
|
||||||
|
|
||||||
def lookupExeFolder():
|
def lookupExeFolder():
|
||||||
|
"""Returns executable folder path"""
|
||||||
if frozen:
|
if frozen:
|
||||||
if frozen == "macosx_app":
|
exeFolder = (
|
||||||
# targetdir/Bitmessage.app/Contents/MacOS/Bitmessage
|
# targetdir/Bitmessage.app/Contents/MacOS/Bitmessage
|
||||||
exeFolder = path.dirname(path.dirname(path.dirname(path.dirname(sys.executable)))) + path.sep
|
os.path.dirname(sys.executable).split(os.path.sep)[0] + os.path.sep
|
||||||
else:
|
if frozen == "macosx_app" else
|
||||||
exeFolder = path.dirname(sys.executable) + path.sep
|
os.path.dirname(sys.executable) + os.path.sep)
|
||||||
elif __file__:
|
elif __file__:
|
||||||
exeFolder = path.dirname(__file__) + path.sep
|
exeFolder = os.path.dirname(__file__) + os.path.sep
|
||||||
else:
|
else:
|
||||||
exeFolder = ''
|
exeFolder = ''
|
||||||
return exeFolder
|
return exeFolder
|
||||||
|
|
||||||
|
|
||||||
def lookupAppdataFolder():
|
def lookupAppdataFolder():
|
||||||
|
"""Returns path of the folder where application data is stored"""
|
||||||
APPNAME = "PyBitmessage"
|
APPNAME = "PyBitmessage"
|
||||||
if "BITMESSAGE_HOME" in environ:
|
dataFolder = os.environ.get('BITMESSAGE_HOME')
|
||||||
dataFolder = environ["BITMESSAGE_HOME"]
|
if dataFolder:
|
||||||
if dataFolder[-1] not in [path.sep, path.altsep]:
|
if dataFolder[-1] not in (os.path.sep, os.path.altsep):
|
||||||
dataFolder += path.sep
|
dataFolder += os.path.sep
|
||||||
elif sys.platform == 'darwin':
|
elif sys.platform == 'darwin':
|
||||||
if "HOME" in environ:
|
|
||||||
dataFolder = path.join(environ["HOME"], "Library/Application Support/", APPNAME) + '/'
|
|
||||||
else:
|
|
||||||
stringToLog = 'Could not find home folder, please report this message and your OS X version to the BitMessage Github.'
|
|
||||||
if 'logger' in globals():
|
|
||||||
logger.critical(stringToLog)
|
|
||||||
else:
|
|
||||||
print stringToLog
|
|
||||||
sys.exit()
|
|
||||||
|
|
||||||
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:
|
try:
|
||||||
dataFolder = path.join(environ["XDG_CONFIG_HOME"], APPNAME)
|
dataFolder = os.path.join(
|
||||||
|
os.environ['HOME'],
|
||||||
|
'Library/Application Support/', APPNAME
|
||||||
|
) + '/' # FIXME: should also be os.path.sep
|
||||||
except KeyError:
|
except KeyError:
|
||||||
dataFolder = path.join(environ["HOME"], ".config", APPNAME)
|
sys.exit(
|
||||||
|
'Could not find home folder, please report this message'
|
||||||
# Migrate existing data to the proper location if this is an existing install
|
' and your OS X version to the BitMessage Github.')
|
||||||
try:
|
elif 'win32' in sys.platform or 'win64' in sys.platform:
|
||||||
move(path.join(environ["HOME"], ".%s" % APPNAME), dataFolder)
|
dataFolder = os.path.join(
|
||||||
stringToLog = "Moving data folder to %s" % (dataFolder)
|
os.environ['APPDATA'].decode(
|
||||||
if 'logger' in globals():
|
sys.getfilesystemencoding(), 'ignore'), APPNAME
|
||||||
logger.info(stringToLog)
|
) + os.path.sep
|
||||||
else:
|
else:
|
||||||
print stringToLog
|
try:
|
||||||
|
dataFolder = os.path.join(os.environ['XDG_CONFIG_HOME'], APPNAME)
|
||||||
|
except KeyError:
|
||||||
|
dataFolder = os.path.join(os.environ['HOME'], '.config', APPNAME)
|
||||||
|
|
||||||
|
# Migrate existing data to the proper location
|
||||||
|
# if this is an existing install
|
||||||
|
try:
|
||||||
|
move(os.path.join(os.environ['HOME'], '.%s' % APPNAME), dataFolder)
|
||||||
|
logger.info('Moving data folder to %s', dataFolder)
|
||||||
except IOError:
|
except IOError:
|
||||||
# Old directory may not exist.
|
# Old directory may not exist.
|
||||||
pass
|
pass
|
||||||
dataFolder = dataFolder + '/'
|
dataFolder = dataFolder + os.path.sep
|
||||||
return dataFolder
|
return dataFolder
|
||||||
|
|
||||||
|
|
||||||
def codePath():
|
def codePath():
|
||||||
if frozen == "macosx_app":
|
"""Returns path to the program sources"""
|
||||||
codePath = environ.get("RESOURCEPATH")
|
if not frozen:
|
||||||
elif frozen: # windows
|
return os.path.dirname(__file__)
|
||||||
codePath = sys._MEIPASS
|
return (
|
||||||
else:
|
os.environ.get('RESOURCEPATH')
|
||||||
codePath = path.dirname(__file__)
|
if frozen == "macosx_app" else sys._MEIPASS)
|
||||||
return codePath
|
|
||||||
|
|
||||||
def tail(f, lines=20):
|
def tail(f, lines=20):
|
||||||
|
"""Returns last lines in the f file object"""
|
||||||
total_lines_wanted = lines
|
total_lines_wanted = lines
|
||||||
|
|
||||||
BLOCK_SIZE = 1024
|
BLOCK_SIZE = 1024
|
||||||
|
@ -78,16 +87,17 @@ def tail(f, lines=20):
|
||||||
block_end_byte = f.tell()
|
block_end_byte = f.tell()
|
||||||
lines_to_go = total_lines_wanted
|
lines_to_go = total_lines_wanted
|
||||||
block_number = -1
|
block_number = -1
|
||||||
blocks = [] # blocks of size BLOCK_SIZE, in reverse order starting
|
# blocks of size BLOCK_SIZE, in reverse order starting
|
||||||
# from the end of the file
|
# from the end of the file
|
||||||
|
blocks = []
|
||||||
while lines_to_go > 0 and block_end_byte > 0:
|
while lines_to_go > 0 and block_end_byte > 0:
|
||||||
if (block_end_byte - BLOCK_SIZE > 0):
|
if (block_end_byte - BLOCK_SIZE > 0):
|
||||||
# read the last block we haven't yet read
|
# read the last block we haven't yet read
|
||||||
f.seek(block_number*BLOCK_SIZE, 2)
|
f.seek(block_number * BLOCK_SIZE, 2)
|
||||||
blocks.append(f.read(BLOCK_SIZE))
|
blocks.append(f.read(BLOCK_SIZE))
|
||||||
else:
|
else:
|
||||||
# file too small, start from begining
|
# file too small, start from begining
|
||||||
f.seek(0,0)
|
f.seek(0, 0)
|
||||||
# only read what was not read
|
# only read what was not read
|
||||||
blocks.append(f.read(block_end_byte))
|
blocks.append(f.read(block_end_byte))
|
||||||
lines_found = blocks[-1].count('\n')
|
lines_found = blocks[-1].count('\n')
|
||||||
|
@ -99,9 +109,12 @@ def tail(f, lines=20):
|
||||||
|
|
||||||
|
|
||||||
def lastCommit():
|
def lastCommit():
|
||||||
githeadfile = path.join(codePath(), '..', '.git', 'logs', 'HEAD')
|
"""
|
||||||
|
Returns last commit information as dict with 'commit' and 'time' keys
|
||||||
|
"""
|
||||||
|
githeadfile = os.path.join(codePath(), '..', '.git', 'logs', 'HEAD')
|
||||||
result = {}
|
result = {}
|
||||||
if path.isfile(githeadfile):
|
if os.path.isfile(githeadfile):
|
||||||
try:
|
try:
|
||||||
with open(githeadfile, 'rt') as githead:
|
with open(githeadfile, 'rt') as githead:
|
||||||
line = tail(githead, 1)
|
line = tail(githead, 1)
|
||||||
|
|
Reference in New Issue
Block a user