1#!/usr/local/bin/python3.8
2
3import sys
4import os
5import argparse
6
7import gi
8gi.require_version('Gtk', '3.0')
9
10sys.path.append('/usr/local/share/cinnamon/cinnamon-settings/bin')
11from Spices import Spice_Harvester
12
13USAGE_DESCRIPTION = 'Installs an applet, desklet, extension, or theme from a local folder. Rather than just doing a shallow copy, it will also install translations, schema files (if present) and update the metadata with a timestamp for version comparison.'
14USAGE_EPILOG = 'This script is designed for developers to test their work. It is recommended that everyone else continue to use cinnamon-settings to install their spices as this script has no safeguards in place to prevent malicious code.'
15SPICE_TYPES = ['applet', 'desklet', 'extension', 'theme', 'search_provider']
16
17parser = argparse.ArgumentParser(description=USAGE_DESCRIPTION, epilog=USAGE_EPILOG)
18parser.add_argument('spice_type', type=str, choices=SPICE_TYPES, help='The type of spice you are installing.')
19parser.add_argument('path', type=str, default=os.getcwd(), nargs='?', help='The path from which you are installing. Default is the current directory.')
20
21args = parser.parse_args()
22
23source_dir = os.path.abspath(args.path)
24spice_type = args.spice_type
25
26if not os.path.exists(source_dir):
27    print('invalid directory - %s does not exist' % source_dir)
28    quit()
29
30uuid = os.path.basename(source_dir)
31if os.path.exists(os.path.join(source_dir, 'files', uuid)):
32    source_dir = os.path.join(source_dir, 'files', uuid)
33
34if spice_type == 'theme':
35    theme_file = os.path.join(source_dir, 'cinnamon/cinnamon.css')
36    if not os.path.exists(theme_file):
37        print('Invalid theme - missing file cinnamon.css')
38        quit()
39else:
40    # we always need metadata.json as well as the corresponding applet.js, deskelt.js, etc.
41    if not os.path.exists(os.path.join(source_dir, 'metadata.json')):
42        print('Invalid %s - metadata.json not found' % spice_type)
43        quit()
44    if not os.path.exists(os.path.join(source_dir, '%s.js' % spice_type)):
45        # multi-version xlets may not have it in the root directory, so check subdirectories as well
46        found = False
47        for name in os.listdir(source_dir):
48            if os.path.exists(os.path.join(source_dir, name, '%s.js' % spice_type)):
49                found = True
50                break
51        if not found:
52            print('Invalid %s - %s.js not found' % (spice_type, spice_type))
53            quit()
54
55spices = Spice_Harvester(spice_type)
56
57spices.install_from_folder(source_dir, uuid)
58