def test_mv_ground(): """rename nonexistent file""" # setup f1 = to_path('f1') f2 = to_path('f2') # run test with pytest.raises(IOError): mv(f1, f2)
def test_mv_ground(): """rename nonexistent file""" # setup f1 = to_path("f1") f2 = to_path("f2") # run test with pytest.raises(IOError): mv(f1, f2)
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_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_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_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_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_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_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_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_mv_endorse(): """rename file, replacing existing file""" # setup f1 = to_path("f1") touch(f1) f2 = to_path("f2") touch(f2) # run test mv(f1, f2) # check assert f2.is_file() assert not f1.exists() # cleanup rm(f1, f2)
def __enter__(self): # resolve src directories self.src_dirs = [to_path(self.resolve(d)) for d in self.src_dirs] # resolve repository and archive self.repository = self.resolve(self.repository) self.archive = self.resolve(self.archive) # resolve other files and directories config_dir = self.resolve(CONFIG_DIR) self.config_dir = to_path(config_dir, config_dir) logfile = self.resolve(LOG_FILE) self.logfile = to_path(config_dir, logfile) if 'no-log' not in self.options: prev_logfile = self.resolve(PREV_LOG_FILE) self.prev_logfile = to_path(config_dir, prev_logfile) rm(self.prev_logfile) if self.logfile.exists(): mv(self.logfile, self.prev_logfile) date_file = self.resolve(DATE_FILE) self.date_file = to_path(config_dir, date_file) # perform locking lockfile = self.lockfile = to_path(config_dir, self.resolve(LOCK_FILE)) if self.requires_exclusivity: # check for existance 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_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_mv_endorse(): """rename file, replacing existing file""" # setup f1 = to_path('f1') touch(f1) f2 = to_path('f2') touch(f2) # run test mv(f1, f2) # check assert f2.is_file() assert not f1.exists() # cleanup rm(f1, f2)
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_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 run(cls, command, args): # read command line cmdline = docopt(cls.USAGE, argv=[command] + args) archive_file = get_setting('archive_file') # first, save existing archive if it exists try: previous_archive = get_setting('previous_archive_file') if previous_archive and archive_file.is_file(): rm(previous_archive) mv(archive_file, previous_archive) except OSError as err: raise Error(os_error(err)) # run the generator generator = PasswordGenerator() # get dictionary that fully describes the contents of each account entries = [] for account in generator.all_accounts: entry = account.archive() if entry: entries.append(indent('%r: %s,' % ( account.get_name(), to_python(entry) ), ' ')) # build file contents from .preferences import ARCHIVE_FILE_CONTENTS import arrow contents = ARCHIVE_FILE_CONTENTS.format( encoding = get_setting('encoding'), date=str(arrow.now()), accounts = '\n\n'.join(entries) ) archive = GnuPG(archive_file) if not archive.will_encrypt(): warn('archive file is not encrypted.', culprit=archive_file) try: archive.save(contents) chmod(0o600, archive_file) except OSError as err: raise Error(os_error(err), culprit=archive_file)
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_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_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_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_mv_liaise(): """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 rename(self, extension): new = to_path(str(self.path) + extension) mv(self.path, new) self.path = new
def __enter__(self): # resolve src directories self.src_dirs = self.as_paths("src_dirs", resolve=False) # set repository repository = self.value("repository") if ":" not in repository: # is a local repository repository = to_path(repository) if not repository.is_absolute(): raise Error( "local repository must be specified using an absolute path.", culprit=repository, ) self.repository = repository # default archive if not given if "archive" not in self.settings: if "prefix" not in self.settings: self.settings[ "prefix"] = "{host_name}-{user_name}-{config_name}-" self.settings["archive"] = self.prefix + "{{now}}" # resolve other files and directories data_dir = to_path(DATA_DIR) if not data_dir.exists(): # data dir does not exist, create it data_dir.mkdir(mode=0o700, parents=True, exist_ok=True) self.date_file = data_dir / self.resolve(DATE_FILE) self.data_dir = data_dir # perform locking lockfile = self.lockfile = data_dir / self.resolve(LOCK_FILE) if self.requires_exclusivity: # check for existence of lockfile if lockfile.exists(): report = True try: lock_contents = lockfile.read_text() pid = None for l in lock_contents.splitlines(): name, _, value = l.partition("=") if name.strip().lower() == "pid": pid = int(value.strip()) assert pid > 0 os.kill(pid, 0) except ProcessLookupError as e: if e.errno == errno.ESRCH: report = False # process no longer exists except Exception as e: log("garbled lock file:", e) if report: 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 # do this after checking lock so we do not overwrite logfile # of emborg process that is currently running self.logfile = data_dir / self.resolve(LOG_FILE) if "no-log" not in self.emborg_opts: self.prev_logfile = data_dir / self.resolve(PREV_LOG_FILE) rm(self.prev_logfile) if self.logfile.exists(): mv(self.logfile, self.prev_logfile) get_informer().set_logfile(self.logfile) log("working dir =", self.working_dir) return self