Skip to content

Commit 9d9ef62

Browse files
author
u
committed
update
1 parent 41b18c6 commit 9d9ef62

3 files changed

Lines changed: 146 additions & 41 deletions

File tree

README.rst

Lines changed: 52 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -22,71 +22,91 @@ You can also check your distro of choice if they provide packages.
2222
Usage
2323
-----
2424

25-
Lets suppose you want to package zope.interface_ and you don't know how it is named
26-
exactly. First of all, you can search for it and download the source tarball if
27-
you found the correct module:
25+
Lets suppose you want to package path.
26+
First of all, you can download rockspec file
27+
and fetch source
2828

2929
.. code-block:: bash
3030
31-
$ py2pack search zope.interface
32-
searching for module zope.interface...
33-
found zope.interface-3.6.1
34-
$ py2pack fetch zope.interface
35-
downloading package zope.interface-3.6.1...
36-
from http://pypi.python.org/packages/source/z/zope.interface/zope.interface-3.6.1.tar.gz
31+
$ luarocks download --rockspec path
32+
33+
$ lua2pack fetch --rockspec 'glob://./*.rockspec'
34+
Cloning into 'lua-path'...
35+
remote: Enumerating objects: 288, done.
36+
remote: Total 288 (delta 0), reused 0 (delta 0), pack-reused 288 (from 1)
37+
Receiving objects: 100% (288/288), 33.78 KiB | 6.76 MiB/s, done.
38+
Resolving deltas: 100% (155/155), done.
39+
Note: switching to '35f8e6b0e8f9a735ecc5b4834147fad83e42851d'.
40+
41+
You are in 'detached HEAD' state. You can look around, make experimental
42+
changes and commit them, and you can discard any commits you make in this
43+
state without impacting any branches by switching back to a branch.
44+
45+
If you want to create a new branch to retain commits you create, you may
46+
do so (now or later) by using -c with the switch command. Example:
47+
48+
git switch -c <new-branch-name>
49+
50+
Or undo this operation with:
51+
52+
git switch -
53+
54+
Turn off this advice by setting config variable advice.detachedHead to false
55+
56+
Packed: /tmp/tmp9utki30_/path-1.1.0-1.src.rock
57+
Successfully packed /tmp/tmp9utki30_/path-1.1.0-1.rockspec in /tmp/tmp9utki30_
58+
59+
Done. You may now enter directory
60+
path-1.1.0-1/lua-path
61+
and type 'luarocks make' to build.
62+
Successfully unpacked /tmp/tmp9utki30_/path-1.1.0-1.src.rock in /tmp/tmp9utki30_
63+
Successfully created /extra/home/suse/Desktop/path-1.1.0-1.tar.gz
3764
3865
3966
As a next step you may want to generate a package recipe for your distribution.
40-
For RPM_-based distributions (let's use openSUSE_ as an example), you want to
41-
generate a spec file (named 'python-zope.interface.spec'):
67+
For RPM_-based distributions you want to
68+
generate a spec file (named 'lua-path.spec'):
4269

4370
.. code-block:: bash
4471
45-
$ py2pack generate zope.interface -t opensuse.spec -f python-zope.interface.spec
72+
$ lua2pack generate --rockspec 'glob://./*.rockspec' --template 'generic.spec'
4673
4774
The source tarball and the package recipe is all you need to generate the RPM_
4875
(or DEB_) file.
49-
This final step may depend on which distribution you use. Again,
50-
for openSUSE_ (and by using the `Open Build Service`_), the complete recipe is:
76+
This final step may depend on which distribution you use.
77+
For building source rpm file the complete recipe is:
5178

5279
.. code-block:: bash
5380
54-
$ osc mkpac python-zope.interface
55-
$ cd python-zope.interface
56-
$ py2pack fetch zope.interface
57-
$ py2pack generate zope.interface -f python-zope.interface.spec
58-
$ osc build
81+
$ rpmbuild -bs "-D_sourcedir $PWD" lua-path.spec
5982
...
6083
6184
Depending on the module, you may have to adapt the resulting spec file slightly.
6285
To get further help about py2pack usage, issue the following command:
6386

6487
.. code-block:: bash
6588
66-
$ py2pack help
89+
$ lua2pack --help
6790
6891
6992
Hacking and contributing
7093
------------------------
7194

72-
You can test py2pack from your git checkout by executing the py2pack module.
73-
74-
Edit `py2pack/version.py` file changing the version number. Adding +1 to the revision
75-
number and optionally appending .dev1 is enough and makes sure that you can
76-
distinguish your hackish version from an installed one.
95+
You can test lua2pack from your git checkout by executing the lua2pack module.
7796

78-
From the py2pack directory, install the py2pack module locally.
97+
Edit `setup.py` file changing the version number.
98+
From the lua2pack directory, install the lua2pack module locally.
7999

80100
.. code-block:: bash
81101
82102
$ pip install -e .
83103
84-
Now you can run your hackish py2pack version. It is usually located in
85-
$HOME/.local/bin/py2pack
104+
Now you can run your hackish lua2pack version. It is usually located in
105+
$HOME/.local/bin/lua2pack
86106

87107
.. code-block:: bash
88108
89-
$ py2pack
109+
$ lua2pack
90110
91111
Fork `the repository`_ on Github to start making your changes to the **master**
92112
branch (or branch off of it). Don't forget to write a test for fixed issues or
@@ -101,7 +121,7 @@ To run a single test class via `tox`_, use i.e.:
101121

102122
.. code-block:: bash
103123
104-
$ tox -epy38 test.test_py2pack:Py2packTestCase
124+
$ tox -epy313 test.test_py2pack:Py2packTestCase
105125
106126
107127
You can also run `pytest`_ directly:
@@ -113,18 +133,15 @@ You can also run `pytest`_ directly:
113133
It assumes you have the test dependencies installed (available on PYTHONPATH)
114134
on your system.
115135

116-
:copyright: (c) 2013 Sascha Peilicke.
136+
:copyright: (c) 2021 huakim tylyktar.
117137
:license: Apache-2.0, see LICENSE for more details.
118138

119139

120140
.. _argparse: http://pypi.python.org/pypi/argparse
121141
.. _Jinja2: http://pypi.python.org/pypi/Jinja2
122-
.. _zope.interface: http://pypi.python.org/pypi/zope.interface/
123-
.. _openSUSE: http://www.opensuse.org/en/
124142
.. _RPM: http://en.wikipedia.org/wiki/RPM_Package_Manager
125143
.. _DEB: http://en.wikipedia.org/wiki/Deb_(file_format)
126144
.. _`Python Package Index`: https://pypi.org/
127-
.. _`Open Build Service`: https://build.opensuse.org/package/show/devel:languages:python/python-py2pack
128-
.. _`the repository`: https://github.com/openSUSE/py2pack
145+
.. _`the repository`: https://github.com/huakim/lua2pack
129146
.. _`pytest`: https://github.com/pytest-dev/pytest
130147
.. _`tox`: http://testrun.org/tox

lua2pack/__init__.py

Lines changed: 94 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,17 @@
55
from pathlib import Path
66
import argparse
77
from .osdeps_utils import lua_code as os_specific_lua_code, generate_args as os_specific_generate_args, mount_adapter
8-
from os.path import isdir, isfile
9-
from os import chdir
8+
from os.path import isdir, isfile, join, relpath, isdir, abspath
9+
from os import chdir, walk, path, listdir, getcwd
1010
from .osdeps import DeclareLuaMapping as LuaMapping, is_enabled, store_flag
11-
from os import path, listdir, getcwd
1211
from jinja2_easy.generator import Generator
12+
1313
import platformdirs
14+
import subprocess
15+
import tempfile
16+
import shutil
17+
import sys
18+
import tarfile
1419

1520
from requests import Session
1621
from requests.exceptions import RequestException
@@ -22,6 +27,71 @@ class GetCwd:
2227
def __repr__(self):
2328
return getcwd()
2429

30+
def fetch(args):
31+
# Step 1: Create a temporary directory
32+
outdir = args.outdir
33+
with tempfile.TemporaryDirectory() as temp_dir:
34+
# set output directory
35+
tmp = args.outdir = str(temp_dir)
36+
37+
if (is_enabled(args, 'tostdout')):
38+
resultdir = tmp
39+
elif not outdir:
40+
resultdir = getcwd()
41+
else:
42+
resultdir = outdir
43+
# set template to rock.rockspec
44+
args.template = 'rock.rockspec'
45+
# Generate rockspec file in temporary directory
46+
generator(args)
47+
# Find the generated rockspec file
48+
rockspec_files = [f for f in listdir(temp_dir) if f.endswith('.rockspec')]
49+
if not rockspec_files:
50+
raise Exception("No .rockspec files found in the temporary directory.")
51+
return
52+
rockspec_file = join(tmp, rockspec_files[0])
53+
# Run luarocks pack in the temporary directory
54+
if subprocess.run(
55+
["luarocks", "pack", rockspec_file],
56+
cwd=temp_dir,
57+
check=True
58+
).returncode == 0:
59+
print(f"Successfully packed {rockspec_file} in {tmp}")
60+
else:
61+
print(f"Error while packing")
62+
# Find the generated .src.rock file
63+
rock_files = [f for f in listdir(temp_dir) if f.endswith('.src.rock')]
64+
if not rock_files:
65+
raise Exception("No .src.rock files found in the temporary directory.")
66+
return
67+
# Unpack the first .src.rock file found
68+
rock_prefix = rock_files[0]
69+
rock_file = join(tmp, rock_prefix)
70+
if subprocess.run(
71+
["luarocks", "unpack", rock_file],
72+
cwd=temp_dir,
73+
check=True
74+
).returncode == 0:
75+
print(f"Successfully unpacked {rock_file} in {tmp}")
76+
else:
77+
print(f"Error while unpacking")
78+
# Get prefix of unpacked directory
79+
rock_prefix = rock_prefix[0:-len('.src.rock')]
80+
# Get directory
81+
rock_dir = join(temp_dir, rock_prefix)
82+
# Find directories in rock_dir
83+
inner_dirs = [f for f in listdir(rock_dir) if isdir(join(rock_dir, f))]
84+
if not inner_dirs:
85+
raise Exception(f"No inner dir at path {rock_dir}")
86+
# Archive the first directory found
87+
inner_dir = join(rock_dir, inner_dirs[0])
88+
outfile = join(resultdir, rock_prefix+".tar.gz")
89+
# create_tar_gz_with_prefix(inner_dir, rock_prefix, outfile)
90+
with tarfile.open(outfile, "w:gz") as tar:
91+
tar.add(inner_dir, arcname=rock_prefix)
92+
print(f"Successfully created {outfile}")
93+
args.outdir = outdir
94+
2595
class generate_rockspec(Generator):
2696
def __init__(self, *args, current_directory=GetCwd(), **kwargs):
2797
super().__init__(*args, **kwargs)
@@ -97,6 +167,17 @@ def custom_dependency(args, name, cache):
97167
# Define generator's template environment
98168
generator = generate_rockspec('lua2pack', __path__[0])
99169

170+
def Munch(args):
171+
import collections
172+
d = collections.defaultdict(lambda: None)
173+
d.update(args)
174+
return type('Munch', tuple(), {
175+
"__getattr__": d.__getitem__,
176+
"__setattr__": d.__setitem__,
177+
"__getitem__": d.__getitem__,
178+
"__setitem__": d.__setitem__,
179+
"__contains__": d.__contains__})()
180+
100181
def main(args=None):
101182
# Create the parser
102183
mainparser = argparse.ArgumentParser(description="A Python script that generates a rockspec file")
@@ -108,17 +189,24 @@ def main(args=None):
108189
subparsers = mainparser.add_subparsers(title='commands')
109190
# add generate command
110191
parser = subparsers.add_parser('generate', help="generate RPM spec or DEB dsc file for a rockspec specification")
192+
# add generate command
193+
fetcher = subparsers.add_parser('fetch', help="fetch sources for a rockspec specification")
111194
# add noop operation
112195
store_flag(parser, 'noop')
196+
store_flag(fetcher, 'noop')
113197
# add tostdout flag
114198
store_flag(parser, 'tostdout')
199+
store_flag(fetcher, 'tostdout')
115200
# Define the command-line arguments
116201
# Rockspec file
117202
parser.add_argument("--rockspec", help="Path to the rockspec file or URI", type=str, action='append')
203+
fetcher.add_argument("--rockspec", help="Path to the rockspec file or URI", type=str, action='append')
118204
# Define lua parameters
119205
parser.add_argument("--define", help="Override some lua parameters", type=str, action='append', nargs='*')
206+
fetcher.add_argument("--define", help="Override some lua parameters", type=str, action='append', nargs='*')
120207
# Add specific lua code
121208
parser.add_argument("--luacode", help="Override some lua codes", type=str, action='append')
209+
fetcher.add_argument("--luacode", help="Override some lua codes", type=str, action='append')
122210
# Add duplicates
123211
for i in duplicates:
124212
parser.add_argument('--'+i.replace('_', '-'), help=f"Additional {i.replace('_', ' ')[1:]} to be added", type=str, action='append')
@@ -130,10 +218,12 @@ def main(args=None):
130218
parser.add_argument('-f', '--filename', help='output filename (optional)')
131219
# Template output directory for generate command
132220
parser.add_argument('--outdir', help='out directory (used by obs service)')
221+
fetcher.add_argument('--outdir', help='out directory (used by obs service)')
133222
# Function for generate command
134223
parser.set_defaults(func=generator)
224+
fetcher.set_defaults(func=fetch)
135225
# Parse arguments
136-
args = mainparser.parse_args(args)
226+
args = Munch(mainparser.parse_args(args).__dict__)
137227
# Check if noop is enabled, if yes, then exit
138228
if is_enabled(args, 'noop'):
139229
return

lua2pack/templates/generic.spec

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,8 @@
11
%define luarocks_pkg_name {{ package }}
22
%define luarocks_pkg_version {{ version }}
33
%{?!luadist:%define luadist(-) lua}
4-
{%- if not sourcerock %}
54
Source1: {{ rockspec }}
65
%define luarocks_rockspec_file %{SOURCE1}
7-
{%- endif %}
86
Name: {{ name }}
97
Version: %{?luarocks_pkg_major}%{?!luarocks_pkg_major:0}
108
Release: %{?luarocks_pkg_minor}%{?!luarocks_pkg_minor:0}%{?autorelease}

0 commit comments

Comments
 (0)