def split_merge_comments(statements): """Find open and close comments.""" def is_genuine_ending(cur): return ((cur.strip()[:2] == '(*' and cur.strip()[-2:] == '*)' ) # cur is just a comment or cur.strip()[-1:] in '.}' or cur.strip() in BRACES) cur = None comment_level = 0 for stmt in statements: str_count = 0 for i in re.split(r'(?<=\*\)) ', stmt.replace('*)', '*) ')): if str_count % 2 == 0: i2 = re.sub('"[^"]*"|"[^"]*$', '', i) else: i2 = re.sub('^[^"]*"|"[^"]*"', '', i) str_count += i.count('"') #print((repr(i), comment_level, repr(i2))) if '(*' not in i2 and '*)' not in i2: if comment_level < 0: if cur is not None: for ret in split_leading_braces(cur + i): yield ret cur = None else: raw_input('UNEXPECTED COMMENT: %s' % i) yield i comment_level = 0 elif comment_level == 0: if cur is None: for ret in split_leading_braces(i): yield ret else: cur += i elif cur is None: cur = i else: cur += i else: comment_level += i2.count('(*') - i2.count('*)') if cur is None: cur = i else: cur += i if cur is not None: curs = list(split_leading_braces(cur)) while curs and curs[0].strip() in BRACES: yield curs.pop(0) cur = ''.join(curs) if curs else None if cur is not None and is_genuine_ending( cur ) and comment_level == 0: # clear out cur, if we're done with it yield cur cur = None if cur is not None: print('Unterminated comment') yield cur
def fix_identifiers(idents, libname): for ident in idents: if ident[:len('Warning:')] == 'Warning:': raw_input(ident) continue elif '.' not in ident: yield ident else: idx = ident.index('.') overlap, rest = ident[:idx], ident[idx:] if libname[-(idx+1):] == '.' + overlap: yield libname + rest else: #raw_input((ident, libname)) #yield ident continue
def parse(self, address): """address (sth of the form user@host:port) is converted to [user, host, port]. """ if address is None: hostport = raw_input("UI Host:") user = raw_input("UI Username:") else: user, hostport = address.split('@') if ':' in hostport: host, port = hostport.split(':') else: host, port = hostport, 22 port = int(port) return [user, host, port]
def cancel(self): """ Cancel a job """ input = raw_input('Are you sure you want to cancel the job with id: '\ + self.__id + ' [n]: ') if (len(input) == 1) & (input[0] == 'y'): ex = self.__executor ex.exec(['edg-job-cancel' , self.__id]) ex.writeBytes(input[0]+"\n") print "Job " + self.name + " is being cancelled... Check the status of the job."
def write(self, bytes = None): """ Function to read from system in and write to the process input (or the proc output) """ writer = self.__outputwriter if bytes is None: bytes = raw_input() #for i in bytes[:]: # print ord(i) writer.writeBytes(bytes+"\n") writer.flush()
def move_from_proof(filename, **kwargs): if kwargs['verbose']: kwargs['log']('Processing %s...' % filename) try: with open(filename, 'r') as f: contents = f.read() except IOError as e: if kwargs['verbose']: kwargs['log']('Failed to process %s' % filename) if kwargs['verbose'] >= 2: kwargs['log'](repr(e)) return ret = [] cur_statements = [] cur_statement = [] deferred_copied_statements = [] deferred_statements = [] orig_space_count = 0 cur_diff_space_count = 0 if ''.join(split_coq_file_contents_with_comments(contents)) != contents: kwargs['log']('WARNING: Could not split %s' % filename) return for i in split_coq_file_contents_with_comments(contents): is_definition_full = (ALL_DEFINITIONS_REG.match(i) is not None and (':=' in strip_parens(strip_comments(i)) or ONELINE_DEFINITIONS_REG.match(i))) is_definition_start = (ALL_DEFINITIONS_REG.match(i) is not None and ':=' not in strip_parens(strip_comments(i)) and not ONELINE_DEFINITIONS_REG.match(i)) #print((is_definition_start, ONELINE_DEFINITIONS_REG.match(i), ALL_DEFINITIONS_REG.match(i), i)) is_definition_end = ALL_ENDINGS.match(i) is not None #if 'not_reachable_iff' in i: # print((ALL_DEFINITIONS_REG.match(i), strip_parens(strip_comments(i)), ONELINE_DEFINITIONS_REG.match(i))) if not is_definition_start and not cur_statements and not cur_statement: if kwargs['verbose'] >= 3: kwargs['log'](repr(i)) ret.append(i) elif is_definition_start: if kwargs['verbose'] >= 2: kwargs['log']('Starting definition (%d): %s' % (len(deferred_statements), repr(i))) if not cur_statement and not deferred_statements: orig_space_count = len(get_leading_space(i)) if cur_statement: deferred_statements.append( (cur_diff_space_count, cur_statement)) cur_diff_space_count = max( 0, len(get_leading_space(i)) - orig_space_count) cur_statement = [remove_leading_space(i, cur_diff_space_count)] elif (SAFE_REG.match(i) or not i.strip()) and cur_statement: if kwargs['verbose'] >= 3: kwargs['log'](repr(i)) cur_statement.append(remove_leading_space(i, cur_diff_space_count)) elif is_definition_end and cur_statement: if kwargs['verbose'] >= 2: kwargs['log']('Ending definition: ' + repr(i)) cur_statement.append(remove_leading_space(i, cur_diff_space_count)) cur_statements.append(''.join(cur_statement)) #print(''.join(preminimize_lifted_statements(cur_statements))) if deferred_statements: cur_diff_space_count, cur_statement = deferred_statements.pop() deferred_copied_statements = list( preminimize_lifted_statements(deferred_copied_statements)) cur_statement.extend( deferred_copied_statements) # todo: fix indentation else: #print('extending') #print(''.join(minimize_lifted_statements(cur_statements))) ret.extend(list(minimize_lifted_statements(cur_statements))) cur_statement = [] cur_statements = [] deferred_copied_statements = [] elif MOVE_UP_REG.match(i) or is_definition_full: if kwargs['verbose'] >= 2: kwargs['log']('Lifting: ' + repr(i)) cur_statements.append(set_leading_space(i, orig_space_count)) elif COPY_UP_REG.match(i) and cur_statement: if kwargs['verbose'] >= 2: kwargs['log']('Lift-copying: ' + repr(i)) cur_statement.append(remove_leading_space(i, cur_diff_space_count)) cur_statements.append(set_leading_space(i, orig_space_count)) deferred_copied_statements.append( remove_leading_space(i, cur_diff_space_count)) else: raw_input('WARNING: Unrecognized: %s' % repr(i)) #print(cur_diff_space_count) #print(remove_leading_space(i, cur_diff_space_count)) if cur_statements or deferred_statements or cur_statement: raw_input('WARNING: extra statements: %s' % repr( (cur_statements, cur_statement, deferred_statements))) cur_statements.append(''.join(cur_statement)) for i in deferred_statements: cur_statements.append(''.join(i)) ret.extend(list(minimize_lifted_statements(cur_statements))) ret = ''.join(ret) if ret == contents: return if kwargs['inplace']: do_backup = kwargs['suffix'] is not None and len(kwargs['suffix']) > 0 write_to_file(filename, ret, do_backup=do_backup, backup_ext=kwargs['suffix']) else: print(ret)