def handle_encrypted(my, base_dir, transaction_code, encrypted): key = my.ticket from_path = "%s/%s" % (base_dir, encrypted) tmp_dir = Environment.get_tmp_dir(include_ticket=True) if encrypted.endswith(".enc"): to_path = "%s/%s" % (tmp_dir, encrypted) to_path = to_path.replace(".enc", "") encrypt_util = EncryptUtil(key) encrypt_util.decrypt_file(from_path, to_path) zip_util = ZipUtil() to_dir = os.path.dirname(to_path) zip_util.extract(to_path) else: to_path = from_path to_dir = tmp_dir zip_util = ZipUtil() zip_util.extract(to_path, to_dir) dirname = encrypted.replace(".enc", "") dirname = dirname.replace(".zip", "") print "Running transaction: [%s]" % transaction_code my.handle_transaction(to_dir, transaction_code, dirname)
def handle_file_mode(self, base_dir, transaction_code, paths, log, transaction_xml, ticket): # drop the transaction into a folder timestamp = log.get_value("timestamp") timestamp = parser.parse(timestamp) timestamp = timestamp.strftime("%Y%m%d_%H%M%S") asset_dir = Environment.get_asset_dir() # create the transactions dir if it does not exist if not os.path.exists("%s/transactions" % base_dir): os.makedirs("%s/transactions" % base_dir) base_dir = "%s/transactions/%s" % (base_dir, transaction_code) is_encrypted = True if is_encrypted == True: # put the transaction in a temp folder tmp_dir = Environment.get_tmp_dir(include_ticket=True) tmp_dir = "%s/%s-%s" % (tmp_dir, transaction_code, timestamp) else: tmp_dir = "%s/%s" % (base_dir, timestamp) if not os.path.exists(tmp_dir): os.makedirs(tmp_dir) from pyasm.common import EncryptUtil encrypt = EncryptUtil(ticket) # create a simple manifest file f = open("%s/manifest.xml" % tmp_dir, 'wb') f.write('''<manifest code='transaction_log' version='1'>\n''') f.write(''' <sobject search_type="sthpw/transaction_log"/>\n''') f.write('''</manifest>\n''') f.close() tpath = "%s/sthpw_transaction_log.spt" % tmp_dir from pyasm.search import TableDataDumper dumper = TableDataDumper() dumper.set_delimiter("#-- Start Entry --#", "#-- End Entry --#") dumper.set_include_id(False) dumper.set_sobjects([log]) dumper.dump_tactic_inserts(tpath, mode='sobject') tpath = "%s/_transaction.xml" % tmp_dir #f = open(tpath, 'wb') f = codecs.getwriter('utf8')(open(tpath, 'wb')) f.write(transaction_xml.to_string()) f.close() # copy the checked in files for path in paths: rel_path = path.replace(asset_dir, "") rel_path = rel_path.lstrip("/") to_path = "%s/%s" % (tmp_dir, rel_path) to_dir = os.path.dirname(to_path) if not os.path.exists(to_dir): os.makedirs(to_dir) shutil.copy(path, to_dir) # zip up and encrypt the transaction if is_encrypted: zip_path = "%s.zip" % (tmp_dir) from pyasm.common import ZipUtil zip = ZipUtil() zip.zip_dir("%s" % (tmp_dir), zip_path) encrypt.encrypt_file(zip_path) shutil.move("%s.enc" % zip_path, "%s-%s.zip.enc" % (base_dir, timestamp)) rmdir = os.path.dirname(tmp_dir) shutil.rmtree(rmdir) #os.unlink("%s.zip" % tmp_dir) job = self.kwargs.get("job") job.set_value("error_log", "") job.commit() return