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

Fix importing vendor lib during config creation. #350

Merged
merged 4 commits into from
May 25, 2017

Conversation

stealthycoin
Copy link
Contributor

Adds the vendor directory to the path when loading the app locally. This
is required for both deployment and local testing since both commands
load the app.py file, which will fail if anything is imported from the
vendor directory without it being added to the pythonpath. fixes #349

I also manually tested chalice deploy and chalice local

cc @jamesls @kyleknap

@codecov-io
Copy link

codecov-io commented May 23, 2017

Codecov Report

Merging #350 into master will increase coverage by 0.01%.
The diff coverage is 100%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master     #350      +/-   ##
==========================================
+ Coverage   90.14%   90.15%   +0.01%     
==========================================
  Files          18       18              
  Lines        2039     2042       +3     
  Branches      252      253       +1     
==========================================
+ Hits         1838     1841       +3     
  Misses        147      147              
  Partials       54       54
Impacted Files Coverage Δ
chalice/cli/factory.py 86.73% <100%> (+0.41%) ⬆️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 63fad06...a9f3411. Read the comment docs.

# For loading the config locally we must add the vendor directory to
# the path so it will be treated the same as if it were running on
# lambda.
vendor_dir = os.path.join(self.project_dir, 'vendor')
Copy link
Member

Choose a reason for hiding this comment

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

You should use the same logic as above and ensure we don't add this multiple times.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Updated.

data = f.read()
f.seek(0, 0)
f.write('from vendedlib.utils import data_getter\n%s' % data)
config = clifactory.create_config_obj()
Copy link
Member

Choose a reason for hiding this comment

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

Is this right? I would expect us to try to do something with data_getter and it's return value.

Copy link
Member

@jamesls jamesls left a comment

Choose a reason for hiding this comment

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

Looks good, just had a suggestion about updating the test.

data = f.read()
f.seek(0, 0)
f.write('from vendedlib import submodule\n%s' % data)
config = clifactory.create_config_obj()
Copy link
Member

Choose a reason for hiding this comment

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

So after looking at this again, I'd still prefer the test to be explicit. This is implicitly relying on the fact that if we can't import the module then this code will propagate some exception in create_config_obj. I'd rather be more explicit, ensure that not only do we not raise an exception but we are in fact able to access the thing we're importing. As an example:

# submodule.py:

SOME_VALUE = 'from-submodule'

# app.py

from vendedlib import submodule
app = Chalice(app_name='foo')
app.from_submodule = submodule.SOME_VALUE

# test
assert app.from_submodule == 'from-submodule'

Or something along those lines. It's just more explicit about successfully importing the vendored submodule

with open(os.path.join(vendedlib_dir, 'submodule.py'), 'a') as f:
f.write('CONST = "foo bar"\n')
app_py = os.path.join(clifactory.project_dir, 'app.py')
with open(app_py, 'r+') as f:
Copy link
Member

Choose a reason for hiding this comment

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

This doesn't need to block merging, but wouldn't just be simpler to open the file at the end and append from vendedlib import submodule\napp.imported_value = submodule.CONST? Not really sure if the .seek(0, 0) and .seek(0, 1) make the test any clearer.

Copy link
Member

@jamesls jamesls left a comment

Choose a reason for hiding this comment

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

Looks good to me, small suggestion on test but feel free to merge.

@jamesls
Copy link
Member

jamesls commented May 24, 2017

Oh and don't forget to add an entry to the changelog, thanks.

jcarlyl added 3 commits May 24, 2017 17:10
Adds the vendor directory to the path when loading the app locally. This
is required for both deployment and local testing since both commands
load the app.py file, which will fail if anything is imported from the
vendor directory without it being added to the pythonpath. fixes aws#349
Also updated a test to make it a little more clear what it is testing
and what it is not.
@stealthycoin stealthycoin merged commit 60148ef into aws:master May 25, 2017
@stealthycoin stealthycoin deleted the fix-vendor-deploy branch May 25, 2017 00:23
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Custom Package in vendor folder not recognized
3 participants