Package: npm2deb
Version: 0.2.5-1
Severity: whishlist
Tags: patch
thanks

Hi Leo,

as you know, not every developer of npm modules does release a tarball for new versions. Some of them commit something to github and just put the new
version in their package.json.

With this patch npm2deb is able to work with such repositoies.
If you do something like (the -b is new):
 ./npm2deb.py  create -b hashish
it will create a different debian/watch file (just saying that you can not use uscan to get a new version), it will add a new target get-orig-source to debian/rules and it will create a debian/get-orig-source script that pulls the latest commit from the repository, creates a orig.tar and puts something like X.Y.Z~gitHHHH as version in debian/changelog.

What do you think of it?


Thanks!
 Thorsten
diff --git a/npm2deb/__init__.py b/npm2deb/__init__.py
index 1eabf0b..bb6d182 100644
--- a/npm2deb/__init__.py
+++ b/npm2deb/__init__.py
@@ -36,6 +36,7 @@ class Npm2Deb(object):
         self.debian_standards = STANDARDS_VERSION
         self.debian_debhelper = DEBHELPER
         self.noclean = False
+        self.build_from_git = False
         if args:
             if 'upstream_license' in args and args['upstream_license']:
                 self.upstream_license = args['upstream_license']
@@ -51,6 +52,8 @@ class Npm2Deb(object):
                 self.debian_debhelper = args['debhelper']
             if 'noclean' in args:
                 self.noclean = args['noclean']
+            if 'build_from_git' in args:
+                self.build_from_git = args['build_from_git']
 
         self.read_package_info()
         self.debian_name = 'node-%s' % self._debianize_name(self.name)
@@ -79,6 +82,7 @@ class Npm2Deb(object):
         self.create_dirs()
         self.create_examples()
         self.create_watch()
+        self.create_get_orig_source()
         self.create_manpages()
         if not self.noclean:
             self.clean()
@@ -103,6 +107,16 @@ class Npm2Deb(object):
             content = _os.path.normpath(self.json['man'])
             utils.create_debian_file('manpages', content)
 
+    def create_get_orig_source(self):
+        args = {}
+        args['debian_name'] = self.debian_name
+        args['url'] = self.upstream_repo_url
+        args['module'] = self.name
+        args['version'] = self.upstream_version
+        if self.build_from_git:
+            content = utils.get_get_orig_source('build_from_git') % args
+            utils.create_debian_file('get-orig-source', content)
+
     def create_watch(self):
         args = {}
         args['debian_name'] = self.debian_name
@@ -110,20 +124,24 @@ class Npm2Deb(object):
         args['url'] = self.upstream_repo_url
         args['module'] = self.name
         try:
-            if self.upstream_repo_url.find('github') >= 0:
-                content = utils.get_watch('github') % args
+            if self.build_from_git:
+                content = utils.get_watch('build_from_git') % args
+                utils.create_debian_file('watch', content)
             else:
-                # if not supported, got to fakeupstream
-                raise ValueError
-
-            utils.create_debian_file('watch', content)
-            # test watch with uscan, raise exception if status is not 0
-            info = _getstatusoutput('uscan --watchfile "debian/watch" '
-                                    '--package "{}" '
-                                    '--upstream-version 0 --no-download'
-                                    .format(self.debian_name))
-            if info[0] != 0:
-                raise ValueError
+                if self.upstream_repo_url.find('github') >= 0:
+                    content = utils.get_watch('github') % args
+                else:
+                    # if not supported, got to fakeupstream
+                    raise ValueError
+
+                utils.create_debian_file('watch', content)
+                # test watch with uscan, raise exception if status is not 0
+                info = _getstatusoutput('uscan --watchfile "debian/watch" '
+                                        '--package "{}" '
+                                        '--upstream-version 0 --no-download'
+                                        .format(self.debian_name))
+                if info[0] != 0:
+                    raise ValueError
 
         except ValueError:
             content = utils.get_watch('fakeupstream') % args
@@ -249,7 +267,10 @@ class Npm2Deb(object):
                 args['overrides'] += "override_dh_installchangelogs:\n" + \
                                      "\tdh_installchangelogs -k %s\n" % filename
                 break
-        content = utils.get_template('rules') % args
+        if self.build_from_git:
+            content = utils.get_rules('build_from_git') % args
+        else:
+            content = utils.get_rules('normal') % args
         utils.create_debian_file("rules", content)
         _os.system('chmod +x debian/rules')
 
diff --git a/npm2deb/scripts.py b/npm2deb/scripts.py
index 673d144..4ec6751 100644
--- a/npm2deb/scripts.py
+++ b/npm2deb/scripts.py
@@ -47,6 +47,9 @@ def main(argv=None):
         '--debian-license', default=None,
         help='license used for debian files [default: the same of upstream]')
     parser_create.add_argument(
+        '-b', '--build-from-git', action="store_true",
+        default=False, help='use git repository instead of released tarball (create get_orig_source target in debian/rules)')
+    parser_create.add_argument(
         'node_module',
         help='node module available via npm')
     parser_create.set_defaults(func=create)
@@ -285,8 +288,13 @@ def create(args):
 This is not a crystal ball, so please take a look at auto-generated files.\n
 You may want fix first these issues:\n""")
     _call('/bin/grep --color=auto FIX_ME -r %s/*' % debian_path, shell=True)
-    print ("\nUse uscan to get orig source files. Fix debian/watch and then run\
-            \n$ uscan --download-current-version\n")
+
+    if npm2deb.build_from_git:
+        print ("\nUse get_orig_source to get orig source files. Run\
+                \n$ make -f debian/rules get_orig_source\n")
+    else:
+        print ("\nUse uscan to get orig source files. Fix debian/watch and then run\
+                \n$ uscan --download-current-version\n")
 
     _show_mapper_warnings()
 
diff --git a/npm2deb/templates.py b/npm2deb/templates.py
index 9bda4ee..497eb02 100644
--- a/npm2deb/templates.py
+++ b/npm2deb/templates.py
@@ -31,7 +31,26 @@ Description: %(Description)s
  Node.js is an event-based server-side JavaScript engine.
 """
 
-RULES = """#!/usr/bin/make -f
+RULES = {}
+
+RULES['normal'] = """#!/usr/bin/make -f
+# -*- makefile -*-
+
+# Uncomment this to turn on verbose mode.
+#export DH_VERBOSE=1
+
+%%:
+	dh $@
+
+#override_dh_auto_build:
+
+#override_dh_auto_test:
+
+%(overrides)s
+
+"""
+
+RULES['build_from_git'] = """#!/usr/bin/make -f
 # -*- makefile -*-
 
 # Uncomment this to turn on verbose mode.
@@ -46,6 +65,8 @@ RULES = """#!/usr/bin/make -f
 
 %(overrides)s
 
+get-orig-source:
+	bash debian/get-orig-source
 """
 
 COPYRIGHT = """Format: http://www.debian.org/doc/packaging-manuals/copyright-format/1.0/
@@ -305,6 +326,73 @@ LICENSES['ISC'] = """ISC
  OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
 """
 
+
+GET_ORIG_SOURCE = {}
+
+GET_ORIG_SOURCE['bad_value'] = """#!/bin/bash
+echo "there is something wrong"
+"""
+
+GET_ORIG_SOURCE['build_from_git'] = """#!/bin/bash
+echo "I: we are building from git"
+# check stuff
+if [ ! -d debian ]; then
+  echo "E: wrong directory, no subdir debian found"
+  exit 1
+fi
+
+#######################################################################
+# init stuff
+currentdir=`pwd`
+version="%(version)s"
+source="%(debian_name)s"
+gitrepo="%(url)s"
+tmpdir=`mktemp -d`
+
+echo "I: upstream version: $version"
+echo "I: package name: $source"
+echo "I: git repository: $gitrepo"
+echo "I: temporary dir: $tmpdir"
+
+#######################################################################
+# get stuff from upstream
+cd $tmpdir
+git clone $gitrepo gitdir
+
+#######################################################################
+# get info from repository
+cd gitdir
+echo "I: gitdir: `pwd`"
+githash=`git show --pretty=format:%%h|head -1`
+echo "I: git hash: $githash"
+
+#######################################################################
+# create orig tarball
+# no release of that version yet, so ~
+origname="${source}_${version}~git${githash}"
+origtar="$origname.orig.tar.xz"
+echo "I: origname: $origname"
+echo "I: origtar:  $origtar"
+cd ..
+mv gitdir $origname
+tar --exclude-vcs -Jcf $origtar $origname
+
+# move origtar to correct place for dpkg-buildpackage
+cd $currentdir
+mv $tmpdir/$origtar ..
+
+# change entry in debian/changelog
+dch -b --newversion ${version}~git${githash}-1 "get git snapshot of hash $githash"
+
+# extract origtar such that everything is ready for dpkg-buildpackage
+tar -Jxf ../$origtar --strip 1
+
+#######################################################################
+# cleanup
+rm -rf $tmpdir
+
+"""
+
 WATCH = {}
 
 WATCH['github'] = """version=3
@@ -325,6 +413,11 @@ filenamemangle=s/.*=// \\
  http://qa.debian.org/cgi-bin/fakeupstream.cgi?upstream=npmjs/%(module)s .*=%(module)s-(\d.*)\.(?:tgz|tar\.(?:gz|bz2|xz))
 """
 
+WATCH['build_from_git'] = """version=3
+# we build from git repository
+# there is no need to use the watch file
+"""
+
 TESTS = {}
 
 TESTS['control'] = """Tests: require
diff --git a/npm2deb/utils.py b/npm2deb/utils.py
index a2daf65..9ebc478 100644
--- a/npm2deb/utils.py
+++ b/npm2deb/utils.py
@@ -35,11 +35,27 @@ def get_template(filename):
     return result
 
 
+def get_rules(which):
+    if which == 'build_from_git':
+       return _templates.RULES['build_from_git']
+    else:
+       return _templates.RULES['normal']
+
+
+def get_get_orig_source(which):
+    if which == 'build_from_git':
+       return _templates.GET_ORIG_SOURCE['build_from_git']
+    else:
+       return _templates.GET_ORIG_SOURCE['bad_value']
+
+
 def get_watch(which):
     if which == 'github':
         return _templates.WATCH['github']
     elif which == 'bitbucket':
         return _templates.WATCH['bitbucket']
+    elif which == 'build_from_git':
+        return _templates.WATCH['build_from_git']
     else:
         return _templates.WATCH['fakeupstream']
 

Reply via email to