Skip to content

Commit

Permalink
fix: read python path insert (#7)
Browse files Browse the repository at this point in the history
* fix: read python path insert

* tmp

* Update mypy config, README.md and add extra logging

---------

Co-authored-by: Attila Gombos <[email protected]>
  • Loading branch information
effective-range and AttilaGombosER authored Aug 6, 2024
1 parent 31f895d commit dd6e772
Show file tree
Hide file tree
Showing 13 changed files with 393 additions and 172 deletions.
1 change: 1 addition & 0 deletions .devcontainer
Submodule .devcontainer added at 190f80
3 changes: 3 additions & 0 deletions .gitmodules
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[submodule ".devcontainer"]
path = .devcontainer
url = https://github.com/EffectiveRange/devcontainer-defs
5 changes: 3 additions & 2 deletions python/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,8 @@ $ sudo gem install -N fpm
### dh-virtualenv

```bash
sudo apt-get install debhelper devscripts equivs dh-virtualenv dh-python python3-virtualenv python3-all python3-stdeb
$ sudo apt-get install debhelper devscripts equivs dh-virtualenv dh-python python3-virtualenv python3-all
$ pip install stdeb
```

## Configuration
Expand Down Expand Up @@ -110,7 +111,7 @@ $ ./pack_python tests/test-project --scripts "wheel fpm-deb"

- `wheel` - Create binary wheel package
- `fpm-deb` - Create debian .deb package using [FPM](https://fpm.readthedocs.io/en/latest/index.html)
- `dh-virtualenv` - Create debian .deb package using [dh-virtualenv](https://dh-virtualenv.readthedocs.io/en/latest/)
- `dh-virtualenv` - Create debian .deb package using [dh-virtualenv](https://pack_dh-virtualenv.readthedocs.io/en/latest/)
and [stdeb](https://github.com/astraw/stdeb)

### wheel
Expand Down
File renamed without changes.
73 changes: 46 additions & 27 deletions python/dh-virtualenv → python/pack_dh-virtualenv
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,14 @@ import glob
import re
import shutil
from argparse import ArgumentParser, ArgumentDefaultsHelpFormatter, Namespace
from os.path import abspath
from os.path import abspath, dirname
from typing import Generator

from common import check_workspace, run_command, get_absolute_path
import sys

sys.path.insert(0, dirname(abspath(__file__)))

from pack_common import check_workspace, run_command, get_absolute_path


def main() -> None:
Expand All @@ -21,48 +25,55 @@ def main() -> None:

check_workspace(workspace_dir)

output_dir = f'{workspace_dir}/dist'
output_dir = f"{workspace_dir}/dist"

if arguments.output_dir:
output_dir = abspath(arguments.output_dir)

results = _create_sources(arguments, workspace_dir, output_dir)

build_dir = f'{output_dir}/{next(results)}'
build_dir = f"{output_dir}/{next(results)}"

results = _build_package(arguments, workspace_dir, build_dir)

for result in results:
print(f'{output_dir}/{result}')
print(f"{output_dir}/{result}")


def _create_sources(arguments: Namespace, workspace_dir: str, output_dir: str) -> Generator[str, None, None]:
def _create_sources(
arguments: Namespace, workspace_dir: str, output_dir: str
) -> Generator[str, None, None]:
package_name = _extract_package_name(workspace_dir)

command_arguments = [
'--package3', package_name,
'--dist-dir', output_dir,
'--with-dh-virtualenv',
'--compat', '10',
"--package3",
package_name,
"--dist-dir",
output_dir,
"--with-dh-virtualenv",
"--compat",
"10",
]

if arguments.arguments:
command_arguments.extend(arguments.arguments.split())

if arguments.service_file:
command_arguments.append('--with-dh-systemd')
command_arguments.append("--with-dh-systemd")

command = [
arguments.python_bin, 'setup.py',
'--command-packages=stdeb.command',
'sdist_dsc', *command_arguments,
arguments.python_bin,
"setup.py",
"--command-packages=stdeb.command",
"sdist_dsc",
*command_arguments,
]

return run_command(workspace_dir, command, r'copying setup.py -> (.+)')
return run_command(workspace_dir, command, r"copying setup.py -> (.+)")


def _extract_package_name(workspace_dir: str) -> str:
with open(f'{workspace_dir}/setup.py', 'r') as file:
with open(f"{workspace_dir}/setup.py", "r") as file:
setup_code = file.read()

pattern = r'name\s*=\s*[\'"]([^\'"]+)[\'"]'
Expand All @@ -72,11 +83,13 @@ def _extract_package_name(workspace_dir: str) -> str:
if match:
return match.group(1)
else:
return workspace_dir.split('/')[-1]
return workspace_dir.split("/")[-1]


def _build_package(arguments: Namespace, workspace_dir: str, build_dir: str) -> Generator[str, None, None]:
debian_dir = f'{build_dir}/debian'
def _build_package(
arguments: Namespace, workspace_dir: str, build_dir: str
) -> Generator[str, None, None]:
debian_dir = f"{build_dir}/debian"

if arguments.service_file:
service_file = get_absolute_path(arguments.service_file, workspace_dir)
Expand All @@ -88,21 +101,27 @@ def _build_package(arguments: Namespace, workspace_dir: str, build_dir: str) ->
for file in glob.glob(extra_files):
shutil.copy(file, debian_dir)

command = ['dpkg-buildpackage', '-us', '-uc', '-ui', '-b']
command = ["dpkg-buildpackage", "-us", "-uc", "-ui", "-b"]

return run_command(build_dir, command, r".*'\.\./(.+\.deb)'")


def _get_arguments() -> Namespace:
parser = ArgumentParser(formatter_class=ArgumentDefaultsHelpFormatter)
parser.add_argument('-a', '--arguments', help='extra arguments passed to stdeb')
parser.add_argument('-p', '--python-bin', help='python executable to use', default='python3')
parser.add_argument('-s', '--service-file', help='service unit file path')
parser.add_argument('-e', '--extra-files', help='add extra files into debian folder before build')
parser.add_argument('-o', '--output-dir', help='package output directory')
parser.add_argument('workspace_dir', help='workspace directory where setup.py is located')
parser.add_argument("-a", "--arguments", help="extra arguments passed to stdeb")
parser.add_argument(
"-p", "--python-bin", help="python executable to use", default="python3"
)
parser.add_argument("-s", "--service-file", help="service unit file path")
parser.add_argument(
"-e", "--extra-files", help="add extra files into debian folder before build"
)
parser.add_argument("-o", "--output-dir", help="package output directory")
parser.add_argument(
"workspace_dir", help="workspace directory where setup.py is located"
)
return parser.parse_args()


if __name__ == '__main__':
if __name__ == "__main__":
main()
45 changes: 29 additions & 16 deletions python/fpm-deb → python/pack_fpm-deb
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,13 @@

import os
from argparse import ArgumentParser, ArgumentDefaultsHelpFormatter, Namespace
from os.path import abspath
from os.path import abspath, dirname

from common import check_workspace, run_command, get_absolute_path
import sys

sys.path.insert(0, dirname(abspath(__file__)))

from pack_common import check_workspace, run_command, get_absolute_path


def main() -> None:
Expand All @@ -19,26 +23,31 @@ def main() -> None:
check_workspace(workspace_dir)

fpm_arguments = [
'-s', 'python',
'-t', 'deb',
'--python-bin', arguments.python_bin,
'--python-package-name-prefix', 'python3',
'--log', 'info',
'-f',
"-s",
"python",
"-t",
"deb",
"--python-bin",
arguments.python_bin,
"--python-package-name-prefix",
"python3",
"--log",
"info",
"-f",
]

if arguments.arguments:
fpm_arguments.extend(arguments.arguments.split())

output_dir = f'{workspace_dir}/dist'
output_dir = f"{workspace_dir}/dist"

if arguments.output_dir:
output_dir = abspath(arguments.output_dir)

os.makedirs(output_dir, exist_ok=True)
fpm_arguments.extend(['--package', output_dir])
fpm_arguments.extend(["--package", output_dir])

command = ['fpm', *fpm_arguments, 'setup.py']
command = ["fpm", *fpm_arguments, "setup.py"]

results = run_command(workspace_dir, command, r'.*"(.+\.deb)"')

Expand All @@ -49,12 +58,16 @@ def main() -> None:

def _get_arguments() -> Namespace:
parser = ArgumentParser(formatter_class=ArgumentDefaultsHelpFormatter)
parser.add_argument('-a', '--arguments', help='extra arguments passed to fpm')
parser.add_argument('-p', '--python-bin', help='python executable to use', default='python3')
parser.add_argument('-o', '--output-dir', help='package output directory')
parser.add_argument('workspace_dir', help='workspace directory where setup.py is located')
parser.add_argument("-a", "--arguments", help="extra arguments passed to fpm")
parser.add_argument(
"-p", "--python-bin", help="python executable to use", default="python3"
)
parser.add_argument("-o", "--output-dir", help="package output directory")
parser.add_argument(
"workspace_dir", help="workspace directory where setup.py is located"
)
return parser.parse_args()


if __name__ == '__main__':
if __name__ == "__main__":
main()
69 changes: 47 additions & 22 deletions python/pack_python
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,13 @@ from argparse import (
from configparser import ConfigParser
from os.path import exists, dirname, abspath

from common import check_workspace, get_absolute_path
import sys

DEFAULT_PACKAGING = 'wheel'
sys.path.insert(0, dirname(abspath(__file__)))

from pack_common import check_workspace, get_absolute_path

DEFAULT_PACKAGING = "wheel"


def main() -> None:
Expand All @@ -36,77 +40,98 @@ def main() -> None:
else:
configuration, packaging = _parse_config(arguments, config_file)

scripts_dir = f'{abspath(dirname(__file__))}'
scripts_dir = f"{abspath(dirname(__file__))}"

for script in packaging:
script_file = f'{scripts_dir}/{script}'
script_file = f"{scripts_dir}/pack_{script}"

if exists(script_file):
print(f"Running packaging script for {script}: {script_file}", file=sys.stderr)

command = [script_file, workspace_dir]

if arg_string := configuration.get(script):
command.extend(_split_arguments(arg_string))

_run_script(arguments, command)
else:
print(f"Packaging script for {script} not found: {script_file}", file=sys.stderr)


def _run_script(arguments: Namespace, command: list[str]) -> None:
if arguments.python_bin:
command.extend(['-p', arguments.python_bin])
command.extend(["-p", arguments.python_bin])

if arguments.output_dir:
command.extend(['-o', arguments.output_dir])
command.extend(["-o", arguments.output_dir])

result = subprocess.run(command, text=True, stdout=subprocess.PIPE)

if result.returncode:
exit(result.returncode)

if result.stdout:
print(result.stdout.rstrip('\n'))
print(result.stdout.rstrip("\n"))


def _parse_config(arguments: Namespace, config_file: str) -> tuple[dict[str, str], list[str]]:
def _parse_config(
arguments: Namespace, config_file: str
) -> tuple[dict[str, str], list[str]]:
configuration = {}
packaging = [DEFAULT_PACKAGING]

if exists(config_file):
parser = ConfigParser()
parser.read(config_file)

if parser.has_section('pack-python'):
configuration = dict(parser['pack-python'])
if parser.has_section("pack-python"):
configuration = dict(parser["pack-python"])

packaging_options = configuration.get('packaging', '').split()
packaging_options = configuration.get("packaging", "").split()

if arguments.all:
packaging = packaging_options
else:
default = configuration.get('default', DEFAULT_PACKAGING)
default = configuration.get("default", DEFAULT_PACKAGING)
packaging = [default]

return configuration, packaging


def _get_arguments() -> Namespace:
parser = ArgumentParser(formatter_class=ArgumentDefaultsHelpFormatter)
parser.add_argument('-s', '--scripts', help='space separated packaging scripts to run')
parser.add_argument('-a', '--all', help='run all configured packaging scripts',
action=BooleanOptionalAction, default=False)
parser.add_argument('-c', '--config-file', help='config file path relative to workspace directory',
default='setup.cfg')
parser.add_argument('-p', '--python-bin', help='python executable to use', default='python3')
parser.add_argument('-o', '--output-dir', help='package output directory')
parser.add_argument('workspace_dir', help='workspace directory where setup.py is located')
parser.add_argument(
"-s", "--scripts", help="space separated packaging scripts to run"
)
parser.add_argument(
"-a",
"--all",
help="run all configured packaging scripts",
action=BooleanOptionalAction,
default=False,
)
parser.add_argument(
"-c",
"--config-file",
help="config file path relative to workspace directory",
default="setup.cfg",
)
parser.add_argument(
"-p", "--python-bin", help="python executable to use", default="python3"
)
parser.add_argument("-o", "--output-dir", help="package output directory")
parser.add_argument(
"workspace_dir", help="workspace directory where setup.py is located"
)
return parser.parse_args()


def _split_arguments(arg_string: str) -> list[str]:
# Split on spaces, but allow spaces inside double quotes
pattern = r'\"(.*?)\"|(\S+)'
pattern = r"\"(.*?)\"|(\S+)"
matches = re.findall(pattern, arg_string)
return [match[0] if match[0] else match[1] for match in matches]


if __name__ == '__main__':
if __name__ == "__main__":
main()
Loading

0 comments on commit dd6e772

Please sign in to comment.