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. 55
      exportfoldertree.py
  2. 34
      importfoldertree.py

55
exportfoldertree.py

@ -28,7 +28,6 @@
# #
# Import Python modules # Import Python modules
import argparse, sys, subprocess, uuid, time, os, glob import argparse, sys, subprocess, uuid, time, os, glob
from sharedfunctions import getfolderid, callrestapi from sharedfunctions import getfolderid, callrestapi
@ -36,38 +35,52 @@ from sharedfunctions import getfolderid, callrestapi
# get python version # get python version
version=int(str(sys.version_info[0])) version=int(str(sys.version_info[0]))
# CHANGE THIS VARIABLE IF YOUR CLI IS IN A DIFFERENT LOCATION # CHANGE THIS VARIABLE IF YOUR CLI IS IN A DIFFERENT LOCATION
clidir='/opt/sas/viya/home/bin/' clidir='/opt/sas/viya/home/bin/'
# get input parameters # get input parameters
parser = argparse.ArgumentParser(description="Export the complete Viya folder tree") parser = argparse.ArgumentParser(description="Export the complete Viya folder tree")
parser.add_argument("-d","--directory", help="Directory for Export",required='True') 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() args= parser.parse_args()
basedir=args.directory 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 # prompt if directory exists because existing json files are deleted
path=basedir if os.path.exists(basedir):
if not os.path.exists(path): os.makedirs(path)
else: # 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") filelist=glob.glob(path+"/*.json")
for file in filelist: for file in filelist:
os.remove(file) os.remove(file)
# retrieve root folders # retrieve root folders
reqtype='get' reqtype='get'
reqval='/folders/rootFolders' reqval='/folders/rootFolders'
resultdata=callrestapi(reqval,reqtype) resultdata=callrestapi(reqval,reqtype)
# loop root folders
# loop root folders if 'items' in resultdata:
if 'items' in resultdata:
total_items=resultdata['count'] total_items=resultdata['count']
@ -75,9 +88,7 @@ if 'items' in resultdata:
if total_items == 0: print("Note: No items returned.") if total_items == 0: print("Note: No items returned.")
else: else:
# export each folder and download the package file to the directory # export each folder and download the package file to the directory
for i in range(0,returned_items): for i in range(0,returned_items):
id=resultdata['items'][i]["id"] id=resultdata['items'][i]["id"]
@ -98,6 +109,8 @@ if 'items' in resultdata:
print(command) print(command)
subprocess.call(command, shell=True) 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")

34
importfoldertree.py

@ -24,20 +24,39 @@ from sharedfunctions import callrestapi
clidir='/opt/sas/viya/home/bin/' clidir='/opt/sas/viya/home/bin/'
# get input parameters # 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("-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() args= parser.parse_args()
basedir=args.directory basedir=args.directory
quietmode=args.quiet
# check that directory exists # get python version
if os.path.isdir(basedir): 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 # loop files in the directory
for filename in os.listdir( basedir ): for filename in os.listdir( basedir ):
# only process json files
if filename.lower().endswith('.json'): if filename.lower().endswith('.json'):
# upload the json package #upload the json package
command=clidir+'sas-admin transfer upload --file '+os.path.join(basedir,filename)+'> /tmp/packageid.json' command=clidir+'sas-admin transfer upload --file '+os.path.join(basedir,filename)+'> /tmp/packageid.json'
print(command) print(command)
subprocess.call(command, shell=True) subprocess.call(command, shell=True)
@ -51,10 +70,13 @@ if os.path.isdir(basedir):
# get the packageid and import the package # get the packageid and import the package
packageid=package_data["id"] packageid=package_data["id"]
command=clidir+'sas-admin -q transfer import --id '+packageid command=clidir+'sas-admin -q transfer import --id '+packageid
print(command) print(command)
subprocess.call(command, shell=True) subprocess.call(command, shell=True)
print("NOTE: Viya root folders imported from json files in "+basedir) print("NOTE: Viya root folders imported from json files in "+basedir)
else: print("ERROR: Directory does not exist")
else: print("ERROR: Directory does not exist")
else:
print("NOTE: Operation cancelled")

Loading…
Cancel
Save