From feec06b0981202b63b6906743327912b010cd93b Mon Sep 17 00:00:00 2001 From: ibodrin-rogers <70901593+ibodrin-rogers@users.noreply.github.com> Date: Wed, 16 Dec 2020 20:45:28 -0500 Subject: [PATCH] Generate a valid .json filename for Linux filesystem (#63) * Generate valid .json filename for Linux filesystem Adding get_valid_filename function to address slashes and other invalid characters in the report name for Linux filesystem. * Moved get_valid_filename to sharedfunctions.py. * Update snapshotreports.py Revision date added in the comment. * Update sharedfunctions.py Revision date is added in the comment. --- sharedfunctions.py | 17 +++++++++++++++++ snapshotreports.py | 14 +++++++------- 2 files changed, 24 insertions(+), 7 deletions(-) diff --git a/sharedfunctions.py b/sharedfunctions.py index 4d8b050..43c57aa 100755 --- a/sharedfunctions.py +++ b/sharedfunctions.py @@ -33,6 +33,7 @@ # 20dec2018 Fixed standard csv output # 14JAN2019 Added getpath # 20SEP2019 Added getidsanduris +# 09dec2020 Added get_valid_filename function to deal with invalid characters for Linux filesystem # # Copyright © 2018, SAS Institute Inc., Cary, NC, USA. All Rights Reserved. # @@ -57,6 +58,7 @@ import json import pprint import os import collections +import re pp = pprint.PrettyPrinter(indent=4) @@ -667,3 +669,18 @@ def simplejsonresults(resultdata): del resultdata['links'] print(json.dumps(resultdata,indent=2)) + + +#The get_valid_filename function is taken from https://github.com/django/django/blob/master/django/utils/text.py. +#The function replaces the characters that are not valid for Linux filsystem in the input string. +#The comment in the source says: +# +# Return the given string converted to a string that can be used for a clean +# filename. Remove leading and trailing spaces; convert other spaces to +# underscores; and remove anything that is not an alphanumeric, dash, +# underscore, or dot. + + +def get_valid_filename(s): + s = str(s).strip().replace(' ', '_') + return re.sub(r'(?u)[^-\w.]', '', s) diff --git a/snapshotreports.py b/snapshotreports.py index 8f2e6b1..e1f4503 100755 --- a/snapshotreports.py +++ b/snapshotreports.py @@ -21,6 +21,7 @@ # 16may2020 add folder path to report name # 16may2020 allow to subset reports exported by the path of the report folder # 10aug2020 add option to auto delete transport file after download completes +# 09dec2020 add get_valid_filename function to deal with invalid characters for Linux filesystem # # Copyright © 2019, SAS Institute Inc., Cary, NC, USA. All Rights Reserved. # @@ -38,9 +39,10 @@ # # Import Python modules +import re import argparse, sys, subprocess, uuid, time, os, glob from datetime import datetime as dt, timedelta as td -from sharedfunctions import getfolderid, callrestapi, getpath +from sharedfunctions import getfolderid, callrestapi, getpath, get_valid_filename # get python version version=int(str(sys.version_info[0])) @@ -145,11 +147,8 @@ if areyousure.upper() =='Y': path_to_report=path_to_report.replace("/","_") package_name=str(uuid.uuid1()) - json_name=path_to_report+resultdata['items'][i]["name"].replace(" ","")+'_'+str(i) - json_name=json_name.replace("(","_") - json_name=json_name.replace(")","_") - json_name=json_name.replace(" ","-") + json_name=get_valid_filename(path_to_report+resultdata['items'][i]["name"].replace(" ","")+'_'+str(i)) command=clidir+'sas-admin transfer export -u /reports/reports/'+id+' --name "'+package_name+'"' print(command) @@ -172,8 +171,9 @@ if areyousure.upper() =='Y': remTransferObject.wait() - print("NOTE: "+str(reports_exported)+" Viya report(s) exported to json files in "+path) + print("NOTE: "+str(reports_exported)+" report(s) exported to json files in "+path) + print("NOTE: "+str(total_items)+" total reports found, "+str(reports_exported)+" reports exported to json files in "+path) else: - print("NOTE: Operation cancelled") \ No newline at end of file + print("NOTE: Operation cancelled")