Browse Source

add error checking

master
root 7 years ago
parent
commit
c98c0ee47b
  1. 47
      exportfoldertree.py
  2. 90
      importfoldertree.py

47
exportfoldertree.py

@ -2,7 +2,7 @@
# -*- coding: utf-8 -*-
#
# exportfoldertree.py
# March 20187
# March 2018
#
# Pass in a directory and this tool will export the complete viya folder
# structure to a sub-directory named for the current date and time
@ -12,7 +12,7 @@
# Change History
#
#
# Copyright © 2019, SAS Institute Inc., Cary, NC, USA. All Rights Reserved.
# Copyright © 2019, SAS Institute Inc., Cary, NC, USA. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the License);
# you may not use this file except in compliance with the License.
@ -29,7 +29,7 @@
# Import Python modules
import argparse, sys, subprocess, uuid, string, time, os
import argparse, sys, subprocess, uuid, time, os
from sharedfunctions import getfolderid, callrestapi
@ -67,25 +67,30 @@ if 'items' in resultdata:
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)
# 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())
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,package_name+'.json')
command=clidir+'sas-admin transfer download --file '+completefile+' --id '+package_id
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)

90
importfoldertree.py

@ -1,76 +1,60 @@
#!/usr/bin/python
# -*- coding: utf-8 -*-
#
# importfoldertree.py
# March 20187
# importfoldertree.py March 2018
#
# Pass in a directory and this tool will import the complete viya folder
# Pass in a directory and this tool will import all the json files in the directory. It depends on the admin CLI
#
# Change History
#
#
# Copyright © 2019, SAS Institute Inc., Cary, NC, USA. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the License);
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
# Licensed under the Apache License, Version 2.0 (the License); you may not use this file except in compliance with the License.
# You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
#
# http://www.apache.org/licenses/LICENSE-2.0
# Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS
# OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
# Import Python modules
import argparse, sys, subprocess, uuid, string, time, os, json
import argparse, sys, subprocess, os, json
from sharedfunctions import 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
# get input parameters
parser = argparse.ArgumentParser(description="Delete a folder and its sub-folders")
parser.add_argument("-d","--directory", help="Directory for Export",required='True')
parser.add_argument("-d","--directory", help="Directory that contains JSON files to import",required='True')
args= parser.parse_args()
basedir=args.directory
# 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")
# retrieve root folders
#reqtype='get'
#reqval='/folders/rootFolders'
#resultdata=callrestapi(reqval,reqtype)
# loop files in the directory
for filename in os.listdir( basedir ):
# 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)

Loading…
Cancel
Save