From 994ac950f56ee4dea0bab3de443f2968e339eb63 Mon Sep 17 00:00:00 2001 From: Josh Schmelzle Date: Thu, 25 May 2023 16:44:31 -0400 Subject: [PATCH] 0.2 work --- .gitignore | 1 + .pypirc | 3 +++ DEV.md | 13 ++++++++++ HISTORY.md | 7 +++++ MANIFEST.in | 2 ++ README.md | 59 ++++++++++++++++++++++++++---------------- pyproject.toml | 42 ++++++++++++++++++++++++++++++ randmac/__init__.py | 7 +++++ randmac/__main__.py | 4 +++ randmac/__version__.py | 6 +++-- randmac/helpers.py | 4 +++ randmac/randmac.py | 25 ++++++++++++------ tests/bench.py | 17 ++++++++---- tests/test_randmac.py | 59 +++++++++++++++++++----------------------- 14 files changed, 178 insertions(+), 71 deletions(-) create mode 100644 .pypirc create mode 100644 DEV.md create mode 100644 pyproject.toml diff --git a/.gitignore b/.gitignore index 47b3ba5..194f599 100644 --- a/.gitignore +++ b/.gitignore @@ -16,3 +16,4 @@ TODO.md dist .pytest_cache/ /.mypy_cache/ +venv/ \ No newline at end of file diff --git a/.pypirc b/.pypirc new file mode 100644 index 0000000..9ce485a --- /dev/null +++ b/.pypirc @@ -0,0 +1,3 @@ +[distutils] +index-servers = + pypi \ No newline at end of file diff --git a/DEV.md b/DEV.md new file mode 100644 index 0000000..eca96ea --- /dev/null +++ b/DEV.md @@ -0,0 +1,13 @@ +# DEV.md + +## Useful commands + +### bumpver + +```bash +bumpver show +bumpver show -vv +bumpver update --dry --minor --no-fetch +bumpver update --dry --minor +bumpver update --minor +``` \ No newline at end of file diff --git a/HISTORY.md b/HISTORY.md index 9c4bcb8..be3cea5 100644 --- a/HISTORY.md +++ b/HISTORY.md @@ -1,6 +1,13 @@ Release History =============== +0.0.2 (2023-05-23) +------------------ + +- Minor bug fixes and fix tests. +- Switch from setup.py to pyproject.toml. +- License change from MIT to BSD-3-Clause. + 0.0.1 (2019-02-27) ------------------ diff --git a/MANIFEST.in b/MANIFEST.in index d47ff5a..6ad0cb3 100644 --- a/MANIFEST.in +++ b/MANIFEST.in @@ -1 +1,3 @@ +# MANIFEST.in + include README.md LICENSE HISTORY.md diff --git a/README.md b/README.md index fe95b72..1d86bdf 100644 --- a/README.md +++ b/README.md @@ -1,44 +1,57 @@ randmac.py ========== -a utility that generates 12-digit mac addresses; either the NIC portion or full 12-digit MAC. +randmac is a utility that generates 12-digit mac addresses; either the NIC portion or full 12-digit MAC. -the optional `-f` argument will return a random 12-digit MAC address that can be identified by the locally administrated address (LAA) format. This means you will always see `x2`, `x6`, `xA`, or `xE` at the beginning of a MAC address generated by randmac. +The optional `-f` argument will return a random 12-digit MAC address that can be identified by the locally administrated address (LAA) format. This means you will always see `x2`, `x6`, `xA`, or `xE` at the beginning of a MAC address generated by randmac. ![](randmac20200913.gif) -# installation +# Installation -to install with pip: +To install with pip: -`pip install randmac` +`python3 -m pip install randmac` -# requirements +# Requirements -Python >3.2 required. +Python 3.2 or greater is required. -# mac address formats +# Supported MAC address formats -Supported MAC address formats: - - MM:MM:MM:SS:SS:SS - - MM-MM-MM-SS-SS-SS - - MM.MM.MM.SS.SS.SS - - MMMM.MMSS.SSSS - - MMMMMMSSSSSS +Supported formats: + + - MM:MM:MM:SS:SS:SS (colons) + - MM-MM-MM-SS-SS-SS (hyphens/dashes) + - MM.MM.MM.SS.SS.SS (dots/periods) + - MMMM.MMSS.SSSS (cisco style) + - MMMMMMSSSSSS (no separator) where `M` stands for the manufacturer or vendor, and `S` stands for the NIC specific portion. -# usage +# Usage + +``` +usage: randmac [-h] [-V] [-p] [mac] + +Generates MAC addresses. By default randmac will generate a 12-digit MAC address following the Locally Administrated Address (LAA) format. randmac only supports 12-digit (48-bit) MAC addresses. + +positional arguments: + mac mac address required for output format -you can `from randmac import RandMac` and use it like `RandMac()`. +options: + -h, --help show this help message and exit + -V, --version show program's version number and exit + -p, --partial randomizes the NIC portion of a MAC address +``` -if you wish to change the mac address format. provide a sample mac so `randmac` knows what the output format should be. +In your own code, you can `from randmac import RandMac` and use it like `RandMac()`. If you wish to change the mac address format, pass in a sample MAC address with the target format. This means you can `from randmac import RandMac` and use it like `RandMac("0000.0000.0000")` or `RandMac("ff-ff-ff-dd-dd-dd")` to specify the delimiter format. -you can `from randmac import RandMac` and use it like `RandMac("0000.0000.0000")`. +From a terminal (if the the console scripts entry point `randmac` is in your path and executable) you can type `randmac` and press enter to get a generate a new 12-digit LAA address, or `randmac 00:00:00:00:00:00 -p` to generate a MAC with the same OUI, but a different NIC portion. The result will look something like `00:00:00:60:cf:6e`. -from a terminal (if the the console scripts entry point `randmac` is in your path and executable) you can use `randmac` to get a generate a new 12-digit LAA address, or `randmac 00:00:00:00:00:00 -p` to generate a MAC with the same OUI, but a different NIC portion. +# Example usage -# example usage +From Python: ``` >>> from randmac import RandMac @@ -52,7 +65,7 @@ from a terminal (if the the console scripts entry point `randmac` is in your pat '06eb4584d1e3' ``` -or +From CLI: ``` > randmac @@ -61,6 +74,6 @@ fa:bf:7c:5d:65:3e 00-00-00-dd-5f-16 ``` -# license +# License -license can be found [here](https://github.com/joshschmelzle/randmac/blob/master/LICENSE). +The license for this project can be found [here](https://github.com/joshschmelzle/randmac/blob/master/LICENSE). diff --git a/pyproject.toml b/pyproject.toml new file mode 100644 index 0000000..db7c8a1 --- /dev/null +++ b/pyproject.toml @@ -0,0 +1,42 @@ +[build-system] +requires = ["setuptools==61.0.0", "wheel"] +build-backend = "setuptools.build_meta" + +[project] +name = "randmac" +description = "" +readme = "README.md" +authors = [{ name = "Josh Schmelzle", email = "josh@joshschmelzle.com"}] +license = { file = "LICENSE" } +classifiers = [ + "Natural Language :: English", + "Development Status :: 3 - Alpha", + "Programming Language :: Python :: 3.2", + "Intended Audience :: Developers", + "Intended Audience :: System Administrators", + "Topic :: Utilities", +] +keywords = ["randmac", "random mac", "random mac address"] + +[project.scripts] +randmac = "randmac=randmac.__main__:main" + + [project.optional-dependencies] + build = ["build", "twine"] + dev = ["black", "bumpver", "isort", "mypy", "pytest"] + +[tool.bumpver] +current_version = "0.1.0" +version_pattern = "MAJOR.MINOR.PATCH" +commit_message = "Bump version {old_version} -> {new_version}" +commit = true +tag = true +push = false + +[tool.bumpver.file_patterns] +"pyproject.toml" = [ + 'current_version = "{version}"', +] +"randmac/__version__.py" = [ + '__version__ = "{version}"' +] \ No newline at end of file diff --git a/randmac/__init__.py b/randmac/__init__.py index 05ad5b5..813e520 100644 --- a/randmac/__init__.py +++ b/randmac/__init__.py @@ -1 +1,8 @@ +# /usr/bin/env python3 +# -*- coding: utf-8 -*- +# +# _ _ _ _| _ _ _ _ +# | (_|| |(_|| | |(_|(_ +# + from randmac.randmac import RandMac diff --git a/randmac/__main__.py b/randmac/__main__.py index bdbd17d..c8c07e9 100644 --- a/randmac/__main__.py +++ b/randmac/__main__.py @@ -1,5 +1,9 @@ # /usr/bin/env python3 # -*- coding: utf-8 -*- +# +# _ _ _ _| _ _ _ _ +# | (_|| |(_|| | |(_|(_ +# import os import platform diff --git a/randmac/__version__.py b/randmac/__version__.py index fb39e84..ba5b9c6 100644 --- a/randmac/__version__.py +++ b/randmac/__version__.py @@ -1,3 +1,5 @@ +# /usr/bin/env python3 +# -*- coding: utf-8 -*- # # _ _ _ _| _ _ _ _ # | (_|| |(_|| | |(_|(_ @@ -6,8 +8,8 @@ __title__ = "randmac" __description__ = "a random 12-digit mac address generator" __url__ = "https://github.com/joshschmelzle/randmac" -__version__ = "0.1" +__version__ = "0.1.0" __author__ = "Josh Schmelzle" __author_email__ = "josh@joshschmelzle.com" -__license__ = "MIT" +__license__ = "BSD-3-Clause" __copyright__ = "Copyright Josh Schmelzle" diff --git a/randmac/helpers.py b/randmac/helpers.py index 39dd08e..06fd42c 100644 --- a/randmac/helpers.py +++ b/randmac/helpers.py @@ -1,5 +1,9 @@ # /usr/bin/env python3 # -*- coding: utf-8 -*- +# +# _ _ _ _| _ _ _ _ +# | (_|| |(_|| | |(_|(_ +# import argparse import textwrap diff --git a/randmac/randmac.py b/randmac/randmac.py index 1ff5828..e85a3b2 100755 --- a/randmac/randmac.py +++ b/randmac/randmac.py @@ -1,10 +1,14 @@ # /usr/bin/env python3 # -*- coding: utf-8 -*- +# +# _ _ _ _| _ _ _ _ +# | (_|| |(_|| | |(_|(_ +# """ -Randomizes the 6 digit NIC portion of the MAC address required for input. +Generates a 12-digit LAA MAC address -Optional argument will generate a 12-digit LAA. +Optional argument will generate 6 digit NIC portion of the MAC address. Supported MAC address formats: MM:MM:MM:SS:SS:SS @@ -14,8 +18,6 @@ """ import random -import sys -import os from enum import Enum @@ -56,6 +58,7 @@ class Format(Enum): def __init__(self, mac=None, generate_partial=None) -> None: if mac is None: mac = "11:11:11:11:11:11" + mac = mac.lower() trimmed = self._trim_separator(mac) _chars = [] for _char in trimmed: @@ -77,16 +80,22 @@ def __init__(self, mac=None, generate_partial=None) -> None: ) def __repr__(self): - return repr(self.mac) + return repr(str(self.mac)) def __str__(self): return str(self.mac) - + + def __getitem__(self, item): + return str(self.mac)[item] + + def __len__(self): + return len(str(self.mac)) + @staticmethod def _trim_separator(mac: str) -> str: """removes separator from MAC address""" - return mac.translate(str.maketrans("", "", ":-.")) - + return mac.replace(":","").replace("-","").replace(".","") + @staticmethod def _set_lettercase(string: str) -> str: """determines lettercase for MAC address""" diff --git a/tests/bench.py b/tests/bench.py index 05226ec..c718115 100755 --- a/tests/bench.py +++ b/tests/bench.py @@ -1,8 +1,15 @@ +# /usr/bin/env python3 +# -*- coding: utf-8 -*- +# +# _ _ _ _| _ _ _ _ +# | (_|| |(_|| | |(_|(_ +# + import time import sys sys.path.insert(0, "../randmac/") -import randmac +from randmac import RandMac def bench(): @@ -36,10 +43,10 @@ def test(): "00000000000f", ]: # print("input {}".format(mac)) - mac = randmac.nic_portion(mac) - # print("output nic {}".format(mac)) - mac = randmac.twelve_digit_mac(mac) - # print("output mac {}".format(mac)) + foo = RandMac(mac, generate_partial=True) + # print("output nic {}".format(foo)) + foo = RandMac(mac) + # print("output mac {}".format(foo)) # print("") diff --git a/tests/test_randmac.py b/tests/test_randmac.py index 31b99ce..60e6ac8 100644 --- a/tests/test_randmac.py +++ b/tests/test_randmac.py @@ -1,44 +1,37 @@ -import pytest +# /usr/bin/env python3 +# -*- coding: utf-8 -*- +# +# _ _ _ _| _ _ _ _ +# | (_|| |(_|| | |(_|(_ +# + import sys sys.path.insert(0, "../randmac/") -from randmac import randmac +from randmac import RandMac -class TestRandMac(object): - def testrandomnic(self): - assert len(randmac.nic_portion("00.00.00.00.00.00")) == 17 - assert len(randmac.nic_portion("0000.0000.0000")) == 14 - assert "123456" in randmac.nic_portion("123456AABBCC") +class Test_RandMac(object): + def test_partial(self): + assert len(RandMac("000000000000", generate_partial=True)) == 12 + assert len(RandMac("00.00.00.00.00.00", generate_partial=True)) == 17 + assert len(RandMac("00-00-00-00-00-00", generate_partial=True)) == 17 + assert len(RandMac("00:00:00:00:00:00", generate_partial=True)) == 17 + assert len(RandMac("0000.0000.0000", generate_partial=True)) == 14 + assert "123456" in str(RandMac("123456AABBCC", generate_partial=True)) + assert "12:34:56" in str(RandMac("12:34:56:AA:BB:CC", generate_partial=True)) - def testmac(self): - assert randmac.twelve_digit_mac("00:00:00:00:00:00")[1].lower() in [ - "2", - "6", - "a", - "e", - ] - assert randmac.twelve_digit_mac("00.00.00.00.00.00")[1].lower() in [ - "2", - "6", - "a", - "e", - ] - assert randmac.twelve_digit_mac("00-00-00-00-00-00")[1].lower() in [ - "2", - "6", - "a", - "e", - ] - assert randmac.twelve_digit_mac("0000.0000.0000")[1].lower() in [ - "2", - "6", - "a", - "e", - ] - assert randmac.twelve_digit_mac("000000000000")[1].lower() in [ + def test_mac(self): + laa_hex = [ "2", "6", "a", "e", ] + assert "123456" not in RandMac("123456AABBCC") + assert "12:34:56" not in RandMac("12:34:56:AA:BB:CC") + assert RandMac("00:00:00:00:00:00")[1].lower() in laa_hex + assert RandMac("00.00.00.00.00.00")[1].lower() in laa_hex + assert RandMac("00-00-00-00-00-00")[1].lower() in laa_hex + assert RandMac("0000.0000.0000")[1].lower() in laa_hex + assert RandMac("000000000000")[1].lower() in laa_hex