def _clone_impl(user, source, dest=None, push=None, remoteauth="write", authtype=None, username=None, password=None, kcpass="", vcs="hg"): working_dir = user.get_location() args = ["clone", source] if dest: args.append(dest) auth = {} if username: auth['username'] = username if password: auth['password'] = password keychain = KeyChain(user, kcpass) keyfile = None if vcs: dialect = main.get_dialect(vcs) else: dialect = None if authtype: auth['type'] = authtype if authtype == "ssh": public_key, private_key = keychain.get_ssh_key() keyfile = TempSSHKeyFile() keyfile.store(public_key, private_key) auth['key'] = keyfile.filename try: context = main.SecureContext(working_dir, auth) command = main.convert(context, args, dialect) output = main.run_command(command, context) finally: if keyfile: keyfile.delete() project = filesystem.get_project(user, user, command.dest) if authtype == "ssh": keychain.set_ssh_for_project(project, remoteauth) elif authtype == "password": keychain.set_credentials_for_project(project, remoteauth, username, password) metadata = project.metadata metadata['remote_url'] = source if push: metadata['push'] = push space_used = project.scan_files() user.amount_used += space_used metadata.close() result = dict(output=str(output), command="clone", project=command.dest) return result
def run_command(user, project, args, kcpass=None): working_dir = project.location metadata = project.metadata try: for i in range(0, len(args)): if args[i] == "_BESPIN_REMOTE_URL": try: args[i] = metadata["remote_url"].encode("utf8") except KeyError: del args[i] break elif args[i] == "_BESPIN_PUSH": try: args[i] = metadata["push"].encode("utf8") except KeyError: del args[i] break context = main.SecureContext(working_dir) if args and args[0] in main.dialects: dialect = None elif not is_new_project_command(args): dialect = main.infer_dialect(working_dir) else: dialect = None command_class = main.get_command_class(context, args, dialect) keyfile = None if command_class.reads_remote or command_class.writes_remote: remote_auth = metadata.get(AUTH_PROPERTY) if command_class.writes_remote or remote_auth == AUTH_BOTH: if not kcpass: raise model.NotAuthorized("Keychain password is required for this command.") keychain = KeyChain(user, kcpass) credentials = keychain.get_credentials_for_project(project) if credentials['type'] == 'ssh': keyfile = TempSSHKeyFile() keyfile.store(credentials['ssh_public_key'], credentials['ssh_private_key']) auth = dict(type='ssh', key=keyfile.filename) else: auth = credentials context.auth = auth try: command = command_class.from_args(context, args) output = main.run_command(command, context) finally: if keyfile: keyfile.delete() finally: metadata.close() return str(output)
def clone(user, source, dest=None, push=None, remoteauth="write", authtype=None, username=None, password=None, kcpass="", vcs="hg"): """Clones or checks out the repository using the command provided.""" working_dir = user.get_location() args = ["clone", source] if dest: args.append(dest) auth = {} if username: auth['username'] = username if password: auth['password'] = password keychain = KeyChain(user, kcpass) keyfile = None if vcs: dialect = main.get_dialect(vcs) else: dialect = None if authtype: auth['type'] = authtype if authtype == "ssh": public_key, private_key = keychain.get_ssh_key() keyfile = TempSSHKeyFile() keyfile.store(public_key, private_key) auth['key'] = keyfile.filename try: context = main.SecureContext(working_dir, auth) command = main.convert(context, args, dialect) output = main.run_command(command, context) finally: if keyfile: keyfile.delete() project = model.get_project(user, user, command.dest) if authtype == "ssh": keychain.set_ssh_for_project(project, remoteauth) elif authtype == "password": keychain.set_credentials_for_project(project, remoteauth, username, password) metadata = project.metadata metadata['remote_url'] = source if push: metadata['push'] = push metadata.close() return str(output)
def _run_command_impl(user, project, args, kcpass): """Synchronous implementation of run_command.""" working_dir = project.location metadata = project.metadata try: for i in range(0, len(args)): if args[i] == "_BESPIN_REMOTE_URL": try: args[i] = metadata["remote_url"].encode("utf8") except KeyError: del args[i] break elif args[i] == "_BESPIN_PUSH": try: args[i] = metadata["push"].encode("utf8") except KeyError: del args[i] break output_file = StringIO() output = main.IOWrapper(output_file) context = main.SecureContext(working_dir, timeout=config.c.vcs_timeout, output=output) context.user = _get_vcs_user(user, project) if args and args[0] in main.dialects: dialect = None elif not is_new_project_command(args): dialect = main.infer_dialect(working_dir) else: dialect = None command_class = main.get_command_class(context, args, dialect) command_name = command_class.__name__ keyfile = None if command_class.reads_remote or command_class.writes_remote: remote_auth = metadata.get(AUTH_PROPERTY) if command_class.writes_remote or remote_auth == AUTH_BOTH: if not kcpass: raise NotAuthorized( "Keychain password is required for this command.") keychain = KeyChain(user, kcpass) credentials = keychain.get_credentials_for_project(project) if credentials['type'] == 'ssh': keyfile = TempSSHKeyFile() keyfile.store(credentials['ssh_public_key'], credentials['ssh_private_key']) auth = dict(type='ssh', key=keyfile.filename) else: auth = credentials context.auth = auth try: try: command = command_class.from_args(context, args) except GetValueFromEditor, e: new_args = e.template_args for i in range(0, len(new_args)): if new_args[i] == inserted_value: new_args[i] = "VALUE-GOES_HERE" return dict(success=False, needsInput=True, args=new_args, prompt=e.prompt) main.run_command(command, context) finally: if keyfile: keyfile.delete() if output.return_code: return dict(command=command_name, success=False, output=output_file.getvalue())
def _clone_impl(user, source, qid, dest=None, push=None, remoteauth="write", authtype=None, username=None, password=None, kcpass="", vcs="hg", vcsuser=None): working_dir = user.get_location() args = ["clone", source] if dest: args.append(dest) auth = {} if username: auth['username'] = username if password: auth['password'] = password keychain = KeyChain(user, kcpass) keyfile = None if vcs: dialect = main.get_dialect(vcs) else: dialect = None if authtype: auth['type'] = authtype if authtype == "ssh": public_key, private_key = keychain.get_ssh_key() keyfile = TempSSHKeyFile() keyfile.store(public_key, private_key) auth['key'] = keyfile.filename try: output_file = StringIO() output = LineCounterOutput(user, qid, output_file) context = main.SecureContext(working_dir, auth, timeout=config.c.vcs_timeout, output=output) command = main.convert(context, args, dialect) main.run_command(command, context) log.debug(output) finally: if keyfile: keyfile.delete() if output.return_code: return dict(success=False, output=output_file.getvalue(), command="clone") project = filesystem.get_project(user, user, command.dest) if authtype == "ssh": keychain.set_ssh_for_project(project, remoteauth) elif authtype == "password": keychain.set_credentials_for_project(project, remoteauth, username, password) metadata = project.metadata metadata['remote_url'] = source if push: metadata['push'] = push if vcsuser: metadata['vcsuser'] = vcsuser space_used = project.scan_files() user.amount_used += space_used metadata.close() result = dict(success=True, output=output_file.getvalue(), command="clone", project=command.dest) return result
def _run_command_impl(user, project, args, kcpass): """Synchronous implementation of run_command.""" working_dir = project.location metadata = project.metadata try: for i in range(0, len(args)): if args[i] == "_BESPIN_REMOTE_URL": try: args[i] = metadata["remote_url"].encode("utf8") except KeyError: del args[i] break elif args[i] == "_BESPIN_PUSH": try: args[i] = metadata["push"].encode("utf8") except KeyError: del args[i] break context = main.SecureContext(working_dir) context.user = _get_vcs_user(user, project) if args and args[0] in main.dialects: dialect = None elif not is_new_project_command(args): dialect = main.infer_dialect(working_dir) else: dialect = None command_class = main.get_command_class(context, args, dialect) command_name = command_class.__name__ keyfile = None if command_class.reads_remote or command_class.writes_remote: remote_auth = metadata.get(AUTH_PROPERTY) if command_class.writes_remote or remote_auth == AUTH_BOTH: if not kcpass: raise NotAuthorized("Keychain password is required for this command.") keychain = KeyChain(user, kcpass) credentials = keychain.get_credentials_for_project(project) if credentials['type'] == 'ssh': keyfile = TempSSHKeyFile() keyfile.store(credentials['ssh_public_key'], credentials['ssh_private_key']) auth = dict(type='ssh', key=keyfile.filename) else: auth = credentials context.auth = auth try: command = command_class.from_args(context, args) output = main.run_command(command, context) log.debug(output) finally: if keyfile: keyfile.delete() finally: metadata.close() result = dict(command=command_name, output=str(output)) return result
def _run_command_impl(user, project, args, kcpass): """Synchronous implementation of run_command.""" working_dir = project.location metadata = project.metadata try: for i in range(0, len(args)): if args[i] == "_BESPIN_REMOTE_URL": try: args[i] = metadata["remote_url"].encode("utf8") except KeyError: del args[i] break elif args[i] == "_BESPIN_PUSH": try: args[i] = metadata["push"].encode("utf8") except KeyError: del args[i] break output_file = StringIO() output = main.IOWrapper(output_file) context = main.SecureContext(working_dir, timeout=config.c.vcs_timeout, output=output) context.user = _get_vcs_user(user, project) if args and args[0] in main.dialects: dialect = None elif not is_new_project_command(args): dialect = main.infer_dialect(working_dir) else: dialect = None command_class = main.get_command_class(context, args, dialect) command_name = command_class.__name__ keyfile = None if command_class.reads_remote or command_class.writes_remote: remote_auth = metadata.get(AUTH_PROPERTY) if command_class.writes_remote or remote_auth == AUTH_BOTH: if not kcpass: raise NotAuthorized("Keychain password is required for this command.") keychain = KeyChain(user, kcpass) credentials = keychain.get_credentials_for_project(project) if credentials['type'] == 'ssh': keyfile = TempSSHKeyFile() keyfile.store(credentials['ssh_public_key'], credentials['ssh_private_key']) auth = dict(type='ssh', key=keyfile.filename) else: auth = credentials context.auth = auth try: try: command = command_class.from_args(context, args) except GetValueFromEditor, e: new_args = e.template_args for i in range(0, len(new_args)): if new_args[i] == inserted_value: new_args[i] = "VALUE-GOES_HERE" return dict(success=False, needsInput=True, args=new_args, prompt = e.prompt) main.run_command(command, context) finally: if keyfile: keyfile.delete() if output.return_code: return dict(command=command_name, success=False, output=output_file.getvalue())