diff --git a/exportfoldertree.py b/exportfoldertree.py index 65213ce..102b05a 100755 --- a/exportfoldertree.py +++ b/exportfoldertree.py @@ -11,6 +11,7 @@ # # Change History # +# SEP2022 Added option to specify the root folder to start at # # Copyright © 2019, SAS Institute Inc., Cary, NC, USA. All Rights Reserved. # @@ -28,7 +29,7 @@ # # Import Python modules -import argparse, sys, subprocess, uuid, time, os, glob +import argparse, sys, subprocess, uuid, time, os, glob, json from sharedfunctions import getfolderid, callrestapi,getapplicationproperties @@ -44,11 +45,14 @@ cliexe=propertylist["sascli.executable"] clicommand=os.path.join(clidir,cliexe) # get input parameters -parser = argparse.ArgumentParser(description="Export the complete Viya folder tree") -parser.add_argument("-d","--directory", help="Directory for Export",required='True') +parser = argparse.ArgumentParser(description="Export the complete Viya folder tree or the members of a folder to a set of Viya Packages.") +parser.add_argument("-d","--directory", help="Directory to store Export Packages",required='True') +parser.add_argument("-f","--folder", help="Folder to start export at (root is default)",default="NONE") parser.add_argument("-q","--quiet", help="Suppress the are you sure prompt.", action='store_true') args= parser.parse_args() + basedir=args.directory +folder=args.folder quietmode=args.quiet @@ -78,12 +82,23 @@ if areyousure.upper() =='Y': filelist=glob.glob(path+"/*.json") for file in filelist: os.remove(file) - - # retrieve root folders + reqtype='get' - reqval='/folders/rootFolders' + + if folder !='NONE': + + folderinfo=getfolderid(folder) + results=(folderinfo[3]) + folderid=results["id"] + reqval='/folders/folders/'+folderid+'/members' + else: + # retrieve root folders + reqval='/folders/rootFolders' + resultdata=callrestapi(reqval,reqtype) + print(json.dumps(resultdata,indent=2)) + # loop root folders if 'items' in resultdata: @@ -96,11 +111,16 @@ if areyousure.upper() =='Y': # export each folder and download the package file to the directory for i in range(0,returned_items): - id=resultdata['items'][i]["id"] + if folder=='NONE': + id=resultdata['items'][i]["id"] + folderuri='/folders/folders/'+id + else: + folderuri=resultdata['items'][i]["uri"] + package_name=str(uuid.uuid1()) json_name=resultdata['items'][i]["name"].replace(" ","")+'_'+str(i) - command=clicommand+' transfer export -u /folders/folders/'+id+' --name "'+package_name+'"' + command=clicommand+' transfer export -u '+folderuri+' --name "'+package_name+'"' print(command) subprocess.call(command, shell=True) @@ -114,7 +134,7 @@ if areyousure.upper() =='Y': print(command) subprocess.call(command, shell=True) - print("NOTE: Viya root folders exported to json files in "+path) + print("NOTE: Viya folders exported to json files in "+path) else: print("NOTE: Operation cancelled") diff --git a/listgroupsandmembers.py b/listgroupsandmembers.py index 4929576..96f4736 100755 --- a/listgroupsandmembers.py +++ b/listgroupsandmembers.py @@ -34,6 +34,7 @@ debug=False # Import Python modules +from __future__ import unicode_literals import argparse import sys from sharedfunctions import callrestapi diff --git a/listmemberswithpath.py b/listmemberswithpath.py index a080137..c708794 100755 --- a/listmemberswithpath.py +++ b/listmemberswithpath.py @@ -38,6 +38,7 @@ debug=False # Import Python modules +from __future__ import unicode_literals import argparse import sys from sharedfunctions import callrestapi,getpath @@ -95,4 +96,7 @@ for member in members: else: outstr=outstr+',' outstr=outstr+','+member['uri'] - print(outstr) + try: + print(outstr) + except UnicodeEncodeError: + print(outstr.encode('ascii','replace')) diff --git a/sharedfunctions.py b/sharedfunctions.py index 930173f..599e34b 100755 --- a/sharedfunctions.py +++ b/sharedfunctions.py @@ -56,6 +56,7 @@ # Import Python modules from __future__ import print_function +from __future__ import unicode_literals import requests import sys import json @@ -65,7 +66,6 @@ import collections import inspect import re - pp = pprint.PrettyPrinter(indent=4) # validate rest api is not used at this time @@ -444,9 +444,15 @@ def csvresults(resultdata,columns=[]): if z==numvals: sep='' else: sep=',' - if key != 'links' and key in columns: print('"'+str(val)+'"'+sep, end="") - - + if key != 'links' and key in columns: + + try: + print('"'+str(val)+'"'+sep, end="") + except UnicodeEncodeError: + newval=val.encode('ascii','replace') + print('"'+str(newval)+'"'+sep, end="") + + print("\n",end="") @@ -476,7 +482,13 @@ def csvresults(resultdata,columns=[]): if z==numvals: sep='' else: sep=',' - if key != 'links': print('"'+str(val)+'"'+sep,end="") + if key != 'links': + + try: + print('"'+str(val)+'"'+sep, end="") + except UnicodeEncodeError: + newval=val.encode('ascii','replace') + print('"'+str(newval)+'"'+sep, end="") print("\n",end="")