diff --git a/uncloud/hack/hackcloud/mac-gen.py b/uncloud/hack/hackcloud/mac-gen.py index 9f23854..f2a5db0 100644 --- a/uncloud/hack/hackcloud/mac-gen.py +++ b/uncloud/hack/hackcloud/mac-gen.py @@ -26,29 +26,32 @@ import os.path import os import re -import cinv -from cinv import fsproperty - log = logging.getLogger(__name__) -class Error(cinv.Error): +class Error(Exception): pass class Mac(object): - def __init__(self): - self.base_dir = self.get_base_dir() + self.base_dir = "." - _prefix = fsproperty.FileStringProperty(lambda obj: os.path.join(obj.base_dir, "prefix")) - free = fsproperty.FileListProperty(lambda obj: os.path.join(obj.base_dir, "free")) - last = fsproperty.FileStringProperty(lambda obj: os.path.join(obj.base_dir, "last")) + self._prefix = "02:00" + + self.free = self.read_file("mac-free") + self.last = self.read_file("mac-last") + + def read_file(self, filename): + fname = os.path.join(self.base_dir, filename) + ret = [] - def _init_base_dir(self): try: - os.makedirs(self.base_dir, exist_ok=True) - except OSError as e: - raise Error(e) + with open(fname, "r") as fd: + ret = fd.readlines() + except Exception as e: + pass + + return ret @staticmethod def validate_mac(mac): @@ -146,26 +149,11 @@ class Mac(object): host.host_type = args.type @classmethod - def commandline_args(cls, parent_parser, parents): - """Add us to the parent parser and add all parents to our parsers""" + def commandline(cls): + pass - parser = {} - parser['sub'] = parent_parser.add_subparsers(title="Mac Commands") - parser['free-add'] = parser['sub'].add_parser('free-add', parents=parents) - parser['free-add'].add_argument('address', help='Address to add to free database') - parser['free-add'].set_defaults(func=cls.commandline_free_add) - - parser['free-list'] = parser['sub'].add_parser('free-list', parents=parents, - help="List free mac addresses") - parser['free-list'].set_defaults(func=cls.commandline_free_list) - - parser['generate'] = parser['sub'].add_parser('generate', parents=parents) - parser['generate'].set_defaults(func=cls.commandline_generate) - - parser['prefix-get'] = parser['sub'].add_parser('prefix-get', parents=parents) - parser['prefix-get'].set_defaults(func=cls.commandline_prefix_get) - - parser['prefix-set'] = parser['sub'].add_parser('prefix-set', parents=parents) - parser['prefix-set'].add_argument('prefix', help="3 Byte address prefix (f.i. '00:16:3e')") - parser['prefix-set'].set_defaults(func=cls.commandline_prefix_set) +if __name__ == '__main__': + m = Mac() + m.commandline() + print(m.free)