def show_diffs(appl_file, ref_file, askconfirm=True): """interactivly replace the old file with the new file according to user decision """ import shutil pipe = subprocess.Popen(['diff', '-u', appl_file, ref_file], stdout=subprocess.PIPE) diffs = pipe.stdout.read().decode('utf-8') if diffs: if askconfirm: print() print(diffs) action = ASK.ask('Replace ?', ('Y', 'n', 'q'), 'Y').lower() else: action = 'y' if action == 'y': try: shutil.copyfile(ref_file, appl_file) except IOError: os.system('chmod a+w %s' % appl_file) shutil.copyfile(ref_file, appl_file) print('replaced') elif action == 'q': sys.exit(0) else: copy_file = appl_file + '.default' copy = open(copy_file, 'w') copy.write(open(ref_file).read()) copy.close() print('keep current version, the new file has been written to', copy_file) else: print('no diff between %s and %s' % (appl_file, ref_file))
def _ask_for_dependencies(self): from logilab.common.shellutils import ASK from logilab.common.textutils import splitstrip depcubes = [] for cube in ServerConfiguration.available_cubes(): answer = ASK.ask("Depends on cube %s? " % cube, ('N', 'y', 'skip', 'type'), 'N') if answer == 'y': depcubes.append(cube) if answer == 'type': depcubes = splitstrip(input('type dependencies: ')) break elif answer == 'skip': break return dict( ('cubicweb-' + cube, ServerConfiguration.cube_version(cube)) for cube in depcubes)
def confirm( self, question, # pylint: disable=E0202 shell=True, abort=True, retry=False, pdb=False, default='y', traceback=None): """ask for confirmation and return true on positive answer if `retry` is true the r[etry] answer may return 2 """ possibleanswers = ['y', 'n'] if abort: possibleanswers.append('abort') if pdb: possibleanswers.append('pdb') if shell: possibleanswers.append('shell') if retry: possibleanswers.append('retry') try: answer = ASK.ask(question, possibleanswers, default) except (EOFError, KeyboardInterrupt): answer = 'abort' if answer == 'n': return False if answer == 'retry': return 2 if answer == 'abort': raise SystemExit(1) if answer == 'shell': self.interactive_shell() return self.confirm(question, shell, abort, retry, pdb, default, traceback) if answer == 'pdb': pdb = utils.get_pdb() if traceback: pdb.post_mortem(traceback) else: pdb.set_trace() return self.confirm(question, shell, abort, retry, pdb, default, traceback) return True
def execscript_confirm(scriptpath): """asks for confirmation before executing a script and provides the ability to show the script's content """ while True: answer = ASK.ask('Execute %r ?' % scriptpath, ('Y', 'n', 'show', 'abort'), 'Y') if answer == 'abort': raise SystemExit(1) elif answer == 'n': return False elif answer == 'show': stream = open(scriptpath) scriptcontent = stream.read() stream.close() print() print(scriptcontent) print() else: return True