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()
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()