PageRenderTime 11ms CodeModel.GetById 2ms app.highlight 6ms RepoModel.GetById 1ms app.codeStats 0ms

/silverlining/mgr-scripts/app-diff.py

https://bitbucket.org/ianb/silverlining/
Python | 56 lines | 54 code | 1 blank | 1 comment | 1 complexity | 9b3060ed7d4767af1b9554ae1a5a4001 MD5 | raw file
 1#!/usr/bin/env python
 2import sys
 3sys.path.insert(0, '/usr/local/share/silverlining/lib')
 4import os
 5import shutil
 6from optparse import OptionParser
 7from silversupport import appdata
 8from silversupport.shell import run
 9from silversupport.transfermethods import make_temp_name
10
11parser = OptionParser(
12    usage="%prog TMP_LOCATION HOSTNAME APP_NAME",
13    description="""\
14Diff between the files in TMP_LOCATION and the identified application.
15
16Will delete the files in TMP_LOCATION unless --keep-tmp is given
17""")
18parser.add_option(
19    '--instance-name',
20    metavar='NAME',
21    help="A specific instance name to select")
22parser.add_option(
23    '--keep-tmp',
24    action='store_true',
25    help="Keep the files in TMP_LOCATION instead of removing them after the diff")
26
27
28def main():
29    options, args = parser.parse_args()
30    tmp_location = args[0]
31    hostname = args[1]
32    app_name = args[2]
33    instance_name = options.instance_name
34    if not instance_name:
35        instance_name = appdata.instance_for_app_name(hostname, app_name)
36        if not instance_name:
37            print 'Error: No instance can be found under http://%s with the name %r' % (
38                hostname, app_name)
39    location = os.path.join('/var/www', instance_name)
40    tmp = make_temp_name('.diffing')
41    for magic_file in ['silver-env-variables.php']:
42        if os.path.exists(os.path.join(location, magic_file)):
43            shutil.copy(os.path.join(location, magic_file),
44                        os.path.join(tmp_location, magic_file))
45    os.mkdir(tmp)
46    os.symlink(location, os.path.join(tmp, instance_name))
47    os.symlink(tmp_location, os.path.join(tmp, 'local'))
48    run(['diff', '-u', instance_name, 'local'],
49        cwd=tmp)
50    shutil.rmtree(tmp)
51    if not options.keep_tmp:
52        shutil.rmtree(tmp_location)
53
54
55if __name__ == '__main__':
56    main()