Browse Source

Bug/unicode (#113)

* teext encode ascii

* bracket remove

* unicodeliterals

* unicode fixes

* unicode fixes

* update exportfoldertree.py

Co-authored-by: cloud-user <cloud-user@race.sas.com>
master
Gerry Nelson 3 years ago
committed by GitHub
parent
commit
024df0138b
No known key found for this signature in database GPG Key ID: 4AEE18F83AFDEB23
  1. 38
      exportfoldertree.py
  2. 1
      listgroupsandmembers.py
  3. 6
      listmemberswithpath.py
  4. 22
      sharedfunctions.py

38
exportfoldertree.py

@ -11,6 +11,7 @@
# #
# Change History # Change History
# #
# SEP2022 Added option to specify the root folder to start at
# #
# Copyright © 2019, SAS Institute Inc., Cary, NC, USA. All Rights Reserved. # Copyright © 2019, SAS Institute Inc., Cary, NC, USA. All Rights Reserved.
# #
@ -28,7 +29,7 @@
# #
# Import Python modules # Import Python modules
import argparse, sys, subprocess, uuid, time, os, glob import argparse, sys, subprocess, uuid, time, os, glob, json
from sharedfunctions import getfolderid, callrestapi,getapplicationproperties from sharedfunctions import getfolderid, callrestapi,getapplicationproperties
@ -44,11 +45,14 @@ cliexe=propertylist["sascli.executable"]
clicommand=os.path.join(clidir,cliexe) clicommand=os.path.join(clidir,cliexe)
# 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 or the members of a folder to a set of Viya Packages.")
parser.add_argument("-d","--directory", help="Directory for Export",required='True') parser.add_argument("-d","--directory", help="Directory to store Export Packages",required='True')
parser.add_argument("-f","--folder", help="Folder to start export at (root is default)",default="NONE")
parser.add_argument("-q","--quiet", help="Suppress the are you sure prompt.", action='store_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
folder=args.folder
quietmode=args.quiet quietmode=args.quiet
@ -78,12 +82,23 @@ if areyousure.upper() =='Y':
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
reqtype='get' reqtype='get'
reqval='/folders/rootFolders'
if folder !='NONE':
folderinfo=getfolderid(folder)
results=(folderinfo[3])
folderid=results["id"]
reqval='/folders/folders/'+folderid+'/members'
else:
# retrieve root folders
reqval='/folders/rootFolders'
resultdata=callrestapi(reqval,reqtype) resultdata=callrestapi(reqval,reqtype)
print(json.dumps(resultdata,indent=2))
# loop root folders # loop root folders
if 'items' in resultdata: if 'items' in resultdata:
@ -96,11 +111,16 @@ if areyousure.upper() =='Y':
# 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"] if folder=='NONE':
id=resultdata['items'][i]["id"]
folderuri='/folders/folders/'+id
else:
folderuri=resultdata['items'][i]["uri"]
package_name=str(uuid.uuid1()) package_name=str(uuid.uuid1())
json_name=resultdata['items'][i]["name"].replace(" ","")+'_'+str(i) json_name=resultdata['items'][i]["name"].replace(" ","")+'_'+str(i)
command=clicommand+' transfer export -u /folders/folders/'+id+' --name "'+package_name+'"' command=clicommand+' transfer export -u '+folderuri+' --name "'+package_name+'"'
print(command) print(command)
subprocess.call(command, shell=True) subprocess.call(command, shell=True)
@ -114,7 +134,7 @@ if areyousure.upper() =='Y':
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 folders exported to json files in "+path)
else: else:
print("NOTE: Operation cancelled") print("NOTE: Operation cancelled")

1
listgroupsandmembers.py

@ -34,6 +34,7 @@
debug=False debug=False
# Import Python modules # Import Python modules
from __future__ import unicode_literals
import argparse import argparse
import sys import sys
from sharedfunctions import callrestapi from sharedfunctions import callrestapi

6
listmemberswithpath.py

@ -38,6 +38,7 @@
debug=False debug=False
# Import Python modules # Import Python modules
from __future__ import unicode_literals
import argparse import argparse
import sys import sys
from sharedfunctions import callrestapi,getpath from sharedfunctions import callrestapi,getpath
@ -95,4 +96,7 @@ for member in members:
else: else:
outstr=outstr+',' outstr=outstr+','
outstr=outstr+','+member['uri'] outstr=outstr+','+member['uri']
print(outstr) try:
print(outstr)
except UnicodeEncodeError:
print(outstr.encode('ascii','replace'))

22
sharedfunctions.py

@ -56,6 +56,7 @@
# Import Python modules # Import Python modules
from __future__ import print_function from __future__ import print_function
from __future__ import unicode_literals
import requests import requests
import sys import sys
import json import json
@ -65,7 +66,6 @@ import collections
import inspect import inspect
import re import re
pp = pprint.PrettyPrinter(indent=4) pp = pprint.PrettyPrinter(indent=4)
# validate rest api is not used at this time # validate rest api is not used at this time
@ -444,9 +444,15 @@ def csvresults(resultdata,columns=[]):
if z==numvals: sep='' if z==numvals: sep=''
else: sep=',' else: sep=','
if key != 'links' and key in columns: print('"'+str(val)+'"'+sep, end="") if key != 'links' and key in columns:
try:
print('"'+str(val)+'"'+sep, end="")
except UnicodeEncodeError:
newval=val.encode('ascii','replace')
print('"'+str(newval)+'"'+sep, end="")
print("\n",end="") print("\n",end="")
@ -476,7 +482,13 @@ def csvresults(resultdata,columns=[]):
if z==numvals: sep='' if z==numvals: sep=''
else: sep=',' else: sep=','
if key != 'links': print('"'+str(val)+'"'+sep,end="") if key != 'links':
try:
print('"'+str(val)+'"'+sep, end="")
except UnicodeEncodeError:
newval=val.encode('ascii','replace')
print('"'+str(newval)+'"'+sep, end="")
print("\n",end="") print("\n",end="")

Loading…
Cancel
Save