Example #1
0
def init(force):
    """
    This command initializes a folder with the typical contents of a Python package.
    After running this and writing your code, you should be ready to publish your package.
    """
    echo('\nThis utility will help you set up a new python module for publishing on PyPi!\n')
    echo('After answering a few questions, it will create a few files.')
    echo('\nPress ^C at any time to bail!\n')

    remap = {
        'entry': 'entry point',
        'package': 'package name'
    }
    d = _defaults()
    for k, v in d.items():
        d[k] = prompt.query('%s:' % remap.get(k, k), default=v)

    echo('\nReady to create the following files:')

    with indent(4, quote='  -'):
        puts('setup.py')
        puts('setup.cfg')
        puts('MANIFEST.in')
        puts(d['package'] + '/' + '__init__.py')
        puts(d['package'] + '/' + d['entry'])
        puts('requirements.txt')

    finalize = prompt.yn('\nSound like a plan?', default='y')

    if finalize:
        echo('')
        _make_package(d, force)
        echo('')

    success('Your package is initialized!')
Example #2
0
def init(force):
    """
    This command initializes a folder with the typical contents of a Python package.
    After running this and writing your code, you should be ready to publish your package.
    """
    echo('\nThis utility will help you set up a new python module for publishing on PyPi!\n')
    echo('After answering a few questions, it will create a few files.')
    echo('\nPress ^C at any time to bail!\n')

    remap = {
        'entry': 'entry point',
        'package': 'package name'
    }
    d = _defaults()
    for k, v in d.items():
        d[k] = prompt.query('%s:' % remap.get(k, k), default=v)

    echo('\nReady to create the following files:')

    with indent(4, quote='  -'):
        puts('setup.py')
        puts('setup.cfg')
        puts('MANIFEST.in')
        puts(d['package'] + '/' + '__init__.py')
        puts(d['package'] + '/' + d['entry'])
        puts('requirements.txt')

    finalize = prompt.yn('\nSound like a plan?', default='y')

    if finalize:
        echo('')
        _make_package(d, force)
        echo('')

    success('Your package is initialized!')
Example #3
0
def modifyDataSets(dataSets):
    putSeparator()
    puts("Modify Data Sets:")
    putSeparator('-', 30)
    printDataSets(dataSets)
    putSeparator('-', 30)
    modifyingData = True
    while modifyingData or len(dataSets['active']) < 1:
        contineEditing = True
        dataName = prompt.query("Data Set Name:")
        if dataName in dataSets['active'] or dataName in dataSets['inactive']:
            puts(
                colored.yellow("WARNING: '%s' already exists in data list" %
                               (dataName, )))
            contineEditing = prompt.yn("Edit '%s' Data Settings?" %
                                       (dataName, ))
        if contineEditing:
            try:
                defaultInput = dataSets['active'][dataName]['input']
                defaultOutput = dataSets['active'][dataName]['output']
            except KeyError:
                try:
                    defaultInput = dataSets['inactive'][dataName]['input']
                    defaultOutput = dataSets['inactive'][dataName]['output']
                except KeyError:
                    defaultInput = ''
                    defaultOutput = ''
            dataInputDir = prompt.query(
                "Data Input Directory:",
                default=defaultInput,
                validators=[sab_validators.PathValidator()])
            dataOutputDir = prompt.query(
                "Data Output Directory:",
                validators=[sab_validators.PathValidator()],
                default=defaultOutput)
            dataState = 'active' if prompt.yn("Active:") else 'inactive'

            dataSets['active'].pop(dataName, None)
            dataSets['inactive'].pop(dataName, None)

            dataSets[dataState][dataName] = {
                'input': dataInputDir,
                'output': dataOutputDir
            }
            #Weirdly clint compares answer against default in order to return boolean
            modifyingData = prompt.yn('Continue Modifying Data Sets?')
Example #4
0
def main():
    print_log("Starting battle-catsort at " +
              datetime.datetime.utcnow().isoformat())

    site = pywikibot.Site("en", "wikipedia")
    site.login()

    args = get_parsed_args()
    if args.count:
        num_edits = args.count
        print_log("Starting off with %d edits made." % num_edits)
    else:
        num_edits = 0

    for each_page in [pywikibot.Page(site, args.page)
                      ] if args.page else site.allpages("Battle of",
                                                        filterredir=False):
        each_title = each_page.title(withNamespace=False).encode("utf-8")
        title_match = BATTLE_TITLE.search(each_title)

        if not title_match:
            print_log("Somehow {} didn't match.".format(each_title))
            continue

        if not is_actual_battle(each_page):
            print_log("{} isn't an actual battle.".format(each_title))
            continue

        print_log("About to process {}.".format(each_title))
        old_text = each_page.text
        changes_made = process(each_page)
        if old_text == each_page.text:
            print_log("No changes made to {}.".format(each_title))
            continue

        if not args.interactive or prompt.yn("({}) Save {}?".format(
                changes_made, each_title)):
            each_page.save(summary=SUMMARY.format(changes_made))
            num_edits += 1
            print_log("%d edits made so far." % num_edits)
            if args.limit and num_edits >= args.limit:
                print_log("%d edits (limit) reached; done." % num_edits)
                sys.exit(0)
        elif prompt.yn("Exit?"):
            sys.exit(0)
Example #5
0
    def get_memory(self):
        """
        Customize default Memory

        https://docs.vagrantup.com/v2/virtualbox/configuration.html
        """
        if prompt.yn('Customize your box memory', default='y'):
            self.content['memory'] = prompt.query(
                "New Memory: ", validators=[IntegerValidator()])
Example #6
0
    def get_cpu(self):
        """
        Customize Default CPU

        https://docs.vagrantup.com/v2/virtualbox/configuration.html
        """
        if prompt.yn('Customize your box cpu', default='y'):
            self.content['cpu'] = prompt.query(
                "New Cpu: ", validators=[IntegerValidator()])
def configure_package(name,
                      contained_files,
                      download_url=None,
                      download_filename=None,
                      install_commands=[],
                      version=None,
                      doubleindent=False):
    SPECIFY_PATH = False
    OWN = False
    if doubleindent:
        indents = '        '
    else:
        indents = '    '
    if not QUIET_MODE and VSVERSION_FIT:
        SPECIFY_PATH = not prompt.yn(
            indents + "Do you want to use your own version of %s?" % (name),
            default='n')
    if SPECIFY_PATH or not VSVERSION_FIT:
        libpath = prompt.query(indents + "Please specify the path to %s. The folder must " % (name) +\
          "contain the files/folders " + ", ".join(contained_files) + ".",
          validators=[PackageValidator(contained_files=contained_files)])
        OWN = True
    else:
        if not download_url is None:
            if os.path.exists(download_filename):
                with indent(4):
                    puts('Download file %s detected. Skipping.' %
                         (download_filename))
            else:
                with indent(4):
                    puts('Downloading...')
                    urllib.urlretrieve(download_url, download_filename,
                                       DOWNLOAD_HOOK)
                    puts('Installing...')
                    for command in install_commands:
                        check_call(command, stdout=STDOUT, stderr=STDERR)
            libpath = None
        else:
            if os.path.exists(os.path.join('nuget-deps', name)):
                with indent(4):
                    puts('nuget folder detected. Skipping.')
            else:
                with indent(4):
                    puts('installing using nuget...')
                if version is None:
                    check_call([
                        'nuget', 'install', name, '-OutputDirectory',
                        'nuget-deps', '-ExcludeVersion'
                    ])
                else:
                    check_call([
                        'nuget', 'install', name, '-Version', version,
                        '-OutputDirectory', 'nuget-deps', '-ExcludeVersion'
                    ])
            libpath = os.path.join("nuget-deps", name)
    return libpath, OWN
Example #8
0
 def remove_config(self):
     """
     Interactive config removal
     """
     reset = prompt.yn('Would you like to reset config?')
     if reset:
         username = self.get('user', 'username')
         os.remove(self.config_file_path)
         self.__delete_password(username)
         print colored.green('Config removed')
     sys.exit()
Example #9
0
 def remove_config(self):
     """
     Interactive config removal
     """
     reset = prompt.yn('Would you like to reset config?')
     if reset:
         username = self.get('user', 'username')
         os.remove(self.config_file_path)
         self.__delete_password(username)
         print colored.green('Config removed')
     sys.exit()
Example #10
0
def main():
    print_log("Starting battle-catsort at " + datetime.datetime.utcnow().isoformat())

    site = pywikibot.Site("en", "wikipedia")
    site.login()

    args = get_parsed_args()
    if args.count:
        num_edits = args.count
        print_log("Starting off with %d edits made." % num_edits)
    else:
        num_edits = 0

    for each_page in [pywikibot.Page(site, args.page)] if args.page else site.allpages("Battle of", filterredir=False):
        each_title = each_page.title(withNamespace=False).encode("utf-8")
        title_match = BATTLE_TITLE.search(each_title)

        if not title_match:
            print_log("Somehow {} didn't match.".format(each_title))
            continue

        if not is_actual_battle(each_page):
            print_log("{} isn't an actual battle.".format(each_title))
            continue

        print_log("About to process {}.".format(each_title))
        old_text = each_page.text
        changes_made = process(each_page)
        if old_text == each_page.text:
            print_log("No changes made to {}.".format(each_title))
            continue

        if not args.interactive or prompt.yn("({}) Save {}?".format(changes_made, each_title)):
            each_page.save(summary=SUMMARY.format(changes_made))
            num_edits += 1
            print_log("%d edits made so far." % num_edits)
            if args.limit and num_edits >= args.limit:
                print_log("%d edits (limit) reached; done." % num_edits)
                sys.exit(0)
        elif prompt.yn("Exit?"):
            sys.exit(0)
Example #11
0
    def get_private_ip(self):
        """
        You can also specify a static IP address for the machine.
        This lets you access the Vagrant managed machine using a static
         known IP. The Vagrantfile for a static IP looks like this:

        https://docs.vagrantup.com/v2/networking/private_network.html
        """
        if prompt.yn('Create a private network ?', default='y'):
            self.content['private_ip'] = prompt.query(
                'Private Ip Address: ',
                validators=[IpAddressValidator()])
Example #12
0
def prompt_drive_config(drive):
    if hasattr(drive, 'config'):
        drive_config_data = drive.config.data
    else:
        drive_config_data = drive_config.DriveConfig.DEFAULT_VALUES
    if drive_config_data['local_root'] is None or drive_config_data['local_root'] == '':
        drive_config_data['local_root'] = OS_USER_HOME + '/OneDrive/' + drive.drive_id
    puts(colored.green('You selected Drive "%s"...' % drive.drive_id))

    puts()
    with indent(4, quote=' >'):
        puts('When specifying local root, pick a directory not used by or under any other Drive.')
        puts('When specifying HTTPS download / upload sizes, note that files larger than those sizes will be handled '
             'as chunks.')
    puts()

    while True:
        local_root = prompt.query('Which local directory do you want to sync with this Drive?',
                                  default=drive_config_data['local_root'])
        try:
            while local_root[-1] == '/':
                local_root = local_root[:-1]
            if not os.path.exists(local_root):
                puts(colored.yellow('Directory "%s" does not exist. Try creating it...' % local_root))
                mkdir(local_root)
                puts(colored.green('Successfully created directory "%s".' % local_root))
            elif os.path.isfile(local_root):
                raise ValueError('Path "%s" is a file.' % local_root)
            if os.path.isdir(local_root):
                drive_config_data['local_root'] = local_root
                break
            raise ValueError('Invalid path "%s"' % local_root)
        except Exception as ex:
            puts(colored.red('Error: ' + str(ex)))
    drive_config_data['max_get_size_bytes'] = prompt.query('Maximum size, in KB, for a single download request?',
                                                           default=str(drive_config_data['max_get_size_bytes'] >> 10),
                                                           validators=[validators.IntegerValidator()]) * 1024
    drive_config_data['max_put_size_bytes'] = prompt.query('Maximum size, in KB, for a single upload request?',
                                                           default=str(drive_config_data['max_put_size_bytes'] >> 10),
                                                           validators=[validators.IntegerValidator()]) * 1024
    try:
        while not prompt.yn('Do you have ignore list files specific to this Drive to add?', default='n'):
            ignore_file_path = prompt.query('Path to the ignore list file (hit [Ctrl+C] to skip): ',
                                            validators=[validators.FileValidator()])
            drive_config_data['ignore_files'].add(ignore_file_path)
            puts(colored.green('Recorded ignore list file: "{}"' % ignore_file_path))
    except KeyboardInterrupt:
        pass
    drive_conf = drive_config.DriveConfig.load(drive_config_data)
    drive.config = drive_conf
    drive_store.add_record(drive)
Example #13
0
    def get_public_network(self):
        """
        Public networks are less private than private networks,
        and the exact meaning actually varies from provider to provider,
        hence the ambiguous definition. The idea is that while private
        networks should never allow the general public access to your
        machine, public networks can.

        https://docs.vagrantup.com/v2/networking/public_network.html
        """
        if prompt.yn('Create a Public network(Static ip) ?', default='y'):
            self.content['public_network'] = prompt.query(
                'Public Ip Address: ',
                validators=[IpAddressValidator()])
Example #14
0
def edit_default_ignore_list():
    puts(colored.green('Editing global ignore list files...\n'))

    default_path = CONFIG_DIR + '/odignore.txt'
    if os.path.isfile(
            default_path
    ) and default_path not in user_conf.default_drive_config.ignore_files:
        puts(
            colored.yellow('Found default ignore list file "%s".' %
                           default_path))
        if prompt.yn('Do you want to add this ignore list? '):
            user_conf.default_drive_config.ignore_files.add(default_path)

    with indent(4, quote=' >'):
        puts(
            'To add a new ignore list file that is effective on all Drives, type the file path below.'
        )
        puts(
            'To remove an existing ignore list file, type a minus sign followed by the index of the path to delete.'
        )
        puts('For example, type "-1" to delete the path indexed 1.')
        puts('To abort and return to main menu, hit [Ctrl+C].')

    try:
        while True:
            path_list = print_existing_ignore_list_paths()
            command = prompt.query(
                'Please specify an absolute path or give a negated index: ')
            try:
                if len(command) > 0 and command[0] == '-':
                    index_to_delete = int(command[1:])
                    if index_to_delete < 0 or index_to_delete > len(path_list):
                        raise ValueError('Invalid path index "%d"' %
                                         index_to_delete)
                    path_to_delete = path_list[index_to_delete]
                    user_conf.default_drive_config.ignore_files.remove(
                        path_to_delete)
                    puts(
                        colored.green('Deleted path "%s" from list.' %
                                      path_to_delete))
                elif os.path.isfile(command):
                    user_conf.default_drive_config.ignore_files.add(command)
                    puts(colored.green('Added path "%s" to list.' % command))
                else:
                    raise ValueError('Path "%s" is not a file.' % command)
            except ValueError as ex:
                puts(colored.red('Error: ' + str(ex)))
    except KeyboardInterrupt:
        puts(colored.green('Aborted.'))
Example #15
0
def prompt_drive_config(drive):
    if hasattr(drive, 'config'):
        drive_config_data = drive.config.data
    else:
        drive_config_data = drive_config.DriveConfig.DEFAULT_VALUES
    if drive_config_data['local_root'] is None or drive_config_data['local_root'] == '':
        drive_config_data['local_root'] = OS_USER_HOME + '/OneDrive/' + drive.drive_id
    puts(colored.green('You selected Drive "%s"...' % drive.drive_id))

    puts()
    with indent(4, quote=' >'):
        puts('When specifying local root, pick a directory not used by or under any other Drive.')
        puts('When specifying HTTPS download / upload sizes, note that files larger than those sizes will be handled '
             'as chunks.')
    puts()

    while True:
        local_root = prompt.query('Which local directory do you want to sync with this Drive?',
                                  default=drive_config_data['local_root'])
        try:
            if not os.path.exists(local_root):
                puts(colored.yellow('Directory "%s" does not exist. Try creating it...' % local_root))
                mkdir(local_root)
                puts(colored.green('Successfully created directory "%s".' % local_root))
            elif os.path.isfile(local_root):
                raise ValueError('Path "%s" is a file.' % local_root)
            if os.path.isdir(local_root):
                drive_config_data['local_root'] = local_root
                break
            raise ValueError('Invalid path "%s"' % local_root)
        except Exception as e:
            puts(colored.red('Error: ' + str(e)))
    drive_config_data['max_get_size_bytes'] = prompt.query('Maximum size, in KB, for a single download request?',
                                                           default=str(drive_config_data['max_get_size_bytes'] >> 10),
                                                           validators=[validators.IntegerValidator()]) * 1024
    drive_config_data['max_put_size_bytes'] = prompt.query('Maximum size, in KB, for a single upload request?',
                                                           default=str(drive_config_data['max_put_size_bytes'] >> 10),
                                                           validators=[validators.IntegerValidator()]) * 1024
    try:
        while not prompt.yn('Do you have ignore list files specific to this Drive to add?', default='n'):
            ignore_file_path = prompt.query('Path to the ignore list file (hit [Ctrl+C] to skip): ',
                                            validators=[validators.FileValidator()])
            drive_config_data['ignore_files'].add(ignore_file_path)
            puts(colored.green('Recorded ignore list file: "{}"' % ignore_file_path))
    except KeyboardInterrupt:
        pass
    drive_conf = drive_config.DriveConfig.load(drive_config_data)
    drive.config = drive_conf
    drive_store.add_record(drive)
Example #16
0
def handle_token(config, configfp):
    """ Prompt the user for the Jenkins token if it is not in config. """
    token = config['jenkins'].get('token', None)
    if token is None:
        token = prompt.query('You must enter a Jenkins token:')
        if token is None:
            raise RuntimeError("A token is required to use the API.")

        store = prompt.yn('Would you like to store your token?')
        if store is True:
            config['jenkins']['token'] = token
            cf = configfp / "config.yaml"
            with open(cf, 'w') as stream:
                yaml.safe_dump(config, stream)
    return token
def configure_package(name,
                      contained_files,
                      download_url=None,
                      download_filename=None,
                      install_commands=[],
                      version=None,
                      doubleindent=False):
    SPECIFY_PATH = False
    OWN = False
    if doubleindent:
        indents = '        '
    else:
        indents = '    '
    if not QUIET_MODE and VSVERSION_FIT:
        SPECIFY_PATH = not prompt.yn(indents + "Do you want to use your own version of %s?" % (name), default='n')
    if SPECIFY_PATH or not VSVERSION_FIT:
        libpath = prompt.query(indents + "Please specify the path to %s. The folder must " % (name) +\
          "contain the files/folders " + ", ".join(contained_files) + ".",
          validators=[PackageValidator(contained_files=contained_files)])
        OWN = True
    else:
        if not download_url is None:
            if os.path.exists(download_filename):
                with indent(4):
                    puts('Download file %s detected. Skipping.' % (download_filename))
            else:
                with indent(4):
                    puts('Downloading...')
                    urllib.urlretrieve(download_url,
                                       download_filename,
                                       DOWNLOAD_HOOK)
                    puts('Installing...')
                    for command in install_commands:
                        check_call(command, stdout=STDOUT, stderr=STDERR)
            libpath = None
        else:
            if os.path.exists(os.path.join('nuget-deps', name)):
                with indent(4):
                    puts('nuget folder detected. Skipping.')
            else:
                with indent(4):
                    puts('installing using nuget...')
                if version is None:
                    check_call(['nuget', 'install', name, '-OutputDirectory', 'nuget-deps', '-ExcludeVersion'])
                else:
                    check_call(['nuget', 'install', name, '-Version', version, '-OutputDirectory', 'nuget-deps', '-ExcludeVersion'])
            libpath = os.path.join("nuget-deps", name)
    return libpath, OWN
Example #18
0
File: init.py Project: ericdill/pim
def init(force):
    """
    This command initializes a folder with the typical contents of a Python package.
    After running this and writing your code, you should be ready to publish your package.
    """
    echo('\nThis utility will help you set up a new python module for publishing on PyPi!\n')
    echo('After answering a few questions, it will create a few files.')
    echo('\nPress ^C at any time to bail!\n')

    d = {}
    d['name'] = prompt.query('name:', default=os.getcwd().split('/')[-1])
    d['version'] = prompt.query('version:', default='1.0.0')
    gitauthor = retrieve(['git', 'config', '--get', 'user.username'], default=getpass.getuser())
    gitemail = retrieve(['git', 'config', '--get', 'user.email'], default=getpass.getuser() + '@gmail.com')
    d['author'] = prompt.query('author:', default=gitauthor)
    d['email'] = prompt.query('email:', default=gitemail)
    gitrepo = 'https://github.com/' + d['author'] + '/' + d['name']
    d['repository'] = prompt.query('repository:', default=gitrepo, validators=[])
    d['readme'] = prompt.query('readme:', default='README.md')
    d['license'] = prompt.query('license:', default='MIT')
    d['package'] = prompt.query('package name:', default=d['name'].replace('-','_'))
    d['entry'] = prompt.query('entry point:', default='main.py')
    d['description'] = prompt.query('description:', default='', validators=[])
    
    echo('\nReady to create the following files:')

    with indent(4, quote='  -'):
        puts('setup.py')
        puts('setup.cfg')
        puts('MANIFEST.in')
        puts(d['package'] + '/' + '__init__.py')
        puts(d['package'] + '/' + d['entry'])
        puts('requirements.txt')

    finalize = prompt.yn('\nSound like a plan?', default='y')

    if finalize:
        echo('')
        write('requirements.txt', force=force)
        write('setup.py', fields=d, force=force)
        write('setup.cfg', fields=d, stringify=False, force=force)
        write('MANIFEST.in', fields=d, stringify=False, force=force)
        write('__init__.py', fields=d, folder=d['package'], force=force)
        write(d['entry'], folder=d['package'], force=force)
        echo('')

    success('Your package is initialized!')
Example #19
0
    def forwarded_port(self, another_ports=None):
        """
        Forwarded ports allow you to access a port on your host
        machine and have all data forwarded to a port on the guest
        machine, over either TCP or UDP.

        https://docs.vagrantup.com/v2/networking/forwarded_ports.html
        """
        ports = another_ports if another_ports else []
        if prompt.yn('Create a forwarded port ?', default='y'):
            guest = prompt.query("Guest Port: ",
                                 validators=[IntegerValidator()])
            host = prompt.query("Host Port: ",
                                validators=[IntegerValidator()])
            data = {'quest': guest, 'host': host}
            ports.append(data)
            self.forwarded_port(another_ports=ports)
        self.content['forwarded_ports'] = ports
Example #20
0
def package(
        script_yes: bool,
        script_no: bool,
        wd: str,
        analyzers: Iterable[str],  # noqa: D401
        requirements_path: str,
        repo: str,
        user: str,
        token: str) -> Union[None, int]:  # noqa: D401,E501
    """
    Package several analyzers to a Docker container and write a sample Docker Compose config \
    for Lookout.

    :param script_yes: Force script execution in the end.
    :param script_no: Disable script execution in the end.
    :param wd: Working directory. All the files will be generated inside.
    :param analyzers: List of analyzer fully qualified module names.
    :param requirements_path: Path to requirements.txt. May be empty.
    :param repo: GitHub repo name to poll. E.g. "src-d/lookout".
    :param user: GitHub user name.
    :param token: GitHub user's token. Must have "repo" permissions. *not a password*
    :return: None or error code.
    """
    log = logging.getLogger("package")
    if script_yes and script_no:
        log.critical("Conflict between -y/--yes and -n/--no")
        return 1
    if os.path.exists(wd) and os.listdir(wd):
        log.critical("Not empty: %s", wd)
        return 2
    log.info("Preparing %s", wd)
    os.makedirs(wd, exist_ok=True)
    package_name = _process_analyzers(analyzers, wd, log)
    packages = _process_requirements(requirements_path,
                                     os.path.join(wd, "requirements.txt"), log)
    ndeps, ndeps_dev = _compose_native_deps(packages)
    _generate_configs(" ".join(analyzers), ndeps, ndeps_dev, package_name, wd,
                      repo, user, token, log)
    script = "cd %s\ndocker build -t %s .\ndocker-compose up --force-recreate" % (
        wd, package_name)
    log.info("These are your next steps:\n\n%s\n", script)
    if script_yes or (not script_no and
                      not prompt.yn("Run these commands now?", default="n")):
        os.execlp("sh", "-x", "-c", script.replace("\n", " && "))
Example #21
0
    def do_actions(self):
        puts('[%s] Task Start.' % (colored.yellow(self.taskid)))
        steps = self.options.get('steps', None)
        preset = self.options.get('preset', None)
        configuration = None

        extra = self.taskmanager.extra
        if extra is not None and 'configuration' in extra:
            configuration = extra.get('configuration')

        if configuration is None:
            return

        configuration.update(preset)

        for option in steps:
            key = option.get('key')
            if key in configuration.data:
                option.update({'default':configuration.data.get(key)})
            step = CommandReader(option)
            self.out.update(step.out)

        configuration.data.update(self.out)
        is_ok = prompt.yn(colored.red('Attention!') + ' The configuration will be saved. Are you sure?')
        if is_ok:
            configuration.save()
            puts('[%s]%s configuration is saved.' % (colored.yellow(self.taskid), colored.green('SUCCESS!')))

            configuration.update({
                'pkg_path': os.path.dirname(configuration.path)
            })
            self.taskmanager.out[self.taskid] = configuration.data

            puts('[%s] Task Finished.' % (colored.yellow(self.taskid)))
            if self.nextTask is not None:
                self.nextTask.do_actions()
        else:
            return
Example #22
0
def edit_default_ignore_list():
    puts(colored.green("Editing global ignore list files...\n"))

    default_path = CONFIG_DIR + "/odignore.txt"
    if os.path.isfile(default_path) and default_path not in user_conf.default_drive_config.ignore_files:
        puts(colored.yellow('Found default ignore list file "%s".' % default_path))
        if prompt.yn("Do you want to add this ignore list? "):
            user_conf.default_drive_config.ignore_files.add(default_path)

    with indent(4, quote=" >"):
        puts("To add a new ignore list file that is effective on all Drives, type the file path below.")
        puts("To remove an existing ignore list file, type a minus sign followed by the index of the path to delete.")
        puts('For example, type "-1" to delete the path indexed 1.')
        puts("To abort and return to main menu, hit [Ctrl+C].")

    try:
        while True:
            path_list = print_existing_ignore_list_paths()
            command = prompt.query("Please specify an absolute path or give a negated index: ")
            try:
                if len(command) > 0 and command[0] == "-":
                    index_to_delete = int(command[1:])
                    if index_to_delete < 0 or index_to_delete > len(path_list):
                        raise ValueError('Invalid path index "%d"' % index_to_delete)
                    path_to_delete = path_list[index_to_delete]
                    user_conf.default_drive_config.ignore_files.remove(path_to_delete)
                    puts(colored.green('Deleted path "%s" from list.' % path_to_delete))
                elif os.path.isfile(command):
                    user_conf.default_drive_config.ignore_files.add(command)
                    puts(colored.green('Added path "%s" to list.' % command))
                else:
                    raise ValueError('Path "%s" is not a file.' % command)
            except ValueError as ex:
                puts(colored.red("Error: " + str(ex)))
    except KeyboardInterrupt:
        puts(colored.green("Aborted."))
Example #23
0
lower = unicode.lower


def has_redundant_templates(page):
    """Checks if the page should be fixed by this bot."""
    text = lower(page.text)
    text = text[:text.find("==")]
    return any("{{" + template in text for template in REDUNDANT_TEMPLATES)


if args.count:
    num_edits = args.count
    print_log("Starting off with %d edits made." % num_edits)
else:
    num_edits = 0
for page in references_gen:
    if has_redundant_templates(page):
        print("About to process %s." %
              page.title(withNamespace=True).encode("utf-8"))
        page.text = process(page.text)
        if not args.interactive or prompt.yn(
                "Save %s?" % page.title(withNamespace=True).encode("utf-8")):
            page.save(summary=SUMMARY)
            num_edits += 1
            print_log("%d edits made so far." % num_edits)
            if args.limit and num_edits >= args.limit:
                print_log("%d edits (limit) reached; done." % num_edits)
                sys.exit(0)
        elif prompt.yn("Exit?"):
            sys.exit(0)
Example #24
0
def run_doctor():
    import distutils.spawn
    import importlib
    import platform
    import subprocess

    from clint.textui import (colored, prompt, puts, indent, validators)
    from halo import Halo
    from pyfiglet import Figlet

    def install_swipl(platform_os):
        if platform_os == "Darwin":
            try:
                stderr = sys.stderr
                sys.stderr = open(os.devnull, 'w')
                subprocess.check_output(["brew", "install", "swi-prolog"])
                sys.stderr = stderr
                return True
            except subprocess.CalledProcessError:
                return False

        if platform_os == "Windows":
            try:
                stderr = sys.stderr
                sys.stderr = open(os.devnull, 'w')
                subprocess.check_output(["choco", "install", "swi-prolog"])
                sys.stderr = stderr
                return True
            except subprocess.CalledProcessError:
                return False

        if platform_os == "Linux":
            try:
                stderr = sys.stderr
                sys.stderr = open(os.devnull, 'w')
                subprocess.check_output(["apt-get", "install", "swi-prolog"])
                sys.stderr = stderr
                return True
            except subprocess.CalledProcessError:
                return False

    HEADER = Figlet(font='slant').renderText('Doctor')

    #def find_all_python_interpreters():
    #    py = distutils.spawn.find_executable("python")
    #    py3 = distutils.spawn.find_executable("python3")
    #    return [py, py3]

    puts(HEADER)
    puts()

    #puts("Following Python interpreters are found")
    #interpreter_paths = find_all_python_interpreters()
    #options = [
    #    *[{"selector": no, "prompt": path, "return": path} for no, path in enumerate(interpreter_paths)],
    #    {"selector": len(interpreter_paths)+1, "prompt": "Select other interpreter", "return": "other"}
    #]

    #interpreter = prompt.options("Which python interpreter to use?", options)

    #if interpreter == "other":
    #    puts(colored.red("Please choose a python 3.7!"))
    #    interpreter = prompt.query('Installation Path of interpreter', validators=[validators.FileValidator()])

    platform_os = platform.system()

    puts("Running script from: {}".format(platform_os))

    if platform_os in ["Darwin", "Linux"]:
        with indent(4):
            puts(
                colored.yellow(
                    "Parts of this project will probably not run on this OS, please try to execute on Windows"
                ))

    puts()
    puts(colored.blue(BOLD_START + "Python Interpreter" + BOLD_END))
    puts()
    # Check if python 3.7 is installed
    #if sys.version_info < REQUIRED_PY:
    #    puts(colored.yellow("Doctor executed with python version < 3.7"))
    #    answer = prompt.yn("Do you still want to run doctor? (Packages will then be installed on current running interpreter", default="n")
    #
    #    if answer:
    #        sys.exit(1)

    # Check for site-packages

    puts("Checking Python site-packages...")
    all_installed = True
    print(REQUIREMENTS)
    with open(REQUIREMENTS) as f:
        requirements = f.readlines()
    requirements_name = [
        req.strip().split("==")[0] for req in requirements
        if not req.startswith("#")
    ]
    not_installed_packges = []

    for req in requirements_name:
        if req in PACKAGE_NAMES:
            req = PACKAGE_NAMES[req]
        try:
            # Import everything silently
            stderr = sys.stderr
            sys.stderr = open(os.devnull, 'w')
            importlib.import_module(req)
            sys.stderr = stderr
            with indent(4):
                puts(
                    colored.green("[{}] ".format(CHECKED_SYMBOL) +
                                  "{} already installed".format(req)))
        except ImportError as e:
            all_installed = False
            not_installed_packges.append(req)
            with indent(4):
                puts(
                    colored.red("[{}] ".format(FAILED_SYMBOL) +
                                "{} not installed".format(req)))

    if not all_installed:
        install_answer = prompt.yn("Installing all missing modules?",
                                   default="n")

        if not install_answer:
            for req in not_installed_packges:
                spinner = Halo(text="Installing package {}...".format(req),
                               spinner='dots')
                spinner.start()
                succesfull = install(req)
                spinner.stop()

                if succesfull:
                    puts(
                        colored.green(
                            "Installation of {} succesfully".format(req)))
                else:
                    puts(colored.red("Installation failed. Try it manually"))

    # Maybe Cuda

    # Client/Server

    puts()
    puts(colored.blue(BOLD_START + "Java" + BOLD_END))
    puts()
    # Check if java in path
    java_path = distutils.spawn.find_executable("java")

    if java_path:
        puts(
            colored.green("[{}] ".format(CHECKED_SYMBOL)) +
            "Java path found at: {}".format(java_path))
    else:
        puts(
            colored.red("[{}]".format(FAILED_SYMBOL)) +
            "Java in path not found")

    puts()
    puts(colored.blue(BOLD_START + "Swipl" + BOLD_END))
    puts()
    # Check if swipl insalled
    swiple_path = distutils.spawn.find_executable("swipl")

    if swiple_path:
        puts(
            colored.green("[{}] ".format(CHECKED_SYMBOL)) +
            "Swipl path found at: {}".format(swiple_path))
    else:
        puts(
            colored.red("[{}]".format(FAILED_SYMBOL)) +
            "Swipl in path not found")

        brew = distutils.spawn.find_executable("brew")

        if brew:
            puts()
            ans = prompt.yn("Should swipl be installed with package manager?",
                            default="n")
            if not ans:
                spinner = Halo(text="Installing swipl...")
                spinner.start()
                successfully = install_swipl(platform_os)
                spinner.stop()
                if successfully:
                    puts(
                        colored.green(
                            "[{}] Successfully installed swipl".format(
                                CHECKED_SYMBOL)))
                else:
                    puts(
                        colored.red("[] Could not install swipl".format(
                            FAILED_SYMBOL)))
            else:
                puts()
                puts(
                    "If swipl is not installed please use a package manager or manually install it"
                )
                puts("E.g")
                with indent(2):
                    puts("Windows: (Chocolatey)")
                    with indent(4):
                        puts("choco intall swi-prolog")
                    puts()
                    puts("Mac: (Homebrew)")
                    with indent(4):
                        puts("brew intall swi-prolog")
                    puts()
                    puts("Linux: (apt-get)")
                    with indent(4):
                        puts("apt-get intall swi-prolog")
Example #25
0
        response = requests.get(
            url="http://100.100.100.1/userRpm/SysRebootRpm.htm",
            params={
                "Reboot": "Rebbot",
            },
            auth=('admin', 'admin'),
        )
    except requests.exceptions.RequestException:
        puts(colored.red('HTTP Request failed, Router is not connected'))
        sys.exit()

    return response

if (args.get(0) == '-f'):
    force = True
else:
    force = prompt.yn(colored.yellow('Restart your Wi-Fi router ?'))

if force:
    puts(colored.magenta('Restaring your router'))
    status = reboot()
    # print status
    if s_message in status.text:
        puts(colored.green('Router is getting restarted, please wait ...'))
    else:
        puts(colored.red('Something is wrong, router is not restarted.'))
else:
    puts(colored.green('Restart aborted.'))
				
# send_request()
Example #26
0
def is_not_empty_package():
    if os.path.exists(os.path.join(os.getcwd(), DEFAULT_CONFIGURATION_FILE)):
        return not prompt.yn(colored.red('Attention!') + ' The configuration file is exist, this will override it. Are you sure?')
    else:
        return False
Example #27
0
def config(**kwargs):
    puts(colored.cyan("Create a new pgrepup config"))
    try:
        while True:
            conf_filename = prompt.query("Configuration filename",
                                         default=kwargs['c'])
            if os.path.isfile(os.path.expanduser(conf_filename)):
                if not prompt.yn(
                        "File %s exists " % conf_filename +
                        "and it'll be overwritten by the new configuration. Are you sure?",
                        default="n"):
                    # warning. prompt.yn return true if the user's answer is the same of default value
                    break
            else:
                break
    except KeyboardInterrupt:
        puts("\n")
        sys.exit(0)

    conf = create_config()

    puts(colored.cyan("Security"))
    conf.add_section("Security")
    if prompt.yn(
            "Do you want to encrypt database credentials using a password?",
            default="y"):
        conf.set("Security", "encrypted_credentials", "y")
        encrypt('')
        puts(
            "You'll be prompted for password every time pgrepup needs to connect to database"
        )
    else:
        conf.set("Security", "encrypted_credentials", "n")

    conf.set(
        "Security", "tmp_folder",
        prompt.query(
            "Folder where pgrepup store temporary dumps and pgpass file",
            "/tmp"))

    conf.set(
        "Security", "app_owner",
        prompt.query("Postgresql username as application owner", "app_owner"))

    puts(colored.cyan("Source Database configuration"))
    conf.add_section("Source")
    conf.set("Source", "host", prompt.query("Ip address or Dns name: "))
    conf.set("Source", "port", prompt.query("Port: "))
    conf.set("Source", "connect_database",
             prompt.query("Connect Database: ", default="template1"))
    conf.set("Source", "user", prompt.query("Username: "******"Source", "password", encrypt(pwd))

    puts(colored.cyan("Destination Database configuration"))
    conf.add_section("Destination")
    conf.set("Destination", "host", prompt.query("Ip address or Dns name: "))
    conf.set("Destination", "port", prompt.query("Port: "))
    conf.set("Destination", "connect_database",
             prompt.query("Connect Database: ", default="template1"))
    conf.set("Destination", "user", prompt.query("Username: "******"Destination", "password", encrypt(pwd))

    save_config(os.path.expanduser(conf_filename))
Example #28
0
def notify_people(people_to_notify, args, wiki):
    "Adds a message to people who ought to be notified about their DYK noms."

    # Check if there's anybody to notify
    if len(people_to_notify) == 0:
        print("Nobody to notify.")
        return

    # Do the notification
    people_notified = dict()

    def write_notified_people_to_file():
        """Update the file of notified people with people_notified."""
        this_month = datetime.now().strftime("%B %Y")
        with open(CONFIG.get("dyknotifier", "ALREADY_NOTIFIED_FILE")) as already_notified_file:
            try:
                already_notified = json.load(already_notified_file)
            except ValueError as error:
                if error.message != "No JSON object could be decoded":
                    raise
                else:
                    already_notified = {} # eh, we'll be writing to it anyway

            already_notified_this_month = already_notified.get(this_month, {})
            with open(CONFIG.get("dyknotifier", "ALREADY_NOTIFIED_FILE"), "w") as already_notified_file:
                usernames = set(already_notified_this_month.keys() +
                                people_notified.keys())
                for username in usernames:
                    already_notified_this_month[username] = list(set(
                        already_notified_this_month.get(username, []) +\
                        people_notified.get(username, [])))

                already_notified[this_month] = already_notified_this_month

                # Remove all data from more than a year ago
                a_year_ago = datetime.today() - timedelta(365)
                def not_too_old(month):
                    """True if the given month was less than a year ago."""
                    return datetime.strptime(month, "%B %Y") > a_year_ago
                already_notified = {k: v for k, v in already_notified.items()
                                    if not_too_old(k)}

                json.dump(already_notified, already_notified_file)

        print("Wrote %d people for %d nominations this month." %
              (len(already_notified_this_month),
               len(functools.reduce(operator.add,
                                    already_notified_this_month.values(),
                                    []))))

    notify_iter = zip(people_to_notify.items(),
                      reversed(range(len(people_to_notify))))
    for (person, nom_names), counter in notify_iter:
        if args.count:
            edits_made = len(functools.reduce(operator.add,
                                              people_notified.values(), []))
            if edits_made >= args.count:
                print("%d notified; exiting.", edits_made)
                write_notified_people_to_file()
                sys.exit(0)

        # Remove namespaces from the nom names.
        nom_names = [name[34:] for name in nom_names]

        # Format nom names into a string
        nom_names_string = "".join(name.encode("utf-8") for name in nom_names)

        if args.interactive:
            print("About to notify {} for {}.".format(person.encode("utf-8"),
                                                      nom_names_string))
            choice = raw_input("What (s[kip], c[ontinue], q[uit])? ")
            if choice[0] == "s":
                if prompt.yn("Because I've already notified them?"):
                    people_notified[person] = people_notified.get(
                        person, []) + nom_names
                print("Skipping " + person + ".")
                continue
            elif choice[0] == "q":
                print("Stop requested; exiting.")
                write_notified_people_to_file()
                sys.exit(0)
        talkpage = pywikibot.Page(wiki, title="User talk:" + person)
        try:
            summary = CONFIG.get("dyknotifier", "SUMMARY").format(nom_names_string)
            talkpage.save(appendtext=generate_message(nom_names, wiki),
                          comment=summary)
            print("Success! Notified %s because of %s. (%d left)" %
                  (person.encode("utf-8"), nom_names_string, counter))
            people_notified[person] = people_notified.get(person,
                                                          []) + nom_names
        except pywikibot.Error as error:
            print("Couldn't notify {} because of {} - result: {}".format(person, nom_names_string, str(error)))
        except (KeyboardInterrupt, SystemExit):
            write_notified_people_to_file()
            raise
        except UnicodeEncodeError as e:
            traceback.print_exc()
            print("Unicode encoding error notifiying {} about {}: {}".format(person.encode("utf-8"), nom_names_string, str(e)))

    write_notified_people_to_file()
Example #29
0
File: prompt.py Project: aeby/clint
#!/usr/bin/env python
# -*- coding: utf-8 -*-

import sys
import os

sys.path.insert(0, os.path.abspath('..'))

from clint.textui import prompt, puts, colored, validators

if __name__ == '__main__':
    # Standard non-empty input
    name = prompt.query("What's your name?")

    # Set validators to an empty list for an optional input
    language = prompt.query("Your favorite tool (optional)?", validators=[])

    # Use a default value and a validator
    path = prompt.query('Installation Path', default='/usr/local/bin/', validators=[validators.PathValidator()])

    # Use y/n choice
    if prompt.yn("Are you sure?"):
        puts(colored.blue('Hi {0}. Install {1} to {2}'.format(name, language or 'nothing', path)))
    else:
        puts(colored.blue('Bye {0}!').format(name))
Example #30
0
def notify_people(people_to_notify, args, wiki):
    "Adds a message to people who ought to be notified about their DYK noms."

    # Check if there's anybody to notify
    if len(people_to_notify) == 0:
        print("Nobody to notify.")
        return

    # Do the notification
    people_notified = dict()

    def write_notified_people_to_file():
        """Update the file of notified people with people_notified."""
        this_month = datetime.now().strftime("%B %Y")
        with open(CONFIG.get("dyknotifier", "ALREADY_NOTIFIED_FILE")) as already_notified_file:
            try:
                already_notified = json.load(already_notified_file)
            except ValueError as error:
                if error.message != "No JSON object could be decoded":
                    raise
                else:
                    already_notified = {} # eh, we'll be writing to it anyway

            already_notified_this_month = already_notified.get(this_month, {})
            with open(CONFIG.get("dyknotifier", "ALREADY_NOTIFIED_FILE"), "w") as already_notified_file:
                usernames = set(already_notified_this_month.keys() +
                                people_notified.keys())
                for username in usernames:
                    already_notified_this_month[username] = list(set(
                        already_notified_this_month.get(username, []) +\
                        people_notified.get(username, [])))

                already_notified[this_month] = already_notified_this_month

                # Remove all data from more than a year ago
                a_year_ago = datetime.today() - timedelta(365)
                def not_too_old(month):
                    """True if the given month was less than a year ago."""
                    return datetime.strptime(month, "%B %Y") > a_year_ago
                already_notified = {k: v for k, v in already_notified.items()
                                    if not_too_old(k)}

                json.dump(already_notified, already_notified_file)

        print("Wrote %d people for %d nominations this month." %
              (len(already_notified_this_month),
               len(functools.reduce(operator.add,
                                    already_notified_this_month.values(),
                                    []))))

    notify_iter = zip(people_to_notify.items(),
                      reversed(range(len(people_to_notify))))
    for (person, nom_names), counter in notify_iter:
        if args.count:
            edits_made = len(functools.reduce(operator.add,
                                              people_notified.values(), []))
            if edits_made >= args.count:
                print("%d notified; exiting.", edits_made)
                write_notified_people_to_file()
                sys.exit(0)

        # Remove namespaces from the nom names.
        nom_names = [name[34:] for name in nom_names]

        # Format nom names into a string
        nom_names_string = "".join(name.encode("utf-8") for name in nom_names)

        if args.interactive:
            print("About to notify {} for {}.".format(person.encode("utf-8"),
                                                      nom_names_string))
            choice = raw_input("What (s[kip], c[ontinue], q[uit])? ")
            if choice[0] == "s":
                if prompt.yn("Because I've already notified them?"):
                    people_notified[person] = people_notified.get(
                        person, []) + nom_names
                print("Skipping " + person + ".")
                continue
            elif choice[0] == "q":
                print("Stop requested; exiting.")
                write_notified_people_to_file()
                sys.exit(0)
        talkpage = pywikibot.Page(wiki, title="User talk:" + person)
        try:
            summary = CONFIG.get("dyknotifier", "SUMMARY").format(nom_names_string)
            talkpage.save(appendtext=generate_message(nom_names, wiki),
                          comment=summary)
            print("Success! Notified %s because of %s. (%d left)" %
                  (person.encode("utf-8"), nom_names_string, counter))
            people_notified[person] = people_notified.get(person,
                                                          []) + nom_names
        except pywikibot.Error as error:
            print("Couldn't notify {} because of {} - result: {}".format(person, nom_names_string, str(error)))
        except (KeyboardInterrupt, SystemExit):
            write_notified_people_to_file()
            raise
        except UnicodeEncodeError as e:
            traceback.print_exc()
            print("Unicode encoding error notifiying {} about {}: {}".format(person.encode("utf-8"), nom_names_string, str(e)))

    write_notified_people_to_file()
Example #31
0
# Set up refs gen
article_history = pywikibot.Page(site, "Template:Article history")
references_args = {"onlyTemplateInclusion": True, "namespaces": (1), "content": True}
references_gen = article_history.getReferences(**references_args)

lower = unicode.lower
def has_redundant_templates(page):
    """Checks if the page should be fixed by this bot."""
    text = lower(page.text)
    text = text[:text.find("==")]
    return any("{{" + template in text for template in REDUNDANT_TEMPLATES)

if args.count:
    num_edits = args.count
    print_log("Starting off with %d edits made." % num_edits)
else:
    num_edits = 0
for page in references_gen:
    if has_redundant_templates(page):
        print("About to process %s." % page.title(withNamespace=True).encode("utf-8"))
        page.text = process(page.text)
        if not args.interactive or prompt.yn("Save %s?" % page.title(withNamespace=True).encode("utf-8")):
            page.save(summary=SUMMARY)
            num_edits += 1
            print_log("%d edits made so far." % num_edits)
            if args.limit and num_edits >= args.limit:
                print_log("%d edits (limit) reached; done." % num_edits)
                sys.exit(0)
        elif prompt.yn("Exit?"):
            sys.exit(0)
Example #32
0
            url="http://100.100.100.1/userRpm/SysRebootRpm.htm",
            params={
                "Reboot": "Rebbot",
            },
            auth=('admin', 'admin'),
        )
    except requests.exceptions.RequestException:
        puts(colored.red('HTTP Request failed, Router is not connected'))
        sys.exit()

    return response


if (args.get(0) == '-f'):
    force = True
else:
    force = prompt.yn(colored.yellow('Restart your Wi-Fi router ?'))

if force:
    puts(colored.magenta('Restaring your router'))
    status = reboot()
    # print status
    if s_message in status.text:
        puts(colored.green('Router is getting restarted, please wait ...'))
    else:
        puts(colored.red('Something is wrong, router is not restarted.'))
else:
    puts(colored.green('Restart aborted.'))

# send_request()