# fa19-516-162: Cloudmesh Common
# E.Cloudmesh.Common.4
# Develop a program that demonstrates the use of cloudmesh.common.Shell.

from cloudmesh.common.Shell import Shell

# Check python version installed
result = Shell.check_python()
print(result)

# Check if command "ping" exists
result = Shell.command_exists("ping")
print(result)

# Check location of git executable
result = Shell.which("git")
print(result)
Пример #2
0
# sp20-516-233 E.Cloudmesh.Common.4

# Develop a program that demonstrates the use of cloudmesh.common.Shell
from cloudmesh.common.Shell import Shell

if __name__ == "__main__":
    # this is the command for getting the path but still gives errors
    # result = Shell.execute('echo %cd%')

    print("These commands are for Windows 10 EDU")
    r1 = Shell.check_python()
    print(r1)
    r2 = Shell.pip()
    print(r2)
    r3 = Shell.ls()
    print(r3)
    # the following code gives UNDEFINED_TERMINAL_TYPE
    r4 = Shell.terminal_type()
    print(r4)
    # the following code gives win32 but I'm on a 64 bit system
    r5 = Shell.operating_system()
    print(r5)

    # commands below do not work
    # result = Shell.execute('pwd')
    # print(result)
    # result = Shell.execute('ls', ["-l", "-a"])
    # print(result)
    # result = Shell.execute('ls', "-l -a")
    # print(result)
# fa19-516-160
# E.Cloudmesh.Common.4
# Task :  Develop a program that demonstrates the use of cloudmesh.common.Shell.

from cloudmesh.common.Shell import Shell

# Check Python Version
version = Shell.check_python()
print(version)

working_dir = Shell.execute('pwd')
print(working_dir)

#Code reference Introduction to python : Gregor von Laszewski.: section 6.5
Пример #4
0
    def do_version(self, args, arguments):
        """
        ::

          Usage:
            version pip [PACKAGE]
            version [--format=FORMAT] [--check=CHECK]
           

          Options:
            --format=FORMAT  the format to print the versions in [default: table]
            --check=CHECK    boolean tp conduct an additional check [default: True]

          Description:
            version 
                Prints out the version number
            version pip
                Prints the contents of pip list
                
          Limitations:
            Package names must not have a . in them instead you need to use -
            Thus to query for cloudmesh-cmd5 use
            
              cms version pip cloudmesh-cmd5
           
        """

        # print (arguments)
        # print (">", args, "<")

        if arguments["pip"]:
            # noinspection PyBroadException
            try:
                package = arguments["PACKAGE"]

                if package is None:
                    result = Shell.execute('pip', ['list', '--format=columns'],
                                           traceflag=False,
                                           witherror=False)
                    print(result)
                else:
                    if "." in package:
                        package = package.replace(".", "-")
                    result = Shell.execute('pip', ['show', package],
                                           traceflag=False,
                                           witherror=False)
                    print(result)

            except Exception as e:
                result = 'N/A'
            return ""

        python_version, pip_version = Shell.get_python()

        # noinspection PyBroadException
        try:
            git_hash_version = Shell.execute('git',
                                             'log -1 --format=%h',
                                             traceflag=False,
                                             witherror=False)
        except:
            git_hash_version = 'N/A'

        versions = {
            "python": {
                "name": "python",
                "version": str(python_version)
            },
            "pip": {
                "name": "pip",
                "version": str(pip_version)
            },
            "git": {
                "name": "git hash",
                "version": str(git_hash_version)
            }
        }

        # dynamically check all installed cloudmesh packages and versions
        pipcheck = subprocess.Popen(('pip', 'freeze'), stdout=subprocess.PIPE)
        try:
            # python 3 returns byte sequence so the decode is necessary
            output = subprocess.check_output(
                ('grep', "cloudmesh"), stdin=pipcheck.stdout).decode("utf-8")
            pkglines = output.strip().split("\n")

            for pkgline in pkglines:
                if "==" in pkgline:
                    values = pkgline.split("==")
                    pkg = values[0]
                    version = values[1].strip()
                    if pkg != 'cloudmesh-installer':
                        pname = pkg.replace("cloudmesh-", "cloudmesh.")
                        i = importlib.import_module(pname)

                        location = i.__file__

                        try:
                            vlocation = path_expand(
                                os.path.join(os.path.dirname(location),
                                             "__version__.py"))
                            v = readfile(vlocation).split('"')[1].strip()
                        except:
                            v = "not found"

                        versions[pkg] = {
                            "name": pkg,
                            "package": pname,
                            "version": version,
                            "source": location,
                            "VERSION": v
                        }
                elif "git" in pkgline:

                    pkgline = pkgline.replace(
                        "-e [email protected]:cloudmesh-community/", "")
                    pkgline = pkgline.replace(
                        "-e git+https://github.com/cloudmesh/", "")
                    pkgline = pkgline.replace("egg=", "")

                    version, pkg = pkgline.split("#")
                    pname = pkg.replace("cloudmesh-", "cloudmesh.")
                    #
                    # The next line needed to be added as for some reason the is an _ here
                    #
                    pname = pkg.replace("cloudmesh_", "cloudmesh.")

                    i = importlib.import_module(pname)

                    location = i.__file__

                    try:
                        vlocation = path_expand(
                            os.path.join(os.path.dirname(location),
                                         "__version__.py"))
                        v = readfile(vlocation).split('"')[1].strip()
                    except:
                        v = "not found"

                    versions[pkg] = {
                        "name": pkg,
                        "package": pname,
                        "version": version,
                        "source": location,
                        "VERSION": v
                    }
        except subprocess.CalledProcessError as e:
            pass
        pipcheck.wait()

        # installedpkgs = []
        #
        # for a preset set of named packages
        '''
        #pkgs = ['cloudmesh-common', 'cloudmesh-cmd5', 'cloudmesh.comet']
        for package in pkgs:
            # check version from pip
            pipcheck = subprocess.Popen(('pip', 'freeze'), stdout=subprocess.PIPE)
            try:
                output = subprocess.check_output(('grep', package), stdin=pipcheck.stdout)
                version = output.split("==")[1].strip()
                versions[package] = {"name": package,
                                     "version": version
                                     }
            except subprocess.CalledProcessError as e:
                pass
            pipcheck.wait()
        '''

        # __version__ not maintained in package file so this won't work
        '''
            try:
                print ("trying package |%s|" % package)
                try_module = __import__(package)
                print ("added one package into the list...")
                installedpkgs.append(package)
            except ImportError as e:
                print ("error importing |%s|" % package)
                pass

        #print (installedpkgs)
        for package in installedpkgs:
            versions[package] = {package: {"name": package,
                                           "version": str(package.__version__)
                                           }
                                 }
        print (versions)
        '''

        print(
            Printer.write(
                versions,
                output=arguments["--format"],
                order=["name", "package", "VERSION", "version", "source"],
                sort_keys="name"))
        if arguments["--check"] in ["True"]:
            Shell.check_python()
Пример #5
0
    def do_version(self, args, arguments):
        """
        Usage:
           version pip [PACKAGE]
           version [--format=FORMAT] [--check=CHECK]
           

        Options:
            --format=FORMAT  the format to print the versions in [default: table]
            --check=CHECK    boolean tp conduct an additional check [default: True]

        Description:
            version 
                Prints out the version number
            version pip
                Prints the contents of pip list
                
        Limitations:
           Package names must not have a . in them instead you need to use -
           Thus to querry for cloudmesh.cmd5 use
            
              cms version pip cloudmesh-cmd5
           
        """

        #print (arguments)

        if arguments["pip"]:
            try:
                package = arguments["PACKAGE"]

                if package is None:
                    result = Shell.execute('pip', ['list', '--format=columns'], traceflag=False, witherror=False)
                    print (result)
                else:
                    if "." in package:
                        package = package.replace(".", "-")
                    result = Shell.execute('pip', ['show', package], traceflag=False, witherror=False)
                    print(result)

            except Exception as e:
                result = 'N/A'
            return ""


        python_version, pip_version = Shell.get_python()

        try:
            git_hash_version = Shell.execute('git', 'log -1 --format=%h', traceflag=False, witherror=False)
        except:
            git_hash_version = 'N/A'

        versions = {
            # "cloudmesh_client": {
            #    "name": "cloudmesh_client",
            #    "version": str(cloudmesh_client.__version__)
            # },
            # "cloudmesh_base": {
            #     "name": "cloudmesh_base",
            #     "version": str(cloudmesh_base.__version__)
            # },
            "python": {
                "name": "python",
                "version": str(python_version)
            },
            "pip": {
                "name": "pip",
                "version": str(pip_version)
            },
            "git": {
                "name": "git hash",
                "version": str(git_hash_version)
            }

        }
        print(Printer.write(versions, output=arguments["--format"],
                            order=["name", "version"]))
        if arguments["--check"] in ["True"]:
            Shell.check_python()
Пример #6
0
    def do_version(self, args, arguments):
        """
        Usage:
           version pip [PACKAGE]
           version [--format=FORMAT] [--check=CHECK]
           

        Options:
            --format=FORMAT  the format to print the versions in [default: table]
            --check=CHECK    boolean tp conduct an additional check [default: True]

        Description:
            version 
                Prints out the version number
            version pip
                Prints the contents of pip list
                
        Limitations:
           Package names must not have a . in them instead you need to use -
           Thus to query for cloudmesh.cmd5 use
            
              cms version pip cloudmesh-cmd5
           
        """

        # print (arguments)

        if arguments["pip"]:
            # noinspection PyBroadException
            try:
                package = arguments["PACKAGE"]

                if package is None:
                    result = Shell.execute('pip', ['list', '--format=columns'],
                                           traceflag=False,
                                           witherror=False)
                    print(result)
                else:
                    if "." in package:
                        package = package.replace(".", "-")
                    result = Shell.execute('pip', ['show', package],
                                           traceflag=False,
                                           witherror=False)
                    print(result)

            except Exception as e:
                result = 'N/A'
            return ""

        python_version, pip_version = Shell.get_python()

        # noinspection PyBroadException
        try:
            git_hash_version = Shell.execute('git',
                                             'log -1 --format=%h',
                                             traceflag=False,
                                             witherror=False)
        except:
            git_hash_version = 'N/A'

        versions = {
            # "cloudmesh_client": {
            #    "name": "cloudmesh_client",
            #    "version": str(cloudmesh_client.__version__)
            # },
            # "cloudmesh_base": {
            #     "name": "cloudmesh_base",
            #     "version": str(cloudmesh_base.__version__)
            # },
            "python": {
                "name": "python",
                "version": str(python_version)
            },
            "pip": {
                "name": "pip",
                "version": str(pip_version)
            },
            "git": {
                "name": "git hash",
                "version": str(git_hash_version)
            }
        }

        # dynamically check all installed cloudmesh packages and versions
        pipcheck = subprocess.Popen(('pip', 'freeze'), stdout=subprocess.PIPE)
        try:
            # python 3 returns byte sequence so the decode is necessary
            output = subprocess.check_output(
                ('grep', "cloudmesh"), stdin=pipcheck.stdout).decode("utf-8")
            pkglines = output.strip().split("\n")
            for pkgline in pkglines:
                values = pkgline.split("==")
                pkg = values[0]
                version = values[1].strip()
                versions[pkg] = {"name": pkg, "version": version}
        except subprocess.CalledProcessError as e:
            pass
        pipcheck.wait()

        #installedpkgs = []
        #
        # for a preset set of named packages
        '''
        #pkgs = ['cloudmesh.common', 'cloudmesh.cmd5', 'cloudmesh.comet']
        for package in pkgs:
            # check version from pip
            pipcheck = subprocess.Popen(('pip', 'freeze'), stdout=subprocess.PIPE)
            try:
                output = subprocess.check_output(('grep', package), stdin=pipcheck.stdout)
                version = output.split("==")[1].strip()
                versions[package] = {"name": package,
                                     "version": version
                                     }
            except subprocess.CalledProcessError as e:
                pass
            pipcheck.wait()
        '''

        # __version__ not maintained in package file so this won't work
        '''
            try:
                print ("trying package |%s|" % package)
                try_module = __import__(package)
                print ("added one package into the list...")
                installedpkgs.append(package)
            except ImportError as e:
                print ("error importing |%s|" % package)
                pass

        #print (installedpkgs)
        for package in installedpkgs:
            versions[package] = {package: {"name": package,
                                           "version": str(package.__version__)
                                           }
                                 }
        print (versions)
        '''

        print(
            Printer.write(versions,
                          output=arguments["--format"],
                          order=["name", "version"],
                          sort_keys="name"))
        if arguments["--check"] in ["True"]:
            Shell.check_python()