diff options
-rwxr-xr-x | dtools/bin/fix-copyright-years | 65 | ||||
-rw-r--r-- | grc/python/Block.py | 74 | ||||
-rw-r--r-- | grc/python/Port.py | 2 |
3 files changed, 106 insertions, 35 deletions
diff --git a/dtools/bin/fix-copyright-years b/dtools/bin/fix-copyright-years new file mode 100755 index 000000000..bb0f3009d --- /dev/null +++ b/dtools/bin/fix-copyright-years @@ -0,0 +1,65 @@ +#!/usr/bin/env python + +import re +import datetime +import subprocess +import multiprocessing + +def command(*args): return subprocess.Popen(args, stdout=subprocess.PIPE).communicate()[0] + +def is_gnuradio_co_source(lines): + for line in lines[:20]: + if 'GNU Radio is free software' in line: return True + return False + +def get_gnuradio_co_line(lines): + for i, line in enumerate(lines[:5]): + if 'Copyright' in line and 'Free Software Foundation' in line: return line, i + return None + +def fix_co_years(files): + for file in files: + print file + lines = open(file).readlines() + if not is_gnuradio_co_source(lines): continue + + #extract the years from the git history + years = set(map( + lambda l: int(l.split()[-2]), + filter( + lambda l: l.startswith('Date'), + command('git', 'log', file).splitlines(), + ), + )) + + #extract line and line number for co line + try: line, num = get_gnuradio_co_line(lines) + except: continue + + #extract years from co string + try: + co_years_str = re.match('^.*Copyright (.*) Free Software Foundation.*$', line).groups()[0] + co_years = set(map(int, co_years_str.split(','))) + except: print ' format error on line %d: "%s"'%(num, line); continue + + #update the years if missing any + all_years = co_years.union(years) + if all_years != co_years: + print ' missing years: %s'%(', '.join(map(str, sorted(all_years - co_years)))) + all_years.add(datetime.datetime.now().year) #add the current year + all_years_str = ', '.join(map(str, sorted(all_years))) + new_text = ''.join(lines[:num] + [line.replace(co_years_str, all_years_str)] + lines[num+1:]) + open(file, 'w').write(new_text) + +if __name__ == "__main__": + #get recursive list of files in the repo + files = command('git', 'ls-tree', '--name-only', 'HEAD', '-r').splitlines() + + #start n+1 processes to handle the files + num_procs = multiprocessing.cpu_count() + procs = [multiprocessing.Process( + target=lambda *files: fix_co_years(files), + args=files[num::num_procs], + ) for num in range(num_procs)] + map(multiprocessing.Process.start, procs) + map(multiprocessing.Process.join, procs) diff --git a/grc/python/Block.py b/grc/python/Block.py index dd39b095d..bd03eb5cd 100644 --- a/grc/python/Block.py +++ b/grc/python/Block.py @@ -75,42 +75,48 @@ class Block(_Block, _GUIBlock): Add and remove ports to adjust for the nports. """ _Block.rewrite(self) + + def insert_port(get_ports, get_port, key): + prev_port = get_port(str(int(key)-1)) + get_ports().insert( + get_ports().index(prev_port)+1, + prev_port.copy(new_key=key), + ) + #restore integer contiguity after insertion + for i, port in enumerate(get_ports()): port._key = str(i) + + def remove_port(get_ports, get_port, key): + port = get_port(key) + for connection in port.get_connections(): + self.get_parent().remove_element(connection) + get_ports().remove(port) + #restore integer contiguity after insertion + for i, port in enumerate(get_ports()): port._key = str(i) + #adjust nports for get_ports, get_port in ( (self.get_sources, self.get_source), (self.get_sinks, self.get_sink), ): - #how many streaming (non-message) ports? - num_ports = len(filter(lambda p: p.get_type() != 'msg', get_ports())) - #do nothing for 0 ports - if not num_ports: continue - #get the nports setting - port0 = get_port(str(0)) - nports = port0.get_nports() - #do nothing for no nports - if not nports: continue - #do nothing if nports is already num ports - if nports == num_ports: continue - #remove excess ports and connections - if nports < num_ports: - #remove the connections - for key in map(str, range(nports, num_ports)): - port = get_port(key) - for connection in port.get_connections(): - self.get_parent().remove_element(connection) - #remove the ports - for key in map(str, range(nports, num_ports)): - get_ports().remove(get_port(key)) - continue - #add more ports - if nports > num_ports: - for key in map(str, range(num_ports, nports)): - prev_port = get_port(str(int(key)-1)) - get_ports().insert( - get_ports().index(prev_port)+1, - prev_port.copy(new_key=key), - ) - continue + master_ports = filter(lambda p: p.get_nports(), get_ports()) + for i, master_port in enumerate(master_ports): + nports = master_port.get_nports() + index_first = get_ports().index(master_port) + try: index_last = get_ports().index(master_ports[i+1]) + except IndexError: index_last = len(get_ports()) + num_ports = index_last - index_first + #do nothing if nports is already num ports + if nports == num_ports: continue + #remove excess ports and connections + if nports < num_ports: + for key in map(str, range(index_first+nports, index_first+num_ports)): + remove_port(get_ports, get_port, key) + continue + #add more ports + if nports > num_ports: + for key in map(str, range(index_first+num_ports, index_first+nports)): + insert_port(get_ports, get_port, key) + continue def port_controller_modify(self, direction): """ @@ -119,10 +125,8 @@ class Block(_Block, _GUIBlock): @return true for change """ changed = False - #concat the nports string from the private nports settings of both port0 - nports_str = \ - (self.get_sinks() and self.get_sinks()[0]._nports or '') + \ - (self.get_sources() and self.get_sources()[0]._nports or '') + #concat the nports string from the private nports settings of all ports + nports_str = ' '.join([port._nports for port in self.get_ports()]) #modify all params whose keys appear in the nports string for param in self.get_params(): if param.is_enum() or param.get_key() not in nports_str: continue diff --git a/grc/python/Port.py b/grc/python/Port.py index 6965371df..6e5a5c59f 100644 --- a/grc/python/Port.py +++ b/grc/python/Port.py @@ -167,5 +167,7 @@ class Port(_Port, _GUIPort): def copy(self, new_key=None): n = self._n.copy() + #remove nports from the key so the copy cannot be a duplicator + if n.has_key('nports'): n.pop('nports') if new_key: n['key'] = new_key return self.__class__(self.get_parent(), n, self._dir) |