Skip to content

feat: add option to copy instead of link on app level #2072

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions doc/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,23 @@ in your home folder:
cp mackup/doc/.mackup.cfg ~/
```

### Sync an application that doesn't work with symbolic links

In the application specific configuration, either as part of this repository or in your home folder's `.mackup` folder,
add the option to **not** symlink:

```ini
[application]
name = Alfred
symlink = false

[configuration_files]
Library/Preferences/com.runningwithcrayons.Alfred-Preferences.plist
Library/Preferences/com.runningwithcrayons.Alfred.plist
```

If this flag is not present, default behaviour is to create symbolic links. This is to ensure backwards compatibility.

### Get official support for an application

Open a [new issue](https://github.com/lra/mackup/issues) and ask for it, or
Expand Down
29 changes: 17 additions & 12 deletions mackup/application.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
class ApplicationProfile(object):
"""Instantiate this class with application specific data."""

def __init__(self, mackup, files, dry_run, verbose):
def __init__(self, mackup, files, symlink, dry_run, verbose):
"""
Create an ApplicationProfile instance.

Expand All @@ -27,6 +27,7 @@ def __init__(self, mackup, files, dry_run, verbose):

self.mackup = mackup
self.files = list(files)
self.symlink = symlink
self.dry_run = dry_run
self.verbose = verbose

Expand Down Expand Up @@ -104,19 +105,17 @@ def backup(self):
):
# Delete the file in Mackup
utils.delete(mackup_filepath)
# Copy the file
utils.copy(home_filepath, mackup_filepath)

if not os.path.exists(mackup_filepath):
# Copy the file
utils.copy(home_filepath, mackup_filepath)

if self.symlink:
# Delete the file in the home
utils.delete(home_filepath)
# Link the backuped file to its original place
utils.link(mackup_filepath, home_filepath)
else:
# Copy the file
utils.copy(home_filepath, mackup_filepath)
# Delete the file in the home
utils.delete(home_filepath)
# Link the backuped file to its original place
utils.link(mackup_filepath, home_filepath)

elif self.verbose:
if os.path.exists(home_filepath):
print(
Expand Down Expand Up @@ -197,9 +196,15 @@ def restore(self):
" your backup?".format(file_type, filename)
):
utils.delete(home_filepath)

if not os.path.exists(home_filepath):
if self.symlink:
# Link the backuped file to its original place
utils.link(mackup_filepath, home_filepath)
else:
utils.link(mackup_filepath, home_filepath)
else:
# Copy the backuped file to its original place
utils.copy(mackup_filepath, home_filepath)

elif self.verbose:
if os.path.exists(home_filepath):
print(
Expand Down
22 changes: 22 additions & 0 deletions mackup/appsdb.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,10 @@ def __init__(self):
app_pretty_name = config.get("application", "name")
self.apps[app_name]["name"] = app_pretty_name

# Add the symlink setting for the app
# Default to True to not break backward compatibility
self.apps[app_name]["symlink"] = config.getboolean("application", "symlink", fallback=True)

# Add the configuration files to sync
self.apps[app_name]["configuration_files"] = set()
if config.has_section("configuration_files"):
Expand Down Expand Up @@ -128,6 +132,24 @@ def get_name(self, name):
"""
return self.apps[name]["name"]

def get_setting(self, name, setting):
"""
Return a setting of an application.

Args:
name (str)
setting (str)

Returns:
str or None.
"""
if setting == "name":
return self.get_name(name)
elif setting == "symlink":
return self.apps[name]["symlink"]
else:
raise ValueError("Unknown setting: {}".format(setting))

def get_files(self, name):
"""
Return the list of config files of an application.
Expand Down
2 changes: 1 addition & 1 deletion mackup/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ def printAppHeader(app_name):

# Backup each application
for app_name in sorted(mckp.get_apps_to_backup()):
app = ApplicationProfile(mckp, app_db.get_files(app_name), dry_run, verbose)
app = ApplicationProfile(mckp, app_db.get_files(app_name), app_db.get_setting(app_name, "symlink"), dry_run, verbose)
printAppHeader(app_name)
app.backup()

Expand Down
Empty file added tests/fixtures/.config/.keep
Empty file.
2 changes: 2 additions & 0 deletions tests/fixtures/.mackup/existing_app.cfg
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
[application]
name = Existing App
3 changes: 3 additions & 0 deletions tests/fixtures/.mackup/nosymlink_app.cfg
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[application]
name = No Symlink App
symlink = false
3 changes: 3 additions & 0 deletions tests/fixtures/.mackup/symlink_app.cfg
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[application]
name = Symlink App
symlink = true
3 changes: 3 additions & 0 deletions tests/fixtures/mackup-application-no-symlink.cfg
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[applications_to_sync]
nosymlink_app

3 changes: 3 additions & 0 deletions tests/fixtures/mackup-application-symlink.cfg
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[applications_to_sync]
existing_app
symlink_app
22 changes: 22 additions & 0 deletions tests/test_config.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import unittest
import os.path

from mackup.appsdb import ApplicationsDatabase
from mackup.constants import (
ENGINE_DROPBOX,
ENGINE_GDRIVE,
Expand Down Expand Up @@ -213,3 +214,24 @@ def test_config_apps_to_ignore_and_sync(self):

def test_config_old_config(self):
self.assertRaises(SystemExit, Config, "mackup-old-config.cfg")

def test_config_symlink_setting(self):
cfg = Config("mackup-application-symlink.cfg")

os.environ["XDG_CONFIG_HOME"] = os.environ["HOME"] + "/.config"

appdb = ApplicationsDatabase()
assert appdb.get_setting("existing_app", "symlink") is True
assert appdb.get_setting("symlink_app", "symlink") is True

assert cfg.apps_to_sync == set(["existing_app", "symlink_app"])

def test_config_no_symlink_setting(self):
cfg = Config("mackup-application-no-symlink.cfg")

os.environ["XDG_CONFIG_HOME"] = os.environ["HOME"] + "/.config"

appdb = ApplicationsDatabase()
assert appdb.get_setting("nosymlink_app", "symlink") is False

assert cfg.apps_to_sync == set(["nosymlink_app"])