File: Resource.py

package info (click to toggle)
fofix-dfsg 3.121-2
  • links: PTS
  • area: main
  • in suites: squeeze
  • size: 27,016 kB
  • ctags: 4,131
  • sloc: python: 44,002; makefile: 99; perl: 42; sh: 39
file content (280 lines) | stat: -rwxr-xr-x 10,466 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
#####################################################################
# -*- coding: iso-8859-1 -*-                                        #
#                                                                   #
# Frets on Fire                                                     #
# Copyright (C) 2006 Sami Ky�stil�                                  #
#               2008 myfingershurt                                  #
#               2008 evilynux <evilynux@gmail.com>                  #
#                                                                   #
# This program is free software; you can redistribute it and/or     #
# modify it under the terms of the GNU General Public License       #
# as published by the Free Software Foundation; either version 2    #
# of the License, or (at your option) any later version.            #
#                                                                   #
# This program is distributed in the hope that it will be useful,   #
# but WITHOUT ANY WARRANTY; without even the implied warranty of    #
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the     #
# GNU General Public License for more details.                      #
#                                                                   #
# You should have received a copy of the GNU General Public License #
# along with this program; if not, write to the Free Software       #
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,        #
# MA  02110-1301, USA.                                              #
#####################################################################

import os
from Queue import Queue, Empty
from threading import Thread, BoundedSemaphore
import time
import shutil
import stat
import Config

from Task import Task
import Log
import Version

class Loader(Thread):
  def __init__(self, target, name, function, resultQueue, loaderSemaphore, onLoad = None, onCancel = None):
    Thread.__init__(self)
    self.semaphore   = loaderSemaphore
    self.target      = target
    self.name        = name
    self.function    = function
    self.resultQueue = resultQueue
    self.result      = None
    self.onLoad      = onLoad
    self.onCancel    = onCancel
    self.exception   = None
    self.time        = 0.0
    self.canceled    = False

    #myfingershurt: the following should be global and done ONCE:
    #self.game_priority = Config.get("performance", "game_priority")
    self.logLoadings = Config.get("game", "log_loadings")

    if target and name:
      setattr(target, name, None)

  def run(self):
    self.semaphore.acquire()
    game_priority = Config.get("performance", "game_priority")
    # Reduce priority on posix
    if os.name == "posix":
      # evilynux - Beware, os.nice _decreases_ priority, hence the reverse logic
      os.nice(5 - game_priority)
    elif os.name == "nt":
      #myfingershurt: the following should be global and done ONCE:
      #self.setPriority(priority = self.game_priority)

      #...or maybe not at all...
      self.setPriority(priority = game_priority)
      
      #wont work without ctypes
      #self.enableScreenSaver(0)

    self.load()
    self.semaphore.release()
    self.resultQueue.put(self)

  def __str__(self):
    return "%s(%s) %s" % (self.function.__name__, self.name, self.canceled and "(canceled)" or "")

  def setPriority(self, pid = None, priority = 2):
    """ Set The Priority of a Windows Process.  Priority is a value between 0-5 where
        2 is normal priority.  Default sets the priority of the current
        python process but can take any valid process ID. """
        
    import win32api, win32process, win32con
    
    priorityClasses = [win32process.IDLE_PRIORITY_CLASS,
                       win32process.BELOW_NORMAL_PRIORITY_CLASS,
                       win32process.NORMAL_PRIORITY_CLASS,
                       win32process.ABOVE_NORMAL_PRIORITY_CLASS,
                       win32process.HIGH_PRIORITY_CLASS,
                       win32process.REALTIME_PRIORITY_CLASS]

    pid = win32api.GetCurrentProcessId()
    handle = win32api.OpenProcess(win32con.PROCESS_ALL_ACCESS, True, pid)
    win32process.SetPriorityClass(handle, priorityClasses[priority])
    win32process.SetProcessAffinityMask(handle, 1)

#  def enableScreenSaver(self, on):
#    import ctypes
#    SPI_SETSCREENSAVEACTIVE = 17
#    ctypes.windll.user32.SystemParametersInfoA(SPI_SETSCREENSAVEACTIVE, on, None, 0)
    
  def cancel(self):
    self.canceled = True

  def load(self):
    try:
      start = time.time()
      self.result = self.function()
      self.time = time.time() - start
    except:
      import sys
      self.exception = sys.exc_info()

  def finish(self):
    if self.canceled:
      if self.onCancel:
        self.onCancel()
      return
    
    if self.logLoadings == 1:
      Log.notice("Loaded %s.%s in %.3f seconds" % (self.target.__class__.__name__, self.name, self.time))
    
    if self.exception:
      raise self.exception[0], self.exception[1], self.exception[2]
    if self.target and self.name:
      setattr(self.target, self.name, self.result)
    if self.onLoad:
      self.onLoad(self.result)
    return self.result

  def __call__(self):
    self.join()
    return self.result

class Resource(Task):
  def __init__(self, dataPath = os.path.join("..", "data")):
    self.resultQueue = Queue()
    self.dataPaths = [dataPath]
    self.loaderSemaphore = BoundedSemaphore(value = 1)
    self.loaders = []

    #myfingershurt: the following should be global, and only done at startup.  Not every damn time a file is loaded.
    self.songPath = []
    self.baseLibrary = Config.get("game", "base_library")
    #evilynux - Support for songs in ~/.fretsonfire/songs (GNU/Linux and MacOS X)
    if self.baseLibrary == "None" and os.name == "posix":
      path = os.path.expanduser("~/." + Version.appName())
      if os.path.isdir(path):
        self.baseLibrary = path
        Config.set("game", "base_library", path)

    if self.baseLibrary and os.path.isdir(self.baseLibrary):
      self.songPath = [self.baseLibrary]

    self.logLoadings = Config.get("game", "log_loadings")

  #myfingershurt: Need a function to refresh the base library after a new one is selected:
  def refreshBaseLib(self):
    self.baseLibrary = Config.get("game", "base_library")
    if self.baseLibrary and os.path.isdir(self.baseLibrary):
      self.songPath = [self.baseLibrary]
  
      

  def addDataPath(self, path):
    if not path in self.dataPaths:
      self.dataPaths = [path] + self.dataPaths

  def removeDataPath(self, path):
    if path in self.dataPaths:
      self.dataPaths.remove(path)

  def fileName(self, *name, **args):
    
    #myfingershurt: the following should be global, and only done at startup.  Not every damn time a file is loaded.
    #songPath = []
    #baseLibrary = Config.get("game", "base_library")
    #if baseLibrary and os.path.isdir(baseLibrary):
    #  songPath = [baseLibrary]
    baseLibrary = self.baseLibrary
    songPath = self.songPath
      
    if not args.get("writable", False):
      for dataPath in self.dataPaths + songPath:
        readOnlyPath = os.path.join(dataPath, *name)
        # If the requested file is in the read-write path and not in the
        # read-only path, use the existing read-write one.
        if os.path.isfile(readOnlyPath):
          return readOnlyPath
        elif os.path.isdir(readOnlyPath):
          return readOnlyPath
        readWritePath = os.path.join(getWritableResourcePath(), *name)
        if os.path.isfile(readWritePath):
          return readWritePath
      return readOnlyPath
    else:
      for dataPath in [self.dataPaths[-1]] + songPath:
        readOnlyPath = os.path.join(dataPath, *name)
        if not (os.path.isfile(readOnlyPath) or os.path.isdir(readOnlyPath)):
          continue
        try:
          # First see if we can write to the original file
          if os.access(readOnlyPath, os.W_OK):
            return readOnlyPath
          # If the original file does not exist, see if we can write to its directory
          if not os.path.isfile(readOnlyPath) and os.access(os.path.dirname(readOnlyPath), os.W_OK):
            pass
            #return readOnlyPath
        except:
          raise
        # If the resource exists in the read-only path, make a copy to the
        # read-write path.
        readWritePath = os.path.join(getWritableResourcePath(), *name)
        if not os.path.isfile(readWritePath) and os.path.isfile(readOnlyPath):
          Log.notice("Copying '%s' to writable data directory." % "/".join(name))
          try:
            os.makedirs(os.path.dirname(readWritePath))
          except:
            pass
          shutil.copy(readOnlyPath, readWritePath)
          self.makeWritable(readWritePath)
        # Create directories if needed
        if not os.path.isdir(readWritePath) and os.path.isdir(readOnlyPath):
          Log.notice("Creating writable directory '%s'." % "/".join(name))
          os.makedirs(readWritePath)
          self.makeWritable(readWritePath)
        return readWritePath
      return readOnlyPath
    
  def makeWritable(self, path):
    os.chmod(path, stat.S_IWRITE | stat.S_IREAD | stat.S_IEXEC)
  
  def load(self, target = None, name = None, function = lambda: None, synch = False, onLoad = None, onCancel = None):

    if self.logLoadings == 1:
      Log.notice("Loading %s.%s %s" % (target.__class__.__name__, name, synch and "synchronously" or "asynchronously"))

    l = Loader(target, name, function, self.resultQueue, self.loaderSemaphore, onLoad = onLoad, onCancel = onCancel)
    if synch:
      l.load()
      return l.finish()
    else:
      self.loaders.append(l)
      l.start()
      return l

  def run(self, ticks):
    try:
      loader = self.resultQueue.get_nowait()
      loader.finish()
      self.loaders.remove(loader)
    except Empty:
      pass

def getWritableResourcePath():
  """
  Returns a path that holds the configuration for the application.
  """
  path = "."
  appname = Version.appName()
  if os.name == "posix":
    path = os.path.expanduser("~/." + appname)
    # evilynux - MacOS X, putting config files in the standard folder
    if( os.uname()[0] == "Darwin" ):
      path = os.path.expanduser("~/Library/Preferences/" + appname)
  elif os.name == "nt":
    try:
      path = os.path.join(os.environ["APPDATA"], appname)
    except:
      pass
  try:
    os.mkdir(path)
  except:
    pass
  return path