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

from __future__ import absolute_import, division, print_function 

 

import json 

import os 

 

from appr.exception import Forbidden, PackageAlreadyExists 

from appr.models.blob_base import BlobBase 

from appr.models.channel_base import ChannelBase 

from appr.models.package_base import PackageBase 

 

 

class ApprDB(object): 

Channel = ChannelBase 

Package = PackageBase 

Blob = BlobBase 

 

@classmethod 

def restore_backup(cls, data): 

""" bulk add data in db """ 

i = 0 

size = len(data['packages']) 

for package_data in data['packages']: 

i += 1 

package = cls.Package(package_data['package'], package_data['release']) 

 

package.data = package_data 

package.blob = cls.Blob(package.package, package_data['blob']) 

try: 

package.save(False) 

 

# print '%s/%s restored: %s(%s) - %s' % (str(i), str(size), 

# package.package, package.release, package.media_type) 

except PackageAlreadyExists: 

pass 

# print '%s/%s existed: %s(%s) - %s' % (str(i), str(size), 

# package.package, package.release, package.media_type) 

 

for channel_name in package_data['channels']: 

channel = cls.Channel(channel_name, package.package) 

channel.add_release(package.release, cls.Package) 

# print "%s/%s restored-channel-release: %s, %s, %s" % (str(i), str(size), 

# channel.package, channel.name, package.release) 

 

i = 0 

size = len(data['channels']) 

for channel_data in data['channels']: 

i += 1 

channel = cls.Channel(channel_data['name'], channel_data['package']) 

channel.add_release(channel_data['current'], cls.Package) 

print("%s/%s restored-channel: %s" % (str(i), str(size), channel.name)) 

 

@classmethod 

def restore_backup_from_file(cls, filepath): 

""" bulk add data in db """ 

with open(filepath, 'rb') as f: 

data = json.load(f) 

return cls.restore_backup(data) 

 

@classmethod 

def reset_db(cls, force=False): 

""" clean the database """ 

if os.getenv("APPR_DB_ALLOW_RESET", "false") == "true" or force: 

raise NotImplementedError 

else: 

raise Forbidden("Reset DB is deactivated") 

 

@classmethod 

def backup(cls): 

data = {'packages': cls.Package.dump_all(cls.Blob), 'channels': cls.Channel.dump_all()} 

return data 

 

@classmethod 

def backup_to_file(cls, filepath): 

with open(filepath, 'wb') as f: 

json.dump(cls.backup(), f)