Skip to content

Commit

Permalink
fix #10 and add --name cli argument and add unittests for the changes
Browse files Browse the repository at this point in the history
  • Loading branch information
jedie committed Jan 23, 2016
1 parent 3cb02bc commit 2b5b008
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 8 deletions.
10 changes: 9 additions & 1 deletion PyHardLinkBackup/phlb/path_helper.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import datetime
import os

import sys

from PyHardLinkBackup.backup_app.models import BackupRun
from PyHardLinkBackup.phlb.config import phlb_config
from PyHardLinkBackup.phlb.phlb_main import log
Expand Down Expand Up @@ -37,7 +39,7 @@ class PathHelper(object):
| `<- self.backup_name
`- phlb_config.backup_path (root dir storage for all backups runs)
"""
def __init__(self, src_path):
def __init__(self, src_path, force_name=None):
self.abs_src_root = self.abs_norm_path(src_path)
log.debug(" * abs_src_root: '%s'", self.abs_src_root)

Expand All @@ -46,6 +48,12 @@ def __init__(self, src_path):

self.src_prefix_path, self.backup_name = os.path.split(self.abs_src_root)
log.debug(" * src_prefix_path: '%s'", self.src_prefix_path)
if force_name is not None:
self.backup_name = force_name
elif not self.backup_name:
print("\nError get name for this backup!", file=sys.stderr)
print("\nPlease use '--name' for force a backup name!\n", file=sys.stderr)
sys.exit(-1)
log.debug(" * backup_name: '%s'", self.backup_name)

backup_datetime = datetime.datetime.now()
Expand Down
8 changes: 4 additions & 4 deletions PyHardLinkBackup/phlb/phlb_main.py
Original file line number Diff line number Diff line change
Expand Up @@ -164,9 +164,9 @@ def deduplication_backup(self, process_bar):


class HardLinkBackup(object):
def __init__(self, src_path):
def __init__(self, src_path, force_name=None):
self.start_time = default_timer()
self.path = PathHelper(src_path)
self.path = PathHelper(src_path, force_name)

print("Backup to: '%s'" % self.path.abs_dst_root)
os.makedirs(
Expand Down Expand Up @@ -328,9 +328,9 @@ def print_summary(self):
print("\n%s\n" % "\n".join(self.get_summary()))


def backup(path):
def backup(path, name):
django.setup()
phlb = HardLinkBackup(src_path=path)
phlb = HardLinkBackup(src_path=path, force_name=name)
phlb.print_summary()

if __name__ == '__main__':
Expand Down
5 changes: 3 additions & 2 deletions PyHardLinkBackup/phlb_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -115,10 +115,11 @@ def migrate():

@click.command()
@click.argument("path", type=click.Path(exists=True))
def backup(path):
@click.option("--name", help="Force a backup name (If not set: Use parent directory name)")
def backup(path, name=None):
"""Start a Backup run"""
from PyHardLinkBackup.phlb.phlb_main import backup
backup(path)
backup(path, name)

cli.add_command(backup)

Expand Down
29 changes: 28 additions & 1 deletion PyHardLinkBackup/tests/test_backup.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@

import sys

from click.testing import CliRunner

from PyHardLinkBackup.phlb_cli import cli
from PyHardLinkBackup.tests.base import BaseCreatedTwoBackupsTestCase, BaseCreatedOneBackupsTestCase, \
BaseSourceDirTestCase, BaseWithSourceFilesTestCase
from PyHardLinkBackup.tests.utils import UnittestFileSystemHelper
Expand Down Expand Up @@ -42,7 +45,6 @@ def test_same_size_different_content(self):

class WithSourceFilesTestCase(BaseWithSourceFilesTestCase):
def test_print_update(self):

first_run_result = self.invoke_cli("backup", self.source_path)
print(first_run_result.output)

Expand All @@ -67,6 +69,31 @@ def test_print_update(self):
self.assertIn("new content saved: 0 files (0 Bytes 0.0%)", second_run_result.output)
self.assertIn("stint space via hardlinks: 5 files (106 Bytes 100.0%)", second_run_result.output)

def test_no_backupname(self):
if sys.platform.startswith("win"):
source_path = "C:\\"
else:
source_path = "/"

runner = CliRunner()
result = runner.invoke(cli, args=["backup", source_path])

print(result.output)
self.assertIn("Error get name for this backup!", result.output)
self.assertIn("Please use '--name' for force a backup name!", result.output)

self.assertIsInstance(result.exception, SystemExit)

def test_force_name(self):
result = self.invoke_cli("backup", self.source_path, "--name", "ForcedName")
print(result.output)

fs_items=os.listdir(self.backup_path)
self.assertEqual(fs_items, ['ForcedName'])

self.assertIn("/PyHardLinkBackups/ForcedName/", result.output)



class TestTwoBackups(BaseCreatedTwoBackupsTestCase):
def test_changed_file(self):
Expand Down
4 changes: 4 additions & 0 deletions README.creole
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,10 @@ Some notes about to setup a development under windows, please look at: [[https:/
== History

* *dev* - [[https://github.com/jedie/PyHardLinkBackup/compare/v0.4.2...master|compare v0.4.2...master]]
** Fix [[https://github.com/jedie/PyHardLinkBackup/issues/10|#10]]:
*** New **--name** cli option (optional) to force a backup name.
*** Display error message if backup name can be found (e.g.: backup a root folder)
* 22.01.2016 - v0.4.2 - [[https://github.com/jedie/PyHardLinkBackup/compare/v0.4.1...v0.4.2|compare v0.4.1...v0.4.2]]
** work-a-round for junction under windows, see also: https://www.python-forum.de/viewtopic.php?f=1&t=37725&p=290429#p290428 (de)
** Bugfix in windows batches: go into work dir.
Expand Down

0 comments on commit 2b5b008

Please sign in to comment.