Skip to content

Commit

Permalink
Mostly done, not sure how restoring would tie in though.
Browse files Browse the repository at this point in the history
  • Loading branch information
starcraftman committed Oct 21, 2015
1 parent 4d19720 commit 40f14ac
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 30 deletions.
17 changes: 11 additions & 6 deletions pakit/shell.py
Original file line number Diff line number Diff line change
Expand Up @@ -276,10 +276,7 @@ def common_suffix(path1, path2):
if len(parts2) < len(parts1):
parts1, parts2 = parts2, parts1

while len(parts1):
if parts1[-1] != parts2[-1]:
break

while len(parts1) and parts1[-1] == parts2[-1]:
suffix.insert(0, parts1.pop())
parts2.pop()

Expand All @@ -297,10 +294,18 @@ def link_resolve_backup(sfile, dfile, restore=False):
dfile: Destination link.
restore: If True, undo resolver action, otherwise ignore.
"""
# TODO: Mirror structure into a backup dir to restore on unlink
link_dir = dfile.replace('/' + common_suffix(sfile, dfile), '')
suffix = common_suffix(sfile, dfile)
link_dir = dfile.replace('/' + suffix, '')
logging.error('%s -> %s, %s. Restore %s',
sfile, dfile, link_dir, str(restore))
backup_file = os.path.join(link_dir, '.pakit_archive', suffix)

try:
os.makedirs(os.path.dirname(backup_file))
except OSError:
pass

shutil.move(dfile, backup_file)


def link_resolve_remove(*args):
Expand Down
67 changes: 43 additions & 24 deletions tests/test_shell.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,11 +48,7 @@ def test_hash_archive_sha256():


def test_cmd_cleanup():
try:
os.makedirs(os.path.basename(pakit.shell.TMP_DIR))
except OSError:
pass

tc.env_setup()
cmd_file = os.path.join(pakit.shell.TMP_DIR, 'cmd1')
with open(cmd_file, 'wb') as fout:
fout.write('hello'.encode())
Expand Down Expand Up @@ -80,21 +76,46 @@ def test_common_suffix():
assert common_suffix(path2, path1) == path1[1:]


# TODO: Make meaningful tests, probably class.
def test_link_resolve_backup():
sfile = os.path.join('/tmp', 'pakit', 'prefix', 'ag', 'bin', 'ag')
dfile = os.path.join('/tmp', 'pakit', 'links', 'bin', 'ag')
pakit.shell.link_resolve_backup(sfile, dfile)
class TestLinkResolve(object):
def setup(self):
config = tc.env_setup()
paths = config.get('pakit.paths')
self.src = paths['prefix']
self.dst = paths['link']
self.sfile = os.path.join(self.src, 'ag', 'bin', 'ag')
self.dfile = os.path.join(self.dst, 'bin', 'ag')

for path in [os.path.dirname(self.sfile), os.path.dirname(self.dfile)]:
try:
os.makedirs(path)
except OSError:
pass

def test_link_resolve_remove():
dfile = os.path.join('/tmp', 'pakit', 'links', 'bin', 'ag')
with pytest.raises(OSError):
pakit.shell.link_resolve_remove(0, dfile)
for fname in [self.sfile, self.dfile]:
with open(fname, 'w') as fout:
fout.write('dummy')

def teardown(self):
tc.delete_it(self.src)
tc.delete_it(self.dst)

def test_link_resolve_backup(self):
assert os.path.exists(self.dfile)
pakit.shell.link_resolve_backup(self.sfile, self.dfile)
assert not os.path.exists(self.dfile)
backup_file = os.path.join(self.dst, '.pakit_archive',
common_suffix(self.sfile, self.dfile))
assert os.path.exists(backup_file)

def test_link_resolve_fail():
pakit.shell.link_resolve_fail()
def test_link_resolve_remove(self):
assert os.path.exists(self.dfile)
pakit.shell.link_resolve_remove(self.sfile, self.dfile)
assert not os.path.exists(self.dfile)

def test_link_resolve_fail(self):
assert os.path.exists(self.dfile)
pakit.shell.link_resolve_fail(self.sfile, self.dfile)
assert os.path.exists(self.dfile)


class TestLinking(object):
Expand All @@ -103,10 +124,13 @@ def setup(self):
paths = config.get('pakit.paths')
self.src = paths['prefix']
self.dst = paths['link']
self.teardown()

self.subdir = os.path.join(self.src, 'subdir')
os.makedirs(self.subdir)

for path in [self.dst, self.subdir]:
try:
os.makedirs(path)
except OSError:
pass

self.fnames = [os.path.join(self.src, 'file' + str(num))
for num in range(0, 6)]
Expand All @@ -121,11 +145,6 @@ def setup(self):
def teardown(self):
tc.delete_it(self.src)
tc.delete_it(self.dst)
for path in [self.src, self.dst]:
try:
os.makedirs(path)
except OSError:
pass

def test_walk_and_link_works(self):
walk_and_link(self.src, self.dst)
Expand Down

0 comments on commit 40f14ac

Please sign in to comment.