napalm-automation / napalm-base

Apache License 2.0
32 stars 48 forks source link

Is load_template code broke if an .egg file is installed #232

Closed ktbyers closed 7 years ago

ktbyers commented 7 years ago

I was having unit test failures in napalm-ios complaining about ./templates/ directory not existing.

This caused me to dig around some more:

I think is broke...you can't just try to load directly like this when it is an .egg file.

def load_template(cls, template_name, template_source=None, template_path=None,
                  openconfig=False, **template_vars):
    try:
        if isinstance(template_source, py23_compat.string_types):
            template = jinja2.Template(template_source)
        else:
            current_dir = os.path.dirname(os.path.abspath(sys.modules[cls.__module__].__file__))
            if (isinstance(template_path, py23_compat.string_types) and 
                    os.path.isdir(template_path) and os.path.isabs(template_path)):
                current_dir = os.path.join(template_path, cls.__module__.split('.')[-1])
                # append driver name at the end of the custom path

            if openconfig:
                template_dir_path = '{current_dir}/oc_templates'.format(current_dir=current_dir)
            else:
                template_dir_path = '{current_dir}/templates'.format(current_dir=current_dir)

            if not os.path.isdir(template_dir_path):
                raise napalm_base.exceptions.DriverTemplateNotImplemented(
                        '''Config template dir does not exist: {path}.
                        Please create it and add driver-specific templates.'''.format(
                            path=template_dir_path
                        )   
                    )   

See discussion about this here:

http://stackoverflow.com/questions/6028000/python-how-to-read-a-static-file-from-inside-a-package

ktbyers commented 7 years ago

More details, this is testing from napalm_ios package.

python setup.py install will fail as an .egg is installed. The templates/*.j2 files are inside the egg, but they can't be read.

I tried to use zip_safe=False in the setup.py file, but this did not change the behavior of python setup.py install. I could not find an answer on why this didn't work.

pip install napalm-ios worked as full package was installed (not clear to me, however, how deterministic this is...i.e. what here is causing it not to be an .egg file).

pip install -e . worked (thought it made an egg-link in site-packages pointing back to the developer directory. An egg-link is a Python version of a symbolic link). The load_template unit test passed with this.

pip install dist/napalm-ios-version.tar.gz also worked.

I also looked at the fix referenced inside of stackoverflow. That is going to be hard for us to implement given our current code structure.

So I think this is probably a non-issue...it means python setup.py install won't work, but we can just have them pip install. People doing the python setup.py install are more likely to be developers.

dbarrosop commented 7 years ago

I think is broke...you can't just try to load directly like this when it is an .egg file.

Yes, that's a known issue. I don't remember the details but @mirceaulinic and I looked into it and got to the same conclusion you got. That's why the only "supported" install method is pip (you can use pip install -e . to install from source like python setup.py install)

ktbyers commented 7 years ago

@dbarrosop It is probably prudent to add the following to setup.py.

zip_safe=False,

I think this will make it much less likely that a future version of PIP installs an egg (at least that was the best I could determine).