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 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_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_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_chmod_ground(): """change mode of a single file""" # setup f1 = 'f1' touch(f1) # run test for i in range(8): chmod(i, f1) # 0o100000 represents a regular file assert to_path(f1).stat().st_mode == 0o100000 + i chmod(8*i, f1) assert to_path(f1).stat().st_mode == 0o100000 + 8*i chmod(8*8*i, f1) assert to_path(f1).stat().st_mode == 0o100000 + 8*8*i # cleanup rm(f1)
def test_chmod_downturn(): """change mode of a single file""" # setup f1 = to_path('f1') touch(f1) # run test for i in range(8): chmod(i, f1) # 0o100000 represents a regular file assert f1.stat().st_mode == 0o100000 + i assert getmod(f1) == i chmod(8 * i, f1) assert f1.stat().st_mode == 0o100000 + 8 * i assert getmod(f1) == 8 * i chmod(8 * 8 * i, f1) assert f1.stat().st_mode == 0o100000 + 8 * 8 * i assert getmod(f1) == 8 * 8 * i # cleanup rm(f1)
def test_chmod_endorse(): """change mode of a single directory""" # setup d1 = to_path('d1') mkdir(d1) # run test for i in range(8): chmod(i, d1) # 0o040000 represents a directory assert d1.stat().st_mode == 0o040000 + i assert getmod(d1) == i chmod(8 * i, d1) assert d1.stat().st_mode == 0o040000 + 8 * i assert getmod(d1) == 8 * i chmod(8 * 8 * i, d1) assert d1.stat().st_mode == 0o040000 + 8 * 8 * i assert getmod(d1) == 8 * 8 * i # cleanup rm(d1)
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 f2.stat().st_mode == 0o100000 + i chmod(8*i, f1, f2) assert f1.stat().st_mode == 0o100000 + 8*i assert f2.stat().st_mode == 0o100000 + 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 # 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 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)