#!/usr/bin/env python

import shlex
import subprocess
import numpy as np
import re

prog = re.compile('.*BUFFACQ')

ind_list = np.linspace(1,15,15).astype('int')

for i in ind_list:
    PV_base = 'EDEF:SYS1:{:0.0f}'.format(i)
    PV_name = '{}:NAME'.format(PV_base)
    command_check = 'caget {}'.format(PV_name)
    
    process = subprocess.Popen(shlex.split(command_check), stderr=subprocess.PIPE, stdout=subprocess.PIPE)
    stdout, stderr = process.communicate()
    
    result = prog.match(stdout)
    
    if result:
        command_clear_name = 'caput {} \'\''.format(PV_name)
        subprocess.call(shlex.split(command_clear_name))
        PV_release = '{}:FREE.PROC'.format(PV_base)
        command_release = 'caput {} 1'.format(PV_release)
        subprocess.call(shlex.split(command_release))

