Skip to content

Commit

Permalink
qipkg: refactor "compile-package" to "release-package"
Browse files Browse the repository at this point in the history
Change-Id: Ic0cdde3230357e07befdd612621fe00fb1d6123f
Reviewed-on: http://gerrit.aldebaran.lan/59189
Reviewed-by: gschweitzer <[email protected]>
Tested-by: gschweitzer <[email protected]>
  • Loading branch information
gschweitzer-aldebaran authored and gschweitzer committed Jun 29, 2015
1 parent e1b5946 commit e059c16
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 46 deletions.
23 changes: 23 additions & 0 deletions python/qipkg/actions/release_package.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# Copyright (c) 2015 Aldebaran Robotics. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the COPYING file.
""" Update a package by replacing python source files by their byte-code equivalent """

import os

import qisys.parsers
import qipkg.release


def configure_parser(parser):
qisys.parsers.default_parser(parser)
parser.add_argument("pkg_path")
parser.add_argument("-o", "--output")


def do(args):
pkg_path = args.pkg_path
output_path = args.output
if not output_path:
output_path = os.path.join(os.getcwd(), os.path.basename(pkg_path))
return qipkg.release.make_release(pkg_path, output_path)
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
""" Update a package by replacing python source files by their byte-code equivalent """

import os
import zipfile
import py_compile
import sys
import re
Expand All @@ -14,24 +13,11 @@
import qisys.parsers


def configure_parser(parser):
qisys.parsers.default_parser(parser)
parser.add_argument("pkg_path")
parser.add_argument("-o", "--output")


def do(args):
pkg_path = args.pkg_path
output_path = args.output

to_add = list()

def make_release(pkg_path, output_path):
# Extract the package to a temp path
with qisys.sh.TempDir() as tmp_path:
archive = zipfile.ZipFile(pkg_path)
qisys.archive.extract(pkg_path, tmp_path,
algo="zip", strict_mode=False)
archive.close()

# Compile python files, remove python sources
_compile_python_files(tmp_path)
Expand All @@ -40,25 +26,7 @@ def do(args):
_update_python_services(tmp_path)

# add everything from the temp path
ui.info(ui.bold, "-> Compressing package ...")
for root, _, filenames in os.walk(tmp_path):
for filename in filenames:
full_path = os.path.join(root, filename)
rel_path = os.path.relpath(full_path, tmp_path)
to_add.append((full_path, rel_path))

if not output_path:
output_path = os.path.join(os.getcwd(), os.path.basename(pkg_path))

archive = zipfile.ZipFile(output_path, "w", zipfile.ZIP_DEFLATED)
for i, (full_path, rel_path) in enumerate(to_add):
n = len(to_add)
if sys.stdout.isatty():
percent = float(i) / n * 100
sys.stdout.write("Done: %.0f%%\r" % percent)
sys.stdout.flush()
archive.write(full_path, rel_path)
archive.close()
qisys.archive.compress(tmp_path, output=output_path, flat=True)

ui.info(ui.green, "Package compiled to", ui.reset,
ui.bold, output_path)
Expand All @@ -85,23 +53,16 @@ def _update_python_services(package_path):
root = tree.getroot()
services = root.findall("services/service")
for service in services:
execStart = service.attrib['execStart']
execStart = re.sub(r".py\b", r".pyc", execStart)
exec_start = service.attrib["execStart"]
exec_start = re.sub(r".py\b", r".pyc", exec_start)

match_object = service_file_regexp.match(execStart)
match_object = service_file_regexp.match(exec_start)
if match_object is None:
continue
service_file_path = match_object.group(1)

# update execStart command only if it points on a file inside the package
if os.path.exists(os.path.join(package_path, service_file_path)):
service.set('execStart', execStart)

qisys.qixml.write(tree, manifest_path, encoding="utf-8")

service.set("execStart", exec_start)

def _print_progress_message(value, max_value, prefix):
if sys.stdout.isatty():
percent = float(value) / max_value * 100
sys.stdout.write(prefix + ": %.0f%%\r" % percent)
sys.stdout.flush()
qisys.qixml.write(tree, manifest_path, encoding="utf-8")
22 changes: 22 additions & 0 deletions python/qipkg/test/test_qipkg.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
import os

import qisys.command
import qisys.qixml
import qipkg.package

import pytest

Expand Down Expand Up @@ -151,3 +153,23 @@ def test_validate_package_exception(qipkg_action):
except:
as_thrown = True
assert as_thrown is True

def test_release_package(qipkg_action, tmpdir):
pkg_path = os.path.join(os.path.dirname(__file__), "projects", "python_services.pkg")
output_path = tmpdir.join("output.pkg")
qipkg_action("release-package", pkg_path, "--output", str(output_path))
dest = tmpdir.mkdir("dest")
qipkg_action.chdir(dest)
qipkg_action("extract-package", str(output_path))
package = dest.join("python_services-0.0.2")
assert package.join("lib", "my_service.pyc").check(file=True)
assert package.join("lib", "my_service.py").check(file=False)

tree = qisys.qixml.read(str(package.join("manifest.xml")))
services = tree.getroot().findall("services/service")
assert(services[0].attrib["execStart"] == "/usr/bin/python2.7 lib/my_service.pyc")
assert(services[1].attrib["execStart"] == "/usr/bin/python2.7 lib/my_service.pyc '127.0.0.1'")
# it was already pointing to a *.pyc file, nothing should have changed
assert(services[2].attrib["execStart"] == "/usr/bin/python2.7 lib/my_service.pyc")
# it is not pointing to a file of the package, nothing should have changed
assert(services[3].attrib["execStart"] == "/usr/bin/python2.7 tata.py")

0 comments on commit e059c16

Please sign in to comment.