1#!/usr/local/bin/python3.8
2# -*- coding: utf-8 -*-
3
4# Copyright (c) 2004 - 2021 Detlev Offenbach <detlev@die-offenbachs.de>
5#
6# This is the install script for eric's translation files.
7
8"""
9Installation script for the eric IDE translation files.
10"""
11
12import sys
13import os
14import shutil
15import glob
16
17try:
18    from eric6config import getConfig
19except ImportError:
20    print("The eric IDE doesn't seem to be installed. Aborting.")
21    sys.exit(1)
22except SyntaxError:
23    # an incomplete or old config file was found
24    print("The eric IDE seems to be installed incompletely. Aborting.")
25    sys.exit(1)
26
27
28def getConfigDir():
29    """
30    Global function to get the name of the directory storing the config data.
31
32    @return directory name of the config dir (string)
33    """
34    cdn = ".eric6"
35
36    hp = os.path.join(os.path.expanduser("~"), cdn)
37    if not os.path.exists(hp):
38        os.mkdir(hp)
39    return hp
40
41# Define the globals.
42progName = None
43configDir = getConfigDir()
44privateInstall = False
45
46
47def usage(rcode=2):
48    """
49    Display a usage message and exit.
50
51    @param rcode return code passed back to the calling process (integer)
52    """
53    global progName, configDir
54
55    print()
56    print("Usage:")
57    print("    {0} [-hp]".format(progName))
58    print("where:")
59    print("    -h        display this help message")
60    print("    -p        install into the private area ({0})".format(
61        configDir))
62
63    sys.exit(rcode)
64
65
66def installTranslations():
67    """
68    Install the translation files into the right place.
69    """
70    global privateInstall, configDir
71
72    targetDir = (configDir if privateInstall
73                 else getConfig('ericTranslationsDir'))
74
75    try:
76        for fn in glob.glob(os.path.join('eric', 'eric6', 'i18n', '*.qm')):
77            shutil.copy2(fn, targetDir)
78            os.chmod(os.path.join(targetDir, os.path.basename(fn)), 0o644)
79    except OSError as msg:
80        sys.stderr.write(
81            'OSError: {0}\nTry install-i18n as root.\n'.format(msg))
82    except OSError as msg:
83        sys.stderr.write(
84            'OSError: {0}\nTry install-i18n with admin rights.\n'.format(msg))
85
86
87def main(argv):
88    """
89    The main function of the script.
90
91    @param argv list of command line arguments (list of strings)
92    """
93    import getopt
94
95    # Parse the command line.
96    global progName, privateInstall
97    progName = os.path.basename(argv[0])
98
99    try:
100        optlist, args = getopt.getopt(argv[1:], "hp")
101    except getopt.GetoptError:
102        usage()
103
104    global platBinDir
105
106    for opt, _arg in optlist:
107        if opt == "-h":
108            usage(0)
109        elif opt == "-p":
110            privateInstall = 1
111
112    installTranslations()
113
114if __name__ == "__main__":
115    try:
116        main(sys.argv)
117    except SystemExit:
118        raise
119    except Exception:
120        print("""An internal error occured.  Please report all the output of"""
121              """ the program,\nincluding the following traceback, to"""
122              """ eric-bugs@eric-ide.python-projects.org.\n""")
123        raise
124
125#
126# eflag: noqa = M801
127