예제 #1
0
    def build_resources(self):
        resources = []
        if not self.root_dir:
            return resources
        for root, dirs, files in os.walk(self.root_dir, followlinks=True):
            for file_name in files:
                path = os.path.join(root, file_name)
                if os.path.getsize(path) > MAX_FILESIZE_BYTES:
                    continue
                with open(path, 'rb') as f:
                    content = f.read()

                    path_for_url = pathname2url(path.replace(self.root_dir, '', 1))
                    if self.base_url[-1] == '/' and path_for_url[0] == '/':
                        path_for_url = path_for_url.replace('/', '' , 1)


                    resource_url = "{0}{1}".format(self.base_url, path_for_url)
                    resource = percy.Resource(
                        resource_url=resource_url,
                        sha=utils.sha256hash(content),
                        local_path=os.path.abspath(path),
                    )
                    resources.append(resource)
        return resources
예제 #2
0
    def build_resources(self):
        resources = []
        if not self.root_dir:
            return resources
        for root, dirs, files in os.walk(self.root_dir, followlinks=True):
            for file_name in files:
                path = os.path.join(root, file_name)
                if os.path.getsize(path) > MAX_FILESIZE_BYTES:
                    continue
                with open(path, 'rb') as f:
                    content = f.read()

                    path_for_url = pathname2url(
                        path.replace(self.root_dir, '', 1))
                    if self.base_url[-1] == '/' and path_for_url[0] == '/':
                        path_for_url = path_for_url.replace('/', '', 1)

                    resource_url = "{0}{1}".format(self.base_url, path_for_url)
                    resource = percy.Resource(
                        resource_url=resource_url,
                        sha=utils.sha256hash(content),
                        local_path=os.path.abspath(path),
                    )
                    resources.append(resource)
        return resources
예제 #3
0
    def test_sha256hash(self):
        self.assertEqual(
            utils.sha256hash('foo'),
            '2c26b46b68ffc68ff99b453c1d30413413422d706483bfa0f98a5e886266e7ae',
        )
        self.assertEqual(
            utils.sha256hash(u'I ♡ Python'),
            '5ebc381ed49ffbbff4efabd2c5e2ac5f116984cd72fffa4272cc528176bb09f6',
        )

        if sys.version_info >= (3, 0):
            binary_content = b'\x01\x02\x99'
        else:
            binary_content = '\x01\x02\x99'

        self.assertEqual(
            utils.sha256hash(binary_content),
            '17b9440d8fa6bb5eb0e06a68dd3882c01c5a757f889118f9fbdf50e6e7025581',
        )
예제 #4
0
    def __init__(self, resource_url, is_root=False, **kwargs):
        self.resource_url = resource_url
        if 'sha' in kwargs and 'content' in kwargs or not ('sha' in kwargs or 'content' in kwargs):
            raise ValueError('Exactly one of sha or content is required.')
        if 'sha' in kwargs and not 'content' in kwargs and not 'local_path' in kwargs:
            raise ValueError('If only "sha" is given, content or local_path is required.')
        self.content = kwargs.get('content')
        self.sha = kwargs.get('sha') or utils.sha256hash(self.content)
        self.is_root = is_root
        self.mimetype = kwargs.get('mimetype')

        # Convenience vars for optimizations, not included when serialized.
        self.local_path = kwargs.get('local_path')
예제 #5
0
 def upload_resource(self, build_id, content):
     sha = utils.sha256hash(content)
     data = {
         'data': {
             'type': 'resources',
             'id': sha,
             'attributes': {
                 'base64-content': utils.base64encode(content),
             }
         }
     }
     path = "{base_url}/builds/{build_id}/resources/".format(
         base_url=self.config.api_url, build_id=build_id)
     return self._connection.post(path=path, data=data)
예제 #6
0
    def __init__(self, resource_url, is_root=False, **kwargs):
        self.resource_url = resource_url
        if 'sha' in kwargs and 'content' in kwargs or not (
                'sha' in kwargs or 'content' in kwargs):
            raise ValueError('Exactly one of sha or content is required.')
        if 'sha' in kwargs and not 'content' in kwargs and not 'local_path' in kwargs:
            raise ValueError(
                'If only "sha" is given, content or local_path is required.')
        self.content = kwargs.get('content')
        self.sha = kwargs.get('sha') or utils.sha256hash(self.content)
        self.is_root = is_root
        self.mimetype = kwargs.get('mimetype')

        # Convenience vars for optimizations, not included when serialized.
        self.local_path = kwargs.get('local_path')
예제 #7
0
    def upload_resource(self, build_id, content):
        sha = utils.sha256hash(content)
        data = {
            'data': {
                'type': 'resources',
                'id': sha,
                'attributes': {
                    'base64-content': utils.base64encode(content),
                }

            }
        }
        path = "{base_url}/builds/{build_id}/resources/".format(
            base_url=self.config.api_url,
            build_id=build_id
        )
        return self._connection.post(path=path, data=data)
    def test_upload_resource(self, mock):
        mock.post('https://percy.io/api/v1/builds/123/resources/', text='{"success": "true"}')

        content = 'foo'
        result = self.percy_client.upload_resource(build_id=123, content=content)

        assert mock.request_history[0].json() == {
            'data': {
                'type': 'resources',
                'id': utils.sha256hash(content),
                'attributes': {
                    'base64-content': utils.base64encode(content)
                }

            }
        }
        assert result == {'success': 'true'}
예제 #9
0
    def test_upload_resource(self, mock):
        mock.post('https://percy.io/api/v1/builds/123/resources/', text='{"success": "true"}')

        content = 'foo'
        result = self.percy_client.upload_resource(build_id=123, content=content)

        assert mock.request_history[0].json() == {
            'data': {
                'type': 'resources',
                'id': utils.sha256hash(content),
                'attributes': {
                    'base64-content': utils.base64encode(content)
                }

            }
        }
        assert result == {'success': 'true'}
    def build_resources(self):
        resources = []
        if not self.root_dir:
            return resources
        for root, dirs, files in os.walk(self.root_dir, followlinks=True):
            for file_name in files:
                path = os.path.join(root, file_name)
                with open(path, 'rb') as f:
                    content = f.read()
                    path_for_url = path.replace(self.root_dir, '', 1)

                    resource_url = "{0}{1}".format(self.base_url, path_for_url)
                    resource = percy.Resource(
                        resource_url=resource_url,
                        sha=utils.sha256hash(content),
                        local_path=os.path.abspath(path),
                    )
                    resources.append(resource)
        return resources