33============
44"""
55# pylint: disable=import-error
6- from os import environ , path
7- import sys
8- import re
6+
7+ import logging
98import os
9+ import re
10+ import sys
1011from datetime import datetime
12+ from shutil import move
1113from kivy .utils import platform
14+
15+
16+ logger = logging .getLogger ('default' )
17+
1218# When using py2exe or py2app, the variable frozen is added to the sys
1319# namespace. This can be used to setup a different code path for
1420# binary distributions vs source distributions.
1521frozen = getattr (sys , 'frozen' , None )
1622
1723
1824def lookupExeFolder ():
19- """
20- Folder with PyBitmessage binary (.exe, .app, ...). If it is run from source, it returns the source root
21- directory
22- """
25+ """Returns executable folder path"""
2326 if frozen :
24- if frozen == "macosx_app" :
27+ exeFolder = (
2528 # targetdir/Bitmessage.app/Contents/MacOS/Bitmessage
26- exeFolder = path . dirname ( path .dirname (path . dirname ( path . dirname ( sys . executable )))) + path .sep
27- else :
28- exeFolder = path .dirname (sys .executable ) + path .sep
29+ os . path .dirname (sys . executable ). split ( os . path . sep )[ 0 ] + os . path .sep
30+ if frozen == "macosx_app" else
31+ os . path .dirname (sys .executable ) + os . path .sep )
2932 elif __file__ :
30- exeFolder = path .dirname (__file__ ) + path .sep
33+ exeFolder = os . path .dirname (__file__ ) + os . path .sep
3134 else :
3235 exeFolder = ''
3336 return exeFolder
3437
3538
36- def lookupAppdataFolder (): # pylint: disable=too-many-branches
37- """Folder with runtime data (like configuration, database, ...)"""
38- # flake8: noqa=F821
39- import traceback
40- print traceback .print_tb
39+ def lookupAppdataFolder ():
40+ """Returns path of the folder where application data is stored"""
4141 APPNAME = "PyBitmessage"
42- if "BITMESSAGE_HOME" in environ :
43- dataFolder = environ [ "BITMESSAGE_HOME" ]
44- if dataFolder [- 1 ] not in [ path .sep , path .altsep ] :
45- dataFolder += path .sep
42+ dataFolder = os . environ . get ( 'BITMESSAGE_HOME' )
43+ if dataFolder :
44+ if dataFolder [- 1 ] not in ( os . path .sep , os . path .altsep ) :
45+ dataFolder += os . path .sep
4646 elif sys .platform == 'darwin' :
47- if "HOME" in environ :
48- dataFolder = path .join (environ ["HOME" ], "Library/Application Support/" , APPNAME ) + '/'
49- else :
50- stringToLog = (
47+ try :
48+ dataFolder = os .path .join (
49+ os .environ ['HOME' ],
50+ 'Library/Application Support/' , APPNAME
51+ ) + '/' # FIXME: should also be os.path.sep
52+ except KeyError :
53+ sys .exit (
5154 'Could not find home folder, please report this message'
5255 ' and your OS X version to the BitMessage Github.' )
53- if 'logger' in globals ():
54- logger .critical (stringToLog ) # pylint: disable=undefined-variable
55- else :
56- print stringToLog
57- sys .exit ()
5856 elif platform == 'android' :
59- dataFolder = path .join (os . environ [ 'ANDROID_PRIVATE' ] + '/' , APPNAME ) + '/'
60-
57+ dataFolder = os . path .join (
58+ os . environ [ 'ANDROID_PRIVATE' ] + '/' , APPNAME ) + '/'
6159 elif 'win32' in sys .platform or 'win64' in sys .platform :
62- dataFolder = path .join (environ ['APPDATA' ].decode (sys .getfilesystemencoding (), 'ignore' ), APPNAME ) + path .sep
60+ dataFolder = os .path .join (
61+ os .environ ['APPDATA' ].decode (
62+ sys .getfilesystemencoding (), 'ignore' ), APPNAME
63+ ) + os .path .sep
6364 else :
64- from shutil import move
6565 try :
66- dataFolder = path .join (environ [" XDG_CONFIG_HOME" ], APPNAME )
66+ dataFolder = os . path .join (os . environ [' XDG_CONFIG_HOME' ], APPNAME )
6767 except KeyError :
68- dataFolder = path .join (environ [" HOME" ], " .config" , APPNAME )
68+ dataFolder = os . path .join (os . environ [' HOME' ], ' .config' , APPNAME )
6969
70- # Migrate existing data to the proper location if this is an existing install
70+ # Migrate existing data to the proper location
71+ # if this is an existing install
7172 try :
72- move (path .join (environ ["HOME" ], ".%s" % APPNAME ), dataFolder )
73- stringToLog = "Moving data folder to %s" % (dataFolder )
74- if 'logger' in globals ():
75- logger .info (stringToLog ) # pylint: disable=undefined-variable
76- else :
77- print stringToLog
73+ move (os .path .join (os .environ ['HOME' ], '.%s' % APPNAME ), dataFolder )
74+ logger .info ('Moving data folder to %s' , dataFolder )
7875 except IOError :
7976 # Old directory may not exist.
8077 pass
81- dataFolder = dataFolder + '/'
78+ dataFolder = dataFolder + os . path . sep
8279 return dataFolder
8380
8481
8582def codePath ():
86- """Return the code path of the running instance"""
87- # pylint: disable=redefined-outer-name
88- if frozen == "macosx_app" :
89- codePath = environ .get ("RESOURCEPATH" )
90- elif frozen : # windows
91- codePath = sys ._MEIPASS # pylint: disable=no-member,protected-access
92- else :
93- codePath = path .dirname (__file__ )
94- return codePath
83+ """Returns path to the program sources"""
84+ if not frozen :
85+ return os .path .dirname (__file__ )
86+ return (
87+ os .environ .get ('RESOURCEPATH' )
88+ if frozen == "macosx_app" else sys ._MEIPASS )
9589
9690
9791def tail (f , lines = 20 ):
98- """Read last lines of a file. Like tail(1) """
92+ """Returns last lines in the f file object """
9993 total_lines_wanted = lines
10094
10195 BLOCK_SIZE = 1024
10296 f .seek (0 , 2 )
10397 block_end_byte = f .tell ()
10498 lines_to_go = total_lines_wanted
10599 block_number = - 1
106- blocks = []
107100 # blocks of size BLOCK_SIZE, in reverse order starting
108101 # from the end of the file
102+ blocks = []
109103 while lines_to_go > 0 and block_end_byte > 0 :
110104 if block_end_byte - BLOCK_SIZE > 0 :
111105 # read the last block we haven't yet read
@@ -125,10 +119,12 @@ def tail(f, lines=20):
125119
126120
127121def lastCommit ():
128- """Git commitish of the currently checked out repository"""
129- githeadfile = path .join (codePath (), '..' , '.git' , 'logs' , 'HEAD' )
122+ """
123+ Returns last commit information as dict with 'commit' and 'time' keys
124+ """
125+ githeadfile = os .path .join (codePath (), '..' , '.git' , 'logs' , 'HEAD' )
130126 result = {}
131- if path .isfile (githeadfile ):
127+ if os . path .isfile (githeadfile ):
132128 try :
133129 with open (githeadfile , 'rt' ) as githead :
134130 line = tail (githead , 1 )
0 commit comments