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. 35
      exportfoldertree.py
  2. 26
      importfoldertree.py

35
exportfoldertree.py

@ -28,7 +28,6 @@
#
# Import Python modules
import argparse, sys, subprocess, uuid, time, os, glob
from sharedfunctions import getfolderid, callrestapi
@ -36,24 +35,39 @@ 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)
# 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':
# I now just use the directory the user specifies
path=basedir
# create directory if it doesn't exist
if not os.path.exists(path): os.makedirs(path)
else:
filelist=glob.glob(path+"/*.json")
@ -65,7 +79,6 @@ reqtype='get'
reqval='/folders/rootFolders'
resultdata=callrestapi(reqval,reqtype)
# loop root folders
if 'items' in resultdata:
@ -75,9 +88,7 @@ if 'items' in resultdata:
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"]
@ -100,4 +111,6 @@ if 'items' in resultdata:
print("NOTE: Viya root folders exported to json files in "+path)
else:
print("NOTE: Operation cancelled")

26
importfoldertree.py

@ -24,10 +24,28 @@ 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):
@ -35,6 +53,7 @@ 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
@ -51,10 +70,13 @@ 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
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")

Loading…
Cancel
Save