<commit_before>"""CLI client for creep"""

import cmd # Command interpreter logic. Gives us the base class for the client
import inspect # Functions to inspect live objects
import json # JSON encoder and decoder
import os # Miscellaneous operating system interfaces
import shutil # High-level file operations
import subprocess # Spawn subprocesses, connect in/out pipes, obtain return codes
import sys # System specific parameters and functions

from qi.console.client import Client
from entity.package import Package

class CreepClient(Client, cmd.Cmd):
    """Creep Mod Package Manager Client"""

    # Version of this client
    VERSION = '0.0.3'

    # Absolute path where this client is installed
    installdir = ''

    # Absolute path to the dotfile for this client
    appdir = ''

    # Absolute path to the minecraft dir
    minecraftdir = ''

    repository = {}

    def __init__(self, **kwargs):
        """Constructor"""
        cmd.Cmd.__init__(self)
        Client.__init__(self, **kwargs)

        self.updateVersionWithGitDescribe()
        self.updatePaths()

        print self.colortext("Creep v{}".format(self.VERSION), self.terminal.C_GREEN)

        self.readRegistry()

    def do_list(self, args):
        """List available packages (mods)
Usage: creep list
"""

        packages = self.repository
        for package in packages:
            line = '{name} {version} - {description}'.format(
                name=package.name,
                version=self.colortext('(' + package.version + ')', self.terminal.C_YELLOW),
                description=self.colortext(package.description, self.terminal.C_MAGENTA)
            )
            print line

    def do_install(self, args):
        """Install a package (mod)
Usage: creep install <packagename>

Example: creep install thecricket/chisel2 
"""

        package = self.fetch_package(args)
        if not package:
            return 1

        cachedir = self.appdir + os.sep + 'cache'
        if not os.path.isfile(cachedir + os.sep + package.filename):
            print "Downloading package {0} from {1}".format(package.name, package.get_download_location())
            package.download(cachedir)

        savedir = self.minecraftdir + os.sep + package.installdir 
        shutil.copyfile(cachedir + os.sep + package.filename, savedir + os.sep + package.filename)

        print "Installed mod '{0}' in '{1}'".format(package.name, savedir + os.sep + package.filename)

    def do_uninstall(self, args):
        """Uninstall a package (mod)
Usage: creep uninstall <packagename>

Example: creep uninstall thecricket/chisel2 
"""

        package = self.fetch_package(args)
        if not package:
            return 1

        savedir = self.minecraftdir + os.sep + package.installdir 
        os.remove(savedir + os.sep + package.filename)
        print "Removed mod '{0}' from '{1}'".format(package.name, savedir)

    def fetch_package(self, name):
        if name == '':
            print 'Missing package name'
            return False

        for package in self.repository:
            if package.name == name:
                return package

        print 'Unknown package {}'.format(name)
        return False

    def readRegistry(self):
        registry = json.load(open(self.installdir + os.sep + 'registry.json'))

        repository = []
        for namekey in registry['packages']:
            for versionkey in registry['packages'][namekey]:
                data = registry['packages'][namekey][versionkey]
                package = Package()
                package.name = data['name']
                package.version = data['version']
                package.description = data['description']
                package.keywords = data['keywords']
                package.require = data['require']
                package.filename = data['filename'] if 'filename' in data else ''
                package.url = data['url'] if 'url' in data else ''
                package.author = data['author']
                package.homepage = data['homepage']
                repository.append(package)

        from operator import attrgetter
        self.repository = repository
        self.repository.sort(key=attrgetter('name'))

    def updatePaths(self):
        self.installdir = os.path.dirname(os.path.dirname(os.path.abspath(inspect.getfile(inspect.currentframe()))))

        self.appdir = self.getHomePath('.creep')
        if not os.path.isdir(self.appdir):
            os.mkdir(self.appdir)

        if not os.path.isdir(self.appdir + os.sep + 'cache'):
            os.mkdir(self.appdir + os.sep + 'cache')

        if sys.platform[:3] == 'win':
            self.minecraftdir = self.getHomePath('AppData\\Roaming\\.minecraft')
        else:
            self.minecraftdir = self.getHomePath('.minecraft')

        if not os.path.isdir(self.minecraftdir + os.sep + 'mods'):
            os.mkdir(self.minecraftdir + os.sep + 'mods')

    def getHomePath(self, path=''):
        """Get the home path for this user from the OS"""
        home = os.getenv('HOME')
        if home == None:<commit_msg>Add check for existence of minecraft dir<commit_after>"""CLI client for creep"""

import cmd # Command interpreter logic. Gives us the base class for the client
import inspect # Functions to inspect live objects
import json # JSON encoder and decoder
import os # Miscellaneous operating system interfaces
import shutil # High-level file operations
import subprocess # Spawn subprocesses, connect in/out pipes, obtain return codes
import sys # System specific parameters and functions

from qi.console.client import Client
from entity.package import Package

class CreepClient(Client, cmd.Cmd):
    """Creep Mod Package Manager Client"""

    # Version of this client
    VERSION = '0.0.3'

    # Absolute path where this client is installed
    installdir = ''

    # Absolute path to the dotfile for this client
    appdir = ''

    # Absolute path to the minecraft dir
    minecraftdir = ''

    repository = {}

    def __init__(self, **kwargs):
        """Constructor"""
        cmd.Cmd.__init__(self)
        Client.__init__(self, **kwargs)

        self.updateVersionWithGitDescribe()
        self.updatePaths()

        print self.colortext("Creep v{}".format(self.VERSION), self.terminal.C_GREEN)

        self.readRegistry()

    def do_list(self, args):
        """List available packages (mods)
Usage: creep list
"""

        packages = self.repository
        for package in packages:
            line = '{name} {version} - {description}'.format(
                name=package.name,
                version=self.colortext('(' + package.version + ')', self.terminal.C_YELLOW),
                description=self.colortext(package.description, self.terminal.C_MAGENTA)
            )
            print line

    def do_install(self, args):
        """Install a package (mod)
Usage: creep install <packagename>

Example: creep install thecricket/chisel2 
"""

        package = self.fetch_package(args)
        if not package:
            return 1

        cachedir = self.appdir + os.sep + 'cache'
        if not os.path.isfile(cachedir + os.sep + package.filename):
            print "Downloading package {0} from {1}".format(package.name, package.get_download_location())
            package.download(cachedir)

        savedir = self.minecraftdir + os.sep + package.installdir 
        shutil.copyfile(cachedir + os.sep + package.filename, savedir + os.sep + package.filename)

        print "Installed mod '{0}' in '{1}'".format(package.name, savedir + os.sep + package.filename)

    def do_uninstall(self, args):
        """Uninstall a package (mod)
Usage: creep uninstall <packagename>

Example: creep uninstall thecricket/chisel2 
"""

        package = self.fetch_package(args)
        if not package:
            return 1

        savedir = self.minecraftdir + os.sep + package.installdir 
        os.remove(savedir + os.sep + package.filename)
        print "Removed mod '{0}' from '{1}'".format(package.name, savedir)

    def fetch_package(self, name):
        if name == '':
            print 'Missing package name'
            return False

        for package in self.repository:
            if package.name == name:
                return package

        print 'Unknown package {}'.format(name)
        return False

    def readRegistry(self):
        registry = json.load(open(self.installdir + os.sep + 'registry.json'))

        repository = []
        for namekey in registry['packages']:
            for versionkey in registry['packages'][namekey]:
                data = registry['packages'][namekey][versionkey]
                package = Package()
                package.name = data['name']
                package.version = data['version']
                package.description = data['description']
                package.keywords = data['keywords']
                package.require = data['require']
                package.filename = data['filename'] if 'filename' in data else ''
                package.url = data['url'] if 'url' in data else ''
                package.author = data['author']
                package.homepage = data['homepage']
                repository.append(package)

        from operator import attrgetter
        self.repository = repository
        self.repository.sort(key=attrgetter('name'))

    def updatePaths(self):
        self.installdir = os.path.dirname(os.path.dirname(os.path.abspath(inspect.getfile(inspect.currentframe()))))

        self.appdir = self.getHomePath('.creep')
        if not os.path.isdir(self.appdir):
            os.mkdir(self.appdir)

        if not os.path.isdir(self.appdir + os.sep + 'cache'):
            os.mkdir(self.appdir + os.sep + 'cache')

        if sys.platform[:3] == 'win':
            self.minecraftdir = self.getHomePath('AppData\\Roaming\\.minecraft')
        else:
            self.minecraftdir = self.getHomePath('.minecraft')

        if not os.path.isdir(self.minecraftdir):
            print "Minecraft dir not found ({})".format(self.minecraftdir)
            print "Is Minecraft installed?"
            sys.exit(2)

        if not os.path.isdir(self.minecraftdir + os.sep + 'mods'):
            os.mkdir(self.minecraftdir + os.sep + 'mods')

    def getHomePath(self, path=''):
        """Get the home path for this user from the OS"""
        home = os.getenv('HOME')
        if home == None:
{'fraction_non_alphanumeric': 0.11281952719584855, 'fraction_numerical': 0.0027868537382279455, 'mean_word_length': 2.3832899869960986, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 6, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2102364', 'n_tokens_mistral': 2940, 'n_tokens_neox': 2816, 'n_words': 767}