text: #!/usr/bin/python3
  import os
  import re
  import shutil
  import subprocess
  import argparse
  
  
  def parse_args():
      ap = argparse.ArgumentParser()
      ap.add_argument('-e', '--experiment', type=str, help='name of the experiment', default='original')
      ap.add_argument('-b', '--batch_size', type=int, help='batch size', default=1)
      ap.add_argument('-o', '--use_original', action='store_true', help='whether or not to use the orignal random number generator for simhash')
      ap.add_argument('-d', '--dump_rand_bits', action='store_true', help='whether or not to dump the random bits used in simhash')
      ap.add_argument('-s', '--strict_ratio', action='store_true', help='whether or not to strict ratio in simhash random bits')
      ap.add_argument('-r', '--random_hash', type=str, help='which random hash function to use', default='')
  
      args = vars(ap.parse_args())
  
      args['base_data_dir'] = os.environ['AMAZON_DATA']
      args['base_log_dir'] = os.environ['SLIDE_LOG']
      args['train_data'] = os.path.join(args['base_data_dir'], 'amazon_train.txt')
      args['test_data'] = os.path.join(args['base_data_dir'], 'amazon_test.txt')
      args['log_dir'] = os.path.join(args['base_log_dir'], args['experiment'])
      args['log_file'] = os.path.join(args['log_dir'], 'log.txt')
      args['rand_bits_dir'] = os.path.join(args['log_dir'], 'rand_bits')
      args['slurm_out_file'] = os.path.join(args['log_dir'], f'{args["experiment"]}.out')
      args['cwd'] = os.path.dirname(os.path.abspath(__file__))
      args['bin_dir'] = os.path.join(args['cwd'], 'bin', args['experiment'])
      args['config_amz'] = os.path.join(args['cwd'], 'SLIDE', 'Config_amz.csv')
      args['config_h'] = os.path.join(args['cwd'], 'SLIDE', 'Config.h')
  
      return args
  
  
  def write_amz_config(args):
      weight_path = os.path.join(args['log_dir'], f'{args["experiment"]}.npz')
      with open(args['config_amz'], 'r') as f:
          data = f.read()
      with open(args['config_amz'], 'w') as f:
          f.truncate(0)
          data = re.sub(r'^Batchsize=.*\n', f'Batchsize={args["batch_size"]}\n', data, flags=re.MULTILINE)
          data = re.sub(r'^trainData=.*\n', f'trainData={args["train_data"]}\n', data, flags=re.MULTILINE)
          data = re.sub(r'^testData=.*\n', f'testData={args["test_data"]}\n', data, flags=re.MULTILINE)
          data = re.sub(r'^logFile=.*\n', f'logFile={args["log_file"]}\n', data, flags=re.MULTILINE)
          data = re.sub(r'^logDir=.*\n', f'logDir={args["log_dir"]}\n', data, flags=re.MULTILINE)
          data = re.sub(r'^weight=.*\n', f'weight={weight_path}\n', data, flags=re.MULTILINE)
          data = re.sub(r'^savedweight=.*\n', f'savedweight={weight_path}\n', data, flags=re.MULTILINE)
          f.write(data)
  
  
  def write_config_h(args):
      with open(args['config_h'], 'r') as f:
          data = f.read()
      with open(args['config_h'], 'w') as f:
          f. truncate(0)
          if args['dump_rand_bits']:
              data = re.sub(r'^.*#define DUMP_RAND_BITS\n', '#define DUMP_RAND_BITS\n', data, flags=re.MULTILINE)
          else:
              data = re.sub(r'^.*#define DUMP_RAND_BITS\n', '//#define DUMP_RAND_BITS\n', data, flags=re.MULTILINE)
          
          if args['strict_ratio'] or args['use_original']:
              data = re.sub(r'^.*#define STRICT_RATIO\n', '#define STRICT_RATIO\n', data, flags=re.MULTILINE)
          else:
              data = re.sub(r'^.*#define STRICT_RATIO\n', '//#define STRICT_RATIO\n', data, flags=re.MULTILINE)
  
          if args['use_original']:
              data = re.sub(r'^.*#define USE_ORIGINAL_SRP\n', '#define USE_ORIGINAL_SRP\n', data, flags=re.MULTILINE)
          else:
              data = re.sub(r'^.*#define USE_ORIGINAL_SRP\n', '//#define USE_ORIGINAL_SRP\n', data, flags=re.MULTILINE)
              data = re.sub(r'^.*#define RANDOM_HASH .*\n', f'#define RANDOM_HASH {args["random_hash"]}\n', data, flags=re.MULTILINE)
  
          f.write(data)
  
  
  def write_sbatch_file(args):
      with open('submit.slurm', 'r') as f:
          data = f.read()
      with open('submit.slurm', 'w') as f:
          f.truncate(0)
  
          data = re.sub(r'^#SBATCH --output=.*\n', f'#SBATCH --output={args["slurm_out_file"]}\n', data, flags=re.MULTILINE)
          data = re.sub(r'^#SBATCH --job-name=.*\n', f'#SBATCH --job-name={args["experiment"]}\n', data, flags=re.MULTILINE)
          data = re.sub(r'^cd .*\n', f'cd {args["bin_dir"]}\n', data, flags=re.MULTILINE)
          data = re.sub(r'^\./runme .*\n', f'./runme {os.path.join(args["log_dir"], "Config_amz.csv")}\n', data, flags=re.MULTILINE)
  
          f.write(data)
  
  
  def reset_dir(dir_name):
      if os.path.exists(dir_name):
          shutil.rmtree(dir_name)
      os.makedirs(dir_name)
  
  
  def run(args):
      reset_dir(args['bin_dir'])
      reset_dir(args['log_dir'])
      if args['dump_rand_bits']:
          os.makedirs(args['rand_bits_dir'])
  
      shutil.copyfile(args['config_amz'], os.path.join(args['log_dir'], 'Config_amz.csv'))
      shutil.copyfile(args['config_h'], os.path.join(args['log_dir'], 'Config.h'))
  
      subprocess.run(['cmake', '../../'], cwd=args['bin_dir'])
      subprocess.run(['make'], cwd=args['bin_dir'])
      subprocess.run(['sbatch', 'submit.slurm'], cwd=args['cwd'])
  
  
  if __name__ == '__main__':
      args = parse_args()
      write_amz_config(args)
      write_config_h(args)
      write_sbatch_file(args)
      run(args)
  