def test_ls_abominate(): """recursive list of directory""" # setup mkdir('work') with cd('work'): d1 = to_path('d1') mkdir(d1) d1d1 = to_path('d1/d1') mkdir(d1d1) d1d2 = to_path('d1/d2') mkdir(d1d2) d1d1f1 = to_path('d1/d1/f1') touch(d1d1f1) d1d2f2 = to_path('d1/d2/f2') touch(d1d2f2) # run test paths = ls('.', select='**/*') # check assert set(str(f) for f in paths) == set( ['d1', 'd1/d1', 'd1/d2', 'd1/d1/f1', 'd1/d2/f2'] ) # cleanup rm('work')
def encrypt_archive(config, workspace, passcode): narrate("Encrypting the archive") with cd(workspace): run('tar -cf archive.tgz archive', 'soEW') with open('archive.tgz', 'rb') as f: cleartext = f.read() gpg = GPG() encrypted = gpg.encrypt( cleartext, recipients=None, symmetric=True, passphrase=str(passcode), ) if encrypted.ok: with open('archive.tgz.gpg', 'w') as f: f.write(str(encrypted)) else: raise EncryptionFailed(encrypted) rm('archive.tgz', 'archive') script = workspace / 'decrypt.sh' script.write_text('''\ #!/bin/sh # Decrypts the archive. gpg -d -o - archive.tgz.gpg | tar xvf - ''') chmod(0o700, script, workspace / 'archive.tgz.gpg') narrate(f"Local archive '{workspace.name}' created.")
def test_ls_abominate(): """recursive list of directory""" # setup mkdir('work') with cd('work'): d1 = to_path('d1') mkdir(d1) d1d1 = to_path('d1/d1') mkdir(d1d1) d1d2 = to_path('d1/d2') mkdir(d1d2) d1d1f1 = to_path('d1/d1/f1') touch(d1d1f1) d1d2f2 = to_path('d1/d2/f2') touch(d1d2f2) # run test paths = ls('.', select='**/*') # check assert set(str(f) for f in paths) == set( ['d1', 'd1/d1', 'd1/d2', 'd1/d1/f1', 'd1/d2/f2']) # cleanup rm('work')
def build_archive(config, interactive=True): narrate("Building the archive") plugins = select_plugins(config, 'archive') if not plugins: raise ConfigError(f"'plugins.archive' not specified, nothing to do.") # Make the archive directory: name = config.get('archive_name', '{host}').format(**PARAMS) workspace = to_path(appdirs.user_data_dir(__slug__), name) archive = to_path(workspace / 'archive') rm(workspace) mkdir(archive) # Apply any 'archive' plugins: for plugin in plugins: subconfigs = config.get('archive', {}).get(plugin.name, []) run_plugin(plugin, config, subconfigs, archive) # Show the user which files were included in the archive. display("The following files were included in the archive:") for root, _, files in os.walk(archive): root = to_path(root).relative_to(archive) for file in files: display(' ', root / file) display() if interactive: input("Is this correct? <Enter> to continue, <Ctrl-C> to cancel: ") return workspace
def test_chmod_gathering(): """change mode of a multiple files""" # setup f1 = to_path('f1') f2 = to_path('f2') touch(f1, f2) # run test for i in range(8): chmod(i, f1, f2) # 0o100000 represents a regular file assert f1.stat().st_mode == 0o100000 + i assert getmod(f1) == i assert f2.stat().st_mode == 0o100000 + i assert getmod(f2) == i chmod(8 * i, f1, f2) assert f1.stat().st_mode == 0o100000 + 8 * i assert getmod(f1) == 8 * i assert f2.stat().st_mode == 0o100000 + 8 * i assert getmod(f2) == 8 * i chmod(8 * 8 * i, f1, f2) assert f1.stat().st_mode == 0o100000 + 8 * 8 * i assert f2.stat().st_mode == 0o100000 + 8 * 8 * i assert getmod(f1) == 8 * 8 * i assert getmod(f2) == 8 * 8 * i # cleanup rm(f1, f2)
def test_chmod_quisling(): """change mode of a multiple directories""" # setup d1 = to_path('d1') d2 = to_path('d2') mkdir(d1, d2) # run test for i in range(8): chmod(i, [d1, d2]) # 0o040000 represents a directory assert d1.stat().st_mode == 0o040000 + i assert getmod(d1) == i assert d2.stat().st_mode == 0o040000 + i assert getmod(d2) == i chmod(8 * i, d1, d2) assert d1.stat().st_mode == 0o040000 + 8 * i assert getmod(d1) == 8 * i assert d2.stat().st_mode == 0o040000 + 8 * i assert getmod(d2) == 8 * i chmod(8 * 8 * i, d1, d2) assert d1.stat().st_mode == 0o040000 + 8 * 8 * i assert d2.stat().st_mode == 0o040000 + 8 * 8 * i assert getmod(d1) == 8 * 8 * i assert getmod(d2) == 8 * 8 * i # cleanup rm(d1)
def test_ls_abominate(): """recursive list of directory""" # setup mkdir("work") with cd("work"): d1 = to_path("d1") mkdir(d1) d1d1 = to_path("d1/d1") mkdir(d1d1) d1d2 = to_path("d1/d2") mkdir(d1d2) d1d1f1 = to_path("d1/d1/f1") touch(d1d1f1) d1d2f2 = to_path("d1/d2/f2") touch(d1d2f2) # run test paths = ls(".", select="**/*") # check assert set(str(f) for f in paths) == set( ["d1", "d1/d1", "d1/d2", "d1/d1/f1", "d1/d2/f2"]) # cleanup rm("work")
def initialize(): with cd(tests_dir): rm("configs .config .local repositories configs.symlink".split()) cp("CONFIGS", "configs") mkdir(".config repositories .local".split()) ln("~/.local/bin", ".local/bin") ln("~/.local/lib", ".local/lib") ln("configs", "configs.symlink") os.environ["HOME"] = str(cwd())
def initialize_configs(initialize): with cd(tests_dir): cp("CONFIGS", ".config/emborg") rm(".config/emborg/subdir") for p in lsf(".config/emborg"): contents = p.read_text() contents = contents.replace('⟪EMBORG⟫', emborg_dir) p.write_text(contents) touch(".config/.nobackup")
def test_rm_endorse(): """remove nonexistent file""" # setup f1 = to_path('f1') # run test rm(f1) # check assert not f1.exists()
def test_rm_downturn(): """remove existing file""" # setup f1 = to_path('f1') touch(f1) # run test rm(f1) # check assert not f1.exists()
def cleanup(self): if self.vim: self.vim.kill() for each in cull([self.file1, self.file2, self.file3, self.file4]): path = to_path(each) dn = path.parent fn = path.name swpfile = to_path(dn, '.' + fn + '.swp') try: rm(swpfile) except OSError as e: error(os_error(e))
def run(cls, command, args, settings, options): # read command line cmdline = docopt(cls.USAGE, argv=[command] + args) date = ['--time', cmdline['--date']] if cmdline['--date'] else [] # run duplicity rm('duplicity.log') cmd = (f'duplicity list-current-files'.split() + duplicity_options(settings, options) + archive_dir_command(settings) + sftp_command(settings) + date + [destination(settings)]) run_duplicity(cmd, settings, True)
def test_mkdir_cymbal(): """attempt to make a directory over an existing file""" # setup f1 = to_path('f1') touch(f1) # run test with pytest.raises(OSError): mkdir(f1) # cleanup rm(f1)
def test_mkdir_downturn(): """make a directory""" # setup d1 = to_path('d1') # run test mkdir(d1) # check assert d1.is_dir() # cleanup rm(d1)
def test_mkdir_solstice(): """attempt to make a directory and it parent directories""" # setup d1d1 = to_path('d1/d1') # run test mkdir(d1d1) # check assert d1d1.is_dir() # cleanup rm(d1d1)
def test_touch_endorse(): """touch a new file""" # setup f1 = to_path("f1") # run test touch(f1) # check assert f1.is_file() # cleanup rm(f1)
def duplicity_options(settings, options): args = [] gpg_binary = settings.value('gpg_binary') if gpg_binary: args.extend(['--gpg-binary', str(to_path(gpg_binary))]) if DUPLICITY_LOG_FILE: args.extend(f'--log-file {DUPLICITY_LOG_FILE}'.split()) rm(DUPLICITY_LOG_FILE) if settings.ssh_backend_method == 'option': args.extend('--ssh-backend pexpect'.split()) args.append('-v9' if 'verbose' in options else '-v8') args.append('--dry-run' if 'trial-run' in options else '') return cull(args)
def test_touch_endorse(): """touch a new file""" # setup f1 = to_path('f1') # run test touch(f1) # check assert f1.is_file() # cleanup rm(f1)
def test_rm_ground(): """remove two files""" # setup f1 = to_path('f1') touch(f1) f2 = to_path('f2') touch(f2) # run test rm(f1, f2) # check assert not f1.exists() assert not f2.exists()
def test_touch_downturn(): """touch an existing file""" # setup f1 = to_path('f1') touch(f1) # run test touch(f1) # check assert f1.is_file() # cleanup rm(f1)
def test_cp_real_downturn(): """copy existing file (ie: not generated by test_cp_real_downturn) to new file""" # setup f1 = to_path('tests/SRCFILE') f2 = to_path('f2') # run test cp(f1, f2) # check assert f2.is_file() # cleanup rm(f2)
def test_ln_endorse(): """link a nonexistent file""" # setup f1 = to_path('f1') f2 = to_path('f2') # run test ln(f1, f2) # check assert f2.is_symlink() # cleanup rm(f1, f2)
def test_ln_ground(): """link to a pre-existing name""" # setup f1 = to_path('f1') touch(f1) f2 = to_path('f2') touch(f2) # run test with pytest.raises(OSError): ln(f1, f2) # cleanup rm(f1, f2)
def run(cls, command, args, settings, options): # read command line docopt(cls.USAGE, argv=[command] + args) # run borg borg = settings.run_borg( cmd='break-lock', args=[settings.destination()], emborg_opts=options, ) out = borg.stdout if out: output(out.rstrip()) rm(settings.lockfile)
def test_ln_endorse(): """link a nonexistent file""" # setup f1 = to_path("f1") f2 = to_path("f2") # run test ln(f1, f2) # check assert f2.is_symlink() # cleanup rm(f1, f2)
def test_ln_ground(): """link to a pre-existing name""" # setup f1 = to_path("f1") touch(f1) f2 = to_path("f2") touch(f2) # run test with pytest.raises(OSError): ln(f1, f2) # cleanup rm(f1, f2)
def test_mkdir_brain(): """attempt to make a directory over an existing directory""" # setup d1d1 = to_path('d1/d1') mkdir(d1d1) # run test mkdir(d1d1) # check assert d1d1.is_dir() # cleanup rm(d1d1)
def test_mkdir_gathering(): """attempt to make a directory over an existing file""" # setup d1 = to_path('d1') mkdir(d1) # run test mkdir(d1) # check assert d1.is_dir() # cleanup rm(d1)
def test_touch_downturn(): """touch an existing file""" # setup f1 = to_path("f1") touch(f1) # run test touch(f1) # check assert f1.is_file() # cleanup rm(f1)
def __enter__(self): # resolve src directories self.src_dirs = [self.resolve_path(d) for d in self.src_dirs] if not self.src_dirs: raise Error('no source directories given.') # resolve repository and archive self.repository = self.resolve_path(self.repository) self.archive = self.resolve(self.archive) # resolve other files and directories data_dir = self.resolve_path(DATA_DIR) self.data_dir = data_dir if not data_dir.exists(): # data dir does not exist, create it self.data_dir.mkdir(mode=0o700, parents=True, exist_ok=True) self.logfile = self.resolve_path(LOG_FILE, data_dir) if 'no-log' not in self.options: self.prev_logfile = self.resolve_path(PREV_LOG_FILE, data_dir) rm(self.prev_logfile) if self.logfile.exists(): mv(self.logfile, self.prev_logfile) self.date_file = self.resolve_path(DATE_FILE, data_dir) # perform locking lockfile = self.lockfile = self.resolve_path(LOCK_FILE, data_dir) if self.requires_exclusivity: # check for existence of lockfile if lockfile.exists(): raise Error(f'currently running (see {lockfile} for details).') # create lockfile now = arrow.now() pid = os.getpid() lockfile.write_text( dedent(f''' started = {now!s} pid = {pid} ''').lstrip()) # open logfile if 'no-log' not in self.options: get_informer().set_logfile(self.logfile) return self
def test_mv_incense(): """move two files into a nonexistent directory""" # setup d1 = to_path('d1') f1 = to_path('f1') touch(f1) f2 = to_path('f2') touch(f2) # run test with pytest.raises(OSError): mv([f1, f2], d1) # cleanup rm(d1, f1, f2)
def test_mkdir_ground(): """make two directories""" # setup d1 = to_path('d1') d2 = to_path('d2') # run test mkdir([d1, d2]) # check assert d1.is_dir() assert d2.is_dir() # cleanup rm(d1, d2)
def test_cp_cymbal(): """copy two files to a new directory""" # setup d1 = to_path('d1') f1 = to_path('f1') touch(f1) f2 = to_path('f2') touch(f2) # run test with pytest.raises(OSError): cp(f1, f2, d1) # cleanup rm(f1, f2, d1)
def test_cp_adieu(): """copy two files to a new directory""" # setup d1 = "d1" f1 = "f1" touch(f1) f2 = "f2" touch(f2) # run test with pytest.raises(OSError): cp(f1, f2, d1) # cleanup rm(f1, f2, d1)
def test_cp_adieu(): """copy two files to a new directory""" # setup d1 = 'd1' f1 = 'f1' touch(f1) f2 = 'f2' touch(f2) # run test with pytest.raises(OSError): cp(f1, f2, d1) # cleanup rm(f1, f2, d1)
def test_cp_demystify(): """copy file to new file""" # setup f1 = 'f1' touch(f1) f2 = 'f2' # run test cp(f1, f2) # check assert to_path(f2).is_file() # cleanup rm(f1, f2)
def test_cp_downturn(): """copy file to new file""" # setup f1 = to_path('f1') touch(f1) f2 = to_path('f2') # run test cp(f1, f2) # check assert f2.is_file() # cleanup rm(f1, f2)
def test_cp_dealer(): """copy two files into a nonexistent directory""" # setup d1 = 'd1' f1 = 'f1' touch(f1) f2 = 'f2' touch(f2) # run test with pytest.raises(OSError): cp([f1, f2], d1) # cleanup rm(f1, f2)
def test_cp_downturn(): """copy file to new file""" # setup f1 = to_path("f1") touch(f1) f2 = to_path("f2") # run test cp(f1, f2) # check assert f2.is_file() # cleanup rm(f1, f2)
def test_cp_cymbal(): """copy two files to a new directory""" # setup d1 = to_path("d1") f1 = to_path("f1") touch(f1) f2 = to_path("f2") touch(f2) # run test with pytest.raises(OSError): cp(f1, f2, d1) # cleanup rm(f1, f2, d1)
def test_cp_dealer(): """copy two files into a nonexistent directory""" # setup d1 = "d1" f1 = "f1" touch(f1) f2 = "f2" touch(f2) # run test with pytest.raises(OSError): cp([f1, f2], d1) # cleanup rm(f1, f2)
def test_mv_cymbal(): """move two files to a new directory""" # setup f1 = to_path('f1') touch(f1) f2 = to_path('f2') touch(f2) d1 = to_path('d2') # run test with pytest.raises(OSError): mv(f1, f2, d1) # cleanup rm(f1, f2, d1)
def test_cp_demystify(): """copy file to new file""" # setup f1 = "f1" touch(f1) f2 = "f2" # run test cp(f1, f2) # check assert to_path(f2).is_file() # cleanup rm(f1, f2)
def test_cwd_downturn(): """change into directory""" # setup d1 = to_path('d1') mkdir(d1) # run test bef = cwd() with cd(d1): aft = cwd() delta = aft.relative_to(bef) assert str(delta) == 'd1' # cleanup rm(d1)
def test_touch_cymbal(): """touch multiple files""" # setup f1 = to_path('f1') f2 = to_path('f2') # run test touch([f1, f2]) # check assert f1.is_file() assert f2.is_file() # cleanup rm(f1, f2)
def test_mv_downturn(): """rename file""" # setup f1 = to_path('f1') touch(f1) f2 = to_path('f2') # run test mv(f1, f2) # check assert f2.is_file() assert not f1.exists() # cleanup rm(f1, f2)
def test_mv_ruminate(): """move two files into an existing file""" # setup f1 = to_path('f1') touch(f1) f2 = to_path('f2') touch(f2) f3 = to_path('f2') touch(f3) # run test with pytest.raises(OSError): mv(f1, f2, f3) # cleanup rm(f1, f2, f3)
def test_ls_narrow(): """list files with select constraint""" # setup f1 = to_path('f1') touch(f1) f2 = to_path('f2') touch(f2) # run test paths = ls(f1, f2, select='*2') # check assert set(str(f) for f in paths) == set(['f2']) # cleanup rm(f1, f2)
def test_lsd_rissole(): """list files""" # setup f1 = to_path('f1') touch(f1) f2 = to_path('f2') touch(f2) # run test files = lsd(f1, f2) # check assert set(str(f) for f in files) == set() # cleanup rm(f1, f2)
def test_ls_rissole(): """list files""" # setup f1 = to_path('f1') touch(f1) f2 = to_path('f2') touch(f2) # run test paths = ls(f1, f2) # check assert set(str(f) for f in paths) == set(['f1', 'f2']) # cleanup rm(f1, f2)
def test_cp_gathering(): """copy file into an existing directory""" # setup d1 = to_path('d1') mkdir(d1) f1 = to_path('f1') touch(f1) # run test cp(f1, d1) # check assert to_path('d1/f1').is_file() # cleanup rm(d1)