Ejemplo n.º 1
0
    def loadOptionsFromFile(cls, configFilePath):
        found = True
        if not configFilePath:
            currentDir = os.getcwd()
            found = False
            while not found:
                configFilePath = os.path.join(currentDir, "snowconf.py")
                if os.path.isfile(configFilePath):
                    found = True
                else:
                    if cls.__isRootDir(currentDir):
                        break
                    currentDir = os.path.dirname(currentDir)

        elif not os.path.isfile(configFilePath):
            SLogger.warning("Config file not found at %s" % configFilePath)
            sys.exit(1)

        if found:
            cls.__configFilePath = os.path.normpath(os.path.abspath(configFilePath))
            loadedOptions = {}
            execfile(cls.__configFilePath, globals(), loadedOptions)
            cls.__setLoadedOptions(loadedOptions)
        else:
            cls.__configFilePath = None
Ejemplo n.º 2
0
 def __scheduleDir(cls, dirPath, rootDirPath):
     if not directoryShouldBeIgnored(dirPath):
         SLogger.debug("Watching dir %s" % os.path.abspath(dirPath))
         lockFilePath = os.path.abspath(os.path.join(dirPath, Config.getLockFilename()))
         if os.path.exists(lockFilePath):
             SLogger.warning("%s already exists. Another swatch might be watching this directory or might not have shut down correctly. Consider using the scleanlockfiles command." % (lockFilePath))
             if dirPath == rootDirPath:
                 Commands.warning("Directory already watched", "Another swatch might be watching or might not have shut down correctly.")
         else:
             lockFile = open(lockFilePath, "w")
             lockFile.close()
             cls.__lockFiles.append(lockFilePath)
         
         cls.__observer.schedule(cls.__handler, path=dirPath)
Ejemplo n.º 3
0
    def __shutdown(cls):
        cls.__observer.stop()
        cls.__dbWorker.stop()

        SLogger.debug("Deleting lock files...")
        for lockFilePath in cls.__lockFiles:
            try:
                os.remove(lockFilePath)
            except OSError:
                SLogger.warning("Lock file %s could not be deleted" % lockFilePath)

        cls.__observer.join()
        cls.__dbWorker.join()

        SLogger.debug("Stopped")
Ejemplo n.º 4
0
    def run(cls, targetURL):
        instance, tableName, query = cls.__parseURL(targetURL)
        nameField, contentField, fileExtension = cls._getTableInfo(tableName)

        localDir = os.getcwd()
        db = SnowFileDB(localDir)
        client = SnowClient(tableName, instance)

        records = client.getRecords(query)

        for record in records:
            recordName = getattr(record, nameField)
            SLogger.debug("Downloading %s ... " % recordName)
            fileName = "%s.%s" % (
                getattr(record, nameField).replace(" ", "-").replace("/", "-").replace("(", "[").replace(")", "]"),
                fileExtension,
            )
            filePath = os.path.abspath(os.path.join(localDir, fileName))
            if os.path.exists(filePath):
                good = False
                suffix = 2
                while not good:
                    filePathNew = filePath + "_" + str(suffix)
                    if not os.path.exists(filePathNew):
                        good = True
                        filePath = filePathNew
                    else:
                        suffix = suffix + 1
                        SLogger.warning("Local file %s already exists, will not overwrite" % filePath)
                        continue

            try:
                content = normalizeNewlines(getattr(record, contentField))
                f = codecs.open(filePath, "w", "utf-8")
                f.write(content)
                f.close()
                db.addFileInfo(
                    FileInfo(
                        fileName, instance, tableName, record.sys_id, nameField, contentField, record.sys_updated_on
                    )
                )
                SLogger.debug("Done. Written to %s" % fileName)
            except Exception:
                SLogger.debug("Problem with this record, will not write")

        db.commitAndClose()
Ejemplo n.º 5
0
    def processFile(self, filePath):
        ignoreWatchFilePath = filePath + Config.getIgnoreWatchFilenameSuffix()
        if os.path.isfile(ignoreWatchFilePath):
            #this file was updated by supdate.py, do not process
            os.remove(ignoreWatchFilePath)
            return

        #Loic Horisberger - 17.10.2014:
        #This is a fix for MacVIM where current files are swap files.        
        #If the file ends with ".swp", remove the extention to get the
        #original filename. Remove the "." at the beginning that makes
        #the file hidden as well.
        if filePath.endswith(".swp"):
            filePath = filePath.replace(".swp","")
            filePath = filePath[::-1].replace("/."[::-1],"/"[::-1],1)[::-1]

        self.__dbWorker.getFileInfo(self, filePath)
        fileInfo = self.__inputQueue.get()

        if fileInfo:
            SLogger.debug("Local modification detected for: %s" % filePath)

            client = clientPool.getClient(fileInfo.getTableName(), fileInfo.getInstance())
            localUpdateDateString = fileInfo.getUpdatedOn()

            canUpdate = True
            if localUpdateDateString:
                SLogger.debug("Checking remote version...")
                remoteRecord = client.get(fileInfo.getSysId())
                remoteUpdatedOnString = remoteRecord.sys_updated_on
                canUpdate = checkCanUpdate(localUpdateDateString, remoteUpdatedOnString)
                if canUpdate:
                    SLogger.debug("Done")
                else:
                    SLogger.warning("Remote file changed by %s on %s. Previous local version is %s. Cannot update" %
                        (remoteRecord.sys_updated_by, snowServerToLocalDate(remoteUpdatedOnString), localUpdateDateString))
                    Commands.cannotUpload(remoteRecord.sys_updated_by, remoteUpdatedOnString, localUpdateDateString)


            if canUpdate:
                SLogger.debug("Updating in SNOW...")
                content = codecs.open(filePath, "r", "utf-8").read()

                success = client.updateContent(fileInfo.getSysId(), fileInfo.getContentFieldName(), content)
                if success:
                    SLogger.debug("Updating local update date...")
                    newRemoteRecord = client.get(fileInfo.getSysId())
                    newRemoteUpdatedOnString = newRemoteRecord.sys_updated_on
                    self.__dbWorker.setUpdatedOn(self, filePath, newRemoteUpdatedOnString)
                    confirmation = self.__inputQueue.get()
                    if confirmation:
                        SLogger.debug("Success updating %s" % fileInfo.getFileName())
                        Commands.success(fileInfo)
                    else:
                        SLogger.warning("Update of local date failed for file %s" % fileInfo)
                        Commands.warning("Upload failed", "Update of local date failed for file %s" % fileInfo)

                else:
                    SLogger.warning("Update of local date failed for file %s" % fileInfo)
                    Commands.warning("Upload failed", "Update of local date failed for file %s" % fileInfo)
Ejemplo n.º 6
0
    def run(cls, targetURL, *extraArgs):
        instance, tableName, sysId = cls.__parseURL(targetURL)
        nameField, contentField, fileExtension = cls._getTableInfo(tableName)

        localDir = os.getcwd()
        db = SnowFileDB(localDir)
        client = SnowClient(tableName, instance)
        record = client.get(sysId)

        if len(extraArgs) > 0:
            fileName = extraArgs[0]
        else:
            fileName = "%s.%s" % (
                getattr(record, nameField).replace(" ", "-").replace("/", "-").replace("(", "[").replace(")", "]"),
                fileExtension,
            )
        filePath = os.path.join(localDir, fileName)
        if os.path.exists(filePath):
            SLogger.warning("Local file %s already exists, will not overwrite" % filePath)
            return

        #         if os.path.isfile(os.path.join(localDir, Config.getLockFilename())):
        #             #swatch is watching, we do not want him to re-upload, we write a file for swatch.py to know
        #             ignoreWatchFilePath = filePath + Config.getIgnoreWatchFilenameSuffix()
        #             SLogger.debug("Creating file %s to avoid swatch to re-upload" % ignoreWatchFilePath)
        #             ignoreWatchFile = open(ignoreWatchFilePath, "w")
        #             ignoreWatchFile.close()

        f = codecs.open(filePath, "w", "utf-8")
        content = normalizeNewlines(getattr(record, contentField))
        f.write(content)
        f.close()
        db.addFileInfo(
            FileInfo(fileName, instance, tableName, record.sys_id, nameField, contentField, record.sys_updated_on)
        )
        db.commitAndClose()
        SLogger.debug("Done. Written to %s" % fileName)
Ejemplo n.º 7
0
 def __processAttributeError(self, e):
     SLogger.warning("Could not retrieve information from ServiceNow. Possibly your password is wrong.")
     Config.clearPassword(self.__instance)
     clientPool.removeClient(self.__table, self.__instance)
     sys.exit(1)
Ejemplo n.º 8
0
 def __processTransportError(cls, e):
     if hasattr(e, "httpcode") and e.httpcode == 401:
         SLogger.warning("Authentication error 401. Verify your password")
         sys.exit(1)
     else:
         raise e
Ejemplo n.º 9
0
 def __processURLError(cls, e):
     if hasattr(e, "reason") and hasattr(e.reason, "args") and e.reason.args[0] == "The read operation timed out":
         SLogger.warning("Connection timed out. Verify your username.")
         sys.exit(1)
     else:
         raise e
Ejemplo n.º 10
0
    def run(cls, targetFiles):

        for targetFile in targetFiles:
            if not os.path.isfile(targetFile):
                SLogger.warning("File %s does not exist, ignoring" % os.path.abspath(targetFile))
                continue

            SLogger.debug("Reading target file %s" % os.path.abspath(targetFile))

            loadedParams = {}
            execfile(targetFile, globals(), loadedParams)

            instance = loadedParams.get("instance", None)
            if not instance:
                SLogger.warning("No instance declared in target file %s, aborting" % targetFile)
                sys.exit(1)

            if not instance.endswith("/"):
                instance = instance + "/"

            for i, target in enumerate(loadedParams["targets"]):

                # we extract the table name
                table = target.get("table", None)
                if not table:
                    SLogger.warning("No table in target number %d of file %s, ignoring" % (i, targetFile))
                    continue

                # we extract the name field, content field, and how to rename files
                tableInfo = cls.__getTableInfo(table, target)
                if not tableInfo:
                    continue
                nameField, contentField, transformName = tableInfo

                # we extract the filtering functions
                query = target.get("query", "")
                recordFilter = target.get("recordFilter", lambda x: True)

                # we extract the local target directory
                localDir = target.get("localDir", None)
                if not localDir:
                    SLogger.debug(
                        "No localDir in target number %d of file %s, using current directory" % (i, targetFile)
                    )
                    localDir = os.getcwd()
                if not os.path.isdir(localDir):
                    SLogger.warning("Local directory %s does not exist, ignoring target %d" % (localDir, i))
                    continue

                # if everything is well, we start downloading and writing
                db = SnowFileDB(localDir)
                client = SnowClient(table, instance)

                records = client.getRecords(query)

                for record in records:
                    if recordFilter(record):
                        recordName = getattr(record, nameField)
                        SLogger.debug("Downloading %s ... " % recordName)
                        fileName = transformName(recordName)
                        filePath = os.path.abspath(os.path.join(localDir, fileName))
                        if os.path.exists(filePath):
                            good = False
                            suffix = 2
                            while not good:
                                filePathNew = filePath + "_" + str(suffix)
                                if not os.path.exists(filePathNew):
                                    good = True
                                    filePath = filePathNew
                                else:
                                    suffix = suffix + 1

                        try:
                            content = normalizeNewlines(getattr(record, contentField))
                            f = codecs.open(filePath, "w", "utf-8")
                            f.write(content)
                            f.close()
                            db.addFileInfo(
                                FileInfo(
                                    fileName,
                                    instance,
                                    table,
                                    record.sys_id,
                                    nameField,
                                    contentField,
                                    record.sys_updated_on,
                                )
                            )
                            SLogger.debug("Done. Written to %s" % fileName)
                        except Exception:
                            SLogger.debug("Problem with this record, will not write")

                db.commitAndClose()