Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Parse environment markers when auto installing. #889

Merged
merged 1 commit into from
May 3, 2019
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 28 additions & 2 deletions mbed/mbed.py
Original file line number Diff line number Diff line change
Expand Up @@ -1002,6 +1002,26 @@ def action_progress(line, sep):
if m.group(1) == "Checking out files":
show_progress('Checking out', (float(m.group(3)) / float(m.group(4))) * 100)

_environment_markers = {
"platform_system": platform.system()
}

_comparators = {
"!=": lambda a, b: a != b,
"==": lambda a, b: a == b,
}

def _eval_environment_marker(marker):
m = re.match(r'\s*([^!=]+)\s*([!=]+)\s*([^\s]*)', marker)
if m:
try:
environment_marker_value = _environment_markers[m.group(1)]
environment_marker_cmp = m.group(3).strip("\"'")
return _comparators[m.group(2)](environment_marker_value, environment_marker_cmp)
except KeyError as e:
pass

raise Exception("Unsupported environment marker: {}".format(marker))

# Repository object
class Repo(object):
Expand Down Expand Up @@ -1652,8 +1672,14 @@ def check_requirements(self, require_install=False):
pkg = re.sub(r'-', '_', re.sub(r'^([^<>=@]+).*$', r'\1', line).lower())
pkg = re.sub(r'^(git|hg|svn|bzr)\+(.*)/([\w.-]+?)(\.(git|hg))?$', r'\3', pkg)
pkg = re.sub(r'\[([\w]+)\]', '', pkg)
if not pkg in installed_packages:
missing.append(pkg)
line_parts = line.split(";")

for line_part in line_parts[1:]:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why was the 1: needed?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A python requirements file looks like this:

pyusb>=1.0.0,<2.0.0
hidapi>=0.7.99,<0.8.0;platform_system!="Linux"

line_parts for the first line would look like: ["pyusb>=1.0.0,<2.0.0"].
For the second one, it would look like ["hidapi>=0.7.99,<0.8.0", "platform_system!=\"Linux\""].
So the [1:] only selects what comes after the first ;, if there is one, which is the environment markers that we're trying to parse.

if not _eval_environment_marker(line_part):
break
else:
if not pkg in installed_packages:
missing.append(pkg)

if missing and install_requirements and require_install:
try:
Expand Down