#!/usr/bin/python3 import cgi, shelve, random, time, os, os.path, sys, subprocess, time import iTunesInfo, analyzedb from PersistentDict import PersistentDict #import cgitb #cgitb.enable() form = cgi.FieldStorage() # First, generate a session randomly. session = ''.join([chr(random.randint(0,25)+ord('a')) for i in range(8)]) while os.path.exists('sessions/shelf.' + session): session = ''.join([chr(random.randint(0,25)+ord('a')) for i in range(8)]) #shelf = shelve.open('sessions/shelf.' + session, writeback=True) #shelf = shelve.Shelf(PersistentDict('sessions/shelf.' + session, 'c', format='json')) shelf = PersistentDict('sessions/shelf.' + session, 'c', format='json') shelf["data"] = {"status": "Receiving file...", "done": False} shelf.close() iTunesFileText = form['iTunesFile'].value.decode('utf-8') doPlaylists = 'doPlaylists' in form with open('sessions/%s' % session, 'w') as outputFile: outputFile.write(iTunesFileText) print("Content-type: text/plain\n\n") #print('
%s
' % (session, session)) print(session) # I tried to make this work by closing stdout and forking, but that doesn't seem to work # anymore. # https://stackoverflow.com/questions/6024472/start-background-process-daemon-from-cgi-script # Neither does starting a subprocess with start_new_session=True. #subprocess.Popen(['./driveiTunesFile.py', session, '1' if doPlaylists else '0'], start_new_session=True) # So use this hack. os.system("echo \"%s %s %s\" | /usr/bin/at now" % (os.path.join(os.getcwd(), 'driveiTunesFile.py'), session, '1' if doPlaylists else '0')) sys.exit()