def move(self):
        """
        *move the move_files_or_folders object*

        **Return:**
            - ``move_files_or_folders``
        """
        self.log.info('starting the ``move`` method')

        messages = []
        if self.moveFolderContents == False:
            message = self.move_single_file(
                sourceFile=self.source,
                destinationFile=self.destination
            )
            messages.append(message)
        else:
            fileSources, fileNames, shareUrls, meta = list_directory_contents(
                log=self.log,
                settings=self.settings,
                directoryToList=self.source
            ).get()

            fileDestinations = []
            fileDestinations[:] = [self.destination + f for f in fileNames]

            for s, d in zip(fileSources, fileDestinations):
                message = self.move_single_file(
                    sourceFile=s,
                    destinationFile=d
                )
                messages.append(message)

        self.log.info('completed the ``move`` method')
        return messages
    def copy(self):
        """
        *copy the copy_files_or_folders object*

        **Return:**
            - ``copy_files_or_folders``
        """
        self.log.info('starting the ``copy`` method')

        if self.copyFolderContents == False:
            self.copy_single_file(
                sourceFile=self.source,
                destinationFile=self.destination
            )
        else:
            fileSources, fileNames, shareUrls, meta = list_directory_contents(
                log=self.log,
                settings=self.settings,
                directoryToList=self.source
            ).get()

            fileDestinations = []
            fileDestinations[:] = [self.destination + f for f in fileNames]

            for s, d in zip(fileSources, fileDestinations):
                self.copy_single_file(
                    sourceFile=s,
                    destinationFile=d
                )

        self.log.info('completed the ``copy`` method')
        return None
Beispiel #3
0
def main(arguments=None):
    """
    *The main function used when ``cl_utils.py`` is run as a single script from the cl, or when installed as a cl command*
    """
    # setup the command-line util settings
    su = tools(
        arguments=arguments,
        docString=__doc__,
        logLevel="WARNING",
        options_first=False,
        projectName="dropterm"
    )
    arguments, settings, log, dbConn = su.setup()

    # tab completion for raw_input
    readline.set_completer_delims(' \t\n;')
    readline.parse_and_bind("tab: complete")
    readline.set_completer(tab_complete)

    # unpack remaining cl arguments using `exec` to setup the variable names
    # automatically
    for arg, val in arguments.iteritems():
        if arg[0] == "-":
            varname = arg.replace("-", "") + "Flag"
        else:
            varname = arg.replace("<", "").replace(">", "")
        if isinstance(val, str) or isinstance(val, unicode):
            val = val.replace("'", "\\'")
            exec(varname + " = '%s'" % (val,))
        else:
            exec(varname + " = %s" % (val,))
        if arg == "--dbConn":
            dbConn = val
        log.debug('%s = %s' % (varname, val,))

    ## START LOGGING ##
    startTime = times.get_now_sql_datetime()
    log.info(
        '--- STARTING TO RUN THE cl_utils.py AT %s' %
        (startTime,))

    # set options interactively if user requests
    if "interactiveFlag" in locals() and interactiveFlag:

        # load previous settings
        moduleDirectory = os.path.dirname(__file__) + "/resources"
        pathToPickleFile = "%(moduleDirectory)s/previousSettings.p" % locals()
        try:
            with open(pathToPickleFile):
                pass
            previousSettingsExist = True
        except:
            previousSettingsExist = False
        previousSettings = {}
        if previousSettingsExist:
            previousSettings = pickle.load(open(pathToPickleFile, "rb"))

        # x-raw-input
        # x-boolean-raw-input
        # x-raw-input-with-default-value-from-previous-settings

        # save the most recently used requests
        pickleMeObjects = []
        pickleMe = {}
        theseLocals = locals()
        for k in pickleMeObjects:
            pickleMe[k] = theseLocals[k]
        pickle.dump(pickleMe, open(pathToPickleFile, "wb"))

    # call the worker function
    if authenticate == True:
        this = authenticate_dropterm.authenticate_dropterm(
            log=log,
            settings=settings,
            pathToSettings=pathToSettingsFile
        )
    if move == True and contents == False:
        this = move_files_or_folders.move_files_or_folders(
            log=log,
            settings=settings,
            source=sourcePath,
            destination=destinationPath
        )
        this.move()
    if move == True and contents == True:
        this = move_files_or_folders.move_files_or_folders(
            log=log,
            settings=settings,
            source=sourcePath,
            destination=destinationPath,
            moveFolderContents=True
        )
        this.move()
    if copy == True and contents == False:

        this = copy_files_or_folders.copy_files_or_folders(
            log=log,
            settings=settings,
            source=sourcePath,
            destination=destinationPath
        )
        this.copy()
    if copy == True and contents == True:
        this = copy_files_or_folders.copy_files_or_folders(
            log=log,
            settings=settings,
            source=sourcePath,
            destination=destinationPath,
            copyFolderContents=True
        )
        this.copy()
    if ls == True:
        this = list_directory_contents(
            log=log,
            settings=settings,
            directoryToList=directoryPath
        )
        filePaths, fileNames, shareUrls, meta = this.get()
        for f in fileNames:
            print f
    if share == True:
        if lFlag == False:
            lFlag = True
        else:
            lFlag = False
        this = share_file.share_file(
            log=log,
            settings=settings,
            filePath=filePath,
            short=lFlag
        )
        url = this.get()
        print url
    if mkdir == True:
        this = mkdirectory.mkdir(
            log=log,
            settings=settings,
            dropboxPath=dirPath
        ).get()
    if touch == True:
        this = touch_file.touch_file(
            log=log,
            settings=settings,
            destination=destinationPath
        ).touch()

    if "dbConn" in locals() and dbConn:
        dbConn.commit()
        dbConn.close()
    ## FINISH LOGGING ##
    endTime = times.get_now_sql_datetime()
    runningTime = times.calculate_time_difference(startTime, endTime)
    log.info('-- FINISHED ATTEMPT TO RUN THE cl_utils.py AT %s (RUNTIME: %s) --' %
             (endTime, runningTime, ))

    return