import argparse import pickle from Crypto.PublicKey import RSA from datetime import datetime, timedelta def encrypt(message, output_filename): private_key_file = 'conf/stack/license_key' with open(private_key_file, 'r') as private_key: encryptor = RSA.importKey(private_key) encrypted_data = encryptor.decrypt(message) with open(output_filename, 'wb') as encrypted_file: encrypted_file.write(encrypted_data) parser = argparse.ArgumentParser(description='Create a license file.') parser.add_argument('--users', type=int, default=20, help='Number of users allowed by the license') parser.add_argument('--days', type=int, default=30, help='Number of days for which the license is valid') parser.add_argument('--warn', type=int, default=7, help='Number of days prior to expiration to warn users') parser.add_argument('--output', type=str, required=True, help='File in which to store the license') if __name__ == "__main__": args = parser.parse_args() print ('Creating license for %s users for %s days in file: %s' % (args.users, args.days, args.output)) license_data = { 'LICENSE_EXPIRATION': datetime.utcnow() + timedelta(days=args.days), 'LICENSE_USER_LIMIT': args.users, 'LICENSE_EXPIRATION_WARNING': datetime.utcnow() + timedelta(days=(args.days - args.warn)), } encrypt(pickle.dumps(license_data, 2), args.output)