Merge "Import repo to temp dir"
This commit is contained in:
commit
0220411933
@ -14,15 +14,15 @@
|
|||||||
# under the License.
|
# under the License.
|
||||||
|
|
||||||
from collections import defaultdict
|
from collections import defaultdict
|
||||||
|
import tempfile
|
||||||
|
|
||||||
|
from enum import Enum
|
||||||
import errno
|
import errno
|
||||||
import os
|
import os
|
||||||
import semantic_version
|
import semantic_version
|
||||||
import shutil
|
import shutil
|
||||||
import yaml
|
import yaml
|
||||||
|
|
||||||
|
|
||||||
from enum import Enum
|
|
||||||
from solar import utils
|
from solar import utils
|
||||||
|
|
||||||
|
|
||||||
@ -63,6 +63,7 @@ class Repository(object):
|
|||||||
|
|
||||||
db_obj = None
|
db_obj = None
|
||||||
_REPOS_LOCATION = '/var/lib/solar/repositories'
|
_REPOS_LOCATION = '/var/lib/solar/repositories'
|
||||||
|
_TMP_DIRNAME = '.tmp'
|
||||||
|
|
||||||
def __init__(self, name):
|
def __init__(self, name):
|
||||||
self.name = name
|
self.name = name
|
||||||
@ -146,15 +147,23 @@ class Repository(object):
|
|||||||
os.mkdir(self.fpath)
|
os.mkdir(self.fpath)
|
||||||
return
|
return
|
||||||
if not link_only:
|
if not link_only:
|
||||||
|
if os.path.isdir(self.fpath):
|
||||||
|
raise RepositoryExists("Repository %s "
|
||||||
|
"already exists" % self.name)
|
||||||
|
|
||||||
|
if not os.path.isdir(self.tmp_dir):
|
||||||
|
os.makedirs(self.tmp_dir)
|
||||||
|
|
||||||
|
old_fpath = self.fpath
|
||||||
|
self.fpath = tempfile.mkdtemp(dir=self.tmp_dir)
|
||||||
try:
|
try:
|
||||||
os.mkdir(self.fpath)
|
self._add_contents(source)
|
||||||
except OSError as e:
|
os.rename(self.fpath, old_fpath)
|
||||||
if e.errno == errno.EEXIST:
|
except Exception as e:
|
||||||
raise RepositoryExists("Repository %s "
|
shutil.rmtree(self.fpath)
|
||||||
"already exists" % self.name)
|
raise
|
||||||
else:
|
finally:
|
||||||
raise
|
self.fpath = old_fpath
|
||||||
self._add_contents(source)
|
|
||||||
else:
|
else:
|
||||||
try:
|
try:
|
||||||
os.symlink(source, self.fpath)
|
os.symlink(source, self.fpath)
|
||||||
@ -244,7 +253,7 @@ class Repository(object):
|
|||||||
return
|
return
|
||||||
|
|
||||||
if resource_name is None:
|
if resource_name is None:
|
||||||
for single in os.listdir(self.fpath):
|
for single in self.list_repos():
|
||||||
for gen in _single(single):
|
for gen in _single(single):
|
||||||
yield gen
|
yield gen
|
||||||
else:
|
else:
|
||||||
@ -335,6 +344,10 @@ class Repository(object):
|
|||||||
spec = self._parse_spec(spec)
|
spec = self._parse_spec(spec)
|
||||||
return self._make_version_path(spec)
|
return self._make_version_path(spec)
|
||||||
|
|
||||||
|
@property
|
||||||
|
def tmp_dir(self):
|
||||||
|
return os.path.join(self._REPOS_LOCATION, self._TMP_DIRNAME)
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
def get_metadata(cls, spec):
|
def get_metadata(cls, spec):
|
||||||
spec = cls._parse_spec(spec)
|
spec = cls._parse_spec(spec)
|
||||||
@ -365,10 +378,11 @@ class Repository(object):
|
|||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
def list_repos(cls):
|
def list_repos(cls):
|
||||||
return filter(lambda x:
|
return filter(
|
||||||
os.path.isdir(os.path.join(cls._REPOS_LOCATION,
|
lambda x: (os.path.isdir(os.path.join(cls._REPOS_LOCATION, x))
|
||||||
x)),
|
and x != cls._TMP_DIRNAME),
|
||||||
os.listdir(cls._REPOS_LOCATION))
|
os.listdir(cls._REPOS_LOCATION)
|
||||||
|
)
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
def parse(cls, spec):
|
def parse(cls, spec):
|
||||||
|
@ -42,9 +42,8 @@ def resources():
|
|||||||
@pytest.fixture(scope='session', autouse=True)
|
@pytest.fixture(scope='session', autouse=True)
|
||||||
def repos_path(tmpdir_factory):
|
def repos_path(tmpdir_factory):
|
||||||
Repository._REPOS_LOCATION = str(tmpdir_factory.mktemp('repositories'))
|
Repository._REPOS_LOCATION = str(tmpdir_factory.mktemp('repositories'))
|
||||||
path = Repository._REPOS_LOCATION
|
|
||||||
repo = Repository('resources')
|
repo = Repository('resources')
|
||||||
repo.create(path)
|
repo.create()
|
||||||
|
|
||||||
|
|
||||||
def plan_from_fixture(name):
|
def plan_from_fixture(name):
|
||||||
|
@ -14,9 +14,12 @@
|
|||||||
# under the License.
|
# under the License.
|
||||||
|
|
||||||
import itertools
|
import itertools
|
||||||
|
import mock
|
||||||
import os
|
import os
|
||||||
import pytest
|
|
||||||
import shutil
|
import shutil
|
||||||
|
|
||||||
|
import pytest
|
||||||
|
|
||||||
from solar.core.resource.repository import Repository
|
from solar.core.resource.repository import Repository
|
||||||
from solar.core.resource.repository import RES_TYPE
|
from solar.core.resource.repository import RES_TYPE
|
||||||
|
|
||||||
@ -243,3 +246,36 @@ def test_create_empty():
|
|||||||
repo = Repository('empty')
|
repo = Repository('empty')
|
||||||
repo.create()
|
repo.create()
|
||||||
assert 'empty' in Repository.list_repos()
|
assert 'empty' in Repository.list_repos()
|
||||||
|
|
||||||
|
|
||||||
|
@mock.patch('solar.core.resource.repository.Repository._add_contents')
|
||||||
|
@mock.patch('tempfile.mkdtemp')
|
||||||
|
@mock.patch('shutil.rmtree')
|
||||||
|
def test_create_from_src_failed(mock_rmtree, mock_mkdtemp, mock_add_contents):
|
||||||
|
tmp_dir = '/tmp/dir'
|
||||||
|
mock_mkdtemp.return_value = tmp_dir
|
||||||
|
|
||||||
|
mock_add_contents.side_effect = Exception()
|
||||||
|
repo = Repository('fail_create')
|
||||||
|
real_path = repo.fpath
|
||||||
|
with pytest.raises(Exception):
|
||||||
|
repo.create(source='source')
|
||||||
|
|
||||||
|
mock_rmtree.assert_called_with(tmp_dir)
|
||||||
|
assert repo.fpath == real_path
|
||||||
|
|
||||||
|
|
||||||
|
@mock.patch('solar.core.resource.repository.Repository._add_contents')
|
||||||
|
@mock.patch('os.rename')
|
||||||
|
@mock.patch('tempfile.mkdtemp')
|
||||||
|
def test_create_from_src(mock_mkdtemp, mock_rename, _):
|
||||||
|
tmp_dir = '/tmp/dir'
|
||||||
|
mock_mkdtemp.return_value = tmp_dir
|
||||||
|
|
||||||
|
repo = Repository('create_from_src')
|
||||||
|
real_path = repo.fpath
|
||||||
|
|
||||||
|
repo.create(source='source')
|
||||||
|
|
||||||
|
mock_rename.assert_called_with(tmp_dir, real_path)
|
||||||
|
assert repo.fpath == real_path
|
||||||
|
Loading…
x
Reference in New Issue
Block a user