def main():
    """Parses the command line arguments and performs a search for the
    given email address in the user map.
    """
    # Set up argument parsing.
    parser = p4gf_util.create_arg_parser(
        "searches for an email address in the user map")
    parser.add_argument('email', metavar='E',
                        help='email address to find')
    args = parser.parse_args()

    # make sure the world is sane
    ec = p4gf_init.main()
    if ec:
        print("p4gf_usermap initialization failed")
        sys.exit(ec)

    p4 = connect_p4(client=p4gf_util.get_object_client_name())
    if not p4:
        sys.exit(1)

    usermap = UserMap(p4)
    user = usermap.lookup_by_email(args.email)
    if user:
        print("Found user {} <{}>".format(user[0], user[2]))
        sys.exit(0)
    else:
        sys.stderr.write("No such user found.\n")
        sys.exit(1)
Exemplo n.º 2
0
def read_user_map(p4):
    """Reads the user map file from Perforce into a list of tuples,
    consisting of username, email address, and full name. If no
    such file exists, an empty list is returned.

    Returns a list of 3-tuples: (p4user, email, fullname)
    """
    usermap = [(key, value['Email'], value['FullName'])
               for key, value in local_users.items() if 'Email' in value]
    client = p4.fetch_client(p4gf_util.get_object_client_name())
    mappath = client['Root'] + '/users/p4gf_usermap'
    # don't let a writable usermap file get in our way
    p4.run('sync', '-fq', mappath)
    if os.path.exists(mappath):
        regex = re.compile('([^ \t]+)[ \t]+([^ \t]+)[ \t]+"([^"]+)"')
        with open(mappath) as mf:
            for line in mf:
                if line:
                    line = line.strip()
                    if line and line[0] != '#':
                        m = regex.search(line)
                        if m:
                            usermap.append(
                                (m.group(1), m.group(2), m.group(3)))
    return usermap
def delete_all_local(args, p4, metrics):
    """Remove "everything" as if from a read-only Git Fusion instance.

    :param args: parsed command line arguments
    :param p4: Git user's Perforce client
    :param metrics: for counting delete actions.

    Similar to deleting everything from the master server, except that very
    little is removed from the Perforce server (e.g. counters and files).
    In short, only the client and local directories are removed.

    """
    p4.user = p4gf_const.P4GF_USER
    print(_('Connected to {P4PORT}').format(P4PORT=p4.port))
    client_name = p4gf_util.get_object_client_name()
    localroot = get_p4gf_localroot(p4)
    if not args.delete:
        if localroot:
            if args.no_obliterate:
                print(NTR('p4 sync -f {}...#none').format(localroot))
            else:
                print(NTR('p4 client -f -d {}').format(client_name))
                print(NTR('rm -rf {}').format(localroot))
    else:
        if localroot:
            if not args.no_obliterate:
                # Need this in order to use --gc later on
                p4gf_util.p4_client_df(p4, client_name)
                metrics.clients += 1
                print_verbose(
                    args,
                    _("Deleting client '{client_name}'s workspace...").format(
                        client_name=client_name))
                _remove_local_root(localroot)
Exemplo n.º 4
0
def main():
    """
    Parses the command line arguments and performs a search for the given
    email address in the user map.
    """
    p4gf_util.has_server_id_or_exit()
    log_l10n()

    # Set up argument parsing.
    parser = p4gf_util.create_arg_parser(
        _("Searches for an email address in the user map."))
    parser.add_argument(NTR('email'), metavar='E',
                        help=_('email address to find'))
    args = parser.parse_args()

    # make sure the world is sane
    ec = p4gf_init.main()
    if ec:
        print(_("p4gf_usermap initialization failed"))
        sys.exit(ec)

    with p4gf_create_p4.Closer():
        p4 = p4gf_create_p4.create_p4(client=p4gf_util.get_object_client_name())
        if not p4:
            sys.exit(1)

        usermap = UserMap(p4)
        user = usermap.lookup_by_email(args.email)
        if user:
            print(_("Found user '{}' <{}>").format(user[0], user[2]))
            sys.exit(0)
        else:
            sys.stderr.write(_("No such user found: '{}'\n").format(args.email))
            sys.exit(1)
Exemplo n.º 5
0
def main():
    '''
    Parse the command-line arguments and print a configuration.
    '''
    p4gf_util.has_server_id_or_exit()
    p4gf_client = p4gf_util.get_object_client_name()
    p4 = p4gf_create_p4.create_p4(client=p4gf_client)
    if not p4:
        sys.exit(1)
    desc = _("""Display the effective global or repository configuration.
All comment lines are elided and formatting is normalized per the
default behavior of the configparser Python module.
The default configuration options will be produced if either of the
configuration files is missing.
""")
    parser = p4gf_util.create_arg_parser(desc=desc)
    parser.add_argument(NTR('repo'), metavar=NTR('R'), nargs='?', default='',
        help=_('name of the repository, or none to display global.'))
    args = parser.parse_args()
    if args.repo:
        cfg = get_repo(p4, args.repo)
    else:
        cfg = get_global(p4)
    if not cfg:
        print(_('Unable to read configuration file!'))
    cfg.write(sys.stdout)
Exemplo n.º 6
0
def pacemaker(view_name, event):
    """
    As long as event flag is clear, update heartbeat of named lock.
    """
    # Running in a separate process, need to establish our own P4 connection
    # and set up a heartbeat-only lock to update the heartbeat of the lock
    # associated with the view.
    p4gf_proc.install_stack_dumper()
    LOG.getChild("pacemaker").debug("starting for lock {}".format(view_name))
    p4 = None
    try:
        p4 = p4gf_create_p4.create_p4(client=p4gf_util.get_object_client_name(), connect=False)
        lock = CounterLock(p4, view_name, heartbeat_only=True)
        while not event.is_set():
            with p4gf_create_p4.Connector(p4):
                lock.update_heartbeat()
            event.wait(HEART_RATE)
    # pylint: disable=W0703
    # Catching too general exception
    except Exception as e:
        LOG.getChild("pacemaker").error("error occurred: {}".format(str(e)))
    finally:
        LOG.getChild("pacemaker").debug("stopping for view {}".format(view_name))
        if p4:
            p4gf_create_p4.destroy(p4)
Exemplo n.º 7
0
def init(p4):
    """Ensure both global and host-specific initialization are completed.
    """
    started_counter = p4gf_const.P4GF_COUNTER_INIT_STARTED
    complete_counter = p4gf_const.P4GF_COUNTER_INIT_COMPLETE
    _maybe_perform_init(p4, started_counter, complete_counter, _global_init)
    client_name = p4gf_util.get_object_client_name()
    started_counter = client_name + '-init-started'
    complete_counter = client_name + '-init-complete'
    home = os.environ.get("HOME")
    p4gf_dir = os.path.join(home, p4gf_const.P4GF_DIR)

    def client_init(p4):
        '''Perform host-specific initialization (and create sample usermap).'''
        # Set up the host-specific client.
        _create_client(p4, client_name, p4gf_dir)
        # Ensure the default user map file is in place.
        _write_user_map(p4, client_name, p4gf_dir)
    if not _maybe_perform_init(p4, started_counter, complete_counter, client_init):
        # If client already created, make sure it hasn't been tweaked.
        ###: do we really need to handle this case? this is here just to pass the tests
        view = ['//{depot}/... //{client}/...'.format(depot=p4gf_const.P4GF_DEPOT,
                client=client_name)]
        p4gf_util.ensure_spec_values(p4, "client", client_name,
                {'Root': p4gf_dir, 'View': view})
Exemplo n.º 8
0
def main():
    """Parses the command line arguments and performs a search for the
    given email address in the user map.
    """
    # Set up argument parsing.
    parser = p4gf_util.create_arg_parser(
        "searches for an email address in the user map")
    parser.add_argument('email', metavar='E', help='email address to find')
    args = parser.parse_args()

    # make sure the world is sane
    ec = p4gf_init.main()
    if ec:
        print("p4gf_usermap initialization failed")
        sys.exit(ec)

    p4 = connect_p4(client=p4gf_util.get_object_client_name())
    if not p4:
        sys.exit(1)

    usermap = UserMap(p4)
    user = usermap.lookup_by_email(args.email)
    if user:
        print("Found user {} <{}>".format(user[0], user[2]))
        sys.exit(0)
    else:
        sys.stderr.write("No such user found.\n")
        sys.exit(1)
Exemplo n.º 9
0
def main():
    '''
    Parse the command-line arguments and print a configuration.
    '''
    p4gf_util.has_server_id_or_exit()
    p4gf_client = p4gf_util.get_object_client_name()
    p4 = p4gf_create_p4.create_p4(client=p4gf_client)
    if not p4:
        sys.exit(1)
    desc = _("""Display the effective global or repository configuration.
All comment lines are elided and formatting is normalized per the
default behavior of the configparser Python module.
The default configuration options will be produced if either of the
configuration files is missing.
""")
    parser = p4gf_util.create_arg_parser(desc=desc)
    parser.add_argument(
        NTR('repo'),
        metavar=NTR('R'),
        nargs='?',
        default='',
        help=_('name of the repository, or none to display global.'))
    args = parser.parse_args()
    if args.repo:
        cfg = get_repo(p4, args.repo)
    else:
        cfg = get_global(p4)
    if not cfg:
        print(_('Unable to read configuration file!'))
    cfg.write(sys.stdout)
Exemplo n.º 10
0
def init(p4):
    """Ensure both global and host-specific initialization are completed.
    """
    started_counter = p4gf_const.P4GF_COUNTER_INIT_STARTED
    complete_counter = p4gf_const.P4GF_COUNTER_INIT_COMPLETE
    if not old_counter_present(p4):
        Verbosity.report(Verbosity.DEBUG, _('Old 2012.2 counter not present.'))
    if not _maybe_perform_init(p4, started_counter, complete_counter,
                               _global_init):
        Verbosity.report(Verbosity.INFO,
                         _('Permissions already initialized. Not changing.'))
    server_id = p4gf_util.get_server_id()
    started_counter = p4gf_const.P4GF_COUNTER_INIT_STARTED + '-' + server_id
    complete_counter = p4gf_const.P4GF_COUNTER_INIT_COMPLETE + '-' + server_id
    p4gf_dir = p4gf_const.P4GF_HOME
    client_name = p4gf_util.get_object_client_name()

    def client_init(p4):
        '''Perform host-specific initialization (and create sample usermap).'''
        # Set up the host-specific client.
        _create_client(p4, client_name, p4gf_dir)
        # Ensure the default user map and global config files are in place.
        _write_user_map(p4, client_name, p4gf_dir)
        p4gf_config.create_file_global(p4)
        _delete_old_init_counters(p4, server_id)

    if not _maybe_perform_init(p4, started_counter, complete_counter,
                               client_init):
        Verbosity.report(
            Verbosity.INFO,
            _('Client and usermap already initialized. Not changing.'))

        # If client already created, make sure it hasn't been tweaked.
        ###: do we really need to handle this case? this is here just to pass the tests
        view = [
            '//{depot}/... //{client}/...'.format(depot=p4gf_const.P4GF_DEPOT,
                                                  client=client_name)
        ]
        p4gf_util.ensure_spec_values(p4, 'client', client_name, {
            'Root': p4gf_dir,
            'View': view
        })

    # Perform any necessary upgrades within a "lock" to avoid race conditions.
    # For now, the lock is global, but could conceivably loosen to host-only.
    started_counter = p4gf_const.P4GF_COUNTER_UPGRADE_STARTED
    complete_counter = p4gf_const.P4GF_COUNTER_UPGRADE_COMPLETE
    if not _maybe_perform_init(p4, started_counter, complete_counter,
                               _upgrade_p4gf):
        Verbosity.report(
            Verbosity.INFO,
            _('Global config file already initialized. Not changing.'))

    # Require non-empty Git config user.name and user.email.
    _ensure_git_config_non_empty('user.name', _('Git Fusion Machinery'))
    _ensure_git_config_non_empty('user.email', _('*****@*****.**'))
def main():
    """Process command line arguments and call functions to do the real
    work of cleaning up the Git mirror and Perforce workspaces.
    """
    # Set up argument parsing.
    parser = p4gf_util.create_arg_parser(
        "Deletes Git Fusion repositories and workspaces.")
    parser.add_argument("-a",
                        "--all",
                        action="store_true",
                        help="remove all known Git mirrors")
    parser.add_argument("-y",
                        "--delete",
                        action="store_true",
                        help="perform the deletion")
    parser.add_argument("-v",
                        "--verbose",
                        action="store_true",
                        help="print details of deletion process")
    parser.add_argument('views',
                        metavar='view',
                        nargs='*',
                        help='name of view to be deleted')
    args = parser.parse_args()

    # Check that either --all or 'views' was specified.
    if not args.all and len(args.views) == 0:
        sys.stderr.write('Missing view names; try adding the --all option.\n')
        sys.exit(2)

    p4 = connect_p4(client=p4gf_util.get_object_client_name())
    if not p4:
        return 2
    # Sanity check the connection (e.g. user logged in?) before proceeding.
    try:
        p4.fetch_client()
    except P4.P4Exception as e:
        sys.stderr.write("P4 exception occurred: {}".format(e))
        sys.exit(1)

    if args.all:
        try:
            delete_all(args, p4)
        except P4.P4Exception as e:
            sys.stderr.write("{}\n".format(e))
            sys.exit(1)
    else:
        # Delete the client(s) for the named view(s).
        for view in args.views:
            client_name = p4gf_context.view_to_client_name(view)
            try:
                delete_client(args, p4, client_name)
            except P4.P4Exception as e:
                sys.stderr.write("{}\n".format(e))
    if not args.delete:
        print("This was report mode. Use -y to make changes.")
Exemplo n.º 12
0
def create_context(view_name, view_lock):
    """Return a Context object that contains the connection details for use
    in communicating with the Perforce server."""
    cfg = Config()
    cfg.p4user = p4gf_const.P4GF_USER
    cfg.p4client = p4gf_util.view_to_client_name(view_name)
    cfg.p4client_gf = p4gf_util.get_object_client_name()
    cfg.view_name = view_name
    ctx = Context(cfg)
    ctx.view_lock = view_lock  # None OK: can run without a lock.
    return ctx
Exemplo n.º 13
0
def create_context(view_name, view_lock):
    """Return a Context object that contains the connection details for use
    in communicating with the Perforce server."""
    cfg = Config()
    cfg.p4user = p4gf_const.P4GF_USER
    cfg.p4client = p4gf_util.view_to_client_name(view_name)
    cfg.p4client_gf = p4gf_util.get_object_client_name()
    cfg.view_name = view_name
    ctx = Context(cfg)
    ctx.view_lock = view_lock  # None OK: can run without a lock.
    return ctx
Exemplo n.º 14
0
def _create_client(p4):
    """Create the host-specific Perforce client.

    This enables working with the object cache in the P4GF_DEPOT depot.
    """
    # See if the old object clients exist, in which case we will remove them.
    for old_client_name in [OLD_OBJECT_CLIENT,
                            OLDER_OBJECT_CLIENT,
                            p4gf_util.get_object_client_name()]:
        if p4gf_p4spec.spec_exists(p4, 'client', old_client_name):
            p4.run('client', '-df', old_client_name)
            _info(_("Old client '{client_name}' deleted.").format(client_name=old_client_name))
Exemplo n.º 15
0
def _add_file(p4, depot_path, file_content, client_spec=None):
    '''
    add a config file to Perforce using the Git Fusion object client
    '''
    old_client = p4.client
    p4.client = p4gf_util.get_object_client_name()
    added = False
    try:
        added = p4gf_util.add_depot_file(p4, depot_path, file_content, client_spec)
    finally:
        p4.client = old_client
    return added
Exemplo n.º 16
0
def _add_file(p4, depot_path, file_content, client_spec=None):
    '''
    add a config file to Perforce using the Git Fusion object client
    '''
    old_client = p4.client
    p4.client = p4gf_util.get_object_client_name()
    added = False
    try:
        added = p4gf_util.add_depot_file(p4, depot_path, file_content,
                                         client_spec)
    finally:
        p4.client = old_client
    return added
def delete_all(args, p4):
    """Find all git-fusion-* clients and remove them, as well as
    the entire object cache (//.git-fusion/objects/...).

    Keyword arguments:
    args -- parsed command line arguments
    p4   -- Git user's Perforce client

    """
    p4.user = p4gf_const.P4GF_USER
    group_list = [p4gf_const.P4GF_GROUP_PULL, p4gf_const.P4GF_GROUP_PUSH]
    print("Connected to {}".format(p4.port))
    print_verbose(args, "Scanning for Git Fusion clients...")
    client_name = p4gf_util.get_object_client_name()
    delete_clients(args, p4, client_name)
    # Retrieve host-specific initialization counters.
    counters = []
    r = p4.run('counters', '-u', '-e', 'git-fusion*-init-started')
    for spec in r:
        counters.append(spec['counter'])
    r = p4.run('counters', '-u', '-e', 'git-fusion*-init-complete')
    for spec in r:
        counters.append(spec['counter'])
    localroot = get_p4gf_localroot(p4)
    if not args.delete:
        if localroot:
            print("p4 sync -f {}...#none".format(localroot))
            print("p4 client -f -d {}".format(client_name))
            print("rm -rf {}".format(localroot))
        print("p4 obliterate -y //.git-fusion/objects/...")
        for counter in counters:
            print("p4 counter -u -d {}".format(counter))
        for group in group_list:
            print("p4 group -a -d {}".format(group))
    else:
        if localroot:
            print_verbose(
                args, "Removing client files for {}...".format(client_name))
            p4.run('sync', '-fq', localroot + '...#none')
            print_verbose(args, "Deleting client {}...".format(client_name))
            p4.run('client', '-df', client_name)
            print_verbose(
                args, "Deleting client {}'s workspace...".format(client_name))
            shutil.rmtree(localroot)
        print_verbose(args, "Obliterating object cache...")
        p4.run('obliterate', '-y', '//.git-fusion/objects/...')
        print_verbose(args, "Removing initialization counters...")
        for counter in counters:
            _delete_counter(p4, counter)
        for group in group_list:
            delete_group(args, p4, group)
def delete_all(args, p4):
    """Find all git-fusion-* clients and remove them, as well as
    the entire object cache (//.git-fusion/objects/...).

    Keyword arguments:
    args -- parsed command line arguments
    p4   -- Git user's Perforce client

    """
    p4.user = p4gf_const.P4GF_USER
    group_list = [p4gf_const.P4GF_GROUP_PULL, p4gf_const.P4GF_GROUP_PUSH]
    print("Connected to {}".format(p4.port))
    print_verbose(args, "Scanning for Git Fusion clients...")
    client_name = p4gf_util.get_object_client_name()
    delete_clients(args, p4, client_name)
    # Retrieve host-specific initialization counters.
    counters = []
    r = p4.run('counters', '-u', '-e', 'git-fusion*-init-started')
    for spec in r:
        counters.append(spec['counter'])
    r = p4.run('counters', '-u', '-e', 'git-fusion*-init-complete')
    for spec in r:
        counters.append(spec['counter'])
    localroot = get_p4gf_localroot(p4)
    if not args.delete:
        if localroot:
            print("p4 sync -f {}...#none".format(localroot))
            print("p4 client -f -d {}".format(client_name))
            print("rm -rf {}".format(localroot))
        print("p4 obliterate -y //.git-fusion/objects/...")
        for counter in counters:
            print("p4 counter -u -d {}".format(counter))
        for group in group_list:
            print("p4 group -a -d {}".format(group))
    else:
        if localroot:
            print_verbose(args, "Removing client files for {}...".format(client_name))
            p4.run('sync', '-fq', localroot + '...#none')
            print_verbose(args, "Deleting client {}...".format(client_name))
            p4.run('client', '-df', client_name)
            print_verbose(args, "Deleting client {}'s workspace...".format(client_name))
            shutil.rmtree(localroot)
        print_verbose(args, "Obliterating object cache...")
        p4.run('obliterate', '-y', '//.git-fusion/objects/...')
        print_verbose(args, "Removing initialization counters...")
        for counter in counters:
            _delete_counter(p4, counter)
        for group in group_list:
            delete_group(args, p4, group)
Exemplo n.º 19
0
def pacemaker(view_name, event):
    """As long as event flag is clear, update heartbeat of named lock.
    """
    # Running in a separate process, need to establish our own P4 connection
    # and set up a heartbeat-only lock to update the heartbeat of the lock
    # associated with the view.
    p4 = p4gf_create_p4.connect_p4(client=p4gf_util.get_object_client_name())
    with p4:
        lock = CounterLock(p4, view_name, heartbeat_only=True)
        LOG.debug("starting pacemaker for lock {}".format(view_name))
        while not event.is_set():
            lock.update_heartbeat()
            event.wait(HEART_RATE)
        lock.clear_heartbeat()
        LOG.debug("stopping pacemaker for lock {}".format(view_name))
Exemplo n.º 20
0
def pacemaker(view_name, event):
    """As long as event flag is clear, update heartbeat of named lock.
    """
    # Running in a separate process, need to establish our own P4 connection
    # and set up a heartbeat-only lock to update the heartbeat of the lock
    # associated with the view.
    p4 = p4gf_create_p4.connect_p4(client=p4gf_util.get_object_client_name())
    with p4:
        lock = CounterLock(p4, view_name, heartbeat_only=True)
        LOG.debug("starting pacemaker for lock {}".format(view_name))
        while not event.is_set():
            lock.update_heartbeat()
            event.wait(HEART_RATE)
        lock.clear_heartbeat()
        LOG.debug("stopping pacemaker for lock {}".format(view_name))
def main():
    """Process command line arguments and call functions to do the real
    work of cleaning up the Git mirror and Perforce workspaces.
    """
    # Set up argument parsing.
    parser = p4gf_util.create_arg_parser(
        "Deletes Git Fusion repositories and workspaces.")
    parser.add_argument("-a", "--all", action="store_true",
                        help="remove all known Git mirrors")
    parser.add_argument("-y", "--delete", action="store_true",
                        help="perform the deletion")
    parser.add_argument("-v", "--verbose", action="store_true",
                        help="print details of deletion process")
    parser.add_argument('views', metavar='view', nargs='*',
                        help='name of view to be deleted')
    args = parser.parse_args()

    # Check that either --all or 'views' was specified.
    if not args.all and len(args.views) == 0:
        sys.stderr.write('Missing view names; try adding the --all option.\n')
        sys.exit(2)

    p4 = connect_p4(client=p4gf_util.get_object_client_name())
    if not p4:
        return 2
    # Sanity check the connection (e.g. user logged in?) before proceeding.
    try:
        p4.fetch_client()
    except P4.P4Exception as e:
        sys.stderr.write("P4 exception occurred: {}".format(e))
        sys.exit(1)

    if args.all:
        try:
            delete_all(args, p4)
        except P4.P4Exception as e:
            sys.stderr.write("{}\n".format(e))
            sys.exit(1)
    else:
        # Delete the client(s) for the named view(s).
        for view in args.views:
            client_name = p4gf_context.view_to_client_name(view)
            try:
                delete_client(args, p4, client_name)
            except P4.P4Exception as e:
                sys.stderr.write("{}\n".format(e))
    if not args.delete:
        print("This was report mode. Use -y to make changes.")
def get_p4gf_localroot(p4):
    """Calculate the local root for the object client."""
    if p4.client != p4gf_util.get_object_client_name():
        raise RuntimeError('incorrect p4 client')
    client = p4.fetch_client()
    rootdir = client["Root"]
    if rootdir.endswith("/"):
        rootdir = rootdir[:len(rootdir) - 1]
    client_map = P4.Map(client["View"])
    lhs = client_map.lhs()
    if len(lhs) > 1:
        # not a conforming Git Fusion client, ignore it
        return None
    rpath = client_map.translate(lhs[0])
    localpath = p4gf_context.client_path_to_local(rpath, p4.client, rootdir)
    localroot = p4gf_context.strip_wild(localpath)
    return localroot
def get_p4gf_localroot(p4):
    """Calculate the local root for the object client."""
    if p4.client != p4gf_util.get_object_client_name():
        raise RuntimeError('incorrect p4 client')
    client = p4.fetch_client()
    rootdir = client["Root"]
    if rootdir.endswith("/"):
        rootdir = rootdir[:len(rootdir) - 1]
    client_map = P4.Map(client["View"])
    lhs = client_map.lhs()
    if len(lhs) > 1:
        # not a conforming Git Fusion client, ignore it
        return None
    rpath = client_map.translate(lhs[0])
    localpath = p4gf_context.client_path_to_local(rpath, p4.client, rootdir)
    localroot = p4gf_context.strip_wild(localpath)
    return localroot
Exemplo n.º 24
0
def _list_for_server():
    '''
    Return list of repos that have been copied to the given Git Fusion
    server.

    "have been copied" here means "has a .git-fusion/views/<view_name>/
    directory on this server."
    '''
    p4 = p4gf_create_p4.create_p4(client=p4gf_util.get_object_client_name())
    result = []
    p4gf_dir = p4gf_util.p4_to_p4gf_dir(p4)

    for view_name in p4gf_util.view_list(p4):
        view_dirs = p4gf_view_dirs.from_p4gf_dir(p4gf_dir, view_name)
        if os.path.exists(view_dirs.GIT_DIR):
            result.append(view_name)
    p4gf_create_p4.destroy(p4)
    return result
Exemplo n.º 25
0
def _list_for_server():
    '''
    Return list of repos that have been copied to the given Git Fusion
    server.

    "have been copied" here means "has a .git-fusion/views/<view_name>/
    directory on this server."
    '''
    p4 = p4gf_create_p4.create_p4(client=p4gf_util.get_object_client_name())
    result = []
    p4gf_dir = p4gf_util.p4_to_p4gf_dir(p4)

    for view_name in p4gf_util.view_list(p4):
        view_dirs = p4gf_view_dirs.from_p4gf_dir(p4gf_dir, view_name)
        if os.path.exists(view_dirs.GIT_DIR):
            result.append(view_name)
    p4gf_create_p4.destroy(p4)
    return result
Exemplo n.º 26
0
def init(p4):
    """Ensure both global and host-specific initialization are completed.
    """
    started_counter = p4gf_const.P4GF_COUNTER_INIT_STARTED
    complete_counter = p4gf_const.P4GF_COUNTER_INIT_COMPLETE
    if not old_counter_present(p4):
        Verbosity.report(Verbosity.DEBUG, _('Old 2012.2 counter not present.'))
    if not _maybe_perform_init(p4, started_counter, complete_counter, _global_init):
        Verbosity.report(Verbosity.INFO, _('Permissions already initialized. Not changing.'))
    server_id = p4gf_util.get_server_id()
    started_counter = p4gf_const.P4GF_COUNTER_INIT_STARTED + '-' + server_id
    complete_counter = p4gf_const.P4GF_COUNTER_INIT_COMPLETE + '-' + server_id
    p4gf_dir = p4gf_const.P4GF_HOME
    client_name = p4gf_util.get_object_client_name()

    def client_init(p4):
        '''Perform host-specific initialization (and create sample usermap).'''
        # Set up the host-specific client.
        _create_client(p4, client_name, p4gf_dir)
        # Ensure the default user map and global config files are in place.
        _write_user_map(p4, client_name, p4gf_dir)
        p4gf_config.create_file_global(p4)
        _delete_old_init_counters(p4, server_id)

    if not _maybe_perform_init(p4, started_counter, complete_counter, client_init):
        Verbosity.report(Verbosity.INFO, _('Client and usermap already initialized. Not changing.'))

        # If client already created, make sure it hasn't been tweaked.
        ###: do we really need to handle this case? this is here just to pass the tests
        view = ['//{depot}/... //{client}/...'.format(depot=p4gf_const.P4GF_DEPOT,
                client=client_name)]
        p4gf_util.ensure_spec_values(p4, 'client', client_name,
                {'Root': p4gf_dir, 'View': view})

    # Perform any necessary upgrades within a "lock" to avoid race conditions.
    # For now, the lock is global, but could conceivably loosen to host-only.
    started_counter = p4gf_const.P4GF_COUNTER_UPGRADE_STARTED
    complete_counter = p4gf_const.P4GF_COUNTER_UPGRADE_COMPLETE
    if not _maybe_perform_init(p4, started_counter, complete_counter, _upgrade_p4gf):
        Verbosity.report(Verbosity.INFO, _('Global config file already initialized. Not changing.'))

    # Require non-empty Git config user.name and user.email.
    _ensure_git_config_non_empty('user.name',  _('Git Fusion Machinery'))
    _ensure_git_config_non_empty('user.email', _('*****@*****.**'))
Exemplo n.º 27
0
def read_user_map(p4):
    """Reads the user map file from Perforce into a list of tuples,
    consisting of username, email address, and full name. If no
    such file exists, an empty list is returned.

    Returns a list of 3-tuples: (p4user, email, fullname)
    """
    usermap = [(key, value['Email'], value['FullName'])
               for key, value in local_users.items() if 'Email' in value]
    client = p4.fetch_client(p4gf_util.get_object_client_name())
    mappath = client['Root'] + '/users/p4gf_usermap'
    # don't let a writable usermap file get in our way
    p4.run('sync', '-fq', mappath)
    if os.path.exists(mappath):
        regex = re.compile('([^ \t]+)[ \t]+([^ \t]+)[ \t]+"([^"]+)"')
        with open(mappath) as mf:
            for line in mf:
                if line:
                    line = line.strip()
                    if line and line[0] != '#':
                        m = regex.search(line)
                        if m:
                            usermap.append((m.group(1), m.group(2), m.group(3)))
    return usermap
def delete_all(args, p4, metrics):
    """Remove all Git Fusion clients, as well as the object cache.

    Keyword arguments:
        args -- parsed command line arguments
        p4   -- Git user's Perforce client
    """
    # pylint:disable=too-many-branches
    p4.user = p4gf_const.P4GF_USER
    group_list = [p4gf_const.P4GF_GROUP_PULL, p4gf_const.P4GF_GROUP_PUSH]
    print(_('Connected to {P4PORT}').format(P4PORT=p4.port))
    print_verbose(args, _('Scanning for Git Fusion clients...'))
    client_name = p4gf_util.get_object_client_name()
    locks = _lock_all_repos(p4)
    if args.delete:
        was_prevented = _prevent_access(p4)
    else:
        was_prevented = None
    delete_clients(args, p4, metrics)
    # Retrieve the names of the initialization/upgrade "lock" p4keys.
    p4keys = [
        p4gf_const.P4GF_P4KEY_ALL_PENDING_MB,
        p4gf_const.P4GF_P4KEY_ALL_REMAINING_MB
    ]
    # Key patterns NOT published in p4gf_const because they have trailing *
    # wildcards and it's not worth cluttering p4gf_const for this one use.
    p4key_patterns = [
        'git-fusion-init-started*', 'git-fusion-init-complete*',
        'git-fusion-upgrade-started*', 'git-fusion-upgrade-complete*',
        'git-fusion-index-*'
    ]
    for p4key_pattern in p4key_patterns:
        d = P4Key.get_all(p4, p4key_pattern)
        p4keys.extend(sorted(d.keys()))
    localroot = get_p4gf_localroot(p4)
    if not args.delete:
        if localroot:
            if args.no_obliterate:
                print(NTR('p4 sync -f #none'))
            else:
                print(NTR('p4 client -f -d {}').format(client_name))
                print(NTR('rm -rf {}').format(localroot))
        if not args.no_obliterate:
            print(
                NTR('p4 obliterate -hay //{}/objects/...').format(
                    p4gf_const.P4GF_DEPOT))
        for p4key in p4keys:
            print(NTR('p4 key -d {}').format(p4key))
        for group in group_list:
            print(NTR('p4 group -a -d {}').format(group))
    else:
        if localroot:
            if not args.no_obliterate:
                # Need this in order to use --gc later on
                # client should not exist; this is likely a NOOP
                p4gf_util.p4_client_df(p4, client_name)
                metrics.clients += 1
                print_verbose(
                    args,
                    _("Deleting client '{client_name}'s workspace...").format(
                        client_name=client_name))
                _remove_local_root(localroot)
        _delete_cache(args, p4, metrics)
        print_verbose(args, _('Removing initialization p4keys...'))
        for p4key in p4keys:
            delete_p4key(p4, p4key, metrics)
        for group in group_list:
            delete_group(args, p4, group, metrics)
    _release_locks(locks)
    if was_prevented is not None:
        if was_prevented != '0':
            P4Key.set(p4, p4gf_const.P4GF_P4KEY_PREVENT_NEW_SESSIONS,
                      was_prevented)
        else:
            P4Key.delete(p4, p4gf_const.P4GF_P4KEY_PREVENT_NEW_SESSIONS)
Exemplo n.º 29
0
def delete_all(args, p4, metrics):
    """Find all git-fusion-* clients and remove them, as well as
    the entire object cache (//P4GF_DEPOT/objects/...).

    Keyword arguments:
        args -- parsed command line arguments
        p4   -- Git user's Perforce client
    """
    p4.user = p4gf_const.P4GF_USER
    group_list = [p4gf_const.P4GF_GROUP_PULL, p4gf_const.P4GF_GROUP_PUSH]
    print(_('Connected to {P4PORT}').format(P4PORT=p4.port))
    print_verbose(args, _('Scanning for Git Fusion clients...'))
    client_name = p4gf_util.get_object_client_name()
    locks = _lock_all_repos(p4)
    was_prevented = _prevent_access(p4)
    delete_clients(args, p4, metrics)
    # Retrieve the names of the initialization/upgrade "lock" counters.
    counters = []
    counter_names = [
        'git-fusion-init-started*', 'git-fusion-init-complete*',
        'git-fusion-upgrade-started*', 'git-fusion-upgrade-complete*',
        'git-fusion-index-*'
    ]
    for counter_name in counter_names:
        r = p4.run('counters', '-u', '-e', counter_name)
        for spec in r:
            counters.append(spec['counter'])
    localroot = get_p4gf_localroot(p4)
    if not args.delete:
        if localroot:
            print(NTR('p4 sync -f {}...#none').format(localroot))
            if not args.no_obliterate:
                print(NTR('p4 client -f -d {}').format(client_name))
                print(NTR('rm -rf {}').format(localroot))
        if not args.no_obliterate:
            print(
                NTR('p4 obliterate -y //{}/objects/...').format(
                    p4gf_const.P4GF_DEPOT))
        for counter in counters:
            print(NTR('p4 counter -u -d {}').format(counter))
        for group in group_list:
            print(NTR('p4 group -a -d {}').format(group))
    else:
        if localroot and not args.no_obliterate:
            print_verbose(
                args,
                _("Removing client files for '{}'...").format(client_name))
            p4.run('sync', '-fq', localroot + '/...#none')
            # Need this in order to use --gc later on
            print_verbose(args,
                          _("Deleting client '{}'...").format(client_name))
            p4.run('client', '-df', client_name)
            metrics.clients += 1
            print_verbose(
                args,
                _("Deleting client '{}'s workspace...").format(client_name))
            _remove_local_root(localroot)
        _delete_cache(args, p4, metrics)
        print_verbose(args, _('Removing initialization counters...'))
        for counter in counters:
            _delete_counter(p4, counter, metrics)
        for group in group_list:
            _delete_group(args, p4, group, metrics)
    _release_locks(locks)
    if was_prevented and was_prevented != '0':
        p4.run('counter', '-u', p4gf_const.P4GF_COUNTER_PREVENT_NEW_SESSIONS,
               was_prevented)
    else:
        p4.run('counter', '-u', '-d',
               p4gf_const.P4GF_COUNTER_PREVENT_NEW_SESSIONS)
Exemplo n.º 30
0
def main():
    """
    Process command line arguments and call functions to do the real
    work of cleaning up the Git mirror and Perforce workspaces.
    """
    log_l10n()
    p4gf_util.has_server_id_or_exit()

    # pylint:disable=C0301
    # Line too long? Too bad. Keep tabular code tabular.
    # Set up argument parsing.
    parser = p4gf_util.create_arg_parser(
        _('Deletes Git Fusion repositories and workspaces.'))
    parser.add_argument('-a',
                        '--all',
                        action='store_true',
                        help=_('remove all known Git mirrors'))
    parser.add_argument('-y',
                        '--delete',
                        action='store_true',
                        help=_('perform the deletion'))
    parser.add_argument('-v',
                        '--verbose',
                        action='store_true',
                        help=_('print details of deletion process'))
    parser.add_argument(
        '-N',
        '--no-obliterate',
        action='store_true',
        help=_('with the --all option, do not obliterate object cache'))
    parser.add_argument(NTR('views'),
                        metavar=NTR('view'),
                        nargs='*',
                        help=_('name of view to be deleted'))
    args = parser.parse_args()
    # pylint:enable=C0301

    # Check that either --all, or 'views' was specified.
    if not args.all and len(args.views) == 0:
        sys.stderr.write(_('Missing view names; try adding --all option.\n'))
        sys.exit(2)

    # Check that --no-obliterate occurs only with --all
    if not args.all and args.no_obliterate:
        sys.stderr.write(
            _('--no-obliterate permitted only with the --all option.\n'))
        sys.exit(2)

    with p4gf_create_p4.Closer():
        p4 = p4gf_create_p4.create_p4(
            client=p4gf_util.get_object_client_name())
        if not p4:
            return 2
        # Sanity check the connection (e.g. user logged in?) before proceeding.
        try:
            p4.fetch_client()
        except P4.P4Exception as e:
            sys.stderr.write(_('P4 exception occurred: {}').format(e))
            sys.exit(1)

        metrics = DeletionMetrics()
        if args.all:
            try:
                delete_all(args, p4, metrics)
            except P4.P4Exception as e:
                sys.stderr.write("{}\n".format(e))
                sys.exit(1)
        else:
            # Delete the client(s) for the named view(s).
            for git_view in args.views:
                view_name = p4gf_translate.TranslateReponame.git_to_repo(
                    git_view)
                client_name = p4gf_util.view_to_client_name(view_name)
                try:
                    with p4gf_lock.view_lock(p4, view_name, -1):
                        delete_client(args, p4, client_name, metrics)
                except P4.P4Exception as e:
                    sys.stderr.write("{}\n".format(e))
        if not args.delete:
            print(_('This was report mode. Use -y to make changes.'))
        else:
            print(
                _('Deleted {:d} files, {:d} groups, {:d} clients, and {:d} counters.'
                  ).format(metrics.files, metrics.groups, metrics.clients,
                           metrics.counters))
            if args.all:
                print(_('Successfully deleted all repos\n'))
            else:
                print(
                    _('Successfully deleted repos:\n{}').format("\n".join(
                        args.views)))
Exemplo n.º 31
0
def delete_all(args, p4, metrics):
    """Find all git-fusion-* clients and remove them, as well as
    the entire object cache (//P4GF_DEPOT/objects/...).

    Keyword arguments:
        args -- parsed command line arguments
        p4   -- Git user's Perforce client
    """
    p4.user = p4gf_const.P4GF_USER
    group_list = [p4gf_const.P4GF_GROUP_PULL, p4gf_const.P4GF_GROUP_PUSH]
    print(_('Connected to {P4PORT}').format(P4PORT=p4.port))
    print_verbose(args, _('Scanning for Git Fusion clients...'))
    client_name = p4gf_util.get_object_client_name()
    locks = _lock_all_repos(p4)
    was_prevented = _prevent_access(p4)
    delete_clients(args, p4, metrics)
    # Retrieve the names of the initialization/upgrade "lock" counters.
    counters = []
    counter_names = ['git-fusion-init-started*',
                     'git-fusion-init-complete*',
                     'git-fusion-upgrade-started*',
                     'git-fusion-upgrade-complete*',
                     'git-fusion-index-*']
    for counter_name in counter_names:
        r = p4.run('counters', '-u', '-e', counter_name)
        for spec in r:
            counters.append(spec['counter'])
    localroot = get_p4gf_localroot(p4)
    if not args.delete:
        if localroot:
            print(NTR('p4 sync -f {}...#none').format(localroot))
            if not args.no_obliterate:
                print(NTR('p4 client -f -d {}').format(client_name))
                print(NTR('rm -rf {}').format(localroot))
        if not args.no_obliterate:
            print(NTR('p4 obliterate -y //{}/objects/...').format(p4gf_const.P4GF_DEPOT))
        for counter in counters:
            print(NTR('p4 counter -u -d {}').format(counter))
        for group in group_list:
            print(NTR('p4 group -a -d {}').format(group))
    else:
        if localroot and not args.no_obliterate:
            print_verbose(args, _("Removing client files for '{}'...").format(client_name))
            p4.run('sync', '-fq', localroot + '/...#none')
            # Need this in order to use --gc later on
            print_verbose(args, _("Deleting client '{}'...").format(client_name))
            p4.run('client', '-df', client_name)
            metrics.clients += 1
            print_verbose(args, _("Deleting client '{}'s workspace...").format(client_name))
            _remove_local_root(localroot)
        _delete_cache(args, p4, metrics)
        print_verbose(args, _('Removing initialization counters...'))
        for counter in counters:
            _delete_counter(p4, counter, metrics)
        for group in group_list:
            _delete_group(args, p4, group, metrics)
    _release_locks(locks)
    if was_prevented and was_prevented != '0':
        p4.run('counter', '-u', p4gf_const.P4GF_COUNTER_PREVENT_NEW_SESSIONS, was_prevented)
    else:
        p4.run('counter', '-u', '-d', p4gf_const.P4GF_COUNTER_PREVENT_NEW_SESSIONS)
Exemplo n.º 32
0
def main():
    """set up repo for a view"""
    p4gf_util.has_server_id_or_exit()
    args = _parse_argv()
    p4gf_version.log_version()
    log_l10n()
    # !!! view_name_git    the untranslated repo name
    # !!! view_name        the translated repo name
    view_name_p4client = None
    if args.p4client:
        view_name_p4client = p4gf_util.argv_to_view_name(args.p4client)
    view_name_git = p4gf_util.argv_to_view_name(args.view)
    #strip leading '/' to conform with p4gf_auth_server behavior
    if view_name_git[0] == '/':
        view_name_git = view_name_git[1:]
    view_name = p4gf_translate.TranslateReponame.git_to_repo(view_name_git)
    p4gf_gitmirror.setup_spawn(view_name)
    p4gf_util.reset_git_enviro()

    p4 = p4gf_create_p4.create_p4()
    if not p4:
        return INIT_REPO_NOVIEW

    LOG.debug("connected to P4 at %s", p4.port)
    p4gf_proc.init()
    try:
        with p4gf_create_p4.Closer():
            p4gf_version.version_check()

            with p4gf_lock.view_lock(p4, view_name) as view_lock:
                # Ensure we have a sane environment.
                p4gf_init.init(p4)

                # Now that we can trust that the git-fusion--p4 client exists,
                # switch to that. Change takes effect immediately, don't need to
                # re-run p4.connect().
                p4.client = p4gf_util.get_object_client_name()

                # If local config file specified, validate it and store in
                # Perforce now. Even if client exists (aka repo was already
                # inited), this is one way for an admin to modify an existing
                # repo's config.
                if args.config:
                    if not os.path.exists(args.config):
                        _print_stderr(
                            _("error: missing config file '{}'").format(
                                args.config))
                        return INIT_REPO_CONFIG_FILE_MISSING
                    with Validator.from_local_file(view_name, p4,
                                                   args.config) as validator:
                        if not validator.is_valid(args.enablemismatchedrhs):
                            return INIT_REPO_CONFIG_FILE_BAD
                    p4gf_config.create_file_repo_with_contents(
                        p4, view_name, args.config)

                elif args.charset and not Validator.valid_charset(
                        args.charset):
                    _print_stderr(
                        _("error: invalid charset: {}").format(args.charset))
                    return INIT_REPO_BAD_CHARSET

                # Initialize the repository if necessary.
                print(_("Initializing '{}'...").format(view_name))
                r = init_repo(p4, view_name, view_lock, args.charset,
                              args.enablemismatchedrhs, view_name_p4client)
                if r > INIT_REPO_OK:
                    return r
                print(_("Initialization complete."))

                # Write --enablemismatchedrhs to config file
                if args.enablemismatchedrhs:
                    config = p4gf_config.read_repo(p4, view_name)
                    config[p4gf_config.SECTION_REPO]\
                          [p4gf_config.KEY_ENABLE_MISMATCHED_RHS] = str(True)
                    p4gf_config.write_repo_if(p4, p4.fetch_client(), view_name,
                                              config)

                # Populate the repo from Perforce unless --noclone.
                if not args.noclone:
                    return populate_repo(view_name, view_lock, args.start)
    except P4.P4Exception as e:
        _print_stderr(_('Error occurred: {}').format(e))

    return INIT_REPO_EXISTS
Exemplo n.º 33
0
def main():
    """set up repo for a view"""
    p4gf_util.has_server_id_or_exit()
    args = _parse_argv()
    p4gf_version.log_version()
    log_l10n()
    # !!! view_name_git    the untranslated repo name
    # !!! view_name        the translated repo name
    view_name_p4client = None
    if args.p4client:
        view_name_p4client = p4gf_util.argv_to_view_name(args.p4client)
    view_name_git = p4gf_util.argv_to_view_name(args.view)
    #strip leading '/' to conform with p4gf_auth_server behavior
    if view_name_git[0] == '/':
        view_name_git = view_name_git[1:]
    view_name = p4gf_translate.TranslateReponame.git_to_repo(view_name_git)
    p4gf_gitmirror.setup_spawn(view_name)
    p4gf_util.reset_git_enviro()

    p4 = p4gf_create_p4.create_p4()
    if not p4:
        return INIT_REPO_NOVIEW

    LOG.debug("connected to P4 at %s", p4.port)
    p4gf_proc.init()
    try:
        with p4gf_create_p4.Closer():
            p4gf_version.version_check()

            with p4gf_lock.view_lock(p4, view_name) as view_lock:
                # Ensure we have a sane environment.
                p4gf_init.init(p4)

                # Now that we can trust that the git-fusion--p4 client exists,
                # switch to that. Change takes effect immediately, don't need to
                # re-run p4.connect().
                p4.client = p4gf_util.get_object_client_name()

                # If local config file specified, validate it and store in
                # Perforce now. Even if client exists (aka repo was already
                # inited), this is one way for an admin to modify an existing
                # repo's config.
                if args.config:
                    if not os.path.exists(args.config):
                        _print_stderr(_("error: missing config file '{}'").format(args.config))
                        return INIT_REPO_CONFIG_FILE_MISSING
                    with Validator.from_local_file(view_name, p4, args.config) as validator:
                        if not validator.is_valid(args.enablemismatchedrhs):
                            return INIT_REPO_CONFIG_FILE_BAD
                    p4gf_config.create_file_repo_with_contents(p4, view_name, args.config)

                elif args.charset and not Validator.valid_charset(args.charset):
                    _print_stderr(_("error: invalid charset: {}").format(args.charset))
                    return INIT_REPO_BAD_CHARSET

                # Initialize the repository if necessary.
                print(_("Initializing '{}'...").format(view_name))
                r = init_repo(p4, view_name, view_lock, args.charset, args.enablemismatchedrhs,
                        view_name_p4client)
                if r > INIT_REPO_OK:
                    return r
                print(_("Initialization complete."))

                # Write --enablemismatchedrhs to config file
                if args.enablemismatchedrhs:
                    config = p4gf_config.read_repo(p4, view_name)
                    config[p4gf_config.SECTION_REPO]\
                          [p4gf_config.KEY_ENABLE_MISMATCHED_RHS] = str(True)
                    p4gf_config.write_repo_if(p4, p4.fetch_client(), view_name, config)

                # Populate the repo from Perforce unless --noclone.
                if not args.noclone:
                    return populate_repo(view_name, view_lock, args.start)
    except P4.P4Exception as e:
        _print_stderr(_('Error occurred: {}').format(e))

    return INIT_REPO_EXISTS
def main():
    """Copy the SSH keys from Perforce to the authorized keys file."""
    # Set up argument parsing.
    parser = p4gf_util.create_arg_parser("""Copies SSH public keys from
Perforce depot to current user's directory. This script assumes OpenSSH
is the SSH implementation in use, and as such, writes to 'authorized_keys'
in the ~/.ssh directory. If --ssh2 is used, then writes to 'authorization'
in the ~/.ssh2 directory, writing the SSH2 formatted public keys in the
'keys' directory under ~/.ssh2, using the Perforce user names to avoid
name collisions. If public keys read from the depot are the wrong format
(OpenSSH vs. SSH2), they will be converted when written to disk.
""")
    parser.add_argument("-r",
                        "--rebuild",
                        action="store_true",
                        help="rebuild keys file")
    parser.add_argument("-v",
                        "--verbose",
                        action="store_true",
                        help="print details of update process")
    parser.add_argument("-2",
                        "--ssh2",
                        action="store_true",
                        help="produce 'SSH2' output")
    parser.add_argument("-f", "--file", help="path to authorized keys file")
    args = parser.parse_args()

    # Since this script is called often (by cron), try to reduce the lines
    # that appear in the log by raising the log level for the p4gf_create_p4
    # module.
    logging.getLogger('p4gf_create_p4').setLevel('WARN')
    p4 = connect_p4(client=p4gf_util.get_object_client_name())
    if not p4:
        return 2
    # Sanity check the connection (e.g. user logged in?) before proceeding.
    try:
        p4.fetch_client()
    except P4Exception as e:
        _print_warn("P4 exception occurred: {}".format(e), error=True)
        sys.exit(1)

    # Update global settings based on command line arguments.
    global Verbose
    Verbose = args.verbose
    global Ssh2
    Ssh2 = args.ssh2
    global SshKeysFile
    SshKeysFile = args.file
    if not SshKeysFile:
        SshKeysFile = "~/.ssh2/authorization" if Ssh2 else "~/.ssh/authorized_keys"
    if SshKeysFile[0] == '~':
        SshKeysFile = os.path.expanduser(SshKeysFile)
    global SshDirectory
    SshDirectory = os.path.dirname(SshKeysFile)

    # Update the keys file based either on latest changes or existing files.
    try:
        if args.rebuild:
            rebuild_all_keys(p4)
        else:
            update_by_changes(p4)
    except P4Exception as e:
        _print_warn("P4 exception occurred: {}".format(e), error=True)
Exemplo n.º 35
0
def main():
    """
    Process command line arguments and call functions to do the real
    work of cleaning up the Git mirror and Perforce workspaces.
    """
    log_l10n()
    p4gf_util.has_server_id_or_exit()

                        # pylint:disable=C0301
                        # Line too long? Too bad. Keep tabular code tabular.
    # Set up argument parsing.
    parser = p4gf_util.create_arg_parser(
        _('Deletes Git Fusion repositories and workspaces.'))
    parser.add_argument('-a',   '--all',            action='store_true',    help=_('remove all known Git mirrors'))
    parser.add_argument('-y',   '--delete',         action='store_true',    help=_('perform the deletion'))
    parser.add_argument('-v',   '--verbose',        action='store_true',    help=_('print details of deletion process'))
    parser.add_argument('-N',   '--no-obliterate',  action='store_true',    help=_('with the --all option, do not obliterate object cache'))
    parser.add_argument(NTR('views'), metavar=NTR('view'), nargs='*',       help=_('name of view to be deleted'))
    args = parser.parse_args()
                        # pylint:enable=C0301

    # Check that either --all, or 'views' was specified.
    if not args.all and len(args.views) == 0:
        sys.stderr.write(_('Missing view names; try adding --all option.\n'))
        sys.exit(2)

    # Check that --no-obliterate occurs only with --all
    if not args.all and args.no_obliterate:
        sys.stderr.write(_('--no-obliterate permitted only with the --all option.\n'))
        sys.exit(2)

    with p4gf_create_p4.Closer():
        p4 = p4gf_create_p4.create_p4(client=p4gf_util.get_object_client_name())
        if not p4:
            return 2
        # Sanity check the connection (e.g. user logged in?) before proceeding.
        try:
            p4.fetch_client()
        except P4.P4Exception as e:
            sys.stderr.write(_('P4 exception occurred: {}').format(e))
            sys.exit(1)

        metrics = DeletionMetrics()
        if args.all:
            try:
                delete_all(args, p4, metrics)
            except P4.P4Exception as e:
                sys.stderr.write("{}\n".format(e))
                sys.exit(1)
        else:
            # Delete the client(s) for the named view(s).
            for git_view in args.views:
                view_name = p4gf_translate.TranslateReponame.git_to_repo(git_view)
                client_name = p4gf_util.view_to_client_name(view_name)
                try:
                    with p4gf_lock.view_lock(p4, view_name, -1):
                        delete_client(args, p4, client_name, metrics)
                except P4.P4Exception as e:
                    sys.stderr.write("{}\n".format(e))
        if  not args.delete:
            print(_('This was report mode. Use -y to make changes.'))
        else:
            print(_('Deleted {:d} files, {:d} groups, {:d} clients, and {:d} counters.').format(
                metrics.files, metrics.groups, metrics.clients, metrics.counters))
            if args.all:
                print(_('Successfully deleted all repos\n'))
            else:
                print(_('Successfully deleted repos:\n{}').format("\n".join(args.views)))