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_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 run(cls, command, args, settings, options): # read command line cmdline = docopt(cls.USAGE, argv=[command] + args) mount_point = cmdline['<mount_point>'] archive = cmdline['--archive'] date = cmdline['--date'] mount_all = cmdline['--all'] include_external_archives = cmdline['--include-external'] # get the desired archive if not archive: if date: archive = get_name_of_nearest_archive(settings, date) elif not mount_all: archive = get_name_of_latest_archive(settings) # create mount point if it does not exist try: mkdir(mount_point) except OSError as e: raise Error(os_error(e)) # run borg borg = settings.run_borg( cmd='mount', args=[settings.destination(archive), mount_point], emborg_opts=options, strip_prefix=include_external_archives, ) out = borg.stdout if out: output(out.rstrip())
def run(cls, command, args, settings, options): # read command line cmdline = docopt(cls.USAGE, argv=[command] + args) mount_point = cmdline['<mount_point>'] archive = cmdline['--archive'] date = cmdline['--date'] # get the desired archive if date and not archive: archive = get_nearest_archive(settings, date) if not archive: raise Error('archive not available.', culprit=date) # create mount point if it does not exist try: mkdir(mount_point) except OSError as e: raise Error(os_error(e)) # run borg borg = settings.run_borg( cmd='mount', args=[settings.destination(archive), mount_point], emborg_opts=options, ) out = borg.stdout if out: output(out.rstrip())
def archive_gpg(config, archive): """ Copy `~/.gnupg` into the archive. """ dest = archive / '.gnupg'; mkdir(dest) # Don't try to copy sockets (S.*); it won't work. srcs = list(ls('~/.gnupg', reject='S.*')) cp(srcs, dest)
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 __enter__(self): # change to working directory working_dir = self.value('working_dir') if not working_dir: working_dir = self.resolve(DEFAULT_WORKING_DIR) self.working_dir = to_path(working_dir) mkdir(self.working_dir) narrate('changing to working_dir:', working_dir) self.starting_dir = cd(self.working_dir).starting_dir # resolve src and dest directories src_dir = self.resolve(self.src_dir) self.src_dir = to_path(src_dir) dest_dir = self.resolve(self.dest_dir) self.dest_dir = to_path(dest_dir) # resolve other files and directories config_dir = self.resolve(CONFIG_DIR) self.config_dir = to_path(config_dir, config_dir) logfile = self.resolve(EMBALM_LOG_FILE) self.logfile = to_path(working_dir, logfile) incr_date_file = self.resolve(INCR_DATE_FILE) self.incr_date_file = to_path(working_dir, incr_date_file) full_date_file = self.resolve(FULL_DATE_FILE) self.full_date_file = to_path(working_dir, full_date_file) restore_dir = self.resolve(RESTORE_DIR) self.restore_dir = to_path(working_dir, restore_dir) archive_dir = self.resolve(ARCHIVE_DIR) self.archive_dir = to_path(working_dir, archive_dir) # perform locking if self.requires_exclusivity: # check for existance of lockfile lockfile = self.lockfile = to_path(working_dir, LOCK_FILE) 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 get_informer().set_logfile(self.logfile) return self
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_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_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_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_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_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 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 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 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_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_mkdir_endorse(): """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_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_cp_overheat(): """copy file into an existing directory""" # setup d1 = 'd1' mkdir(d1) f1 = 'f1' touch(f1) # run test cp(f1, d1) # check assert to_path('d1/f1').is_file() # cleanup rm(d1)
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)
def test_rm_cymbal(): """remove directory""" # setup d1 = to_path('d1') mkdir(d1) d1f1 = to_path('d1/f1') touch(d1f1) f2 = to_path('f2') touch(f2) # run test rm(d1, f2) # check assert not d1.exists() assert not f2.exists()
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)
def test_cp_convict(): """copy directory into an nonexistent directory""" # setup d1 = 'd1' mkdir(d1) f1 = 'd1/f1' touch(f1) d2 = 'd2' # run test cp(d1, d2) # check assert to_path('d2/f1').is_file() # cleanup rm(d1, d2)
def test_mv_gathering(): """move file into an existing directory""" # setup d1 = to_path("d1") mkdir(d1) f1 = to_path("f1") touch(f1) # run test mv(f1, d1) # check assert to_path("d1/f1").is_file() assert not f1.exists() # cleanup rm(d1, f1)
def test_cp_convict(): """copy directory into an nonexistent directory""" # setup d1 = "d1" mkdir(d1) f1 = "d1/f1" touch(f1) d2 = "d2" # run test cp(d1, d2) # check assert to_path("d2/f1").is_file() # cleanup rm(d1, d2)
def test_mv_gathering(): """move file into an existing directory""" # setup d1 = to_path('d1') mkdir(d1) f1 = to_path('f1') touch(f1) # run test mv(f1, d1) # check assert to_path('d1/f1').is_file() assert not f1.exists() # cleanup rm(d1, f1)
def test_ln_cymbal(): """link an existing directory""" # setup d1 = to_path("d1") mkdir(d1) d2 = to_path("d2") # run test ln(d1, d2) # check assert d1.is_dir() assert d2.is_dir() assert d2.is_symlink() # assert d1.samefile(d2) # cleanup rm(d1, d2)
def test_mv_swine(): """rename directory""" # setup d1 = to_path('d1') mkdir(d1) f1 = to_path('d1/f1') touch(f1) d2 = to_path('d2') # run test mv(d1, d2) # check assert to_path('d2/f1').is_file() assert not d1.exists() # cleanup rm(d1, d2)
def test_ln_cymbal(): """link an existing directory""" # setup d1 = to_path('d1') mkdir(d1) d2 = to_path('d2') # run test ln(d1, d2) # check assert d1.is_dir() assert d2.is_dir() assert d2.is_symlink() #assert d1.samefile(d2) # cleanup rm(d1, d2)
def test_chmod_cymbal(): """change mode of a single directory""" # setup d1 = 'd1' mkdir(d1) # run test for i in range(8): chmod(i, d1) # 0o040000 represents a directory assert to_path(d1).stat().st_mode == 0o040000 + i chmod(8*i, d1) assert to_path(d1).stat().st_mode == 0o040000 + 8*i chmod(8*8*i, d1) assert to_path(d1).stat().st_mode == 0o040000 + 8*8*i # cleanup rm(d1)
def test_mv_swine(): """rename directory""" # setup d1 = to_path("d1") mkdir(d1) f1 = to_path("d1/f1") touch(f1) d2 = to_path("d2") # run test mv(d1, d2) # check assert to_path("d2/f1").is_file() assert not d1.exists() # cleanup rm(d1, d2)
def test_cp_liaise(): """copy two files into an existing directory""" # setup d1 = to_path('d1') mkdir(d1) f1 = to_path('f1') touch(f1) f2 = to_path('f2') touch(f2) # run test cp([f1, f2], d1) # check assert to_path('d1/f1').is_file() assert to_path('d1/f2').is_file() # cleanup rm(d1, f1, f2)
def test_cp_headstone(): """copy directory into an existing directory""" # setup d1 = 'd1' mkdir(d1) f1 = 'd1/f1' touch(f1) d2 = 'd2' mkdir(d2) # run test cp(d1, d2) # check assert to_path('d2/d1').is_dir() assert to_path('d2/d1/f1').is_file() # cleanup rm(d1, d2)
def test_cp_quisling(): """copy two files into an existing directory""" # setup d1 = to_path("d1") mkdir(d1) f1 = to_path("f1") touch(f1) f2 = to_path("f2") touch(f2) # run test cp(f1, f2, d1) # check assert to_path("d1/f1").is_file() assert to_path("d1/f2").is_file() # cleanup rm(d1)
def test_cp_hairbrush(): """copy two files into an existing directory""" # setup d1 = 'd1' mkdir(d1) f1 = 'f1' touch(f1) f2 = 'f2' touch(f2) # run test cp([f1, f2], d1) # check assert to_path('d1/f1').is_file() assert to_path('d1/f2').is_file() # cleanup rm(d1, f1, f2)
def test_cp_calculate(): """copy two files into an existing directory""" # setup d1 = 'd1' mkdir(d1) f1 = 'f1' touch(f1) f2 = 'f2' touch(f2) # run test cp(f1, f2, d1) # check assert to_path('d1/f1').is_file() assert to_path('d1/f2').is_file() # cleanup rm(d1)
def test_cp_headstone(): """copy directory into an existing directory""" # setup d1 = "d1" mkdir(d1) f1 = "d1/f1" touch(f1) d2 = "d2" mkdir(d2) # run test cp(d1, d2) # check assert to_path("d2/d1").is_dir() assert to_path("d2/d1/f1").is_file() # cleanup rm(d1, d2)
def test_cp_hairbrush(): """copy two files into an existing directory""" # setup d1 = "d1" mkdir(d1) f1 = "f1" touch(f1) f2 = "f2" touch(f2) # run test cp([f1, f2], d1) # check assert to_path("d1/f1").is_file() assert to_path("d1/f2").is_file() # cleanup rm(d1, f1, f2)
def test_mv_mobilize(): """move directory into an existing directory""" # setup d1 = to_path('d1') mkdir(d1) f1 = to_path('d1/f1') touch(f1) d2 = to_path('d2') mkdir(d2) # run test mv(d1, d2) # check assert to_path('d2/d1').is_dir() assert to_path('d2/d1/f1').is_file() assert not d1.exists() # cleanup rm(d1, d2)
def test_cd_quoit(): """change into directory""" # setup dot = cwd() d1 = 'd1' mkdir(d1) d1p = to_path(d1).resolve() d1f1 = 'd1/f1' touch(d1f1) f1 = 'f1' rm(f1) # run test with cd(d1): assert to_path(f1).is_file() assert str(d1p) == str(cwd()) assert str(dot) == str(cwd()) # clean up rm(d1)
def test_cd_downturn(): """change into directory""" # setup dot = cwd() d1 = to_path('d1') mkdir(d1) d1p = to_path(d1).resolve() d1f1 = to_path('d1/f1') touch(d1f1) f1 = to_path('f1') rm(f1) # run test cd(d1) assert to_path(f1).is_file() assert str(d1p) == str(cwd()) cd('..') assert str(dot) == str(cwd()) # clean up rm(d1)
def test_mv_quisling(): """move two files into an existing directory""" # setup d1 = to_path('d1') mkdir(d1) f1 = to_path('f1') touch(f1) f2 = to_path('f2') touch(f2) # run test mv(f1, f2, d1) # check assert to_path('d1/f1').is_file() assert to_path('d1/f2').is_file() assert not f1.exists() assert not f2.exists() # cleanup rm(d1, 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 d2.stat().st_mode == 0o040000 + i chmod(8*i, d1, d2) assert d1.stat().st_mode == 0o040000 + 8*i assert d2.stat().st_mode == 0o040000 + 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 # cleanup rm(d1)
def test_lsd_island(): """list a directory that contains dot files with select constraint""" # setup d1 = to_path('d1') mkdir(d1) d1f1 = to_path('d1/.f1') touch(d1f1) d1f2 = to_path('d1/.f2') touch(d1f2) d1d1 = to_path('d1/.d1') mkdir(d1d1) d1d2 = to_path('d1/.d2') mkdir(d1d2) # run test files = lsd(d1, select='.*') # check assert set(str(f) for f in files) == set(['d1/.d1', 'd1/.d2']) # cleanup rm(d1)
def test_ls_manicure(): """list a directory that contains dot files with select constraint""" # setup d1 = to_path('d1') mkdir(d1) d1f1 = to_path('d1/.f1') touch(d1f1) d1f2 = to_path('d1/.f2') touch(d1f2) d1d1 = to_path('d1/.d1') mkdir(d1d1) d1d2 = to_path('d1/.d2') mkdir(d1d2) # run test paths = ls(d1) # check assert set(str(f) for f in paths) == set([]) # cleanup rm(d1)
def test_ls_downturn(): """list a directory""" # setup d1 = to_path('d1') mkdir(d1) d1f1 = to_path('d1/f1') touch(d1f1) d1f2 = to_path('d1/f2') touch(d1f2) d1d1 = to_path('d1/d1') mkdir(d1d1) d1d2 = to_path('d1/d2') mkdir(d1d2) # run test paths = ls(d1) # check assert set(str(f) for f in paths) == set(['d1/d1', 'd1/d2', 'd1/f1', 'd1/f2']) # cleanup rm(d1)
def test_ls_endorse(): """list a directory with select constraint""" # setup d1 = to_path('d1') mkdir(d1) d1f1 = to_path('d1/f1') touch(d1f1) d1f2 = to_path('d1/f2') touch(d1f2) d1d1 = to_path('d1/d1') mkdir(d1d1) d1d2 = to_path('d1/d2') mkdir(d1d2) # run test paths = ls(d1, select='*2') # check assert set(str(f) for f in paths) == set(['d1/d2', 'd1/f2']) # cleanup rm(d1)
def test_ls_contrast(): """recursive list of directories in directory""" # setup 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(d1, select='**') # check assert set(str(f) for f in paths) == set(['d1', 'd1/d1', 'd1/d2']) # cleanup rm(d1)
def test_ls_cadge(): """list a directory that contains dot files while discarding hidden""" # setup d1 = to_path('d1') mkdir(d1) d1f1 = to_path('d1/.f1') touch(d1f1) d1f2 = to_path('d1/f2') touch(d1f2) d1d1 = to_path('d1/.d1') mkdir(d1d1) d1d2 = to_path('d1/d2') mkdir(d1d2) # run test paths = ls(d1, hidden=False) # check assert set(str(f) for f in paths) == set(['d1/d2', 'd1/f2']) # cleanup rm(d1)
def test_lsf_manicure(): """list a directory that contains dot files with select constraint""" # setup d1 = to_path("d1") mkdir(d1) d1f1 = to_path("d1/.f1") touch(d1f1) d1f2 = to_path("d1/.f2") touch(d1f2) d1d1 = to_path("d1/.d1") mkdir(d1d1) d1d2 = to_path("d1/.d2") mkdir(d1d2) # run test files = lsf(d1) # check assert set(str(f) for f in files) == set() # cleanup rm(d1)