Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion gearbox/command.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
from .template import GearBoxTemplate


class Command(object):
class Command:
deprecated = False

def __init__(self, app, app_args, cmd_name=None):
Expand Down
16 changes: 3 additions & 13 deletions gearbox/commandmanager.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
LOG = logging.getLogger(__name__)


class EntryPointWrapper(object):
class EntryPointWrapper:
"""Wrap up a command class already imported to make it look like a plugin."""

def __init__(self, name, command_class):
Expand All @@ -21,7 +21,7 @@ def load(self, require=False):
return self.command_class


class CommandManager(object):
class CommandManager:
"""Discovers commands and handles lookup based on argv data.

:param namespace: String containing the setuptools entrypoint namespace
Expand All @@ -35,26 +35,16 @@ def __init__(self, namespace, convert_underscores=True):
self.commands = {}
self.namespace = namespace
self.convert_underscores = convert_underscores
self._load_commands()

def _load_commands(self):
# NOTE(jamielennox): kept for compatability.
self.load_commands(self.namespace)

def load_commands(self, namespace):
"""Load all the commands from an entrypoint"""
entry_points = importlib.metadata.entry_points()
if hasattr(entry_points, "select"):
entry_points = entry_points.select(group=namespace)
else:
entry_points = entry_points.get(namespace, [])
for ep in entry_points:
for ep in importlib.metadata.entry_points().select(group=namespace):
LOG.debug("found command %r", ep.name)
cmd_name = (
ep.name.replace("_", " ") if self.convert_underscores else ep.name
)
self.commands[cmd_name] = ep
return

def __iter__(self):
return iter(self.commands.items())
Expand Down
4 changes: 1 addition & 3 deletions gearbox/commands/basic_package/command.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
from __future__ import print_function

import re

from gearbox.command import TemplateCommand
Expand All @@ -12,7 +10,7 @@ def get_description(self):
return "Creates a basic python package"

def get_parser(self, prog_name):
parser = super(MakePackageCommand, self).get_parser(prog_name)
parser = super().get_parser(prog_name)

parser.add_argument(
"-n",
Expand Down
10 changes: 4 additions & 6 deletions gearbox/commands/basic_package/template/README.rst_tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,10 @@ About {{package}}
Installing
-------------------------------

{{package}} can be installed from pypi::

easy_install {{package}}

or::
{{package}} can be installed from PyPI::

pip install {{package}}

should just work for most of the users
For local development (editable install)::

pip install -e .
14 changes: 14 additions & 0 deletions gearbox/commands/basic_package/template/pyproject.toml_tmpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
[build-system]
requires = ["setuptools>=61"]
build-backend = "setuptools.build_meta"

[project]
name = "{{project}}"
version = "{{version}}"
description = "{{description or ''}}"
readme = "README.rst"
requires-python = ">=3.10"
authors = [{name = "{{author or ''}}", email = "{{author_email or ''}}"}]
license = {text = "{{license_name or ''}}"}
keywords = ["{{keywords or ''}}"]
urls = {Homepage = "{{url or ''}}"}
Empty file.
31 changes: 0 additions & 31 deletions gearbox/commands/basic_package/template/setup.py_tmpl

This file was deleted.

2 changes: 1 addition & 1 deletion gearbox/commands/help.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ class HelpCommand(Command):
"""print detailed help for another command"""

def get_parser(self, prog_name):
parser = super(HelpCommand, self).get_parser(prog_name)
parser = super().get_parser(prog_name)
parser.add_argument(
"cmd",
nargs="*",
Expand Down
48 changes: 23 additions & 25 deletions gearbox/commands/patch.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
from __future__ import print_function

import fnmatch
import os
import re
Expand Down Expand Up @@ -30,7 +28,7 @@ def get_description(self):
"""

def get_parser(self, prog_name):
parser = super(PatchCommand, self).get_parser(prog_name)
parser = super().get_parser(prog_name)
parser.formatter_class = RawDescriptionHelpFormatter

parser.add_argument(
Expand Down Expand Up @@ -85,28 +83,6 @@ def get_parser(self, prog_name):

return parser

def _walk_recursive(self):
for root, dirnames, filenames in os.walk(os.getcwd()):
for filename in filenames:
yield os.path.join(root, filename)

def _walk_flat(self):
root = os.getcwd()
for filename in os.listdir(root):
yield os.path.join(root, filename)

def _replace_regex(self, line, text, replacement):
return re.sub(text, replacement, line)

def _replace_plain(self, line, text, replacement):
return line.replace(text, replacement)

def _match_regex(self, line, text):
return re.search(text, line) is not None

def _match_plain(self, line, text):
return text in line

def take_action(self, opts):
walk = self._walk_flat
if opts.recursive:
Expand Down Expand Up @@ -158,3 +134,25 @@ def take_action(self, opts):
if matches:
with open(filepath, "w") as f:
f.writelines(lines)

def _walk_recursive(self):
for root, dirnames, filenames in os.walk(os.getcwd()):
for filename in filenames:
yield os.path.join(root, filename)

def _walk_flat(self):
root = os.getcwd()
for filename in os.listdir(root):
yield os.path.join(root, filename)

def _replace_regex(self, line, text, replacement):
return re.sub(text, replacement, line)

def _replace_plain(self, line, text, replacement):
return line.replace(text, replacement)

def _match_regex(self, line, text):
return re.search(text, line) is not None

def _match_plain(self, line, text):
return text in line
24 changes: 11 additions & 13 deletions gearbox/commands/scaffold.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
from __future__ import print_function

import os
from argparse import RawDescriptionHelpFormatter

Expand All @@ -23,7 +21,7 @@ def get_description(self):
"""

def get_parser(self, prog_name):
parser = super(ScaffoldCommand, self).get_parser(prog_name)
parser = super().get_parser(prog_name)

parser.formatter_class = RawDescriptionHelpFormatter

Expand Down Expand Up @@ -67,16 +65,6 @@ def get_parser(self, prog_name):

return parser

def _lookup(self, template, where):
template_filename = None
for root, __, files in os.walk(where):
for f in files:
fname, fext = os.path.splitext(f)
if fext == ".template" and os.path.splitext(fname)[0] == template:
template_filename = os.path.join(root, f)
break
return template_filename

def take_action(self, opts):
for template in opts.scaffold_name:
template_filename = None
Expand Down Expand Up @@ -144,3 +132,13 @@ def take_action(self, opts):

with open(output_path, "w") as of:
of.write(text)

def _lookup(self, template, where):
template_filename = None
for root, __, files in os.walk(where):
for f in files:
fname, fext = os.path.splitext(f)
if fext == ".template" and os.path.splitext(fname)[0] == template:
template_filename = os.path.join(root, f)
break
return template_filename
48 changes: 9 additions & 39 deletions gearbox/commands/serve.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,9 @@
# lib/site.py

import atexit
import ctypes
import errno
import logging
import os
import platform
import re
import subprocess
import sys
Expand All @@ -30,17 +28,7 @@

MAXFD = 1024

if platform.system() == "Windows" and not hasattr(os, "kill"): # pragma: no cover
# py 2.6 on windows
def kill(pid, sig=None):
"""kill function for Win32"""
# signal is ignored, semibogus raise message
kernel32 = ctypes.windll.kernel32
handle = kernel32.OpenProcess(1, 0, pid)
if 0 == kernel32.TerminateProcess(handle, 0):
raise OSError("No such process %s" % pid)
else:
kill = os.kill
kill = os.kill


class DaemonizeException(Exception):
Expand All @@ -55,7 +43,7 @@ class ServeCommand(Command):
possible_subcommands = ("start", "stop", "restart", "status")

def get_parser(self, prog_name):
parser = super(ServeCommand, self).get_parser(prog_name)
parser = super().get_parser(prog_name)

parser.add_argument(
"-c",
Expand Down Expand Up @@ -512,7 +500,7 @@ def restart_with_monitor(self): # pragma: no cover
if self.verbose > 0:
self.out("Starting subprocess with angel")

while 1:
while True:
args = self.get_fixed_argv()
new_environ = os.environ.copy()
new_environ[self._monitor_environ_key] = "true"
Expand Down Expand Up @@ -580,7 +568,7 @@ def change_user_group(self, user, group): # pragma: no cover
os.setuid(uid)


class LazyWriter(object):
class LazyWriter:
"""
File-like object that opens a file lazily when it is first written
to.
Expand Down Expand Up @@ -718,10 +706,7 @@ def get_request(self):
server_class = SecureWSGIServer

if threaded:
try:
from socketserver import ThreadingMixIn
except ImportError:
from SocketServer import ThreadingMixIn
from socketserver import ThreadingMixIn

class GearboxWSGIServer(ThreadingMixIn, server_class):
pass
Expand All @@ -747,12 +732,8 @@ class GearboxWSGIServer(server_class):
# For paste.deploy server instantiation (egg:gearbox#gevent)
def gevent_server_factory(global_config, **kw):
from gevent import reinit

try:
from gevent.pywsgi import WSGIServer
except ImportError:
from gevent.wsgi import WSGIServer
from gevent.monkey import patch_all
from gevent.pywsgi import WSGIServer

reinit()
patch_all(dns=False)
Expand Down Expand Up @@ -858,27 +839,16 @@ def cherrypy_server_runner(
if var is not None:
kwargs[var_name] = int(var)

server = None
try:
# Try to import from newer CherryPy releases.
import cheroot.wsgi as wsgiserver

server = wsgiserver.Server(bind_addr, app, server_name=server_name, **kwargs)
except ImportError:
# Nope. Try to import from older CherryPy releases.
# We might just take another ImportError here. Oh well.
from cherrypy import wsgiserver
import cheroot.wsgi as wsgiserver

server = wsgiserver.CherryPyWSGIServer(
bind_addr, app, server_name=server_name, **kwargs
)
server = wsgiserver.Server(bind_addr, app, server_name=server_name, **kwargs)

server.ssl_certificate = server.ssl_private_key = ssl_pem
if protocol_version:
server.protocol = protocol_version

try:
protocol = is_ssl and "https" or "http"
protocol = "https" if is_ssl else "http"
if host == "0.0.0.0":
print(
"serving on 0.0.0.0:%s view at %s://127.0.0.1:%s"
Expand Down
Loading
Loading