Esempio n. 1
0
    def save_file(self, relative_path, content, overwrite=False):
        """Save the file locally."""
        path = self.base_path + util.rpath(relative_path)

        # move file instead of overwriting it
        if self.exists(relative_path) and not overwrite:
            to = self.base_path + util.rpath(BaseSaver.OVERW_FOLDER + relative_path)
            shutil.move(path, to)

        # save file
        with open(path, 'wb') as file:
            try:
                file.write(content)
                return True
            except IOError:
                return False
Esempio n. 2
0
    def exists(self, relative_path):
        """Check whether a file or a folder already exists at the given path relative in Dropbox."""
        path = util.dbpath(self.base_path + util.rpath(relative_path))

        try:
            self.dbx.files_get_metadata(path)
        except ApiError:
            return False
        return True
Esempio n. 3
0
    def download_file(self, relative_download_path, destination_path):
        """Download a file located at relative_download_path and
           save it at destination_path."""
        down = util.dbpath(self.base_path + util.rpath(relative_download_path))
        dest = destination_path

        # download file
        with open(dest, 'wb') as f:
            metadata, res = self.dbx.files_download(down)
            f.write(res.content)
Esempio n. 4
0
    def save_file(self, relative_path, content, mute=False, overwrite=False):
        """Save the file in Dropbox by uploading it with the Dropbox API."""
        path = util.dbpath(self.base_path + util.rpath(relative_path))

        file_size = len(content)
        large_file = file_size > CHUNK

        # handle potential overwriting
        if not overwrite:  # default
            upload_mode = dropbox.files.WriteMode.add
        else:  # allow overwriting
            upload_mode = dropbox.files.WriteMode.overwrite

        # if file exists and it should not be overwritten or it is a large file
        # move it to the overwritten folder to avoid overwriting the existing file
        if self.exists(relative_path) and (not overwrite or large_file):
            try:
                self.move_file(relative_path,
                               BaseSaver.OVERW_FOLDER + relative_path)
            except ApiError as err:
                print('Moving {} failed due to:\n{}\n'.format(path, err))
                return False

        try:
            # file is uploaded as a whole
            if not large_file:
                self.dbx.files_upload(content,
                                      path,
                                      mute=mute,
                                      mode=upload_mode)
                return True

            # file exceeds size CHUNK, upload in smaller chunks
            else:
                f = io.BytesIO(content)
                result = self.dbx.files_upload_session_start(f.read(CHUNK))
                cursor = dropbox.files.UploadSessionCursor(
                    session_id=result.session_id, offset=f.tell())
                commit = dropbox.files.CommitInfo(path=path)

                while f.tell() < int(file_size):
                    if (int(file_size) - f.tell()) <= CHUNK:
                        self.dbx.files_upload_session_finish(
                            f.read(CHUNK), cursor, commit)
                        return True
                    else:
                        self.dbx.files_upload_session_append(
                            f.read(CHUNK), cursor.session_id, cursor.offset)
                        cursor.offset = f.tell()

        except ApiError as err:
            print('Uploading {} failed due to:\n{}\n'.format(path, err))
            return False
Esempio n. 5
0
 def move_file(self, relative_from_path, relative_to_path):
     """Move a file from relative_from_path to relative_to_path."""
     fr = util.dbpath(self.base_path + util.rpath(relative_from_path))
     to = util.dbpath(self.base_path + util.rpath(relative_to_path))
     self.dbx.files_move(fr, to, autorename=True)
Esempio n. 6
0
 def create_folder(self, relative_path):
     """Creating a folder at the given path in Dropbox."""
     if not self.exists(relative_path):
         path = util.dbpath(self.base_path + util.rpath(relative_path))
         self.dbx.files_create_folder_v2(path=path, autorename=False)
Esempio n. 7
0
def unit_test(bld, sources, incs=(), uses=(), csources=()):
    '''Register unit tests.

    Example usage

    >>> def build(bld):
    >>>     tsrcs=bld.path.ant_glob("test/test*.cpp")
    >>>     bld.unit_test(tsrcs, "inc src")

    The sources should list all unit test main source files.
    
    The incs may give any special include directories such as the
    source directory if tests require access to private headers.

    The uses can specify additional dependencies beyond what the
    package requires.

    The csources are like sources but for to make "check" programs
    which are not executed as unit tests and not installed but
    available under build directory to run for local checking.

    '''
    sources = util.listify(sources)
    incs = util.listify(incs)

    me = getattr(Context.g_module, 'APPNAME', None)
    uses = util.listify(uses) + util.uses(bld)
    if me:
        uses.insert(0, me)

    if bld.options.no_tests:
        return
    if not sources:
        return

    features = 'test cxx'
    if bld.options.quell_tests:
        features = 'cxx'

    rpath = util.rpath(bld) + [bld.path.find_or_declare(bld.out_dir)]

    for tmain in sources:
        bld.program(features=features,
                    source=[tmain],
                    target=tmain.name.replace('.cpp', ''),
                    ut_cwd=bld.path,
                    install_path=None,
                    includes=incs,
                    rpath=rpath,
                    use=uses)

    for cmain in csources:
        bld.program(features='cxx',
                    source=[cmain],
                    target=cmain.name.replace('.cpp', ''),
                    ut_cwd=bld.path,
                    install_path=None,
                    includes=incs,
                    rpath=rpath,
                    use=uses)

    bld.add_post_fun(waf_unit_test.summary)
Esempio n. 8
0
 def create_folder(self, relative_path):
     """Creating a folder at the given relative path."""
     if not self.exists(relative_path):
         path = self.base_path + util.rpath(relative_path)
         os.makedirs(path)
Esempio n. 9
0
 def exists(self, relative_path):
     """Check whether a file or a folder already exists at the given relative path."""
     path = self.base_path + util.rpath(relative_path)
     return os.path.exists(path)