Browse Source

Add prompting to export and import folder tree.

* Added prompting

* Update foldetree

* comments and error checking

* Error checking added

* add prompts
master
Gerry Nelson 7 years ago
committed by GitHub
parent
commit
c4965b13d7
No known key found for this signature in database GPG Key ID: 4AEE18F83AFDEB23
  1. 93
      exportfoldertree.py
  2. 64
      importfoldertree.py

93
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)
# prompt if directory exists because existing json files are deleted
if os.path.exists(basedir):
# retrieve root folders
reqtype='get'
reqval='/folders/rootFolders'
resultdata=callrestapi(reqval,reqtype)
# 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"
# loop root folders
if 'items' in resultdata:
else: areyousure="Y"
total_items=resultdata['count']
# prompt is Y if user selected Y, its a new directory, or user selected quiet mode
if areyousure.upper() =='Y':
returned_items=len(resultdata['items'])
path=basedir
if total_items == 0: print("Note: No items returned.")
else:
# 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)
# export each folder and download the package file to the directory
# retrieve root folders
reqtype='get'
reqval='/folders/rootFolders'
resultdata=callrestapi(reqval,reqtype)
for i in range(0,returned_items):
# loop root folders
if 'items' in resultdata:
id=resultdata['items'][i]["id"]
package_name=str(uuid.uuid1())
json_name=resultdata['items'][i]["name"].replace(" ","")+'_'+str(i)
total_items=resultdata['count']
command=clidir+'sas-admin transfer export -u /folders/folders/'+id+' --name "'+package_name+'"'
print(command)
subprocess.call(command, shell=True)
returned_items=len(resultdata['items'])
reqval='/transfer/packages?filter=eq(name,"'+package_name+'")'
package_info=callrestapi(reqval,reqtype)
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):
package_id=package_info['items'][0]['id']
id=resultdata['items'][i]["id"]
package_name=str(uuid.uuid1())
json_name=resultdata['items'][i]["name"].replace(" ","")+'_'+str(i)
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)
command=clidir+'sas-admin transfer export -u /folders/folders/'+id+' --name "'+package_name+'"'
print(command)
subprocess.call(command, shell=True)
print("NOTE: Viya root folders exported to json files in "+path)
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)
else:
print("NOTE: Operation cancelled")

64
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
# check that directory exists
if os.path.isdir(basedir):
# get python version
version=int(str(sys.version_info[0]))
# loop files in the directory
for filename in os.listdir( basedir ):
if filename.lower().endswith('.json'):
# if the quiet mode flag is not passed then prompt to continue
if not quietmode:
# 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)
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"
#print the json from the upload
with open('/tmp/packageid.json') as json_file:
package_data = json.load(json_file)
if areyousure.upper() =='Y':
print(json.dumps(package_data,indent=2))
# check that directory exists
if os.path.isdir(basedir):
# get the packageid and import the package
packageid=package_data["id"]
command=clidir+'sas-admin -q transfer import --id '+packageid
# loop files in the directory
for filename in os.listdir( basedir ):
print(command)
# 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")
subprocess.call(command, shell=True)
print("NOTE: Viya root folders imported from json files in "+basedir)
else: print("ERROR: Directory does not exist")

Loading…
Cancel
Save