def test_access_uploaded_file(test_client, upload_test_file_response):
    # test that accessing the uploaded file gives the uploaded file data
    response = test_client.get(f"/uploads/{TEST_FILENAME}")

    assert response.status_code == 200

    with open(os.path.join(TEST_FILEPATH, TEST_FILENAME), "rb") as test_file:
        test_file_cksum = cksum(test_file)
    assert cksum(BytesIO(response.data)) == test_file_cksum
Example #2
0
    def _fetch(self,
               session: SessionWithHeaderRedirection,
               url: str,
               destination: Path,
               overwrite: bool,
               check: bool,
               ) -> Tuple[str, Union[None, Exception]]:
        """Helper function to fetch HDF files

        Args:
            session (SessionWithHeaderRedirection): requests session to fetch file.
            url (str): URL for file.
            destination (Path): Target directory.
            overwrite (bool): Overwrite existing.
            check (bool): Check file size and checksum.

        Returns:
            Tuple[str, Union[None, Exception]]: Returns tuple with
                either (filename, None) for success and (URL, Exception) for error.

        """
        filename = url.split("/")[-1]
        filename_full = destination.joinpath(filename)

        if not exists(filename_full) or overwrite:

            filename_temp = filename_full.with_suffix(".modapedl")

            try:

                with session.get(url, stream=True, allow_redirects=True) as response:
                    response.raise_for_status()
                    with open(filename_temp, "wb") as openfile:
                        shutil.copyfileobj(response.raw, openfile, length=16*1024*1024)

                if check:

                    with session.get(url + ".xml", allow_redirects=True) as response:
                        response.raise_for_status()
                        file_metadata = self._parse_hdfxml(response)

                    # check filesize
                    assert filename_temp.stat().st_size == file_metadata["FileSize"]
                    with open(filename_temp, "rb") as openfile:
                        checksum = cksum(openfile)
                    # check checksum
                    assert checksum == file_metadata["Checksum"]

                shutil.move(filename_temp, filename_full)

            except (HTTPError, ConnectionError, AssertionError, FileNotFoundError) as e:
                try:
                    filename_temp.unlink()
                except FileNotFoundError:
                    pass
                return (filename, e)
        else:
            log.info("%s exists in target. Please set overwrite to True.", filename_full)

        return (filename, None)
Example #3
0
    def post(self):
        """Receive uploaded file and save to destination directory."""
        uploaded_file = request.files["file"]
        if uploaded_file and allowed_file(uploaded_file.filename):

            filename = secure_filename(uploaded_file.filename).rstrip()
            filepath = os.path.join("app", os.getenv("BDD_UPLOAD_FOLDER"),
                                    filename)

            if os.path.isfile(filepath):
                # is this the same as a file that's already been uploaded?
                with open(filepath, "rb") as existing_file:
                    existing_file_cksum = cksum(existing_file)
                new_file_cksum = cksum(uploaded_file)
                if existing_file_cksum == new_file_cksum:
                    return {"filename": filename}, 200

                # if it's not, give the new file a unique name
                suffix = 1
                while os.path.isfile(filepath):
                    filename = re.sub(
                        r"\.\w+$",
                        f"{suffix}\g<0>",
                        filename,
                    )  # noqa W605
                    filepath = os.path.join(
                        "app",
                        os.getenv("BDD_UPLOAD_FOLDER"),
                        filename,
                    )
                    suffix += 1

            uploaded_file.save(filepath)
            return {"filename": filename}, 202
        else:
            # return error
            fname = uploaded_file.filename
            self.logger.error(
                f"Error uploading file [{fname}]: bad or nonexistent filename",
            )
            return {
                "message":
                f"Filename not valid for upload: {uploaded_file.filename}",
            }, 422
Example #4
0
def printCli(root, path):
    """Recursively prints the tree."""
    path = path + root.attrib.get('name',
                                  root.text).format().strip('\n').strip(' ')
    if path:
        path += '_'
    for elem in root.getchildren():
        if elem.tag.title() == "Class":
            printCli(elem, path)
        elif elem.tag.title() == "Method":
            argList = ""
            for arg in elem.getchildren():
                if arg.tag.title() == "Argument":
                    argList += arg.attrib.get('type',
                                              arg.text) + ":" + arg.attrib.get(
                                                  'name', arg.text) + " "

            fullPath = path + elem.attrib.get('name', elem.text)
            ck = pycksum.cksum(fullPath)
            cli_cmds_list.append(CliCmdType(C=ck, P=fullPath, A=argList))
            print '0x%08x %s %s' % \
              (ck, fullPath, argList)
Example #5
0
 def test_bytes(self):
     if sys.version_info < (3,):
         b = b'"Seen On a Bumper Sticker: I\'d give my right hand to be ambidextrous."'
         self.assertEqual(pycksum.cksum(b), 21159027)
Example #6
0
 def test_iterable(self):
     l = ["Hello", " ", "world", "\n"]
     self.assertEqual(pycksum.cksum(l), 3083891038)
Example #7
0
 def test_file(self):
     with open(self.file_path) as fd:
         self.assertEqual(pycksum.cksum(fd), 3083891038)
Example #8
0
 def test_string(self):
     self.assertEqual(pycksum.cksum(""), 4294967295)  # echo -n | cksum
     self.assertEqual(pycksum.cksum("Hello world\n"), 3083891038)  # echo "Hello world" | cksum
Example #9
0
 def test_bytes(self):
     if sys.version_info < (3, ):
         b = b'"Seen On a Bumper Sticker: I\'d give my right hand to be ambidextrous."'
         self.assertEqual(pycksum.cksum(b), 21159027)
Example #10
0
 def test_iterable(self):
     l = ["Hello", " ", "world", "\n"]
     self.assertEqual(pycksum.cksum(l), 3083891038)
Example #11
0
 def test_file(self):
     with open(self.file_path) as fd:
         self.assertEqual(pycksum.cksum(fd), 3083891038)
Example #12
0
 def test_string(self):
     self.assertEqual(pycksum.cksum(""), 4294967295)  # echo -n | cksum
     self.assertEqual(pycksum.cksum("Hello world\n"),
                      3083891038)  # echo "Hello world" | cksum