Completed
Push — master ( 02d3a1...898e86 )
by Andreas
34s
created

_load_test_suite()   A

Complexity

Conditions 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
c 1
b 0
f 0
dl 0
loc 4
rs 10
1
#
2
# tpmstore - TeamPasswordManager lookup plugin for Ansible.
3
# Copyright (C) 2017 Andreas Hubert
4
# See LICENSE.txt for licensing details
5
#
6
# File: setup.py
7
#
8
9
from __future__ import print_function;
10
11
try:
12
    from setuptools import setup;
13
except ImportError:
14
    from ez_setup import use_setuptools;
15
    use_setuptools();
16
17
from setuptools.command.install import install;
18
from setuptools.command.sdist import sdist;
19
from setuptools.command.develop import develop;
20
from setuptools import setup;
21
from codecs import open;
22
import traceback;
23
import os;
24
import sys;
25
import re;
26
import stat;
27
28
29
pkg_name = 'tpmstore';
30
pkg_ver = '0.1.1';
31
32
cmdclass = {};
33
34
35
def pre_build_toolkit():
36
    print("[INFO] checking whether 'ansible' python package is installed ...");
37
    ansible_dirs = _find_py_package('ansible');
38
    if len(ansible_dirs) == 0:
39
        print("[ERROR] 'ansible' python package was not found");
40
        return [];
41
    print("[INFO] the path to 'ansible' python package is: " + str(ansible_dirs));
42
    for ansible_dir in ansible_dirs:
43
        for suffix in ['.py', '.pyc']:
44
            plugin_type = 'plugins/lookup'
45
            plugin_file = os.path.join(ansible_dir, plugin_type , pkg_name + suffix);
46
            try:
47
                os.unlink(plugin_file);
48
            except:
49
                pass;
50
            try:
51
                os.remove(plugin_file);
52
            except:
53
                pass;
54
            if os.path.exists(plugin_file):
55
                print("[ERROR] 'ansible' python package contains traces '" + pkg_name + "' package ("+ plugin_file +"), failed to delete, aborting!");
56
            else:
57
                print("[INFO] 'ansible' python package contains traces '" + pkg_name + "' package ("+ plugin_file +"), deleted!");
58
    return ansible_dirs;
59
60
def _find_utility(name):
61
    x = any(os.access(os.path.join(path, name), os.X_OK) for path in os.environ["PATH"].split(os.pathsep));
62
    return x;
63
64
def _find_py_package(name):
65
    pkg_dirs = [];
66
    for path in sys.path:
67
        if not re.search('site-packages$', path):
68
            continue;
69
        if not os.path.exists(path):
70
            continue;
71
        if not os.path.isdir(path):
72
            continue
73
        target = os.path.join(path, name);
74
        if not os.path.exists(target):
75
            continue;
76
        if not os.path.isdir(target):
77
            continue;
78
        if target not in pkg_dirs:
79
            pkg_dirs.append(target);
80
    return pkg_dirs;
81
82
def _post_build_toolkit(ansible_dirs, plugin_dir=None):
83
    if plugin_dir is None:
84
        plugin_dirs = _find_py_package(pkg_name);
85
        if len(plugin_dirs) > 0:
86
            print("[INFO] the path to '" + pkg_name + "' python package is: " + str(plugin_dirs));
87
            for d in plugin_dirs:
88
                if re.search('bdist', d) or re.search('build', d):
89
                    continue;
90
                plugin_dir = d;
91
                break;
92
    if plugin_dir is None:
93
        print("[ERROR] failed to find '" + pkg_name + "' python package, aborting!");
94
        return;
95
    if re.search('bdist', plugin_dir) or re.search('build', plugin_dir):
96
        return;
97
    if re.search('site-packages.?$', plugin_dir):
98
        plugin_dir += pkg_name;
99
    print("[INFO] the path to '" + pkg_name + "' python package is: " + str(plugin_dir));
100
    '''
101
    Create a symlink, i.e. `ln -s TARGET LINK_NAME`
102
    '''
103
    _egg_files = [];
104
    for ansible_dir in ansible_dirs:
105
        symlink_target = os.path.join(plugin_dir, 'tpmstore.py');
106
        symlink_name = os.path.join(ansible_dir, 'plugins/lookup/tpmstore.py');
107
        try:
108
            os.symlink(symlink_target, symlink_name);
109
            os.chmod(symlink_name, stat.S_IRUSR | stat.S_IWUSR | stat.S_IRGRP | stat.S_IROTH);
110
            _egg_files.append(symlink_name);
111
            _egg_files.append(symlink_name + 'c');
112
            print("[INFO] created symlink '" + symlink_name + "' to plugin '" + symlink_target + "'");
113
        except:
114
            exc_type, exc_value, exc_traceback = sys.exc_info();
115
            print('[ERROR] an attempt to create a symlink ' + symlink_name + ' to plugin ' + symlink_target + ' failed, aborting!');
116
            print(traceback.format_exception(exc_type, exc_value, exc_traceback));
117
    return;
118
119
class install_(install):
120
    def run(self):
121
        ansible_dirs = pre_build_toolkit();
122
        if len(ansible_dirs) == 0:
123
            return 1;
124
        install.run(self);
125
        if len(ansible_dirs) > 0:
126
            self.execute(_post_build_toolkit, (ansible_dirs, self.install_lib, ), msg="running post_install_scripts");
127
128
cmdclass['install'] = install_;
129
130
class uninstall_(develop):
131
    def run(self):
132
        plugin_dirs = [];
133
        for dp in sys.path:
134
            if not re.search('site-packages$', dp):
135
                continue;
136
            ds = [name for name in os.listdir(dp) if os.path.isdir(os.path.join(dp, name))];
137
            if ds:
138
                for d in ds:
139
                    if not re.match(pkg_name, d):
140
                        continue;
141
                    if os.path.join(dp, d) not in plugin_dirs:
142
                        plugin_dirs.append(os.path.join(dp, d));
143
        if plugin_dirs:
144
            for dp in plugin_dirs:
145
                try:
146
                    for root, dirs, files in os.walk(dp, topdown=False):
147
                        for name in files:
148
                            if os.path.islink(os.path.join(root, name)):
149
                                os.unlink(os.path.join(root, name));
150
                            else:
151
                                os.remove(os.path.join(root, name));
152
                        for name in dirs:
153
                            os.rmdir(os.path.join(root, name));
154
                    os.rmdir(dp);
155
                    print("[INFO] deleted '" + dp + "'");
156
                except:
157
                    print("[INFO] failed to delete '" + dp + "'");
158
                    exc_type, exc_value, exc_traceback = sys.exc_info();
159
                    print(traceback.format_exception(exc_type, exc_value, exc_traceback));
160
        else:
161
            print("[INFO] no relevant files for the uninstall found, all clean");
162
163
        ansible_dirs = _find_py_package('ansible');
164
        if len(ansible_dirs) == 0:
165
            print("[ERROR] 'ansible' python package was not found");
166
            return;
167
        for ansible_dir in ansible_dirs:
168
            for suffix in ['.py', '.pyc']:
169
                plugin_type = 'plugins/lookup'
170
                plugin_file = os.path.join(ansible_dir, plugin_type , pkg_name + suffix);
171
                try:
172
                    os.unlink(plugin_file);
173
                except:
174
                    pass;
175
                try:
176
                    os.remove(plugin_file);
177
                except:
178
                    pass;
179
        return;
180
181
182
cmdclass['uninstall'] = uninstall_;
183
184
pkg_dir = os.path.abspath(os.path.dirname(__file__));
185
pkg_license='OSI Approved :: GNU General Public License v3 or later (GPLv3+)';
186
pkg_description = 'Lookup TeamPasswordManager from Ansible.';
187
pkg_url = 'https://github.com/peshay/' + pkg_name;
188
#pkg_download_url = 'http://pypi.python.org/packages/source/' + pkg_name[0] + '/' + pkg_name + '/' + pkg_name + '-' + pkg_ver + '.tar.gz';
189
pkg_download_url = 'https://github.com/peshay/tpmstore/archive/master.zip';
190
pkg_author = 'Andreas Hubert';
191
pkg_author_email = '[email protected]';
192
pkg_packages = [pkg_name.lower()];
193
pkg_requires = ['ansible>=2.0', 'tpm'];
194
pkg_data=[
195
    'plugins/lookup/*.py',
196
    'README',
197
    'LICENSE.txt',
198
];
199
pkg_platforms='any';
200
pkg_classifiers=[
201
    'Development Status :: 5 - Production/Stable',
202
    'Environment :: Console',
203
    'Intended Audience :: Information Technology',
204
    'Intended Audience :: System Administrators',
205
    'License :: ' + pkg_license,
206
    'Programming Language :: Python',
207
    'Operating System :: POSIX :: Linux',
208
    'Topic :: Utilities',
209
    'Topic :: System :: Systems Administration',
210
    'Programming Language :: Python :: 2',
211
    'Programming Language :: Python :: 2.7',
212
    'Programming Language :: Python :: 3',
213
    'Programming Language :: Python :: 3.3',
214
    'Programming Language :: Python :: 3.4',
215
    'Programming Language :: Python :: 3.5',
216
    'Programming Language :: Python :: 3.6'
217
];
218
pkg_keywords=[
219
    'ansible',
220
    'ansible plugin',
221
    'console',
222
    'automation',
223
];
224
225
pkg_long_description=pkg_description;
226
with open(os.path.join(pkg_dir, pkg_name, 'README'), encoding='utf-8') as f:
227
    pkg_long_description = f.read();
228
229
setup(
230
    name=pkg_name,
231
    version=pkg_ver,
232
    description=pkg_description,
233
    long_description=pkg_long_description,
234
    url=pkg_url,
235
    download_url=pkg_download_url,
236
    author=pkg_author,
237
    author_email=pkg_author_email,
238
    license=pkg_license,
239
    platforms=pkg_platforms,
240
    classifiers=pkg_classifiers,
241
    packages=pkg_packages,
242
    package_data= {
243
        pkg_name.lower() : pkg_data,
244
    },
245
    keywords=pkg_keywords,
246
    install_requires=pkg_requires,
247
    cmdclass=cmdclass
248
);
249