Browse Source

Updatesnapshot close #50 (#51)

* add folder path functionality

* update help

* clarify default and  add example
master
Gerry Nelson 6 years ago
committed by GitHub
parent
commit
826fd19699
No known key found for this signature in database GPG Key ID: 4AEE18F83AFDEB23
  1. 4
      EXAMPLES.md
  2. BIN
      __pycache__/sharedfunctions.cpython-36.pyc
  3. 62
      snapshotreports.py

4
EXAMPLES.md

@ -269,8 +269,12 @@ importpackages.py -d /tmp/mypackage -q
```bash ```bash
#his tool will export all the reports in your viya system to there own #his tool will export all the reports in your viya system to there own
# individual json file in a directory # individual json file in a directory
# optionally you can pass in folder path and only export reports that
# are located under that folder
./snapshotreports.py -c 10 -d ~/snapshot ./snapshotreports.py -c 10 -d ~/snapshot
./snapshotreports.py -c 10 -d ~/snapshot/salesreports -f /gelcontent/sales
``` ```
**creategroups.py** **creategroups.py**

BIN
__pycache__/sharedfunctions.cpython-36.pyc

Binary file not shown.

62
snapshotreports.py

@ -18,6 +18,8 @@
# #
# Change History # Change History
# #
# 16may2020 add folder path to report name
# 16may2020 allow to subset reports exported by the path of the report folder
# #
# Copyright © 2019, SAS Institute Inc., Cary, NC, USA. All Rights Reserved. # Copyright © 2019, SAS Institute Inc., Cary, NC, USA. All Rights Reserved.
# #
@ -37,7 +39,7 @@
# Import Python modules # Import Python modules
import argparse, sys, subprocess, uuid, time, os, glob import argparse, sys, subprocess, uuid, time, os, glob
from datetime import datetime as dt, timedelta as td from datetime import datetime as dt, timedelta as td
from sharedfunctions import getfolderid, callrestapi from sharedfunctions import getfolderid, callrestapi, getpath
# get python version # get python version
version=int(str(sys.version_info[0])) version=int(str(sys.version_info[0]))
@ -46,12 +48,13 @@ version=int(str(sys.version_info[0]))
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 Viya Reports each to its own unique transfer package")
parser.add_argument("-d","--directory", help="Directory for Export",required='True') parser.add_argument("-d","--directory", help="Directory to store report packages",required='True')
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')
parser.add_argument("-c","--changeddays", help="Reports changed in the how many days",default='1') parser.add_argument("-c","--changeddays", help="Reports changed in the how many days (defaults to 1 day)?",default='1')
parser.add_argument("-m","--modifiedby", help="Last modified id equals",default=None) parser.add_argument("-m","--modifiedby", help="Last modified id equals?",default=None)
parser.add_argument("-n","--name", help="Name contains",default=None) parser.add_argument("-n","--name", help="Name contains?",default=None)
parser.add_argument("-f","--folderpath", help="Folder Path starts with?",default="/")
args= parser.parse_args() args= parser.parse_args()
basedir=args.directory basedir=args.directory
@ -60,7 +63,7 @@ quietmode=args.quiet
changeddays=args.changeddays changeddays=args.changeddays
modby=args.modifiedby modby=args.modifiedby
nameval=args.name nameval=args.name
folderpath=args.folderpath
# calculate time period for files # calculate time period for files
now=dt.today()-td(days=int(changeddays)) now=dt.today()-td(days=int(changeddays))
@ -79,7 +82,6 @@ if modby!=None: filtercond.append("eq(modifiedBy,"+modby+")")
# add the start and end and comma delimit the filter # add the start and end and comma delimit the filter
delimiter = ',' delimiter = ','
completefilter = 'and('+delimiter.join(filtercond)+')' completefilter = 'and('+delimiter.join(filtercond)+')'
print(completefilter)
# prompt if directory exists because existing json files are deleted # prompt if directory exists because existing json files are deleted
if os.path.exists(basedir): if os.path.exists(basedir):
@ -124,30 +126,44 @@ if areyousure.upper() =='Y':
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
reports_exported=0
for i in range(0,returned_items): for i in range(0,returned_items):
id=resultdata['items'][i]["id"] id=resultdata['items'][i]["id"]
package_name=str(uuid.uuid1())
json_name=resultdata['items'][i]["name"].replace(" ","")+'_'+str(i)
json_name=json_name.replace("(","_") path_to_report=getpath("/reports/reports/"+id)
json_name=json_name.replace(")","_")
if path_to_report.startswith(folderpath):
reports_exported=reports_exported+1
path_to_report=path_to_report.replace("/","_")
package_name=str(uuid.uuid1())
json_name=path_to_report+resultdata['items'][i]["name"].replace(" ","")+'_'+str(i)
json_name=json_name.replace("(","_")
json_name=json_name.replace(")","_")
json_name=json_name.replace(" ","-")
command=clidir+'sas-admin transfer export -u /reports/reports/'+id+' --name "'+package_name+'"'
print(command)
subprocess.call(command, shell=True)
command=clidir+'sas-admin transfer export -u /reports/reports/'+id+' --name "'+package_name+'"' reqval='/transfer/packages?filter=eq(name,"'+package_name+'")'
print(command) package_info=callrestapi(reqval,reqtype)
subprocess.call(command, shell=True)
reqval='/transfer/packages?filter=eq(name,"'+package_name+'")' package_id=package_info['items'][0]['id']
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)
completefile=os.path.join(path,json_name+'.json') print("NOTE: "+str(reports_exported)+" Viya report(s) exported to json files in "+path)
command=clidir+'sas-admin transfer download --file '+completefile+' --id '+package_id
print(command)
subprocess.call(command, shell=True)
print("NOTE: Viya reports exported to json files in "+path)
else: else:
print("NOTE: Operation cancelled") print("NOTE: Operation cancelled")

Loading…
Cancel
Save