#!/usr/bin/env python import sys import os import os.path import errno SETUP_PY = """ ################################ # These variables are overwritten by Zenoss when the ZenPack is exported # or saved. Do not modify them directly here. # NB: PACKAGES is deprecated NAME = "{zenpack_name}" VERSION = "1.0.0dev" AUTHOR = "Zenoss Professional Services" LICENSE = "" NAMESPACE_PACKAGES = {namespace_packages} PACKAGES = {packages} INSTALL_REQUIRES = ['ZenPacks.zenoss.ZenPackLib>=2.0.0'] COMPAT_ZENOSS_VERS = "" PREV_ZENPACK_NAME = "" # STOP_REPLACEMENTS ################################ # Zenoss will not overwrite any changes you make below here. from setuptools import setup, find_packages import sys TEST_DEPENDENCY_LINKS = [] if 'test' in sys.argv: TEST_DEPENDENCY_LINKS = [ "https://github.com/zenoss/ZenPacks.zenoss.ZenPackLib/tarball/master#egg=ZenPacks.zenoss.ZenPackLib-2.0", ] setup( # This ZenPack metadata should usually be edited with the Zenoss # ZenPack edit page. Whenever the edit page is submitted it will # overwrite the values below (the ones it knows about) with new values. name=NAME, version=VERSION, author=AUTHOR, license=LICENSE, # This is the version spec which indicates what versions of Zenoss # this ZenPack is compatible with compatZenossVers=COMPAT_ZENOSS_VERS, # previousZenPackName is a facility for telling Zenoss that the name # of this ZenPack has changed. If no ZenPack with the current name is # installed then a zenpack of this name if installed will be upgraded. prevZenPackName=PREV_ZENPACK_NAME, # Indicate to setuptools which namespace packages the zenpack # participates in namespace_packages=NAMESPACE_PACKAGES, # Tell setuptools what packages this zenpack provides. packages=find_packages(), # Tell setuptools to figure out for itself which files to include # in the binary egg when it is built. include_package_data=True, # Indicate dependencies on other python modules or ZenPacks. This line # is modified by zenoss when the ZenPack edit page is submitted. Zenoss # tries to put add/delete the names it manages at the beginning of this # list, so any manual additions should be added to the end. Things will # go poorly if this line is broken into multiple lines or modified to # dramatically. install_requires=INSTALL_REQUIRES, # Every ZenPack egg must define exactly one zenoss.zenpacks entry point # of this form. entry_points={{ 'zenoss.zenpacks': '%s = %s' % (NAME, NAME), }}, # All ZenPack eggs must be installed in unzipped form. zip_safe=False, dependency_links=TEST_DEPENDENCY_LINKS, tests_require=INSTALL_REQUIRES, ) """.lstrip() TEST_SUITE_PY = """ import unittest import os def standalone_test_suite(): test_loader = unittest.TestLoader() testSuite = test_loader.discover('tests/standalone', pattern='test*.py') return testSuite def integration_test_suite(): test_loader = unittest.TestLoader() testSuite = test_loader.discover('tests/integration', pattern='test*.py') return testSuite """.lstrip() TEST_YAML_PY = """ import unittest from {zenpack_name} import CFG class Test(unittest.TestCase): def testYamlName(self): self.assertEquals('{zenpack_name}', CFG.name) """.lstrip() def create_zenpack(zenpack_name): zenpack_name_parts = zenpack_name.split('.') packages = reduce( lambda x, y: x + ['.'.join((x[-1], y))], zenpack_name_parts[1:], ['ZenPacks']) namespace_packages = packages[:-1] # Create ZenPacks.example.Thing/ZenPacks/example/Thing directory. module_directory = os.path.join(zenpack_name, *zenpack_name_parts) sub_module_directory = os.path.join(*zenpack_name_parts) try: print " - making directory: {}".format(module_directory) os.makedirs(module_directory) except OSError as e: if e.errno == errno.EEXIST: sys.exit("{} directory already exists.".format(zenpack_name)) else: sys.exit( "Failed to create {!r} directory: {}" .format(zenpack_name, e.strerror)) # Create setup.py. setup_py_fname = os.path.join(zenpack_name, 'setup.py') print " - creating file: {}".format(setup_py_fname) with open(setup_py_fname, 'w') as setup_py_f: setup_py_f.write( SETUP_PY.format( zenpack_name=zenpack_name, namespace_packages=namespace_packages, packages=packages, sub_module_directory=sub_module_directory,)) # Create tests/standalone tests/integration directories tests_dnames = ('tests', 'tests/standalone', 'tests/integration') for tests_dname in tests_dnames: try: fptests_dname = os.path.join(zenpack_name, tests_dname) print " - making directory: {}".format(fptests_dname) os.makedirs(fptests_dname) init_py_fname = os.path.join(fptests_dname, '__init__.py') print " - making init file: {}".format(init_py_fname) with open(init_py_fname, 'w') as init_py_f: if tests_dname == 'tests': init_py_f.write("__import__('pkg_resources').declare_namespace(__name__)") else: init_py_f.write("\n") except OSError as e: if e.errno == errno.EEXIST: sys.exit("{} directory already exists.".format(tests_dname)) else: sys.exit( "Failed to create {!r} directory: {}" .format(tests_dname, e.strerror)) # Create testsuite.py # TODO Add try testsuite_dname = os.path.join(zenpack_name, 'tests') testsuite_py_fname = os.path.join(testsuite_dname, 'testsuite.py') with open(testsuite_py_fname, 'w') as testsuite_py_f: testsuite_py_f.write(TEST_SUITE_PY) # Create initial testYaml.py # TODO Add try testyaml_dname = os.path.join(zenpack_name, 'tests/standalone') testyaml_py_fname = os.path.join(testyaml_dname, 'testYaml.py') with open(testyaml_py_fname, 'w') as testyaml_py_f: testyaml_py_f.write(TEST_YAML_PY.format(zenpack_name=zenpack_name)) # Create MANIFEST.in. manifest_in_fname = os.path.join(zenpack_name, 'MANIFEST.in') print " - creating file: {}".format(manifest_in_fname) with open(manifest_in_fname, 'w') as manifest_in_f: manifest_in_f.write("graft ZenPacks\n") # Create __init__.py files in all namespace directories. for namespace_package in namespace_packages: namespace_init_fname = os.path.join( zenpack_name, os.path.join(*namespace_package.split('.')), '__init__.py') print " - creating file: {}".format(namespace_init_fname) with open(namespace_init_fname, 'w') as namespace_init_f: namespace_init_f.write( "__import__('pkg_resources').declare_namespace(__name__)\n") # Create common subdirectories subdirs = ['modeler', 'modeler/plugins'] for subdir in subdirs: dirpath = os.path.join(module_directory, subdir) os.makedirs(dirpath) init_fname = os.path.join(dirpath, '__init__.py') print " - creating file: {}".format(init_fname) with open(init_fname, 'w') as init_f: init_f.write("\n") # Create __init__.py in ZenPack module directory. init_fname = os.path.join(module_directory, '__init__.py') print " - creating file: {}".format(init_fname) with open(init_fname, 'w') as init_f: init_f.write( "import os\n" "from ZenPacks.zenoss.ZenPackLib import zenpacklib\n\n" "CFG = zenpacklib.load_yaml(" "[os.path.join(os.path.dirname(__file__), \"zenpack.yaml\")]" ", verbose=False, level=30)\n" "schema = CFG.zenpack_module.schema\n") # Create zenpack.yaml in ZenPack module directory. yaml_fname = os.path.join(module_directory, 'zenpack.yaml') print " - creating file: {}".format(yaml_fname) with open(yaml_fname, 'w') as yaml_f: yaml_f.write("name: {}\n".format(zenpack_name)) # Create README.rst in ZenPack module directory readme_fname = os.path.join(module_directory, 'README.rst') print " - creating file: {}".format(readme_fname) with open(readme_fname, 'w') as readme_f: readme_f.write("Description\n" "-----------\n") readme_sym_link = os.path.join(zenpack_name, 'README.rst') readme_source = os.path.join(sub_module_directory, 'README.rst') os.symlink(readme_source, readme_sym_link) gitignore_fname = os.path.join(zenpack_name, '.gitignore') with open(gitignore_fname, 'w') as gitignore_f: gitignore_f.write("*.pyc\n" "*.pyo\n" "._*\n" ".*.swp\n" "*~\n" ".svn\n" ".idea\n" ".DS_Store\n" "build\n" "dist\n" "*.egg-info\n" "*.jar\n" "src/java/target\n" "test.py\n" "test\n" ".tox\n" ".project\n" ".pydevproject\n" "tests/__init__.py\n" ) if __name__ == '__main__': create_zenpack(sys.argv[1])