1
|
|
|
# -*- coding: utf-8 -*- |
2
|
|
|
from __future__ import print_function, division, absolute_import |
3
|
|
|
from distutils.sysconfig import get_python_lib |
4
|
|
|
from logging import getLogger |
5
|
|
|
from os import chdir, getcwd |
6
|
|
|
from os.path import (abspath, dirname, exists, expanduser, expandvars, isdir, isfile, join, |
7
|
|
|
normpath, sep) |
8
|
|
|
import pkg_resources |
9
|
|
|
import sys |
10
|
|
|
|
11
|
|
|
log = getLogger(__name__) |
12
|
|
|
|
13
|
|
|
|
14
|
|
|
ROOT_PATH = abspath(sep) |
15
|
|
|
|
16
|
|
|
|
17
|
|
|
def site_packages_paths(): |
18
|
|
|
if hasattr(sys, 'real_prefix'): |
19
|
|
|
# in a virtualenv |
20
|
|
|
log.debug('searching virtualenv') |
21
|
|
|
return [p for p in sys.path if p.endswith('site-packages')] |
22
|
|
|
else: |
23
|
|
|
# not in a virtualenv |
24
|
|
|
log.debug('searching outside virtualenv') # pragma: no cover |
25
|
|
|
return get_python_lib() # pragma: no cover |
26
|
|
|
|
27
|
|
|
|
28
|
|
|
class PackageFile(object): |
29
|
|
|
|
30
|
|
|
def __init__(self, file_path, package_name): |
31
|
|
|
self.file_path = file_path |
32
|
|
|
self.package_name = package_name |
33
|
|
|
|
34
|
|
|
def __enter__(self): |
35
|
|
|
self.file_handle = open_package_file(self.file_path, self.package_name) |
36
|
|
|
return self.file_handle |
37
|
|
|
|
38
|
|
|
def __exit__(self, *args): |
39
|
|
|
self.file_handle.close() |
40
|
|
|
|
41
|
|
|
|
42
|
|
|
class ChangePath(object): |
43
|
|
|
|
44
|
|
|
def __init__(self, path): |
45
|
|
|
self.dirpath = dirname(path) if isfile(path) else path |
46
|
|
|
if not isdir(self.dirpath): |
47
|
|
|
raise IOError('File or directory not found: {0}'.format(path)) |
48
|
|
|
|
49
|
|
|
def __enter__(self): |
50
|
|
|
self.cwd = getcwd() |
51
|
|
|
chdir(self.dirpath) |
52
|
|
|
return self |
53
|
|
|
|
54
|
|
|
def __exit__(self, *args): |
55
|
|
|
chdir(self.cwd) |
56
|
|
|
|
57
|
|
|
|
58
|
|
|
def open_package_file(file_path, package_name): |
59
|
|
|
file_path = expand(file_path) |
60
|
|
|
|
61
|
|
|
# look for file at relative path |
62
|
|
|
if exists(file_path): |
63
|
|
|
log.info("found real file {0}".format(file_path)) |
64
|
|
|
return open(file_path) |
65
|
|
|
|
66
|
|
|
# look for file in package resources |
67
|
|
|
if package_name and pkg_resources.resource_exists(package_name, file_path): |
68
|
|
|
log.info("found package resource file {0} for package {1}".format(file_path, package_name)) |
69
|
|
|
return pkg_resources.resource_stream(package_name, file_path) |
70
|
|
|
|
71
|
|
|
# look for file in site-packages |
72
|
|
|
package_path = find_file_in_site_packages(file_path, package_name) |
73
|
|
|
if package_path: |
74
|
|
|
return open(package_path) # pragma: no cover |
75
|
|
|
|
76
|
|
|
msg = "file for module [{0}] cannot be found at path {1}".format(package_name, file_path) |
77
|
|
|
log.error(msg) |
78
|
|
|
raise IOError(msg) |
79
|
|
|
|
80
|
|
|
|
81
|
|
|
def find_file_in_site_packages(file_path, package_name): |
82
|
|
|
package_path = package_name.replace('.', '/') |
83
|
|
|
for site_packages_path in site_packages_paths(): |
84
|
|
|
test_path = join(site_packages_path, package_path, file_path) |
85
|
|
|
if exists(test_path): |
86
|
|
|
log.info("found site-package file {0} for package {1}".format(file_path, package_name)) |
87
|
|
|
return test_path |
88
|
|
|
else: |
89
|
|
|
log.error("No file found at {0}.".format(test_path)) |
90
|
|
|
return None |
91
|
|
|
|
92
|
|
|
|
93
|
|
|
def expand(path): |
94
|
|
|
return normpath(expanduser(expandvars(path))) |
95
|
|
|
|
96
|
|
|
|
97
|
|
|
def absdirname(path): |
98
|
|
|
return abspath(expanduser(dirname(path))) |
99
|
|
|
|