File: iinit.py

package info (click to toggle)
python-irodsclient 3.0.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 1,352 kB
  • sloc: python: 16,650; xml: 525; sh: 104; awk: 5; sql: 3; makefile: 3
file content (51 lines) | stat: -rw-r--r-- 1,274 bytes parent folder | download
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
from getpass import getpass
from irods.password_obfuscation import encode
import json
import os
import sys
from os import chmod
from os.path import expanduser, exists, join
from getopt import getopt


home_env_path = expanduser("~/.irods")
env_file_path = join(home_env_path, "irods_environment.json")
auth_file_path = join(home_env_path, ".irodsA")


def do_iinit(host, port, user, zone, password):
    if not exists(home_env_path):
        os.makedirs(home_env_path)
    else:
        raise RuntimeError("~/.irods already exists")

    with open(env_file_path, "w") as env_file:
        json.dump(
            {
                "irods_host": host,
                "irods_port": int(port),
                "irods_user_name": user,
                "irods_zone_name": zone,
            },
            env_file,
            indent=4,
        )
    with open(auth_file_path, "w") as auth_file:
        auth_file.write(encode(password))
    chmod(auth_file_path, 0o600)


def get_kv_pairs_from_cmdline(*args):
    arglist = list(args)
    while arglist:
        k = arglist.pop(0)
        v = arglist.pop(0)
        yield k, v


if __name__ == "__main__":
    import sys

    args = sys.argv[1:]
    dct = {k: v for k, v in get_kv_pairs_from_cmdline(*args)}
    do_iinit(**dct)