Skip to contentSkip to conversation

python-package-generator

PATCH In review Denis THULIN 3 participants 8 messages Jul 9, 2015 – Aug 8, 2015

lore
Share & tools
lore
#0 lore raw

cover python-package-generator

This patch series:
* Add utility scanpypi
* Add package python-robotframework

scanpypi is an utility intended to automatically generatepython packages for buildroot using metadata provided by the python package index
https://pypi.python.org.

python-robotframework is a package generated using scanpypi

v0: original patch

v0 -> v1: modification done according to Thomas Petazzoni and
Arnout Vandecappelle's comments

v1 -> v2: Made a class for packages in scanpypi. Also removed python-magic from
patch because I have no need for it and it is not working for now.

Denis THULIN (2):
scanpypi: new utility
python-robotframework: New package

 docs/manual/adding-packages-python.txt             |  32 ++
 package/Config.in                                  |   1 +
 package/python-robotframework/Config.in            |  10 +
 .../python-robotframework.hash                     |   3 +
 .../python-robotframework/python-robotframework.mk |  14 +
 support/scripts/scanpypi                           | 620 +++++++++++++++++++++
 6 files changed, 680 insertions(+)
 create mode 100644 package/python-robotframework/Config.in
 create mode 100644 package/python-robotframework/python-robotframework.hash
 create mode 100644 package/python-robotframework/python-robotframework.mk
 create mode 100755 support/scripts/scanpypi
Signature · 1 line
2.4.4
#1 lore raw

PATCH 1/2 scanpypi: new utility

An utility for creating python package from the python package index

---
v0: initial commit
python-pacakage-generator.py is an utility for automatically generating a python package. It fetches packages info from http://pypi.python.org and generates corresponding packages files.

v1:
- renamed python-package-generator to scanpypi
- split the huge script into a lot of functions
- fixed mistakes and small bugs

v2:
- Rewrited most of the functions into a class
- Changed the method for importing setup.py
- Created a main function to avoid use of global variable
- Now adds new dependencies to the list of packages to create
- Droppped the .py extension

---
 docs/manual/adding-packages-python.txt |  32 ++
 support/scripts/scanpypi               | 620 +++++++++++++++++++++++++++++++++
 2 files changed, 652 insertions(+)
 create mode 100755 support/scripts/scanpypi
docs/manual/adding-packages-python.txt +32 -0
index f81d625..dcde08d 100644
@@ -7,6 +7,38 @@ This infrastructure applies to Python packages that use the standard
 Python setuptools mechanism as their build system, generally
 recognizable by the usage of a +setup.py+ script.
 
+[[scanpypi]]
+
+==== Generating a +python-package+ from a pypi repository
+
+You may want to use the +scanpypi.py+ located in
++support/script+ to generate a package from an existing pypi(pip) package.
+
+you can find the list of existing pypi package here: https://pypi.python.org .
+
+Please keep in mind that you most likely need to manually check the package for
+any mistakes as there are things that cannot be guessed by the generator (e.g. 
+dependencies on any of the python core modules such as BR2_PACKAGE_PYTHON_ZLIB)
+. You need no manually add the package to the +package/Config.in+ file.
+
+When at the root of your buildroot directory just do :
+
+-----------------------
+./support/script/scanpypi.py foo bar -o package
+-----------------------
+
+This will generate packages +python-foo+ and +python-bar+ in the package
+folder if they exist on https://pypi.python.org.
+
+Find the +external python modules+ menu and insert your package inside.
+Keep in mind that the items inside a menu should be in alphabetical order.
+
+Option +-h+ wil list the options available
+
+-----------------------
+./support/script/scanpypi.py -h
+-----------------------
+
 [[python-package-tutorial]]
 
 ==== +python-package+ tutorial
support/scripts/scanpypi new file +620 -0

620 lines not on this page · raw message

Signature · 1 line
2.4.4
#2 lore raw

PATCH 1/2 scanpypi: new utility

Quoted 1 line · Denis THULIN

An utility for creating python package from the python package index

It would make sense to list here the packages with which you have tested it.
I've tried it for about 5 packages and it worked for none of them... Usually the problem is that setup.py either has an if __name__ == '__main__', or it tries to load other modules from the package.

I think you should at least test it with the packages that already are in
buildroot. django, pyzmq, six, twisted are a few nice examples. This also allows you to see how the automatically generated files differ from what we have already.

Quoted 7 lines

---
v0: initial commit
python-pacakage-generator.py is an utility for automatically generating a python package. It fetches packages info from http://pypi.python.org and generates corresponding packages files.

Actually, this sentence should go into the commit log as well.

Quoted 9 lines

v1:
- renamed python-package-generator to scanpypi
- split the huge script into a lot of functions
- fixed mistakes and small bugs

v2:
- Rewrited most of the functions into a class
- Changed the method for importing setup.py

And what was the reason for that change? It turns out not to work so well...

Quoted 16 lines

- Created a main function to avoid use of global variable
- Now adds new dependencies to the list of packages to create
- Droppped the .py extension

---
 docs/manual/adding-packages-python.txt |  32 ++
 support/scripts/scanpypi               | 620 +++++++++++++++++++++++++++++++++
 2 files changed, 652 insertions(+)
 create mode 100755 support/scripts/scanpypi
docs/manual/adding-packages-python.txt +0 -0
index f81d625..dcde08d 100644

docs/manual/adding-packages-python.txt This infrastructure applies to Python packages that use the standard · line 14

Quoted 8 lines
docs/manual/adding-packages-python.txt +5 -0
@@ -7,6 +7,38 @@ This infrastructure applies to Python packages that use the standard
 Python setuptools mechanism as their build system, generally
 recognizable by the usage of a +setup.py+ script.
 
+[[scanpypi]]
+
+==== Generating a +python-package+ from a pypi repository
+
+You may want to use the +scanpypi.py+ located in

Drop the .py :-) and rewrap.

docs/manual/adding-packages-python.txt This infrastructure applies to Python packages that use the standard · line 15

Quoted 1 line
docs/manual/adding-packages-python.txt +1 -0
++support/script+ to generate a package from an existing pypi(pip) package.

PyPI is capitalized differently. Rephrase:

... from an existing PyPI package (i.e. a package that can be installed with pip).

docs/manual/adding-packages-python.txt This infrastructure applies to Python packages that use the standard · line 17

Quoted 2 lines
docs/manual/adding-packages-python.txt +2 -0
+
+you can find the list of existing pypi package here: https://pypi.python.org .

You can find the list of existing PyPI packages https://pypi.python.org[here].

docs/manual/adding-packages-python.txt This infrastructure applies to Python packages that use the standard · line 21

Quoted 4 lines
docs/manual/adding-packages-python.txt +4 -0
+
+Please keep in mind that you most likely need to manually check the package for
+any mistakes as there are things that cannot be guessed by the generator (e.g. 
+dependencies on any of the python core modules such as BR2_PACKAGE_PYTHON_ZLIB)

Also useful te mention: the license and license files (they're a bit of ad hoc
guesswork).

docs/manual/adding-packages-python.txt This infrastructure applies to Python packages that use the standard · line 22

Quoted 1 line
docs/manual/adding-packages-python.txt +1 -0
+. You need no manually add the package to the +package/Config.in+ file.

That . should still be on the preceding line, without space in front of it.

You also need to manually add the package the the +package/Config.in+ file.

docs/manual/adding-packages-python.txt This infrastructure applies to Python packages that use the standard · line 27

Quoted 5 lines
docs/manual/adding-packages-python.txt +5 -0
+
+When at the root of your buildroot directory just do :
+
+-----------------------
+./support/script/scanpypi.py foo bar -o package

s/.py//

docs/manual/adding-packages-python.txt This infrastructure applies to Python packages that use the standard · line 34

Quoted 7 lines
docs/manual/adding-packages-python.txt +7 -0
+-----------------------
+
+This will generate packages +python-foo+ and +python-bar+ in the package
+folder if they exist on https://pypi.python.org.
+
+Find the +external python modules+ menu and insert your package inside.
+Keep in mind that the items inside a menu should be in alphabetical order.

I would also make an explicit reference here to how to use it with
BR2_EXTERNAL. I.e., using the -o flag.

docs/manual/adding-packages-python.txt This infrastructure applies to Python packages that use the standard · line 39

Quoted 5 lines
docs/manual/adding-packages-python.txt +5 -0
+
+Option +-h+ wil list the options available
+
+-----------------------
+./support/script/scanpypi.py -h

s/.py//

support/scripts/scanpypi line 1

Quoted 12 lines
docs/manual/adding-packages-python.txt +2 -0
+-----------------------
+
 [[python-package-tutorial]]
 
 ==== +python-package+ tutorial
support/scripts/scanpypi new file +1 -0
new file mode 100755
index 0000000..e98e8f8
@@ -0,0 +1,620 @@
+#!/usr/bin/python2

It would be nice to make it python3 compliant as well, but that can be done later.

Quoted 16 lines
support/scripts/scanpypi new file +16 -0
+"""
+Utility for building buildroot packages for existing pypi packages
+
+Any package built by scanpypi should be manually checked for
+errors.
+"""
+from __future__ import print_function
+import argparse
+import json
+import urllib2
+import sys
+import os
+import shutil
+import StringIO
+import tarfile
+import errno

support/scripts/scanpypi line 27

Quoted 10 lines
support/scripts/scanpypi new file +10 -0
+import hashlib
+import re
+import textwrap
+import tempfile
+import imp
+from functools import wraps
+
+
+# private global
+_calls = {}

I guess this could be converted into a class member of BuildrootPackage.

Also, the name could be a bit better, e.g. setup_args.

support/scripts/scanpypi line 36

Quoted 9 lines
support/scripts/scanpypi new file +9 -0
+
+
+def setup_info(pkg_name):
+    """Get a package info from _calls
+
+    Keyword arguments:
+    pkg_name -- the name of the package
+    """
+    return _calls[pkg_name]

I still don't think it's worthwhile to make a function from this.

support/scripts/scanpypi line 42

Quoted 6 lines
support/scripts/scanpypi new file +6 -0
+
+
+def setup_decorator(func, method):
+    """
+    Decorator for distutils.core.setup and setuptools.setup.
+    Puts the args of setup as a dict inside global private dict _calls.

I finally understand what this does :-) Even better:

Puts the arguments with which setup is called as a dict ...

support/scripts/scanpypi line 52

Quoted 10 lines
support/scripts/scanpypi new file +10 -0
+    Add key 'method' which should be either 'setuptools' or 'distutils'.
+
+    Keyword arguments:
+    func -- either setuptools.setup or distutils.core.setup
+    method -- either 'setuptools' or 'distutils'
+    """
+
+    @wraps(func)
+    def closure(*args, **kwargs):
+        _calls[kwargs['name']] = kwargs

Perhaps document where this 'name' comes from. It's quite obvious if you're
familiar with distutils, but this wrapping is not easy to understand so it's worthwhile to mention it.

support/scripts/scanpypi line 61

Quoted 9 lines
support/scripts/scanpypi new file +9 -0
+        _calls[kwargs['name']]['method'] = method
+    return closure
+
+
+def find_file_upper_case(filenames, path='./'):
+    """
+    List generator:
+    Recursively find files that matches one of the specified filenames.
+    Returns absolute path

No it doesn't, it returns a relative path starting with path.

Quoted 4 lines
support/scripts/scanpypi new file +4 -0
+
+    Keyword arguments:
+    filenames -- List of filenames to be found
+    path -- Path to the directory to search

support/scripts/scanpypi line 75

Quoted 10 lines
support/scripts/scanpypi new file +10 -0
+    """
+    for root, dirs, files in os.walk(path):
+        for file in files:
+            if file.upper() in filenames:
+                yield (os.path.join(root, file))
+
+
+def pkg_buildroot_name(pkg_name):
+    """
+    Returns name to avoid troublesome characters.

No it doesn't.

Returns the buildroot package name for the PyPI package pkg_name.

In fact, the function should also prepend the python- prefix, because the
buildroot name starts with python-.

Quoted 6 lines
support/scripts/scanpypi new file +6 -0
+    Remove all non alphanumeric characters except -
+    Also lowers the name
+
+    Keyword arguments:
+    pkg_name -- String to rename
+    """

support/scripts/scanpypi line 91

Quoted 10 lines
support/scripts/scanpypi new file +10 -0
+    name = re.sub('[^\w-]', '', pkg_name.lower())
+    name = re.sub('^python-', '', name)
+    return name
+
+
+# monkey patch
+import setuptools
+setuptools.setup = setup_decorator(setuptools.setup, 'setuptools')
+import distutils
+distutils.core.setup = setup_decorator(setuptools.setup, 'distutils')

This should go just below the definition of the decorator.

Quoted 19 lines
support/scripts/scanpypi new file +19 -0
+
+
+class SetupNotFound(Exception):
+    pass
+
+
+class BuildrootPackage():
+    """
+    This class's methods are not meant to be used individually please use those
+    in the correct order:
+    __init__
+
+    download_package
+
+    extract_package
+
+    load_module
+
+    get_requirements

support/scripts/scanpypi line 120

Quoted 10 lines
support/scripts/scanpypi new file +10 -0
+
+    create_package_mk
+
+    create_hash_file
+
+    create_config_in
+    """
+    def __init__(self, real_name, pkg_folder):
+        self.real_name = real_name
+        self.name = pkg_buildroot_name(self.real_name)

Perhaps to clarify the distinction, call it buildroot_name instead of just name.

support/scripts/scanpypi line 121

Quoted 1 line
support/scripts/scanpypi new file +1 -0
+        self.pkg_dir = pkg_folder + '/python-' + self.name

It's good practice to set all the members you're going to create to None in
__init__, as a kind of declaration of what members are available.

support/scripts/scanpypi line 127

Quoted 6 lines
support/scripts/scanpypi new file +6 -0
+
+    def find_setup(self, folder):
+        """
+        Search for setup.py file in an archive and returns the path to this
+        file
+        if it is found

Wrapping.

support/scripts/scanpypi line 138

Quoted 11 lines
support/scripts/scanpypi new file +11 -0
+
+        Keyword arguments:
+        folder -- path to search in
+        """
+        filename = 'setup.py'
+        # Next return the first element of an iterable,
+        # here, it returns the path to the first file named "setup.py"
+        # found in the python package or raises StopIteration if not found
+        self.setup_location = next(os.path.join(root, filename)
+                                   for root, dirs, files in os.walk(folder)
+                                   if filename in files)

Is it just me or is setup_location not used anywhere? I anyway don't think this
is a good idea: if setup.py is not in the top dir, most likely other things in this script will break.

So the only thing this function does, really, is checking if there is any
setup.py file in the folder. Which is later done again by calling imp.find_module, which will anyway raise an ImportError if the module isn't found.

IOW, remove this function.

support/scripts/scanpypi line 142

Quoted 4 lines
support/scripts/scanpypi new file +4 -0
+
+    def fetch_package_info(self):
+        """
+        Fetch a package's metadata for the python package index

for -> from

support/scripts/scanpypi line 144

Quoted 2 lines
support/scripts/scanpypi new file +2 -0
+        """
+        url = 'https://pypi.python.org/pypi/{pkg}/json'.format(

Already asign to metadata_url here.

support/scripts/scanpypi line 146

Quoted 2 lines
support/scripts/scanpypi new file +2 -0
+            pkg=self.real_name)
+        print('URL:', url)

This print is not needed.

Quoted 6 lines
support/scripts/scanpypi new file +6 -0
+        try:
+            pkg_json = urllib2.urlopen(url).read().decode()
+        except urllib2.HTTPError as error:
+            print('ERROR:', error.getcode(), error.msg, file=sys.stderr)
+            print('ERROR: Could not find package {pkg}.\n'
+                  'Check syntax inside the python package index:\n'

support/scripts/scanpypi line 162

Quoted 10 lines
support/scripts/scanpypi new file +10 -0
+                  'https://pypi.python.org/pypi/ '
+                  .format(pkg=self.real_name))
+            raise
+        except urllib2.URLError:
+            print('ERROR: Could not find package {pkg}.\n'
+                  'Check syntax inside the python package index:\n'
+                  'https://pypi.python.org/pypi/ '
+                  .format(pkg=self.real_name))
+            raise
+        else:

Else is not needed here since the exceptions are raise'd.

support/scripts/scanpypi line 175

Quoted 13 lines
support/scripts/scanpypi new file +13 -0
+            self.metadata_url = url
+            self.metadata = json.loads(pkg_json)
+            self.metadata_name = self.metadata['info']['name']
+
+    def download_package(self):
+        """
+        Download a package using metadata from pypi
+        """
+        try:
+            self.metadata['urls'][0]['filename']
+        except IndexError:
+            print(
+                'Non conventional package, ',

Non-conventional

support/scripts/scanpypi line 176

Quoted 1 line
support/scripts/scanpypi new file +1 -0
+                'please check manually after creation')

manually -> carefully

support/scripts/scanpypi line 177

Quoted 1 line
support/scripts/scanpypi new file +1 -0
+            download_url = self.metadata['info']['download_url']

I think it would be simpler to, instead of repeating the download infra, just
emulate the needed metadata and use the same infra as below. So:

download_url = [{
'packagetype': 'tgz',
'url', self.metadata['info']['download_url'],
'md5_digest': None,
}]

support/scripts/scanpypi line 179

Quoted 2 lines
support/scripts/scanpypi new file +2 -0
+            try:
+                self.download = urllib2.urlopen(download_url)

self.download is not used outside this function, so make it a local variable.
Or actually, drop it completely and read directly into self.as_string().

support/scripts/scanpypi line 186

Quoted 7 lines
support/scripts/scanpypi new file +7 -0
+            except urllib2.HTTPError as http_error:
+                self.download = http_error
+            else:
+                self.as_string = self.download.read()
+                self.used_url = {'url': download_url,
+                                 'md5_digest': hashlib.md5(self.as_string)
+                                                      .hexdigest()}

I don't think it makes a lot os sense to compute the md5 here. Better set it to
None and not mention it in the .hash file.

support/scripts/scanpypi line 194

Quoted 8 lines
support/scripts/scanpypi new file +8 -0
+                # In this case, we can't get the name of the downloaded file
+                # from the pypi api, so we need to find it, this should work
+                urlpath = urllib2.urlparse.urlparse(self.download.url).path
+                # urlparse().path give something like
+                # /path/to/file-version.tar.gz
+                # We use basename to remove /path/to
+                self.targz = os.path.basename(urlpath)
+                self.used_url['filename'] = self.targz

This magic is still needed to set download_url['filename'], but it doesn't need
to be downloaded yet to do that.

support/scripts/scanpypi line 197

Quoted 3 lines
support/scripts/scanpypi new file +3 -0
+        else:
+            for download_url in self.metadata['urls']:
+                if 'wheel' in download_url['packagetype']:

This could be zip or egg as well. Isn't there a positive match possible?

support/scripts/scanpypi line 199

Quoted 2 lines
support/scripts/scanpypi new file +2 -0
+                    continue
+                try:

I think it's useful to print the download URL here, just before downloading. So
move the print ('Downloading package ...') here instead, and add the download URL to it.

support/scripts/scanpypi line 202

Quoted 3 lines
support/scripts/scanpypi new file +3 -0
+                    self.download = urllib2.urlopen(download_url['url'])
+                except urllib2.HTTPError as http_error:
+                    self.download = http_error

This is only used to raise it later, so you can just drop the entire try block
and let the exception leak up.

support/scripts/scanpypi line 208

Quoted 6 lines
support/scripts/scanpypi new file +6 -0
+                else:
+                    self.used_url = download_url
+                    self.as_string = self.download.read()
+                    self.md5_sum = hashlib.md5(self.as_string).hexdigest()
+                    self.targz = self.used_url['filename']
+                    if self.md5_sum == download_url['md5_digest']:

This should probably raise an exception if it doesn't match.

support/scripts/scanpypi line 209

Quoted 1 line
support/scripts/scanpypi new file +1 -0
+                        break

The for loop needs an else: in case none of them can be downloaded.

support/scripts/scanpypi line 213

Quoted 4 lines
support/scripts/scanpypi new file +4 -0
+        if self.download.__class__ == urllib2.HTTPError:
+            raise self.download
+        else:
+            self.digest_method = 'sha256'

This is always sha256 so no point making a variable for it.

support/scripts/scanpypi line 214

Quoted 1 line
support/scripts/scanpypi new file +1 -0
+            self.digest = hashlib.sha256(self.as_string).hexdigest()

Minor nit: it's used only once, so I'd calculate it at the time that you use it.

support/scripts/scanpypi line 219

Quoted 5 lines
support/scripts/scanpypi new file +5 -0
+
+    def extract_package(self, tmp_path):
+        """
+        Create folders used for extracting a package as file object and extract
+        it

A bit too detailed...

Extract the package contents into a directory.

support/scripts/scanpypi line 222

Quoted 3 lines
support/scripts/scanpypi new file +3 -0
+
+        Keyword arguments:
+        tmp_path -- folder where you want the package to be extracted

s/folder/directory/g

Quoted 10 lines
support/scripts/scanpypi new file +10 -0
+        """
+        as_file = StringIO.StringIO(self.as_string)
+        with tarfile.open(fileobj=as_file) as as_tarfile:
+            tmp_pkg = os.path.join(tmp_path, self.name)
+            try:
+                os.makedirs(tmp_pkg)
+            except OSError as exception:
+                if exception.errno != errno.EEXIST:
+                    print("ERROR: ", exception.message, file=sys.stderr)
+                    return None, None

support/scripts/scanpypi line 242

Quoted 10 lines
support/scripts/scanpypi new file +10 -0
+                print('WARNING:', exception.message, file=sys.stderr)
+                print('Removing {pkg}...'.format(pkg=tmp_pkg))
+                shutil.rmtree(tmp_pkg)
+                os.makedirs(tmp_pkg)
+            as_tarfile.extractall(tmp_pkg)
+        try:
+            self.find_setup(tmp_pkg)
+        except StopIteration:
+            raise SetupNotFound('Could not find file setup.py for package {}'
+                                .format(self.real_name))

As I said before, this is not needed since it is covered by load_setup().

support/scripts/scanpypi line 243

Quoted 1 line
support/scripts/scanpypi new file +1 -0
+        tmp_extract = '{folder}/{name}-{version}'

Is this guaranteed to be the directory used by the package?

support/scripts/scanpypi line 255

Quoted 12 lines
support/scripts/scanpypi new file +12 -0
+        self.tmp_extract = tmp_extract.format(
+            folder=tmp_pkg,
+            name=self.metadata_name,
+            version=self.metadata['info']['version'])
+
+    def load_setup(self):
+        """
+        Loads the corresponding setup and store its metadata
+        """
+        s_file, s_path, s_desc = imp.find_module('setup', [self.tmp_extract])
+        imp.load_module('setup', s_file, s_path, s_desc)
+        self.setup_metadata = setup_info(self.metadata_name)

Is the name given in the metadata guaranteed to be the same as what is passed
to setup()? Especially considering case...

Quoted 6 lines
support/scripts/scanpypi new file +6 -0
+
+    def get_requirements(self, pkg_folder, packages):
+        """
+        Retrieve dependencies from the metadata found in the setup.py script of
+        a pypi package.
+

support/scripts/scanpypi line 271

Quoted 10 lines
support/scripts/scanpypi new file +10 -0
+        Keyword Arguments:
+        pkg_folder -- location of the already created packages
+        packages  -- other packages to be built
+        """
+        if 'install_requires' not in self.setup_metadata:
+            self.pkg_req = None
+            self.req_not_found = []
+            return
+        self.pkg_req = self.setup_metadata['install_requires']
+        self.pkg_req = [re.sub('([\w-]+)[><=]*.*', r'\1', req).lower()

The .lower() is handled by pkg_buildroot_name so not needed here.

support/scripts/scanpypi line 276

Quoted 5 lines
support/scripts/scanpypi new file +5 -0
+                        for req in self.pkg_req]
+        self.pkg_req = map(pkg_buildroot_name, self.pkg_req)
+        self.req_not_found = [
+            pkg for pkg in self.pkg_req
+            if 'python-{name}'.format(name=pkg)

pkg_buildroot_name already adds python- (or at least it should :-).

support/scripts/scanpypi line 277

Quoted 1 line
support/scripts/scanpypi new file +1 -0
+            not in os.listdir(pkg_folder)

Maybe better use "not os.path.isdir(pkg)".

support/scripts/scanpypi line 280

Quoted 3 lines
support/scripts/scanpypi new file +3 -0
+        ]
+        self.req_not_found = [pkg for pkg in self.req_not_found
+                              if pkg not in packages]

Small nit: I'd make this function just return req_not_found instead of adding
it as a member, and move the set.difference logic up to the caller. Oh, and perhaps use a set :-)

Quoted 11 lines
support/scripts/scanpypi new file +11 -0
+
+    def __create_mk_header(self):
+        """
+        Create the header of the <package_name>.mk file
+        """
+        header = ['#' * 80 + '\n']
+        header.append('#\n')
+        header.append('# python-{name}\n'.format(name=self.name))
+        header.append('#\n')
+        header.append('#' * 80 + '\n')
+        header.append('\n')

support/scripts/scanpypi line 301

Quoted 10 lines
support/scripts/scanpypi new file +10 -0
+        return header
+
+    def __create_mk_download_info(self):
+        """
+        Create the lines refering to the download information of the
+        <package_name>.mk file
+        """
+        lines = []
+        version_line = 'PYTHON_{name}_VERSION = {version}\n'.format(
+            name=self.name.upper().replace('-', '_'),

Like you have pkg_buildroot_name, you should also have something for the
conversion to uppercase. Becasue below, you forget the replace('-', '_'). Also it's probably best to add the uppercase name as a member.

support/scripts/scanpypi line 302

Quoted 1 line
support/scripts/scanpypi new file +1 -0
+            version=self.metadata['info']['version'])

I think it makes sense to add these things as direct members of
BuildrootPackage rather than going through metadata all the time: version, url, filename.

Quoted 7 lines
support/scripts/scanpypi new file +7 -0
+        lines.append(version_line)
+
+        targz = self.targz.replace(
+            self.metadata['info']['version'],
+            '$(PYTHON_{name}_VERSION)'.format(name=self.name.upper()))
+        targz_line = 'PYTHON_{name}_SOURCE = {filename}\n'.format(
+            name=self.name.upper(),

support/scripts/scanpypi line 319

Quoted 10 lines
support/scripts/scanpypi new file +10 -0
+            filename=targz)
+        lines.append(targz_line)
+
+        site_line = ('PYTHON_{name}_SITE = {url}'.format(
+            name=self.name.upper(),
+            url=self.used_url['url'].replace(self.used_url['filename'], '')))
+        if 'sourceforge' in site_line:
+            site_line = ('PYTHON_{name}_SITE = {url}'.format(
+                name=self.name.upper(),
+                url=self.used_url['url']))

This looks very weird... At least add an explanatory comment. And anyway, it's
better to do any fixups in a new variable (e.g. 'site') and format the line only once.

Quoted 5 lines
support/scripts/scanpypi new file +5 -0
+        site_line = site_line.rstrip('/') + '\n'
+        lines.append(site_line)
+        return lines
+
+    def __create_mk_setup(self):

support/scripts/scanpypi line 334

Quoted 10 lines
support/scripts/scanpypi new file +10 -0
+        """
+        Create the line refering to the setup method of the package of the
+        <package_name>.mk file
+
+        There are two things you can use to make an installer
+        for a python package: distutils or setuptools
+        distutils comes with python but does not support dependencies.
+        distutils is mostly still there for backward support.
+        setuptools is what smart people use,
+        but it is not shipped with python :(

This comment is a bit redundant, but OK.

Quoted 24 lines
support/scripts/scanpypi new file +24 -0
+        """
+        lines = []
+        setup_type_line = 'PYTHON_{name}_SETUP_TYPE = {method}\n'.format(
+            name=self.name.upper(),
+            method=self.setup_metadata['method'])
+        lines.append(setup_type_line)
+        return lines
+
+    def __create_mk_license(self):
+        """
+        Create the lines referring to the package's license informations of the
+        <package_name>.mk file
+
+        The license is found using the metadata from pypi.
+        In the metadata, the license can be found either with standard names in
+        the classifiers part or with naming from the packager in the "License"
+        part.
+
+        From the classifiers, the license is "translated" according to
+        buildroot standards if need be (i.e. from Apache Software License to
+        Apache-2.0).
+
+        From the License part, we cannot guess what formatting the packager
+        used. Hence, it is likely to be incorrect. (i.e. Apache License 2.0

support/scripts/scanpypi line 368

Quoted 10 lines
support/scripts/scanpypi new file +10 -0
+        instead of Apache-2.0).
+
+        The license's files are found by searching the package for files named
+        license or license.txt (case insensitive).
+        If more than one license file is found, the user is asked to select
+        which ones he wants to use.
+        """
+        license_dict = {
+            'Apache Software License': 'Apache-2.0',
+            'BSD License': 'BSD',

We normally put BSD-2c, BSD-3c, etc. but it may be difficult to find that out
automatically.

support/scripts/scanpypi line 377

Quoted 9 lines
support/scripts/scanpypi new file +9 -0
+            'European Union Public Licence 1.0': 'EUPLv1.0',
+            'European Union Public Licence 1.1': 'EUPLv1.1',
+            "GNU General Public License": "GPL",
+            "GNU General Public License v2": "GPLv2",
+            "GNU General Public License v2 or later": "GPLv2+",
+            "GNU General Public License v3": "GPLv3",
+            "GNU General Public License v3 or later": "GPLv3+",
+            "GNU Lesser General Public License v2": "LGPLv2",
+            "GNU Lesser General Public License v2 or later": "LGPLv2+",

That's actually LGPVLv2.1(+). LGPLv2 is the GNU Library General Public License.

support/scripts/scanpypi line 380

Quoted 3 lines
support/scripts/scanpypi new file +3 -0
+            "GNU Lesser General Public License v3": "LGPLv3",
+            "GNU Lesser General Public License v3 or later": "LGPLv3+",
+            "GNU Library or Lesser General Public License": "LGPL",

So this is probably LGPLv2 (there was no library/lesser version of v1).

support/scripts/scanpypi line 387

Quoted 7 lines
support/scripts/scanpypi new file +7 -0
+            "ISC License": "ISC",
+            "MIT License": "MIT",
+            "Mozilla Public License 1.0": "MPL-1.0",
+            "Mozilla Public License 1.1": "MPL-1.1",
+            "Mozilla Public License 2.0": "MPL-2.0",
+            "Zope Public License": "ZPL"
+            }

Nice list!

support/scripts/scanpypi line 393

Quoted 6 lines
support/scripts/scanpypi new file +6 -0
+        regexp = re.compile('^License :* *.* *:+ (.*)( \(.*\))?$')
+        classifiers_licenses = [regexp.sub(r"\1", lic)
+                                for lic in self.metadata['info']['classifiers']
+                                if regexp.match(lic)]
+        licenses = map(lambda x: license_dict[x] if x in license_dict else x,
+                       classifiers_licenses)

I would give an explicit warning if the license is not in license_dict, since
it most likely means it's wrong.

support/scripts/scanpypi line 396

Quoted 3 lines
support/scripts/scanpypi new file +3 -0
+        lines = []
+        if not len(licenses):
+            licenses = [self.metadata['info']['license']]

This should probably go before the license_dict map.

support/scripts/scanpypi line 403

Quoted 7 lines
support/scripts/scanpypi new file +7 -0
+        license_line = 'PYTHON_{name}_LICENSE = {license}\n'.format(
+            name=self.name.upper(),
+            license=', '.join(licenses))
+        lines.append(license_line)
+        print('WARNING: License has been set to "{license}",'
+              ' please change it manually if necessary'.format(
+                  license=', '.join(licenses)))

So this warning is not necessary if it was found in the dict.

support/scripts/scanpypi line 405

Quoted 2 lines
support/scripts/scanpypi new file +2 -0
+
+        filenames = ['LICENSE', 'LICENSE.TXT']

Shouldn't we also look for COPYING?

Quoted 5 lines
support/scripts/scanpypi new file +5 -0
+        license_files = list(find_file_upper_case(filenames, self.tmp_extract))
+        license_files = [license.replace(self.tmp_extract, '')[1:]
+                         for license in license_files]
+        if len(license_files) > 0:
+            if len(license_files) > 1:

support/scripts/scanpypi line 420

Quoted 10 lines
support/scripts/scanpypi new file +10 -0
+                print('More than one file found for license:',
+                      ', '.join(license_files))
+            license_files = [filename
+                             for index, filename in enumerate(license_files)]
+            license_file_line = ('PYTHON_{name}_LICENSE_FILES ='
+                                 ' {files}\n'.format(
+                                     name=self.name.upper(),
+                                     files=' '.join(license_files)))
+            lines.append(license_file_line)
+        elif len(license_files) == 0:

Can len(license_files) be anything else than 0 at this point?

support/scripts/scanpypi line 422

Quoted 2 lines
support/scripts/scanpypi new file +2 -0
+            print('WARNING: No license file found,'
+                  ' please specify it manually afterward')

afterwards

Quoted 42 lines
support/scripts/scanpypi new file +42 -0
+            license_file_line = '# No license file found\n'
+
+        return lines
+
+    def __create_mk_requirements(self):
+        """
+        Create the lines referring to the dependencies of the of the
+        <package_name>.mk file
+
+        Keyword Arguments:
+        pkg_name -- name of the package
+        pkg_req -- dependencies of the package
+        """
+        lines = []
+        python_pkg_req = ['python-{name}'.format(name=pkg)
+                          for pkg in self.pkg_req]
+        dependencies_line = ('PYTHON_{name}_DEPENDENCIES ='
+                             ' {reqs}\n'.format(
+                                 name=self.name.upper(),
+                                 reqs=' '.join(python_pkg_req)))
+        lines.append(dependencies_line)
+        return lines
+
+    def create_package_mk(self):
+        """
+        Create the lines corresponding to the <package_name>.mk file
+        """
+        pkg_mk = 'python-{name}.mk'.format(name=self.name)
+        path_to_mk = os.path.join(self.pkg_dir, pkg_mk)
+        print('Creating {file}...'.format(file=path_to_mk))
+        lines = self.__create_mk_header()
+        lines += self.__create_mk_download_info()
+        lines += self.__create_mk_setup()
+        lines += self.__create_mk_license()
+        if self.pkg_req:
+            lines += self.__create_mk_requirements()
+
+        lines.append('\n')
+        lines.append('$(eval $(python-package))')
+        lines.append('\n')
+        with open(path_to_mk, 'w') as mk_file:
+            mk_file.writelines(lines)

support/scripts/scanpypi line 474

Quoted 10 lines
support/scripts/scanpypi new file +10 -0
+
+    def create_hash_file(self):
+        """
+        Create the lines corresponding to the <package_name>.hash files
+        """
+        pkg_hash = 'python-{name}.hash'.format(name=self.name)
+        path_to_hash = os.path.join(self.pkg_dir, pkg_hash)
+        print('Creating {filename}...'.format(filename=path_to_hash))
+        lines = []
+        commented_line = '# md5 from {url}, {method} calculated by scanpypi\n'

Actually, the md5 comes from the pypi/json url, so mention that one. So just:

# From https://pypi.python.org/pypi/{pkg}/json

support/scripts/scanpypi line 481

Quoted 7 lines
support/scripts/scanpypi new file +7 -0
+        commented_line = commented_line.format(url=self.used_url['url'],
+                                               method=self.digest_method)
+        lines.append(commented_line)
+        hash_line = '{method}\t{digest}  {filename}\n'.format(
+            method='md5',
+            digest=self.used_url['md5_digest'],
+            filename=self.used_url['filename'])

The md5 should only be added if there really was an md5.

Quoted 22 lines
support/scripts/scanpypi new file +22 -0
+        lines.append(hash_line)
+        hash_line = '{method}\t{digest}  {filename}\n'.format(
+            method=self.digest_method,
+            digest=self.digest,
+            filename=self.used_url['filename'])
+        lines.append(hash_line)
+
+        with open(path_to_hash, 'w') as hash_file:
+            hash_file.writelines(lines)
+
+    def create_config_in(self):
+        """
+        Creates the Config.in file of a package
+        """
+        path_to_config = os.path.join(self.pkg_dir, 'Config.in')
+        print('Creating {file}...'.format(file=path_to_config))
+        lines = []
+        config_line = 'config BR2_PACKAGE_PYTHON_{name}\n'.format(
+            name=self.name.upper())
+        lines.append(config_line)
+
+        bool_line = '\tbool "python-{name}"\n'.format(name=self.name)

support/scripts/scanpypi line 513

Quoted 10 lines
support/scripts/scanpypi new file +10 -0
+        lines.append(bool_line)
+        if self.pkg_req:
+            for dep in self.pkg_req:
+                dep_line = '\tselect BR2_PACKAGE_PYTHON_{req}\n'.format(
+                    req=dep.upper())
+                lines.append(dep_line)
+
+        lines.append('\thelp\n')
+
+        help_lines = textwrap.wrap(self.metadata['info']['summary'], 67)

I'm not really sure if we should use the summary or the description here... The
summary is _really_ short.

Also, use textwrap.fill and add the initial_indent='\t ' argument, then all
the mangling below should no longer be necessary.

Quoted 9 lines
support/scripts/scanpypi new file +9 -0
+        # \t + two spaces is 3 char long
+        help_lines.append('')
+        help_lines.append(self.metadata['info']['home_page'])
+        help_lines = map(lambda l: '\t  {}\n'.format(l)
+                                   if not l == '' else '\n',
+                         help_lines)
+        # help_lines = ['\t  {line}\n'.format(line=line).replace('\t  \n','\n')
+        #              for line in help_lines]
+        lines += help_lines

support/scripts/scanpypi line 532

Quoted 10 lines
support/scripts/scanpypi new file +10 -0
+
+        with open(path_to_config, 'w') as config_file:
+            config_file.writelines(lines)
+
+
+def main():
+    # Building the parser
+    parser = argparse.ArgumentParser(
+        description="Creates buildroot packages from the metadata of "
+                    "an existing pypi(pip) packages and include it "

PyPI

support/scripts/scanpypi line 535

Quoted 3 lines
support/scripts/scanpypi new file +3 -0
+                    "in menuconfig")
+    parser.add_argument("packages",
+                        help="list of packages to be made",

made -> created

support/scripts/scanpypi line 539

Quoted 4 lines
support/scripts/scanpypi new file +4 -0
+                        nargs='+')
+    parser.add_argument("-o", "--output",
+                        help="""
+                        Output directory for packages

Please explicitly mention the default in the help text.

Quoted 28 lines
support/scripts/scanpypi new file +28 -0
+                        """,
+                        default='./package')
+
+    args = parser.parse_args()
+    packages = list(set(args.packages))
+
+    # tmp_path is where we'll extract the files later
+    tmp_prefix = 'scanpypi-'
+    pkg_folder = args.output
+    tmp_path = tempfile.mkdtemp(prefix=tmp_prefix)
+    try:
+        for real_pkg_name in packages:
+            package = BuildrootPackage(real_pkg_name, pkg_folder)
+            print('buildroot package name for {}:'.format(package.real_name),
+                  package.name)
+            # First we download the package
+            # Most of the info we need can only be found inside the package
+            print('Package:', package.name)
+            print('Fetching package', package.real_name)
+            try:
+                package.fetch_package_info()
+            except (urllib2.URLError, urllib2.HTTPError):
+                continue
+
+            print('Downloading package {pkg}...'.format(
+                  pkg=package.metadata['info']['name']))
+            try:
+                package.download_package()

support/scripts/scanpypi line 577

Quoted 10 lines
support/scripts/scanpypi new file +10 -0
+            except urllib2.HTTPError as error:
+                print('Error: {code} {reason}'.format(code=error.code,
+                                                      reason=error.reason))
+                print('Error downloading package :', package.name)
+                continue
+
+            # extract the tarball
+            package.extract_package(tmp_path)
+            print(package.metadata_name)
+            print(package.tmp_extract)

These two prints are redundant.

support/scripts/scanpypi line 584

Quoted 7 lines
support/scripts/scanpypi new file +7 -0
+
+            # Loading the package install info from the package
+            package.load_setup()
+
+            # Package requirement are an argument of the setup function
+            package.get_requirements(pkg_folder, packages)
+            packages += package.req_not_found

Python doesn't allow updating a list while iterating over it. But I believe
there is something in itertools that supports that.

Quoted 23 lines
support/scripts/scanpypi new file +23 -0
+            if package.req_not_found:
+                print('Added packages \'{pkgs}\' as dependencies of {pkg}'
+                      .format(pkgs=", ".join(package.req_not_found),
+                              pkg=package.name))
+            print('Checking if package {name} already exists...'.format(
+                name=package.pkg_dir))
+            try:
+                os.makedirs(package.pkg_dir)
+            except OSError as exception:
+                if exception.errno != errno.EEXIST:
+                    print("ERROR: ", exception.message, file=sys.stderr)
+                    continue
+                print('Error: Package {name} already exists'
+                      .format(name=package.pkg_dir))
+                del_pkg = raw_input(
+                    'Do you want to delete existing package ? [y/N]')
+                if del_pkg.lower() == 'y':
+                    shutil.rmtree(package.pkg_dir)
+                    os.makedirs(package.pkg_dir)
+                else:
+                    continue
+            package.create_package_mk()
+

support/scripts/scanpypi line 617

Quoted 10 lines
support/scripts/scanpypi new file +10 -0
+            package.create_hash_file()
+
+            package.create_config_in()
+            print()
+            # printing an empty line for visual confort
+    except:
+        shutil.rmtree(tmp_path)
+        raise
+    else:
+        shutil.rmtree(tmp_path)

Isn't finally: supposed to do exactly what you're trying to do here?

Regards,
Arnout

Quoted 4 lines
support/scripts/scanpypi new file +3 -0
+
+if __name__ == "__main__":
+    main()
Signature · 6 lines
Arnout Vandecappelle                          arnout at mind be
Senior Embedded Software Architect            +32-16-286500
Essensium/Mind                                http://www.mind.be
G.Geenslaan 9, 3001 Leuven, Belgium           BE 872 984 063 RPR Leuven
LinkedIn profile: http://www.linkedin.com/in/arnoutvandecappelle
GPG fingerprint:  7493 020B C7E3 8618 8DEC 222C 82EB F404 F9AC 0DDF
#3 lore raw

PATCH 1/2 scanpypi: new utility

Hi Arnout,

----- Mail original -----

Quoted 12 lines · Denis THULIN
Quoted 2 lines

An utility for creating python package from the python package index

It would make sense to list here the packages with which you have tested it.
I've tried it for about 5 packages and it worked for none of them... Usually the
problem is that setup.py either has an if __name__ == '__main__', or it tries to
load other modules from the package.

Quoted 6 lines

I think you should at least test it with the packages that already are in
buildroot. django, pyzmq, six, twisted are a few nice examples. This also allows
you to see how the automatically generated files differ from what we have already.

django used to work fine, but I made some mistake and I did not test it again. Now, It works again on my local version.
The other works too.

Quoted 11 lines
Quoted 10 lines

---
v0: initial commit
python-pacakage-generator.py is an utility for automatically generating a
python package. It fetches packages info from
http://pypi.python.org and
generates corresponding packages files.

Quoted 1 line

Actually, this sentence should go into the commit log as well.

Ok.

Quoted 7 lines
Quoted 7 lines

v1:
- renamed python-package-generator to scanpypi
- split the huge script into a lot of functions
- fixed mistakes and small bugs

Quoted 6 lines
Quoted 3 lines

v2:
- Rewrited most of the functions into a class
- Changed the method for importing setup.py

And what was the reason for that change? It turns out not to work so well...

Well I thougt using the proper "python" way of doing these kind of import would be better that what I was doing previously.

Quoted 135 lines
Quoted 24 lines

- Created a main function to avoid use of global variable
- Now adds new dependencies to the list of packages to create
- Droppped the .py extension

---
 docs/manual/adding-packages-python.txt |  32 ++
 support/scripts/scanpypi               | 620
 +++++++++++++++++++++++++++++++++
 2 files changed, 652 insertions(+)
 create mode 100755 support/scripts/scanpypi
docs/manual/adding-packages-python.txt +0 -0
index f81d625..dcde08d 100644
@@ -7,6 +7,38 @@ This infrastructure applies to Python packages

that use the standard
Python setuptools mechanism as their build system, generally recognizable by the usage of a +setup.py+ script.

+[[scanpypi]]

docs/manual/adding-packages-python.txt This infrastructure applies to Python packages that use the standard · line 14

Quoted 4 lines
docs/manual/adding-packages-python.txt +4 -0
+
+==== Generating a +python-package+ from a pypi repository
+
+You may want to use the +scanpypi.py+ located in

Drop the .py :-) and rewrap.

Quoted 2 lines

++support/script+ to generate a package from an existing pypi(pip) package.

PyPI is capitalized differently. Rephrase:

... from an existing PyPI package (i.e. a package that can be installed with pip).

Quoted 3 lines
docs/manual/adding-packages-python.txt +2 -0
+
+you can find the list of existing pypi package here:

https://pypi.python.org .

You can find the list of existing PyPI packages
https://pypi.python.org[here].

Quoted 7 lines
docs/manual/adding-packages-python.txt +2 -0
+
+Please keep in mind that you most likely need to manually check

the package for
+any mistakes as there are things that cannot be guessed by the generator (e.g.
+dependencies on any of the python core modules such as BR2_PACKAGE_PYTHON_ZLIB)

Also useful te mention: the license and license files (they're a bit of ad hoc
guesswork).

Quoted 2 lines

+. You need no manually add the package to the +package/Config.in+ file.

That . should still be on the preceding line, without space in front of it.

You also need to manually add the package the the +package/Config.in+ file.

docs/manual/adding-packages-python.txt This infrastructure applies to Python packages that use the standard · line 27

Quoted 5 lines
docs/manual/adding-packages-python.txt +5 -0
+
+When at the root of your buildroot directory just do :
+
+-----------------------
+./support/script/scanpypi.py foo bar -o package

s/.py//

Quoted 10 lines

+-----------------------

docs/manual/adding-packages-python.txt +2 -0
+
+This will generate packages +python-foo+ and +python-bar+ in the

package

docs/manual/adding-packages-python.txt +3 -0
+folder if they exist on https://pypi.python.org.
+
+Find the +external python modules+ menu and insert your package

inside.
+Keep in mind that the items inside a menu should be in alphabetical order.

I would also make an explicit reference here to how to use it with
BR2_EXTERNAL. I.e., using the -o flag.

docs/manual/adding-packages-python.txt This infrastructure applies to Python packages that use the standard · line 39

Quoted 5 lines
docs/manual/adding-packages-python.txt +5 -0
+
+Option +-h+ wil list the options available
+
+-----------------------
+./support/script/scanpypi.py -h

s/.py//

support/scripts/scanpypi line 1

Quoted 12 lines

+-----------------------

docs/manual/adding-packages-python.txt +1 -0
+
 [[python-package-tutorial]]
 
 ==== +python-package+ tutorial
support/scripts/scanpypi new file +1 -0
new file mode 100755
index 0000000..e98e8f8
@@ -0,0 +1,620 @@
+#!/usr/bin/python2

It would be nice to make it python3 compliant as well, but that can be done later.

Quoted 21 lines
support/scripts/scanpypi new file +21 -0
+"""
+Utility for building buildroot packages for existing pypi packages
+
+Any package built by scanpypi should be manually checked for
+errors.
+"""
+from __future__ import print_function
+import argparse
+import json
+import urllib2
+import sys
+import os
+import shutil
+import StringIO
+import tarfile
+import errno
+import hashlib
+import re
+import textwrap
+import tempfile
+import imp
Quoted 8 lines

support/scripts/scanpypi line 27

Quoted 5 lines
support/scripts/scanpypi new file +5 -0
+from functools import wraps
+
+
+# private global
+_calls = {}

I guess this could be converted into a class member of BuildrootPackage.

I'll do that.

Quoted 45 lines

Also, the name could be a bit better, e.g. setup_args.

support/scripts/scanpypi line 36

Quoted 9 lines
support/scripts/scanpypi new file +9 -0
+
+
+def setup_info(pkg_name):
+    """Get a package info from _calls
+
+    Keyword arguments:
+    pkg_name -- the name of the package
+    """
+    return _calls[pkg_name]

I still don't think it's worthwhile to make a function from this.

Quoted 7 lines
support/scripts/scanpypi new file +6 -0
+
+
+def setup_decorator(func, method):
+    """
+    Decorator for distutils.core.setup and setuptools.setup.
+    Puts the args of setup as a dict inside global private dict

_calls.

I finally understand what this does :-) Even better:

Puts the arguments with which setup is called as a dict ...

support/scripts/scanpypi line 52

Quoted 11 lines

+ Add key 'method' which should be either 'setuptools' or
'distutils'.

support/scripts/scanpypi new file +9 -0
+
+    Keyword arguments:
+    func -- either setuptools.setup or distutils.core.setup
+    method -- either 'setuptools' or 'distutils'
+    """
+
+    @wraps(func)
+    def closure(*args, **kwargs):
+        _calls[kwargs['name']] = kwargs

Perhaps document where this 'name' comes from. It's quite obvious if you're
familiar with distutils, but this wrapping is not easy to understand so it's
worthwhile to mention it.

Quoted 12 lines
Quoted 10 lines
support/scripts/scanpypi new file +8 -0
+        _calls[kwargs['name']]['method'] = method
+    return closure
+
+
+def find_file_upper_case(filenames, path='./'):
+    """
+    List generator:
+    Recursively find files that matches one of the specified

filenames.
+ Returns absolute path

No it doesn't, it returns a relative path starting with path.

Yes, that is what I meant to write, I don't know why I wrote that.

Quoted 18 lines
Quoted 5 lines
support/scripts/scanpypi new file +4 -0
+
+    Keyword arguments:
+    filenames -- List of filenames to be found
+    path -- Path to the directory to search

support/scripts/scanpypi line 75

Quoted 10 lines
support/scripts/scanpypi new file +10 -0
+    """
+    for root, dirs, files in os.walk(path):
+        for file in files:
+            if file.upper() in filenames:
+                yield (os.path.join(root, file))
+
+
+def pkg_buildroot_name(pkg_name):
+    """
+    Returns name to avoid troublesome characters.

No it doesn't.

Quoted 6 lines

Returns the buildroot package name for the PyPI package pkg_name.

In fact, the function should also prepend the python- prefix, because the
buildroot name starts with python-.

Actually, I have a question here, should I change . into - or into nothing ? I'm asking this because when looking into existing packages I noticed 'web.py' became 'python-webpy' but 'zope.interface' became 'python-zope-interface'

Quoted 12 lines
Quoted 12 lines
support/scripts/scanpypi new file +11 -0
+    Remove all non alphanumeric characters except -
+    Also lowers the name
+
+    Keyword arguments:
+    pkg_name -- String to rename
+    """
+    name = re.sub('[^\w-]', '', pkg_name.lower())
+    name = re.sub('^python-', '', name)
+    return name
+
+
Quoted 8 lines
Quoted 6 lines
support/scripts/scanpypi new file +5 -0
+# monkey patch
+import setuptools
+setuptools.setup = setup_decorator(setuptools.setup, 'setuptools')
+import distutils
+distutils.core.setup = setup_decorator(setuptools.setup,

'distutils')

This should go just below the definition of the decorator.

Yes, that would make things easier to understand :)

Quoted 26 lines
Quoted 26 lines
support/scripts/scanpypi new file +9 -0
+
+
+class SetupNotFound(Exception):
+    pass
+
+
+class BuildrootPackage():
+    """
+    This class's methods are not meant to be used individually

please use those

support/scripts/scanpypi new file +15 -0
+    in the correct order:
+    __init__
+
+    download_package
+
+    extract_package
+
+    load_module
+
+    get_requirements
+
+    create_package_mk
+
+    create_hash_file
+
Quoted 8 lines

support/scripts/scanpypi line 120

Quoted 5 lines
support/scripts/scanpypi new file +5 -0
+    create_config_in
+    """
+    def __init__(self, real_name, pkg_folder):
+        self.real_name = real_name
+        self.name = pkg_buildroot_name(self.real_name)

Perhaps to clarify the distinction, call it buildroot_name instead of just name.

Good idea, I just called it that way to reduce line lengths

Quoted 6 lines
Quoted 2 lines

+ self.pkg_dir = pkg_folder + '/python-' + self.name

It's good practice to set all the members you're going to create to None in
__init__, as a kind of declaration of what members are available.

Ok, I will do that

Quoted 32 lines

support/scripts/scanpypi line 127

Quoted 8 lines
support/scripts/scanpypi new file +4 -0
+
+    def find_setup(self, folder):
+        """
+        Search for setup.py file in an archive and returns the

path to this

support/scripts/scanpypi new file +2 -0
+        file
+        if it is found

Wrapping.

Quoted 12 lines
support/scripts/scanpypi new file +7 -0
+
+        Keyword arguments:
+        folder -- path to search in
+        """
+        filename = 'setup.py'
+        # Next return the first element of an iterable,
+        # here, it returns the path to the first file named

"setup.py"
+ # found in the python package or raises StopIteration if
not found

support/scripts/scanpypi new file +2 -0
+        self.setup_location = next(os.path.join(root, filename)
+                                   for root, dirs, files in
Quoted 2 lines

os.walk(folder)
+ if filename in files)

Is it just me or is setup_location not used anywhere? I anyway don't think this
is a good idea: if setup.py is not in the top dir, most likely other things in
this script will break.

Quoted 7 lines

So the only thing this function does, really, is checking if there is any
setup.py file in the folder. Which is later done again by calling imp.find_module, which will anyway raise an ImportError if the module isn't found.

IOW, remove this function.

This was needed when I wasn't using imp, now it's not I will remove it.

Quoted 11 lines

support/scripts/scanpypi line 142

Quoted 5 lines
support/scripts/scanpypi new file +4 -0
+
+    def fetch_package_info(self):
+        """
+        Fetch a package's metadata for the python package index

for -> from

support/scripts/scanpypi

Quoted 2 lines
support/scripts/scanpypi new file +2 -0
+        """
+        url = 'https://pypi.python.org/pypi/{pkg}/json'.format(
Quoted 6 lines

Already asign to metadata_url here.

support/scripts/scanpypi line 146

Quoted 2 lines
support/scripts/scanpypi new file +2 -0
+            pkg=self.real_name)
+        print('URL:', url)

This print is not needed.

That's a debug print I forgot to remove

Quoted 21 lines
Quoted 16 lines
support/scripts/scanpypi new file +4 -0
+        try:
+            pkg_json = urllib2.urlopen(url).read().decode()
+        except urllib2.HTTPError as error:
+            print('ERROR:', error.getcode(), error.msg,

file=sys.stderr)

support/scripts/scanpypi new file +2 -0
+            print('ERROR: Could not find package {pkg}.\n'
+                  'Check syntax inside the python package

index:\n'

support/scripts/scanpypi new file +6 -0
+                  'https://pypi.python.org/pypi/ '
+                  .format(pkg=self.real_name))
+            raise
+        except urllib2.URLError:
+            print('ERROR: Could not find package {pkg}.\n'
+                  'Check syntax inside the python package

index:\n'

support/scripts/scanpypi line 162

Quoted 4 lines
support/scripts/scanpypi new file +4 -0
+                  'https://pypi.python.org/pypi/ '
+                  .format(pkg=self.real_name))
+            raise
+        else:
Quoted 1 line

Else is not needed here since the exceptions are raise'd.

True, I forgot to remove it when I decided to raise the exception

Quoted 51 lines
Quoted 4 lines
support/scripts/scanpypi new file +3 -0
+            self.metadata_url = url
+            self.metadata = json.loads(pkg_json)
+            self.metadata_name = self.metadata['info']['name']

support/scripts/scanpypi line 175

Quoted 10 lines
support/scripts/scanpypi new file +10 -0
+
+    def download_package(self):
+        """
+        Download a package using metadata from pypi
+        """
+        try:
+            self.metadata['urls'][0]['filename']
+        except IndexError:
+            print(
+                'Non conventional package, ',

Non-conventional

Quoted 1 line

+ 'please check manually after creation')

manually -> carefully

Quoted 1 line

+ download_url = self.metadata['info']['download_url']

I think it would be simpler to, instead of repeating the download infra, just
emulate the needed metadata and use the same infra as below. So:

download_url = [{
'packagetype': 'tgz',
'url', self.metadata['info']['download_url'],
'md5_digest': None,
}]

support/scripts/scanpypi

Quoted 2 lines
support/scripts/scanpypi new file +2 -0
+            try:
+                self.download = urllib2.urlopen(download_url)

self.download is not used outside this function, so make it a local variable.
Or actually, drop it completely and read directly into self.as_string().

Quoted 9 lines
support/scripts/scanpypi new file +6 -0
+            except urllib2.HTTPError as http_error:
+                self.download = http_error
+            else:
+                self.as_string = self.download.read()
+                self.used_url = {'url': download_url,
+                                 'md5_digest':

hashlib.md5(self.as_string)
+

                                                     .hexdigest()}
Quoted 3 lines

I don't think it makes a lot os sense to compute the md5 here. Better set it to
None and not mention it in the .hash file.

True

Quoted 13 lines

support/scripts/scanpypi line 194

Quoted 12 lines

+ # In this case, we can't get the name of the
downloaded file
+ # from the pypi api, so we need to find it, this
should work
+ urlpath =
urllib2.urlparse.urlparse(self.download.url).path

support/scripts/scanpypi new file +5 -0
+                # urlparse().path give something like
+                # /path/to/file-version.tar.gz
+                # We use basename to remove /path/to
+                self.targz = os.path.basename(urlpath)
+                self.used_url['filename'] = self.targz
Quoted 3 lines

This magic is still needed to set download_url['filename'], but it doesn't need
to be downloaded yet to do that.

That's true, but if I download it first, I'm sure the filename is correct.

Quoted 7 lines

support/scripts/scanpypi line 197

Quoted 4 lines
support/scripts/scanpypi new file +3 -0
+        else:
+            for download_url in self.metadata['urls']:
+                if 'wheel' in download_url['packagetype']:

This could be zip or egg as well. Isn't there a positive match possible?

Yes, I'm changing that to avoid bdist packages rather than wheel. And I'm using zipfile as well in the next version

Quoted 10 lines

support/scripts/scanpypi

Quoted 3 lines
support/scripts/scanpypi new file +2 -0
+                    continue
+                try:

I think it's useful to print the download URL here, just before downloading. So
move the print ('Downloading package ...') here instead, and add the download
URL to it.

Quoted 8 lines

support/scripts/scanpypi

Quoted 4 lines

+ self.download =
urllib2.urlopen(download_url['url'])

support/scripts/scanpypi new file +2 -0
+                except urllib2.HTTPError as http_error:
+                    self.download = http_error

This is only used to raise it later, so you can just drop the entire try block
and let the exception leak up.

No it's not. Some package like pyzmq have more than one url I can use, (one with a zip and one with a tar.gz) I raise it only if all urls fail.

Quoted 10 lines

support/scripts/scanpypi line 208

Quoted 8 lines
support/scripts/scanpypi new file +4 -0
+                else:
+                    self.used_url = download_url
+                    self.as_string = self.download.read()
+                    self.md5_sum =

hashlib.md5(self.as_string).hexdigest()

support/scripts/scanpypi new file +2 -0
+                    self.targz = self.used_url['filename']
+                    if self.md5_sum == download_url['md5_digest']:

This should probably raise an exception if it doesn't match.

Again, I don't raise an exception because I want to try other urls if there are any left. I should raise one if the last on fails though.

Quoted 60 lines
Quoted 2 lines

+ break

The for loop needs an else: in case none of them can be downloaded.

support/scripts/scanpypi line 213

Quoted 4 lines
support/scripts/scanpypi new file +4 -0
+        if self.download.__class__ == urllib2.HTTPError:
+            raise self.download
+        else:
+            self.digest_method = 'sha256'

This is always sha256 so no point making a variable for it.

Quoted 2 lines

+ self.digest =
hashlib.sha256(self.as_string).hexdigest()

Minor nit: it's used only once, so I'd calculate it at the time that you use it.

Quoted 6 lines
support/scripts/scanpypi new file +4 -0
+
+    def extract_package(self, tmp_path):
+        """
+        Create folders used for extracting a package as file

object and extract
+ it

A bit too detailed...

Extract the package contents into a directory.

Quoted 4 lines
support/scripts/scanpypi new file +3 -0
+
+        Keyword arguments:
+        tmp_path -- folder where you want the package to be

extracted

s/folder/directory/g

Quoted 21 lines
support/scripts/scanpypi new file +9 -0
+        """
+        as_file = StringIO.StringIO(self.as_string)
+        with tarfile.open(fileobj=as_file) as as_tarfile:
+            tmp_pkg = os.path.join(tmp_path, self.name)
+            try:
+                os.makedirs(tmp_pkg)
+            except OSError as exception:
+                if exception.errno != errno.EEXIST:
+                    print("ERROR: ", exception.message,

file=sys.stderr)

support/scripts/scanpypi new file +2 -0
+                    return None, None
+                print('WARNING:', exception.message,

file=sys.stderr)

support/scripts/scanpypi new file +8 -0
+                print('Removing {pkg}...'.format(pkg=tmp_pkg))
+                shutil.rmtree(tmp_pkg)
+                os.makedirs(tmp_pkg)
+            as_tarfile.extractall(tmp_pkg)
+        try:
+            self.find_setup(tmp_pkg)
+        except StopIteration:
+            raise SetupNotFound('Could not find file setup.py for
Quoted 2 lines

package {}'
+ .format(self.real_name))

Quoted 6 lines

As I said before, this is not needed since it is covered by load_setup().

Quoted 1 line

+ tmp_extract = '{folder}/{name}-{version}'

Is this guaranteed to be the directory used by the package?

sdist and bdist create your archive, so the directory should allways have that name.

Quoted 15 lines
Quoted 12 lines
support/scripts/scanpypi new file +10 -0
+        self.tmp_extract = tmp_extract.format(
+            folder=tmp_pkg,
+            name=self.metadata_name,
+            version=self.metadata['info']['version'])
+
+    def load_setup(self):
+        """
+        Loads the corresponding setup and store its metadata
+        """
+        s_file, s_path, s_desc = imp.find_module('setup',

[self.tmp_extract])

support/scripts/scanpypi line 255

Quoted 2 lines
support/scripts/scanpypi new file +2 -0
+        imp.load_module('setup', s_file, s_path, s_desc)
+        self.setup_metadata = setup_info(self.metadata_name)
Quoted 3 lines

Is the name given in the metadata guaranteed to be the same as what is passed
to setup()? Especially considering case...

Case is the reason I use the metadata. The name in the metadata from PyPi is the same as the one from setup. As the PyPi name comes from the setup.py

Quoted 13 lines
Quoted 13 lines
support/scripts/scanpypi new file +4 -0
+
+    def get_requirements(self, pkg_folder, packages):
+        """
+        Retrieve dependencies from the metadata found in the

setup.py script of

support/scripts/scanpypi new file +7 -0
+        a pypi package.
+
+        Keyword Arguments:
+        pkg_folder -- location of the already created packages
+        packages  -- other packages to be built
+        """
+        if 'install_requires' not in self.setup_metadata:
Quoted 8 lines
Quoted 6 lines
support/scripts/scanpypi new file +5 -0
+            self.pkg_req = None
+            self.req_not_found = []
+            return
+        self.pkg_req = self.setup_metadata['install_requires']
+        self.pkg_req = [re.sub('([\w-]+)[><=]*.*', r'\1',

req).lower()

The .lower() is handled by pkg_buildroot_name so not needed here.

Correct.

Quoted 8 lines

support/scripts/scanpypi line 276

Quoted 6 lines
support/scripts/scanpypi new file +5 -0
+                        for req in self.pkg_req]
+        self.pkg_req = map(pkg_buildroot_name, self.pkg_req)
+        self.req_not_found = [
+            pkg for pkg in self.pkg_req
+            if 'python-{name}'.format(name=pkg)

pkg_buildroot_name already adds python- (or at least it should :-).

It will :).

Quoted 4 lines
Quoted 2 lines

+ not in os.listdir(pkg_folder)

Maybe better use "not os.path.isdir(pkg)".

Yes, that would be better

Quoted 10 lines

support/scripts/scanpypi line 280

Quoted 4 lines
support/scripts/scanpypi new file +3 -0
+        ]
+        self.req_not_found = [pkg for pkg in self.req_not_found
+                              if pkg not in packages]

Small nit: I'd make this function just return req_not_found instead of adding
it as a member, and move the set.difference logic up to the caller. Oh, and
perhaps use a set :-)

I will do that as well

Quoted 22 lines
Quoted 22 lines
support/scripts/scanpypi new file +16 -0
+
+    def __create_mk_header(self):
+        """
+        Create the header of the <package_name>.mk file
+        """
+        header = ['#' * 80 + '\n']
+        header.append('#\n')
+        header.append('# python-{name}\n'.format(name=self.name))
+        header.append('#\n')
+        header.append('#' * 80 + '\n')
+        header.append('\n')
+        return header
+
+    def __create_mk_download_info(self):
+        """
+        Create the lines refering to the download information of

the

support/scripts/scanpypi new file +4 -0
+        <package_name>.mk file
+        """
+        lines = []
+        version_line = 'PYTHON_{name}_VERSION =
Quoted 8 lines
Quoted 2 lines

{version}\n'.format(
+ name=self.name.upper().replace('-', '_'),

Like you have pkg_buildroot_name, you should also have something for the
conversion to uppercase. Becasue below, you forget the replace('-', '_'). Also
it's probably best to add the uppercase name as a member.

Nice suggestion, thanks :)

Quoted 7 lines
Quoted 2 lines

+ version=self.metadata['info']['version'])

I think it makes sense to add these things as direct members of
BuildrootPackage rather than going through metadata all the time:
version, url,
filename.

I will change that

Quoted 19 lines
Quoted 19 lines
support/scripts/scanpypi new file +13 -0
+        lines.append(version_line)
+
+        targz = self.targz.replace(
+            self.metadata['info']['version'],
+
           '$(PYTHON_{name}_VERSION)'.format(name=self.name.upper()))
+        targz_line = 'PYTHON_{name}_SOURCE = {filename}\n'.format(
+            name=self.name.upper(),
+            filename=targz)
+        lines.append(targz_line)
+
+        site_line = ('PYTHON_{name}_SITE = {url}'.format(
+            name=self.name.upper(),
+
           url=self.used_url['url'].replace(self.used_url['filename'],

'')))

support/scripts/scanpypi new file +2 -0
+        if 'sourceforge' in site_line:
+            site_line = ('PYTHON_{name}_SITE = {url}'.format(
Quoted 8 lines

support/scripts/scanpypi

Quoted 2 lines
support/scripts/scanpypi new file +2 -0
+                name=self.name.upper(),
+                url=self.used_url['url']))

This looks very weird... At least add an explanatory comment. And anyway, it's
better to do any fixups in a new variable (e.g. 'site') and format the line only
once.

Ok.

Quoted 61 lines
Quoted 15 lines
support/scripts/scanpypi new file +7 -0
+        site_line = site_line.rstrip('/') + '\n'
+        lines.append(site_line)
+        return lines
+
+    def __create_mk_setup(self):
+        """
+        Create the line refering to the setup method of the

package of the

support/scripts/scanpypi new file +5 -0
+        <package_name>.mk file
+
+        There are two things you can use to make an installer
+        for a python package: distutils or setuptools
+        distutils comes with python but does not support

dependencies.

support/scripts/scanpypi line 334

Quoted 3 lines
support/scripts/scanpypi new file +3 -0
+        distutils is mostly still there for backward support.
+        setuptools is what smart people use,
+        but it is not shipped with python :(

This comment is a bit redundant, but OK.

Quoted 40 lines
support/scripts/scanpypi new file +3 -0
+        """
+        lines = []
+        setup_type_line = 'PYTHON_{name}_SETUP_TYPE =

{method}\n'.format(

support/scripts/scanpypi new file +8 -0
+            name=self.name.upper(),
+            method=self.setup_metadata['method'])
+        lines.append(setup_type_line)
+        return lines
+
+    def __create_mk_license(self):
+        """
+        Create the lines referring to the package's license

informations of the

support/scripts/scanpypi new file +4 -0
+        <package_name>.mk file
+
+        The license is found using the metadata from pypi.
+        In the metadata, the license can be found either with

standard names in
+ the classifiers part or with naming from the packager in
the "License"

support/scripts/scanpypi new file +3 -0
+        part.
+
+        From the classifiers, the license is "translated"

according to
+ buildroot standards if need be (i.e. from Apache Software
License to

support/scripts/scanpypi new file +3 -0
+        Apache-2.0).
+
+        From the License part, we cannot guess what formatting the

packager
+ used. Hence, it is likely to be incorrect. (i.e. Apache
License 2.0

support/scripts/scanpypi new file +3 -0
+        instead of Apache-2.0).
+
+        The license's files are found by searching the package for

files named

support/scripts/scanpypi new file +2 -0
+        license or license.txt (case insensitive).
+        If more than one license file is found, the user is asked

to select
+ which ones he wants to use.

Quoted 8 lines

support/scripts/scanpypi line 368

Quoted 4 lines
support/scripts/scanpypi new file +4 -0
+        """
+        license_dict = {
+            'Apache Software License': 'Apache-2.0',
+            'BSD License': 'BSD',

We normally put BSD-2c, BSD-3c, etc. but it may be difficult to find that out
automatically.

Yes, I wanted to, but I have no way to know which version it is.

Quoted 12 lines
Quoted 11 lines
support/scripts/scanpypi new file +9 -0
+            'European Union Public Licence 1.0': 'EUPLv1.0',
+            'European Union Public Licence 1.1': 'EUPLv1.1',
+            "GNU General Public License": "GPL",
+            "GNU General Public License v2": "GPLv2",
+            "GNU General Public License v2 or later": "GPLv2+",
+            "GNU General Public License v3": "GPLv3",
+            "GNU General Public License v3 or later": "GPLv3+",
+            "GNU Lesser General Public License v2": "LGPLv2",
+            "GNU Lesser General Public License v2 or later":

"LGPLv2+",

Quoted 2 lines

That's actually LGPVLv2.1(+). LGPLv2 is the GNU Library General Public License.

Ok. Thanks

Quoted 9 lines
Quoted 6 lines
support/scripts/scanpypi new file +2 -0
+            "GNU Lesser General Public License v3": "LGPLv3",
+            "GNU Lesser General Public License v3 or later":

"LGPLv3+",
+ "GNU Library or Lesser General Public License":
"LGPL",

So this is probably LGPLv2 (there was no library/lesser version of v1).

Ok.

Quoted 20 lines

support/scripts/scanpypi line 387

Quoted 8 lines
support/scripts/scanpypi new file +7 -0
+            "ISC License": "ISC",
+            "MIT License": "MIT",
+            "Mozilla Public License 1.0": "MPL-1.0",
+            "Mozilla Public License 1.1": "MPL-1.1",
+            "Mozilla Public License 2.0": "MPL-2.0",
+            "Zope Public License": "ZPL"
+            }

Nice list!

Quoted 8 lines
support/scripts/scanpypi new file +3 -0
+        regexp = re.compile('^License :* *.* *:+ (.*)( \(.*\))?$')
+        classifiers_licenses = [regexp.sub(r"\1", lic)
+                                for lic in

self.metadata['info']['classifiers']

support/scripts/scanpypi new file +2 -0
+                                if regexp.match(lic)]
+        licenses = map(lambda x: license_dict[x] if x in

license_dict else x,
+ classifiers_licenses)

Quoted 3 lines

I would give an explicit warning if the license is not in license_dict, since
it most likely means it's wrong.

Ok.

Quoted 7 lines

support/scripts/scanpypi line 396

Quoted 5 lines
support/scripts/scanpypi new file +3 -0
+        lines = []
+        if not len(licenses):
+            licenses = [self.metadata['info']['license']]

This should probably go before the license_dict map.

This is a default case, as the license in the metadata is not subject to any kind of standard naming. It is very unlikely that it matches the classifiers syntax if the packager did not use classifiers... But I should give a warning in that case.

Quoted 11 lines

support/scripts/scanpypi line 403

Quoted 9 lines

+ license_line = 'PYTHON_{name}_LICENSE =
{license}\n'.format(

support/scripts/scanpypi new file +6 -0
+            name=self.name.upper(),
+            license=', '.join(licenses))
+        lines.append(license_line)
+        print('WARNING: License has been set to "{license}",'
+              ' please change it manually if necessary'.format(
+                  license=', '.join(licenses)))

So this warning is not necessary if it was found in the dict.

That is true.

Quoted 5 lines

support/scripts/scanpypi line 405

Quoted 3 lines
support/scripts/scanpypi new file +2 -0
+
+        filenames = ['LICENSE', 'LICENSE.TXT']

Shouldn't we also look for COPYING?

That would be a good idea.

Quoted 20 lines
Quoted 17 lines

+ license_files = list(find_file_upper_case(filenames,
self.tmp_extract))

support/scripts/scanpypi new file +8 -0
+        license_files = [license.replace(self.tmp_extract, '')[1:]
+                         for license in license_files]
+        if len(license_files) > 0:
+            if len(license_files) > 1:
+                print('More than one file found for license:',
+                      ', '.join(license_files))
+            license_files = [filename
+                             for index, filename in

enumerate(license_files)]

support/scripts/scanpypi new file +4 -0
+            license_file_line = ('PYTHON_{name}_LICENSE_FILES ='
+                                 ' {files}\n'.format(
+                                     name=self.name.upper(),
+                                     files='

'.join(license_files)))

support/scripts/scanpypi line 420

Quoted 2 lines
support/scripts/scanpypi new file +2 -0
+            lines.append(license_file_line)
+        elif len(license_files) == 0:
Quoted 1 line

Can len(license_files) be anything else than 0 at this point?

No it cannot.
Unless someone overrides len so that it returns something negative. But that would be a curious thing to do :).

Quoted 58 lines

support/scripts/scanpypi line 422

Quoted 3 lines
support/scripts/scanpypi new file +2 -0
+            print('WARNING: No license file found,'
+                  ' please specify it manually afterward')

afterwards

Quoted 52 lines
support/scripts/scanpypi new file +7 -0
+            license_file_line = '# No license file found\n'
+
+        return lines
+
+    def __create_mk_requirements(self):
+        """
+        Create the lines referring to the dependencies of the of

the

support/scripts/scanpypi new file +19 -0
+        <package_name>.mk file
+
+        Keyword Arguments:
+        pkg_name -- name of the package
+        pkg_req -- dependencies of the package
+        """
+        lines = []
+        python_pkg_req = ['python-{name}'.format(name=pkg)
+                          for pkg in self.pkg_req]
+        dependencies_line = ('PYTHON_{name}_DEPENDENCIES ='
+                             ' {reqs}\n'.format(
+                                 name=self.name.upper(),
+                                 reqs=' '.join(python_pkg_req)))
+        lines.append(dependencies_line)
+        return lines
+
+    def create_package_mk(self):
+        """
+        Create the lines corresponding to the <package_name>.mk

file

support/scripts/scanpypi new file +20 -0
+        """
+        pkg_mk = 'python-{name}.mk'.format(name=self.name)
+        path_to_mk = os.path.join(self.pkg_dir, pkg_mk)
+        print('Creating {file}...'.format(file=path_to_mk))
+        lines = self.__create_mk_header()
+        lines += self.__create_mk_download_info()
+        lines += self.__create_mk_setup()
+        lines += self.__create_mk_license()
+        if self.pkg_req:
+            lines += self.__create_mk_requirements()
+
+        lines.append('\n')
+        lines.append('$(eval $(python-package))')
+        lines.append('\n')
+        with open(path_to_mk, 'w') as mk_file:
+            mk_file.writelines(lines)
+
+    def create_hash_file(self):
+        """
+        Create the lines corresponding to the <package_name>.hash

files

support/scripts/scanpypi new file +3 -0
+        """
+        pkg_hash = 'python-{name}.hash'.format(name=self.name)
+        path_to_hash = os.path.join(self.pkg_dir, pkg_hash)
Quoted 8 lines
Quoted 5 lines

+ print('Creating
{filename}...'.format(filename=path_to_hash))

support/scripts/scanpypi new file +2 -0
+        lines = []
+        commented_line = '# md5 from {url}, {method} calculated by

scanpypi\n'

Actually, the md5 comes from the pypi/json url, so mention that one. So just:

Ok. Thanks

Quoted 13 lines

# From https://pypi.python.org/pypi/{pkg}/json

support/scripts/scanpypi line 481

Quoted 9 lines

+ commented_line =
commented_line.format(url=self.used_url['url'],

support/scripts/scanpypi new file +6 -0
+
                                              method=self.digest_method)
+        lines.append(commented_line)
+        hash_line = '{method}\t{digest}  {filename}\n'.format(
+            method='md5',
+            digest=self.used_url['md5_digest'],
+            filename=self.used_url['filename'])
Quoted 1 line

The md5 should only be added if there really was an md5.

Yes, I will test if it exists

Quoted 32 lines
Quoted 32 lines
support/scripts/scanpypi new file +22 -0
+        lines.append(hash_line)
+        hash_line = '{method}\t{digest}  {filename}\n'.format(
+            method=self.digest_method,
+            digest=self.digest,
+            filename=self.used_url['filename'])
+        lines.append(hash_line)
+
+        with open(path_to_hash, 'w') as hash_file:
+            hash_file.writelines(lines)
+
+    def create_config_in(self):
+        """
+        Creates the Config.in file of a package
+        """
+        path_to_config = os.path.join(self.pkg_dir, 'Config.in')
+        print('Creating {file}...'.format(file=path_to_config))
+        lines = []
+        config_line = 'config BR2_PACKAGE_PYTHON_{name}\n'.format(
+            name=self.name.upper())
+        lines.append(config_line)
+
+        bool_line = '\tbool

"python-{name}"\n'.format(name=self.name)

support/scripts/scanpypi new file +4 -0
+        lines.append(bool_line)
+        if self.pkg_req:
+            for dep in self.pkg_req:
+                dep_line = '\tselect

BR2_PACKAGE_PYTHON_{req}\n'.format(

support/scripts/scanpypi new file +3 -0
+                    req=dep.upper())
+                lines.append(dep_line)
+
Quoted 8 lines
Quoted 4 lines
support/scripts/scanpypi new file +3 -0
+        lines.append('\thelp\n')
+
+        help_lines =

textwrap.wrap(self.metadata['info']['summary'], 67)

I'm not really sure if we should use the summary or the description here... The
summary is _really_ short.

Well the description can be very long and it's full of \n i.e.: https://pypi.python.org/pypi/requests/json

Quoted 4 lines

Also, use textwrap.fill and add the initial_indent='\t ' argument, then all
the mangling below should no longer be necessary.

Oh, I did not see that argument. Thanks :)

Quoted 75 lines
Quoted 20 lines
support/scripts/scanpypi new file +18 -0
+        # \t + two spaces is 3 char long
+        help_lines.append('')
+        help_lines.append(self.metadata['info']['home_page'])
+        help_lines = map(lambda l: '\t  {}\n'.format(l)
+                                   if not l == '' else '\n',
+                         help_lines)
+        # help_lines = ['\t
 {line}\n'.format(line=line).replace('\t  \n','\n')
+        #              for line in help_lines]
+        lines += help_lines
+
+        with open(path_to_config, 'w') as config_file:
+            config_file.writelines(lines)
+
+
+def main():
+    # Building the parser
+    parser = argparse.ArgumentParser(
+        description="Creates buildroot packages from the metadata
Quoted 3 lines

of "
+ "an existing pypi(pip) packages and include it
"

PyPI

support/scripts/scanpypi line 535

Quoted 3 lines
support/scripts/scanpypi new file +3 -0
+                    "in menuconfig")
+    parser.add_argument("packages",
+                        help="list of packages to be made",

made -> created

support/scripts/scanpypi line 539

Quoted 4 lines
support/scripts/scanpypi new file +4 -0
+                        nargs='+')
+    parser.add_argument("-o", "--output",
+                        help="""
+                        Output directory for packages

Please explicitly mention the default in the help text.

Quoted 36 lines
support/scripts/scanpypi new file +14 -0
+                        """,
+                        default='./package')
+
+    args = parser.parse_args()
+    packages = list(set(args.packages))
+
+    # tmp_path is where we'll extract the files later
+    tmp_prefix = 'scanpypi-'
+    pkg_folder = args.output
+    tmp_path = tempfile.mkdtemp(prefix=tmp_prefix)
+    try:
+        for real_pkg_name in packages:
+            package = BuildrootPackage(real_pkg_name, pkg_folder)
+            print('buildroot package name for

{}:'.format(package.real_name),

support/scripts/scanpypi new file +3 -0
+                  package.name)
+            # First we download the package
+            # Most of the info we need can only be found inside

the package

support/scripts/scanpypi new file +13 -0
+            print('Package:', package.name)
+            print('Fetching package', package.real_name)
+            try:
+                package.fetch_package_info()
+            except (urllib2.URLError, urllib2.HTTPError):
+                continue
+
+            print('Downloading package {pkg}...'.format(
+                  pkg=package.metadata['info']['name']))
+            try:
+                package.download_package()
+            except urllib2.HTTPError as error:
+                print('Error: {code}

{reason}'.format(code=error.code,

support/scripts/scanpypi new file +2 -0
+
                                                     reason=error.reason))
+                print('Error downloading package :', package.name)
Quoted 8 lines

support/scripts/scanpypi line 577

Quoted 6 lines
support/scripts/scanpypi new file +6 -0
+                continue
+
+            # extract the tarball
+            package.extract_package(tmp_path)
+            print(package.metadata_name)
+            print(package.tmp_extract)

These two prints are redundant.

debug prints again.

Quoted 13 lines

support/scripts/scanpypi line 584

Quoted 9 lines
support/scripts/scanpypi new file +5 -0
+
+            # Loading the package install info from the package
+            package.load_setup()
+
+            # Package requirement are an argument of the setup

function

support/scripts/scanpypi new file +2 -0
+            package.get_requirements(pkg_folder, packages)
+            packages += package.req_not_found

Python doesn't allow updating a list while iterating over it. But I believe
there is something in itertools that supports that.

I thought so too. But strangely, it works.
Try flask with option -o on an empty directory, scanpypi will create Werkzeug, jinja2, itsdangerous and markupsafe as well. It is probably not the best way to do it, but it works fine.

Quoted 33 lines
Quoted 33 lines
support/scripts/scanpypi new file +2 -0
+            if package.req_not_found:
+                print('Added packages \'{pkgs}\' as dependencies

of {pkg}'
+ .format(pkgs=",
".join(package.req_not_found),

support/scripts/scanpypi new file +2 -0
+                              pkg=package.name))
+            print('Checking if package {name} already

exists...'.format(

support/scripts/scanpypi new file +6 -0
+                name=package.pkg_dir))
+            try:
+                os.makedirs(package.pkg_dir)
+            except OSError as exception:
+                if exception.errno != errno.EEXIST:
+                    print("ERROR: ", exception.message,

file=sys.stderr)

support/scripts/scanpypi new file +5 -0
+                    continue
+                print('Error: Package {name} already exists'
+                      .format(name=package.pkg_dir))
+                del_pkg = raw_input(
+                    'Do you want to delete existing package ?

[y/N]')

support/scripts/scanpypi new file +11 -0
+                if del_pkg.lower() == 'y':
+                    shutil.rmtree(package.pkg_dir)
+                    os.makedirs(package.pkg_dir)
+                else:
+                    continue
+            package.create_package_mk()
+
+            package.create_hash_file()
+
+            package.create_config_in()
+            print()
Quoted 8 lines

support/scripts/scanpypi line 617

Quoted 6 lines
support/scripts/scanpypi new file +6 -0
+            # printing an empty line for visual confort
+    except:
+        shutil.rmtree(tmp_path)
+        raise
+    else:
+        shutil.rmtree(tmp_path)

Isn't finally: supposed to do exactly what you're trying to do here?

Yes it is

Quoted 3 lines

Regards,
Arnout

Regards,
Denis

Quoted 16 lines

support/scripts/scanpypi line 620

Quoted 5 lines
support/scripts/scanpypi new file +3 -0
+
+if __name__ == "__main__":
+    main()

--

Arnout Vandecappelle                          arnout at mind be
Senior Embedded Software Architect            +32-16-286500
Essensium/Mind                                http://www.mind.be
G.Geenslaan 9, 3001 Leuven, Belgium           BE 872 984 063 RPR
Leuven
LinkedIn profile: http://www.linkedin.com/in/arnoutvandecappelle
GPG fingerprint:  7493 020B C7E3 8618 8DEC 222C 82EB F404 F9AC 0DDF
#4 lore raw

[Buildroot] Package naming convention [was: Re: [PATCH 1/2] scanpypi: new utility]

Hi all,

A topic for all to discuss here...

On 07/15/15 16:08, Denis Thulin wrote:
[snip]

Quoted 9 lines
Quoted 9 lines

support/scripts/scanpypi line 75

Quoted 3 lines
support/scripts/scanpypi new file +3 -0
+def pkg_buildroot_name(pkg_name):
+    """
+    Returns name to avoid troublesome characters.

No it doesn't.

Returns the buildroot package name for the PyPI package pkg_name.

Quoted 7 lines
Quoted 3 lines

In fact, the function should also prepend the python- prefix, because the
buildroot name starts with python-.

Actually, I have a question here, should I change . into - or into nothing ? I'm asking this because when looking into existing packages I noticed 'web.py' became 'python-webpy' but 'zope.interface' became 'python-zope-interface'

Good question, we indeed have no real convention of what the buildroot name is
for packages with weird characters.

Actually, we do: keep the upstream name as much as possible. So python-webpy,
for instance, keeps the upstream webpy name (note that the upstream name is webpy, even though the package is identified in PyPI as web.py. PyPI isn't very consistent either). For zope.interface, I actually see no reason not to keep the upstream name, so the package really should have been called python-zope.interface IMHO.

What do the others think?

BTW Thanks Denis for pointing this out.

Regards,
Arnout

Quoted 11 lines
Quoted 11 lines
Quoted 11 lines
support/scripts/scanpypi new file +9 -0
+    Remove all non alphanumeric characters except -
+    Also lowers the name
+
+    Keyword arguments:
+    pkg_name -- String to rename
+    """
+    name = re.sub('[^\w-]', '', pkg_name.lower())
+    name = re.sub('^python-', '', name)
+    return name

[snip]

Signature · 6 lines
Arnout Vandecappelle      arnout dot vandecappelle at essensium dot com
Senior Embedded Software Architect . . . . . . +32-478-010353 (mobile)
Essensium, Mind division . . . . . . . . . . . . . . http://www.mind.be
G.Geenslaan 9, 3001 Leuven, Belgium . . . . . BE 872 984 063 RPR Leuven
LinkedIn profile: http://www.linkedin.com/in/arnoutvandecappelle
GPG fingerprint:  7493 020B C7E3 8618 8DEC 222C 82EB F404 F9AC 0DDF
#5 lore raw

[Buildroot] Package naming convention [was: Re: [PATCH 1/2] scanpypi: new utility]

Dear Arnout Vandecappelle,

Quoted 11 lines · Arnout Vandecappelle

Good question, we indeed have no real convention of what the buildroot name is
for packages with weird characters.

Actually, we do: keep the upstream name as much as possible. So python-webpy,
for instance, keeps the upstream webpy name (note that the upstream name is webpy, even though the package is identified in PyPI as web.py. PyPI isn't very consistent either). For zope.interface, I actually see no reason not to keep the upstream name, so the package really should have been called python-zope.interface IMHO.

What do the others think?

I missed this message. On my side, I would prefer to not have a package named python-zope.interface. It should be named python-zope-interface instead. I.e all weird characters should be replaced by '-' in package names.

Thanks,

Thomas

Signature · 3 lines
Thomas Petazzoni, CTO, Free Electrons
Embedded Linux, Kernel and Android engineering
http://free-electrons.com
#6 lore raw

PATCH 2/2 python-robotframework: New package

In reply to Denis THULIN

A generic test automation framework written in python.

---
v0:
- Generated using python-package-generator.py
- Dependencies on python-zlib and python-pyexpat were added manually
- Modification of packages/Config.in done manually.
v1:
- fixed indentation error on dependencies (was 4 spaces instead of tab)
- Changed hash from md5 to sha256 (modification of scanpypi)
- Fixed typos (modification of scanpypi)
v2:
- Added the md5 hash from pypi
- Fixed typos

---

---
 package/Config.in                                        |  1 +
 package/python-robotframework/Config.in                  | 10 ++++++++++
 package/python-robotframework/python-robotframework.hash |  3 +++
 package/python-robotframework/python-robotframework.mk   | 14 ++++++++++++++
 4 files changed, 28 insertions(+)
 create mode 100644 package/python-robotframework/Config.in
 create mode 100644 package/python-robotframework/python-robotframework.hash
 create mode 100644 package/python-robotframework/python-robotframework.mk
package/Config.in +1 -0
index d9b0794..a14ba4c 100644
@@ -640,6 +640,7 @@ menu "external python modules"
 	source "package/python-pyxml/Config.in"
 	source "package/python-pyzmq/Config.in"
 	source "package/python-requests/Config.in"
+	source "package/python-robotframework/Config.in"
 	source "package/python-rtslib-fb/Config.in"
 	source "package/python-serial/Config.in"
 	source "package/python-setuptools/Config.in"
package/python-robotframework/Config.in new file +10 -0
new file mode 100644
index 0000000..9adda53
@@ -0,0 +1,10 @@
+config BR2_PACKAGE_PYTHON_ROBOTFRAMEWORK
+	bool "python-robotframework"
+	depends on BR2_PACKAGE_PYTHON
+	select BR2_PACKAGE_PYTHON_ZLIB
+	select BR2_PACKAGE_PYTHON_PYEXPAT
+	help
+	  A generic test automation framework
+	  Only works with Python 2
+
+	  http://robotframework.org
package/python-robotframework/python-robotframework.hash new file +3 -0
new file mode 100644
index 0000000..1ad9dc5
@@ -0,0 +1,3 @@
+# md5 from https://pypi.python.org/packages/source/r/robotframework/robotframework-2.9b2.tar.gz, sha256 calculated by scanpypi
+md5	723c30c827bb1f66e0fb032eff0c01cf  robotframework-2.9b2.tar.gz
+sha256	188db920cdedfd1b5e29adb284c410ab3711ebc3035f8f439d342d361fbda3d3  robotframework-2.9b2.tar.gz
package/python-robotframework/python-robotframework.mk new file +14 -0
new file mode 100644
index 0000000..1327a7e
@@ -0,0 +1,14 @@
+################################################################################
+#
+# python-robotframework
+#
+################################################################################
+
+PYTHON_ROBOTFRAMEWORK_VERSION = 2.9b2
+PYTHON_ROBOTFRAMEWORK_SOURCE = robotframework-$(PYTHON_ROBOTFRAMEWORK_VERSION).tar.gz
+PYTHON_ROBOTFRAMEWORK_SITE = https://pypi.python.org/packages/source/r/robotframework
+PYTHON_ROBOTFRAMEWORK_SETUP_TYPE = distutils
+PYTHON_ROBOTFRAMEWORK_LICENSE = Apache-2.0
+PYTHON_ROBOTFRAMEWORK_LICENSE_FILES = LICENSE.txt
+
+$(eval $(python-package))
Signature · 1 line
2.4.4
#7 lore raw

PATCH 2/2 python-robotframework: New package

Quoted 47 lines · Denis THULIN

A generic test automation framework written in python.

---
v0:
- Generated using python-package-generator.py
- Dependencies on python-zlib and python-pyexpat were added manually
- Modification of packages/Config.in done manually.
v1:
- fixed indentation error on dependencies (was 4 spaces instead of tab)
- Changed hash from md5 to sha256 (modification of scanpypi)
- Fixed typos (modification of scanpypi)
v2:
- Added the md5 hash from pypi
- Fixed typos

---

---
 package/Config.in                                        |  1 +
 package/python-robotframework/Config.in                  | 10 ++++++++++
 package/python-robotframework/python-robotframework.hash |  3 +++
 package/python-robotframework/python-robotframework.mk   | 14 ++++++++++++++
 4 files changed, 28 insertions(+)
 create mode 100644 package/python-robotframework/Config.in
 create mode 100644 package/python-robotframework/python-robotframework.hash
 create mode 100644 package/python-robotframework/python-robotframework.mk
package/Config.in +1 -0
index d9b0794..a14ba4c 100644
@@ -640,6 +640,7 @@ menu "external python modules"
 	source "package/python-pyxml/Config.in"
 	source "package/python-pyzmq/Config.in"
 	source "package/python-requests/Config.in"
+	source "package/python-robotframework/Config.in"
 	source "package/python-rtslib-fb/Config.in"
 	source "package/python-serial/Config.in"
 	source "package/python-setuptools/Config.in"
package/python-robotframework/Config.in new file +0 -0
new file mode 100644
index 0000000..9adda53

package/python-robotframework/Config.in line 8

Quoted 8 lines
package/python-robotframework/Config.in new file +8 -0
@@ -0,0 +1,10 @@
+config BR2_PACKAGE_PYTHON_ROBOTFRAMEWORK
+	bool "python-robotframework"
+	depends on BR2_PACKAGE_PYTHON
+	select BR2_PACKAGE_PYTHON_ZLIB
+	select BR2_PACKAGE_PYTHON_PYEXPAT
+	help
+	  A generic test automation framework
+	  Only works with Python 2

That 'only works' bit is more suitable for the commit log than for the help
text - it will anyway only be visible for Python 2.

package/python-robotframework/python-robotframework.hash line 3

Quoted 11 lines
package/python-robotframework/Config.in new file +2 -0
+
+	  http://robotframework.org
package/python-robotframework/python-robotframework.hash new file +3 -0
new file mode 100644
index 0000000..1ad9dc5
@@ -0,0 +1,3 @@
+# md5 from https://pypi.python.org/packages/source/r/robotframework/robotframework-2.9b2.tar.gz, sha256 calculated by scanpypi
+md5	723c30c827bb1f66e0fb032eff0c01cf  robotframework-2.9b2.tar.gz
+sha256	188db920cdedfd1b5e29adb284c410ab3711ebc3035f8f439d342d361fbda3d3  robotframework-2.9b2.tar.gz

So the hash file generated by scanpypi should look like this:

# From https://pypi.python.org/robotframework/json
md5 723c30c827bb1f66e0fb032eff0c01cf robotframework-2.9b2.tar.gz
# Locally calculated by scanpypi
sha256 188db920cdedfd1b5e29adb284c410ab3711ebc3035f8f439d342d361fbda3d3 robotframework-2.9b2.tar.gz

Regards,
Arnout

Quoted 21 lines
package/python-robotframework/python-robotframework.mk new file +14 -0
new file mode 100644
index 0000000..1327a7e
@@ -0,0 +1,14 @@
+################################################################################
+#
+# python-robotframework
+#
+################################################################################
+
+PYTHON_ROBOTFRAMEWORK_VERSION = 2.9b2
+PYTHON_ROBOTFRAMEWORK_SOURCE = robotframework-$(PYTHON_ROBOTFRAMEWORK_VERSION).tar.gz
+PYTHON_ROBOTFRAMEWORK_SITE = https://pypi.python.org/packages/source/r/robotframework
+PYTHON_ROBOTFRAMEWORK_SETUP_TYPE = distutils
+PYTHON_ROBOTFRAMEWORK_LICENSE = Apache-2.0
+PYTHON_ROBOTFRAMEWORK_LICENSE_FILES = LICENSE.txt
+
+$(eval $(python-package))
Signature · 6 lines
Arnout Vandecappelle                          arnout at mind be
Senior Embedded Software Architect            +32-16-286500
Essensium/Mind                                http://www.mind.be
G.Geenslaan 9, 3001 Leuven, Belgium           BE 872 984 063 RPR Leuven
LinkedIn profile: http://www.linkedin.com/in/arnoutvandecappelle
GPG fingerprint:  7493 020B C7E3 8618 8DEC 222C 82EB F404 F9AC 0DDF