def _verbose(self, msg): """ Writes `msg` to output stream if '--verbose'. Does not prepend anything to `msg`. Adds trailing linesep to `msg` if there's not one already. """ if self.options.verbose: self.ostream.write(add_linesep_if_missing(msg))
def _out(self, msg): """ Write `msg` to output stream if not '--quiet'. Does not prepend anything to `msg`. Adds trailing linesep to `msg` if there's not one already. """ if not self.options.quiet: self.ostream.write(add_linesep_if_missing(msg))
def _error(self, msg): sys.stderr.write( add_linesep_if_missing( dedent(msg).replace( '%prog', self.program_name ) ) ) self._exit(1)
def __write_windows_hook(self, ostream): # XXX TODO: this is blatantly broken, as is all the other Windows # stuff at the moment. lines = [ '@echo off', ] lines += [ self.conf.evn_hook_code_for_testing_if_svn_hook_is_enabled, self.conf.evn_run_hook_code ] ostream.write(add_linesep_if_missing(os.linesep.join(lines))) args = (sys.executable, self.conf.python_evn_admin_cli_file_fullpath) ostream.write('"%s" "%s" run-hook %%*\n' % args)
def __write_unix_hook(self, ostream): env = os.environ args = ('main', 'unix-hook-autoexpand-env-vars') envvars = [ '%s=%s' % (name, env[name]) for name in ( self.conf.get_csv_as_list(*args) ) if name in env ] args = ('main', 'unix-hook-force-env-vars') envvars += self.conf.get_csv_as_list(*args) lines = [ '#!/bin/sh', ] lines += envvars lines += [ 'export %s' % e.split('=')[0] for e in envvars ] lines += [ self.conf.evn_hook_code_for_testing_if_svn_hook_is_enabled, self.conf.evn_run_hook_code ] ostream.write(add_linesep_if_missing(os.linesep.join(lines)))
def version(self, args=None): sys.stdout.write(add_linesep_if_missing(evn.__version__)) self._exit(0)
def configure_to_call_evn_hook(self): output = list() backup = None existing = None code = self.conf.svn_hook_syntax_for_invoking_evn_hook if self.exists and not self.is_empty: assert not self.is_configured_to_call_evn_hook existing = self.read() lines = existing.split(os.linesep) else: lines = self.conf.svn_hook_code_empty.split(os.linesep) first = True inserted = False for line in lines: if first and os.name != 'nt': assert line == '#!/bin/sh' if inserted: output.append(line) else: if line.startswith(self.comment_character): output.append(line) elif not line: # Skip blank lines. output.append(line) else: if first and line.lower().startswith('@echo'): output.append(line) output += [ '', code, '' ] inserted = True else: output += [ '', code, '' ] output.append(line) if first: first = False if not inserted: output += [ '', code, '' ] if existing: suffix = datetime.datetime.now().strftime('%Y%m%d%H%M%S-%f') backup = self.path + '.bak.' + suffix assert not os.path.exists(backup) with open(backup, 'w+') as f: f.truncate(0) f.seek(0) f.write(existing) f.flush() f.close() assert os.path.exists(backup) with open(backup, 'r') as f: f.seek(0) assert (f.read() == existing) f.close() failed = True try: expected = add_linesep_if_missing(os.linesep.join(output)) with open(self.path, 'w+') as f: f.truncate(0) f.seek(0) f.write(expected) f.flush() f.close() assert self.exists assert not self.is_empty with open(self.path, 'r') as f: f.seek(0) assert (f.read() == expected) f.close() failed = False finally: if not failed and backup is not None: os.unlink(backup)
def configure_to_call_evn_hook(self): output = list() backup = None existing = None code = self.conf.svn_hook_syntax_for_invoking_evn_hook if self.exists and not self.is_empty: assert not self.is_configured_to_call_evn_hook existing = self.read() lines = existing.split(os.linesep) else: lines = self.conf.svn_hook_code_empty.split(os.linesep) if os.name != 'nt': insert_shebang = True try: if lines[0].startswith('#!'): insert_shebang = False except: pass if insert_shebang: output.append('#!/bin/sh') first = True inserted = False for line in lines: if inserted: if line != code: output.append(line) else: if line == code: inserted = True output.append(line) elif line.startswith(self.comment_character): output.append(line) elif not line: # Skip blank lines. output.append(line) else: if first and line.lower().startswith('@echo'): # Special-case for `@echo off` first line on Windows. output.append(line) output.append(code) else: output.append(code) output.append(line) inserted = True if first: first = False assert inserted # Remove trailing empty lines. while not output[-1]: output.pop() if existing: suffix = datetime.datetime.now().strftime('%Y%m%d%H%M%S-%f') backup = self.path + '.bak.' + suffix assert not os.path.exists(backup) with open(backup, 'w+') as f: f.truncate(0) f.seek(0) f.write(existing) f.flush() f.close() assert os.path.exists(backup) with open(backup, 'r') as f: f.seek(0) assert (f.read() == existing) f.close() failed = True try: expected = add_linesep_if_missing(os.linesep.join(output)) with open(self.path, 'w+') as f: f.truncate(0) f.seek(0) f.write(expected) f.flush() f.close() assert self.exists assert not self.is_empty with open(self.path, 'r') as f: f.seek(0) assert (f.read() == expected) f.close() failed = False finally: if not failed and backup is not None: os.unlink(backup)