Ejemplo n.º 1
0
    def execute(self):
        """Execute this finish command.
        """
        if self.branch.name == self.namespace.branch:
            exit("Already up-to-date.")

        configuration = self.git.get_configuration("branch",
                self.namespace.branch)
        branch = configuration.get("integration-branch",
                self.get_value("continuity", "integration-branch"))

        if branch == self.branch.name:
            try:
                self._merge_branch(self.namespace.branch,
                        *self.namespace.parameters)
                puts("Merged branch '{0}' into {1}.".format(
                    self.namespace.branch, self.branch.name))

                try:
                    self.git.delete_branch(self.namespace.branch)
                    puts("Deleted branch {0}.".format(self.namespace.branch))
                except GitException:
                    exit("conflict: Fix conflicts and then commit the result.")
            except GitException, error:
                paths = self.git.repo.index.unmerged_blobs()

                if paths:
                    for path in paths:
                        puts_err("Merge conflict: {0}".format(path))
                else:
                    puts_err(error.message)
                    exit(error.status)
Ejemplo n.º 2
0
Archivo: utils.py Proyecto: iNamja/mech
def add_box_file(name, version, filename, url=None, force=False, save=True):
    puts_err(colored.blue("Checking box '{}' integrity...".format(name)))

    if os.name == 'posix':
        proc = subprocess.Popen(['tar', '-tqf' if sys.platform.startswith('darwin') else '-tf', filename, '*.vmx'])
        valid_tar = not proc.wait()
    else:
        tar = tarfile.open(filename, 'r')
        files = tar.getnames()
        valid_tar = False
        for i in files:
            if i.endswith('vmx'):
                valid_tar = True
                break
            if i.startswith('/') or i.startswith('..'):
                puts_err(colored.red(textwrap.fill(
                    "This box is comprised of filenames starting with '/' or '..' "
                    "Exiting for the safety of your files."
                )))
                sys.exit(1)

    if valid_tar:
        if save:
            boxname = os.path.basename(url if url else filename)
            box = os.path.join(*filter(None, (HOME, 'boxes', name, version, boxname)))
            path = os.path.dirname(box)
            if not os.path.exists(path):
                os.makedirs(path)
            if not os.path.exists(box) or force:
                copyfile(filename, box)
        else:
            box = filename
        return name, version, box
Ejemplo n.º 3
0
 def get_unprocessed_items(self, want_types=None, force_all=False):
     """Return new items we found"""
     wt = self.source_conf.get('want_types', 'upload,favorite').split(',')
     j = self._get_data()
     
     n_items     = 0
     max_items   = self.source_conf.getint('max_items', None)
     
     for obj in j['data']:
         t = obj['type']
         if t in wt:
             # pp = pprint.PrettyPrinter(indent=4)
             # pp.pprint(obj)
             funk = self.TYPE_MAP.get(t, None)
             if funk:
                 for mci in funk(self, obj):
                     ctime_epoch = float( mci.created_time.strftime("%s") )
                     if force_all or not self.metadata_db.is_processed(mci.key):
                         if n_items < max_items or max_items is None:
                             yield mci
                             n_items += 1  # Counting feed items, not cc's
                         else:
                             puts(colored.red("Ignored (> max_items): %s" % mci))
                     else:
                         if self.verbose:
                             puts(colored.red("Already processed: %s" % mci))
                 if max_items:
                     if not force_all:
                         if n_items == max_items:
                             puts(colored.magenta("Reached max_items of %d for %s" % (max_items, self.source_name)))
                         
             else:
                 puts_err(colored.red("Don't know how to handle requested type '%s'" % t))
Ejemplo n.º 4
0
def main(args=None):
    if sys.version_info >= (3, 2) and sys.version_info < (3, 3):
        puts_err(colored.red("\nFor Python 3, Flask requires Python >= 3.3\n"))
        sys.exit(1)

    parser = setup_parser()
    opts = parser.parse_args(args=args)

    rc = 0

    try:
        opts.func(opts)
    except InsufficientPermissionsError as e:
        puts_err(colored.red("\nInsufficient permissions error:\n") + str(e) + "\n")
        rc = 1
    except BlockadeError as e:
        puts_err(colored.red("\nError:\n") + str(e) + "\n")
        rc = 1

    except KeyboardInterrupt:
        puts_err(colored.red("Caught Ctrl-C. exiting!"))

    except:
        puts_err(
            colored.red("\nUnexpected error! This may be a Blockade bug.\n"))
        traceback.print_exc()
        rc = 2

    sys.exit(rc)
Ejemplo n.º 5
0
def instances():
    makedirs(DATA_DIR)
    index_path = os.path.join(DATA_DIR, 'index')
    index_lock = os.path.join(DATA_DIR, 'index.lock')
    try:
        with FileLock(index_lock, timeout=3):
            updated = False
            if os.path.exists(index_path):
                with open(index_path) as fp:
                    instances = json.loads(uncomment(fp.read()))
                # prune unexistent Mechfiles
                for k in list(instances):
                    instance_data = instances[k]
                    path = instance_data and instance_data.get('path')
                    if not path or not os.path.exists(os.path.join(path, 'Mechfile')):
                        del instances[k]
                        updated = True
            else:
                instances = {}
            if updated:
                with open(index_path, 'w') as fp:
                    json.dump(instances, fp, sort_keys=True, indent=2, separators=(',', ': '))
            return instances
    except Timeout:
        puts_err(colored.red(textwrap.fill("Couldn't access index, it seems locked.")))
        sys.exit(1)
    except json.decoder.JSONDecodeError:
        puts_err(colored.red(textwrap.fill("Index file seems broken. Try to remove {}.".format(index_path))))
        sys.exit(1)
Ejemplo n.º 6
0
Archivo: utils.py Proyecto: jal2/mech
def init_box(name, version, force=False, save=True, requests_kwargs={}):
    if not os.path.exists('.mech'):
        name_version_box = add_box(name,
                                   name=name,
                                   version=version,
                                   force=force,
                                   save=save,
                                   requests_kwargs=requests_kwargs)
        if not name_version_box:
            return
        name, version, box = name_version_box
        # box = locate(os.path.join(*filter(None, (HOME, 'boxes', name, version))), '*.box')

        puts_err(colored.blue("Extracting box '{}'...".format(name)))
        os.makedirs('.mech')
        if os.name == 'posix':
            proc = subprocess.Popen(['tar', '-xf', box], cwd='.mech')
            if proc.wait():
                puts_err(colored.red("Cannot extract box"))
                sys.exit(1)
        else:
            tar = tarfile.open(box, 'r')
            tar.extractall('.mech')

        if not save and box.startswith(tempfile.gettempdir()):
            os.unlink(box)

    vmx = get_vmx()

    update_vmx(vmx)

    return vmx
Ejemplo n.º 7
0
def update_all():
    puts(colored.magenta('>> gem update'))
    success, _ = gem.update_all()
    if success:
        puts(colored.green('✔\n'))
    else:
        puts_err(colored.red('! failed to update gems\n'))
Ejemplo n.º 8
0
 def shell_action(self, action):
     """Run a shell command, interpolating (with escapes) vars for this item"""
     format_args = self._escaped_shell_vars()
     cmd = action.format(**format_args)
     # Why all this tempfile nonsense?  Becuase subprocess and `sh -c`
     # cannot be trusted to pass args as real args.  Best to run the action
     # as a script.
     with tempfile.NamedTemporaryFile() as script:
         script.write(bytes(cmd, 'utf8'))
         script.flush()
         with open(script.name, 'r') as f:
             puts("Will exec: %s" % f.read())
         with indent(2):
             # Not available until Python 3.5
             # cp = subprocess.run(
             #     ['/bin/sh', script.name],
             #     check=True,
             #     stdout=subprocess.PIPE, 
             #     stderr=subprocess.STDOUT
             # )
             
             try:
                 output = subprocess.check_output(
                     ['/bin/sh', script.name],
                     stderr=subprocess.STDOUT
                 )
             except subprocess.CalledProcessError as e:
                 puts_err(colored.red("Action returned error code %d" % e.returncode))
                 puts_err(e.output)
                 raise
             else:
                 puts("Output: %s" % output)
Ejemplo n.º 9
0
def initialise(arguments):
    verbose = arguments['--verbose']
    config_file = arguments['--config']

    if verbose:
        logging.basicConfig(level=logging.DEBUG)

    config = read_authentication(config_file or AUTH_FILE)
    if not config:
        # TODO: prompt for config and store
        pass

    username, password, account_id = tuple(config.get('auth', key)
        for key in config.options('auth'))

    server_parameters = dict(config.items('server-defaults'))
    righteous.init(username, password, account_id, **server_parameters)

    if righteous.login():
        cache_authentication(username, password, account_id,
            config_file or AUTH_FILE)
    else:
        puts_err(colored.red('Authentication failed'))
        exit(2)

    return verbose
Ejemplo n.º 10
0
def main(args=None):
    if sys.version_info >= (3, 2) and sys.version_info < (3, 3):
        puts_err(colored.red("\nFor Python 3, Flask requires Python >= 3.3\n"))
        sys.exit(1)

    parser = setup_parser()
    opts = parser.parse_args(args=args)

    rc = 0

    try:
        opts.func(opts)
    except InsufficientPermissionsError as e:
        puts_err(
            colored.red("\nInsufficient permissions error:\n") + str(e) + "\n")
        rc = 1
    except BlockadeError as e:
        puts_err(colored.red("\nError:\n") + str(e) + "\n")
        rc = 1

    except KeyboardInterrupt:
        puts_err(colored.red("Caught Ctrl-C. exiting!"))

    except:
        puts_err(
            colored.red("\nUnexpected error! This may be a Blockade bug.\n"))
        traceback.print_exc()
        rc = 2

    sys.exit(rc)
Ejemplo n.º 11
0
def get_current_name(exit_if_not_set=False):
    current_profile_name = environ[ENV.DOTFILES_PROFILE] if ENV.DOTFILES_PROFILE in environ else None
    if exit_if_not_set and not current_profile_name:
        puts_err(colored.red('! No dotfiles profile set ($%s). Complete the bootstrap setup first.' %
                             ENV.DOTFILES_PROFILE))
        exit(1)
    return current_profile_name
Ejemplo n.º 12
0
Archivo: utils.py Proyecto: toggit/mech
def update_vmx(path):
    updated = False

    vmx = parse_vmx(path)

    # Check if there is an existing interface
    has_network = False
    for vmx_key in vmx:
        if vmx_key.startswith('ethernet'):
            has_network = True

    # Write one if there is not
    if not has_network:
        vmx["ethernet0.addresstype"] = "generated"
        vmx["ethernet0.bsdname"] = "en0"
        vmx["ethernet0.connectiontype"] = "nat"
        vmx["ethernet0.displayname"] = "Ethernet"
        vmx["ethernet0.linkstatepropagation.enable"] = "FALSE"
        vmx["ethernet0.pcislotnumber"] = "32"
        vmx["ethernet0.present"] = "TRUE"
        vmx["ethernet0.virtualdev"] = "e1000"
        vmx["ethernet0.wakeonpcktrcv"] = "FALSE"
        puts_err(colored.yellow("Added network interface to vmx file"))
        updated = True

    if updated:
        with open(path, 'w') as new_vmx:
            for key in vmx:
                value = vmx[key]
                row = "{} = {}".format(key, value)
                new_vmx.write(row + os.linesep)
Ejemplo n.º 13
0
Archivo: utils.py Proyecto: toggit/mech
def settle_instance(instance_name, obj=None, force=False):
    makedirs(DATA_DIR)
    index_path = os.path.join(DATA_DIR, 'index')
    index_lock = os.path.join(DATA_DIR, 'index.lock')
    try:
        with FileLock(index_lock, timeout=3):
            updated = False
            if os.path.exists(index_path):
                with open(index_path) as fp:
                    instances = json.loads(uncomment(fp.read()))
                # prune unexistent Mechfiles
                for k in list(instances):
                    instance_data = instances[k]
                    path = instance_data and instance_data.get('path')
                    if not path or not os.path.exists(os.path.join(path, 'Mechfile')):
                        del instances[k]
                        updated = True
            else:
                instances = {}
            instance_data = instances.get(instance_name)
            if not instance_data or force:
                if obj:
                    instance_data = instances[instance_name] = obj
                    updated = True
                else:
                    instance_data = {}
            if updated:
                with open(index_path, 'w') as fp:
                    json.dump(instances, fp, sort_keys=True, indent=2, separators=(',', ': '))
            return instance_data
    except Timeout:
        puts_err(colored.red(textwrap.fill("Couldn't access index, it seems locked.")))
        sys.exit(1)
Ejemplo n.º 14
0
Archivo: mech.py Proyecto: toggit/mech
    def save(self, arguments):
        """
        Take a snapshot of the current state of the machine.

        Usage: mech snapshot save [options] <name> [<instance>]

        Notes:
            Take a snapshot of the current state of the machine. The snapshot
            can be restored via `mech snapshot restore` at any point in the
            future to get back to this exact machine state.

            Snapshots are useful for experimenting in a machine and being able
            to rollback quickly.

        Options:
            -f  --force                      Replace snapshot without confirmation
            -h, --help                       Print this help
        """
        name = arguments['<name>']

        instance_name = arguments['<instance>']
        instance_name = self.activate(instance_name)

        vmrun = VMrun(self.vmx, user=self.user, password=self.password)
        if vmrun.snapshot(name) is None:
            puts_err(colored.red("Cannot take snapshot"))
        else:
            puts_err(colored.green("Snapshot {} taken".format(name)))
Ejemplo n.º 15
0
def install_nvm():
    puts(colored.magenta('>> Installing Node Version Manager:'))
    p = subprocess.Popen(['/bin/bash', PATHS.SCRIPTS_CONFIGURE_ITERM2])
    return_code = p.wait()
    if return_code == 0:
        puts(colored.green('✔\n'))
    else:
        puts_err(colored.red('! failed to install nvm\n'))
Ejemplo n.º 16
0
def run_cleanups():
    _logger.debug("Running cleanup functions")
    try:
        get_host_exec().close()
    except:
        puts_err(
            colored.red("\nUnexpected error in cleanup! This may be a Blockade bug.\n"))
        traceback.print_exc()
Ejemplo n.º 17
0
def run_cleanups():
    _logger.debug("Running cleanup functions")
    try:
        get_host_exec().close()
    except:
        puts_err(
            colored.red("\nUnexpected error in cleanup! This may be a Blockade bug.\n"))
        traceback.print_exc()
Ejemplo n.º 18
0
def configure_iterm2():
    puts(colored.magenta('>> Configuring iterm2:'))
    p = subprocess.Popen(['/bin/bash', PATHS.SCRIPTS_CONFIGURE_ITERM2])
    return_code = p.wait()
    if return_code == 0:
        puts(colored.green('✔\n'))
    else:
        puts_err(colored.red('! failed to configure iterm2\n'))
Ejemplo n.º 19
0
def configure_system_settings():
    puts(colored.magenta('>> Configuring system settings:'))
    p = subprocess.Popen(
        ['/bin/bash', PATHS.SCRIPTS_CONFIGURE_SYSTEM_SETTINGS])
    return_code = p.wait()
    if return_code == 0:
        puts(colored.green('✔\n'))
    else:
        puts_err(colored.red('! failed to configure system settings\n'))
Ejemplo n.º 20
0
def install_fonts():
    puts(colored.magenta('>> Installing fonts:'))

    p = subprocess.Popen(['/bin/bash', PATHS.FONTS_INSTALL_SCRIPT])
    return_code = p.wait()
    if return_code == 0:
        puts(colored.green('✔\n'))
    else:
        puts_err(colored.red('! failed to install fonts\n'))
Ejemplo n.º 21
0
 def box_name(self):
     box_name = self.get('box')
     if not box_name:
         puts_err(
             colored.red(
                 textwrap.fill(
                     "Cannot find a box configured in the Mechfile")))
         sys.exit(1)
     return box_name
Ejemplo n.º 22
0
Archivo: utils.py Proyecto: iNamja/mech
def get_vmx():
    vmx = locate('.mech', '*.vmx')
    if not vmx:
        puts_err(colored.red("Cannot locate a VMX file"))
        sys.exit(1)

    if update_vmx(vmx):
        puts_err(colored.yellow("Added network interface to vmx file"))

    return vmx
Ejemplo n.º 23
0
Archivo: mech.py Proyecto: toggit/mech
    def push(self, arguments):
        """
        Deploys code in this environment to a configured destination.

        Usage: mech push [options] [<strategy>]

        Options:
            -h, --help                       Print this help
        """
        puts_err(colored.red("Not implemented!"))
Ejemplo n.º 24
0
 def result(self, entry_path, entry_password, is_match):
     puts_err("\r{}".format(LINE_CLEAR), newline=False)
     if is_match:
         puts(colored.red(
             "\r{} has a guessable password!".format(entry_path)),
              newline=False)
     else:
         puts(colored.green("\r{} looks OK".format(entry_path)),
              newline=False)
     puts("")
Ejemplo n.º 25
0
Archivo: utils.py Proyecto: toggit/mech
def add_mechfile(mechfile, name=None, version=None, force=False, save=True, requests_kwargs={}):
    url = mechfile.get('url')
    file = mechfile.get('file')
    name = mechfile.get('box')
    version = mechfile.get('box_version')
    if file:
        return add_box_file(name, version, file, force=force, save=save)
    if url:
        return add_box_url(name, version, url, force=force, save=save, requests_kwargs=requests_kwargs)
    puts_err(colored.red("Couldn't find a VMWare compatible VM for '{}'{}".format(name, " ({})".format(version) if version else "")))
Ejemplo n.º 26
0
def set_default(bundle_id, schemes):
    """Set the default bundle to handle a URI scheme"""
    for scheme in schemes:
        status = LSSetDefaultHandlerForURLScheme(scheme, bundle_id)
        if status == 0:
            puts('set handler for "{}" to "{}"'
                 .format(blue(scheme), green(bundle_id)))
        else:
            puts_err(red('An error occurred trying to set "{}" '
                         'to handle "{}"'.format(bundle_id, scheme)))
Ejemplo n.º 27
0
def install_gem(rake):
    puts(colored.magenta('>> gem install %s' % rake))
    success, _ = gem.install_gem(rake)

    if success:
        puts(colored.green('✔\n'))
        return True

    puts_err(colored.red('! failed to install: %s\n' % rake))
    return False
Ejemplo n.º 28
0
    def git(self):
        """Git accessor.
        """
        try:
            ret_val = GitService()
        except GitException:
            puts_err("fatal: Not a git repository.")
            exit(128)

        return ret_val
Ejemplo n.º 29
0
def set_default(bundle_id, schemes):
    """Set the default bundle to handle a URI scheme"""
    for scheme in schemes:
        status = LSSetDefaultHandlerForURLScheme(scheme, bundle_id)
        if status == 0:
            puts('set handler for "{}" to "{}"'.format(blue(scheme),
                                                       green(bundle_id)))
        else:
            puts_err(
                red('An error occurred trying to set "{}" '
                    'to handle "{}"'.format(bundle_id, scheme)))
Ejemplo n.º 30
0
Archivo: utils.py Proyecto: toggit/mech
def index_active_instance(instance_name):
    path = os.getcwd()
    instance = settle_instance(instance_name, {
        'path': path,
    })
    if instance.get('path') != path:
        puts_err(colored.red(textwrap.fill((
            "There is already a Mech box with the name '{}' at {}"
        ).format(instance_name, instance.get('path')))))
        sys.exit(1)
    return path
Ejemplo n.º 31
0
def delete(arguments):
    initialise(arguments)
    for environment in arguments['<environment>']:
        server = righteous.find_server(environment)
        success = righteous.delete_server(server['href'])
        if success:
            puts(colored.green('Successfully deleted %s @ %s' %
                (environment, server['href'])))
        else:
            puts_err(colored.magenta('Error deleting %s @ %s' %
                (environment, server['href'])))
Ejemplo n.º 32
0
Archivo: mech.py Proyecto: toggit/mech
    def restore(self, arguments):
        """
        Restore a snapshot taken previously with snapshot save.

        Usage: mech snapshot restore [options] <name> [<instance>]

        Options:
                --provision                  Enable provisioning
            -h, --help                       Print this help
        """
        puts_err(colored.red("Not implemented!"))
Ejemplo n.º 33
0
Archivo: mech.py Proyecto: iNamja/mech
    def reload(self, arguments):
        """
        Restarts mech machine, loads new mechfile configuration.

        Usage: mech reload [options] [<instance>]

        Options:
                --provision                  Enable provisioning
            -h, --help                       Print this help
        """
        puts_err(colored.red("Not implemented!"))
Ejemplo n.º 34
0
def main(args=None):
    if sys.version_info >= (3, 2) and sys.version_info < (3, 3):
        puts_err(colored.red("\nFor Python 3, Flask requires Python >= 3.3\n"))
        sys.exit(1)

    parser = setup_parser()
    opts = parser.parse_args(args=args)
    _setup_logging(opts)

    # register a signal handler to trigger graceful shutdown. The sys.exit
    # call will ultimately trigger the finally block below to run.
    # note that SIGINT is handled below in the normal flow of the try/except
    # block.
    def _signal_handler(*args):
        sys.exit()

    signal.signal(signal.SIGTERM, _signal_handler)

    rc = 0

    try:
        # don't bother pinging docker for a version command
        if opts.func != cmd_version:
            check_docker()

        opts.func(opts)

    except InsufficientPermissionsError as e:
        puts_err(
            colored.red("\nInsufficient permissions error:\n") + str(e) + "\n")
        rc = 1
    except EmbargoError as e:
        puts_err(colored.red("\nError:\n") + str(e) + "\n")
        rc = 1

    except KeyboardInterrupt:
        puts_err(colored.red("Caught Ctrl-C. exiting!"))

    except SystemExit:
        pass

    except AttributeError:
        parser.print_help()

    except:
        puts_err(
            colored.red("\nUnexpected error! This may be a Embargo bug.\n"))
        traceback.print_exc()
        rc = 2

    finally:
        run_cleanups()

    sys.exit(rc)
Ejemplo n.º 35
0
Archivo: mech.py Proyecto: iNamja/mech
    def port(self, arguments):
        """
        Displays information about guest port mappings.

        Usage: mech port [options] [<instance>]

        Options:
                --guest PORT                 Output the host port that maps to the given guest port
                --machine-readable           Display machine-readable output
            -h, --help                       Print this help
        """
        puts_err(colored.red("Not implemented!"))
Ejemplo n.º 36
0
def subscribe(config, repo):
    with indent(3, quote=':: '):
        puts('Subscribing to repository...')
    with indent(1):
        puts(colored.magenta(repo))
    
        repos = config['repos']
        if repo in repos:
            puts_err(colored.red(
                'You\'re already subscribed to this repository!'))
        else:
            repos.append(repo)
Ejemplo n.º 37
0
def dotfiles_private_pull_latest():
    dotfiles_private_exit_if_master_branch_not_checked_out()
    dotfiles_private_exit_if_has_changes_staged()

    puts(colored.magenta('>> Pulling latest dotfiles_private changes:'))
    success, return_code = git.dotfiles_private_pull_latest()
    if success:
        puts(colored.green('✔\n'))
        return True

    puts_err(colored.red('! git pull failed'))
    exit(return_code)
Ejemplo n.º 38
0
def fetch(remote_name, exit_on_error=False):
    puts(colored.magenta('>> git fetch %s' % remote_name))
    success, return_code = git.fetch(remote_name)
    if success:
        puts(colored.green('✔\n'))
        return True

    puts_err(colored.red('! failed: git fetch %s' % remote_name))
    if exit_on_error:
        exit(return_code)
    else:
        return False
Ejemplo n.º 39
0
def clone(url, dest_dirname, cwd=None, exit_on_error=False):
    puts(colored.magenta('>> git clone %s %s' % (url, dest_dirname)))
    success, return_code = git.clone(url, dest_dirname, cwd)
    if success:
        puts(colored.green('✔\n'))
        return True

    puts_err(colored.red('! failed: git clone %s %s' % (url, dest_dirname)))
    if exit_on_error:
        exit(return_code)
    else:
        return False
Ejemplo n.º 40
0
Archivo: mech.py Proyecto: toggit/mech
    def pop(self, arguments):
        """
        Restore state that was pushed with `mech snapshot push`.

        Usage: mech snapshot pop [options] [<instance>]

        Options:
                --provision                  Enable provisioning
                --no-delete                  Don't delete the snapshot after the restore
            -h, --help                       Print this help
        """
        puts_err(colored.red("Not implemented!"))
Ejemplo n.º 41
0
def add_remote(name, url, cwd=None, exit_on_error=False):
    puts(colored.magenta('>> git remote add %s %s' % (name, url)))
    success, return_code = git.add_remote(name, url, cwd=cwd)
    if success:
        puts(colored.green('✔\n'))
        return True

    puts_err(colored.red('! failed: git remote add %s %s' % (name, url)))
    if exit_on_error:
        exit(return_code)
    else:
        return False
Ejemplo n.º 42
0
    def scp(self, arguments):
        """
        Copies files to and from the machine via SCP.

        Usage: mech scp [options] <src> <dst> [-- <extra scp args>...]

        Options:
            -h, --help                       Print this help
        """
        extra = arguments['<extra scp args>']
        src = arguments['<src>']
        dst = arguments['<dst>']

        dst_instance, dst_is_host, dst = dst.partition(':')
        src_instance, src_is_host, src = src.partition(':')

        if dst_is_host and src_is_host:
            puts_err(colored.red("Both src and host are host destinations"))
            sys.exit(1)
        if dst_is_host:
            instance_name = dst_instance
        else:
            dst = dst_instance
        if src_is_host:
            instance_name = src_instance
        else:
            src = src_instance

        instance_name = self.activate(instance_name)

        config_ssh = self.config_ssh
        fp = tempfile.NamedTemporaryFile(delete=False)
        try:
            fp.write(utils.config_ssh_string(config_ssh))
            fp.close()

            cmds = ['scp']
            cmds.extend(('-F', fp.name))
            if extra:
                cmds.extend(extra)

            host = config_ssh['Host']
            dst = '{}:{}'.format(host, dst) if dst_is_host else dst
            src = '{}:{}'.format(host, src) if src_is_host else src
            cmds.extend((src, dst))

            logger.debug(" ".join(
                "'{}'".format(c.replace("'", "\\'")) if ' ' in c else c
                for c in cmds))
            return subprocess.call(cmds)
        finally:
            os.unlink(fp.name)
Ejemplo n.º 43
0
Archivo: mech.py Proyecto: toggit/mech
    def remove(self, arguments):
        """
        Remove a box from mech that matches the given name.

        Usage: mech box remove [options] <name>

        Options:
            -f, --force                      Remove without confirmation.
                --box-version VERSION        The specific version of the box to remove
                --all                        Remove all available versions of the box
            -h, --help                       Print this help
        """
        puts_err(colored.red("Not implemented!"))
Ejemplo n.º 44
0
def main(args=None):
    if sys.version_info >= (3, 2) and sys.version_info < (3, 3):
        puts_err(colored.red("\nFor Python 3, Flask requires Python >= 3.3\n"))
        sys.exit(1)

    parser = setup_parser()
    opts = parser.parse_args(args=args)
    _setup_logging(opts)

    # register a signal handler to trigger graceful shutdown. The sys.exit
    # call will ultimately trigger the finally block below to run.
    # note that SIGINT is handled below in the normal flow of the try/except
    # block.
    def _signal_handler(*args):
        sys.exit()

    signal.signal(signal.SIGTERM, _signal_handler)

    rc = 0

    try:

        # don't bother pinging docker for a version command
        if opts.func != cmd_version:
            check_docker()

        opts.func(opts)
    except InsufficientPermissionsError as e:
        puts_err(colored.red(
                 "\nInsufficient permissions error:\n") + str(e) + "\n")
        rc = 1
    except BlockadeError as e:
        puts_err(colored.red("\nError:\n") + str(e) + "\n")
        rc = 1

    except KeyboardInterrupt:
        puts_err(colored.red("Caught Ctrl-C. exiting!"))

    except SystemExit:
        pass

    except:
        puts_err(
            colored.red("\nUnexpected error! This may be a Blockade bug.\n"))
        traceback.print_exc()
        rc = 2

    finally:
        run_cleanups()

    sys.exit(rc)
Ejemplo n.º 45
0
def calc_coverage(bamfile, regions=None, mtchr=None):
    depths = []
    for region in regions:
        output_dir = OrderedDict()
        if type(region) == Interval:
            # Add one to start as starts are 0 based; ends are 1 based.
            chrom, start, end = str(region.chrom), region.start + 1, region.stop
            output_dir["name"] = region.name
        else:
            chrom, start, end = re.split("[:-]", region)
            start, end = int(start), int(end)
        output_dir["chrom"] = chrom
        output_dir["start"] = start
        output_dir["end"] = end

        # If end extends to far, adjust for chrom
        chrom_len = bamfile.lengths[bamfile.gettid(chrom)]
        if end > chrom_len:
            with indent(4):
                puts_err(
                    colored.yellow(
                        "\nSpecified chromosome end extends beyond chromosome length. Set to max of: "
                        + str(chrom_len)
                        + "\n"
                    )
                )
                end = chrom_len

        region = bamfile.pileup(chrom, start, end + 1, truncate=True, max_depth=1e8)
        cum_depth = 0
        pos_covered = 0
        for n, i in enumerate(region):
            pos_covered += 1
            cum_depth += i.nsegments
        length = end - start + 1
        coverage = cum_depth / float(length)
        breadth = pos_covered / float(length)
        output_dir["ATTR"] = "bases_mapped"
        print eav(bam_name, output_dir, cum_depth, args["--tsv"], args["--header"])
        output_dir["ATTR"] = "depth_of_coverage"
        print eav(bam_name, output_dir, coverage, args["--tsv"])
        output_dir["ATTR"] = "breadth_of_coverage"
        print eav(bam_name, output_dir, breadth, args["--tsv"])
        output_dir["ATTR"] = "length"
        print eav(bam_name, output_dir, length, args["--tsv"])
        output_dir["ATTR"] = "pos_mapped"
        print eav(bam_name, output_dir, pos_covered, args["--tsv"])
        depths.append(
            {"chrom": chrom, "bases_mapped": cum_depth, "pos_covered": pos_covered, "depth_of_coverage": coverage}
        )
    return depths
Ejemplo n.º 46
0
def help(services=None, methods=None):
    puts_err("Usage: sunlight <service> <method> [<args>, ...]")

    if services:
        puts_err("Available services:")
        with indent(4):
            for s in services:
                puts_err(s)

    if methods:
        puts_err("Available methods:")
        with indent(4):
            for m in methods:
                puts_err(m)
Ejemplo n.º 47
0
    def create(self, package):
        textui.puts("Creating package %s" % package)
        destination = join(os.getcwd(), package)
        if exists(destination):
            textui.puts_err("Package already exists!")
            return

        module_dir = join(destination, package.replace(".", "/"))
        os.makedirs(module_dir)

        if package.find(".") is not -1:
            self.add_default_inits(destination, package)

        self.copy_files_into_location(module_dir, destination)
Ejemplo n.º 48
0
def set_configs(configs, cwd=None, exit_on_error=False):
    puts(colored.magenta('>> Configuring git repo: %s' % cwd or ''))
    for key, value in configs.items():
        success, _ = git.set_config(key, value, cwd=cwd)
        if success:
            puts('%s=%s' % (key, value))
        else:
            puts_err(colored.red('! failed: git config %s %s' % (key, value)))
            if exit_on_error:
                exit(1)
            else:
                return False
    puts(colored.green('✔\n'))
    return True
Ejemplo n.º 49
0
    def create(self, package):
        textui.puts("Creating package %s" % package)
        destination = join(os.getcwd(), package)
        if exists(destination):
            textui.puts_err("Package already exists!")
            return

        module_dir = join(destination, package.replace(".", "/"))
        os.makedirs(module_dir)

        if package.find(".") is not -1:
            self.add_default_inits(destination, package)

        self.copy_files_into_location(module_dir, destination)
Ejemplo n.º 50
0
 def summary(self, results):
     puts_err("\r{}".format(LINE_CLEAR), newline=False)
     if len(results) == 0:
         puts(
             colored.green(
                 "\r0/{} of your passwords are trivially guessable!".format(
                     self.entry_count)))
     else:
         puts(
             colored.red(
                 "\r{}/{} of your passwords are trivially guessable:".
                 format(len(results), self.entry_count)))
         for result in results:
             puts(result[0])
Ejemplo n.º 51
0
def remove_remote(name, exit_on_error=False):
    exit_if_cwd_not_git_repo()

    puts(colored.magenta('>> git remote rm %s' % name))
    success, return_code = git.remove_remote(name)
    if success:
        puts(colored.green('✔\n'))
        return True

    puts_err(colored.red('! failed: git remote rm %s' % name))
    if exit_on_error:
        exit(return_code)
    else:
        return False
Ejemplo n.º 52
0
def main(args=None):
    parser = setup_parser()
    opts = parser.parse_args(args=args)

    rc = 0

    try:
        opts.func(opts)
    except InsufficientPermissionsError as e:
        puts_err(colored.red("\nInsufficient permissions error:\n") + str(e) + "\n")
        rc = 1
    except BlockadeError as e:
        puts_err(colored.red("\nError:\n") + str(e) + "\n")
        rc = 1

    except KeyboardInterrupt:
        puts_err(colored.red("Caught Ctrl-C. exiting!"))

    except:
        puts_err(
            colored.red("\nUnexpected error! This may be a Blockade bug.\n"))
        traceback.print_exc()
        rc = 2

    sys.exit(rc)
Ejemplo n.º 53
0
Archivo: mech.py Proyecto: toggit/mech
    def repackage(self, arguments):
        """
        Repackage the box that is in use in the current mech environment.

        Usage: mech box repackage [options] <name> <version>

        Notes:
            Puts it in the current directory so you can redistribute it.
            The name and version of the box can be retrieved using mech box list.

        Options:
            -h, --help                       Print this help
        """
        puts_err(colored.red("Not implemented!"))
Ejemplo n.º 54
0
def help(services=None, methods=None):
    puts_err("Usage: sunlight <service> <method> [<args>, ...]")

    if services:
        puts_err("Available services:")
        with indent(4):
            for s in services:
                puts_err(s)

    if methods:
        puts_err("Available methods:")
        with indent(4):
            for m in methods:
                puts_err(m)
Ejemplo n.º 55
0
def multiWellington(regions, reads, **kwargs):
    p = mp.Pool(CPUs)
    for i in progress.bar(regions):
        if clargs.one_dimension:
            fp = footprinting.wellington1D(i, reads, **kwargs)
        else:
            fp = footprinting.wellington(i, reads, **kwargs)
        p.apply_async(fp, callback=writetodisk)
        # Hold here while the queue is bigger than the number of reads we're happy to store in memory
        while p._taskqueue.qsize() > max_regions_cached_in_memory:
            pass
    p.close()
    puts_err("Waiting for the last {0} jobs to finish...".format(max_regions_cached_in_memory))
    p.join()
Ejemplo n.º 56
0
Archivo: utils.py Proyecto: toggit/mech
def catalog_to_mechfile(catalog, name=None, version=None):
    mechfile = {}
    versions = catalog.get('versions', [])
    for v in versions:
        current_version = v['version']
        if not version or current_version == version:
            for provider in v['providers']:
                if 'vmware' in provider['name']:
                    mechfile['box'] = catalog['name']
                    mechfile['box_version'] = current_version
                    mechfile['url'] = provider['url']
                    return mechfile
    puts_err(colored.red("Couldn't find a VMWare compatible VM for '{}'{}".format(name, " ({})".format(version) if version else "")))
    sys.exit(1)
Ejemplo n.º 57
0
def main(debug=None):
    args = docopt(__doc__,
                  version=__version__,
                  argv=debug)

    if args["<vcf>"] == "":
        print(__doc__)

    wz = large_int(args["<window-size>"])
    sz = None
    if not args["--sliding"]:
        sz = large_int(args["<step-size>"])

    if wz < sz:
        exit(puts_err(colored.red("\n\tWindow size must be >= step size.\n")))

    if args["--no-header"] is False:
        header_line = ["CHROM",
                       "BIN_START",
                       "BIN_END",
                       "N_Sites",
                       "N_SNPs",
                       "TajimaD"]
        if args["--extra"]:
            header_line += ["filename",
                            "window_size",
                            "step_size"]
        print "\t".join(header_line)
    for i in tajima(args["<vcf>"]).calc_tajima(wz, sz, args["--sliding"], extra=args["--extra"]):
        print(i)
Ejemplo n.º 58
0
def stop(arguments):
    initialise(arguments)

    for environment in arguments['<environment>']:
        answer = raw_input('Confirm decommission of %s [Y/n] ' % environment)
        if answer in ['n', 'no']:
            continue

        server = righteous.find_server(environment)
        success = righteous.stop_server(server['href'])
        if success:
            puts(colored.cyan('Initiated decommission of %s @ %s' %
                (environment, server['href'])))
        else:
            puts_err(colored.magenta('Error stopping server %s @ %s' %
                (environment, server['href'])))
Ejemplo n.º 59
0
    def __init__(self, reference, method="pcr"):
        self.reference = reference
        self.PRIMER_GC_CLAMP = 1
        self.PRIMER_OPT_SIZE = 20
        self.PRIMER_MIN_SIZE = 18  # Must be set
        self.PRIMER_MAX_SIZE = 20  # Must be set
        self.PRIMER_NUM_RETURN = 5
        self.PRIMER_PRODUCT_SIZE_RANGE = "600-800"
        
        if method == "pcr":
            seq_template_length = self.PRIMER_PRODUCT_SIZE_RANGE.split("-")[1]
            self.seq_template_length = int(seq_template_length)
            self.PRIMER_TASK = "pick_pcr_primers"
            self.generate_pcr_template = True

        # Global default
        thermo_paths = ["/usr/local/share/primer3_config/",
                        "/usr/local/share/primer3/primer3_config/",
                        "~/.linuxbrew/share/primer3_config/",
                        "~/.linuxbrew/share/primer3/primer3_config/",
                        "/.linuxbrew/share/primer3_config/",
                        "/.linuxbrew/share/primer3/primer3_config/",
                        primer3_config]
        paths = filter(lambda x: os.path.exists(os.path.expanduser(x)), thermo_paths)
        if len(paths) == 0:
            with indent(4):
                exit(puts_err(colored.red("\nCannot find thermo path '/primer3_config/\n")))
        thermo_path = paths[0]
        self.PRIMER_THERMODYNAMIC_PARAMETERS_PATH = thermo_path

        # Set primer returns:
        if self.PRIMER_TASK == "pick_left_only":
            self.left_or_right = ["PRIMER_LEFT"]
        else:
            self.left_or_right = ["PRIMER_LEFT", "PRIMER_RIGHT"]