-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathsfsync.py
executable file
·154 lines (125 loc) · 4.76 KB
/
sfsync.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
#!/usr/local/bin/python3
#hashchecker.py
#Current Version: 1.0
#Version History
# Old Version History is lost....
# 1.0 - 20200612
# -Added shebang #!/usr/local/bin/python3 so that we don't need to pt python3 before the script
#######################################################################################################################
###REQUIRED LIBRARIES####
###simple_salesforce
#######################################################################################################################
import os
import time
import re
import argparse
import config
import csv
import json
from simple_salesforce import Salesforce
def getBarcode(dict):
barcode = dict.get("Filename")[4:11] #get the barcode from the filename
for b in barcode:
if not b.isdigit(): #this makes sure that the barcode is 7 numbers. if not it'll throw a failure
print("ERROR: File Barcode Not Found for " + sourceBasename)
else:
dict["Barcode"] = barcode
return dict
def querySF(sf,barcode):
result = sf.query("SELECT Id FROM Preservation_Object__c WHERE Name = '" + barcode + "'")
return result
def initLog(sourceCSV):
'''
initializes log file
txtFile = open(destination + "/LoadingScript.log", "a+")
txtFile.write("Load and Verify Script Started at: " + time.strftime("%Y-%m-%d_%H:%M:%S") + "\n")
for f in sourceList:
txtFile.write("From: " + f + "\n")
txtFile.write("To: " + destination + "\n")
txtFile.write("Hash algorithm: " + hashalg + "\n")
txtFile.write("\n\n")
txtFile.close()
'''
def logNewLine(text,destination):
txtFile = open(destination + "/LoadingScript.log", "a+")
txtFile.write("\n" + time.strftime("%Y-%m-%d_%H:%M:%S") + ": " + text)
def logSameLine(text,destination):
txtFile = open(destination + "/LoadingScript.log", "a+")
txtFile.write(text)
def createDictList(inputArgs):
dict_list = []
for c in inputArgs:
with open(c, mode='r') as infile:
reader = csv.DictReader(infile)
for line in reader:
dict_list.append(line)
return dict_list
def processList(dict_list,sf):
for d in dict_list:
#create json out of ordered dict
dString = json.dumps(d)
#some string magic to fix improperly named fields
temp = dString.replace("messageDigestAlgorithm", "messageDigestAlgorithm__c")
dString = temp.replace("\"messageDigest\"", "\"messageDigest__c\"")
#turn the string back into a JSON sturcture
j = json.loads(dString)
#get the record ID of the associated salesforce record
sfData = querySF(sf,d['Name'])
recordID = sfData['records'][0]['Id']
#insert the metadata!
print(bcolors.OKBLUE + "\nInserting Metadata for record: " + bcolors.ENDC + d['Name'])
try:
sf.Preservation_Object__c.update(recordID,j)
print(bcolors.OKGREEN + "\nSuccess!" + bcolors.ENDC)
except:
print(bcolors.FAIL + "\nFailed!" + bcolors.ENDC)
def make_args():
'''
initialize arguments from the cli
'''
parser = argparse.ArgumentParser()
parser.add_argument('sourceCSV',nargs='+',help="As many files are directories you would like processed. Only sidecar checksum files are processed.")
return parser.parse_args()
# Used to make colored text
class bcolors:
HEADER = '\033[95m'
OKBLUE = '\033[94m'
OKGREEN = '\033[92m'
WARNING = '\033[93m'
FAIL = '\033[91m'
ENDC = '\033[0m'
BOLD = '\033[1m'
UNDERLINE = '\033[4m'
def main():
'''
do the thing
'''
#init args from cli
args = make_args()
#init salesforce login#
try:
print(bcolors.OKBLUE + "\nConnecting To Salesforce" + bcolors.ENDC)
sf = Salesforce(username=config.username,password=config.password,security_token=config.security_token)
print(bcolors.OKGREEN + "\nSalesforce connection succesfull!" + bcolors.ENDC)
except:
print(bcolors.FAIL + "\nSalesforce Connection Failed. Quitting Script" + bcolors.ENDC)
exit()
#init variables
#Check that input conforms
if len(args.sourceCSV) < 1: #if less than two input arguments we have to exit
print("CRITICAL ERROR: You must give this script at least one argument")
exit()
for c in args.sourceCSV:
if os.path.splitext(c)[1] != ".csv":
print("CRITICAL ERROR: This script can only accept CSV files!")
exit()
#Initialize log
#initLog(args.sourceCSV)
#Create dict list containing CSV info
dict_list = createDictList(args.sourceCSV)
#update the data from each CSV into Salesforce
processList(dict_list,sf)
print("\n")
#tally up the success and failures, print the failed files.
#this isn't in here yet
main()