Add command that builds a new 'out-of-tree' project.
[debian/gnuradio] / gr-utils / src / python / create-gnuradio-out-of-tree-project
1 #!/usr/bin/env python
2
3 from optparse import OptionParser
4 import os
5 import re
6 import sys
7 import subprocess
8
9 tarball_url="http://gnuradio.org/releases/gnuradio/gr-howto-write-a-block-3.3git-651-g642252d8.tar.gz"
10
11 def open_tarball(tarball_name=None):
12     """Return a readable fileobject that references the tarball.
13     If tarball_name is None, download from the net
14     """
15     if tarball_name:
16         return open(tarball_name, 'r')
17     p = subprocess.Popen(["wget", "-O", "-", tarball_url],
18                          close_fds=True, stdout=subprocess.PIPE)
19     return p.stdout
20
21 def extract_and_rename_files(tarball, module_name):
22     #  Requires GNU tar.
23     tar_cmd = 'tar xz --strip-components=1 --transform="s/howto/%s/g"' % (module_name,)
24     p = subprocess.Popen(tar_cmd, shell=True, stdin=tarball, close_fds=True)
25     sts = os.waitpid(p.pid, 0)
26     return sts == 0
27
28 def main():
29     usage = "%prog: [options] new_module_name"
30     description="Create a prototype 'out of tree' GNU Radio project"
31     parser = OptionParser(usage=usage, description=description)
32     parser.add_option("-T", "--tarball", type="string", default=None,
33                       help="path to gr-howto-build-a-block gzipped tarball [default=<get from web>]")
34     (options, args) = parser.parse_args()
35     if len(args) != 1:
36         parser.print_help()
37         raise SystemExit,2
38     module_name = args[0]
39     if not re.match(r'[_a-z][_a-z0-9]*$', module_name):
40         sys.stderr.write("module_name '%s' may only contain [a-z0-9_]\n" % (module_name))
41         raise SystemExit, 1
42
43     # Ensure there's no file or directory called <module_name>
44     if os.path.exists(module_name):
45         sys.stderr.write("A file or directory named '%s' already exists\n" % (module_name,))
46         raise SystemExit, 1
47
48     os.mkdir(module_name)
49     os.chdir(module_name)
50
51     ok = extract_and_rename_files(open_tarball(options.tarball), module_name)
52
53     # rename file contents
54     upper_module_name = module_name.upper()
55     sed_cmd = 'sed -i -e "s/howto/%s/g" -e "s/HOWTO/%s/g"' % (module_name,
56                                                                  upper_module_name)
57     os.system('find . -type f -print0 | xargs -0 ' + sed_cmd)
58
59     sys.stdout.write("""
60 Project '%s' is now ready to build.
61
62   $ ./bootstrap
63   $ ./configure --prefix=/home/[user]/install
64   $ (make && make check && make install) 2>&1 | tee make.log
65
66 """ % (module_name,))
67
68 if __name__ == '__main__':
69     main()