<commit_before>#!python
# -*- coding: utf-8 -*-

#
# Copyright (c) 2011-2016 Genestack Limited
# All Rights Reserved
# THIS IS UNPUBLISHED PROPRIETARY SOURCE CODE OF GENESTACK LIMITED
# The copyright notice above does not evidence any
# actual or intended publication of such source code.
#

import sys
import os
import urllib2
import zipfile
import xml.dom.minidom as minidom
import json
from collections import namedtuple
from genestack_client import GenestackException
from genestack_client.genestack_shell import GenestackShell, Command
from genestack_client.utils import isatty, ask_confirmation

if sys.stdout.encoding is None:
    # wrap sys.stdout into a StreamWriter to allow writing unicode to pipe
    # (in that case Python 2 cannot determine encoding)
    import codecs
    import locale
    sys.stdout = codecs.getwriter(locale.getpreferredencoding())(sys.stdout)

def validate_application_id(app_id):
    if len(app_id.split('/')) != 2:
        sys.stderr.write('Invalid application ID: expected "{vendor}/{application}", but got "%s" instead\n' % app_id)
        return False
    return True

APPLICATION_ID = 'genestack/application-manager'


SCOPE_DICT = {
    'system': 'SYSTEM',
    'user': 'USER',
    'session': 'SESSION'
}
DEFAULT_SCOPE = 'user'


VISIBILITY_DICT = {
    'owner': 'OWNER',
    'all': 'ALL'
}
DEFAULT_VISIBILITY = 'owner'


class Info(Command):
    COMMAND = 'info'
    DESCRIPTION = "Display information about an application's JAR file."
    OFFLINE = True

    def update_parser(self, p):
        p.add_argument(
            '-f', '--with-filename', action='store_true',
            help='show file names for each JAR'
        )
        p.add_argument(
            '-F', '--no-filename', action='store_true',
            help='do not show file names'
        )
        p.add_argument(
            '--vendor', action='store_true',
            help='show only vendor for each JAR file'
        )
        p.add_argument(
            'files', metavar='<jar_file_or_folder>', nargs='+',
            help='file to upload or folder with single JAR file inside (recursively)'
        )

    def run(self):
        jar_files = [resolve_jar_file(f) for f in self.args.files]
        return show_info(
            jar_files, self.args.vendor,
            self.args.with_filename, self.args.no_filename
        )


class Install(Command):
    COMMAND = 'install'
    DESCRIPTION = "Upload and install an application's JAR file to a Genestack instance."

    def update_parser(self, p):
        p.add_argument(
            '-f', '--force', action='store_true',
            default=False,
            help='Run installation without any prompts (use with caution)'
        )
        p.add_argument(
            '-o', '--override', action='store_true',
            help='overwrite old version of the applications with the new one'
        )
        p.add_argument(
            '-s', '--stable', action='store_true',
            help='mark installed applications as stable'
        )
        p.add_argument(
            '-S', '--scope', metavar='<scope>', choices=SCOPE_DICT.keys(),
            default=DEFAULT_SCOPE,
            help='scope in which application will be stable'
                 ' (default is \'%s\'): %s' %
                 (DEFAULT_SCOPE, ' | '.join(SCOPE_DICT.keys()))
        )
        p.add_argument(
            'version', metavar='<version>',
            help='version of applications to upload'
        )
        p.add_argument(
            'files', metavar='<jar_file_or_folder>', nargs='+',
            help='file to upload or folder with single JAR file inside (recursively)'
        )

    def run(self):
        jar_files = [resolve_jar_file(f) for f in self.args.files]
        return upload_file(
            self.connection.application(APPLICATION_ID),
            jar_files, self.args.version, self.args.override,
            self.args.stable, self.args.scope, self.args.force
        )


class ListVersions(Command):
    COMMAND = 'versions'
    DESCRIPTION = 'Show information about available applications.'

    def update_parser(self, p):
        p.add_argument(
            '-s', action='store_true', dest='show_stable',
            help='display stable scopes in output (S: System, U: User, E: sEssion)'
        )
        p.add_argument(
            '-i', action="store_true", dest='show_visibilities',
            help='display visibility of each version'
        )
        p.add_argument(
            '-r', action="store_true", dest='show_release_state',
            help='display release state of version'
        )
        p.add_argument(
            '-o', action='store_true', dest='show_owned',
            help='show only versions owned by current user'
        )
        p.add_argument(
            'app_id', metavar='<appId>',
            help='application identifier to show versions'
        )

    def run(self):
        app_id = self.args.app_id
        if not validate_application_id(app_id):
            return
        stable_versions = None
        if self.args.show_stable:
            stable_versions = self.connection.application(APPLICATION_ID).invoke('getStableVersions', app_id)
        result = self.connection.application(APPLICATION_ID).invoke('listVersions', app_id, self.args.show_owned)
        result.sort()

        visibility_map = None
        if self.args.show_visibilities or self.args.show_release_state:
            visibility_map = self.connection.application(APPLICATION_ID).invoke('getVisibilityMap', app_id)

        max_len = max(len(x) for x in result)
        for item in result:
            output_string = ''
            if stable_versions is not None:
                output_string += '%s%s%s ' % (
                    'S' if item == stable_versions.get('SYSTEM') else '-',
                    'U' if item == stable_versions.get('USER') else '-',
                    'E' if item == stable_versions.get('SESSION') else '-'
                )
            output_string += '%-*s' % (max_len, item)
            if self.args.show_visibilities:
                output_string += '\t%5s' % visibility_map[item]['visibilityLevel']
            if self.args.show_release_state:
                output_string += '\t%s' % ('released' if visibility_map[item]['released'] else 'not released')
            print output_string


class Visibility(Command):
    COMMAND = 'visibility'
    DESCRIPTION = 'Set visibility for application'

    def update_parser(self, p):
        p.add_argument(
            'app_id', metavar='<appId>', help='application identifier'
        )
        p.add_argument(
            'version', metavar='<version>', help='application version'
        )
        p.add_argument(
            'level', metavar='<level>', choices=VISIBILITY_DICT.keys(),
            default=DEFAULT_VISIBILITY,
            help='Visibility level which will be set to application'
                 ' (default is \'%s\'): %s' %
                 (DEFAULT_VISIBILITY, ' | '.join(VISIBILITY_DICT.keys()))<commit_msg>Fixed output formatting for release and visibility in `versions` command<commit_after>#!python
# -*- coding: utf-8 -*-

#
# Copyright (c) 2011-2016 Genestack Limited
# All Rights Reserved
# THIS IS UNPUBLISHED PROPRIETARY SOURCE CODE OF GENESTACK LIMITED
# The copyright notice above does not evidence any
# actual or intended publication of such source code.
#

import sys
import os
import urllib2
import zipfile
import xml.dom.minidom as minidom
import json
from collections import namedtuple
from genestack_client import GenestackException
from genestack_client.genestack_shell import GenestackShell, Command
from genestack_client.utils import isatty, ask_confirmation

if sys.stdout.encoding is None:
    # wrap sys.stdout into a StreamWriter to allow writing unicode to pipe
    # (in that case Python 2 cannot determine encoding)
    import codecs
    import locale
    sys.stdout = codecs.getwriter(locale.getpreferredencoding())(sys.stdout)

def validate_application_id(app_id):
    if len(app_id.split('/')) != 2:
        sys.stderr.write('Invalid application ID: expected "{vendor}/{application}", but got "%s" instead\n' % app_id)
        return False
    return True

APPLICATION_ID = 'genestack/application-manager'


SCOPE_DICT = {
    'system': 'SYSTEM',
    'user': 'USER',
    'session': 'SESSION'
}
DEFAULT_SCOPE = 'user'


VISIBILITY_DICT = {
    'owner': 'OWNER',
    'all': 'ALL'
}
DEFAULT_VISIBILITY = 'owner'


class Info(Command):
    COMMAND = 'info'
    DESCRIPTION = "Display information about an application's JAR file."
    OFFLINE = True

    def update_parser(self, p):
        p.add_argument(
            '-f', '--with-filename', action='store_true',
            help='show file names for each JAR'
        )
        p.add_argument(
            '-F', '--no-filename', action='store_true',
            help='do not show file names'
        )
        p.add_argument(
            '--vendor', action='store_true',
            help='show only vendor for each JAR file'
        )
        p.add_argument(
            'files', metavar='<jar_file_or_folder>', nargs='+',
            help='file to upload or folder with single JAR file inside (recursively)'
        )

    def run(self):
        jar_files = [resolve_jar_file(f) for f in self.args.files]
        return show_info(
            jar_files, self.args.vendor,
            self.args.with_filename, self.args.no_filename
        )


class Install(Command):
    COMMAND = 'install'
    DESCRIPTION = "Upload and install an application's JAR file to a Genestack instance."

    def update_parser(self, p):
        p.add_argument(
            '-f', '--force', action='store_true',
            default=False,
            help='Run installation without any prompts (use with caution)'
        )
        p.add_argument(
            '-o', '--override', action='store_true',
            help='overwrite old version of the applications with the new one'
        )
        p.add_argument(
            '-s', '--stable', action='store_true',
            help='mark installed applications as stable'
        )
        p.add_argument(
            '-S', '--scope', metavar='<scope>', choices=SCOPE_DICT.keys(),
            default=DEFAULT_SCOPE,
            help='scope in which application will be stable'
                 ' (default is \'%s\'): %s' %
                 (DEFAULT_SCOPE, ' | '.join(SCOPE_DICT.keys()))
        )
        p.add_argument(
            'version', metavar='<version>',
            help='version of applications to upload'
        )
        p.add_argument(
            'files', metavar='<jar_file_or_folder>', nargs='+',
            help='file to upload or folder with single JAR file inside (recursively)'
        )

    def run(self):
        jar_files = [resolve_jar_file(f) for f in self.args.files]
        return upload_file(
            self.connection.application(APPLICATION_ID),
            jar_files, self.args.version, self.args.override,
            self.args.stable, self.args.scope, self.args.force
        )


class ListVersions(Command):
    COMMAND = 'versions'
    DESCRIPTION = 'Show information about available applications.'

    def update_parser(self, p):
        p.add_argument(
            '-s', action='store_true', dest='show_stable',
            help='display stable scopes in output (S: System, U: User, E: sEssion)'
        )
        p.add_argument(
            '-i', action="store_true", dest='show_visibilities',
            help='display visibility of each version'
        )
        p.add_argument(
            '-r', action="store_true", dest='show_release_state',
            help='display release state of version'
        )
        p.add_argument(
            '-o', action='store_true', dest='show_owned',
            help='show only versions owned by current user'
        )
        p.add_argument(
            'app_id', metavar='<appId>',
            help='application identifier to show versions'
        )

    def run(self):
        app_id = self.args.app_id
        if not validate_application_id(app_id):
            return
        stable_versions = None
        if self.args.show_stable:
            stable_versions = self.connection.application(APPLICATION_ID).invoke('getStableVersions', app_id)
        result = self.connection.application(APPLICATION_ID).invoke('listVersions', app_id, self.args.show_owned)
        result.sort()

        visibility_map = None
        if self.args.show_visibilities or self.args.show_release_state:
            visibility_map = self.connection.application(APPLICATION_ID).invoke('getVisibilityMap', app_id)

        max_len = max(len(x) for x in result)
        for item in result:
            output_string = ''
            if stable_versions is not None:
                output_string += '%s%s%s ' % (
                    'S' if item == stable_versions.get('SYSTEM') else '-',
                    'U' if item == stable_versions.get('USER') else '-',
                    'E' if item == stable_versions.get('SESSION') else '-'
                )
            output_string += '%-*s' % (max_len, item)
            if self.args.show_visibilities:
                output_string += '%8s' % visibility_map[item]['visibilityLevel']
            if self.args.show_release_state:
                output_string += '   %s' % ('released' if visibility_map[item]['released'] else 'not released')
            print output_string


class Visibility(Command):
    COMMAND = 'visibility'
    DESCRIPTION = 'Set visibility for application'

    def update_parser(self, p):
        p.add_argument(
            'app_id', metavar='<appId>', help='application identifier'
        )
        p.add_argument(
            'version', metavar='<version>', help='application version'
        )
        p.add_argument(
            'level', metavar='<level>', choices=VISIBILITY_DICT.keys(),
            default=DEFAULT_VISIBILITY,
            help='Visibility level which will be set to application'
                 ' (default is \'%s\'): %s' %
                 (DEFAULT_VISIBILITY, ' | '.join(VISIBILITY_DICT.keys()))
{'fraction_non_alphanumeric': 0.11387546204151265, 'fraction_numerical': 0.0018481660506113164, 'mean_word_length': 2.336258003319896, 'pattern_counts': {'":': 0, '<': 19, '<?xml version=': 0, '>': 19, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 2}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '824807', 'n_tokens_mistral': 4105, 'n_tokens_neox': 3965, 'n_words': 1034}