We currently use TFS as ticket system for Support Tickets. I wrote the code below so that a user can dump an email into a folder and then access a webpage to submit a form and create a TFS Support Ticket and assign it to the user. The goal is to have the emails removed from the folder after the form has been submitted and the Support Ticket has been created.
The problem I'm running into is that when I try to remove the emails after the TFS Support Ticket has been created it removes some of the files but then gets stuck on the last one and display the following error message.
"PermissionError: [WinError 32] The process cannot access the file because it is being used by another process: 'C:\Users\b4bw3\Documents\Python\simple-salesforce\Email\Delete_Me1.msg'"
If I remove the portion of code that creates the TFS ticket then it will rename the email and remove it after it is done as expected. It appears that the hang up is with TFS. I'm stumped and need help.
Any thoughts? btw, this is my first attempt at Python, so be gentle. :)
'''
# -*- coding: utf-8 -*-
import extract_msg
import cgi
import json
from tfs import TFSAPI
import os, sys
import requests
import glob
import cgitb
cgitb.enable()
form = cgi.FieldStorage()
associate = form.getvalue('associate')
login = json.load(open('login.json'))
personal_access_token = login['tfs_login']
client = TFSAPI("https://url/", project="LoanSystems/", pat=personal_access_token)
os.chdir('C:\\Users\\b4bw3\\Documents\\Python\\simple-salesforce\\Email')
def e2tfs():
associate = form.getvalue('associate')
i=1
for file in os.listdir():
src = file
dst = "Delete_Me"+str(i)+".msg"
os.rename(src,dst)
msg = extract_msg.Message(dst)
msg_sender = msg.sender
# msg_date = msg.date
msg_subj = msg.subject
msg_message = msg.body
i+=1
fields = {'System.Title' : 'E2TFS: {}'.format(msg_subj),
'Microsoft.VSTS.CMMI.Symptom': 'Body: {}'.format(msg_message),
'Microsoft.VSTS.TCM.ReproSteps': 'TBD',
'Regions.Custom.DocumentationArea': 'Unknown',
'Regions.Custom.Application': 'nCino',
'Regions.Custom.Channel': 'Email',
'Microsoft.VSTS.CMMI.FoundInEnvironment': 'Production',
'Regions.Custom.ImpactedAssociate': 'Sender: {}'.format(msg_sender),
'Regions.Custom.Associate_Role': 'ALL USERS',
'Regions.Custom.BusinessGroupsImpacted2': 'All Business Groups',
'AFS.phase.dev': 'ALL USERS',
'Regions.Custom.PriorityCustomField': 'High',
'Regions.Custom.CaseOwner': associate
}
client.create_workitem('Support Ticket', fields=fields)
query_tfs = "SELECT [System.Id], [System.Title] FROM workitems WHERE [System.CreatedDate] = @today AND [System.CreatedBy] = @me AND [System.WorkItemType] = 'Support Ticket'"
wiql = client.run_wiql(query_tfs)
# Get all found workitems
workitems = wiql.workitems
tfs_number = workitems[-1]['Id']
print(f"Support Ticket {tfs_number} was created.\n")
#Create a link to the Support Ticket
url = 'https://sfdctfs/tfs/LoanSystems/SalesForce%20COE/_workitems/edit/' + str(tfs_number)
print("<a target=_blank href=\"" + url + "\"> Click Here to View in a New Tab</a></br></p>\r\n")
filelist = glob.glob('C:\\Users\\b4bw3\\Documents\\Python\\simple-salesforce\\Email\\*.msg')
for files in filelist:
# print(f'{files} to be removed\n')
os.remove(files)
# print(f'{files} file was removed\n')
print("Content-type:text/html\r\n\r\n")
print("<html>")
print("<head>")
print("<title>Email to TFS</title>")
print("</head>")
print("<body>")
print("<h2>Email to TFS</h2> <br />")
print('<a href=\"http://localhost:8000/cgi-bin/case.py\">Click here</a> if you need to create a "Support Ticket" from a "Case Assignment."<br /><br />')
print("1. Drag and drop the email(s) to the designated folder.<br />")
print('2. Select an Associate below to assign as the "Case Owner" on the "Support Ticket(s)."<br />')
print('3. Click the "Submit" button to generate a "Support Ticket(s)" in TFS. <br /><br />')
print("<form action =\"/cgi-bin/template.py\">")
print("Associate: <select name=\"associate\"> <br /> ")
print("<option value=\"--None--\">--None--</option>")
print("<option value=\"April\">April</option>")
print("<option value=\"Mac\">Mac</option>")
print("<option value=\"Michael\">Michael</option>")
print("<option value=\"Chris\">Chris</option>")
print("<input type = \"submit\" value = \"Submit\">")
print("</form><br />")
print("</body>")
print("</html>")
if associate != None:
e2tfs()
print("<i>**Remember to remove the emails out of the directory when you're done.</i>")
'''
I did try adding the close() in the bottom of the e2tfs() method but I received the following error:
A problem occurred in a Python script. Here is the sequence of function calls leading up to the error, in the order they occurred.
C:\Users\b4bw3\Documents\Python\simple-salesforce\cgi-bin\template2.py in ()
70 print("</html>")
71 if associate != None:
=> 72 e2tfs()
73 print("<i>**Remember to remove the emails out of the directory when you're done.</i>")
74
e2tfs = C:\Users\b4bw3\Documents\Python\simple-salesforce\cgi-bin\template2.py in e2tfs()
40 for files in filelist:
41 print(f'{files} to be removed\n')
=> 42 files.close()
43 os.remove(files)
44 print(f'{files} file was removed\n')
files = r'C:\Users\b4bw3\Documents\Python\simple-salesforce\Email\Delete_Me1.msg', files.close undefined AttributeError: 'str' object has no attribute 'close' args = ("'str' object has no attribute 'close'",) with_traceback =
'''
filelist = glob.glob('C:\\Users\\b4bw3\\Documents\\Python\\simple-salesforce\\Email\\*.msg')
for files in filelist:
print(f'{files} to be removed\n')
files.close()
os.remove(files)
print(f'{files} file was removed\n')
'''