Beispiel #1
0
    def show_backups(self, restore=False):
        if not restore:
            print('\nExisting backups:')
        #relevant_files = self.client.list()[1:] # leave out first item, it's the containing folder
        all_files = self.client.list()
        all_files.sort()  # sorts by name
        relevant_files = []
        for i, resource in enumerate(all_files):
            if re.search('_(\d+)-(\d+)-(\d+)_(\d+)$', resource):
                relevant_files.append(resource)
            else:
                log.debug('Sync: Skipping resource: {}'.format(all_files[i]))

        for j, file in enumerate(relevant_files):
            file = '({}) - {}'.format(j, file)
            print(file)

        if restore:
            restore_id = ask_user('Restore backup #: ')
            try:
                restore_file = relevant_files[int(restore_id)]
            except ValueError:
                log.warning('Nothing to restore!')
                raise SystemExit
            except IndexError:
                log.warning('Non-existent ID. Nothing to restore!')
                raise SystemExit
            print('Restoring backup {}...'.format(restore_file))
            return restore_file
        print()
Beispiel #2
0
 def restore(self):
     print('\nWhich backup would you like to restore?')
     restore_filename = self.show_backups(restore=True)
     overwrite = ask_user(
         "Download backup and overwrite local file {} (n)? ".format(
             self.discobase))
     if overwrite.lower() == 'y':
         self.client.download_sync(
             remote_path='{}'.format(restore_filename),
             local_path='{}'.format(self.discobase))
         self._touch_to_backupdate(restore_filename)
Beispiel #3
0
 def restore(self):
     print('\nWhich backup would you like to restore?')
     restore_file = self.show_backups(restore=True)
     full_bak_path = '{}/{}'.format(self.backuppath, restore_file.name)
     overwrite = ask_user(
         "Download backup and overwrite local file {} (y/N)? ".format(
             self.discobase))
     if overwrite.lower() == 'y':
         self.dbx.files_download_to_file(self.discobase, full_bak_path,
                                         restore_file.rev)
         self._touch_to_backupdate(restore_file.name)
Beispiel #4
0
    def show_backups(self, restore=False):
        if not restore:
            print('\nExisting backups:')
        all_files = self.dbx.files_list_folder(path=self.backuppath)
        relevant_files = []

        for resource in all_files.entries:
            if re.search('_(\d+)-(\d+)-(\d+)_(\d+)$', resource.name):
                relevant_files.append(resource)
            else:
                log.debug('Sync: Skipping resource: {}'.format(resource.name))

        # FIXME sorting as in webdav: just by title
        #relevant_files.sort() # sorts by name
        #print(dir(relevant_files))

        for j, item in enumerate(relevant_files):
            file = '({}) - {}'.format(j, item.name)
            print(file)
            #print(item.client_modified)
            #print(item.server_modified)

        if restore:
            restore_id = ask_user('Restore backup #: ')
            try:
                restore_file = relevant_files[int(restore_id)]
            except ValueError:
                log.warning('Nothing to restore!')
                raise SystemExit
            except IndexError:
                log.warning('Non-existent ID. Nothing to restore!')
                raise SystemExit
            print('Restoring backup {}...'.format(
                restore_file.name))  # name attribute
            return restore_file  # return the whole object here
        print()
Beispiel #5
0
    def __init__(self, no_create_conf=False):
        # is set to true on initial run and config create
        self.config_created = False
        self.no_create_conf = no_create_conf
        # path handling
        # determine if application is a script file or frozen exe
        if getattr(sys, 'frozen', False):
            self.frozen = True
            log.debug("Config.frozen: Running as a bundled executable.")
            self.discodos_root = Path(os.path.dirname(sys.executable))
            self.discodos_data = create_data_dir(self.discodos_root)
        else:
            log.debug("Config.frozen: Running as a Python script.")
            self.frozen = False
            # where is our library? how are we running?
            discodos_lib = Path(os.path.dirname(os.path.abspath(__file__)))
            log.info('Config: discodos package is in: {}'.format(discodos_lib))
            # discodos_root is where our wrappers should be placed in
            # but we only want them when running frozen
            self.discodos_root = discodos_lib
            self.venv = os.getenv('VIRTUAL_ENV')
            # in unfrozen we need to find proper place for data_dir
            self.discodos_data = create_data_dir(self.discodos_root)
            # currently no difference if in venv or not - leave this for now
            if self.venv is None:
                log.info('Config: We are _not_ in a venv.')
            else:
                log.info('Config: We are running in a venv: {}'.format(
                    self.venv))
        log.info("Config.discodos_root: {}".format(self.discodos_root))
        log.info("Config.discodos_data: {}".format(self.discodos_data))

        # config.yaml path
        self.file = self.discodos_data / "config.yaml"

        # try to get a configuration from config file
        self.conf = read_yaml(self.file)
        if not self.conf:
            # on windows when user clicks Startmenu "Edit Conf...",
            # we show a popup and ask for rerun
            if self.no_create_conf and os.name == "nt":
                log.info(
                    "Config: We are running Windows and no_create_conf is set. Not creating a config file!"
                )
                import ctypes  # An included library with Python install.
                ctypes.windll.user32.MessageBoxW(
                    0,
                    "No configuration file existing yet, please run DiscoDOS first!",
                    "DiscoDOS", 0)
                raise SystemExit(0)
            # SystemExit on macOS is evil - We don't create a config, just log
            # this is invoked from open_shell_mac.py
            elif self.no_create_conf and platform.system() == "Darwin":
                log.info(
                    "Config: We are running macOS and no_create_conf is set. Not creating a config file!"
                )
            # on a shell we just create config and show steps,
            else:
                self.create_conf()
                raise SystemExit(0)

        # The only setting we _always_ try to fetch is log_level!!!
        try:  # optional setting log_level
            self.log_level = self.conf["log_level"]
            log.info("config.yaml entry log_level is {}.".format(
                self.log_level))
        except KeyError:
            self.log_level = "WARNING"
            log.warn(
                "config.yaml entry log_level not set, taking log_level from CLI option or default (WARNING)."
            )
        except:  # any other error: set INFO
            self.log_level = "WARNING"
            log.warn(
                "config.yaml not existing or other error, setting log_level to WARNING."
            )

        # Don't fetch settings when no_create_conf is set
        # (Config init from open_shell_mac.py)
        # Windows is existing above, this is only necessary for macOS because
        # SystemExit is evil in a mac app!
        if self.no_create_conf is False:
            # db file handling
            db_file = self._get_config_entry(
                'discobase_file')  # maybe configured?
            if not db_file:  # if not set, use default value
                db_file = 'discobase.db'
            self.discobase = self.discodos_data / db_file
            log.info("Config.discobase: {}".format(self.discobase))

            # then other settings
            self.discogs_appid = 'DiscoDOS/1.0 +https://github.com/JOJ0/discodos'
            self.musicbrainz_appid = [
                '1.0', 'DiscoDOS https://github.com/JOJ0/discodos'
            ]
            self.dropbox_token = self._get_config_entry('dropbox_token')
            self.musicbrainz_user = self._get_config_entry('musicbrainz_user')
            self.musicbrainz_password = self._get_config_entry(
                'musicbrainz_password')
            self.webdav_user = self._get_config_entry('webdav_user')
            self.webdav_password = self._get_config_entry('webdav_password')
            self.webdav_url = self._get_config_entry('webdav_url')

            # discogs_token is essential, bother user until we have one
            # but not when no_ask_token is set (macOS)
            self.discogs_token = self._get_config_entry('discogs_token', False)
            if self.discogs_token == '':
                token = ''
                while token == '':
                    token = ask_user("Please input discogs_token: ")
                self.conf['discogs_token'] = token
                written = self._write_yaml(self.conf, self.file)
                if written:
                    log.info('Config: config.yaml written successfully.')
                    self.discogs_token = self._get_config_entry(
                        'discogs_token', False)
                else:
                    log.error('writing config.yaml.')