summaryrefslogtreecommitdiff
path: root/lib/thandy/ThpCLI.py
blob: 52938d647ee9235686c450410ecef29941b75542 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
# Copyright 2011 The Tor Project, Inc.  See LICENSE for licensing information.

import sys
import os
import getopt
import tempfile
import time
import shutil
import zipfile

import thandy.keys
import thandy.util
import thandy.formats

json = thandy.util.importJSON()

def makethppackage(args):
    options, args = getopt.getopt(args, "", "keyid=")
    keyid = None
    scriptsPath = None
    for o,v in options:
        if o == "--keyid":
            keyid = v

    if len(args) < 3:
        usage()

    tmpPath = tempfile.mkdtemp(suffix=str(time.time()),
                               prefix="thp")

    print "Using temporary directory: %s" % tmpPath

    configFile = args[0]
    dataPath = args[1]
    thpPath = args[2]
    if len(args) > 3:
      scriptsPath = args[3]

    print "Generating package metadata..."
    metadata = thandy.formats.makeThpPackageObj(configFile, dataPath)

    print "Generating directory structure..."
    try:
        os.mkdir(os.path.join(tmpPath, "meta"));
    except Exception as e:
        print e
        thandy.util.deltree(tmpPath)
        sys.exit(1)

    thandy.util.replaceFile(os.path.join(tmpPath, "meta", "package.json"),
                            json.dumps(metadata, indent=3))

    shutil.copytree(dataPath, os.path.join(tmpPath, "content"))

    if "scripts" in metadata:
      try:
          os.mkdir(os.path.join(tmpPath, "meta", "scripts"))
      except Exception as e:
          print e
          thandy.util.deltree(tmpPath)
          sys.exit(1)
      for lang in metadata["scripts"]:
        for script in metadata['scripts'][lang]:
          shutil.copyfile(os.path.join(scriptsPath, script[0]),
                          os.path.join(tmpPath, "meta", "scripts", script[0]))

    thpFileName = "%s-%s.thp" % (metadata['package_name'],
                                 metadata['package_version'])

    print "Generating thp file in %s" % thpFileName
    thpFile = zipfile.ZipFile(os.path.join(thpPath, 
                                           thpFileName), "w")

    for file in metadata['manifest']:
        thpFile.write(os.path.join(tmpPath, "content", file['name']),
                      os.path.join("content", file['name']))

    if "scripts" in metadata:
      for lang in metadata["scripts"]:
        for script in metadata['scripts'][lang]:
          thpFile.write(os.path.join(tmpPath, "meta", "scripts", script[0]),
                        os.path.join("meta", "scripts", script[0]))

    thpFile.write(os.path.join(tmpPath, "meta", "package.json"),
                  os.path.join("meta", "package.json"))

    thpFile.close()

    print "All done. Cleaning tmp directory..."
    thandy.util.deltree(tmpPath)

def usage():
    print "Known commands:"
    print "  makethppackage config datapath thpPath scriptsPath"
    sys.exit(1)

def main():
    if len(sys.argv) < 2:
        usage()
    cmd = sys.argv[1]
    args = sys.argv[2:]
    if cmd in [ "makethppackage", ]:
        try:
            globals()[cmd](args)
        except thandy.BadPassword:
            print >>sys.stderr, "Password incorrect."
    else:
        usage()

if __name__ == '__main__':
    main()