Skip to content

Commit 131c074

Browse files
Merge pull request #481 from adobe-apiplatform/feature/resource-manager
Feature - Resource Manager
2 parents 3ddb456 + 0bcf603 commit 131c074

5 files changed

Lines changed: 246 additions & 0 deletions

File tree

.build/pre_build.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
if __name__ == '__main__':
2+
pass

tests/test_resource.py

Lines changed: 157 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,157 @@
1+
# Copyright (c) 2016-2017 Adobe Inc. All rights reserved.
2+
#
3+
# Permission is hereby granted, free of charge, to any person obtaining a copy
4+
# of this software and associated documentation files (the "Software"), to deal
5+
# in the Software without restriction, including without limitation the rights
6+
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7+
# copies of the Software, and to permit persons to whom the Software is
8+
# furnished to do so, subject to the following conditions:
9+
#
10+
# The above copyright notice and this permission notice shall be included in all
11+
# copies or substantial portions of the Software.
12+
#
13+
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14+
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15+
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16+
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17+
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18+
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
19+
# SOFTWARE.
20+
21+
import os
22+
import sys
23+
import pytest
24+
import pkg_resources
25+
from user_sync import resource
26+
27+
28+
@pytest.fixture
29+
def resource_file():
30+
"""
31+
Create an empty resource file
32+
:return:
33+
"""
34+
def _resource_file(dirname, filename):
35+
filepath = os.path.join(dirname, filename)
36+
open(filepath, 'a').close()
37+
return filepath
38+
return _resource_file
39+
40+
41+
def test_resource_file_bundle(resource_file, tmpdir, monkeypatch):
42+
"""test for valid resource file in an EXE bundle"""
43+
tmpdir = str(tmpdir)
44+
with monkeypatch.context() as m:
45+
m.setattr(resource, '_run_context', None, False)
46+
m.setattr(sys, 'frozen', True, False)
47+
m.setattr(sys, '_MEIPASS', tmpdir, False)
48+
rootdir = os.path.join(sys._MEIPASS, resource._BUNDLE_DIR)
49+
os.mkdir(rootdir)
50+
51+
resfile = "test.txt"
52+
assert resource_file(rootdir, resfile) == resource.get_resource(resfile)
53+
54+
55+
def test_resource_file_package(resource_file, tmpdir, monkeypatch):
56+
"""test for valid resource file in a Package context"""
57+
tmpdir = str(tmpdir)
58+
with monkeypatch.context() as m:
59+
resfile = "test.txt"
60+
m.setattr(pkg_resources, "resource_filename", lambda *args: os.path.join(tmpdir, resfile))
61+
assert resource_file(tmpdir, resfile) == resource.get_resource(resfile)
62+
63+
64+
def test_resource_invalid_file_bundle(tmpdir, monkeypatch):
65+
"""test for non-existent resource file in a bundle"""
66+
tmpdir = str(tmpdir)
67+
with monkeypatch.context() as m:
68+
m.setattr(resource, '_run_context', None, False)
69+
m.setattr(sys, 'frozen', True, False)
70+
m.setattr(sys, '_MEIPASS', tmpdir, False)
71+
72+
rootdir = os.path.join(sys._MEIPASS, resource._BUNDLE_DIR)
73+
os.mkdir(rootdir)
74+
75+
resfile = "test.txt"
76+
assert resource.get_resource(resfile) is None
77+
78+
79+
def test_resource_invalid_file_package(tmpdir, monkeypatch):
80+
"""test for non-existent resource file in a package"""
81+
tmpdir = str(tmpdir)
82+
with monkeypatch.context() as m:
83+
m.setattr(pkg_resources, "resource_filename", lambda *args: os.path.join('invalid', 'file', 'path'))
84+
resfile = os.path.join('invalid', 'file', 'path')
85+
assert resource.get_resource(resfile) is None
86+
87+
88+
def test_resource_dir_bundle(resource_file, tmpdir, monkeypatch):
89+
"""test for valid resource files in directory in standalone run context"""
90+
tmpdir = str(tmpdir)
91+
with monkeypatch.context() as m:
92+
m.setattr(resource, '_run_context', None, False)
93+
m.setattr(sys, 'frozen', True, False)
94+
m.setattr(sys, '_MEIPASS', tmpdir, False)
95+
96+
rootdir = os.path.join(sys._MEIPASS, resource._BUNDLE_DIR)
97+
os.mkdir(rootdir)
98+
99+
test_dir = os.path.join(rootdir, "test")
100+
os.mkdir(test_dir)
101+
102+
resfile = "test_{}.txt"
103+
104+
res_paths = [resource_file(test_dir, resfile.format(n+1)) for n in range(3)]
105+
106+
assert sorted(res_paths) == sorted(resource.get_resource_dir('test'))
107+
108+
109+
def test_resource_dir_package(resource_file, tmpdir, monkeypatch):
110+
"""test for valid resource files in directory in package run context"""
111+
tmpdir = str(tmpdir)
112+
with monkeypatch.context() as m:
113+
test_dir = os.path.join(tmpdir, "test")
114+
os.mkdir(test_dir)
115+
116+
m.setattr(pkg_resources, "resource_filename", lambda *args: os.path.join(tmpdir, test_dir))
117+
118+
resfile = "test_{}.txt"
119+
120+
res_test_files = [resfile.format(n+1) for n in range(3)]
121+
122+
m.setattr(pkg_resources, "resource_listdir", lambda *args: res_test_files)
123+
124+
res_paths = [resource_file(test_dir, resfile.format(n+1)) for n in range(3)]
125+
126+
assert sorted(res_paths) == sorted(resource.get_resource_dir('test'))
127+
128+
129+
def test_resource_dir_empty(tmpdir, monkeypatch):
130+
"""test for empty resource directory"""
131+
tmpdir = str(tmpdir)
132+
with monkeypatch.context() as m:
133+
m.setattr(resource, '_run_context', None, False)
134+
m.setattr(sys, 'frozen', True, False)
135+
m.setattr(sys, '_MEIPASS', tmpdir, False)
136+
137+
rootdir = os.path.join(sys._MEIPASS, resource._BUNDLE_DIR)
138+
os.mkdir(rootdir)
139+
140+
test_dir = os.path.join(rootdir, "test")
141+
os.mkdir(test_dir)
142+
143+
assert [] == resource.get_resource_dir('test')
144+
145+
146+
def test_resource_dir_invalid(tmpdir, monkeypatch):
147+
"""test for nonexistent resource directory"""
148+
tmpdir = str(tmpdir)
149+
with monkeypatch.context() as m:
150+
m.setattr(resource, '_run_context', None, False)
151+
m.setattr(sys, 'frozen', True, False)
152+
m.setattr(sys, '_MEIPASS', tmpdir, False)
153+
rootdir = os.path.join(sys._MEIPASS, resource._BUNDLE_DIR)
154+
os.mkdir(rootdir)
155+
156+
with pytest.raises(AssertionError):
157+
resource.get_resource_dir('test')

user_sync/resource.py

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
# Copyright (c) 2016-2017 Adobe Inc. All rights reserved.
2+
#
3+
# Permission is hereby granted, free of charge, to any person obtaining a copy
4+
# of this software and associated documentation files (the "Software"), to deal
5+
# in the Software without restriction, including without limitation the rights
6+
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7+
# copies of the Software, and to permit persons to whom the Software is
8+
# furnished to do so, subject to the following conditions:
9+
#
10+
# The above copyright notice and this permission notice shall be included in all
11+
# copies or substantial portions of the Software.
12+
#
13+
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14+
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15+
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16+
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17+
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18+
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
19+
# SOFTWARE.
20+
21+
import os
22+
import sys
23+
import enum
24+
import pkg_resources
25+
26+
_BUNDLE_DIR = "resources"
27+
28+
_PKG = "user_sync.resources"
29+
30+
_run_context = None
31+
32+
33+
class RunContext(enum.Enum):
34+
EXEBundle = 'exe'
35+
Package = 'package'
36+
37+
38+
def get_run_context():
39+
if getattr(sys, 'frozen', False):
40+
return RunContext.EXEBundle
41+
return RunContext.Package
42+
43+
44+
def get_resource(resource):
45+
"""
46+
Get a path to a single resource file
47+
:param str resource: Relative resource file path (relative to resource root directory)
48+
:return str: Absolute path to resource file or None if no resource was found
49+
"""
50+
global _run_context
51+
if _run_context is None:
52+
_run_context = get_run_context()
53+
54+
if _run_context == RunContext.EXEBundle:
55+
assert getattr(sys, '_MEIPASS', False), "Bundle root dir is not set"
56+
resource_path = os.path.join(getattr(sys, '_MEIPASS'), "resources", resource)
57+
else:
58+
resource_path = pkg_resources.resource_filename(_PKG, resource)
59+
if os.path.exists(resource_path) and os.path.isfile(resource_path):
60+
return resource_path
61+
return None
62+
63+
64+
def get_resource_dir(resource_dir):
65+
"""
66+
Get list of paths to all resource files for given directory.
67+
68+
Only files directly contained in directory will be returned. Recursion is not currently supported.
69+
:param str resource_dir: Relative path of directory (relative to resource root directory)
70+
:return list(str): List of resource files in directory or None if directory was not found
71+
"""
72+
global _run_context
73+
if _run_context is None:
74+
_run_context = get_run_context()
75+
76+
if _run_context == RunContext.EXEBundle:
77+
assert getattr(sys, '_MEIPASS', False)
78+
resource_path = os.path.join(getattr(sys, '_MEIPASS'), "resources", resource_dir)
79+
assert os.path.isdir(resource_path), "Resource directory does not exist"
80+
81+
return [os.path.join(resource_path, f) for f in os.listdir(resource_path)
82+
if os.path.isfile(os.path.join(resource_path, f))]
83+
else:
84+
resource_path = pkg_resources.resource_filename(_PKG, resource_dir)
85+
return [os.path.join(resource_path, f) for f in pkg_resources.resource_listdir(_PKG, resource_dir)
86+
if os.path.isfile(os.path.join(resource_path, f))]

user_sync/resources/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Resource files go here (usually at build time).

user_sync/resources/__init__.py

Whitespace-only changes.

0 commit comments

Comments
 (0)