Skip to content

Commit

Permalink
fix(api): Write .env with absolute path for conf on make install
Browse files Browse the repository at this point in the history
The old checked-in .env had a relative path into tests/ for the development
index.json file that drives settings. This makes it impossible to import
opentrons from outside the api directory. One way to fix this would be to add correct, sane, and not duplicated
logic to the way we load settings; another is to write the .env and the
index.json with absolute paths when you run make install.

Also, this fixes an issue where ‘make local-install’ worked on appveyor (because
appveyor uses mingw make) but doesn’t work on other dev setups that install make
using e.g. chocolatey into the windows shell or powershell, which do not do
wildcard expansion.

Closes #2495
  • Loading branch information
sfoster1 committed Oct 18, 2018
1 parent ddd5454 commit fe9a1be
Show file tree
Hide file tree
Showing 6 changed files with 71 additions and 31 deletions.
5 changes: 5 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@ venv/
env/
.env/

# Generated files for local API server config paths
api/.env
api/index.json

# MyPy caches
.mypy_cache

Expand Down Expand Up @@ -78,6 +82,7 @@ target/

# Testing
api/tests/opentrons/data/configs/
api/tests/opentrons/data/labware-def/
api/opentrons/config/*.json
sample_protocol.py
api/opentrons/server/endpoints/ignore.json
Expand Down
1 change: 0 additions & 1 deletion api/.env

This file was deleted.

3 changes: 2 additions & 1 deletion api/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ pipenv_opts += $(and $(CI),--keep-outdated)
# Find the version of the wheel from package.json using a helper script. We
# use python here so we can use the same version normalization that will be
# used to create the wheel.
wheel_file = dist/opentrons-$(shell $(python) normalized_version.py)-py2.py3-none-any.whl
wheel_file = dist/opentrons-$(shell $(python) build_tools.py normalize_version)-py2.py3-none-any.whl
wheel_pattern := dist/opentrons-%-py2.py3-none-any.whl

# These variables can be overriden when make is invoked to customize the
Expand All @@ -47,6 +47,7 @@ clean_cmd = shx rm -rf build dist .coverage coverage.xml '*.egg-info' '**/__pyca
.PHONY: install
install:
pipenv sync $(pipenv_opts)
@$(python) build_tools.py write_local_env

.PHONY: all
all: lint test
Expand Down
64 changes: 64 additions & 0 deletions api/build_tools.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
""" Tools to bridge the Makefile and the python build environment
(or provide utilities)
"""

import argparse
import json
import os

# Pipenv requires setuptools >= 36.2.1. Since 36.2.1, setuptools changed
# the way they vendor dependencies, like the packaging module that
# provides the way to normalize version numbers for wheel file names. So
# we try all the possible ways to find it.
try:
# new way
from setuptools.extern import packaging
except ImportError:
# old way
from pkg_resources.extern import packaging


def normalize_version():
pkg_json_path = os.path.join('src', 'opentrons', 'package.json')
old_ver = json.load(open(pkg_json_path))['version']
vers_obj = packaging.version.Version(old_ver)
return str(vers_obj)


def write_local_env():

here = os.getcwd()

with open('./.env', 'w') as dotenv:
dotenv.write('OVERRIDE_SETTINGS_DIR={}'.format(here))

shared_data = os.path.join(here, os.pardir, 'shared-data')
test_data = os.path.join(here, 'tests', 'opentrons', 'data')
local_index = {
"labware": {
"baseDefinitionDir": os.path.join(shared_data, 'definitions'),
"userDefinitionDir": os.path.join(test_data, 'labware-def',
'definitions'),
"offsetDir": os.path.join(test_data, 'labware-def', 'offsets')
},
"pipetteConfigFile": os.path.join(shared_data, 'robot-data',
'pipette-config.json'),
"featureFlagFile": os.path.join(shared_data, 'settings.json'),
"deckCalibrationFile": os.path.join(test_data,
'configs',
'deckCalibration.json'),
"robotSettingsFile": os.path.join(test_data, 'configs',
'robotSettings.json')
}

with open('./index.json', 'w') as index:
index.write(json.dumps(local_index))


if __name__ == '__main__':
parser = argparse.ArgumentParser(
description='Perform one of several build-time tasks')
parser.add_argument(dest='task', metavar='TASK', type=str,
choices=['normalize_version', 'write_local_env'])
args = parser.parse_args()
print(locals()[args.task]())
18 changes: 0 additions & 18 deletions api/normalized_version.py

This file was deleted.

11 changes: 0 additions & 11 deletions api/tests/opentrons/data/index.json

This file was deleted.

0 comments on commit fe9a1be

Please sign in to comment.