示例#1
0
 def __attrs_post_init__(self):
     self.index = Index(self.path, self.rebuild)
     if not self.path or not Path(self.path).exists() or self.rebuild:
         self.root = {
             'format': 'products:1.0',
             'datatype': 'image-downloads',
             'content_id': 'images',
             'products': {}
         }
     else:
         with open(str(Path(self.path, 'images.json'))) as json_file:
             self.root = json.load(json_file)
 def test_generate_json(self):
     INDEX = {
         'format': 'index:1.0',
         'index': {
             'images': {
                 'datatype': 'image-downloads',
                 'path': 'streams/v1/images.json',
                 'format': 'products:1.0',
                 'products': ['product1', 'product2']
             }
         }
     }
     index = Index()
     index.add('product1')
     index.add('product2')
     out = json.loads(index.to_json())
     assert_that(out, is_(equal_to(INDEX)))
 def test_update_product(self):
     INDEX = {
         'format': 'index:1.0',
         'index': {
             'images': {
                 'datatype': 'image-downloads',
                 'path': 'streams/v1/images.json',
                 'format': 'products:1.0',
                 'products': ['product1']
             }
         }
     }
     index = Index()
     index.add('product1')
     index.add('iats:xenial:amd64:default')
     index.delete('iats:xenial:amd64:default')
     out = json.loads(index.to_json())
     assert_that(out, is_(equal_to(INDEX)))
示例#4
0
class Images(object):
    path = attr.ib(default=None)
    rebuild = attr.ib(default=False)
    logger = attr.ib(default=None)

    def __attrs_post_init__(self):
        self.index = Index(self.path, self.rebuild)
        if not self.path or not Path(self.path).exists() or self.rebuild:
            self.root = {
                'format': 'products:1.0',
                'datatype': 'image-downloads',
                'content_id': 'images',
                'products': {}
            }
        else:
            with open(str(Path(self.path, 'images.json'))) as json_file:
                self.root = json.load(json_file)

    def update(self, operations):
        for op in operations:
            if op.is_root:
                for product in [
                        x for x in self.root['products'] if op.name in x
                ]:
                    del self.root['products'][product]
            else:
                # Always delete for the operations and add if needed
                if op.name in self.root['products'] and \
                    op.path.split('/')[-1] in \
                        self.root['products'][op.name]['versions']:
                    del self.root['products'][op.name]['versions'][
                        op.path.split('/')[-1]]
                    if not self.root['products'][op.name]['versions']:
                        del self.root['products'][op.name]
                        self.index.delete(op.name)

                if op.operation == OperationType.ADD_MOD:
                    self._add(op.name, op.path, op.root)
                    self.index.add(op.name)

    def _load_info_from_file(self, path):
        f = Path(path, "metadata.json")
        o = {}
        if f.exists() and f.is_file():
            if self.logger:
                self.logger.debug("Loading info from {}".format(str(f)))
            try:
                with open(str(f)) as json_file:
                    o = json.load(json_file)
            except:
                if self.logger:
                    self.logger.warn("Failed to load JSON from {}".format(
                        str(f)))
        return o

    def _add(self, name, path, root):

        if Path(path).exists():
            v = Version(path.split('/')[-1], path, root)

            if name not in self.root['products']:
                fields = name.split(':')

                o = self._load_info_from_file(path)
                alias = '/'.join(fields)
                d = {
                    'versions': {},
                    'os': fields[0],
                    'release': fields[1],
                    'release_title': fields[1],
                    'arch': fields[2],
                    'aliases': alias
                }
                # direct copy fields:
                for field in ['os', 'release_title', 'aliases']:
                    if field in o:
                        d[field] = o[field]

                if alias not in d['aliases'].split(","):
                    d['aliases'] += "," + alias

                self.root['products'].update({name: d})

            self.root['products'][name]['versions'].update(v.root)

    def to_json(self):
        return json.dumps(self.root)

    def save(self):
        if self.path:
            with open(str(Path(self.path, 'images.json')), 'w') as outfile:
                self.root['last_update'] = time.time()
                json.dump(self.root, outfile)
        self.index.save()
示例#5
0
class Images(object):
    path = attr.ib(default=None)
    rebuild = attr.ib(default=False)

    def __attrs_post_init__(self):
        self.index = Index(self.path, self.rebuild)
        if not self.path or not Path(self.path).exists() or self.rebuild:
            self.root = {
                'format': 'products:1.0',
                'datatype': 'image-downloads',
                'content_id': 'images',
                'products': {}
            }
        else:
            with open(str(Path(self.path, 'images.json'))) as json_file:
                self.root = json.load(json_file)

    def update(self, operations):
        for op in operations:
            if op.is_root:
                for product in [
                        x for x in self.root['products'] if op.name in x
                ]:
                    del self.root['products'][product]
            else:
                # Always delete for the operations and add if needed
                if op.name in self.root['products'] and \
                    op.path.split('/')[-1] in \
                        self.root['products'][op.name]['versions']:
                    del self.root['products'][op.name]['versions'][
                        op.path.split('/')[-1]]
                    if not self.root['products'][op.name]['versions']:
                        del self.root['products'][op.name]
                        self.index.delete(op.name)

                if op.operation == OperationType.ADD_MOD:
                    self._add(op.name, op.path, op.root)
                    self.index.add(op.name)

    def _add(self, name, path, root):
        if Path(path).exists():
            v = Version(path.split('/')[-1], path, root)

            if name not in self.root['products']:
                fields = name.split(':')
                self.root['products'].update({
                    name: {
                        'versions': {},
                        'os': fields[0],
                        'release': fields[1],
                        'release_title': fields[1],
                        'arch': fields[2],
                        'aliases': '/'.join(fields)
                    }
                })

            self.root['products'][name]['versions'].update(v.root)

    def to_json(self):
        return json.dumps(self.root)

    def save(self):
        if self.path:
            with open(str(Path(self.path, 'images.json')), 'w') as outfile:
                self.root['last_update'] = time.time()
                json.dump(self.root, outfile)
        self.index.save()