Debian Codegraph @

spark/utils/misc.py

raw
1 # -*- coding: utf-8 -*-
2 #
3 # Copyright (C) 2017-2018 Matthias Klumpp <[email protected]>
4 #
5 # Licensed under the GNU Lesser General Public License Version 3
6 #
7 # This program is free software: you can redistribute it and/or modify
8 # it under the terms of the GNU Lesser General Public License as published by
9 # the Free Software Foundation, either version 3 of the license, or
10 # (at your option) any later version.
11 #
12 # This software is distributed in the hope that it will be useful,
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 # GNU Lesser General Public License for more details.
16 #
17 # You should have received a copy of the GNU Lesser General Public License
18 # along with this software. If not, see <http://www.gnu.org/licenses/>.
19
20 import os
21 import json
22 import shutil
23 import tempfile
24 from contextlib import contextmanager
25
26 from spark.utils.command import safe_run
27
28
29 def sign(changes, gpg):
30 if changes.endswith(".dud"):
31 safe_run(['gpg', '-u', gpg, '--clearsign', changes])
32 os.rename("%s.asc" % (changes), changes)
33 else:
34 safe_run(['debsign', '-k', gpg, changes])
35
36
37 def upload(changes, gpg, host, config_file):
38 sign(changes, gpg)
39 return safe_run(['dput', '-c', config_file, host, changes])
40
41
42 @contextmanager
43 def cd(where):
44 ncwd = os.getcwd()
45 try:
46 yield os.chdir(where)
47 finally:
48 os.chdir(ncwd)
49
50
51 @contextmanager
52 def tdir():
53 fp = tempfile.mkdtemp()
54 try:
55 yield fp
56 finally:
57 shutil.rmtree(fp)
58
59
60 def to_compact_json(json_object, sort_keys=False):
61 '''
62 Create JSON representation of :json_object in the most
63 compact way possible.
64 '''
65 data = json.dumps(json_object, ensure_ascii=False, separators=(',', ':'), sort_keys=sort_keys)
66 return str(data)