def _inject(self, dest_path): new_data = '' write_it = False f = open(dest_path, 'r') line_num = 0 for line in f.readlines(): line_num = line_num + 1 # only one injection per line is allowed for inj,inj_data in self.config['injections'].items(): pattern = '(.*)\%sboss.mark\:%s\%s(.*)' % ( self.config['start_delimiter'], inj, self.config['end_delimiter'], ) m = re.match(pattern, line) if m: print("Injecting %s into %s at line #%s" % \ (inj, dest_path, line_num)) line = line + "%s\n" % self._sub(inj_data) write_it = True break new_data = new_data + line f.close() if write_it: fs.backup(dest_path, suffix='.boss.bak') self._write_file(dest_path, new_data, overwrite=True)
def test_backup(tmp): bkfile = fs.backup(tmp.file) assert "%s.bak" % os.path.basename(tmp.file) == os.path.basename(bkfile) bkfile = fs.backup(tmp.file) assert "%s.bak.0" % os.path.basename(tmp.file) == os.path.basename(bkfile) bkfile = fs.backup(tmp.file) assert "%s.bak.1" % os.path.basename(tmp.file) == os.path.basename(bkfile) bkdir = fs.backup(tmp.dir) assert "%s.bak" % os.path.basename(tmp.dir) == os.path.basename(bkdir) assert fs.backup('someboguspath') is None
def test_backup(self): _, tmpfile = tempfile.mkstemp() bkfile = fs.backup(tmpfile) self.eq("%s.bak" % os.path.basename(tmpfile), os.path.basename(bkfile)) bkfile = fs.backup(tmpfile) self.eq("%s.bak.0" % os.path.basename(tmpfile), os.path.basename(bkfile)) bkfile = fs.backup(tmpfile) self.eq("%s.bak.1" % os.path.basename(tmpfile), os.path.basename(bkfile)) tmpdir = tempfile.mkdtemp() bkdir = fs.backup(tmpdir) self.eq("%s.bak" % os.path.basename(tmpdir), os.path.basename(bkdir)) res = fs.backup('someboguspath') self.eq(res, None)
def test_backup(self): tmp_file = os.path.join(self.tmp_dir, 'test.file') tmp_dir = os.path.join(self.tmp_dir, 'test.dir') open(tmp_file, 'w').close() os.makedirs(tmp_dir) bkfile = fs.backup(tmp_file) self.eq("%s.bak" % os.path.basename(tmp_file), os.path.basename(bkfile)) bkfile = fs.backup(tmp_file) self.eq("%s.bak.0" % os.path.basename(tmp_file), os.path.basename(bkfile)) bkfile = fs.backup(tmp_file) self.eq("%s.bak.1" % os.path.basename(tmp_file), os.path.basename(bkfile)) bkdir = fs.backup(tmp_dir) self.eq("%s.bak" % os.path.basename(tmp_dir), os.path.basename(bkdir)) res = fs.backup('someboguspath') self.eq(res, None)
def _default(self): """Default action if no sub-command is passed.""" path = self._clean_path(self.app.pargs.path) if self.app.pargs.suffix is not None: suffix = self.app.pargs.suffix else: suffix = self.app.config.get('dotbak', 'suffix') if self.app.config.get('dotbak', 'timestamps') is True: timestamp = datetime.now().strftime('%Y-%m-%d-%H:%M:%S') suffix = f'{suffix}-{timestamp}' if not os.path.exists(self.app.pargs.path): raise exc.DotBakError(f'Path does not exist: {path}') res = fs.backup(path, suffix=suffix) self.app.log.info(f'Copied {path} -> {res}')