From f7a79c0003fc3a03d5e31d8b7dd1f1fb50477a82 Mon Sep 17 00:00:00 2001 From: Your Name Date: Fri, 30 May 2014 19:58:07 +0200 Subject: [PATCH] Binary for fixing filenames. --- Gruntfile.js | 9 +++++ bin/fix_key_file_names.py | 69 +++++++++++++++++++++++++++++++++++++++ package.json | 2 +- server/main.js | 0 4 files changed, 79 insertions(+), 1 deletion(-) create mode 100755 bin/fix_key_file_names.py mode change 100755 => 100644 server/main.js diff --git a/Gruntfile.js b/Gruntfile.js index ae34016..d39d4ea 100644 --- a/Gruntfile.js +++ b/Gruntfile.js @@ -304,6 +304,9 @@ module.exports = function (grunt) { // Copies remaining files to places other tasks can use copy: { dist: { + options: { + mode: true + }, files: [ { expand: true, @@ -342,6 +345,12 @@ module.exports = function (grunt) { cwd: '.', dest: '<%= yeoman.dist %>/', src: ['package.json'] + }, + { + expand: true, + cwd: 'bin', + dest: '<%= yeoman.dist %>/bin', + src: ['*'] } ] }, diff --git a/bin/fix_key_file_names.py b/bin/fix_key_file_names.py new file mode 100755 index 0000000..5bd07ec --- /dev/null +++ b/bin/fix_key_file_names.py @@ -0,0 +1,69 @@ +#!/usr/bin/env python2 + +import os +import sys + +if len(sys.argv) != 2: + print('usage: ' + sys.argv[0] + ' /path/to/peers') + sys.exit(1) + +peersDir = sys.argv[1] + +def normalizeMac(mac): + mac = mac.lower() + normalized = '' + + n = 0 + + for c in mac: + if c != ':': + if n > 0 and n % 2 == 0: + normalized = normalized + ':' + normalized = normalized + c + n += 1 + + return normalized + +def toFilename(peer): + filename = '' + for field in ['name', 'mac', 'vpn', 'token']: + if peer.has_key(field): + filename = filename + peer[field] + filename = filename + '@' + + return filename[:-1] + +for filename in os.listdir(peersDir): + if len(filename) == 0 or filename[0] == '.': + continue + + absFilename = peersDir + '/' + filename + if os.path.isfile(absFilename): + peerFile = open(absFilename, 'r') + try: + peerLines = peerFile.readlines() + peer = {} + mac = None + for line in peerLines: + parts = line.split() + + if len(parts) > 0: + if parts[1] == 'Knotenname:': + peer['name'] = parts[2].lower() + elif parts[1] == 'MAC:': + peer['mac'] = normalizeMac(parts[2]) + elif parts[1] == 'Token:': + peer['token'] = parts[2].lower() + elif parts[0] == 'key': + peer['vpn'] = parts[1].split('"')[1].lower() + + newFilename = toFilename(peer) + + if filename != newFilename: + os.rename(absFilename, peersDir + '/' + newFilename) + + except Exception as e: + print('Error in %s, ignoring peer: %s' % (absFilename, e)); + finally: + peerFile.close() + diff --git a/package.json b/package.json index 25629ee..6742139 100644 --- a/package.json +++ b/package.json @@ -15,7 +15,7 @@ "grunt-contrib-compass": "~0.7.2", "grunt-contrib-concat": "~0.3.0", "grunt-contrib-connect": "~0.5.0", - "grunt-contrib-copy": "~0.4.1", + "grunt-contrib-copy": "~0.5.0", "grunt-contrib-cssmin": "~0.7.0", "grunt-contrib-htmlmin": "~0.1.3", "grunt-contrib-imagemin": "~0.3.0", diff --git a/server/main.js b/server/main.js old mode 100755 new mode 100644