mMecoPackage - Python API


You can check mMecoPackage API Reference

Creating a Package

You can use static mMecoPackage.packageLib.Package.create method of the Package class to create a new package. Entire folder structure of the package including additional folders and files will also be created.

import mMecoPackage.packageLib

package = mMecoPackage.packageLib.Package.create(name='testPackage',
                                                 description='This is a test package.',
                                                 path='<PATH>',
                                                 external=False)

print(package)

# NAME                : testPackage
# VERSION             : 1.0.0
# DESCRIPTION         : This is a test package.
# KEYWORDS            :
# PLATFORMS           : Linux, Darwin, Windows
# DOCUMENTS           : N/A
# APPLICATIONS        : all
# PYTHON_VERSIONS     : 2, 3
# IS_ACTIVE           : True
# IS_EXTERNAL         : False
# DEVELOPERS          : DEVELOPER_EMAIL_ADDRESS
# DEPENDENT_PACKAGES  :
# PYTHON_PACKAGES     : testPackage
# IS_VERSIONED        : False
# PATH                : <PATH>

Using Package Class in your Package

You can utilize the mMecoPackage.packageLib.Package class in any of Python modules of your package in order to operate on it. By doing so, you can obtain data about the package itself.

import mMecoPackage.packageLib

package = mMecoPackage.packageLib.Package(__file__)

package.path()

# /<PATH>/meco/master/developers/soner/development/main/mMecoPackage

Getting Information About a Package

from   pprint import pprint
import mMecoPackage.packageLib

package = mMecoPackage.packageLib.Package(__file__)

pprint(package.asDict())

# OrderedDict([('APPLICATIONS', ['all']),
#              ('DEPENDENT_PACKAGES',
#               ['mCore', 'mFileSystem', 'mMeco', 'mMecoSettings']),
#              ('DESCRIPTION', 'Operate on Meco packages'),
#              ('DEVELOPERS', ['developer@company.com']),
#              ('DOCUMENTS', []),
#              ('IS_ACTIVE', True),
#              ('IS_EXTERNAL', False),
#              ('IS_VERSIONED', False),
#              ('KEYWORDS', ['meco', 'package']),
#              ('NAME', 'mMecoPackage'),
#              ('PATH',
#               '/<PATH>/meco/master/developers/soner/development/main/mMecoPackage'),
#              ('PLATFORMS', ['Linux', 'Darwin', 'Windows']),
#              ('PYTHON_PACKAGES', ['mMecoPackage']),
#              ('PYTHON_VERSIONS', ['2', '3']),
#              ('VERSION', '1.0.0')])

pprint(package.asHTML())

# ('<head><style>span{margin-left:4px}</style><b>Applications   </b>: '
#  '<span>all</span><br><br><b>Dependent Packages</b>: <span>mCore, mFileSystem, '
#  'mMeco, mMecoSettings</span><br><br><b>Description    </b>: <span>Operate on '
#  'Meco packages</span><br><br><b>Developers     </b>: '
#  '<span>developer@company.com</span><br><br><b>Documents      </b>: '
#  '<span>N/A</span><br><br><b>Is Active      </b>: '
#  '<span>True</span><br><br><b>Is External    </b>: '
#  '<span>False</span><br><br><b>Is Versioned   </b>: '
#  '<span>False</span><br><br><b>Keywords       </b>: <span>meco, '
#  'package</span><br><br><b>Name           </b>: '
#  '<span>mMecoPackage</span><br><br><b>Path           </b>: '
#  '<span>/<PATH>/meco/master/developers/soner/development/main/mMecoPackage</span><br><br><b>Platforms      '
#
# '</b>: <span>Linux, Darwin, Windows</span><br><br><b>Python Packages</b>: '

Getting Local Folder and Location of a Package

import mMecoPackage.enumLib
import mMecoPackage.packageLib

package = mMecoPackage.packageLib.Package(__file__)

print(package.getLocalPath(mMecoPackage.enumLib.PackageFolderStructure.kBinDarwin))

# /<PATH>/meco/master/developers/soner/development/main/mMecoPackage/bin/darwin

print(package.getPythonPackagePath())

# /<PATH>/meco/master/developers/soner/development/main/mMecoPackage/python/mMecoPackage

Getting Python Package Related Information

import mMecoPackage.enumLib
import mMecoPackage.packageLib

package = mMecoPackage.packageLib.Package(__file__)

print(package.getPythonPath())

# /<PATH>/meco/master/developers/soner/development/main/mMecoPackage/python

print(package.getPythonPackagePath())

# /<PATH>/meco/master/developers/soner/development/main/mMecoPackage/python/mMecoPackage

print(package.getPythonPackagePath('pythonPackageThatDoesntExist'))

# None

print(package.getPythonPackages())

# ['mMecoPackage']

Examples from Unit Test

The code snippet below has been taken from mMecoPackage.tests.packageLibTest Python unit test module.

#
# ----------------------------------------------------------------------------------------------------
# IMPORTS
# ----------------------------------------------------------------------------------------------------
import os
import shutil
import unittest

import mMecoPackage.packageLib
import mMecoPackage.enumLib


#
#-----------------------------------------------------------------------------------------------------
# CODE
#-----------------------------------------------------------------------------------------------------
class PackageTest(unittest.TestCase):

    def setUp(self):

        self._testPackageName = 'testPackage'
        self._packageName  = 'mMecoPackage'
        self._packagesPath = os.path.abspath(os.path.join(os.path.dirname(__file__),
                                                          '..',
                                                          '..',
                                                          '..',
                                                          '..'))

        self._packageRoot = os.path.abspath(os.path.join(os.path.dirname(__file__),
                                                         '..',
                                                         '..',
                                                         '..'))

        self._packageName = os.path.basename(self._packageRoot)

        self._packageInfoModuleFilePath = os.path.join(self._packageRoot,
                                                       'python',
                                                       self._packageName,
                                                       'packageInfoLib.py')

    def tearDown(self):

        testPackagePath = os.path.join(self._packagesPath, self._testPackageName)
        if os.path.isdir(testPackagePath):
            shutil.rmtree(testPackagePath)

    #
    # ------------------------------------------------------------------------------------------------
    # PUBLIC METHODS
    # ------------------------------------------------------------------------------------------------
    def test_create(self):

        package = mMecoPackage.packageLib.Package.create(name=self._testPackageName,
                                                         description='This is a test package.',
                                                         path=self._packagesPath,
                                                         external=False)

        self.assertEqual(package.version(), '1.0.0')

    def test_set(self):

        package = mMecoPackage.packageLib.Package()

        self.assertTrue(package.setPackage(__file__))

        self.assertEqual(package.name(), self._packageName)

        self.assertTrue(package.setPackage(os.path.join(self._packageRoot,
                                                 'python',
                                                        self._packageName,
                                                 'tests')
                                           ))

        self.assertEqual(package.name(), self._packageName)

        self.assertTrue(package.setPackage(os.path.join(self._packageRoot,
                                                 'python',
                                                        self._packageName,
                                                 'tests',
                                                 'packageLibTest.py')
                                           ))

        self.assertEqual(package.name(), self._packageName)

        package = mMecoPackage.packageLib.Package(__file__)

        self.assertEqual(package.name(), self._packageName)

    #
    # ------------------------------------------------------------------------------------------------
    # RELEASE
    # ------------------------------------------------------------------------------------------------
    def test_getPackageReleaseRelativePath(self):

        package = mMecoPackage.packageLib.Package(self._packageRoot)

        self.assertEqual(package.getPackageReleaseRelativePath(),
                         '{}/{}/{}'.format(self._packageName,
                                           package.version(),
                                           self._packageName))

    #
    # ------------------------------------------------------------------------------------------------
    # CONTENT
    # ------------------------------------------------------------------------------------------------
    def test_getPythonPackages(self):

        package = mMecoPackage.packageLib.Package(self._packageRoot)

        self.assertEqual(package.getPythonPackages(),
                         ['mMecoPackage'])

    def test_getLocalDocument(self):

        package = mMecoPackage.packageLib.Package(self._packageRoot)

        self.assertEqual(package.getLocalDocument(mMecoPackage.enumLib.PackageFolderStructure.kDocDeveloperCPPAPIReference),
                         None)

    #
    # ------------------------------------------------------------------------------------------------
    # LOCATION
    # ------------------------------------------------------------------------------------------------
    def test_getLocation(self):

        package = mMecoPackage.packageLib.Package(self._packageRoot)

        self.assertEqual(package.getLocation(),
                         self._packagesPath)

    def test_getPythonPath(self):

        package = mMecoPackage.packageLib.Package(self._packageRoot)

        self.assertEqual(package.getPythonPath(),
                         os.path.join(self._packageRoot,
                                      'python')
                         )

    def test_getPythonPackagePath(self):

        package = mMecoPackage.packageLib.Package(self._packageRoot)

        self.assertEqual(package.getPythonPackagePath(),
                         os.path.join(self._packageRoot,
                                      'python',
                                      package.name())
                         )


    def test_getLocalPath(self):

        package = mMecoPackage.packageLib.Package(self._packageRoot)

        self.assertEqual(package.getLocalPath(mMecoPackage.enumLib.PackageFolderStructure.kBinDarwin),
                         os.path.join(self._packageRoot,
                                      'bin',
                                      'darwin')
                         )

    #
    # ----------------------------------------------------------------------------------------------------
    # QUERY
    # ----------------------------------------------------------------------------------------------------
    def test_isRootOfAPackage(self):

        self.assertTrue(mMecoPackage.packageLib.Package.isRootOfAPackage(self._packageRoot))

    def test_isInfoModuleFile(self):

        self.assertEqual(mMecoPackage.packageLib.Package.isInfoModuleFile(self._packageInfoModuleFilePath),
                         self._packageRoot)

    #
    # ----------------------------------------------------------------------------------------------------
    # DISCOVER
    # ----------------------------------------------------------------------------------------------------
    def test_getPackageName(self):

        self.assertEqual(mMecoPackage.packageLib.Package.getPackageName(self._packageInfoModuleFilePath),
                         self._packageName)

    def test_getInfoModuleFile(self):

        packageInfoModuleFile = mMecoPackage.packageLib.Package.getInfoModuleFile(self._packageRoot)

        self.assertTrue(packageInfoModuleFile.endswith('/development/main/mMecoPackage/python/mMecoPackage/packageInfoLib.py'))

        #

        nonPackagePath = os.path.abspath(os.path.join(self._packageRoot, '..', 'noPackage'))

        self.assertIsNone(mMecoPackage.packageLib.Package.getInfoModuleFile(nonPackagePath))

    def test_getPackageByImport(self):

        self.assertTrue(isinstance(mMecoPackage.packageLib.Package.getPackageByImport('{}.packageInfoLib'.format(self._packageName)),
                                   mMecoPackage.packageLib.Package))

        self.assertIsNone(mMecoPackage.packageLib.Package.getPackageByImport('NoSuchPackage.packageInfoLib'))

    def test_list(self):

        self.assertNotEqual(len(mMecoPackage.packageLib.Package.list()), 0)

#
#-----------------------------------------------------------------------------------------------------
# INVOKE
#-----------------------------------------------------------------------------------------------------
if __name__ == '__main__':

    unittest.main()