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

 

import os 

import re 

from .. import GPI, logger, ConfigUtils, Utils 

from . import ROOTDIR 

from .BaseJobtreeReader import BaseJobtreeReader 

from PythonCK.decorators import lazy_property 

 

#------------------------------------------------------------------------------- 

# OFFLINE Reader 

#------------------------------------------------------------------------------- 

 

def read(path): 

""" 

Shortcut method to read all data in given external file. 

""" 

with open(path) as fin: 

return ''.join(fin.readlines()) 

 

class OfflineJobtreeReader(BaseJobtreeReader): 

""" 

Derived class to read the local jobtree file outside ganga interactive session. 

""" 

 

__slots__ = ('dat', '_lazy_jobs_all') 

 

def __init__(self, datpath=None): 

""" 

Args: 

datpath (string): Path to jobtree file. If None, it'll try to locate the 

appropriate location by itself ( guessing jobs.metadata ) 

 

>>> OfflineJobtreeReader('tests/res/jobtree_test') 

<GangaCK.Jobtree.OfflineJobtreeReader.OfflineJobtreeReader object at ...> 

 

""" 

super(OfflineJobtreeReader, self).__init__() 

if not datpath: 

datpath = os.path.join(ConfigUtils.dir_repository(), 'jobs.metadata/0xxx/0/data') 

self.dat = eval(re.findall(r'{.*}', read(datpath))[0]) 

 

def ls(self, cwd=ROOTDIR): 

""" 

 

>>> ojtr = OfflineJobtreeReader('tests/res/jobtree_test') 

 

>>> pprint(ojtr.ls()) 

{'folders': ['002 Z+BJet Analysis[CLOSED]', '013_Tau_Identification'], 

'jobs': []} 

 

>>> pprint(ojtr.ls('013_Tau_Identification')) 

{'folders': ['00_samples', '02_ditau_passthrough_v2', '03_Stripping23'], 

'jobs': []} 

 

>>> ojtr.ls('BADDIR') 

Traceback (most recent call last): 

... 

KeyError: 'Missing key: BADDIR' 

 

>>> pprint(ojtr.ls('013_Tau_Identification/00_samples')) 

{'folders': [], 'jobs': [2543, 2811, 2812, 2861, 2862]} 

 

## Ignore [CLOSED] 

 

>>> pprint(ojtr.ls('013_Tau_Identification/00_samples [CLOSED]')) 

{'folders': [], 'jobs': [2543, 2811, 2812, 2861, 2862]} 

 

>>> pprint(ojtr.ls('013_Tau_Identification[CLOSED]/00_samples')) 

{'folders': [], 'jobs': [2543, 2811, 2812, 2861, 2862]} 

 

>>> ojtr.jobs 

[197, 202, 232, 269, 892, 893, 894, 907, 2543, 2767, 2768, 2769, 2770, 2771, 2811, 2812, 2861, 2862] 

 

""" 

d2 = dict(self.dat[ROOTDIR]) # Make a copy 

for s in cwd.split('/'): 

if s and (s != ROOTDIR): 

d2 = Utils.dictget_ignore_closed(d2, s) 

list_jobs = [] 

list_dirs = [] 

for key,val in sorted(d2.iteritems()): 

if isinstance(val, dict): 

list_dirs.append(key) 

else: 

list_jobs.append(int(key)) 

return {'folders':list_dirs, 'jobs':list_jobs} 

 

def job(self, jid): 

""" 

 

>>> _ = getfixture('job197') 

>>> ojtr = OfflineJobtreeReader('tests/res/jobtree_test') 

>>> j = ojtr.job(197) 

>>> '[{0.id}] {0.name}'.format(j) 

'[197] Z02MuMuLine' 

 

""" 

if int(jid) not in self.jobs_all: 

return None 

try: 

return GPI.jobs(jid) 

except IOError, e: 

logger.warning(e) 

return None # Corrupt job 

 

@lazy_property 

def jobs_all(self): 

""" 

Use the combination of dirnames in repo+work. 

""" 

s1 = ConfigUtils.jids_repository() 

s2 = ConfigUtils.jids_workspace() 

return s1.union(s2) 

 

# searchpath = os.path.join(self.gdir, 'repository/khurewat/LocalXML/6.0/jobs/*/*') 

# return [ int(re.findall(r'.*/(\d+)', s)[0]) for s in sorted(glob(searchpath)) ]