Created
August 15, 2013 19:26
-
-
Save akbiggs/6243948 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# This script backs up your settings from a Xamarin Studio CSharp solution file. | |
# This is useful for when your settings randomly disappear after doing some git | |
# operations, but it is really just a workaround for not having figured out why | |
# the setings are disappearing in the first place. | |
# | |
# USAGE: | |
# to backup: python backup_settings.py <solution_filename> | |
# to restore: python backup_settings.py -r (OR --restore) <solution_filename> | |
import sys | |
USAGE_BACKUP = "python %s <solution_filename>" % sys.argv[0] | |
USAGE_RESTORE = \ | |
"python %s --restore <solution_filename>" % sys.argv[0] | |
BACKUP_FILENAME = "solution_settings.backup" | |
SETTINGS_START = "GlobalSection(MonoDevelopProperties)" | |
SETTINGS_END = "EndGlobalSection" | |
def extract_settings_from(input_filename, include_tags=False): | |
with open(input_filename) as f: | |
settings = [] | |
is_extracting = False | |
for line in f: | |
if SETTINGS_START in line: | |
is_extracting = True | |
if is_extracting: | |
if should_include_setting(line, include_tags): | |
settings.append(line) | |
if SETTINGS_END in line: | |
is_extracting = False | |
return settings | |
def should_include_setting(line, include_tags): | |
return include_tags or not (SETTINGS_START in line or SETTINGS_END in line) | |
def restore_settings_on(solution_file, settings_to_restore): | |
lines_to_write = get_file_contents_with_restored_settings(solution_file, | |
settings_to_restore) | |
with open(solution_file, "w") as f: | |
for line in lines_to_write: | |
f.write(line) | |
def get_file_contents_with_restored_settings(solution_file, settings_to_restore): | |
restored_contents = [] | |
with open(solution_file, "r") as f: | |
line = f.readline() | |
while line: | |
if SETTINGS_START in line: | |
restored_contents.append(line) | |
# ignore old setting lines | |
while line and SETTINGS_END not in line: | |
line = f.readline() | |
# restore our backed up settings | |
for setting_to_restore in settings_to_restore: | |
restored_contents.append(setting_to_restore) | |
else: | |
restored_contents.append(line) | |
line = f.readline() | |
return restored_contents | |
def throw_usage_error(): | |
sys.stderr.write("usage: %s\n" % USAGE_BACKUP) | |
sys.stderr.write("\tOR %s\n" % USAGE_RESTORE) | |
if __name__ == "__main__": | |
if len(sys.argv) > 1 and (sys.argv[1] == "-r" or sys.argv[1] == "--restore"): | |
if len(sys.argv) != 3: | |
throw_usage_error() | |
else: | |
solution_file = sys.argv[2] | |
settings = extract_settings_from(BACKUP_FILENAME) | |
restore_settings_on(solution_file, settings) | |
elif len(sys.argv) > 1: | |
solution_file = sys.argv[1] | |
with open(BACKUP_FILENAME, "w") as f: | |
settings = extract_settings_from(solution_file, True) | |
for setting in settings: | |
f.write(setting) | |
else: | |
throw_usage_error() | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment