diff --git a/exportfoldertree.py b/exportfoldertree.py index c443565..80ffb92 100755 --- a/exportfoldertree.py +++ b/exportfoldertree.py @@ -28,7 +28,6 @@ # # Import Python modules - import argparse, sys, subprocess, uuid, time, os, glob from sharedfunctions import getfolderid, callrestapi @@ -36,68 +35,82 @@ from sharedfunctions import getfolderid, callrestapi # get python version version=int(str(sys.version_info[0])) - # CHANGE THIS VARIABLE IF YOUR CLI IS IN A DIFFERENT LOCATION clidir='/opt/sas/viya/home/bin/' - # 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.add_argument("-q","--quiet", help="Suppress the are you sure prompt.", action='store_true') args= parser.parse_args() basedir=args.directory +quietmode=args.quiet -# create output directory: was giving the directory a name -#now=time.localtime() -#dirname="D_"+time.strftime('%Y-%m-%dT%H:%M:%S',now) -#path=os.path.join(basedir,dirname) - -# I now just use the directory the user specifies -path=basedir -if not os.path.exists(path): os.makedirs(path) -else: - filelist=glob.glob(path+"/*.json") - for file in filelist: - os.remove(file) - -# retrieve root folders -reqtype='get' -reqval='/folders/rootFolders' -resultdata=callrestapi(reqval,reqtype) - - -# loop root folders -if 'items' in resultdata: - - total_items=resultdata['count'] - - returned_items=len(resultdata['items']) - - if total_items == 0: print("Note: No items returned.") - else: - - # export each folder and download the package file to the directory - - for i in range(0,returned_items): - - id=resultdata['items'][i]["id"] - package_name=str(uuid.uuid1()) - json_name=resultdata['items'][i]["name"].replace(" ","")+'_'+str(i) - - command=clidir+'sas-admin transfer export -u /folders/folders/'+id+' --name "'+package_name+'"' - print(command) - subprocess.call(command, shell=True) - reqval='/transfer/packages?filter=eq(name,"'+package_name+'")' - package_info=callrestapi(reqval,reqtype) +# prompt if directory exists because existing json files are deleted +if os.path.exists(basedir): + + # if the quiet mode flag is not passed then prompt to continue + if not quietmode: + + if version > 2: + areyousure=input("The folder exists any existing json files in it will be deleted. Continue? (Y)") + else: + areyousure=raw_input("The folder already exists any existing json files in it will be deleted. Continue? (Y)") + else: + areyousure="Y" + +else: areyousure="Y" + +# prompt is Y if user selected Y, its a new directory, or user selected quiet mode +if areyousure.upper() =='Y': + + path=basedir + + # create directory if it doesn't exist + if not os.path.exists(path): os.makedirs(path) + else: + filelist=glob.glob(path+"/*.json") + for file in filelist: + os.remove(file) + + # retrieve root folders + reqtype='get' + reqval='/folders/rootFolders' + resultdata=callrestapi(reqval,reqtype) + + # loop root folders + if 'items' in resultdata: + + total_items=resultdata['count'] - package_id=package_info['items'][0]['id'] + returned_items=len(resultdata['items']) - completefile=os.path.join(path,json_name+'.json') - command=clidir+'sas-admin transfer download --file '+completefile+' --id '+package_id - print(command) - subprocess.call(command, shell=True) + if total_items == 0: print("Note: No items returned.") + else: + # export each folder and download the package file to the directory + for i in range(0,returned_items): + + id=resultdata['items'][i]["id"] + package_name=str(uuid.uuid1()) + json_name=resultdata['items'][i]["name"].replace(" ","")+'_'+str(i) + + command=clidir+'sas-admin transfer export -u /folders/folders/'+id+' --name "'+package_name+'"' + print(command) + subprocess.call(command, shell=True) + + reqval='/transfer/packages?filter=eq(name,"'+package_name+'")' + package_info=callrestapi(reqval,reqtype) + + package_id=package_info['items'][0]['id'] + + completefile=os.path.join(path,json_name+'.json') + command=clidir+'sas-admin transfer download --file '+completefile+' --id '+package_id + print(command) + subprocess.call(command, shell=True) + + print("NOTE: Viya root folders exported to json files in "+path) -print("NOTE: Viya root folders exported to json files in "+path) - +else: + print("NOTE: Operation cancelled") diff --git a/importfoldertree.py b/importfoldertree.py index deabdd1..6e68fdb 100755 --- a/importfoldertree.py +++ b/importfoldertree.py @@ -24,37 +24,59 @@ from sharedfunctions import callrestapi clidir='/opt/sas/viya/home/bin/' # get input parameters -parser = argparse.ArgumentParser(description="Delete a folder and its sub-folders") +parser = argparse.ArgumentParser(description="Import JSON files from directory. All json files in directory will be imported.") parser.add_argument("-d","--directory", help="Directory that contains JSON files to import",required='True') +parser.add_argument("-q","--quiet", help="Suppress the are you sure prompt.", action='store_true') args= parser.parse_args() basedir=args.directory +quietmode=args.quiet + +# get python version +version=int(str(sys.version_info[0])) + + +# if the quiet mode flag is not passed then prompt to continue +if not quietmode: + + if version > 2: + areyousure=input("WARNING: If content from the packages already exists in folders it will be replaced. Continue? (Y)") + else: + areyousure=raw_input("WARNING:If content from the packages already exists in folders it will be replaced. Continue? (Y)") +else: + areyousure="Y" + +if areyousure.upper() =='Y': + + # check that directory exists + if os.path.isdir(basedir): + + # loop files in the directory + for filename in os.listdir( basedir ): + + # only process json files + if filename.lower().endswith('.json'): + + #upload the json package + command=clidir+'sas-admin transfer upload --file '+os.path.join(basedir,filename)+'> /tmp/packageid.json' + print(command) + subprocess.call(command, shell=True) + + #print the json from the upload + with open('/tmp/packageid.json') as json_file: + package_data = json.load(json_file) + + print(json.dumps(package_data,indent=2)) + + # get the packageid and import the package + packageid=package_data["id"] + command=clidir+'sas-admin -q transfer import --id '+packageid + print(command) + + subprocess.call(command, shell=True) + print("NOTE: Viya root folders imported from json files in "+basedir) + + else: print("ERROR: Directory does not exist") +else: + print("NOTE: Operation cancelled") -# check that directory exists -if os.path.isdir(basedir): - - # loop files in the directory - for filename in os.listdir( basedir ): - - if filename.lower().endswith('.json'): - - # upload the json package - command=clidir+'sas-admin transfer upload --file '+os.path.join(basedir,filename)+'> /tmp/packageid.json' - print(command) - subprocess.call(command, shell=True) - - #print the json from the upload - with open('/tmp/packageid.json') as json_file: - package_data = json.load(json_file) - - print(json.dumps(package_data,indent=2)) - - # get the packageid and import the package - packageid=package_data["id"] - command=clidir+'sas-admin -q transfer import --id '+packageid - - print(command) - - subprocess.call(command, shell=True) - print("NOTE: Viya root folders imported from json files in "+basedir) -else: print("ERROR: Directory does not exist")