Hide keyboard shortcuts

Hot-keys on this page

r m x p   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

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

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

96

97

98

99

100

101

102

103

104

105

106

107

108

109

110

111

112

113

114

115

116

117

118

119

120

121

122

123

124

125

126

127

128

129

130

131

132

133

134

135

136

137

138

139

140

141

142

143

144

145

146

147

148

149

150

151

# encoding: utf-8 

 

from functools import partial 

import os 

import sys 

 

import click 

 

 

print_ok = partial(click.secho) 

print_err = partial(click.secho, fg="red", bold=True) 

 

 

@click.group() 

def main(): 

"""the `pool` command provides sub commands to run imports from eawag data warehouse landing 

zones and supports test runs during development and integration of data sources 

""" 

pass 

 

 

def print_banner(function): 

def inner(*a, **kw): 

import inspect 

command_name = inspect.stack()[2].frame.f_locals.get("self").name 

print() 

click.secho("> {}".format(command_name), fg="blue") 

return function(*a, **kw) 

# click needs this to generate help texts from doc strings: 

inner.__doc__ = function.__doc__ 

return inner 

 

 

def inject_pdb(function): 

def inner(*a, **kw): 

try: 

return function(*a, **kw) 

except SystemExit: 

raise 

except: 

import pdb, traceback 

traceback.print_exc() 

if os.getenv("PDB"): 

type, value, tb = sys.exc_info() 

pdb.post_mortem(tb) 

else: 

print() 

print("set environment variable PDB to start debugger automatically.") 

sys.exit(1) 

# click needs this to generate help texts from doc strings: 

inner.__doc__ = function.__doc__ 

return inner 

 

 

@main.command("init-config") 

@click.option("--verbose", is_flag=True, help="dumps lots of output from interaction with db") 

@click.option('--force', "force_count", count=True, 

help="use this twice to overwrite existing config files") 

@click.argument("landing-zone-folder", type=str) 

@print_banner 

@inject_pdb 

def init_config(verbose, landing_zone_folder, force_count): 

"""initializes /etc/datapool/ folder with config files. 

 

landing_zone_folder must be a non-existing folder on the current machine. 

""" 

 

from .commands import init_config 

sys.exit(init_config(landing_zone_folder, force_count > 1, print_ok, print_err, verbose)) 

 

 

@main.command("check-config") 

@click.option("--verbose", is_flag=True, help="dumps lots of output from interaction with db") 

@print_banner 

@inject_pdb 

def check_config(verbose): 

"""checks if config file(s) in /etc/datapool are valid. 

""" 

from .commands import check_config 

sys.exit(check_config(print_ok, print_err, verbose)) 

 

 

@main.command("init-db") 

@click.option("--verbose", is_flag=True, help="dumps lots of output from interaction with db") 

@click.option('--force', "force_count", count=True, help="use this twice to overwrite existing db") 

@print_banner 

@inject_pdb 

def init_db(verbose, force_count): 

"""creates empty tables in operational database. Run check_config first to see if the configured 

data base settings are valid. 

""" 

from .commands import init_db 

sys.exit(init_db(force_count > 1, verbose, print_ok, print_err)) 

 

 

@main.command("start-develop") 

@click.option("--verbose", is_flag=True, help="dumps lots of output from interaction with db") 

@click.option('--force', 'force_count', count=True, help="use this twice to overwrite existing db") 

@click.argument("development-landing-zone-folder", type=str) 

@print_banner 

@inject_pdb 

def start_develop(development_landing_zone_folder, force_count, verbose): 

"""setup local db and local landing zone for adding new site / instrument / conversion script. 

this command will either setup an landing zone with example files or clone the operational 

landing zone. 

""" 

from .commands import start_develop 

reset = (force_count > 1) 

sys.exit(start_develop(development_landing_zone_folder, reset, verbose, print_ok, print_err)) 

 

 

@main.command("update-operational") 

@click.option("--verbose", is_flag=True, help="might dump lots of output") 

@click.option('--force', 'force_count', count=True, 

help="use this twice to overwrite existing landing zone in case of errors when" 

" checking") 

@click.argument("development-landing-zone-folder", type=str) 

@print_banner 

@inject_pdb 

def update_operational(development_landing_zone_folder, force_count, verbose): 

"""deploys local changes to operational landing zone. 

""" 

from .commands import update_operational 

overwrite = (force_count > 1) 

sys.exit(update_operational(development_landing_zone_folder, verbose, overwrite, 

print_ok, print_err)) 

 

 

@main.command("check-scripts") 

@click.option('--result-folder', type=str, default=None, help="provide target for results") 

@click.option("--verbose", is_flag=True, help="might dump lots of output") 

@click.argument("development-landing-zone-folder", type=str) 

@print_banner 

@inject_pdb 

def check_scripts(development_landing_zone_folder, result_folder, verbose): 

"""checks scripts and produced results in given landing zone. does not write to database. 

""" 

from .commands import check_scripts 

sys.exit(check_scripts(development_landing_zone_folder, result_folder, verbose, print_ok, print_err)) 

 

 

@main.command("check-yamls") 

@click.option("--verbose", is_flag=True, help="might dump lots of output") 

@click.argument("development-landing-zone-folder", type=str) 

@print_banner 

@inject_pdb 

def check_yamls(development_landing_zone_folder, verbose): 

"""checks scripts and produced results in given landing zone. does not write to database. 

""" 

from .commands import check_yamls 

sys.exit(check_yamls(development_landing_zone_folder, verbose, print_ok, print_err))