Skip to content

Commit

Permalink
fix: skip deps that do not match the current environment
Browse files Browse the repository at this point in the history
Note: sys_platform only for now.
  • Loading branch information
darscan committed Jun 26, 2017
1 parent d887e82 commit 482f75d
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 0 deletions.
17 changes: 17 additions & 0 deletions plug/pip_resolve.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import pip
import pkg_resources
import utils
import re


def create_tree_of_packages_dependencies(dist_tree, packages_names, req_file_path):
Expand Down Expand Up @@ -63,8 +64,24 @@ def create_package_as_root(package, dir_as_root):
dir_as_root[DEPENDENCIES][package_as_root[NAME]] = package_tree
return dir_as_root

sys_platform_re = re.compile('sys_platform\s*==\s*[\'"](.+)[\'"]')
sys_platform = sys.platform.lower()

def matches_environment(requirement):
"""Filter out requirements that should not be installed
in this environment. Only sys_platform is inspected right now.
This should be expanded to include other environment markers.
See: https://www.python.org/dev/peps/pep-0508/#environment-markers
"""
if 'sys_platform' in requirement.line:
match = sys_platform_re.findall(requirement.line)
if len(match) > 0:
return match[0].lower() == sys_platform
return True

def get_requirements_list(requirements_file):
req_list = list(requirements.parse(requirements_file))
req_list = filter(matches_environment, req_list)
required = [req.name.replace('_', '-') for req in req_list]
return required

Expand Down
26 changes: 26 additions & 0 deletions test/inspect.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,32 @@ test('package name differs from requirement', function (t) {
});
});

test('package depends on platform', function (t) {
chdirWorkspaces('pip-app-deps-conditional');
return subProcess.execute('pip',
['install', '-r', 'requirements.txt', '--disable-pip-version-check']
)
.then(function () {
return plugin.inspect('.', 'requirements.txt')
.then(function (result) {
var pkg = result.package;
t.notOk(pkg.dependencies.pypiwin32, 'win32 dep ignored');
t.same(pkg.dependencies['posix-ipc'], {
from: [
'pip-app-deps-conditional@0.0.0',
'posix-ipc@1.0.0',
],
name: 'posix-ipc',
version: '1.0.0',
}, 'posix-ipc looks ok');
t.end();
});
})
.catch(function (error) {
t.fail(error);
});
});

function chdirWorkspaces(dir) {
process.chdir(path.resolve(__dirname, 'workspaces', dir));
}
2 changes: 2 additions & 0 deletions test/workspaces/pip-app-deps-conditional/requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
pypiwin32==219; sys_platform == 'win32'
posix_ipc==1.0.0

0 comments on commit 482f75d

Please sign in to comment.