Skip to content

Commit

Permalink
Zip Package (#273)
Browse files Browse the repository at this point in the history
* wip

* working

* extra zip

* fix

* tests working

---------

Co-authored-by: Sean Shookman <seanshookman@Seans-MacBook-Pro.local>
Co-authored-by: root <root@ip-172-28-197-111.cars.com>
  • Loading branch information
3 people authored Nov 27, 2024
1 parent 1020d8f commit 9532b09
Show file tree
Hide file tree
Showing 6 changed files with 136 additions and 2 deletions.
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,14 @@ Documenting All Changes to the Skelebot Project

---

## v2.1.0
#### Added
- **Package Component** | Added `package` component to allow easy zipping of codebase

---

## v2.0.1
#### Merged: 2024-06-27
#### Changed
- **Base Images** | Added `git` to all the base images.
- **CI/CD** | Upload base images to Docker Hub via GitHub Action whenever there's an update.
Expand Down
2 changes: 1 addition & 1 deletion VERSION
Original file line number Diff line number Diff line change
@@ -1 +1 @@
2.0.1
2.1.0
2 changes: 2 additions & 0 deletions skelebot/components/componentFactory.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
from ..objects.component import Activation
from ..common import PLUGINS_HOME, PLUGINS_QUARANTINE
from .plugin import Plugin
from .package import Package
from .jupyter import Jupyter
from .kerberos import Kerberos
from .bump import Bump
Expand Down Expand Up @@ -35,6 +36,7 @@ def __init__(self):

self.COMPONENTS = {
Plugin.__name__.lower(): Plugin,
Package.__name__.lower(): Package,
Jupyter.__name__.lower(): Jupyter,
Kerberos.__name__.lower(): Kerberos,
Bump.__name__.lower(): Bump,
Expand Down
55 changes: 55 additions & 0 deletions skelebot/components/package.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
"""Package Component"""

from subprocess import call
from schema import Schema, And, Optional
from ..objects.component import Activation, Component

HELP_TEMPLATE = "Package the codebase into a single zip file ({path})"
COMMAND_TEMPLATE = "zip -r {path} .{ignores}"

class Package(Component):
"""
Package Class
Provides the ability to package the codebase into a zip file artifact for easy distribution
"""

activation = Activation.PROJECT
commands = ["package"]

schema = Schema({
'path': And(str, error='Package \'path\' must be a String'),
Optional('ignores'): And(list, error='Package \'ignores\' must be a List')
}, ignore_extra_keys=True)

path = None
ignores = None

def __init__(self, path=None, ignores=None):
"""Initialize the class with simple default values for path and ignores"""

self.path = path
self.ignores = ignores

def addParsers(self, subparsers):
"""
SkeleParser Hook
Adds a parser for the package command that zips up the codebase
"""

helpMessage = HELP_TEMPLATE.format(path=self.path)
subparsers.add_parser("package", help=helpMessage)
return subparsers

def execute(self, config, args, host=None):
"""
Execution Hook
Executed when the package command is provided it zips the codebase into a single file
while ingoring a list of folders and files in the process
"""

ignores = f" -x {' '.join(self.ignores)}" if (self.ignores is not None) else ""
command = COMMAND_TEMPLATE.format(path=self.path, ignores=ignores)
return call(command, shell=True)
70 changes: 70 additions & 0 deletions test/test_components_package.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
import copy
import argparse
import unittest
from unittest import mock

from schema import SchemaError

import skelebot as sb

class TestPlugin(unittest.TestCase):

package_yaml = {
"path": "test.zip",
"ignores": ["one.txt", "two.txt"]
}

def test_addParsers(self):
package = sb.components.package.Package()

parser = argparse.ArgumentParser(formatter_class=argparse.RawTextHelpFormatter)
subparsers = parser.add_subparsers(dest="job")
subparsers = package.addParsers(subparsers)

self.assertNotEqual(subparsers.choices["package"], None)

@mock.patch('skelebot.components.package.call')
def test_execute(self, mock_call):
mock_call.return_value = 0

config = sb.objects.config.Config()
args = argparse.Namespace()

package = sb.components.package.Package(path="test.zip")
package.execute(config, args)

mock_call.assert_called_once_with("zip -r test.zip .", shell=True)

@mock.patch('skelebot.components.package.call')
def test_execute_ignores(self, mock_call):
mock_call.return_value = 0

config = sb.objects.config.Config()
args = argparse.Namespace()

package = sb.components.package.Package(path="test.zip", ignores=["folder/**\*", "file.txt"])
package.execute(config, args)

mock_call.assert_called_once_with("zip -r test.zip . -x folder/**\* file.txt", shell=True)

def test_validate_valid(self):
try:
sb.components.package.Package.validate(self.package_yaml)
except:
self.fail("Validation Raised Exception Unexpectedly")

def validate_error(self, attr, reset, expected):
package_yaml = copy.deepcopy(self.package_yaml)
package_yaml[attr] = reset

try:
sb.components.package.Package.validate(package_yaml)
except SchemaError as error:
self.assertEqual(error.code, "Package '{attr}' must be a{expected}".format(attr=attr, expected=expected))

def test_invalid(self):
self.validate_error('path', 123, ' String')
self.validate_error('ignores', 123, ' List')

if __name__ == '__main__':
unittest.main()
2 changes: 1 addition & 1 deletion test/test_systems_generators_yaml.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ def validateYaml(self, config, isTestEnv=False):
self.assertEqual(component.port, 1128 if isTestEnv else 1127)
self.assertEqual(component.folder, "notebooks/")

expectedComponents = ["Registry", "Plugin", "Jupyter", "Bump", "Prime", "Environments", "Dexec", "AddNumbers"]
expectedComponents = ["Registry", "Plugin", "Package", "Jupyter", "Bump", "Prime", "Environments", "Dexec", "AddNumbers"]
if isTestEnv:
expectedComponents.append("Repository")
self.assertTrue(all(elem in components for elem in expectedComponents))
Expand Down

0 comments on commit 9532b09

Please sign in to comment.