1- from os import environ , path
2- import sys
1+ import logging
2+ import os
33import re
4+ import sys
45from datetime import datetime
6+ from shutil import move
7+
8+
9+ logger = logging .getLogger ('default' )
510
611# When using py2exe or py2app, the variable frozen is added to the sys
7- # namespace. This can be used to setup a different code path for
12+ # namespace. This can be used to setup a different code path for
813# binary distributions vs source distributions.
9- frozen = getattr (sys ,'frozen' , None )
14+ frozen = getattr (sys , 'frozen' , None )
15+
1016
1117def lookupExeFolder ():
18+ """Returns executable folder path"""
1219 if frozen :
13- if frozen == "macosx_app" :
20+ exeFolder = (
1421 # targetdir/Bitmessage.app/Contents/MacOS/Bitmessage
15- exeFolder = path . dirname ( path .dirname (path . dirname ( path . dirname ( sys . executable )))) + path .sep
16- else :
17- exeFolder = path .dirname (sys .executable ) + path .sep
22+ os . path .dirname (sys . executable ). split ( os . path . sep )[ 0 ] + os . path .sep
23+ if frozen == "macosx_app" else
24+ os . path .dirname (sys .executable ) + os . path .sep )
1825 elif __file__ :
19- exeFolder = path .dirname (__file__ ) + path .sep
26+ exeFolder = os . path .dirname (__file__ ) + os . path .sep
2027 else :
2128 exeFolder = ''
2229 return exeFolder
2330
31+
2432def lookupAppdataFolder ():
33+ """Returns path of the folder where application data is stored"""
2534 APPNAME = "PyBitmessage"
26- if "BITMESSAGE_HOME" in environ :
27- dataFolder = environ [ "BITMESSAGE_HOME" ]
28- if dataFolder [- 1 ] not in [ path .sep , path .altsep ] :
29- dataFolder += path .sep
35+ dataFolder = os . environ . get ( 'BITMESSAGE_HOME' )
36+ if dataFolder :
37+ if dataFolder [- 1 ] not in ( os . path .sep , os . path .altsep ) :
38+ dataFolder += os . path .sep
3039 elif sys .platform == 'darwin' :
31- if "HOME" in environ :
32- dataFolder = path .join (environ ["HOME" ], "Library/Application Support/" , APPNAME ) + '/'
33- else :
34- stringToLog = 'Could not find home folder, please report this message and your OS X version to the BitMessage Github.'
35- if 'logger' in globals ():
36- logger .critical (stringToLog )
37- else :
38- print stringToLog
39- sys .exit ()
40-
40+ try :
41+ dataFolder = os .path .join (
42+ os .environ ['HOME' ],
43+ 'Library/Application Support/' , APPNAME
44+ ) + '/' # FIXME: should also be os.path.sep
45+ except KeyError :
46+ sys .exit (
47+ 'Could not find home folder, please report this message'
48+ ' and your OS X version to the BitMessage Github.' )
4149 elif 'win32' in sys .platform or 'win64' in sys .platform :
42- dataFolder = path .join (environ ['APPDATA' ].decode (sys .getfilesystemencoding (), 'ignore' ), APPNAME ) + path .sep
50+ dataFolder = os .path .join (
51+ os .environ ['APPDATA' ].decode (
52+ sys .getfilesystemencoding (), 'ignore' ), APPNAME
53+ ) + os .path .sep
4354 else :
44- from shutil import move
4555 try :
46- dataFolder = path .join (environ [" XDG_CONFIG_HOME" ], APPNAME )
56+ dataFolder = os . path .join (os . environ [' XDG_CONFIG_HOME' ], APPNAME )
4757 except KeyError :
48- dataFolder = path .join (environ [" HOME" ], " .config" , APPNAME )
58+ dataFolder = os . path .join (os . environ [' HOME' ], ' .config' , APPNAME )
4959
50- # Migrate existing data to the proper location if this is an existing install
60+ # Migrate existing data to the proper location
61+ # if this is an existing install
5162 try :
52- move (path .join (environ ["HOME" ], ".%s" % APPNAME ), dataFolder )
53- stringToLog = "Moving data folder to %s" % (dataFolder )
54- if 'logger' in globals ():
55- logger .info (stringToLog )
56- else :
57- print stringToLog
63+ move (os .path .join (os .environ ['HOME' ], '.%s' % APPNAME ), dataFolder )
64+ logger .info ('Moving data folder to %s' , dataFolder )
5865 except IOError :
5966 # Old directory may not exist.
6067 pass
61- dataFolder = dataFolder + '/'
68+ dataFolder = dataFolder + os . path . sep
6269 return dataFolder
63-
70+
71+
6472def codePath ():
65- if frozen == "macosx_app" :
66- codePath = environ . get ( "RESOURCEPATH" )
67- elif frozen : # windows
68- codePath = sys . _MEIPASS
69- else :
70- codePath = path . dirname ( __file__ )
71- return codePath
73+ """Returns path to the program sources"""
74+ if not frozen :
75+ return os . path . dirname ( __file__ )
76+ return (
77+ os . environ . get ( 'RESOURCEPATH' )
78+ if frozen == "macosx_app" else sys . _MEIPASS )
79+
7280
7381def tail (f , lines = 20 ):
82+ """Returns last lines in the f file object"""
7483 total_lines_wanted = lines
7584
7685 BLOCK_SIZE = 1024
7786 f .seek (0 , 2 )
7887 block_end_byte = f .tell ()
7988 lines_to_go = total_lines_wanted
8089 block_number = - 1
81- blocks = [] # blocks of size BLOCK_SIZE, in reverse order starting
82- # from the end of the file
90+ # blocks of size BLOCK_SIZE, in reverse order starting
91+ # from the end of the file
92+ blocks = []
8393 while lines_to_go > 0 and block_end_byte > 0 :
8494 if (block_end_byte - BLOCK_SIZE > 0 ):
8595 # read the last block we haven't yet read
86- f .seek (block_number * BLOCK_SIZE , 2 )
96+ f .seek (block_number * BLOCK_SIZE , 2 )
8797 blocks .append (f .read (BLOCK_SIZE ))
8898 else :
8999 # file too small, start from begining
90- f .seek (0 ,0 )
100+ f .seek (0 , 0 )
91101 # only read what was not read
92102 blocks .append (f .read (block_end_byte ))
93103 lines_found = blocks [- 1 ].count ('\n ' )
@@ -99,9 +109,12 @@ def tail(f, lines=20):
99109
100110
101111def lastCommit ():
102- githeadfile = path .join (codePath (), '..' , '.git' , 'logs' , 'HEAD' )
112+ """
113+ Returns last commit information as dict with 'commit' and 'time' keys
114+ """
115+ githeadfile = os .path .join (codePath (), '..' , '.git' , 'logs' , 'HEAD' )
103116 result = {}
104- if path .isfile (githeadfile ):
117+ if os . path .isfile (githeadfile ):
105118 try :
106119 with open (githeadfile , 'rt' ) as githead :
107120 line = tail (githead , 1 )
0 commit comments