Package rekall :: Module resources
[frames] | no frames]

Source Code for Module rekall.resources

 1  #!/usr/bin/env python 
 2   
 3  # Rekall 
 4  # Copyright (C) 2012 Michael Cohen <scudette@gmail.com> 
 5  # Copyright 2013 Google Inc. All Rights Reserved. 
 6  # 
 7  # This program is free software; you can redistribute it and/or modify 
 8  # it under the terms of the GNU General Public License as published by 
 9  # the Free Software Foundation; either version 2 of the License, or (at 
10  # your option) any later version. 
11  # 
12  # This program is distributed in the hope that it will be useful, but 
13  # WITHOUT ANY WARRANTY; without even the implied warranty of 
14  # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 
15  # General Public License for more details. 
16  # 
17  # You should have received a copy of the GNU General Public License 
18  # along with this program; if not, write to the Free Software 
19  # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 
20  # 
21   
22  __author__ = "Michael Cohen <scudette@gmail.com>" 
23  """Finds resources which are normally bundled with Rekall. 
24   
25  Rekall is shipped both as a python package and as a pyinstaller 
26  application. This module is the interface between the two. 
27  """ 
28   
29  import os 
30  import sys 
31   
32  import pkg_resources 
33   
34   
35 -def get_resource(filename, package="rekall-core", prefix="resources"):
36 """Use the pkg_resources API to extract resources. 37 38 This will extract into a temporary file in case the egg is compressed. 39 40 Args: 41 package: name of the package (e.g. rekall-core, rekall-gui). 42 filename: The filename inside the package. 43 prefix: The sub-directory in the source distribution which contains the 44 resource. 45 46 Returns: 47 A path to the actual filename. 48 49 """ 50 target = _get_pkg_resource(filename, package, prefix) 51 if target and os.access(target, os.R_OK): 52 return target 53 54 # Installing from wheel places data_files relative to sys.prefix and not 55 # site-packages. If we can not find in site-packages, check sys.prefix 56 # instead. 57 # https://python-packaging-user-guide.readthedocs.io/en/latest/distributing/#data-files 58 target = os.path.join(sys.prefix, prefix, filename) 59 if target and os.access(target, os.R_OK): 60 return target 61 62 raise IOError("Unable to find resource %s" % filename)
63 64
65 -def _get_pkg_resource(filename, package, prefix):
66 """Query pkg_resources for the location of the filename.""" 67 requirement = pkg_resources.Requirement.parse(package) 68 target = os.path.join(prefix, filename) 69 try: 70 return pkg_resources.resource_filename(requirement, target) 71 except pkg_resources.DistributionNotFound: 72 # It may be that the working set is not in sync (e.g. if sys.path was 73 # manipulated). Try to reload it just in case. 74 pkg_resources.working_set = pkg_resources.WorkingSet() 75 try: 76 return pkg_resources.resource_filename(requirement, target) 77 except pkg_resources.DistributionNotFound: 78 return None
79