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

Replace imp #260

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
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
10 changes: 10 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
[build-system]
requires = [
"setuptools>=42",
"wheel",
"setuptools_scm"
]
build-backend = "setuptools.build_meta"

[tool.setuptools_scm]
write_to = "_version.py"
34 changes: 11 additions & 23 deletions setup.py
Original file line number Diff line number Diff line change
@@ -1,26 +1,13 @@
#!/usr/bin/env python
import os
import sys
import re
from distutils.core import setup
from setuptools import find_packages
from setuptools import setup, find_namespace_packages

def get_long_description():
path = os.path.join(os.path.dirname(__file__), 'README.md')
with open(path) as f:
return f.read()

def get_version():
return open('src/ansiblecmdb/data/VERSION', 'r').read().strip()

def get_data_files(path, strip='', prefix=''):
data_files = []
for dirpath, dirnames, filenames in os.walk(path):
files = [os.path.join(dirpath, filename) for filename in filenames]
data_files.append( [prefix + dirpath[len(strip):], files] )
return data_files


if sys.argv[-1] == 'publish':
os.system('python setup.py sdist upload')
print('You should also add a git tag for this version:')
Expand All @@ -30,7 +17,8 @@ def get_data_files(path, strip='', prefix=''):

setup(
name='ansible-cmdb',
version=get_version(),
use_scm_version=True,
setup_requires=['setuptools_scm'],
license='GPLv3',
description='Generate host overview from ansible fact gathering output',
long_description=get_long_description(),
Expand All @@ -40,19 +28,19 @@ def get_data_files(path, strip='', prefix=''):
author_email='[email protected]',

package_dir={'': 'src'},
packages=find_packages('src'),
packages=find_namespace_packages('src'),
package_data={
'ansiblecmdb.data': ['*.*'],
'ansiblecmdb.data.static.images': ['*.*'],
'ansiblecmdb.data.static.js': ['*.*'],
'ansiblecmdb.data.tpl': ['*.*']
},
include_package_data=True,
data_files=\
get_data_files(
'src/ansiblecmdb/data',
strip='src',
prefix='lib'
) +
[['lib/ansiblecmdb/', ['src/ansible-cmdb.py']]],
zip_safe=False,
install_requires=['mako', 'pyyaml', 'ushlex', 'jsonxs'],
scripts=[
'src/ansible-cmdb',
'src/ansible-cmdb.py'
],

classifiers=[
Expand Down
7 changes: 6 additions & 1 deletion src/ansible-cmdb.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,11 @@
import ansiblecmdb
import ansiblecmdb.util as util
import ansiblecmdb.render as render
try:
from importlib.metadata import version
except ImportError:
# Backport for Python < 3.8
from importlib_metadata import version


# Verify Python version
Expand Down Expand Up @@ -145,7 +150,7 @@ def parse_user_params(user_params):
data_dir = get_data_dir()
tpl_dir = os.path.join(data_dir, 'tpl')
static_dir = os.path.join(data_dir, 'static')
version = open(os.path.join(data_dir, 'VERSION')).read().strip()
version = version("ansible-cmdb")
Copy link

@mgedmin mgedmin Sep 10, 2024

Choose a reason for hiding this comment

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

This doesn't work:

UnboundLocalError: cannot access local variable 'version' where it is not associated with a value

You need to give different names to the imported global function and the local variable.

This works: mgedmin@53b5c30


parser = optparse.OptionParser(version="%prog v{0}".format(version))
parser.set_usage(os.path.basename(sys.argv[0]) + " [option] <dir> > output.html")
Expand Down
1 change: 0 additions & 1 deletion src/ansiblecmdb/data/VERSION

This file was deleted.

12 changes: 10 additions & 2 deletions src/ansiblecmdb/render.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import os
import imp
import importlib.util
import importlib.machinery
from mako.template import Template
from mako.lookup import TemplateLookup

Expand Down Expand Up @@ -66,6 +67,13 @@ def _render_mako(self, hosts, vars={}):
output_encoding='utf-8')
return template.render(hosts=hosts, **vars)

def load_source(self, modname, filename):
loader = importlib.machinery.SourceFileLoader(modname, filename)
spec = importlib.util.spec_from_file_location(modname, filename, loader=loader)
module = importlib.util.module_from_spec(spec)
loader.exec_module(module)
return module

def _render_py(self, hosts, vars={}):
module = imp.load_source('r', self.tpl_file)
module = self.load_source('r', self.tpl_file)
return module.render(hosts, vars=vars, tpl_dirs=self.tpl_dirs)