Beispiel #1
0
    def clean_data_dir(self):
        '''
            Clean the data directory.

            >>> from tempfile import gettempdir
            >>> prefs = PreferencesForm()
            >>> prefs.cleaned_data = {'data_dir': os.path.join(gettempdir(), 'bitcoin/data')}
            >>> prefs.clean_data_dir()
            '/tmp/bitcoin/data'
            >>> prefs.cleaned_data = {'data_dir': None}
            >>> data_dir = prefs.clean_data_dir()
            >>> data_dir.endswith('.bitcoin')
            True
        '''

        data_dir = self.cleaned_data['data_dir']
        if data_dir is None:
            data_dir = os.path.join(getdir(), '.bitcoin')
            if not os.path.exists(data_dir):
                os.makedirs(data_dir)
            self.cleaned_data['data_dir'] = data_dir

        ok, error = data_dir_ok(data_dir)

        if ok and error is None:
            data_dir = self.cleaned_data['data_dir']
        else:
            self.log(error)
            raise ValidationError(error)

        return data_dir
Beispiel #2
0
def get_data_dir():
    '''
        Get the data dir from the prefs.

        >>> from blockchain_backup.bitcoin.tests import utils as test_utils
        >>> test_utils.init_database()
        >>> get_data_dir()
        '/tmp/bitcoin/data/testnet3'
    '''

    data_dir = None
    try:
        prefs = get_preferences()
        data_dir = prefs.data_dir
    except:  # 'bare except' because it catches more than "except Exception"
        log(format_exc())

    if data_dir is None:
        data_dir = os.path.join(getdir(), '.bitcoin')

    use_test_net = '-testnet' in get_extra_args()
    if use_test_net and not data_dir.rstrip(os.sep).endswith(
            constants.TEST_NET_DIR):
        data_dir = os.path.join(data_dir, constants.TEST_NET_DIR)

    return data_dir
Beispiel #3
0
    def get_page(self, request):

        try:
            prefs = preferences.get_preferences()
        except OperationalError as oe:
            report_operational_error(oe)

        try:
            if prefs.data_dir is None:
                prefs.data_dir = os.path.join(getdir(), '.bitcoin')

            if prefs.backup_dir is None and prefs.data_dir is not None:
                prefs.backup_dir = os.path.join(prefs.data_dir,
                                                constants.DEFAULT_BACKUPS_DIR)

            if prefs.bin_dir is None:
                prefs.bin_dir = bitcoin_utils.get_path_of_core_apps()

            form = PreferencesForm(instance=prefs)
        except:  # 'bare except' because it catches more than "except Exception"
            log(format_exc())
            form = PreferencesForm()

        return render(request, self.form_url, {
            'form': form,
            'context': bitcoin_utils.get_blockchain_context()
        })
Beispiel #4
0
from time import sleep

from django.core.management import call_command
from django.utils.timezone import now, utc

from blockchain_backup.bitcoin import constants, preferences, state
from blockchain_backup.bitcoin import utils as bitcoin_utils
from blockchain_backup.bitcoin.models import Preferences, State
from blockchain_backup.settings import PROJECT_PATH, TIME_ZONE
from denova.os import command
from denova.os.user import getdir
from denova.python.log import get_log, get_log_path
from denova.python.ve import virtualenv_dir


HOME_BITCOIN_DIR = os.path.join(getdir(), '.bitcoin')
DATA_WITH_BLOCKS_DIR = '/tmp/bitcoin/data-with-blocks'
INITIAL_DATA_DIR = '/tmp/bitcoin/data-initial'

log = get_log()

def setup_tmp_dir():
    '''
        Set up a temporary test directory.

        >>> setup_tmp_dir()
    '''

    TEST_ENV_PATH = os.path.join(PROJECT_PATH, '..', 'test-env')
    TEST_BITCOIN_ENV_PATH = os.path.join(TEST_ENV_PATH, 'bitcoin')
Beispiel #5
0
def venv(dirname=None, django_app=None, restore=True):
    ''' Context manager to activate a virtualenv.

        Example::

            from ve import venv

            with venv():

                # imports delayed until in virtualenv
                import ...

                ... code to run in virtualenv ...

        'dirname' is the directory where venv will start the search for
        a virtualenv. The default dirname is the current dir. The search
        includes dirname and its parent dirs.

        'django_app' is the django module with settings.py. Do not confuse
        this with a django subapp, also confusingly called a django app.

        To activate a virtualenv once for a module, use activate().

        This context manager will:
           * Set the VIRTUAL_ENV environment variable to the virtualenv dir
           * Set the current dir to the virtualenv dir
           * Prepend virtualenv/bin to the os environment PATH
           * Prepend sites-packages to sys.path
           * Optionally set the DJANGO_SETTINGS_MODULE environment variable

        If dir is not included or is None, ve searches for a virtualenv.
        See virtualenv_dir().

       On exiting the context, any changes to these system environment
       variables or python's sys.path are lost.

        Virtualenv's 'bin/activate' doesn't work well with fabric. See
        http://stackoverflow.com/questions/1691076/activate-virtualenv-via-os-system
    '''

    # venv is called from denova/wsgi_django.py every time
    # gunicorn restarts (starts?) a worker # DEBUG
    # from denova.python.utils import stacktrace # DEBUG
    # debug(f'called venv() from:\n{stacktrace()}') # DEBUG

    old_virtualenv = os.environ.get('VIRTUAL_ENV')
    old_settings_module = os.environ.get('DJANGO_SETTINGS_MODULE')
    old_path = os.environ.get('PATH')
    old_python_path = list(sys.path)
    try:
        old_cwd = os.getcwd()
    except:   # 'bare except' because it catches more than "except Exception"
        # import late so environment is configured properly
        from denova.os.user import getdir

        old_cwd = getdir()

    if dirname:
        debug(f'starting dirname: {dirname}')
    else:
        debug(f'set dirname to cwd: {old_cwd}')
        dirname = old_cwd

    venv_dir = virtualenv_dir(dirname)
    debug(f'set up virtual_env: {dirname}')

    os.environ['VIRTUAL_ENV'] = venv_dir

    bin_dir = os.path.join(venv_dir, 'bin')
    path_dirs = os.environ['PATH'].split(':')
    if bin_dir not in path_dirs:
        new_path = ':'.join([bin_dir] + path_dirs)
        os.environ['PATH'] = new_path

    if django_app:
        os.environ['DJANGO_SETTINGS_MODULE'] = f'{django_app}.settings'
        debug(f'django settings: {django_app}')

    os.chdir(venv_dir)

    sys.path = venv_sys_path(venv_dir)

    try:
        yield

    finally:

        # activate() isn't a context manager, so it sets restore=False
        if restore:

            debug('finally restoring environment')
            if old_virtualenv:
                os.environ['VIRTUAL_ENV'] = old_virtualenv
            else:
                del os.environ['VIRTUAL_ENV']

            os.environ['PATH'] = old_path

            if old_settings_module:
                os.environ['DJANGO_SETTINGS_MODULE'] = old_settings_module
            else:
                if 'DJANGO_SETTINGS_MODULE' in os.environ:
                    del os.environ['DJANGO_SETTINGS_MODULE']

            try_to_cd_back(old_cwd)

            sys.path[:] = old_python_path

    debug('finished venv()')