So I tried your setup I assume the “Windows Stuff” and “Linux Stuff” pieces were where my script would go. It ran and completed but it didn’t do anything. Here’s my full script any sensitive bits are in the relevance so this should be okay to show.
if {windows of operating system} do
//
//Deleting the append file
//
delete __appendfile
//
//Moving to the Monitoring_Tool table, removing any remnants of Monitor.py, json, and zip and then running the jar file
//
appendfile cd C:\beanstore-client\dg\util\Monitoring_Tool
appendfile delete Monitor.py
appendfile delete *.json
appendfile delete *.zip
appendfile java -cp .:mariadb-java-client-1.7.3.jar ProductSkuTest
//
// Python program starts after the following line
//
createfile until _EOF
#!/usr/bin/env python
##Import Statements
import glob
import csv
import re
import zipfile
import os
from datetime import date
##This is the engine that calls every function in the program
def engine():
fileread()
products = fileread()
datavalidation(products)
productfields = datavalidation(products)
removal(products)
csvwriting(products, productfields)
zippy(products, productfields)
def fileread():
##Using glob glob to grab the correct json file
name = glob.glob('s*-cr*.json')
##Reading the file into a dictionary
for fname_in in name:
f = open(fname_in)
s = f.read()
f.close()
filedict = eval(s)
##Pulling out the list of products as that's all we want
products = filedict["Products"]
return products
##Products now = all of the data being read in
def datavalidation(products):
## The "Tax Codes" field is not a single value, but rather a list of dicts
## Need to consolidate it to a single value. For now just make a string
## from the whole thing.
## Also using replaces while its a string to remove unwanted characters from the output
## Also setting up additional fields as requested by pricing team with store number and till + store number
with open('/beanstore-client/pos/global.properties', 'r') as f:
count = 0
for line in f:
if not line.startswith("beanstore.till.tillnumber="):
continue
count = count + 1
if count == 2:
break
global ATILL
ATILL = re.findall('s[0-9]+-cr[0-9]+', str(line))
ATILL = str(ATILL)
ATILL = ATILL.replace("']", "")
ATILL = ATILL.replace("['", "")
STORE = ATILL
STORE = re.findall('([0-9]+)-', str(line))
STORE = str(STORE)
STORE = STORE.replace("']", "")
STORE = STORE.replace("['", "")
for product in products:
product["2TILL"] = ATILL
product["1STORE"] = STORE
try:
product["Tax Codes"] = str(product["Tax Codes"])
product["Tax Codes"] = product["Tax Codes"].replace("[{{", "")
product["Tax Codes"] = product["Tax Codes"].replace("}]", "")
product["Tax Codes"] = product["Tax Codes"].replace("'", "")
product["Tax Codes"] = product["Tax Codes"].replace("Tax ", "")
product["Tax Codes"] = product["Tax Codes"].replace("Code: ", "")
product["Tax Codes"] = product["Tax Codes"].replace("To Date: ", "")
product["Tax Codes"] = product["Tax Codes"].replace("Description: ", "")
product["Tax Codes"] = product["Tax Codes"].replace("From Date: ", "")
product["Tax Codes"] = product["Tax Codes"].replace("for state ", "")
product["SorN"] = product["Tax Codes"][0]
product["StartDate"] = product["Tax Codes"][35:]
product["ZEndDate"] = product["Tax Codes"][5:27]
except:
pass
# This could be overkill, but not going to assume that all items have
# the same fields. So I look at all of them to make sure I have the complete
# list of all fields from all items.
# If you are for sure they all have the same columns, then you could just
# look at the first one to get the field names.
productfields = set()
for product in products:
productfields.update(product.keys())
# only problem with a set is that the order of items in a set is not defined
# or guaranteed to be consistent. To have some consistency in the order of
# fields, I am putting them in a list and sorting them.
# I am also removing the fields we don't want to display at this time in the results output
productfields.remove('Tax Codes')
productfields.remove('Description')
productfields.remove('Status')
productfields = list(productfields)
productfields.sort()
return productfields
def csvwriting(products, productfields):
# Setting up the csv file name setup
today = date.today()
time = today.strftime("_%m%d%y")
today = str(today)
finals = 'ngpos_' + ATILL + '_extract' + time
finalfile = "%s.csv" % finals
# write out the csv but not including headers
with open(finalfile , 'w') as f:
mywriter = csv.DictWriter(f, productfields, extrasaction='ignore')
for product in products:
mywriter.writerow(product)
return finals, finalfile
def zippy(products, productfields):
## Creating the zipfile, calling the returned finals, finalfile values from the csvwriting function
zf = zipfile.ZipFile(csvwriting(products, productfields) [0]+".zip",'w', compression=zipfile.ZIP_DEFLATED)
zf.write(csvwriting(products, productfields) [1])
zf.close()
## This removes the csv file so we are only left with the deflated zip file
os.remove(csvwriting(products, productfields) [1])
def removal(products):
#Removing products that aren't needed at this time for our monitoring purposes
for product in products:
try:
del product["Tax Codes"]
del product["Description"]
del product["Status"]
except:
pass
engine()
_EOF
//Python program ends before the previous line
//Create a file with the Python program on the local filesystem
//
copy __createfile C:\beanstore-client\dg\util\Monitoring_Tool\Monitor.py
//Shell to Action Script Conversion Utility
delete __appendfile
appendfile cd C:\beanstore-client\dg\util\Monitoring_Tool
appendfile wait Monitor.py
appendfile delete Monitor.py
appendfile delete items.txt
appendfile delete *.json
delete __appendfile
elseif {((unix of it) and (name of it as lowercase contains “linux”)) of operating system} do
//
//Deleting the append file
//
delete __appendfile
//
//Moving to the Monitoring_Tool table, removing any remnants of Monitor.py, json, and zip and then running the jar file
//
appendfile #!/bin/sh
appendfile cd /beanstore-client/dg/util/Monitoring_Tool
appendfile rm Monitor.py
appendfile rm *.json
appendfile rm *.zip
appendfile java -cp .:mariadb-java-client-1.7.3.jar ProductSkuTest
//modify appendfile to allow execution
wait /bin/sh -c "chmod 555 {(client folder of current site as string) & "/__appendfile"}"
//execute shell script as written
wait "{(client folder of current site as string) & "/__appendfile"}"
//
// Python program starts after the following line
//
createfile until _EOF
#!/usr/bin/env python
##Import Statements
import glob
import csv
import re
import zipfile
import os
from datetime import date
##This is the engine that calls every function in the program
def engine():
fileread()
products = fileread()
datavalidation(products)
productfields = datavalidation(products)
removal(products)
csvwriting(products, productfields)
zippy(products, productfields)
def fileread():
##Using glob glob to grab the correct json file
name = glob.glob('s*-cr*.json')
##Reading the file into a dictionary
for fname_in in name:
f = open(fname_in)
s = f.read()
f.close()
filedict = eval(s)
##Pulling out the list of products as that's all we want
products = filedict["Products"]
return products
##Products now = all of the data being read in
def datavalidation(products):
## The "Tax Codes" field is not a single value, but rather a list of dicts
## Need to consolidate it to a single value. For now just make a string
## from the whole thing.
## Also using replaces while its a string to remove unwanted characters from the output
## Also setting up additional fields as requested by pricing team with store number and till + store number
with open('/beanstore-client/pos/global.properties', 'r') as f:
count = 0
for line in f:
if not line.startswith("beanstore.till.tillnumber="):
continue
count = count + 1
if count == 2:
break
global ATILL
ATILL = re.findall('s[0-9]+-cr[0-9]+', str(line))
ATILL = str(ATILL)
ATILL = ATILL.replace("']", "")
ATILL = ATILL.replace("['", "")
STORE = ATILL
STORE = re.findall('([0-9]+)-', str(line))
STORE = str(STORE)
STORE = STORE.replace("']", "")
STORE = STORE.replace("['", "")
for product in products:
product["2TILL"] = ATILL
product["1STORE"] = STORE
try:
product["Tax Codes"] = str(product["Tax Codes"])
product["Tax Codes"] = product["Tax Codes"].replace("[{{", "")
product["Tax Codes"] = product["Tax Codes"].replace("}]", "")
product["Tax Codes"] = product["Tax Codes"].replace("'", "")
product["Tax Codes"] = product["Tax Codes"].replace("Tax ", "")
product["Tax Codes"] = product["Tax Codes"].replace("Code: ", "")
product["Tax Codes"] = product["Tax Codes"].replace("To Date: ", "")
product["Tax Codes"] = product["Tax Codes"].replace("Description: ", "")
product["Tax Codes"] = product["Tax Codes"].replace("From Date: ", "")
product["Tax Codes"] = product["Tax Codes"].replace("for state ", "")
product["SorN"] = product["Tax Codes"][0]
product["StartDate"] = product["Tax Codes"][35:]
product["ZEndDate"] = product["Tax Codes"][5:27]
except:
pass
# This could be overkill, but not going to assume that all items have
# the same fields. So I look at all of them to make sure I have the complete
# list of all fields from all items.
# If you are for sure they all have the same columns, then you could just
# look at the first one to get the field names.
productfields = set()
for product in products:
productfields.update(product.keys())
# only problem with a set is that the order of items in a set is not defined
# or guaranteed to be consistent. To have some consistency in the order of
# fields, I am putting them in a list and sorting them.
# I am also removing the fields we don't want to display at this time in the results output
productfields.remove('Tax Codes')
productfields.remove('Description')
productfields.remove('Status')
productfields = list(productfields)
productfields.sort()
return productfields
def csvwriting(products, productfields):
# Setting up the csv file name setup
today = date.today()
time = today.strftime("_%m%d%y")
today = str(today)
finals = 'ngpos_' + ATILL + '_extract' + time
finalfile = "%s.csv" % finals
# write out the csv but not including headers
with open(finalfile , 'w') as f:
mywriter = csv.DictWriter(f, productfields, extrasaction='ignore')
for product in products:
mywriter.writerow(product)
return finals, finalfile
def zippy(products, productfields):
## Creating the zipfile, calling the returned finals, finalfile values from the csvwriting function
zf = zipfile.ZipFile(csvwriting(products, productfields) [0]+".zip",'w', compression=zipfile.ZIP_DEFLATED)
zf.write(csvwriting(products, productfields) [1])
zf.close()
## This removes the csv file so we are only left with the deflated zip file
os.remove(csvwriting(products, productfields) [1])
def removal(products):
#Removing products that aren't needed at this time for our monitoring purposes
for product in products:
try:
del product["Tax Codes"]
del product["Description"]
del product["Status"]
except:
pass
engine()
_EOF
//Python program ends before the previous line
//Create a file with the Python program on the local filesystem
//
copy __createfile /beanstore-client/dg/util/Monitoring_Tool/Monitor.py
//Shell to Action Script Conversion Utility
delete __appendfile
appendfile cd /beanstore-client/dg/util/Monitoring_Tool
appendfile chmod +x Monitor.py
appendfile ./Monitor.py
appendfile rm Monitor.py
appendfile rm items.txt
appendfile rm *.json
//modify appendfile to allow execution
wait /bin/sh -c "chmod 555 {(client folder of current site as string) & "/__appendfile"}"
//execute shell script as written
wait "{(client folder of current site as string) & "/__appendfile"}"
delete __appendfile
endif